blob: d890a1ccd39a56df9a7602f297e371bbcde071e6 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <inttypes.h>
31#include <malloc.h>
32#include <string.h>
33#include <sys/cdefs.h>
34#include <sys/param.h>
35#include <unistd.h>
36
Christopher Ferris602b88c2017-08-04 13:04:04 -070037#include <mutex>
Christopher Ferris63860cb2015-11-16 17:30:32 -080038#include <vector>
39
Christopher Ferris602b88c2017-08-04 13:04:04 -070040#include <android-base/file.h>
41#include <android-base/stringprintf.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080042#include <private/bionic_malloc_dispatch.h>
43
44#include "backtrace.h"
Christopher Ferris72df6702016-02-11 15:51:31 -080045#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080046#include "DebugData.h"
47#include "debug_disable.h"
48#include "debug_log.h"
49#include "malloc_debug.h"
50
51// ------------------------------------------------------------------------
52// Global Data
53// ------------------------------------------------------------------------
54DebugData* g_debug;
55
56int* g_malloc_zygote_child;
57
58const MallocDispatch* g_dispatch;
59// ------------------------------------------------------------------------
60
61// ------------------------------------------------------------------------
62// Use C style prototypes for all exported functions. This makes it easy
63// to do dlsym lookups during libc initialization when malloc debug
64// is enabled.
65// ------------------------------------------------------------------------
66__BEGIN_DECLS
67
Tamas Berghammerac81fe82016-08-26 15:54:59 +010068bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
69 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080070void debug_finalize();
Christopher Ferris602b88c2017-08-04 13:04:04 -070071bool debug_dump_heap(const char* file_name);
Christopher Ferris63860cb2015-11-16 17:30:32 -080072void debug_get_malloc_leak_info(
73 uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory,
74 size_t* backtrace_size);
Colin Cross2d4721c2016-02-02 11:57:54 -080075ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080076void debug_free_malloc_leak_info(uint8_t* info);
77size_t debug_malloc_usable_size(void* pointer);
78void* debug_malloc(size_t size);
79void debug_free(void* pointer);
80void* debug_memalign(size_t alignment, size_t bytes);
81void* debug_realloc(void* pointer, size_t bytes);
82void* debug_calloc(size_t nmemb, size_t bytes);
83struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070084int debug_mallopt(int param, int value);
Christopher Ferris63860cb2015-11-16 17:30:32 -080085int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080086int debug_iterate(uintptr_t base, size_t size,
87 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
88void debug_malloc_disable();
89void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080090
91#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
92void* debug_pvalloc(size_t bytes);
93void* debug_valloc(size_t size);
94#endif
95
96__END_DECLS
97// ------------------------------------------------------------------------
98
Colin Cross7a28a3c2016-02-07 22:51:15 -080099static void InitAtfork() {
100 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
101 pthread_once(&atfork_init, [](){
102 pthread_atfork(
103 [](){
104 if (g_debug != nullptr) {
105 g_debug->PrepareFork();
106 }
107 },
108 [](){
109 if (g_debug != nullptr) {
110 g_debug->PostForkParent();
111 }
112 },
113 [](){
114 if (g_debug != nullptr) {
115 g_debug->PostForkChild();
116 }
117 }
118 );
119 });
120}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700121
Christopher Ferris63860cb2015-11-16 17:30:32 -0800122static void LogTagError(const Header* header, const void* pointer, const char* name) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800123 error_log(LOG_DIVIDER);
Christopher Ferris7993b802016-01-28 18:35:05 -0800124 if (header->tag == DEBUG_FREE_TAG) {
125 error_log("+++ ALLOCATION %p USED AFTER FREE (%s)", pointer, name);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700126 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800127 g_debug->free_track->LogBacktrace(header);
128 }
129 } else {
130 error_log("+++ ALLOCATION %p HAS INVALID TAG %" PRIx32 " (%s)", pointer, header->tag, name);
131 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800132 error_log("Backtrace at time of failure:");
133 std::vector<uintptr_t> frames(64);
134 size_t frame_num = backtrace_get(frames.data(), frames.size());
135 frames.resize(frame_num);
136 backtrace_log(frames.data(), frames.size());
137 error_log(LOG_DIVIDER);
138}
139
140static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
141 header->tag = DEBUG_TAG;
142 header->orig_pointer = orig_pointer;
143 header->size = size;
144 if (*g_malloc_zygote_child) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700145 header->set_zygote_child_alloc();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800146 }
147 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
148 if (header->usable_size == 0) {
149 g_dispatch->free(orig_pointer);
150 return nullptr;
151 }
152 header->usable_size -= g_debug->pointer_offset() +
Christopher Ferrisd0919622016-03-15 22:39:39 -0700153 reinterpret_cast<uintptr_t>(header) - reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800154
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700155 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800156 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700157 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800158 }
159
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700160 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800161 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700162 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800163 // If the rear guard is enabled, set the usable size to the exact size
164 // of the allocation.
165 header->usable_size = header->real_size();
166 }
167
168 bool backtrace_found = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700169 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800170 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700171 if (g_debug->backtrace->ShouldBacktrace()) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800172 back_header->num_frames = backtrace_get(
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700173 &back_header->frames[0], g_debug->config().backtrace_frames());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800174 backtrace_found = back_header->num_frames > 0;
175 } else {
176 back_header->num_frames = 0;
177 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800178 }
179
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700180 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800181 g_debug->track->Add(header, backtrace_found);
182 }
183
184 return g_debug->GetPointer(header);
185}
186
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100187bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
188 const char* options) {
189 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800190 return false;
191 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800192
193 InitAtfork();
194
Christopher Ferris63860cb2015-11-16 17:30:32 -0800195 g_malloc_zygote_child = malloc_zygote_child;
196
197 g_dispatch = malloc_dispatch;
198
199 if (!DebugDisableInitialize()) {
200 return false;
201 }
202
203 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100204 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800205 delete debug;
206 DebugDisableFinalize();
207 return false;
208 }
209 g_debug = debug;
210
211 // Always enable the backtrace code since we will use it in a number
212 // of different error cases.
213 backtrace_startup();
214
215 return true;
216}
217
218void debug_finalize() {
219 if (g_debug == nullptr) {
220 return;
221 }
222
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700223 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700224 g_debug->free_track->VerifyAll();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800225 }
226
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700227 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700228 g_debug->track->DisplayLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800229 }
230
Christopher Ferris602b88c2017-08-04 13:04:04 -0700231 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
232 ScopedDisableDebugCalls disable;
233 debug_dump_heap(
234 android::base::StringPrintf("%s.%d.exit.txt",
235 g_debug->config().backtrace_dump_prefix().c_str(), getpid()).c_str());
236 }
237
Christopher Ferris63860cb2015-11-16 17:30:32 -0800238 DebugDisableSet(true);
239
Colin Cross2c759912016-02-05 16:17:39 -0800240 backtrace_shutdown();
241
Christopher Ferris63860cb2015-11-16 17:30:32 -0800242 delete g_debug;
243 g_debug = nullptr;
244
245 DebugDisableFinalize();
246}
247
248void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
249 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
250 ScopedDisableDebugCalls disable;
251
252 // Verify the arguments.
253 if (info == nullptr || overall_size == nullptr || info_size == NULL ||
254 total_memory == nullptr || backtrace_size == nullptr) {
255 error_log("get_malloc_leak_info: At least one invalid parameter.");
256 return;
257 }
258
259 *info = nullptr;
260 *overall_size = 0;
261 *info_size = 0;
262 *total_memory = 0;
263 *backtrace_size = 0;
264
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700265 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800266 error_log("get_malloc_leak_info: Allocations not being tracked, to enable "
267 "set the option 'backtrace'.");
268 return;
269 }
270
Christopher Ferris55a89a42016-04-07 17:14:53 -0700271 g_debug->track->GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800272}
273
274void debug_free_malloc_leak_info(uint8_t* info) {
275 g_dispatch->free(info);
276}
277
Christopher Ferris55a89a42016-04-07 17:14:53 -0700278static size_t internal_malloc_usable_size(void* pointer) {
279 if (g_debug->need_header()) {
280 Header* header = g_debug->GetHeader(pointer);
281 if (header->tag != DEBUG_TAG) {
282 LogTagError(header, pointer, "malloc_usable_size");
283 return 0;
284 }
285
286 return header->usable_size;
287 } else {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800288 return g_dispatch->malloc_usable_size(pointer);
289 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800290}
291
Christopher Ferris55a89a42016-04-07 17:14:53 -0700292size_t debug_malloc_usable_size(void* pointer) {
293 if (DebugCallsDisabled() || pointer == nullptr) {
294 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800295 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700296 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800297
Christopher Ferris55a89a42016-04-07 17:14:53 -0700298 return internal_malloc_usable_size(pointer);
299}
300
301static void *internal_malloc(size_t size) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700302 if ((g_debug->config().options() & BACKTRACE) && g_debug->backtrace->ShouldDumpAndReset()) {
303 debug_dump_heap(
304 android::base::StringPrintf("%s.%d.txt",
305 g_debug->config().backtrace_dump_prefix().c_str(),
306 getpid()).c_str());
307 }
308
Colin Cross9567c7b2016-03-09 17:56:14 -0800309 if (size == 0) {
310 size = 1;
311 }
312
Christopher Ferris63860cb2015-11-16 17:30:32 -0800313 size_t real_size = size + g_debug->extra_bytes();
314 if (real_size < size) {
315 // Overflow.
316 errno = ENOMEM;
317 return nullptr;
318 }
319
320 void* pointer;
321 if (g_debug->need_header()) {
322 if (size > Header::max_size()) {
323 errno = ENOMEM;
324 return nullptr;
325 }
326
Christopher Ferris72df6702016-02-11 15:51:31 -0800327 Header* header = reinterpret_cast<Header*>(
328 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800329 if (header == nullptr) {
330 return nullptr;
331 }
332 pointer = InitHeader(header, header, size);
333 } else {
334 pointer = g_dispatch->malloc(real_size);
335 }
336
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700337 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700338 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700339 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800340 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700341 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800342 }
343 return pointer;
344}
345
Christopher Ferris55a89a42016-04-07 17:14:53 -0700346void* debug_malloc(size_t size) {
347 if (DebugCallsDisabled()) {
348 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800349 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700350 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800351
Christopher Ferris7bd01782016-04-20 12:30:58 -0700352 void* pointer = internal_malloc(size);
353
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700354 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700355 g_debug->record->AddEntry(new MallocEntry(pointer, size));
356 }
357
358 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700359}
360
361static void internal_free(void* pointer) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700362 if ((g_debug->config().options() & BACKTRACE) && g_debug->backtrace->ShouldDumpAndReset()) {
363 debug_dump_heap(
364 android::base::StringPrintf("%s.%d.txt",
365 g_debug->config().backtrace_dump_prefix().c_str(),
366 getpid()).c_str());
367 }
368
Christopher Ferris63860cb2015-11-16 17:30:32 -0800369 void* free_pointer = pointer;
370 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700371 Header* header;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800372 if (g_debug->need_header()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700373 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800374 if (header->tag != DEBUG_TAG) {
375 LogTagError(header, pointer, "free");
376 return;
377 }
378 free_pointer = header->orig_pointer;
379
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700380 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700381 if (!g_debug->front_guard->Valid(header)) {
382 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800383 }
384 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700385 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700386 if (!g_debug->rear_guard->Valid(header)) {
387 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800388 }
389 }
390
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700391 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800392 bool backtrace_found = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700393 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800394 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
395 backtrace_found = back_header->num_frames > 0;
396 }
397 g_debug->track->Remove(header, backtrace_found);
398 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800399 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800400
401 bytes = header->usable_size;
402 } else {
403 bytes = g_dispatch->malloc_usable_size(pointer);
404 }
405
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700406 if (g_debug->config().options() & FILL_ON_FREE) {
407 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800408 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700409 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800410 }
411
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700412 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700413 // Do not add the allocation until we are done modifying the pointer
414 // itself. This avoids a race if a lot of threads are all doing
415 // frees at the same time and we wind up trying to really free this
416 // pointer from another thread, while still trying to free it in
417 // this function.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700418 g_debug->free_track->Add(header);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700419 } else {
420 g_dispatch->free(free_pointer);
421 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800422}
423
Christopher Ferris55a89a42016-04-07 17:14:53 -0700424void debug_free(void* pointer) {
425 if (DebugCallsDisabled() || pointer == nullptr) {
426 return g_dispatch->free(pointer);
427 }
428 ScopedDisableDebugCalls disable;
429
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700430 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700431 g_debug->record->AddEntry(new FreeEntry(pointer));
432 }
433
Christopher Ferris55a89a42016-04-07 17:14:53 -0700434 internal_free(pointer);
435}
436
Christopher Ferris63860cb2015-11-16 17:30:32 -0800437void* debug_memalign(size_t alignment, size_t bytes) {
438 if (DebugCallsDisabled()) {
439 return g_dispatch->memalign(alignment, bytes);
440 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700441 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800442
Colin Cross9567c7b2016-03-09 17:56:14 -0800443 if (bytes == 0) {
444 bytes = 1;
445 }
446
Christopher Ferris63860cb2015-11-16 17:30:32 -0800447 void* pointer;
448 if (g_debug->need_header()) {
449 if (bytes > Header::max_size()) {
450 errno = ENOMEM;
451 return nullptr;
452 }
453
454 // Make the alignment a power of two.
455 if (!powerof2(alignment)) {
456 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
457 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800458 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800459 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800460 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
461 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800462 }
463
464 // We don't have any idea what the natural alignment of
465 // the underlying native allocator is, so we always need to
466 // over allocate.
467 size_t real_size = alignment + bytes + g_debug->extra_bytes();
468 if (real_size < bytes) {
469 // Overflow.
470 errno = ENOMEM;
471 return nullptr;
472 }
473
474 pointer = g_dispatch->malloc(real_size);
475 if (pointer == nullptr) {
476 return nullptr;
477 }
478
479 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
480 // Now align the pointer.
481 value += (-value % alignment);
482
483 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
484 pointer = InitHeader(header, pointer, bytes);
485 } else {
486 size_t real_size = bytes + g_debug->extra_bytes();
487 if (real_size < bytes) {
488 // Overflow.
489 errno = ENOMEM;
490 return nullptr;
491 }
492 pointer = g_dispatch->memalign(alignment, real_size);
493 }
494
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700495 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700496 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700497 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800498 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700499 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800500 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700501
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700502 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700503 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
504 }
505
Christopher Ferris63860cb2015-11-16 17:30:32 -0800506 return pointer;
507}
508
509void* debug_realloc(void* pointer, size_t bytes) {
510 if (DebugCallsDisabled()) {
511 return g_dispatch->realloc(pointer, bytes);
512 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700513 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800514
515 if (pointer == nullptr) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700516 pointer = internal_malloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700517 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700518 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
519 }
520 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800521 }
522
523 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700524 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700525 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
526 }
527
Christopher Ferris55a89a42016-04-07 17:14:53 -0700528 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800529 return nullptr;
530 }
531
532 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700533 if (g_debug->config().options() & EXPAND_ALLOC) {
534 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800535 if (real_size < bytes) {
536 // Overflow.
537 errno = ENOMEM;
538 return nullptr;
539 }
540 }
541
542 void* new_pointer;
543 size_t prev_size;
544 if (g_debug->need_header()) {
545 if (bytes > Header::max_size()) {
546 errno = ENOMEM;
547 return nullptr;
548 }
549
550 Header* header = g_debug->GetHeader(pointer);
551 if (header->tag != DEBUG_TAG) {
552 LogTagError(header, pointer, "realloc");
553 return nullptr;
554 }
555
556 // Same size, do nothing.
557 if (real_size == header->real_size()) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700558 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800559 return pointer;
560 }
561
562 // Allocation is shrinking.
563 if (real_size < header->usable_size) {
564 header->size = real_size;
565 if (*g_malloc_zygote_child) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700566 header->set_zygote_child_alloc();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800567 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700568 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800569 // Don't bother allocating a smaller pointer in this case, simply
570 // change the header usable_size and reset the rear guard.
571 header->usable_size = header->real_size();
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700572 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
573 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800574 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700575 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800576 return pointer;
577 }
578
579 // Allocate the new size.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700580 new_pointer = internal_malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800581 if (new_pointer == nullptr) {
582 errno = ENOMEM;
583 return nullptr;
584 }
585
586 prev_size = header->usable_size;
587 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700588 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800589 } else {
590 prev_size = g_dispatch->malloc_usable_size(pointer);
591 new_pointer = g_dispatch->realloc(pointer, real_size);
592 if (new_pointer == nullptr) {
593 return nullptr;
594 }
595 }
596
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700597 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700598 size_t bytes = internal_malloc_usable_size(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700599 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
600 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800601 }
602 if (bytes > prev_size) {
603 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700604 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800605 }
606 }
607
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700608 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700609 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
610 }
611
Christopher Ferris63860cb2015-11-16 17:30:32 -0800612 return new_pointer;
613}
614
615void* debug_calloc(size_t nmemb, size_t bytes) {
616 if (DebugCallsDisabled()) {
617 return g_dispatch->calloc(nmemb, bytes);
618 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700619 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800620
Colin Cross7877df62016-03-10 13:01:27 -0800621 size_t size;
622 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
623 // Overflow
624 errno = ENOMEM;
625 return nullptr;
626 }
627
Colin Cross9567c7b2016-03-09 17:56:14 -0800628 if (size == 0) {
629 size = 1;
630 }
631
Colin Cross7877df62016-03-10 13:01:27 -0800632 size_t real_size;
633 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800634 // Overflow.
635 errno = ENOMEM;
636 return nullptr;
637 }
638
Christopher Ferris7bd01782016-04-20 12:30:58 -0700639 void* pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800640 if (g_debug->need_header()) {
641 // The above check will guarantee the multiply will not overflow.
Colin Cross9567c7b2016-03-09 17:56:14 -0800642 if (size > Header::max_size()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800643 errno = ENOMEM;
644 return nullptr;
645 }
646
647 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800648 Header* header = reinterpret_cast<Header*>(
649 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800650 if (header == nullptr) {
651 return nullptr;
652 }
653 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700654 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800655 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700656 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800657 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700658 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700659 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
660 }
661 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800662}
663
664struct mallinfo debug_mallinfo() {
665 return g_dispatch->mallinfo();
666}
667
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700668int debug_mallopt(int param, int value) {
669 return g_dispatch->mallopt(param, value);
670}
671
Christopher Ferris63860cb2015-11-16 17:30:32 -0800672int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
673 if (DebugCallsDisabled()) {
674 return g_dispatch->posix_memalign(memptr, alignment, size);
675 }
676
677 if (!powerof2(alignment)) {
678 return EINVAL;
679 }
680 int saved_errno = errno;
681 *memptr = debug_memalign(alignment, size);
682 errno = saved_errno;
683 return (*memptr != nullptr) ? 0 : ENOMEM;
684}
685
Colin Cross869691c2016-01-29 12:48:18 -0800686int debug_iterate(uintptr_t base, size_t size,
687 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
688 // Can't allocate, malloc is disabled
689 // Manual capture of the arguments to pass to the lambda below as void* arg
690 struct iterate_ctx {
691 decltype(callback) callback;
692 decltype(arg) arg;
693 } ctx = { callback, arg };
694
695 return g_dispatch->iterate(base, size,
696 [](uintptr_t base, size_t size, void* arg) {
697 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
698 const void* pointer = reinterpret_cast<void*>(base);
699 if (g_debug->need_header()) {
700 const Header* header = reinterpret_cast<const Header*>(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700701 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross869691c2016-01-29 12:48:18 -0800702 if (g_debug->track->Contains(header)) {
703 // Return just the body of the allocation if we're sure the header exists
704 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
Colin Crossbaa7c6f2016-03-09 16:33:44 -0800705 header->usable_size, ctx->arg);
Colin Cross869691c2016-01-29 12:48:18 -0800706 return;
707 }
708 }
709 }
710 // Fall back to returning the whole allocation
711 ctx->callback(base, size, ctx->arg);
712 }, &ctx);
713}
714
715void debug_malloc_disable() {
716 g_dispatch->malloc_disable();
717 if (g_debug->track) {
718 g_debug->track->PrepareFork();
719 }
720}
721
722void debug_malloc_enable() {
723 if (g_debug->track) {
724 g_debug->track->PostForkParent();
725 }
726 g_dispatch->malloc_enable();
727}
728
Colin Cross2d4721c2016-02-02 11:57:54 -0800729ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
730 if (DebugCallsDisabled() || pointer == nullptr) {
731 return 0;
732 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700733 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800734
735 if (g_debug->need_header()) {
736 Header* header;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700737 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800738 header = g_debug->GetHeader(pointer);
739 if (!g_debug->track->Contains(header)) {
740 return 0;
741 }
742 } else {
743 header = reinterpret_cast<Header*>(pointer);
744 }
745 if (header->tag != DEBUG_TAG) {
746 return 0;
747 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700748 if (g_debug->config().options() & BACKTRACE) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800749 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
750 if (back_header->num_frames > 0) {
751 if (frame_count > back_header->num_frames) {
752 frame_count = back_header->num_frames;
753 }
754 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
755 return frame_count;
756 }
757 }
758 }
759
760 return 0;
761}
762
Christopher Ferris63860cb2015-11-16 17:30:32 -0800763#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
764void* debug_pvalloc(size_t bytes) {
765 if (DebugCallsDisabled()) {
766 return g_dispatch->pvalloc(bytes);
767 }
768
769 size_t pagesize = getpagesize();
770 size_t size = BIONIC_ALIGN(bytes, pagesize);
771 if (size < bytes) {
772 // Overflow
773 errno = ENOMEM;
774 return nullptr;
775 }
776 return debug_memalign(pagesize, size);
777}
778
779void* debug_valloc(size_t size) {
780 if (DebugCallsDisabled()) {
781 return g_dispatch->valloc(size);
782 }
783 return debug_memalign(getpagesize(), size);
784}
785#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700786
787static std::mutex g_dump_lock;
788
789bool debug_dump_heap(const char* file_name) {
790 ScopedDisableDebugCalls disable;
791
792 std::lock_guard<std::mutex> guard(g_dump_lock);
793
794 FILE* fp = fopen(file_name, "w+e");
795 if (fp == nullptr) {
796 error_log("Unable to create file: %s", file_name);
797 return false;
798 }
799 error_log("Dumping to file: %s\n", file_name);
800
801 if (!(g_debug->config().options() & BACKTRACE)) {
802 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
803 fprintf(fp, "# adb shell stop\n");
804 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
805 fprintf(fp, "# adb shell start\n");
806 fclose(fp);
807 return false;
808 }
809
810 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
811
812 std::vector<const Header*> list;
813 size_t total_memory;
814 g_debug->track->GetListBySizeThenBacktrace(&list, &total_memory);
815 fprintf(fp, "Total memory: %zu\n", total_memory);
816 fprintf(fp, "Allocation records: %zd\n", list.size());
817 fprintf(fp, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
818 fprintf(fp, "\n");
819
820 for (const auto& header : list) {
821 const BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
822 fprintf(fp, "z %d sz %8zu num 1 bt", (header->zygote_child_alloc()) ? 1 : 0,
823 header->real_size());
824 for (size_t i = 0; i < back_header->num_frames; i++) {
825 if (back_header->frames[i] == 0) {
826 break;
827 }
828#ifdef __LP64__
829 fprintf(fp, " %016" PRIxPTR, back_header->frames[i]);
830#else
831 fprintf(fp, " %08" PRIxPTR, back_header->frames[i]);
832#endif
833 }
834 fprintf(fp, "\n");
835 }
836
837 fprintf(fp, "MAPS\n");
838 std::string content;
839 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
840 fprintf(fp, "Could not open /proc/self/maps\n");
841 } else {
842 fprintf(fp, "%s", content.c_str());
843 }
844 fprintf(fp, "END\n");
845 fclose(fp);
846 return true;
847}