blob: b866e54faee93366abb857306284370629659de9 [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
Christopher Ferris72df6702016-02-11 15:51:31 -080044#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080045#include "DebugData.h"
Christopher Ferris8b70a022018-03-07 13:38:48 -080046#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080047#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,
Christopher Ferris8b70a022018-03-07 13:38:48 -080069 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 Ferris8b70a022018-03-07 13:38:48 -080072void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
73 size_t* total_memory, size_t* backtrace_size);
Colin Cross2d4721c2016-02-02 11:57:54 -080074ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080075void debug_free_malloc_leak_info(uint8_t* info);
76size_t debug_malloc_usable_size(void* pointer);
77void* debug_malloc(size_t size);
78void debug_free(void* pointer);
Christopher Ferrisd69ee592018-02-05 18:14:55 -080079void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080080void* 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,
Christopher Ferris8b70a022018-03-07 13:38:48 -080087 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -080088void 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;
Christopher Ferris8b70a022018-03-07 13:38:48 -0800101 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800102 pthread_atfork(
Christopher Ferris8b70a022018-03-07 13:38:48 -0800103 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800104 if (g_debug != nullptr) {
105 g_debug->PrepareFork();
106 }
107 },
Christopher Ferris8b70a022018-03-07 13:38:48 -0800108 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800109 if (g_debug != nullptr) {
110 g_debug->PostForkParent();
111 }
112 },
Christopher Ferris8b70a022018-03-07 13:38:48 -0800113 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800114 if (g_debug != nullptr) {
115 g_debug->PostForkChild();
116 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800117 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800118 });
119}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700120
Christopher Ferris8b70a022018-03-07 13:38:48 -0800121static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800122 error_log(LOG_DIVIDER);
Christopher Ferris8b70a022018-03-07 13:38:48 -0800123 error_log("+++ ALLOCATION %p %s", pointer, error_str);
124
125 // If we are tracking already freed pointers, check to see if this is
126 // one so we can print extra information.
127 if (g_debug->config().options() & FREE_TRACK) {
128 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800129 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800130
131 std::vector<uintptr_t> frames(128);
132 size_t num_frames = backtrace_get(frames.data(), frames.size());
133 if (num_frames == 0) {
134 error_log("Backtrace failed to get any frames.");
135 } else {
136 error_log("Backtrace at time of failure:");
137 backtrace_log(frames.data(), num_frames);
138 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800139 error_log(LOG_DIVIDER);
140}
141
Christopher Ferris8b70a022018-03-07 13:38:48 -0800142static bool VerifyPointer(const void* pointer, const char* function_name) {
143 if (g_debug->HeaderEnabled()) {
144 Header* header = g_debug->GetHeader(pointer);
145 if (header->tag != DEBUG_TAG) {
146 std::string error_str;
147 if (header->tag == DEBUG_FREE_TAG) {
148 error_str = std::string("USED AFTER FREE (") + function_name + ")";
149 } else {
150 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
151 function_name);
152 }
153 LogError(pointer, error_str.c_str());
154 return false;
155 }
156 }
157
158 if (g_debug->TrackPointers()) {
159 if (!PointerData::Exists(pointer)) {
160 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
161 LogError(pointer, error_str.c_str());
162 return false;
163 }
164 }
165 return true;
166}
167
168static size_t InternalMallocUsableSize(void* pointer) {
169 if (g_debug->HeaderEnabled()) {
170 return g_debug->GetHeader(pointer)->usable_size;
171 } else {
172 return g_dispatch->malloc_usable_size(pointer);
173 }
174}
175
Christopher Ferris63860cb2015-11-16 17:30:32 -0800176static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
177 header->tag = DEBUG_TAG;
178 header->orig_pointer = orig_pointer;
179 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800180 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
181 if (header->usable_size == 0) {
182 g_dispatch->free(orig_pointer);
183 return nullptr;
184 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800185 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
186 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800187
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700188 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800189 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700190 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800191 }
192
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700193 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800194 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700195 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196 // If the rear guard is enabled, set the usable size to the exact size
197 // of the allocation.
Christopher Ferris8b70a022018-03-07 13:38:48 -0800198 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800199 }
200
201 return g_debug->GetPointer(header);
202}
203
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100204bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris8b70a022018-03-07 13:38:48 -0800205 const char* options) {
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100206 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800207 return false;
208 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800209
210 InitAtfork();
211
Christopher Ferris63860cb2015-11-16 17:30:32 -0800212 g_malloc_zygote_child = malloc_zygote_child;
213
214 g_dispatch = malloc_dispatch;
215
216 if (!DebugDisableInitialize()) {
217 return false;
218 }
219
220 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100221 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800222 delete debug;
223 DebugDisableFinalize();
224 return false;
225 }
226 g_debug = debug;
227
228 // Always enable the backtrace code since we will use it in a number
229 // of different error cases.
230 backtrace_startup();
231
232 return true;
233}
234
235void debug_finalize() {
236 if (g_debug == nullptr) {
237 return;
238 }
239
Christopher Ferris6fffb232018-07-10 14:45:24 -0700240 // Turn off capturing allocations calls.
241 DebugDisableSet(true);
242
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700243 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800244 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800245 }
246
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700247 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800248 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800249 }
250
Christopher Ferris602b88c2017-08-04 13:04:04 -0700251 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800252 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
253 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris6fffb232018-07-10 14:45:24 -0700254 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700255 }
256
Colin Cross2c759912016-02-05 16:17:39 -0800257 backtrace_shutdown();
258
Christopher Ferris63860cb2015-11-16 17:30:32 -0800259 delete g_debug;
260 g_debug = nullptr;
261
262 DebugDisableFinalize();
263}
264
Christopher Ferris8b70a022018-03-07 13:38:48 -0800265void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
266 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800267 ScopedDisableDebugCalls disable;
268
269 // Verify the arguments.
Christopher Ferris8b70a022018-03-07 13:38:48 -0800270 if (info == nullptr || overall_size == nullptr || info_size == NULL || total_memory == nullptr ||
271 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800272 error_log("get_malloc_leak_info: At least one invalid parameter.");
273 return;
274 }
275
276 *info = nullptr;
277 *overall_size = 0;
278 *info_size = 0;
279 *total_memory = 0;
280 *backtrace_size = 0;
281
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700282 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800283 error_log(
284 "get_malloc_leak_info: Allocations not being tracked, to enable "
285 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800286 return;
287 }
288
Christopher Ferris8b70a022018-03-07 13:38:48 -0800289 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800290}
291
292void debug_free_malloc_leak_info(uint8_t* info) {
293 g_dispatch->free(info);
294}
295
Christopher Ferris55a89a42016-04-07 17:14:53 -0700296size_t debug_malloc_usable_size(void* pointer) {
297 if (DebugCallsDisabled() || pointer == nullptr) {
298 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800299 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700300 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800301
Christopher Ferris8b70a022018-03-07 13:38:48 -0800302 if (!VerifyPointer(pointer, "malloc_usable_size")) {
303 return 0;
304 }
305
306 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700307}
308
Christopher Ferris8b70a022018-03-07 13:38:48 -0800309static void* InternalMalloc(size_t size) {
310 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
311 debug_dump_heap(android::base::StringPrintf(
312 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
313 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700314 }
315
Colin Cross9567c7b2016-03-09 17:56:14 -0800316 if (size == 0) {
317 size = 1;
318 }
319
Christopher Ferris63860cb2015-11-16 17:30:32 -0800320 size_t real_size = size + g_debug->extra_bytes();
321 if (real_size < size) {
322 // Overflow.
323 errno = ENOMEM;
324 return nullptr;
325 }
326
Christopher Ferris8b70a022018-03-07 13:38:48 -0800327 if (size > PointerInfoType::MaxSize()) {
328 errno = ENOMEM;
329 return nullptr;
330 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800331
Christopher Ferris8b70a022018-03-07 13:38:48 -0800332 void* pointer;
333 if (g_debug->HeaderEnabled()) {
334 Header* header =
335 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800336 if (header == nullptr) {
337 return nullptr;
338 }
339 pointer = InitHeader(header, header, size);
340 } else {
341 pointer = g_dispatch->malloc(real_size);
342 }
343
Christopher Ferris8b70a022018-03-07 13:38:48 -0800344 if (pointer != nullptr) {
345 if (g_debug->TrackPointers()) {
346 PointerData::Add(pointer, size);
347 }
348
349 if (g_debug->config().options() & FILL_ON_ALLOC) {
350 size_t bytes = InternalMallocUsableSize(pointer);
351 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
352 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
353 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
354 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800355 }
356 return pointer;
357}
358
Christopher Ferris55a89a42016-04-07 17:14:53 -0700359void* debug_malloc(size_t size) {
360 if (DebugCallsDisabled()) {
361 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800362 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700363 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800364
Christopher Ferris8b70a022018-03-07 13:38:48 -0800365 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700366
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700367 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700368 g_debug->record->AddEntry(new MallocEntry(pointer, size));
369 }
370
371 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700372}
373
Christopher Ferris8b70a022018-03-07 13:38:48 -0800374static void InternalFree(void* pointer) {
375 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
376 debug_dump_heap(android::base::StringPrintf(
377 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
378 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700379 }
380
Christopher Ferris63860cb2015-11-16 17:30:32 -0800381 void* free_pointer = pointer;
382 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700383 Header* header;
Christopher Ferris8b70a022018-03-07 13:38:48 -0800384 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700385 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800386 free_pointer = header->orig_pointer;
387
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700388 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700389 if (!g_debug->front_guard->Valid(header)) {
390 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800391 }
392 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700393 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700394 if (!g_debug->rear_guard->Valid(header)) {
395 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800396 }
397 }
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 Ferris8b70a022018-03-07 13:38:48 -0800412 if (g_debug->TrackPointers()) {
413 PointerData::Remove(pointer);
414 }
415
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700416 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700417 // Do not add the allocation until we are done modifying the pointer
418 // itself. This avoids a race if a lot of threads are all doing
419 // frees at the same time and we wind up trying to really free this
420 // pointer from another thread, while still trying to free it in
421 // this function.
Christopher Ferris8b70a022018-03-07 13:38:48 -0800422 pointer = PointerData::AddFreed(pointer);
423 if (pointer != nullptr) {
424 if (g_debug->HeaderEnabled()) {
425 pointer = g_debug->GetHeader(pointer)->orig_pointer;
426 }
427 g_dispatch->free(pointer);
428 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700429 } else {
430 g_dispatch->free(free_pointer);
431 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800432}
433
Christopher Ferris55a89a42016-04-07 17:14:53 -0700434void debug_free(void* pointer) {
435 if (DebugCallsDisabled() || pointer == nullptr) {
436 return g_dispatch->free(pointer);
437 }
438 ScopedDisableDebugCalls disable;
439
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700440 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700441 g_debug->record->AddEntry(new FreeEntry(pointer));
442 }
443
Christopher Ferris8b70a022018-03-07 13:38:48 -0800444 if (!VerifyPointer(pointer, "free")) {
445 return;
446 }
447
448 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700449}
450
Christopher Ferris63860cb2015-11-16 17:30:32 -0800451void* debug_memalign(size_t alignment, size_t bytes) {
452 if (DebugCallsDisabled()) {
453 return g_dispatch->memalign(alignment, bytes);
454 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700455 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800456
Colin Cross9567c7b2016-03-09 17:56:14 -0800457 if (bytes == 0) {
458 bytes = 1;
459 }
460
Christopher Ferris8b70a022018-03-07 13:38:48 -0800461 if (bytes > PointerInfoType::MaxSize()) {
462 errno = ENOMEM;
463 return nullptr;
464 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800465
Christopher Ferris8b70a022018-03-07 13:38:48 -0800466 void* pointer;
467 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800468 // Make the alignment a power of two.
469 if (!powerof2(alignment)) {
470 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
471 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800472 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800473 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800474 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
475 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800476 }
477
478 // We don't have any idea what the natural alignment of
479 // the underlying native allocator is, so we always need to
480 // over allocate.
481 size_t real_size = alignment + bytes + g_debug->extra_bytes();
482 if (real_size < bytes) {
483 // Overflow.
484 errno = ENOMEM;
485 return nullptr;
486 }
487
488 pointer = g_dispatch->malloc(real_size);
489 if (pointer == nullptr) {
490 return nullptr;
491 }
492
493 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
494 // Now align the pointer.
495 value += (-value % alignment);
496
497 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
498 pointer = InitHeader(header, pointer, bytes);
499 } else {
500 size_t real_size = bytes + g_debug->extra_bytes();
501 if (real_size < bytes) {
502 // Overflow.
503 errno = ENOMEM;
504 return nullptr;
505 }
506 pointer = g_dispatch->memalign(alignment, real_size);
507 }
508
Christopher Ferris8b70a022018-03-07 13:38:48 -0800509 if (pointer != nullptr) {
510 if (g_debug->TrackPointers()) {
511 PointerData::Add(pointer, bytes);
512 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700513
Christopher Ferris8b70a022018-03-07 13:38:48 -0800514 if (g_debug->config().options() & FILL_ON_ALLOC) {
515 size_t bytes = InternalMallocUsableSize(pointer);
516 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
517 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
518 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
519 }
520
521 if (g_debug->config().options() & RECORD_ALLOCS) {
522 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
523 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700524 }
525
Christopher Ferris63860cb2015-11-16 17:30:32 -0800526 return pointer;
527}
528
529void* debug_realloc(void* pointer, size_t bytes) {
530 if (DebugCallsDisabled()) {
531 return g_dispatch->realloc(pointer, bytes);
532 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700533 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800534
535 if (pointer == nullptr) {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800536 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700537 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700538 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
539 }
540 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800541 }
542
Christopher Ferris8b70a022018-03-07 13:38:48 -0800543 if (!VerifyPointer(pointer, "realloc")) {
544 return nullptr;
545 }
546
Christopher Ferris63860cb2015-11-16 17:30:32 -0800547 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700548 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700549 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
550 }
551
Christopher Ferris8b70a022018-03-07 13:38:48 -0800552 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800553 return nullptr;
554 }
555
556 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700557 if (g_debug->config().options() & EXPAND_ALLOC) {
558 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800559 if (real_size < bytes) {
560 // Overflow.
561 errno = ENOMEM;
562 return nullptr;
563 }
564 }
565
Christopher Ferris8b70a022018-03-07 13:38:48 -0800566 if (bytes > PointerInfoType::MaxSize()) {
567 errno = ENOMEM;
568 return nullptr;
569 }
570
Christopher Ferris63860cb2015-11-16 17:30:32 -0800571 void* new_pointer;
572 size_t prev_size;
Christopher Ferris8b70a022018-03-07 13:38:48 -0800573 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800574 // Same size, do nothing.
Christopher Ferris8b70a022018-03-07 13:38:48 -0800575 Header* header = g_debug->GetHeader(pointer);
576 if (real_size == header->size) {
577 if (g_debug->TrackPointers()) {
578 // Remove and re-add so that the backtrace is updated.
579 PointerData::Remove(pointer);
580 PointerData::Add(pointer, real_size);
581 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800582 return pointer;
583 }
584
585 // Allocation is shrinking.
586 if (real_size < header->usable_size) {
587 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700588 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800589 // Don't bother allocating a smaller pointer in this case, simply
590 // change the header usable_size and reset the rear guard.
Christopher Ferris8b70a022018-03-07 13:38:48 -0800591 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700592 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
593 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800594 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800595 if (g_debug->TrackPointers()) {
596 // Remove and re-add so that the backtrace is updated.
597 PointerData::Remove(pointer);
598 PointerData::Add(pointer, real_size);
599 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800600 return pointer;
601 }
602
603 // Allocate the new size.
Christopher Ferris8b70a022018-03-07 13:38:48 -0800604 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800605 if (new_pointer == nullptr) {
606 errno = ENOMEM;
607 return nullptr;
608 }
609
610 prev_size = header->usable_size;
611 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris8b70a022018-03-07 13:38:48 -0800612 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800613 } else {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800614 if (g_debug->TrackPointers()) {
615 PointerData::Remove(pointer);
616 }
617
Christopher Ferris63860cb2015-11-16 17:30:32 -0800618 prev_size = g_dispatch->malloc_usable_size(pointer);
619 new_pointer = g_dispatch->realloc(pointer, real_size);
620 if (new_pointer == nullptr) {
621 return nullptr;
622 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800623
624 if (g_debug->TrackPointers()) {
625 PointerData::Add(new_pointer, real_size);
626 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800627 }
628
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700629 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800630 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700631 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
632 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800633 }
634 if (bytes > prev_size) {
635 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700636 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800637 }
638 }
639
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700640 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700641 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
642 }
643
Christopher Ferris63860cb2015-11-16 17:30:32 -0800644 return new_pointer;
645}
646
647void* debug_calloc(size_t nmemb, size_t bytes) {
648 if (DebugCallsDisabled()) {
649 return g_dispatch->calloc(nmemb, bytes);
650 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700651 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800652
Colin Cross7877df62016-03-10 13:01:27 -0800653 size_t size;
654 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
655 // Overflow
656 errno = ENOMEM;
657 return nullptr;
658 }
659
Colin Cross9567c7b2016-03-09 17:56:14 -0800660 if (size == 0) {
661 size = 1;
662 }
663
Colin Cross7877df62016-03-10 13:01:27 -0800664 size_t real_size;
665 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800666 // Overflow.
667 errno = ENOMEM;
668 return nullptr;
669 }
670
Christopher Ferris8b70a022018-03-07 13:38:48 -0800671 if (real_size > PointerInfoType::MaxSize()) {
672 errno = ENOMEM;
673 return nullptr;
674 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800675
Christopher Ferris8b70a022018-03-07 13:38:48 -0800676 void* pointer;
677 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800678 // Need to guarantee the alignment of the header.
Christopher Ferris8b70a022018-03-07 13:38:48 -0800679 Header* header =
680 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800681 if (header == nullptr) {
682 return nullptr;
683 }
684 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700685 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800686 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700687 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800688 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800689
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700690 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700691 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
692 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800693
694 if (pointer != nullptr && g_debug->TrackPointers()) {
695 PointerData::Add(pointer, size);
696 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700697 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800698}
699
700struct mallinfo debug_mallinfo() {
701 return g_dispatch->mallinfo();
702}
703
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700704int debug_mallopt(int param, int value) {
705 return g_dispatch->mallopt(param, value);
706}
707
Christopher Ferrisd69ee592018-02-05 18:14:55 -0800708void* debug_aligned_alloc(size_t alignment, size_t size) {
709 if (DebugCallsDisabled()) {
710 return g_dispatch->aligned_alloc(alignment, size);
711 }
712 if (!powerof2(alignment)) {
713 errno = EINVAL;
714 return nullptr;
715 }
716 return debug_memalign(alignment, size);
717}
718
Christopher Ferris63860cb2015-11-16 17:30:32 -0800719int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
720 if (DebugCallsDisabled()) {
721 return g_dispatch->posix_memalign(memptr, alignment, size);
722 }
723
724 if (!powerof2(alignment)) {
725 return EINVAL;
726 }
727 int saved_errno = errno;
728 *memptr = debug_memalign(alignment, size);
729 errno = saved_errno;
730 return (*memptr != nullptr) ? 0 : ENOMEM;
731}
732
Christopher Ferris8b70a022018-03-07 13:38:48 -0800733int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
734 void* arg) {
735 if (g_debug->TrackPointers()) {
736 // Since malloc is disabled, don't bother acquiring any locks.
737 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
738 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
739 }
740 return 0;
741 }
Colin Cross869691c2016-01-29 12:48:18 -0800742
Christopher Ferris8b70a022018-03-07 13:38:48 -0800743 // An option that adds a header will add pointer tracking, so no need to
744 // check if headers are enabled.
745 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800746}
747
748void debug_malloc_disable() {
749 g_dispatch->malloc_disable();
Christopher Ferris8b70a022018-03-07 13:38:48 -0800750 if (g_debug->pointer) {
751 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800752 }
753}
754
755void debug_malloc_enable() {
Christopher Ferris8b70a022018-03-07 13:38:48 -0800756 if (g_debug->pointer) {
757 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800758 }
759 g_dispatch->malloc_enable();
760}
761
Christopher Ferris8b70a022018-03-07 13:38:48 -0800762ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800763 if (DebugCallsDisabled() || pointer == nullptr) {
764 return 0;
765 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700766 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800767
Christopher Ferris8b70a022018-03-07 13:38:48 -0800768 if (!(g_debug->config().options() & BACKTRACE)) {
769 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800770 }
Christopher Ferris8b70a022018-03-07 13:38:48 -0800771 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800772}
773
Christopher Ferris63860cb2015-11-16 17:30:32 -0800774#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
775void* debug_pvalloc(size_t bytes) {
776 if (DebugCallsDisabled()) {
777 return g_dispatch->pvalloc(bytes);
778 }
779
780 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700781 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800782 if (size < bytes) {
783 // Overflow
784 errno = ENOMEM;
785 return nullptr;
786 }
787 return debug_memalign(pagesize, size);
788}
789
790void* debug_valloc(size_t size) {
791 if (DebugCallsDisabled()) {
792 return g_dispatch->valloc(size);
793 }
794 return debug_memalign(getpagesize(), size);
795}
796#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700797
798static std::mutex g_dump_lock;
799
800bool debug_dump_heap(const char* file_name) {
801 ScopedDisableDebugCalls disable;
802
803 std::lock_guard<std::mutex> guard(g_dump_lock);
804
805 FILE* fp = fopen(file_name, "w+e");
806 if (fp == nullptr) {
807 error_log("Unable to create file: %s", file_name);
808 return false;
809 }
810 error_log("Dumping to file: %s\n", file_name);
811
812 if (!(g_debug->config().options() & BACKTRACE)) {
813 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
814 fprintf(fp, "# adb shell stop\n");
815 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
816 fprintf(fp, "# adb shell start\n");
817 fclose(fp);
818 return false;
819 }
820
Christopher Ferris03cb53a2018-05-29 17:39:12 -0700821 fprintf(fp, "Android Native Heap Dump v1.1\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700822
Christopher Ferris8b70a022018-03-07 13:38:48 -0800823 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700824
825 fprintf(fp, "MAPS\n");
826 std::string content;
827 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
828 fprintf(fp, "Could not open /proc/self/maps\n");
829 } else {
830 fprintf(fp, "%s", content.c_str());
831 }
832 fprintf(fp, "END\n");
833 fclose(fp);
834 return true;
835}