diff --git a/headers/private/libroot/libroot_private.h b/headers/private/libroot/libroot_private.h index 0b1c924061..20d035a10e 100644 --- a/headers/private/libroot/libroot_private.h +++ b/headers/private/libroot/libroot_private.h @@ -32,6 +32,7 @@ status_t __flatten_process_args(const char* const* args, int32 argCount, void _call_atexit_hooks_for_range(addr_t start, addr_t size); void __init_env(const struct user_space_program_args *args); void __init_heap(void); +void __init_heap_post_env(void); void __init_time(void); void __arch_init_time(struct real_time_data *data, bool setDefaults); diff --git a/src/system/libroot/libroot_init.c b/src/system/libroot/libroot_init.c index bceb3d9faf..7739b01828 100644 --- a/src/system/libroot/libroot_init.c +++ b/src/system/libroot/libroot_init.c @@ -58,6 +58,7 @@ initialize_before(image_id imageID) __init_time(); __init_heap(); __init_env(__gRuntimeLoader->program_args); + __init_heap_post_env(); __init_pwd_backend(); } diff --git a/src/system/libroot/posix/malloc/arch-specific.cpp b/src/system/libroot/posix/malloc/arch-specific.cpp index cd51a3651b..8cb1f6723b 100644 --- a/src/system/libroot/posix/malloc/arch-specific.cpp +++ b/src/system/libroot/posix/malloc/arch-specific.cpp @@ -119,6 +119,13 @@ __init_heap(void) } +extern "C" void +__init_heap_post_env(void) +{ + // no heap options available +} + + static void insert_chunk(free_chunk *newChunk) { diff --git a/src/system/libroot/posix/malloc_debug/heap.cpp b/src/system/libroot/posix/malloc_debug/heap.cpp index 2dc4d90126..1b0f85f2a9 100644 --- a/src/system/libroot/posix/malloc_debug/heap.cpp +++ b/src/system/libroot/posix/malloc_debug/heap.cpp @@ -2,13 +2,14 @@ * Copyright 2008-2009, Michael Lotz, mmlr@mlotz.ch. * Distributed under the terms of the MIT License. * - * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2002-2011, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. * * Copyright 2001, Travis Geiselbrecht. All rights reserved. * Distributed under the terms of the NewOS License. */ + #include #include #include @@ -41,6 +42,7 @@ static bool sReuseMemory = true; static bool sParanoidValidation = false; static thread_id sWallCheckThread = -1; static bool sStopWallChecking = false; +static bool sUseGuardPage = false; void @@ -1817,6 +1819,25 @@ __init_heap(void) } +extern "C" void +__init_heap_post_env(void) +{ + const char *mode = getenv("MALLOC_DEBUG"); + if (mode != NULL) { + if (strchr(mode, 'p')) + heap_debug_set_paranoid_validation(true); + if (strchr(mode, 'w')) + heap_debug_start_wall_checking(500); + else if (strchr(mode, 'W')) + heap_debug_start_wall_checking(100); + if (strchr(mode, 'g')) + sUseGuardPage = true; + if (strchr(mode, 'r')) + heap_debug_set_memory_reuse(false); + } +} + + // #pragma mark - Public API @@ -1908,6 +1929,9 @@ memalign(size_t alignment, size_t size) void * malloc(size_t size) { + if (sUseGuardPage) + return heap_debug_malloc_with_guard_page(size); + return memalign(0, size); }