aboutsummaryrefslogtreecommitdiff
path: root/tests/malloc
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2024-08-21 23:31:48 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2024-08-21 23:31:48 +0300
commit97ffab0a8472981b927d0f4e81bb72f6ecd69b86 (patch)
treec8a36352d599dc9477a6e647878d5e6191b4e98f /tests/malloc
parentab011165cd440b5535d39febd8118e3e26b64b57 (diff)
downloadkmi-97ffab0a8472981b927d0f4e81bb72f6ecd69b86.tar.gz
kmi-97ffab0a8472981b927d0f4e81bb72f6ecd69b86.zip
add malloc test
Diffstat (limited to 'tests/malloc')
-rw-r--r--tests/malloc/check.mk4
-rw-r--r--tests/malloc/init.c42
-rw-r--r--tests/malloc/source.mk2
3 files changed, 48 insertions, 0 deletions
diff --git a/tests/malloc/check.mk b/tests/malloc/check.mk
new file mode 100644
index 0000000..3a178cb
--- /dev/null
+++ b/tests/malloc/check.mk
@@ -0,0 +1,4 @@
+malloc: do-malloc
+ @grep 'BUG' reports/malloc/log \
+ && echo 'BUG' > reports/malloc/OK \
+ || tail -n1 reports/malloc/log | tr -d '\r' > reports/malloc/OK
diff --git a/tests/malloc/init.c b/tests/malloc/init.c
new file mode 100644
index 0000000..5048e82
--- /dev/null
+++ b/tests/malloc/init.c
@@ -0,0 +1,42 @@
+#include <common/test.h>
+
+START(pid, tid, d0, d1, d2, d3)
+{
+ UNUSED(pid);
+ UNUSED(tid);
+ UNUSED(d0);
+ UNUSED(d1);
+ UNUSED(d2);
+ UNUSED(d3);
+
+ printf("allocating space for pointers...\n");
+ /* 12 MiB / 4K */
+ void **allocs = sys_req_mem(200 * sizeof(void *), VM_W | VM_R);
+ check(allocs, "initial allocation failed\n");
+
+ printf("allocating memory until we run out...\n");
+
+ long i = 0;
+ while (1) {
+ char *p = sys_req_mem(1, VM_W | VM_R);
+ if (!p)
+ break;
+
+ /* check that we actually got a page */
+ *p = 'a';
+ allocs[i++] = p;
+ }
+
+ printf("ran out of memory, freeing...\n");
+ for (; i >= 0; --i)
+ sys_free_mem((uintptr_t)allocs[i]);
+
+ printf("freed all memory, doing one last allocation to check we're good...\n");
+ /* not super exhaustive but would hopefully go haywire if the page
+ * allocator got corrupted or something */
+ char *p = sys_req_mem(1, VM_W | VM_R);
+ *p = 'b';
+ sys_free_mem((uintptr_t)p);
+ sys_free_mem((uintptr_t)allocs);
+ ok();
+}
diff --git a/tests/malloc/source.mk b/tests/malloc/source.mk
new file mode 100644
index 0000000..9498682
--- /dev/null
+++ b/tests/malloc/source.mk
@@ -0,0 +1,2 @@
+DO != ./scripts/gen-prog -n malloc -p init init.c
+DO != ./scripts/gen-simple -n malloc -p init