summaryrefslogtreecommitdiff
path: root/libc/malloc_debug/tests
AgeCommit message (Collapse)Author
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-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-04-22Include log/log_read.h for reading logsTom Cherry
log/log.h is concerned with writing logs. Bug: 78370064 Test: build Change-Id: I03d35f47acaa6eb0c8865836767d855be0203e92
2020-03-23Remove ANDROID_LOG_RDONLYTom Cherry
This macro hasn't been meaningful in years. Test: logging unit tests Change-Id: I849a466052524c24f1dba585a6423e80198c6b9c
2020-01-22Update for change MapInfo constructor.Christopher Ferris
Bug: 148075852 Test: Ran unit tests. Change-Id: I8326d8db9887e2bba26d6d94786a72c49edc5d21
2020-01-02Move bionic_macros.h from private to platform.Josh Gao
Test: treehugger Change-Id: Ie473914f4c8924c7240b3ac22093a9daf42fc948
2019-11-15Add automatic running of tests on bionic changes.Christopher Ferris
malloc debug and malloc hooks have been broken for a long time and no one noticed. So add them to be run by default on bionic changes since that provides the most coverage. Change the malloc debug and malloc hooks tests to support isolated runs. Changed the name of the malloc hooks unit tests to system tests because they weren't really unit tests. Changed the verify leak malloc debug tests to print out extra information so it is possible to figure out what sized allocation failed. Test: Ran tests. Change-Id: Idea4c864f1d62598148ee78d7c9397e45234b1ca
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-09-16Make bionic_malloc.h a platform header.Christopher Ferris
Instead of having platform directories directly include the private header, create a platform header directory and export it. Bug: 130763340 Test: Builds. Change-Id: Ie0f092b3fe077a3de8b90266c0b28bfbc20d0dfa Merged-In: Ie0f092b3fe077a3de8b90266c0b28bfbc20d0dfa (cherry picked from commit 8f582ef2f8a77d953d0e9f33387f592d20f852e2)
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-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-15Add a platform API for setting an allocation limit.Christopher Ferris
Introduce an M_SET_ALLOCATION_LIMIT enumerator for android_mallopt(), which can be used to set an upper bound on the total size of all allocations made using the memory allocation APIs. This is useful for programs such as audioextractor and mediaserver which need to set such a limit as a security mitigation. Currently these programs are using setrlimit(RLIMIT_AS) which isn't exactly what these programs want to control. RLIMIT_AS is also problematic under sanitizers which allocate large amounts of address space as shadow memory, and is especially problematic under shadow call stack, which requires 16MB of address space per thread. Add new unit tests for bionic. Add new unit tests for malloc debug that verify that when the limit is enabled, malloc debug still functions for nearly every allocation function. Bug: 118642754 Test: Ran bionic-unit-tests/bionic-unit-tests-static. Test: Ran malloc debug tests and perfetto integration tests. Change-Id: I735403c4d2c87f00fb2cdef81d00af0af446b2bb
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-02-14malloc debug: fix LogFreeError error logIris Chang
When free_track option is enabled and malloc debug detects error in VerifyFreedPointer flow, if freed pointer's usable_size is more than g_debug->config().fill_on_free_bytes(), the error log is not correct. The max. bytes printed to error message should be the max bytes to cmp, not usable size. Bug: 124420174 Test: build pass and test pass Change-Id: I41f35ab3330e49e0a6ad276d405bf4f6c3f0ea92
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-10-04Add new parameter for creation of MapInfo object.Christopher Ferris
Bug: 109657296 Test: Unit tests pass. Change-Id: Ie33b50234fa9ba2c5107c3eb0da36a466bba1589
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-25Point to online documentation.Christopher Ferris
Test: Unit tests pass. Change-Id: Ibbdc260bfdf6a6daf091c4a49cdf03e51f6ca6cf
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-05-01Fix nullptr dereference during sort.Christopher Ferris
Add new unit test that will crash without this fix. Bug: 78900050 Test: Ran unit tests. Change-Id: I73e1b89e965a7b399822c3a6f25cbc70d2d355e2
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-11-28Always wrap waitpid in TEMP_FAILURE_RETRY.Elliott Hughes
Strictly not needed in the WNOHANG case, but it's probably best to have every waitpid wrapped for future copy & pasters. Bug: https://issuetracker.google.com/69525592 Test: ran tests Change-Id: I013b0a52d2753e3d32638e9b84c79af7327fb405
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-05-03Move libc_log code into libasync_safe.Christopher Ferris
This library is used by a number of different libraries in the system. Make it easy for platform libraries to use this library and create an actual exported include file. Change the names of the functions to reflect the new name of the library. Run clang_format on the async_safe_log.cpp file since the formatting is all over the place. Bug: 31919199 Test: Compiled for angler/bullhead, and booted. Test: Ran bionic unit tests. Test: Ran the malloc debug tests. Change-Id: I8071bf690c17b0ea3bc8dc5749cdd5b6ad58478a
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
2017-03-09Fix the way malloc debug returns info.Christopher Ferris
When I rewrote malloc debug, I accidentally thought that each value returned in the info buffer contained the number of backtrace frames in the backtrace. This was incorrect, it should have been the total number of allocations with the same backtrace. This is a temporary fix that sets that value to 1. The better fix is to de-dupe backtraces and then return all allocations of the same size with the same backtrace. I updated the documents to describe this. Bug: 31854476 Test: Unit tests pass. Change-Id: Idf9efaa3d363923b5d7543d90dc7c65a0ed553d9
2017-01-23Removed include log\logger.h from log_fake.cppVijay Venkatraman
Test: Compile malloc_debug_unit_tests Change-Id: I635687016570ba52c571435b87d8e89f7f04d609
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-09-07Fix unittests broken by 545808aTamas Berghammer
Change-Id: I590af5958e4dcb772b710be965ed6c99cd1d9234
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-20Change pointer to allocation in unit tests.Christopher Ferris
I changed the output from "pointer" to "allocation" for allocation mismatches. I forgot to update the tests. Bug: 28218530 Change-Id: I3278cb1dd17fb50ff7448c00e069c1af68ce5a57
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-14Fix problem where unknown option is not an error.Christopher Ferris
If the first option is found, then any unknown options are simply ignored, but do not produce an error. Fixed this so that each option found is actually verified to exist. Bug: 27620263 Change-Id: If58732df3285eeae72188162c505e0e202fada8c
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-02-26Move hard-coded values to constants.Christopher Ferris
Change all of the hard-coded config values to be defined constants. Also make sure to use these constants in the usage output. Bug: 26739265 Change-Id: I597a6d71279a2df9cc7e98c42ac70224261bac99
2016-02-22Fix wrong guard values for 64 bit.Christopher Ferris
I added the code to force alignments of 8 for 32 bit and 16 for 64 bit, but I missed a couple of tests that failed due to this change. Fix the failing tests. Bug: 26739265 Change-Id: Ice9932d1419e59c07483c4c9fcdb84970844f0a6
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-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-02Remove the unused TrackHeader structure.Christopher Ferris
Change-Id: I07ae5de717e1fcfbe5dfe552a531db9f630da622
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-27Add min/max values for config values.Christopher Ferris
This is to only allow realistic values for some of the options. Bug: 26739265 Change-Id: I172d5401c27e5d638d80f1c93c678c26c4dae0fc
2016-01-25Fix gcc test issues.Christopher Ferris
Change-Id: I2b26b779f5198c7c9362a452b971260a8852aa7b
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