Add test for alloc and add asert statement
This commit is contained in:
Mathieu Maret 2019-04-11 23:08:59 +02:00
parent 7def058cf4
commit e03f0c0d78
5 changed files with 66 additions and 34 deletions

View File

@ -12,7 +12,7 @@
struct slabDesc *slubArray[SLUB_SIZE]; struct slabDesc *slubArray[SLUB_SIZE];
int initSlabDesc(struct slabDesc **desc, size_t size); int initSlabDesc(struct slabDesc **desc, size_t size);
int formatPage(struct slabDesc *desc, size_t size); static int formatPage(struct slabDesc *desc, size_t size);
int initSlab(void) int initSlab(void)
{ {
@ -44,7 +44,7 @@ int initSlabDesc(struct slabDesc **desc, size_t size)
return formatPage((*desc), size); return formatPage((*desc), size);
} }
int formatPage(struct slabDesc *desc, size_t size) static int formatPage(struct slabDesc *desc, size_t size)
{ {
char *cur = (char *)desc + sizeof(struct slabDesc); char *cur = (char *)desc + sizeof(struct slabDesc);
ulong i; ulong i;

21
core/assert.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "stack.h"
#define assert(p) do { \
if (!(p)) { \
printf("BUG at %s:%d assert(%s)\n", \
__FILE__, __LINE__, #p); \
printStackTrace(5); \
while(1){} \
} \
} while (0)
#define assertmsg(p, ...) do { \
if (!(p)) { \
printf("BUG at %s:%d assert(%s)\n", \
__FILE__, __LINE__, #p); \
printf(__VA_ARGS__); \
printStackTrace(5); \
while(1){} \
} \
} while (0)

View File

@ -81,10 +81,10 @@ void kmain(unsigned long magic, unsigned long addr)
printf("Setting up Serial link (115200)\n"); printf("Setting up Serial link (115200)\n");
serialSetup(115200); serialSetup(115200);
initSlab();
#ifdef RUN_TEST #ifdef RUN_TEST
run_test(); run_test();
#endif #endif
initSlab();
int count = 0; int count = 0;
while (1) { while (1) {

View File

@ -55,3 +55,6 @@ void cursorMove(int x, int y);
#define pr_devel(fmt, ...) \ #define pr_devel(fmt, ...) \
no_printf(pr_fmt(fmt), ##__VA_ARGS__) no_printf(pr_fmt(fmt), ##__VA_ARGS__)
#endif #endif
#define pr_info(fmt, ...) \
printf(pr_fmt(fmt), ##__VA_ARGS__)

View File

@ -1,5 +1,7 @@
#include "list.h" #include "alloc.h"
#include "assert.h"
#include "klibc.h" #include "klibc.h"
#include "list.h"
#include "mem.h" #include "mem.h"
#include "paging.h" #include "paging.h"
#include "serial.h" #include "serial.h"
@ -24,21 +26,31 @@ void testPhymem(void)
printf("%d pages allocated\n", allocCount); printf("%d pages allocated\n", allocCount);
while ((page = list_pop_head(allocated_page_list)) != NULL) { while ((page = list_pop_head(allocated_page_list)) != NULL) {
if (page->phy_addr != (ulong)freeCount) { assertmsg(page->phy_addr == (ulong)freeCount, "page %d modified", page);
printf("Error page %d modified\n", (ulong)page); assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n",
} (ulong)page);
if (unrefPhyPage((ulong)page) < 0) {
printf("Failed to free page %d\n", (ulong)page);
}
freeCount++; freeCount++;
} }
printf("%d pages freed\n", freeCount); printf("%d pages freed\n", freeCount);
if ((page = (struct mem_desc *)allocPhyPage()) == NULL) { assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n");
printf("Error ! Cannot allocate memory\n");
} else {
unrefPhyPage((ulong)page); unrefPhyPage((ulong)page);
} }
static void testAlloc(void)
{
assert(malloc(1));
assert(malloc(2));
assert(malloc(3));
assert(malloc(4));
void *malloc1 = malloc(sizeof(void *));
void *malloc2 = malloc(sizeof(void *));
assert((char *)malloc2 == ((char *)malloc1 + sizeof(void *)));
assert(malloc(1024));
assert(malloc(1024));
assert(malloc(1024));
assert(malloc(1024));
assert(malloc(1024));
} }
static void testPaging(void) static void testPaging(void)
@ -52,10 +64,8 @@ static void testPaging(void)
int freeCount = 0; int freeCount = 0;
while ((page = (struct mem_desc *)allocPhyPage()) != NULL) { while ((page = (struct mem_desc *)allocPhyPage()) != NULL) {
if (pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE)){ assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0,
printf("Fail to map page %d\n", allocCount); "Fail to map page %d\n", allocCount);
break;
}
page->phy_addr = allocCount; page->phy_addr = allocCount;
allocCount++; allocCount++;
list_add_tail(allocated_page_list, page); list_add_tail(allocated_page_list, page);
@ -63,33 +73,30 @@ static void testPaging(void)
printf("%d pages allocated\n", allocCount); printf("%d pages allocated\n", allocCount);
while ((page = list_pop_head(allocated_page_list)) != NULL) { while ((page = list_pop_head(allocated_page_list)) != NULL) {
if (page->phy_addr != (ulong)freeCount) { assertmsg(page->phy_addr == (ulong)freeCount, "page modified");
printf("Error page %d modified\n", (ulong)page); assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n",
} (ulong)page);
if (unrefPhyPage((ulong)page) < 0) {
printf("Failed to free page %d\n", (ulong)page);
}
pageUnmap((vaddr_t)page); pageUnmap((vaddr_t)page);
freeCount++; freeCount++;
} }
printf("%d pages freed\n", freeCount); printf("%d pages freed\n", freeCount);
if ((page = (struct mem_desc *)allocPhyPage()) == NULL) { assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n");
printf("Error ! Cannot allocate memory\n");
} else {
unrefPhyPage((ulong)page); unrefPhyPage((ulong)page);
} }
}
static void test_backtrace_2(int a, int b){ static void test_backtrace_2(int a, int b)
{
printStackTrace(a + b); printStackTrace(a + b);
} }
static void test_backtrace_1(int a){ static void test_backtrace_1(int a)
{
test_backtrace_2(a, 3); test_backtrace_2(a, 3);
} }
void test_backtrace(){ void test_backtrace()
{
test_backtrace_1(2); test_backtrace_1(2);
} }
@ -102,6 +109,7 @@ void run_test(void)
serialWrite('l'); serialWrite('l');
serialWrite('l'); serialWrite('l');
serialWrite('o'); serialWrite('o');
testAlloc();
printf("Testing backtrace\n"); printf("Testing backtrace\n");
test_backtrace(); test_backtrace();
} }