Usermode/libc - Move free logic to another method, allowing libraries to catch and...
authorJohn Hodge <[email protected]>
Mon, 18 Aug 2014 10:26:20 +0000 (18:26 +0800)
committerJohn Hodge <[email protected]>
Mon, 18 Aug 2014 10:26:20 +0000 (18:26 +0800)
Usermode/Libraries/libc.so_src/heap.c

index 5346032..d65fc33 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdlib.h>\r
 #include <string.h>\r
 #include <assert.h>\r
+#include <stdbool.h>\r
 #include "lib.h"\r
 \r
 #if 0\r
@@ -72,6 +73,7 @@ static const heap_head        _heap_zero_allocation;
 EXPORT void    *malloc(size_t bytes);\r
 void   *_malloc(size_t bytes, void *owner);\r
 EXPORT void    *calloc(size_t bytes, size_t count);\r
+bool   _libc_free(void *mem);\r
 EXPORT void    free(void *mem);\r
 EXPORT void    *realloc(void *mem, size_t bytes);\r
 EXPORT void    *sbrk(int increment);\r
@@ -222,26 +224,30 @@ EXPORT void *calloc(size_t __nmemb, size_t __size)
  \param mem    Pointer - Memory to free\r
 */\r
 EXPORT void free(void *mem)\r
+{\r
+       if( !_libc_free(mem) ) {\r
+               Heap_Validate(1);\r
+               exit(0);\r
+       }\r
+}\r
+\r
+bool _libc_free(void *mem)\r
 {\r
        heap_head       *head = (heap_head*)mem - 1;\r
 \r
        // Free of NULL or the zero allocation does nothing\r
        if(!mem || mem == _heap_zero_allocation.data)\r
-               return ;\r
+               return true;\r
        \r
        // Sanity check the head address\r
        if(head->magic != MAGIC) {\r
                if( head->magic != MAGIC_FREE ) {\r
-                       _SysDebug("Double free of %p", mem);\r
-                       Heap_Validate(1);\r
-                       exit(0);\r
+                       _SysDebug("Double free of %p by %p", mem, __builtin_return_address(0));\r
                }\r
                else {\r
-                       _SysDebug("Free of invalid pointer %p", mem);\r
-                       Heap_Validate(1);\r
-                       exit(0);\r
+                       _SysDebug("Free of invalid pointer %p by ", mem, __builtin_return_address(0));\r
                }\r
-               return;\r
+               return false;\r
        }\r
        \r
        head->magic = MAGIC_FREE;\r
@@ -266,8 +272,9 @@ EXPORT void free(void *mem)
                heap_foot *prevFoot = PREV_FOOT(head);\r
                if( prevFoot->magic != MAGIC )\r
                {\r
+                       _SysDebug("Heap corruption, previous foot magic invalid");\r
                        Heap_Validate(1);\r
-                       exit(1);\r
+                       return false;\r
                }\r
                \r
                heap_head *prevHead = prevFoot->header;\r
@@ -282,6 +289,8 @@ EXPORT void free(void *mem)
                        prevFoot->header = NULL;\r
                }\r
        }\r
+       \r
+       return true;\r
 }\r
 \r
 /**\r

UCC git Repository :: git.ucc.asn.au