DivestOS/Patches/LineageOS-21.0/android_bionic/0001-HM-Use_HM.patch
Tavi 7dd773c83b
Churn
Signed-off-by: Tavi <tavi@divested.dev>
2024-11-29 10:26:14 -05:00

190 lines
5.5 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Daniel Micay <danielmicay@gmail.com>
Date: Wed, 5 Dec 2018 08:51:56 +0200
Subject: [PATCH] use Scudo on 32-bit and hardened_malloc on 64-bit
64-bit Scudo can be swithed to at runtime, see the next commit.
Squashed with 6562b94dfc6dec13e1df79a1b029e6c78f4aa9ad
Co-authored-by: anupritaisno1 <www.anuprita804@gmail.com>
---
libc/Android.bp | 45 ++++++++++++++++++++++------
libc/bionic/h_malloc_wrapper.cpp | 51 ++++++++++++++++++++++++++++++++
libc/bionic/malloc_common.h | 25 +++++++++-------
3 files changed, 102 insertions(+), 19 deletions(-)
create mode 100644 libc/bionic/h_malloc_wrapper.cpp
diff --git a/libc/Android.bp b/libc/Android.bp
index 68155b1db..a5f87c3a2 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -48,6 +48,8 @@ libc_common_flags = [
// ever touch 0, 1, or 2 bytes into a call to memset, which was never going
// to amortize.)
"-fno-builtin",
+
+ "-DH_MALLOC_PREFIX",
]
// Define some common cflags
@@ -131,13 +133,30 @@ libc_scudo_product_variables = {
// ========================================================
cc_defaults {
name: "libc_native_allocator_defaults",
+ whole_static_libs: ["libc_jemalloc_wrapper"],
+ multilib: {
+ lib32: {
+ cflags: ["-DUSE_SCUDO"],
+ whole_static_libs: ["libscudo"],
+ exclude_static_libs: [
+ "libjemalloc5",
+ "libc_jemalloc_wrapper",
+ ],
+ },
+ lib64: {
+ cflags: [
+ "-DH_MALLOC_PREFIX",
+ "-DUSE_H_MALLOC",
+ "-DUSE_SCUDO",
+ ],
+ whole_static_libs: [
+ "libhardened_malloc",
+ "libscudo",
+ ],
+ },
+ },
- whole_static_libs: [
- "libjemalloc5",
- "libc_jemalloc_wrapper",
- ],
header_libs: ["gwp_asan_headers"],
- product_variables: libc_scudo_product_variables,
}
// Functions not implemented by jemalloc directly, or that need to
@@ -145,12 +164,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 <errno.h>
+#include <malloc.h>
+#include <sys/param.h>
+#include <unistd.h>
+
+#include <private/MallocXmlElem.h>
+
+#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..8852c85a2 100644
--- a/libc/bionic/malloc_common.h
+++ b/libc/bionic/malloc_common.h
@@ -55,21 +55,26 @@ __END_DECLS
#else // __has_feature(hwaddress_sanitizer)
-#if defined(USE_SCUDO)
-
-#include "scudo.h"
-#define Malloc(function) scudo_ ## function
+#ifdef __LP64__
+#ifndef USE_H_MALLOC
+#error missing USE_H_MALLOC
+#endif
-#elif defined(USE_SCUDO_SVELTE)
+#include "h_malloc.h"
+#define Malloc(function) h_ ## function
+__BEGIN_DECLS
+int h_malloc_info(int options, FILE* fp);
+__END_DECLS
+#if defined(USE_SCUDO)
#include "scudo.h"
-#define Malloc(function) scudo_svelte_ ## function
-
-#else
+#endif
-#include "jemalloc.h"
-#define Malloc(function) je_ ## function
+#define BOTH_H_MALLOC_AND_SCUDO
+#else // 32-bit
+#include "scudo.h"
+#define Malloc(function) scudo_ ## function
#endif
#endif