Преглед на файлове

LibC: Make the malloc()/free() scrubbing runtime optional (default on.)

Memory returned by malloc() is normally memset with 0x85.
Memory passed to free() is normally memset with 0x82.

These behaviors can now be disabled by setting one or both of
LIBC_NOSCRUB_MALLOC and LIBC_NOSCRUB_FREE in your environment. :^)
Andreas Kling преди 6 години
родител
ревизия
95ddca8a52
променени са 1 файла, в които са добавени 13 реда и са изтрити 2 реда
  1. 13 2
      LibC/stdlib.cpp

+ 13 - 2
LibC/stdlib.cpp

@@ -36,6 +36,9 @@ static byte* s_malloc_pool;
 static uint32_t s_malloc_sum_alloc = 0;
 static uint32_t s_malloc_sum_free = POOL_SIZE;
 
+static bool s_scrub_malloc = true;
+static bool s_scrub_free = true;
+
 void* malloc(size_t size)
 {
     if (size == 0)
@@ -108,7 +111,8 @@ void* malloc(size_t size)
                 s_malloc_sum_alloc += header->chunk_count * CHUNK_SIZE;
                 s_malloc_sum_free  -= header->chunk_count * CHUNK_SIZE;
 
-                memset(ptr, MALLOC_SCRUB_BYTE, (header->chunk_count * CHUNK_SIZE) - sizeof(MallocHeader));
+                if (s_scrub_malloc)
+                    memset(ptr, MALLOC_SCRUB_BYTE, (header->chunk_count * CHUNK_SIZE) - sizeof(MallocHeader));
                 return ptr;
             }
         }
@@ -139,7 +143,8 @@ void free(void* ptr)
     s_malloc_sum_alloc -= header->chunk_count * CHUNK_SIZE;
     s_malloc_sum_free += header->chunk_count * CHUNK_SIZE;
 
-    memset(header, FREE_SCRUB_BYTE, header->chunk_count * CHUNK_SIZE);
+    if (s_scrub_free)
+        memset(header, FREE_SCRUB_BYTE, header->chunk_count * CHUNK_SIZE);
 }
 
 void __malloc_init()
@@ -148,6 +153,12 @@ void __malloc_init()
     int rc = set_mmap_name(s_malloc_pool, malloc_budget, "malloc pool");
     if (rc < 0)
         perror("set_mmap_name failed");
+
+    if (auto* scrub_malloc_env = getenv("LIBC_NOSCRUB_MALLOC"))
+        s_scrub_malloc = false;
+    if (auto* scrub_free_env = getenv("LIBC_NOSCRUB_FREE"))
+        s_scrub_free = false;
+
 }
 
 void* calloc(size_t count, size_t size)