From c9df70d93462eeb3e802b5f6f0cdb3df1c3ee798 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 11 Dec 2018 12:37:37 -0500 Subject: [PATCH] add support for labelling memory regions --- Android.bp | 1 + h_malloc.c | 14 ++++++++++++++ memory.c | 15 +++++++++++++++ memory.h | 1 + 4 files changed, 31 insertions(+) diff --git a/Android.bp b/Android.bp index b8483e4..e2d962f 100644 --- a/Android.bp +++ b/Android.bp @@ -24,6 +24,7 @@ common_cflags = [ "-DREGION_QUARANTINE_SKIP_THRESHOLD=33554432", // 32MiB "-DFREE_SLABS_QUARANTINE_RANDOM_LENGTH=32", "-DCONFIG_CLASS_REGION_SIZE=1073741824", // 1GiB + "-DLABEL_MEMORY", ] cc_defaults { diff --git a/h_malloc.c b/h_malloc.c index 38f14e3..7b6854b 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -119,6 +119,18 @@ static const u16 size_class_slots[] = { /* 2048 */ 6, 5, 4, 4 }; +static const char *const size_class_labels[] = { + /* 0 */ "malloc 0", + /* 16 */ "malloc 16", "malloc 32", "malloc 48", "malloc 64", "malloc 80", "malloc 96", "malloc 112", "malloc 128", + /* 32 */ "malloc 160", "malloc 192", "malloc 224", "malloc 256", + /* 64 */ "malloc 320", "malloc 384", "malloc 448", "malloc 512", + /* 128 */ "malloc 640", "malloc 768", "malloc 896", "malloc 1024", + /* 256 */ "malloc 1280", "malloc 1536", "malloc 1792", "malloc 2048", + /* 512 */ "malloc 2560", "malloc 3072", "malloc 3584", "malloc 4096", + /* 1024 */ "malloc 5120", "malloc 6144", "malloc 7168", "malloc 8192", + /* 2048 */ "malloc 10240", "malloc 12288", "malloc 14336", "malloc 16384" +}; + int get_metadata_key(void) { #ifdef USE_PKEY return ro.metadata_pkey; @@ -960,6 +972,7 @@ COLD static void init_slow_path(void) { if (allocator_state == NULL) { fatal_error("failed to reserve allocator state"); } + memory_set_name(allocator_state, sizeof(struct allocator_state), "malloc allocator_state"); if (memory_protect_rw_metadata(allocator_state, offsetof(struct allocator_state, regions_a))) { fatal_error("failed to unprotect allocator state"); } @@ -994,6 +1007,7 @@ COLD static void init_slow_path(void) { size_t bound = (REAL_CLASS_REGION_SIZE - CLASS_REGION_SIZE) / PAGE_SIZE - 1; size_t gap = (get_random_u64_uniform(rng, bound) + 1) * PAGE_SIZE; c->class_region_start = (char *)slab_region_start + REAL_CLASS_REGION_SIZE * class + gap; + memory_set_name(c->class_region_start, CLASS_REGION_SIZE, size_class_labels[class]); size_t size = size_classes[class]; if (size == 0) { diff --git a/memory.c b/memory.c index 15cbf9f..f0020a3 100644 --- a/memory.c +++ b/memory.c @@ -1,6 +1,15 @@ #include #include +#include + +#ifndef PR_SET_VMA +#define PR_SET_VMA 0x53564d41 +#endif + +#ifndef PR_SET_VMA_ANON_NAME +#define PR_SET_VMA_ANON_NAME 0 +#endif #include "memory.h" #include "util.h" @@ -80,3 +89,9 @@ int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size) { } return 0; } + +void memory_set_name(UNUSED void *ptr, UNUSED size_t size, UNUSED const char *name) { +#ifdef LABEL_MEMORY + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, name); +#endif +} diff --git a/memory.h b/memory.h index b4c0d4b..3a6decd 100644 --- a/memory.h +++ b/memory.h @@ -13,5 +13,6 @@ int memory_protect_rw(void *ptr, size_t size); int memory_protect_rw_metadata(void *ptr, size_t size); int memory_remap(void *old, size_t old_size, size_t new_size); int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size); +void memory_set_name(void *ptr, size_t size, const char *name); #endif