浏览代码

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_alloc = 0;
 static uint32_t s_malloc_sum_free = POOL_SIZE;
 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)
 void* malloc(size_t size)
 {
 {
     if (size == 0)
     if (size == 0)
@@ -108,7 +111,8 @@ void* malloc(size_t size)
                 s_malloc_sum_alloc += header->chunk_count * CHUNK_SIZE;
                 s_malloc_sum_alloc += header->chunk_count * CHUNK_SIZE;
                 s_malloc_sum_free  -= 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;
                 return ptr;
             }
             }
         }
         }
@@ -139,7 +143,8 @@ void free(void* ptr)
     s_malloc_sum_alloc -= header->chunk_count * CHUNK_SIZE;
     s_malloc_sum_alloc -= header->chunk_count * CHUNK_SIZE;
     s_malloc_sum_free += 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()
 void __malloc_init()
@@ -148,6 +153,12 @@ void __malloc_init()
     int rc = set_mmap_name(s_malloc_pool, malloc_budget, "malloc pool");
     int rc = set_mmap_name(s_malloc_pool, malloc_budget, "malloc pool");
     if (rc < 0)
     if (rc < 0)
         perror("set_mmap_name failed");
         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)
 void* calloc(size_t count, size_t size)