| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <dlfcn.h> | |
| 6 #include <errno.h> | |
| 7 #include <fcntl.h> | |
| 8 #include <stdbool.h> | |
| 9 #include <stddef.h> | |
| 10 #include <stdint.h> | |
| 11 #include <stdlib.h> | |
| 12 #include <string.h> | |
| 13 #include <sys/mman.h> | |
| 14 #include <unistd.h> | |
| 15 #include <unwind.h> | |
| 16 | |
| 17 #include "tools/android/heap_profiler/heap_profiler.h" | |
| 18 | |
| 19 #define HEAP_PROFILER_EXPORT __attribute__((visibility("default"))) | |
| 20 | |
| 21 | |
| 22 static inline __attribute__((always_inline)) | |
| 23 uint32_t get_backtrace(uintptr_t* frames, uint32_t max_depth); | |
| 24 | |
| 25 // Function pointers typedefs for the hooked symbols. | |
| 26 typedef void* (*mmap_t)(void*, size_t, int, int, int, off_t); | |
| 27 typedef void* (*mmap2_t)(void*, size_t, int, int, int, off_t); | |
| 28 typedef void* (*mmap64_t)(void*, size_t, int, int, int, off64_t); | |
| 29 typedef void* (*mremap_t)(void*, size_t, size_t, unsigned long); | |
| 30 typedef int (*munmap_t)(void*, size_t); | |
| 31 typedef void* (*malloc_t)(size_t); | |
| 32 typedef void* (*calloc_t)(size_t, size_t); | |
| 33 typedef void* (*realloc_t)(void*, size_t); | |
| 34 typedef void (*free_t)(void*); | |
| 35 | |
| 36 // And their actual definitions. | |
| 37 static mmap_t real_mmap; | |
| 38 static mmap2_t real_mmap2; | |
| 39 static mmap64_t real_mmap64; | |
| 40 static mremap_t real_mremap; | |
| 41 static munmap_t real_munmap; | |
| 42 static malloc_t real_malloc; | |
| 43 static calloc_t real_calloc; | |
| 44 static realloc_t real_realloc; | |
| 45 static free_t real_free; | |
| 46 static int* has_forked_off_zygote; | |
| 47 | |
| 48 HEAP_PROFILER_EXPORT const HeapStats* heap_profiler_stats_for_tests; | |
| 49 | |
| 50 // +---------------------------------------------------------------------------+ | |
| 51 // + Initialization of heap_profiler and lookup of hooks' addresses + | |
| 52 // +---------------------------------------------------------------------------+ | |
| 53 __attribute__((constructor)) | |
| 54 static void initialize() { | |
| 55 real_mmap = (mmap_t) dlsym(RTLD_NEXT, "mmap"); | |
| 56 real_mmap2 = (mmap_t) dlsym(RTLD_NEXT, "mmap2"); | |
| 57 real_mmap64 = (mmap64_t) dlsym(RTLD_NEXT, "mmap64"); | |
| 58 real_mremap = (mremap_t) dlsym(RTLD_NEXT, "mremap"); | |
| 59 real_munmap = (munmap_t) dlsym(RTLD_NEXT, "munmap"); | |
| 60 real_malloc = (malloc_t) dlsym(RTLD_NEXT, "malloc"); | |
| 61 real_calloc = (calloc_t) dlsym(RTLD_NEXT, "calloc"); | |
| 62 real_realloc = (realloc_t) dlsym(RTLD_NEXT, "realloc"); | |
| 63 real_free = (free_t) dlsym(RTLD_NEXT, "free"); | |
| 64 | |
| 65 // gMallocLeakZygoteChild is an extra useful piece of information to have. | |
| 66 // When available, it tells whether we're in the zygote (=0) or forked (=1) | |
| 67 // a child off it. In the worst case it will be NULL and we'll just ignore it. | |
| 68 has_forked_off_zygote = (int*) dlsym(RTLD_NEXT, "gMallocLeakZygoteChild"); | |
| 69 | |
| 70 // Allocate room for the HeapStats area and initialize the heap profiler. | |
| 71 // Make an explicit map of /dev/zero (instead of MAP_ANONYMOUS), so that the | |
| 72 // heap_dump tool can easily spot the mapping in the target process. | |
| 73 int fd = open("/dev/zero", O_RDONLY); | |
| 74 if (fd < 0) { | |
| 75 abort(); // This world has gone wrong. Good night Vienna. | |
| 76 } | |
| 77 | |
| 78 HeapStats* stats = (HeapStats*) real_mmap( | |
| 79 0, sizeof(HeapStats), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
| 80 heap_profiler_stats_for_tests = stats; | |
| 81 heap_profiler_init(stats); | |
| 82 } | |
| 83 | |
| 84 static inline __attribute__((always_inline)) void unwind_and_record_alloc( | |
| 85 void* start, size_t size, uint32_t flags) { | |
| 86 const int errno_save = errno; | |
| 87 uintptr_t frames[HEAP_PROFILER_MAX_DEPTH]; | |
| 88 const uint32_t depth = get_backtrace(frames, HEAP_PROFILER_MAX_DEPTH); | |
| 89 if (has_forked_off_zygote != NULL && *has_forked_off_zygote == 0) | |
| 90 flags |= HEAP_PROFILER_FLAGS_IN_ZYGOTE; | |
| 91 heap_profiler_alloc(start, size, frames, depth, flags); | |
| 92 errno = errno_save; | |
| 93 } | |
| 94 | |
| 95 static inline __attribute__((always_inline)) void discard_alloc( | |
| 96 void* start, size_t size, uint32_t* old_flags) { | |
| 97 const int errno_save = errno; | |
| 98 heap_profiler_free(start, size, old_flags); | |
| 99 errno = errno_save; | |
| 100 } | |
| 101 | |
| 102 // Flags are non-functional extra decorators that are made available to the | |
| 103 // final heap_dump tool, to get more details about the source of the allocation. | |
| 104 static uint32_t get_flags_for_mmap(int fd) { | |
| 105 return HEAP_PROFILER_FLAGS_MMAP | (fd ? HEAP_PROFILER_FLAGS_MMAP_FILE : 0); | |
| 106 } | |
| 107 | |
| 108 // +---------------------------------------------------------------------------+ | |
| 109 // + Actual mmap/malloc hooks + | |
| 110 // +---------------------------------------------------------------------------+ | |
| 111 HEAP_PROFILER_EXPORT void* mmap( | |
| 112 void* addr, size_t size, int prot, int flags, int fd, off_t offset) { | |
| 113 void* ret = real_mmap(addr, size, prot, flags, fd, offset); | |
| 114 if (ret != MAP_FAILED) | |
| 115 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
| 116 return ret; | |
| 117 } | |
| 118 | |
| 119 HEAP_PROFILER_EXPORT void* mmap2( | |
| 120 void* addr, size_t size, int prot, int flags, int fd, off_t pgoffset) { | |
| 121 void* ret = real_mmap2(addr, size, prot, flags, fd, pgoffset); | |
| 122 if (ret != MAP_FAILED) | |
| 123 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
| 124 return ret; | |
| 125 } | |
| 126 | |
| 127 HEAP_PROFILER_EXPORT void* mmap64( | |
| 128 void* addr, size_t size, int prot, int flags, int fd, off64_t offset) { | |
| 129 void* ret = real_mmap64(addr, size, prot, flags, fd, offset); | |
| 130 if (ret != MAP_FAILED) | |
| 131 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
| 132 return ret; | |
| 133 } | |
| 134 | |
| 135 HEAP_PROFILER_EXPORT void* mremap( | |
| 136 void* addr, size_t oldlen, size_t newlen, unsigned long flags) { | |
| 137 void* ret = real_mremap(addr, oldlen, newlen, flags); | |
| 138 if (ret != MAP_FAILED) { | |
| 139 uint32_t flags = 0; | |
| 140 if (addr) | |
| 141 discard_alloc(addr, oldlen, &flags); | |
| 142 if (newlen > 0) | |
| 143 unwind_and_record_alloc(ret, newlen, flags); | |
| 144 } | |
| 145 return ret; | |
| 146 } | |
| 147 | |
| 148 HEAP_PROFILER_EXPORT int munmap(void* ptr, size_t size) { | |
| 149 int ret = real_munmap(ptr, size); | |
| 150 discard_alloc(ptr, size, /*old_flags=*/NULL); | |
| 151 return ret; | |
| 152 } | |
| 153 | |
| 154 HEAP_PROFILER_EXPORT void* malloc(size_t byte_count) { | |
| 155 void* ret = real_malloc(byte_count); | |
| 156 if (ret != NULL) | |
| 157 unwind_and_record_alloc(ret, byte_count, HEAP_PROFILER_FLAGS_MALLOC); | |
| 158 return ret; | |
| 159 } | |
| 160 | |
| 161 HEAP_PROFILER_EXPORT void* calloc(size_t nmemb, size_t size) { | |
| 162 void* ret = real_calloc(nmemb, size); | |
| 163 if (ret != NULL) | |
| 164 unwind_and_record_alloc(ret, nmemb * size, HEAP_PROFILER_FLAGS_MALLOC); | |
| 165 return ret; | |
| 166 } | |
| 167 | |
| 168 HEAP_PROFILER_EXPORT void* realloc(void* ptr, size_t size) { | |
| 169 void* ret = real_realloc(ptr, size); | |
| 170 uint32_t flags = 0; | |
| 171 if (ptr) | |
| 172 discard_alloc(ptr, 0, &flags); | |
| 173 if (ret != NULL) | |
| 174 unwind_and_record_alloc(ret, size, flags | HEAP_PROFILER_FLAGS_MALLOC); | |
| 175 return ret; | |
| 176 } | |
| 177 | |
| 178 HEAP_PROFILER_EXPORT void free(void* ptr) { | |
| 179 real_free(ptr); | |
| 180 discard_alloc(ptr, 0, /*old_flags=*/NULL); | |
| 181 } | |
| 182 | |
| 183 // +---------------------------------------------------------------------------+ | |
| 184 // + Stack unwinder + | |
| 185 // +---------------------------------------------------------------------------+ | |
| 186 typedef struct { | |
| 187 uintptr_t* frames; | |
| 188 uint32_t frame_count; | |
| 189 uint32_t max_depth; | |
| 190 bool have_skipped_self; | |
| 191 } stack_crawl_state_t; | |
| 192 | |
| 193 static _Unwind_Reason_Code unwind_fn(struct _Unwind_Context* ctx, void* arg) { | |
| 194 stack_crawl_state_t* state = (stack_crawl_state_t*) arg; | |
| 195 uintptr_t ip = _Unwind_GetIP(ctx); | |
| 196 | |
| 197 if (ip != 0 && !state->have_skipped_self) { | |
| 198 state->have_skipped_self = true; | |
| 199 return _URC_NO_REASON; | |
| 200 } | |
| 201 | |
| 202 state->frames[state->frame_count++] = ip; | |
| 203 return (state->frame_count >= state->max_depth) ? | |
| 204 _URC_END_OF_STACK : _URC_NO_REASON; | |
| 205 } | |
| 206 | |
| 207 static uint32_t get_backtrace(uintptr_t* frames, uint32_t max_depth) { | |
| 208 stack_crawl_state_t state = {.frames = frames, .max_depth = max_depth}; | |
| 209 _Unwind_Backtrace(unwind_fn, &state); | |
| 210 return state.frame_count; | |
| 211 } | |
| OLD | NEW |