From 5a73e033054057381a358e68cf30b0ac3479fe07 Mon Sep 17 00:00:00 2001 From: Evgenii Stepanov Date: Wed, 29 Apr 2020 14:59:44 -0700 Subject: [PATCH] [libc] Zero and pattern initialization of heap memory. Bug: 155227507 Test: scudo_unit_tests Change-Id: I85075acfd85172f6cc7e48f79eeb577e293d0d30 --- libc/Android.bp | 6 ++++++ libc/bionic/libc_init_common.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/libc/Android.bp b/libc/Android.bp index 4c3663589..f366ddbd8 100644 --- a/libc/Android.bp +++ b/libc/Android.bp @@ -88,6 +88,12 @@ cc_defaults { experimental_mte: { cflags: ["-DANDROID_EXPERIMENTAL_MTE"], }, + malloc_zero_contents: { + cflags: ["-DSCUDO_ZERO_CONTENTS"], + }, + malloc_pattern_fill_contents: { + cflags: ["-DSCUDO_PATTERN_FILL_CONTENTS"], + }, }, } diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp index 12628f753..a47c2fcd0 100644 --- a/libc/bionic/libc_init_common.cpp +++ b/libc/bionic/libc_init_common.cpp @@ -52,6 +52,8 @@ #include "pthread_internal.h" extern "C" int __system_properties_init(void); +extern "C" void scudo_malloc_set_zero_contents(int); +extern "C" void scudo_malloc_set_pattern_fill_contents(int); __LIBC_HIDDEN__ WriteProtected __libc_globals; @@ -83,6 +85,14 @@ static void arc4random_fork_handler() { _thread_arc4_lock(); } +static void __libc_init_malloc_fill_contents() { +#if defined(SCUDO_PATTERN_FILL_CONTENTS) + scudo_malloc_set_pattern_fill_contents(1); +#elif defined(SCUDO_ZERO_CONTENTS) + scudo_malloc_set_zero_contents(1); +#endif +} + __BIONIC_WEAK_FOR_NATIVE_BRIDGE void __libc_add_main_thread() { // Get the main thread from TLS and add it to the thread list. @@ -106,6 +116,7 @@ void __libc_init_common() { __libc_init_fdsan(); // Requires system properties (for debug.fdsan). __libc_init_fdtrack(); + __libc_init_malloc_fill_contents(); SetDefaultHeapTaggingLevel(); }