Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kernel] Experimental heap_free valitity checks #107

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tlvc/include/linuxmt/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct heap {
list_s free;
word_t size;
byte_t tag;
byte_t unused;
byte_t canary;
};

typedef struct heap heap_s;
Expand Down
14 changes: 13 additions & 1 deletion tlvc/lib/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
// plus enough space in body to be useful
// (= size of the smallest allocation)

#define HEAP_MIN_SIZE (sizeof (heap_s) + 16)
#define HEAP_MIN_SIZE (sizeof(heap_s) + 16)
#define HEAP_SEG_OPT /* allocate small SEG descriptors from the upper */
/* end of the heap to reduce fragmentation */

#define HEAP_CANARY 0xA5U /* for header validation */
#define VALIDATE_HEAPFREE

#define HEAP_DEBUG 0
#if HEAP_DEBUG
#define debug_heap printk
Expand Down Expand Up @@ -105,6 +108,7 @@ static heap_s *free_get(word_t size0, byte_t tag)
list_remove(&(best_h->free));
}
best_h->tag = HEAP_TAG_USED | tag;
best_h->canary = HEAP_CANARY;
}
#ifdef HEAP_SEG_OPT
debug_heap("highfree: %x/%u, tag 0x%x\n", high_free, high_free->size, tag);
Expand All @@ -118,6 +122,7 @@ static heap_s *free_get(word_t size0, byte_t tag)
static void heap_merge(heap_s *h1, heap_s *h2)
{
h1->size = h1->size + sizeof(heap_s) + h2->size;
h2->canary = 0;
list_remove(&(h2->all));
}

Expand Down Expand Up @@ -152,6 +157,13 @@ void heap_free(void *data)
list_s *i = &_heap_free;
debug_heap("free 0x%x/%u; ", h, h->size);

#ifdef VALIDATE_HEAPFREE
if (h->canary != HEAP_CANARY) {
printk("WARNING: bogus heap_free");
return;
}
#endif

// Try to merge with previous block if free

list_s *p = h->all.prev;
Expand Down