From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 5 Dec 2018 01:51:56 -0500 Subject: [PATCH] use Scudo on 32-bit and hardened_malloc on 64-bit Co-authored-by: anupritaisno1 Signed-off-by: anupritaisno1 [tad@spotco.us]: kept Lineage's scudo 32-bit workaround --- libc/Android.bp | 37 ++++++++++++++--------- libc/bionic/h_malloc_wrapper.cpp | 51 ++++++++++++++++++++++++++++++++ libc/bionic/malloc_common.h | 8 +++++ 3 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 libc/bionic/h_malloc_wrapper.cpp diff --git a/libc/Android.bp b/libc/Android.bp index 49108bf35..77908e7d9 100644 --- a/libc/Android.bp +++ b/libc/Android.bp @@ -50,6 +50,8 @@ libc_common_flags = [ // GWP-ASan requires platform TLS. "-fno-emulated-tls", + + "-DH_MALLOC_PREFIX", ] // Define some common cflags @@ -122,20 +124,19 @@ libc32_scudo_product_variables = { // ======================================================== cc_defaults { name: "libc_native_allocator_defaults", - - whole_static_libs: [ - "libjemalloc5", - "libc_jemalloc_wrapper", - ], - header_libs: ["gwp_asan_headers"], + whole_static_libs: ["libc_jemalloc_wrapper"], multilib: { - lib64: { - product_variables: libc_scudo_product_variables, - }, lib32: { product_variables: libc32_scudo_product_variables, - } + whole_static_libs: ["libjemalloc5"], + }, + lib64: { + cflags: ["-DH_MALLOC_PREFIX"], + whole_static_libs: ["libhardened_malloc"], + }, }, + + header_libs: ["gwp_asan_headers"], } // Functions not implemented by jemalloc directly, or that need to @@ -143,12 +144,20 @@ cc_defaults { cc_library_static { name: "libc_jemalloc_wrapper", defaults: ["libc_defaults"], - srcs: ["bionic/jemalloc_wrapper.cpp"], + multilib: { + lib32: { + // Used to pull in the jemalloc/hardened_malloc include directory so that if the + // library is removed, the include directory is also removed. + srcs: ["bionic/jemalloc_wrapper.cpp"], + static_libs: ["libjemalloc5"], + }, + lib64: { + srcs: ["bionic/h_malloc_wrapper.cpp"], + static_libs: ["libhardened_malloc"], + }, + }, cflags: ["-fvisibility=hidden"], - // Used to pull in the jemalloc include directory so that if the - // library is removed, the include directory is also removed. - static_libs: ["libjemalloc5"], } // ======================================================== diff --git a/libc/bionic/h_malloc_wrapper.cpp b/libc/bionic/h_malloc_wrapper.cpp new file mode 100644 index 000000000..5fb0968c2 --- /dev/null +++ b/libc/bionic/h_malloc_wrapper.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +#include + +#include "h_malloc.h" + +__BEGIN_DECLS +int h_malloc_info(int options, FILE* fp); +__END_DECLS + +int h_malloc_info(int options, FILE* fp) { + if (options != 0) { + errno = EINVAL; + return -1; + } + + fflush(fp); + int fd = fileno(fp); + MallocXmlElem root(fd, "malloc", "version=\"jemalloc-1\""); + + // Dump all of the large allocations in the arenas. + for (size_t i = 0; i < h_mallinfo_narenas(); i++) { + struct mallinfo mi = h_mallinfo_arena_info(i); + if (mi.hblkhd != 0) { + MallocXmlElem arena_elem(fd, "heap", "nr=\"%d\"", i); + { + MallocXmlElem(fd, "allocated-large").Contents("%zu", mi.ordblks); + MallocXmlElem(fd, "allocated-huge").Contents("%zu", mi.uordblks); + MallocXmlElem(fd, "allocated-bins").Contents("%zu", mi.fsmblks); + + size_t total = 0; + for (size_t j = 0; j < h_mallinfo_nbins(); j++) { + struct mallinfo mi = h_mallinfo_bin_info(i, j); + if (mi.ordblks != 0) { + MallocXmlElem bin_elem(fd, "bin", "nr=\"%d\"", j); + MallocXmlElem(fd, "allocated").Contents("%zu", mi.ordblks); + MallocXmlElem(fd, "nmalloc").Contents("%zu", mi.uordblks); + MallocXmlElem(fd, "ndalloc").Contents("%zu", mi.fordblks); + total += mi.ordblks; + } + } + MallocXmlElem(fd, "bins-total").Contents("%zu", total); + } + } + } + + return 0; +} diff --git a/libc/bionic/malloc_common.h b/libc/bionic/malloc_common.h index 4afcc4a8d..cca9e5202 100644 --- a/libc/bionic/malloc_common.h +++ b/libc/bionic/malloc_common.h @@ -67,8 +67,16 @@ __END_DECLS #else +#ifdef __LP64__ +#include "h_malloc.h" +#define Malloc(function) h_ ## function +__BEGIN_DECLS +int h_malloc_info(int options, FILE* fp); +__END_DECLS +#else #include "jemalloc.h" #define Malloc(function) je_ ## function +#endif #endif