浏览代码

Kernel: Fix kmalloc memory corruption

Rather than hardcoding where the kmalloc pool should be, place
it at the end of the kernel image instead. This avoids corrupting
global variables or other parts of the kernel as it grows.

Fixes #3257
Tom 4 年之前
父节点
当前提交
b306f240a4
共有 1 个文件被更改,包括 7 次插入3 次删除
  1. 7 3
      Kernel/Heap/kmalloc.cpp

+ 7 - 3
Kernel/Heap/kmalloc.cpp

@@ -40,6 +40,7 @@
 #include <Kernel/Scheduler.h>
 #include <Kernel/SpinLock.h>
 #include <Kernel/StdLib.h>
+#include <Kernel/VM/MemoryManager.h>
 
 #define SANITIZE_KMALLOC
 
@@ -48,13 +49,16 @@ struct AllocationHeader {
     u8 data[0];
 };
 
-#define BASE_PHYSICAL (0xc0000000 + (4 * MiB))
 #define CHUNK_SIZE 32
 #define POOL_SIZE (3 * MiB)
-
-#define ETERNAL_BASE_PHYSICAL (0xc0000000 + (2 * MiB))
 #define ETERNAL_RANGE_SIZE (2 * MiB)
 
+// We need to make sure to not stomp on global variables or other parts
+// of the kernel image!
+extern u32 end_of_kernel_bss;
+#define ETERNAL_BASE_PHYSICAL ((u8*)PAGE_ROUND_UP(&end_of_kernel_bss))
+#define BASE_PHYSICAL (ETERNAL_BASE_PHYSICAL + ETERNAL_RANGE_SIZE)
+
 static u8 alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
 
 size_t g_kmalloc_bytes_allocated = 0;