From c1d3ac750b8483c9a942d2fb5d5b7245d014e905 Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Sat, 13 Jul 2024 23:37:37 +0200
Subject: kernel/malloc: limit the maximum allocation size to under a page

This will likely be changed back, but for the time being it will let me
implement a better allocator without too much effort.
---
 src/kernel/malloc.c                 | 5 +++++
 src/kernel/malloc.h                 | 6 +++++-
 src/kernel/syscalls.c               | 4 +++-
 src/libk/include/assert.h           | 2 ++
 src/libk/include/camellia/execbuf.h | 2 +-
 5 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/kernel/malloc.c b/src/kernel/malloc.c
index 8ba00dc..ba1ab42 100644
--- a/src/kernel/malloc.c
+++ b/src/kernel/malloc.c
@@ -116,6 +116,7 @@ mem_debugprint(void)
 void
 *page_alloc(size_t pages)
 {
+	assert(pages == 1); /* not using that assertion... yet */
 	/* i do realize how painfully slow this is */
 	size_t streak = 0;
 	for (size_t i = pbitmap_firstfree; i < pbitmap_len * 8; i++) {
@@ -177,7 +178,11 @@ void
 	Allocation *hdr;
 	void *addr;
 
+	if (KMALLOC_MAX < len) {
+		panic_invalid_state();
+	}
 	len += sizeof(Allocation);
+	assert(len <= PAGE_SIZE);
 	hdr = page_alloc(page_amt(len));
 	hdr->magic = MALLOC_MAGIC;
 	hdr->len = len;
diff --git a/src/kernel/malloc.h b/src/kernel/malloc.h
index 03934de..3469f5f 100644
--- a/src/kernel/malloc.h
+++ b/src/kernel/malloc.h
@@ -3,12 +3,16 @@
 #include <shared/mem.h>
 #include <stddef.h>
 
+/* This seems to be fine for now, and it means that i don't need to concern
+ * myself with allocating contiguous pages for now, which is way easier to do
+ * well. */
+#define KMALLOC_MAX 2048
+
 void mem_init(void *memtop);
 void mem_reserve(void *addr, size_t len);
 void mem_debugprint(void);
 
 // allocates `pages` consecutive pages
-// TODO deprecate
 void *page_alloc(size_t pages);
 // zeroes the allocated pages
 void *page_zalloc(size_t pages);
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index 5771b6a..7fcbd2a 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -62,6 +62,7 @@ hid_t _sys_open(const char __user *path, long len, int flags) {
 
 	if (flags & ~(OPEN_RW | OPEN_CREATE)) SYSCALL_RETURN(-ENOSYS);
 
+	static_assert(PATH_MAX <= KMALLOC_MAX);
 	if (PATH_MAX < len)
 		SYSCALL_RETURN(-ENAMETOOLONG);
 	/* Doesn't check for free handles. Another thread could use up all
@@ -461,7 +462,8 @@ hid_t _sys_getnull(int flags) {
 
 long _sys_execbuf(void __user *ubuf, size_t len) {
 	if (len == 0) SYSCALL_RETURN(0);
-	if (len > EXECBUF_MAX_LEN)
+	static_assert(EXECBUF_MAX_LEN <= KMALLOC_MAX);
+	if (EXECBUF_MAX_LEN < len)
 		SYSCALL_RETURN(-1);
 	if (proc_cur->execbuf.buf)
 		SYSCALL_RETURN(-1);
diff --git a/src/libk/include/assert.h b/src/libk/include/assert.h
index affe1dc..c85d096 100644
--- a/src/libk/include/assert.h
+++ b/src/libk/include/assert.h
@@ -9,4 +9,6 @@
 #define assert(stmt) (0)
 #endif
 
+#define static_assert _Static_assert
+
 _Noreturn void __badassert(const char *func, const char *file, int line);
diff --git a/src/libk/include/camellia/execbuf.h b/src/libk/include/camellia/execbuf.h
index de7ae3b..8ffabad 100644
--- a/src/libk/include/camellia/execbuf.h
+++ b/src/libk/include/camellia/execbuf.h
@@ -1,7 +1,7 @@
 #pragma once
 /* the instruction format is bound to change, atm it's extremely inefficient */
 
-#define EXECBUF_MAX_LEN 4096
+#define EXECBUF_MAX_LEN 2048
 
 /* takes 6 arguments */
 #define EXECBUF_SYSCALL 0xF0000001
-- 
cgit v1.2.3