[libc] Zero and pattern initialization of heap memory.
Bug: 155227507
Test: scudo_unit_tests
Change-Id: I85075acfd85172f6cc7e48f79eeb577e293d0d30
diff --git a/libc/Android.bp b/libc/Android.bp
index 4c36635..f366ddb 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -88,6 +88,12 @@
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 12628f7..a47c2fc 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> __libc_globals;
@@ -83,6 +85,14 @@
_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 @@
__libc_init_fdsan(); // Requires system properties (for debug.fdsan).
__libc_init_fdtrack();
+ __libc_init_malloc_fill_contents();
SetDefaultHeapTaggingLevel();
}