mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
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. :^)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user