summaryrefslogtreecommitdiff
path: root/libc/malloc_debug/malloc_debug.cpp
AgeCommit message (Collapse)Author
2021-07-27Merge SP1A.210723.002Scott Lobdell
Change-Id: I0fe27377cab01d329a1e8fbe80c2f9d8a0e35a30
2021-07-15Fix race when frees after main thread finishes.Christopher Ferris
When the main thread is exiting, the code deleted the g_debug global pointer and destroys the disable pthread key. Unfortunately, if malloc debug was enabled in a way that requires a header for the pointer, any frees that occur after the main thread is torn down result in calls to the underlying allocator with bad pointers. To avoid this, don't delete the g_debug pointer and don't destroy the disable pthread key. Added a new system test that allocates a lot of pointers and frees them after letting the main thread finish. Also, fix one test that can fail sporadically due to a lack of unwinding information on arm32. Bug: 189541929 Test: Passes new system tests. Change-Id: I1cfe868987a8f0dc880a5b65de6709f44a5f1988 Merged-In: I1cfe868987a8f0dc880a5b65de6709f44a5f1988 (cherry picked from commit 33d73379aad3ed7dcbb9b10f945d3bc6264b79bd)
2020-05-29Merge RP1A.200526.001Scott Lobdell
Change-Id: Ic96234634b30ccc621f69f78a036c551f08abed0
2020-05-21Fix deadlock/timeout in thread unwinding.Christopher Ferris
When malloc debug is enabled, using libbacktrace to unwind can result in a deadlock. This happens when an unwind of a thread is occuring which triggers a signal to be sent to that thread. If that thread is interrupted while a malloc debug function is executing and owns a lock, that thread is then stuck in the signal handler. Then the original unwinding thread attempts to do an allocation and gets stuck waiting for the same malloc debug lock. This is not a complete deadlock since the unwinder has timeouts, but it results in truncated unwinds that take at least five seconds to complete. Only the backtrace signals needs to be blocked because it is the only known signal that will result in a thread being paused in a signal handler. Also, added a named signal in the reserved signal list for the special bionic backtrace signal. Bug: 150833265 Test: New unit tests pass with fix, fail without fix. Change-Id: If3e41f092ebd40ce62a59ef51d636a91bc31ed80 (cherry picked from commit 9bf7817dd29d15ea49c88436db4067d87fc7e6c4)
2020-05-20Fix deadlock/timeout in thread unwinding.Christopher Ferris
When malloc debug is enabled, using libbacktrace to unwind can result in a deadlock. This happens when an unwind of a thread is occuring which triggers a signal to be sent to that thread. If that thread is interrupted while a malloc debug function is executing and owns a lock, that thread is then stuck in the signal handler. Then the original unwinding thread attempts to do an allocation and gets stuck waiting for the same malloc debug lock. This is not a complete deadlock since the unwinder has timeouts, but it results in truncated unwinds that take at least five seconds to complete. Only the backtrace signals needs to be blocked because it is the only known signal that will result in a thread being paused in a signal handler. Also, added a named signal in the reserved signal list for the special bionic backtrace signal. Bug: 150833265 Test: New unit tests pass with fix, fail without fix. Change-Id: If3e41f092ebd40ce62a59ef51d636a91bc31ed80
2020-02-04Merge RP1A.200204.001Steven Laver
Change-Id: Ief66494e3f158448edb3f131363437cd3c927883
2020-01-23Add tagged pointers to bionic.Mitch Phillips
This patch introduces tagged pointers to bionic. We add a static tag to all pointers on arm64 compatible platforms (needs requisite top-byte-ignore hardware feature and relevant kernel patches). We dynamically detect TBI-compatible devices (a device with the TBI feature and kernel support) at process start time, and insert an implementation-dependent tag into the top byte of the pointer for all heap allocations. We then check that the tag has not been truncated when deallocating the memory. If an application incorrectly writes to the top byte of the pointer, we terminate the process at time of detection. This will allow MTE-incompatible applications to be caught early. Bug: 135754954 Bug: 147147490 Test: cd bionic && atest . Change-Id: Ie424325ba1e3c4443040ac265aeaa28d9e405d28
2020-01-21Revert "Add tagged pointers to bionic."Nicolas Geoffray
This reverts commit 43d5f9d4dd83d15a859d9be1359c4a4a47381fea. Bug: 135754954 Bug: 147147490 Exempt-From-Owner-Approval: clean revert Reason for revert: Breaks ART gtest, see: https://ci.chromium.org/p/art/builders/ci/angler-armv8-non-gen-cc/561 The crash happens on mprotect of a page, the test crashes with ENOMEM. Change-Id: I52eea1abbfaf8d8e2226f92d30aa55aba3810528
2020-01-15Add tagged pointers to bionic.Mitch Phillips
This patch introduces tagged pointers to bionic. We add a static tag to all pointers on arm64 compatible platforms (needs requisite top-byte-ignore hardware feature and relevant kernel patches). We dynamically detect TBI-compatible devices (a device with the TBI feature and kernel support) at process start time, and insert an implementation-dependent tag into the top byte of the pointer for all heap allocations. We then check that the tag has not been truncated when deallocating the memory. If an application incorrectly writes to the top byte of the pointer, we terminate the process at time of detection. This will allow MTE-incompatible applications to be caught early. Bug: 135754954 Bug: 147147490 Test: cd bionic && atest . Change-Id: I6e5b809fc81f55dd517f845eaf20f3c0ebd4d86e
2019-11-14Merge RP1A.191114.001Steven Laver
Change-Id: I19fb768a647d471d430af4b5c3f519d4125fdeee
2019-11-08Rename iterate to malloc_iterate internally.Christopher Ferris
I have no idea why I used the iterate name internally which is completely unlike every other function name. Change this to match everyone else so that it's now malloc_iterate everywhere. This is probably the last chance to change this before mainline modules begin, so make everything consistent. Test: Compiles, unit tests passes. Change-Id: I56d293377fa0fe1a3dc3dd85d6432f877cc2003c
2019-11-06Fix allocations escaping malloc debug.Christopher Ferris
When using a FILE object for some malloc debug functions, calling fprintf will trigger an allocation to be put in the object. The problem is that these allocations were not allocated by the malloc debug wrapper and they get freed during the fclose as if they are malloc debug allocation. In most cases, the code will detect the bad pointer and leak the memory, but it might also cause a crash. The fix is to avoid using fprintf so that no allocations are made in the object that survive and need to be freed in the fclose call. Change the MallocXmlElem.h to use a file decsriptor not a FILE object. Add new unit and system tests to detect this case. Bug: 143742907 Test: Ran unit and system tests. Test: Ran bionic unit tests. Change-Id: I524392de822a29483aa5be8f14c680e70033eba2
2019-08-07Merge RP1A.190528.001Steven Laver
Change-Id: If6e905407e26a19e0266185af46b4ff461c4d45e
2019-06-14Avoid recording backtrace if alloc-size is outside range of interestShibin George
Steps: 1) setprop libc.debug.malloc.program <program name> 2) setprop libc.debug.malloc.options "backtrace leak_track" 3) setprop libc.debug.malloc.minalloctorecord <int> # defaults to 0 4) setprop libc.debug.malloc.maxalloctorecord <int> # defaults to SIZE_MAX With this, backtrace recording of only those allocations which fall within the desired range will be done. CRs-Fixed: 2471840 Change-Id: I586dfd590e429b53e7b1b237294f0298ff7b4017
2019-05-22Disable malloc debug when asan enabled.Christopher Ferris
Bug: 123312263 Test: Verified with asan enabled, malloc debug does not initialize. Test: Ran tests on non-asan build and verify they pass. Change-Id: I3c37c170bf6c1de42740972f2113ae991351d931
2019-05-08Avoid using malloc debug code after exit.Christopher Ferris
I wrote a new unit test that would fail on the old version of the code. On a walleye big cpu, this costs about 40ns-50ns (going from ~430ns to ~480ns). I think this is an acceptable performance degradation. Bug: 131867816 Test: New unit tests pass. Change-Id: I4c0f4373fb0694bf29c3824dbb1224a8a17e211e Merged-In: I4c0f4373fb0694bf29c3824dbb1224a8a17e211e (cherry picked from commit d269fcc935b276502b9e47a575d76693fe1b8455)
2019-05-07Avoid using malloc debug code after exit.Christopher Ferris
I wrote a new unit test that would fail on the old version of the code. On a walleye big cpu, this costs about 40ns-50ns (going from ~430ns to ~480ns). I think this is an acceptable performance degradation. Bug: 131867816 Test: New unit tests pass. Change-Id: I4c0f4373fb0694bf29c3824dbb1224a8a17e211e
2019-04-16Remove gMallocLeakZygoteChild.Christopher Ferris
Remove this global variable and change the setting of it to non-zero to a call to android_mallopt. In addition, change the initialize function to use pass a bool* instead of int*. Bug: 130028357 Test: Ran malloc_debug/malloc_hooks/perfetto tests. Change-Id: I20d382bdeaaf38aac6b9dcabea5b3dfab3c945f6 Merged-In: I20d382bdeaaf38aac6b9dcabea5b3dfab3c945f6 (cherry picked from commit 5225b342f0810c027df3d09fbbcef4d324b19b93)
2019-04-16Remove gMallocLeakZygoteChild.Christopher Ferris
Remove this global variable and change the setting of it to non-zero to a call to android_mallopt. In addition, change the initialize function to use pass a bool* instead of int*. Bug: 130028357 Test: Ran malloc_debug/malloc_hooks/perfetto tests. Change-Id: I20d382bdeaaf38aac6b9dcabea5b3dfab3c945f6 Merged-In: I20d382bdeaaf38aac6b9dcabea5b3dfab3c945f6 (cherry picked from commit 5225b342f0810c027df3d09fbbcef4d324b19b93)
2019-04-02Disable info messages by default for malloc debug.Christopher Ferris
Add a new option verbose for malloc debug that is not enabled by default. This disables all of the info log messages. It turns out these log messages can add a measurable amount of time and can change the boot up. Bug: 129239269 Test: Adjusted unit tests pass. Test: Verified no messages unless verbose option used. Change-Id: I805cb7c8ecb44de88119574e59d784877cacc383
2019-03-07Refactor the malloc_info code.Christopher Ferris
malloc_info needs to be per native allocator, but the code treated it like a global function that doesn't depend on the native memory allocator. Update malloc debug to dump the actual pointers that it has been tracking. Test: bionic-unit-tests pass. Test: malloc debug tests pass. Test: malloc hook tests pass. Change-Id: I3b0d4d748489dd84c16d16933479dc8b8d79013e Merged-In: I3b0d4d748489dd84c16d16933479dc8b8d79013e (cherry picked from commit a3656a98b10d2a4a6194a5d9705ad9c2cc5877b0)
2019-03-01Make aligned_alloc match the standard.Christopher Ferris
Jemalloc does not verify that the size parameter is a multiple of alignment. Fix this since it only went into P. Fix the unit tests, and fix malloc debug/malloc hooks to handle this new restrictive behavior. Bug: 126944692 Test: Ran bionic unit tests. Test: Ran bionic unit tests with malloc hooks enabled (no new tests fail). Test: Ran bionic unit tests with malloc debug enabled (no new tests fail). Test: Ran malloc debug unit tests. Change-Id: I4d50785928815679c781ca729f998454d76b9192
2019-01-22Bionic malloc debug: add a new option "abort_on_error"Iris Chang
This new option causes an abort after malloc debug detects an error. This allows vendors to get process coredumps to analyze memory for corruption. Bug: 123009873 Test: New test cases added for unit tests and config tests. Change-Id: I6b480af7f747d6a82f61e8bf3df204a5f7ba017f
2018-08-02Modernize codebase by replacing NULL with nullptrYi Kong
Fixes -Wzero-as-null-pointer-constant warning. Test: m Bug: 68236239 Change-Id: I5b4123bc6709641315120a191e36cc57541349b2
2018-07-11Disable malloc debug intercepts when exiting.Christopher Ferris
There is a hang when enabling leak_track since the dumping of the leak data can wind up doing an allocation. Add new system unit test to make sure this doesn't happen again. Bug: 111146059 Test: Test program that leaks does not hang forever. Test: Unit tests pass. Change-Id: Icf99be58ba5db98ee124a471b957a086045f5870
2018-06-15Change heap dump format slightly.Christopher Ferris
Bump the version from v1.1 to v1.2 and add a build fingerprint line. Update the heap dump documentation to match the new format and reflect what made it in P and what made it in Q. Update the unit tests for this change. Add -O0 to unit test code to make it easier to debug. Add an external function that can be used by the framework code so that there is only one way to dump the heap. Bug: 110095681 Test: Ran unit tests. Test: Did a dump of a real process and verified fingerprint. Test: Did a dump of a process without malloc debug enabled. Change-Id: I769a476cbeaf4c85c5d75bd6d6385f0e3add948c Merged-In: I769a476cbeaf4c85c5d75bd6d6385f0e3add948c (cherry picked from commit c84a2a2601a4112ca6e43a33defb989c1da8c2f4)
2018-05-24Add support for using the new unwinder.Christopher Ferris
This adds a new option backtrace_full, when it is set, then it will use libunwindstack. Modify the dump to file data to dump the extra information from libunwindstack. Along with the new dump file format, change the version to v1.1. Updated document for new format of file data. Add unit tests for the new functionality. Bug: 74361929 Test: Ran unit tests. Change-Id: I40fff795f5346bba7b9d7fde2e04f269ff4eb7f1
2018-04-02Refactor malloc debug.Christopher Ferris
Changes - Refactor the code so that only guards require creating a special header for every pointer allocated. - Store only a single copy of every backtrace. This saves memory so that turning on the backtrace option doesn't result in 10X memory usage. - Added new option track_allocs that only verifies pointers are valid for free/malloc_usable_size/realloc. - Remove suffix from test names. - Add the TRACK_ALLOCS options to all guard options. - Add new option verify_pointers that is a lightweight way to verify pointers that are passed to allocation routines. - Do auto-formatting of the code. - Updated documentation for all of these changes. Bug: 74361929 Test: Ran unit tests. Test: Ran libmemunreachable unit tests. Test: Ran an app with backtrace enabled. Change-Id: I3246c48ae4f9811f64622d90d0a9b4d9d818702c
2018-02-07Add aligned_alloc to libc.Christopher Ferris
Bug: 72969374 Test: Bionic unit tests pass. Test: Malloc debug unit tests pass. Change-Id: I235985bbc638855d94249c97c98f14ab2924bda0 (cherry picked from commit d69ee59594088c0d92ba9273188ef53ea5e6cd6a)
2017-10-05Add a legacy inline for mmap64.Dan Albert
While this was never an inline, this function alone has caused most of the bug reports related to _FILE_OFFSET_BITS=64. Providing an inline for it should allow a lot more code to build with _FILE_OFFSET_BITS=64 when targeting pre-L. Test: make checkbuild Test: built trivial cc_binary for LP32 against API 14 with _FILE_OFFSET_BITS=64 set Bug: lots Change-Id: I8479d34af4da358c11423bee43d45b59e9d4143e
2017-09-05Provide method to dump backtrace heap data.Christopher Ferris
For non-zygote spawned processes, we might want to dump the backtrace data. Provide a method to send a signal to a process and then dump the data to a file. Adds a method to dump the backtrace data on exit. Update documentation and explain format of heap dump data. Test: Ran unit tests, enabled new options and used them. Change-Id: Ie2fa706694160731afe02c1382b037d06df1d069
2017-05-15Add support for modifying decay timer.Christopher Ferris
Add the mallopt function, and only a single option so far. Bug: 36401135 Test: Built and booted bullhead. Test: Ran jemalloc unit tests. Test: Ran bionic unit tests. Test: Ran a test that allocated and free'd a large piece of memory, Test: and verified that after changing the parameter, the PSS Test: sticks around (decay timer set to 1), the PSS is purged (decay Test: timer set to 0). Change-Id: I6927929b0c539c1023d34772d9e26bb6a8a45877
2017-04-06Refactor Config from a struct to a class.Christopher Ferris
This should make it easier to add new options, and to add options that are complex. For example, I want to modify the behavior of record_allocs_file so that it also enables record_allocs to a default state. Test: All unit tests pass. Test: Enable the backtrace option and restart. Change-Id: Idf5cdeed06ade3bc2c8ae39d228734bf65209b4f
2016-11-17Enable malloc debug using environment variablesTamas Berghammer
Previously malloc debug can be enabled only using global settings accessible to the root user only. This CL adds a new option to enable it using environment variables making it possible to use it with pure native (shell) applications on production builds (from shell user) and prepares it for using it from logwrapper on production devices. Remove the old environment variable and property since they are not necessary. Test: Enable malloc debug using environment variable and verify Test: that it only affects the commands launched from the shell. Test: Enable malloc debug using the property variable and verify Test: that it affects all commands. Test: Run all unit tests in 32 bit and 64 bit. Change-Id: Iecb75a3471552f619f196ad550c5f41fcd9ce8e5
2016-07-08Add the record alloc option.Christopher Ferris
This option adds the ability to record all of the allocation requests and dump them to a file when a signal is sent to the process. Included in this change, redo the option processing to add a new string option. Bug: 27747898 Change-Id: Ida043362e38b5eb1d459c99db9c2581015dab366
2016-04-12Small refactor.Christopher Ferris
- Move all ScopedDisableDebugCalls into the debug_XXX calls. This avoids any issues that might arise where every part of the code needs to properly guard anything that might allocate. Instead everything is already guarded. - Add a pointer to debug_data in all of the XXData classes. This avoids calling individual functions passing in the debug_data pointer. - Flip the NO_HEADER_OPTIONS to an explicit HEADER_OPTIONS list since fewer options actually require a header. - Move the extern of g_debug to the DebugData.h header. Change-Id: Ia213a391b4a44d9ce122a709d09fe4f1b5426f36
2016-03-16Fix race in malloc debug option free_track.Christopher Ferris
The free track mechanism could fail if, at the same time a free occurs, another thread is trying to free and verify the same allocation. This doesn't work if the freed allocation is added to the list and we still do work on it. The fix is to only add to the free list when we are done with the allocation. Also fix a problem where the usable size is computed incorrectly because two of the arguments where reversed. In addition, add a check that the allocation being verified has the correct tag before trying to check the body of the allocation. Add a test to catch the original failure. Add a test for the tag being different. Bug: 27601650 Change-Id: Ie9200677d066255b8e668a48422f23f909f4ddee
2016-03-10malloc_debug: fix multiplication overflow in debug_callocColin Cross
The over flow check for nmemb * bytes in debug_calloc is incorrect, use the builtin overflow functions to check for multiplication and addition overflow. Change-Id: I3f1c13102621bc5380be1f69caa88dba2118f3cb (cherry picked from commit 239838608dbe9917acddfe5a51d92350a4c8e135)
2016-03-10malloc_debug: round 0 byte allocations up to 1 byteColin Cross
0 byte allocations can cause problems if they are immediately followed by another allocation with no header, as both allocations will have the same address. Treat 0 byte allocations as 1 byte allocations so that debug_iterate will return separate addresses for them. Bug: 27578580 Change-Id: Ia8dc3481fa7062391e9b3ae58a36e8d47e7ee557 (cherry picked from commit 15af478080cfbfa800fb8172fdf70a84075925e3)
2016-03-10malloc_debug: iterate: use usable_sizeColin Cross
malloc_iterate with malloc debug enabled was returning allocation sizes using the requested size instead of usable size. If anything wrote pointers above the requested size but below the usable size, those pointers would be invisible to libmemunreachable and referenced allocations could be reported as a leak. Bug: 27107100 Change-Id: I6b19fd631f68ce93b3aee408cc3d296ec457bd01 (cherry picked from commit b23c6606d2326f8835fc7fc35b23ff7ee5a48f48)
2016-02-18Add backtrace_string and export to libmemunreachableColin Cross
Add backtrace_string to convert a malloc_debug backtrace to a string. Also move the backtrace functions to libc_malloc_debug_backtrace so that libmemunreachable can reuse them. Change-Id: I5ad67001c0b4d184903c762863a8588181d4873b
2016-02-18Export malloc_backtraceColin Cross
Change-Id: Ic1adb4dfd86b9ca698443a36263a3df2c91edda3
2016-02-18Export malloc_iterate and friendsColin Cross
Export malloc_iterate, malloc_enable, and malloc_disable to be used by libmemunreachable. Change-Id: I08a50349af82a95d096b6b4cbac37ababe4b9b06
2016-02-17Merge "malloc_debug: reset TrackData mutex after fork"Colin Cross
2016-02-12Fix the default alignment of the allocations.Christopher Ferris
In order to enforce this constraint: The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated. Force all allocations on 32 bit systems to have 8 byte alignment, and all allocations on 64 bit systems to have 16 byte alignment. Add a test to verify that the allocator returns the correct alignments. Bug: 26739265 Change-Id: I9af53279617408676b94e4ec6481b3ed7ffafc6a
2016-02-12malloc_debug: reset TrackData mutex after forkColin Cross
Add a pthread_atfork handler to malloc_debug to lock the TrackData mutex during fork and reset it in the child. Ensures that the TrackData is consistent when forking from a multi-threaded process, and that the mutex is in a defined state in the child. Change-Id: I0dfed30045a28551217ceac227d33b2e18894932
2016-01-29Add better free tracking.Christopher Ferris
Included in this change: - Change the tag when a pointer is freed so it's easy to detect if an already freed pointer is being used. - Move the free backtrace out of the header. This backtrace is only used under only some circumstances, so no need to allocate space in all headers for it. - Add new option free_track_backtrace_num_frames to specify how many frames to record when the free occurs. This removes the dependency on the backtrace option to get backtraces. Bug: 26739265 Change-Id: I76f5209507dcf46af67ada162a7cb2bf282116f2
2016-01-25Malloc debug rewrite.Christopher Ferris
The major components of the rewrite: - Completely remove the qemu shared library code. Nobody was using it and it appears to have broken at some point. - Adds the ability to enable/disable different options independently. - Adds a new option that can enable the backtrace on alloc/free when a process gets a specific signal. - Adds a new way to enable malloc debug. If a special property is set, and the process has an environment variable set, then debug malloc will be enabled. This allows something that might be a derivative of app_process to be started with an environment variable being enabled. - get_malloc_leak_info() used to return one element for each pointer that had the exact same backtrace. The new version returns information for every one of the pointers with same backtrace. It turns out ddms already automatically coalesces these, so the old method simply hid the fact that there where multiple pointers with the same amount of backtrace. - Moved all of the malloc debug specific code into the library. Nothing related to the malloc debug data structures remains in libc. - Removed the calls to the debug malloc cleanup routine. Instead, I added an atexit call with the debug malloc cleanup routine. This gets around most problems related to the timing of doing the cleanup. The new properties and environment variables: libc.debug.malloc.options Set by option name (such as "backtrace"). Setting this to a bad value will cause a usage statement to be printed to the log. libc.debug.malloc.program Same as before. If this is set, then only the program named will be launched with malloc debug enabled. This is not a complete match, but if any part of the property is in the program name, malloc debug is enabled. libc.debug.malloc.env_enabled If set, then malloc debug is only enabled if the running process has the environment variable LIBC_DEBUG_MALLOC_ENABLE set. Bug: 19145921 Change-Id: I7b0e58cc85cc6d4118173fe1f8627a391b64c0d7