From 95ddca8a52ae4d25f35ae66c04c3c7cb7846cb5d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 27 Apr 2019 21:19:56 +0200 Subject: [PATCH] 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. :^) --- LibC/stdlib.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp index 36157e3682..a4f8fa5526 100644 --- a/LibC/stdlib.cpp +++ b/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)