Bladeren bron

Scan the allocated chunks for heap pointers

rexim 4 jaren geleden
bovenliggende
commit
7917a795b2
3 gewijzigde bestanden met toevoegingen van 21 en 5 verwijderingen
  1. 1 1
      heap.c
  2. 5 4
      heap.h
  3. 15 0
      main.c

+ 1 - 1
heap.c

@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include "./heap.h"
 
-uintptr_t heap[HEAP_CAP] = {0};
+uintptr_t heap[HEAP_CAP_WORDS] = {0};
 
 Chunk_List alloced_chunks = {0};
 Chunk_List freed_chunks = {

+ 5 - 4
heap.h

@@ -12,12 +12,13 @@
         abort(); \
     } while(0)
 
-#define HEAP_CAP 640000
-
-static_assert(HEAP_CAP % sizeof(uintptr_t) == 0,
+#define HEAP_CAP_BYTES 640000
+static_assert(HEAP_CAP_BYTES % sizeof(uintptr_t) == 0,
               "The heap capacity is not divisible by "
               "the size of the pointer. Of the platform.");
-extern uintptr_t heap[HEAP_CAP];
+#define HEAP_CAP_WORDS (HEAP_CAP_BYTES / sizeof(uintptr_t))
+
+extern uintptr_t heap[HEAP_CAP_WORDS];
 
 void *heap_alloc(size_t size_bytes);
 void heap_free(void *ptr);

+ 15 - 0
main.c

@@ -66,5 +66,20 @@ int main()
 
     print_tree(root, &jim);
 
+    printf("------------------------------\n");
+
+    size_t heap_ptrs_count = 0;
+    for (size_t i = 0; i < alloced_chunks.count; ++i) {
+        for (size_t j = 0; j < alloced_chunks.chunks[i].size; ++j) {
+            uintptr_t *p = (uintptr_t*) alloced_chunks.chunks[i].start[j];
+            if (heap <= p && p < heap + HEAP_CAP_WORDS) {
+                printf("DETECTED HEAP POINTER: %p\n", (void*) p);
+                heap_ptrs_count += 1;
+            }
+        }
+    }
+
+    printf("Detected %zu heap pointers\n", heap_ptrs_count);
+
     return 0;
 }