Skip to content

Commit 9e55af4

Browse files
committed
Panic on heap corruption detected in memory allocator
The memory allocator previously attempted to continue execution even if validate_block() failed, which indicates unexpected heap corruption or invalid memory operations. Once the heap is corrupted, the kernel can no longer guarantee safe or correct behavior. Invoke panic(ERR_HEAP_CORRUPT) when validate_block() fails in malloc(), free(), or realloc(), ensuring the kernel halts immediately on fatal allocator errors.
1 parent a49b345 commit 9e55af4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/malloc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sys/task.h>
55
#include <types.h>
66

7+
#include "private/error.h"
78
#include "private/utils.h"
89

910
/* Memory allocator using first-fit strategy with selective coalescing.
@@ -71,6 +72,7 @@ void free(void *ptr)
7172
/* Validate the block being freed */
7273
if (!validate_block(p) || !IS_USED(p)) {
7374
CRITICAL_LEAVE();
75+
panic(ERR_HEAP_CORRUPT);
7476
return; /* Invalid or double-free */
7577
}
7678

@@ -146,6 +148,7 @@ void *malloc(uint32_t size)
146148
while (p) {
147149
if (!validate_block(p)) {
148150
CRITICAL_LEAVE();
151+
panic(ERR_HEAP_CORRUPT);
149152
return NULL; /* Heap corruption detected */
150153
}
151154

@@ -239,8 +242,10 @@ void *realloc(void *ptr, uint32_t size)
239242
memblock_t *old_block = ((memblock_t *) ptr) - 1;
240243

241244
/* Validate the existing block */
242-
if (!validate_block(old_block) || !IS_USED(old_block))
245+
if (!validate_block(old_block) || !IS_USED(old_block)) {
246+
panic(ERR_HEAP_CORRUPT);
243247
return NULL;
248+
}
244249

245250
size_t old_size = GET_SIZE(old_block);
246251

0 commit comments

Comments
 (0)