blob: ecfbd7112fbbdf0d541094afe8f7a5a6939b020f [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);
Christopher Ferriscae21a92018-02-05 18:14:55 -080080void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080081void* debug_memalign(size_t alignment, size_t bytes);
82void* debug_realloc(void* pointer, size_t bytes);
83void* debug_calloc(size_t nmemb, size_t bytes);
84struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070085int debug_mallopt(int param, int value);
Christopher Ferris63860cb2015-11-16 17:30:32 -080086int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080087int debug_iterate(uintptr_t base, size_t size,
88 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
89void debug_malloc_disable();
90void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080091
92#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
93void* debug_pvalloc(size_t bytes);
94void* debug_valloc(size_t size);
95#endif
96
97__END_DECLS
98// ------------------------------------------------------------------------
99
Colin Cross7a28a3c2016-02-07 22:51:15 -0800100static void InitAtfork() {
101 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
102 pthread_once(&atfork_init, [](){
103 pthread_atfork(
104 [](){
105 if (g_debug != nullptr) {
106 g_debug->PrepareFork();
107 }
108 },
109 [](){
110 if (g_debug != nullptr) {
111 g_debug->PostForkParent();
112 }
113 },
114 [](){
115 if (g_debug != nullptr) {
116 g_debug->PostForkChild();
117 }
118 }
119 );
120 });
121}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700122
Christopher Ferris63860cb2015-11-16 17:30:32 -0800123static void LogTagError(const Header* header, const void* pointer, const char* name) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800124 error_log(LOG_DIVIDER);
Christopher Ferris7993b802016-01-28 18:35:05 -0800125 if (header->tag == DEBUG_FREE_TAG) {
126 error_log("+++ ALLOCATION %p USED AFTER FREE (%s)", pointer, name);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700127 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800128 g_debug->free_track->LogBacktrace(header);
129 }
130 } else {
131 error_log("+++ ALLOCATION %p HAS INVALID TAG %" PRIx32 " (%s)", pointer, header->tag, name);
132 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800133 error_log("Backtrace at time of failure:");
134 std::vector<uintptr_t> frames(64);
135 size_t frame_num = backtrace_get(frames.data(), frames.size());
136 frames.resize(frame_num);
137 backtrace_log(frames.data(), frames.size());
138 error_log(LOG_DIVIDER);
139}
140
141static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
142 header->tag = DEBUG_TAG;
143 header->orig_pointer = orig_pointer;
144 header->size = size;
145 if (*g_malloc_zygote_child) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700146 header->set_zygote_child_alloc();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800147 }
148 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
149 if (header->usable_size == 0) {
150 g_dispatch->free(orig_pointer);
151 return nullptr;
152 }
153 header->usable_size -= g_debug->pointer_offset() +
Christopher Ferrisd0919622016-03-15 22:39:39 -0700154 reinterpret_cast<uintptr_t>(header) - reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800155
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700156 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800157 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700158 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800159 }
160
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700161 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800162 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700163 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800164 // If the rear guard is enabled, set the usable size to the exact size
165 // of the allocation.
166 header->usable_size = header->real_size();
167 }
168
169 bool backtrace_found = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700170 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800171 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700172 if (g_debug->backtrace->ShouldBacktrace()) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800173 back_header->num_frames = backtrace_get(
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700174 &back_header->frames[0], g_debug->config().backtrace_frames());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800175 backtrace_found = back_header->num_frames > 0;
176 } else {
177 back_header->num_frames = 0;
178 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800179 }
180
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700181 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800182 g_debug->track->Add(header, backtrace_found);
183 }
184
185 return g_debug->GetPointer(header);
186}
187
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100188bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
189 const char* options) {
190 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800191 return false;
192 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800193
194 InitAtfork();
195
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196 g_malloc_zygote_child = malloc_zygote_child;
197
198 g_dispatch = malloc_dispatch;
199
200 if (!DebugDisableInitialize()) {
201 return false;
202 }
203
204 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100205 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800206 delete debug;
207 DebugDisableFinalize();
208 return false;
209 }
210 g_debug = debug;
211
212 // Always enable the backtrace code since we will use it in a number
213 // of different error cases.
214 backtrace_startup();
215
216 return true;
217}
218
219void debug_finalize() {
220 if (g_debug == nullptr) {
221 return;
222 }
223
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700224 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700225 g_debug->free_track->VerifyAll();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800226 }
227
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700228 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700229 g_debug->track->DisplayLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800230 }
231
Christopher Ferris602b88c2017-08-04 13:04:04 -0700232 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
233 ScopedDisableDebugCalls disable;
234 debug_dump_heap(
235 android::base::StringPrintf("%s.%d.exit.txt",
236 g_debug->config().backtrace_dump_prefix().c_str(), getpid()).c_str());
237 }
238
Christopher Ferris63860cb2015-11-16 17:30:32 -0800239 DebugDisableSet(true);
240
Colin Cross2c759912016-02-05 16:17:39 -0800241 backtrace_shutdown();
242
Christopher Ferris63860cb2015-11-16 17:30:32 -0800243 delete g_debug;
244 g_debug = nullptr;
245
246 DebugDisableFinalize();
247}
248
249void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
250 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
251 ScopedDisableDebugCalls disable;
252
253 // Verify the arguments.
254 if (info == nullptr || overall_size == nullptr || info_size == NULL ||
255 total_memory == nullptr || backtrace_size == nullptr) {
256 error_log("get_malloc_leak_info: At least one invalid parameter.");
257 return;
258 }
259
260 *info = nullptr;
261 *overall_size = 0;
262 *info_size = 0;
263 *total_memory = 0;
264 *backtrace_size = 0;
265
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700266 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800267 error_log("get_malloc_leak_info: Allocations not being tracked, to enable "
268 "set the option 'backtrace'.");
269 return;
270 }
271
Christopher Ferris55a89a42016-04-07 17:14:53 -0700272 g_debug->track->GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800273}
274
275void debug_free_malloc_leak_info(uint8_t* info) {
276 g_dispatch->free(info);
277}
278
Christopher Ferris55a89a42016-04-07 17:14:53 -0700279static size_t internal_malloc_usable_size(void* pointer) {
280 if (g_debug->need_header()) {
281 Header* header = g_debug->GetHeader(pointer);
282 if (header->tag != DEBUG_TAG) {
283 LogTagError(header, pointer, "malloc_usable_size");
284 return 0;
285 }
286
287 return header->usable_size;
288 } else {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800289 return g_dispatch->malloc_usable_size(pointer);
290 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800291}
292
Christopher Ferris55a89a42016-04-07 17:14:53 -0700293size_t debug_malloc_usable_size(void* pointer) {
294 if (DebugCallsDisabled() || pointer == nullptr) {
295 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800296 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700297 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800298
Christopher Ferris55a89a42016-04-07 17:14:53 -0700299 return internal_malloc_usable_size(pointer);
300}
301
302static void *internal_malloc(size_t size) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700303 if ((g_debug->config().options() & BACKTRACE) && g_debug->backtrace->ShouldDumpAndReset()) {
304 debug_dump_heap(
305 android::base::StringPrintf("%s.%d.txt",
306 g_debug->config().backtrace_dump_prefix().c_str(),
307 getpid()).c_str());
308 }
309
Colin Cross9567c7b2016-03-09 17:56:14 -0800310 if (size == 0) {
311 size = 1;
312 }
313
Christopher Ferris63860cb2015-11-16 17:30:32 -0800314 size_t real_size = size + g_debug->extra_bytes();
315 if (real_size < size) {
316 // Overflow.
317 errno = ENOMEM;
318 return nullptr;
319 }
320
321 void* pointer;
322 if (g_debug->need_header()) {
323 if (size > Header::max_size()) {
324 errno = ENOMEM;
325 return nullptr;
326 }
327
Christopher Ferris72df6702016-02-11 15:51:31 -0800328 Header* header = reinterpret_cast<Header*>(
329 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800330 if (header == nullptr) {
331 return nullptr;
332 }
333 pointer = InitHeader(header, header, size);
334 } else {
335 pointer = g_dispatch->malloc(real_size);
336 }
337
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700338 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700339 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700340 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800341 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700342 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800343 }
344 return pointer;
345}
346
Christopher Ferris55a89a42016-04-07 17:14:53 -0700347void* debug_malloc(size_t size) {
348 if (DebugCallsDisabled()) {
349 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800350 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700351 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800352
Christopher Ferris7bd01782016-04-20 12:30:58 -0700353 void* pointer = internal_malloc(size);
354
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700355 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700356 g_debug->record->AddEntry(new MallocEntry(pointer, size));
357 }
358
359 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700360}
361
362static void internal_free(void* pointer) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700363 if ((g_debug->config().options() & BACKTRACE) && g_debug->backtrace->ShouldDumpAndReset()) {
364 debug_dump_heap(
365 android::base::StringPrintf("%s.%d.txt",
366 g_debug->config().backtrace_dump_prefix().c_str(),
367 getpid()).c_str());
368 }
369
Christopher Ferris63860cb2015-11-16 17:30:32 -0800370 void* free_pointer = pointer;
371 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700372 Header* header;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800373 if (g_debug->need_header()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700374 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800375 if (header->tag != DEBUG_TAG) {
376 LogTagError(header, pointer, "free");
377 return;
378 }
379 free_pointer = header->orig_pointer;
380
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700381 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700382 if (!g_debug->front_guard->Valid(header)) {
383 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800384 }
385 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700386 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700387 if (!g_debug->rear_guard->Valid(header)) {
388 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800389 }
390 }
391
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700392 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800393 bool backtrace_found = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700394 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800395 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
396 backtrace_found = back_header->num_frames > 0;
397 }
398 g_debug->track->Remove(header, backtrace_found);
399 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800400 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800401
402 bytes = header->usable_size;
403 } else {
404 bytes = g_dispatch->malloc_usable_size(pointer);
405 }
406
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700407 if (g_debug->config().options() & FILL_ON_FREE) {
408 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800409 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700410 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800411 }
412
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700413 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700414 // Do not add the allocation until we are done modifying the pointer
415 // itself. This avoids a race if a lot of threads are all doing
416 // frees at the same time and we wind up trying to really free this
417 // pointer from another thread, while still trying to free it in
418 // this function.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700419 g_debug->free_track->Add(header);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700420 } else {
421 g_dispatch->free(free_pointer);
422 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800423}
424
Christopher Ferris55a89a42016-04-07 17:14:53 -0700425void debug_free(void* pointer) {
426 if (DebugCallsDisabled() || pointer == nullptr) {
427 return g_dispatch->free(pointer);
428 }
429 ScopedDisableDebugCalls disable;
430
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700431 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700432 g_debug->record->AddEntry(new FreeEntry(pointer));
433 }
434
Christopher Ferris55a89a42016-04-07 17:14:53 -0700435 internal_free(pointer);
436}
437
Christopher Ferris63860cb2015-11-16 17:30:32 -0800438void* debug_memalign(size_t alignment, size_t bytes) {
439 if (DebugCallsDisabled()) {
440 return g_dispatch->memalign(alignment, bytes);
441 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700442 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800443
Colin Cross9567c7b2016-03-09 17:56:14 -0800444 if (bytes == 0) {
445 bytes = 1;
446 }
447
Christopher Ferris63860cb2015-11-16 17:30:32 -0800448 void* pointer;
449 if (g_debug->need_header()) {
450 if (bytes > Header::max_size()) {
451 errno = ENOMEM;
452 return nullptr;
453 }
454
455 // Make the alignment a power of two.
456 if (!powerof2(alignment)) {
457 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
458 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800459 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800460 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800461 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
462 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800463 }
464
465 // We don't have any idea what the natural alignment of
466 // the underlying native allocator is, so we always need to
467 // over allocate.
468 size_t real_size = alignment + bytes + g_debug->extra_bytes();
469 if (real_size < bytes) {
470 // Overflow.
471 errno = ENOMEM;
472 return nullptr;
473 }
474
475 pointer = g_dispatch->malloc(real_size);
476 if (pointer == nullptr) {
477 return nullptr;
478 }
479
480 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
481 // Now align the pointer.
482 value += (-value % alignment);
483
484 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
485 pointer = InitHeader(header, pointer, bytes);
486 } else {
487 size_t real_size = bytes + g_debug->extra_bytes();
488 if (real_size < bytes) {
489 // Overflow.
490 errno = ENOMEM;
491 return nullptr;
492 }
493 pointer = g_dispatch->memalign(alignment, real_size);
494 }
495
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700496 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700497 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700498 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800499 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700500 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800501 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700502
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700503 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700504 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
505 }
506
Christopher Ferris63860cb2015-11-16 17:30:32 -0800507 return pointer;
508}
509
510void* debug_realloc(void* pointer, size_t bytes) {
511 if (DebugCallsDisabled()) {
512 return g_dispatch->realloc(pointer, bytes);
513 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700514 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800515
516 if (pointer == nullptr) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700517 pointer = internal_malloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700518 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700519 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
520 }
521 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800522 }
523
524 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700525 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700526 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
527 }
528
Christopher Ferris55a89a42016-04-07 17:14:53 -0700529 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800530 return nullptr;
531 }
532
533 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700534 if (g_debug->config().options() & EXPAND_ALLOC) {
535 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800536 if (real_size < bytes) {
537 // Overflow.
538 errno = ENOMEM;
539 return nullptr;
540 }
541 }
542
543 void* new_pointer;
544 size_t prev_size;
545 if (g_debug->need_header()) {
546 if (bytes > Header::max_size()) {
547 errno = ENOMEM;
548 return nullptr;
549 }
550
551 Header* header = g_debug->GetHeader(pointer);
552 if (header->tag != DEBUG_TAG) {
553 LogTagError(header, pointer, "realloc");
554 return nullptr;
555 }
556
557 // Same size, do nothing.
558 if (real_size == header->real_size()) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700559 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800560 return pointer;
561 }
562
563 // Allocation is shrinking.
564 if (real_size < header->usable_size) {
565 header->size = real_size;
566 if (*g_malloc_zygote_child) {
Christopher Ferris602b88c2017-08-04 13:04:04 -0700567 header->set_zygote_child_alloc();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800568 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700569 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800570 // Don't bother allocating a smaller pointer in this case, simply
571 // change the header usable_size and reset the rear guard.
572 header->usable_size = header->real_size();
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700573 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
574 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800575 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700576 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800577 return pointer;
578 }
579
580 // Allocate the new size.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700581 new_pointer = internal_malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800582 if (new_pointer == nullptr) {
583 errno = ENOMEM;
584 return nullptr;
585 }
586
587 prev_size = header->usable_size;
588 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700589 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800590 } else {
591 prev_size = g_dispatch->malloc_usable_size(pointer);
592 new_pointer = g_dispatch->realloc(pointer, real_size);
593 if (new_pointer == nullptr) {
594 return nullptr;
595 }
596 }
597
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700598 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700599 size_t bytes = internal_malloc_usable_size(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700600 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
601 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800602 }
603 if (bytes > prev_size) {
604 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700605 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800606 }
607 }
608
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700609 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700610 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
611 }
612
Christopher Ferris63860cb2015-11-16 17:30:32 -0800613 return new_pointer;
614}
615
616void* debug_calloc(size_t nmemb, size_t bytes) {
617 if (DebugCallsDisabled()) {
618 return g_dispatch->calloc(nmemb, bytes);
619 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700620 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800621
Colin Cross7877df62016-03-10 13:01:27 -0800622 size_t size;
623 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
624 // Overflow
625 errno = ENOMEM;
626 return nullptr;
627 }
628
Colin Cross9567c7b2016-03-09 17:56:14 -0800629 if (size == 0) {
630 size = 1;
631 }
632
Colin Cross7877df62016-03-10 13:01:27 -0800633 size_t real_size;
634 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800635 // Overflow.
636 errno = ENOMEM;
637 return nullptr;
638 }
639
Christopher Ferris7bd01782016-04-20 12:30:58 -0700640 void* pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800641 if (g_debug->need_header()) {
642 // The above check will guarantee the multiply will not overflow.
Colin Cross9567c7b2016-03-09 17:56:14 -0800643 if (size > Header::max_size()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800644 errno = ENOMEM;
645 return nullptr;
646 }
647
648 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800649 Header* header = reinterpret_cast<Header*>(
650 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800651 if (header == nullptr) {
652 return nullptr;
653 }
654 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700655 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800656 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700657 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800658 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700659 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700660 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
661 }
662 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800663}
664
665struct mallinfo debug_mallinfo() {
666 return g_dispatch->mallinfo();
667}
668
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700669int debug_mallopt(int param, int value) {
670 return g_dispatch->mallopt(param, value);
671}
672
Christopher Ferriscae21a92018-02-05 18:14:55 -0800673void* debug_aligned_alloc(size_t alignment, size_t size) {
674 if (DebugCallsDisabled()) {
675 return g_dispatch->aligned_alloc(alignment, size);
676 }
677 if (!powerof2(alignment)) {
678 errno = EINVAL;
679 return nullptr;
680 }
681 return debug_memalign(alignment, size);
682}
683
Christopher Ferris63860cb2015-11-16 17:30:32 -0800684int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
685 if (DebugCallsDisabled()) {
686 return g_dispatch->posix_memalign(memptr, alignment, size);
687 }
688
689 if (!powerof2(alignment)) {
690 return EINVAL;
691 }
692 int saved_errno = errno;
693 *memptr = debug_memalign(alignment, size);
694 errno = saved_errno;
695 return (*memptr != nullptr) ? 0 : ENOMEM;
696}
697
Colin Cross869691c2016-01-29 12:48:18 -0800698int debug_iterate(uintptr_t base, size_t size,
699 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
700 // Can't allocate, malloc is disabled
701 // Manual capture of the arguments to pass to the lambda below as void* arg
702 struct iterate_ctx {
703 decltype(callback) callback;
704 decltype(arg) arg;
705 } ctx = { callback, arg };
706
707 return g_dispatch->iterate(base, size,
708 [](uintptr_t base, size_t size, void* arg) {
709 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
710 const void* pointer = reinterpret_cast<void*>(base);
711 if (g_debug->need_header()) {
712 const Header* header = reinterpret_cast<const Header*>(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700713 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross869691c2016-01-29 12:48:18 -0800714 if (g_debug->track->Contains(header)) {
715 // Return just the body of the allocation if we're sure the header exists
716 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
Colin Crossbaa7c6f2016-03-09 16:33:44 -0800717 header->usable_size, ctx->arg);
Colin Cross869691c2016-01-29 12:48:18 -0800718 return;
719 }
720 }
721 }
722 // Fall back to returning the whole allocation
723 ctx->callback(base, size, ctx->arg);
724 }, &ctx);
725}
726
727void debug_malloc_disable() {
728 g_dispatch->malloc_disable();
729 if (g_debug->track) {
730 g_debug->track->PrepareFork();
731 }
732}
733
734void debug_malloc_enable() {
735 if (g_debug->track) {
736 g_debug->track->PostForkParent();
737 }
738 g_dispatch->malloc_enable();
739}
740
Colin Cross2d4721c2016-02-02 11:57:54 -0800741ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
742 if (DebugCallsDisabled() || pointer == nullptr) {
743 return 0;
744 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700745 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800746
747 if (g_debug->need_header()) {
748 Header* header;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700749 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800750 header = g_debug->GetHeader(pointer);
751 if (!g_debug->track->Contains(header)) {
752 return 0;
753 }
754 } else {
755 header = reinterpret_cast<Header*>(pointer);
756 }
757 if (header->tag != DEBUG_TAG) {
758 return 0;
759 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700760 if (g_debug->config().options() & BACKTRACE) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800761 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
762 if (back_header->num_frames > 0) {
763 if (frame_count > back_header->num_frames) {
764 frame_count = back_header->num_frames;
765 }
766 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
767 return frame_count;
768 }
769 }
770 }
771
772 return 0;
773}
774
Christopher Ferris63860cb2015-11-16 17:30:32 -0800775#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
776void* debug_pvalloc(size_t bytes) {
777 if (DebugCallsDisabled()) {
778 return g_dispatch->pvalloc(bytes);
779 }
780
781 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700782 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800783 if (size < bytes) {
784 // Overflow
785 errno = ENOMEM;
786 return nullptr;
787 }
788 return debug_memalign(pagesize, size);
789}
790
791void* debug_valloc(size_t size) {
792 if (DebugCallsDisabled()) {
793 return g_dispatch->valloc(size);
794 }
795 return debug_memalign(getpagesize(), size);
796}
797#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700798
799static std::mutex g_dump_lock;
800
801bool debug_dump_heap(const char* file_name) {
802 ScopedDisableDebugCalls disable;
803
804 std::lock_guard<std::mutex> guard(g_dump_lock);
805
806 FILE* fp = fopen(file_name, "w+e");
807 if (fp == nullptr) {
808 error_log("Unable to create file: %s", file_name);
809 return false;
810 }
811 error_log("Dumping to file: %s\n", file_name);
812
813 if (!(g_debug->config().options() & BACKTRACE)) {
814 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
815 fprintf(fp, "# adb shell stop\n");
816 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
817 fprintf(fp, "# adb shell start\n");
818 fclose(fp);
819 return false;
820 }
821
822 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
823
824 std::vector<const Header*> list;
825 size_t total_memory;
826 g_debug->track->GetListBySizeThenBacktrace(&list, &total_memory);
827 fprintf(fp, "Total memory: %zu\n", total_memory);
828 fprintf(fp, "Allocation records: %zd\n", list.size());
829 fprintf(fp, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
830 fprintf(fp, "\n");
831
832 for (const auto& header : list) {
833 const BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
834 fprintf(fp, "z %d sz %8zu num 1 bt", (header->zygote_child_alloc()) ? 1 : 0,
835 header->real_size());
836 for (size_t i = 0; i < back_header->num_frames; i++) {
837 if (back_header->frames[i] == 0) {
838 break;
839 }
840#ifdef __LP64__
841 fprintf(fp, " %016" PRIxPTR, back_header->frames[i]);
842#else
843 fprintf(fp, " %08" PRIxPTR, back_header->frames[i]);
844#endif
845 }
846 fprintf(fp, "\n");
847 }
848
849 fprintf(fp, "MAPS\n");
850 std::string content;
851 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
852 fprintf(fp, "Could not open /proc/self/maps\n");
853 } else {
854 fprintf(fp, "%s", content.c_str());
855 }
856 fprintf(fp, "END\n");
857 fclose(fp);
858 return true;
859}