Skip to content

Commit

Permalink
Merge pull request #107 from Mellvik/misc
Browse files Browse the repository at this point in the history
[kernel] Experimental heap_free valitity checks
  • Loading branch information
Mellvik authored Nov 24, 2024
2 parents 279f3d6 + 7805da0 commit 03292ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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

0 comments on commit 03292ee

Please sign in to comment.