diff options
| author | Christopher Ferris <cferris@google.com> | 2013-05-21 17:48:01 -0700 |
|---|---|---|
| committer | Christopher Ferris <cferris@google.com> | 2013-06-07 14:55:32 -0700 |
| commit | 885f3b9cad01b8158aadc55c159c17dbf34f622c (patch) | |
| tree | 55991f909d2a12f185c0773de57cdf01b7074ecd /libc/bionic/malloc_debug_qemu.cpp | |
| parent | b7b4f5b838cd41407c734af74e8211f5c7da0036 (diff) | |
| download | bionic-885f3b9cad01b8158aadc55c159c17dbf34f622c.zip bionic-885f3b9cad01b8158aadc55c159c17dbf34f622c.tar.gz bionic-885f3b9cad01b8158aadc55c159c17dbf34f622c.tar.bz2 | |
Implement malloc_usable_size for debug impls.
- Implemented chk_memalign.
- Fixed a few bugs in leak_memalign.
- Implemented {leak,fill,check,qemu}_malloc_usable_size.
- Make malloc_usable_size update at run time.
- Add malloc_test.cpp as a small set of tests for the
malloc debug routines.
- Fix the qemu routines since it's been broken since it moved to C++.
- Add support for the %u format to the out_vformat in libc_logging.cpp.
This is used by the emulator code.
Tested using the bionic-unit-tests with setprop libc.debug.malloc
set to 1, 5, and 10.
I tested as much as possible on the emulator, but tracing doesn't appear
to be working properly.
Bug: 6143477
Merge change from internal master.
(cherry-picked from commit 3d594c258045783fc9e1956ce7a4d91e302f011e)
Change-Id: I4ae00fffba82315a8c283f35893fd554460722fb
Diffstat (limited to 'libc/bionic/malloc_debug_qemu.cpp')
| -rw-r--r-- | libc/bionic/malloc_debug_qemu.cpp | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/libc/bionic/malloc_debug_qemu.cpp b/libc/bionic/malloc_debug_qemu.cpp index 34ddb87..4c666a9 100644 --- a/libc/bionic/malloc_debug_qemu.cpp +++ b/libc/bionic/malloc_debug_qemu.cpp @@ -137,7 +137,7 @@ struct MallocDescQuery { * will respond with information about allocated block that contains this * pointer. */ - void* ptr; + const void* ptr; /* Id of the process that initialized libc instance, in which this query * is called. This field is used by the emulator to report errors in @@ -469,7 +469,7 @@ static inline int notify_qemu_free(void* ptr_to_free) { * Return: * Zero on success, or -1 on failure. */ -static inline int query_qemu_malloc_info(void* ptr, MallocDesc* desc, uint32_t routine) { +static inline int query_qemu_malloc_info(const void* ptr, MallocDesc* desc, uint32_t routine) { volatile MallocDescQuery query; query.ptr = ptr; @@ -574,11 +574,12 @@ static void test_access_violation(const MallocDesc* desc) { // API routines // ============================================================================= -void* qemu_instrumented_malloc(size_t bytes); -void qemu_instrumented_free(void* mem); -void* qemu_instrumented_calloc(size_t n_elements, size_t elem_size); -void* qemu_instrumented_realloc(void* mem, size_t bytes); -void* qemu_instrumented_memalign(size_t alignment, size_t bytes); +extern "C" void* qemu_instrumented_malloc(size_t bytes); +extern "C" void qemu_instrumented_free(void* mem); +extern "C" void* qemu_instrumented_calloc(size_t n_elements, size_t elem_size); +extern "C" void* qemu_instrumented_realloc(void* mem, size_t bytes); +extern "C" void* qemu_instrumented_memalign(size_t alignment, size_t bytes); +extern "C" size_t qemu_instrumented_malloc_usable_size(const void* mem); /* Initializes malloc debugging instrumentation for the emulator. * This routine is called from malloc_init_impl routine implemented in @@ -589,7 +590,7 @@ void* qemu_instrumented_memalign(size_t alignment, size_t bytes); * Return: * 0 on success, or -1 on failure. */ -int malloc_debug_initialize() { +extern "C" int malloc_debug_initialize() { /* We will be using emulator's magic page to report memory allocation * activities. In essence, what magic page does, it translates writes to * the memory mapped spaces into writes to an I/O port that emulator @@ -627,7 +628,7 @@ int malloc_debug_initialize() { * Return: * 0 on success, or -1 on failure. */ -int memcheck_initialize(int alignment, const char* memcheck_param) { +extern "C" int memcheck_initialize(int alignment, const char* memcheck_param) { malloc_alignment = alignment; /* Parse -memcheck parameter for the guest tracing flags. */ @@ -673,7 +674,7 @@ int memcheck_initialize(int alignment, const char* memcheck_param) { * bytes (plus prefix, and suffix guards), and report allocation to the * emulator. */ -void* qemu_instrumented_malloc(size_t bytes) { +extern "C" void* qemu_instrumented_malloc(size_t bytes) { MallocDesc desc; /* Initialize block descriptor and allocate memory. Note that dlmalloc @@ -708,7 +709,7 @@ void* qemu_instrumented_malloc(size_t bytes) { * Primary responsibility of this routine is to free requested memory, and * report free block to the emulator. */ -void qemu_instrumented_free(void* mem) { +extern "C" void qemu_instrumented_free(void* mem) { MallocDesc desc; if (mem == NULL) { @@ -751,7 +752,7 @@ void qemu_instrumented_free(void* mem) { /* This routine serves as entry point for 'calloc'. * This routine behaves similarly to qemu_instrumented_malloc. */ -void* qemu_instrumented_calloc(size_t n_elements, size_t elem_size) { +extern "C" void* qemu_instrumented_calloc(size_t n_elements, size_t elem_size) { if (n_elements == 0 || elem_size == 0) { // Just let go zero bytes allocation. qemu_info_log("::: <libc_pid=%03u, pid=%03u>: Zero calloc redir to malloc", @@ -823,7 +824,7 @@ void* qemu_instrumented_calloc(size_t n_elements, size_t elem_size) { * allocation, but overall it doesn't seem to matter, as caller of realloc * should not expect that pointer returned after shrinking will remain the same. */ -void* qemu_instrumented_realloc(void* mem, size_t bytes) { +extern "C" void* qemu_instrumented_realloc(void* mem, size_t bytes) { MallocDesc new_desc; MallocDesc cur_desc; size_t to_copy; @@ -927,7 +928,7 @@ void* qemu_instrumented_realloc(void* mem, size_t bytes) { /* This routine serves as entry point for 'memalign'. * This routine behaves similarly to qemu_instrumented_malloc. */ -void* qemu_instrumented_memalign(size_t alignment, size_t bytes) { +extern "C" void* qemu_instrumented_memalign(size_t alignment, size_t bytes) { MallocDesc desc; if (bytes == 0) { @@ -967,3 +968,27 @@ void* qemu_instrumented_memalign(size_t alignment, size_t bytes) { malloc_pid, getpid(), alignment, bytes); return mallocdesc_user_ptr(&desc); } + +extern "C" size_t qemu_instrumented_malloc_usable_size(const void* mem) { + MallocDesc cur_desc; + + // Query emulator for the reallocating block information. + if (query_qemu_malloc_info(mem, &cur_desc, 2)) { + // Note that this violation should be already caught in the emulator. + error_log("<libc_pid=%03u, pid=%03u>: malloc_usable_size(%p) query_info failed.", + malloc_pid, getpid(), mem); + return 0; + } + + /* Make sure that reallocating pointer value is what we would expect + * for this memory block. Note that this violation should be already caught + * in the emulator.*/ + if (mem != mallocdesc_user_ptr(&cur_desc)) { + log_mdesc(error, &cur_desc, "<libc_pid=%03u, pid=%03u>: malloc_usable_size(%p) is invalid for ", + malloc_pid, getpid(), mem); + return 0; + } + + /* during instrumentation, we can't really report anything more than requested_bytes */ + return cur_desc.requested_bytes; +} |
