From 36f3ca7f76275329ab19d8d04cfb8256a68269f3 Mon Sep 17 00:00:00 2001 From: Vitaliy Cherepanov Date: Thu, 8 Dec 2016 16:22:26 +0300 Subject: [PATCH] lsan: save mapped address instead of original addr Addresses returned by allocation funcitons should be changed before saving to library hash because of lsan search addresses in process memory Change-Id: Icd197343826c1cfec19d406858e676622ebc8794 Signed-off-by: Vitaliy Cherepanov --- probe_memory/da_memory.h | 7 +++++++ probe_memory/libdamemalloc.c | 21 +++++++++++---------- probe_memory/libdanew.cpp | 17 +++++++++-------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/probe_memory/da_memory.h b/probe_memory/da_memory.h index a8ae788..7963e83 100644 --- a/probe_memory/da_memory.h +++ b/probe_memory/da_memory.h @@ -51,4 +51,11 @@ * ********************************************************************/ + +static inline void *convert_addr_to_hash(void *ptr) +{ + return (void *)((void *)0 - ptr); +} + + #endif // __DA_MEMORY_H__ diff --git a/probe_memory/libdamemalloc.c b/probe_memory/libdamemalloc.c index 2885ed2..7f8b644 100755 --- a/probe_memory/libdamemalloc.c +++ b/probe_memory/libdamemalloc.c @@ -100,7 +100,7 @@ HANDLER_WRAPPERS(memory_feature, void *, malloc, size_t, size) pret = (*mallocp)(size); if(pret != NULL) - add_memory_hash(pret, size, MEMTYPE_ALLOC, + add_memory_hash(convert_addr_to_hash(pret), size, MEMTYPE_ALLOC, PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -134,7 +134,7 @@ HANDLER_WRAPPERS_VOID(memory_feature, void, free, void*, ptr) PRE_PROBEBLOCK(); if(ptr != NULL) - del_memory_hash(ptr, MEMTYPE_FREE, NULL); + del_memory_hash(convert_addr_to_hash(ptr), MEMTYPE_FREE, NULL); (*freep)(ptr); @@ -168,13 +168,14 @@ HANDLER_WRAPPERS(memory_feature, void *, realloc, void *, memblock, size_t, size PRE_PROBEBLOCK(); if(memblock != NULL) - del_memory_hash(memblock, MEMTYPE_FREE, NULL); + del_memory_hash(convert_addr_to_hash(memblock), + MEMTYPE_FREE, NULL); pret = (*reallocp)(memblock, size); if(pret != NULL) - add_memory_hash(pret, size, MEMTYPE_ALLOC, - PROBE_GET_CALL_TYPE(probe_data)); + add_memory_hash(convert_addr_to_hash(pret), size, + MEMTYPE_ALLOC, PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -231,8 +232,8 @@ HANDLER_WRAPPERS(memory_feature, void *, calloc, size_t, nelem, size_t, elsize) pret = (*callocp)(nelem, elsize); if(pret != NULL) - add_memory_hash(pret, nelem * elsize, MEMTYPE_ALLOC, - PROBE_GET_CALL_TYPE(probe_data)); + add_memory_hash(convert_addr_to_hash(pret), nelem * elsize, + MEMTYPE_ALLOC, PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -272,8 +273,8 @@ HANDLER_WRAPPERS(memory_feature, int, posix_memalign, void**, memptr, iret = (*posix_memalignp)(memptr, alignment, size); if (iret == 0) - add_memory_hash(*memptr, size, MEMTYPE_ALLOC, - PROBE_GET_CALL_TYPE(probe_data)); + add_memory_hash(convert_addr_to_hash(*memptr), size, + MEMTYPE_ALLOC, PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -310,7 +311,7 @@ HANDLER_WRAPPERS(memory_feature, char *, strdup, const char*, s) pret = (*strdupp)(s); if (pret == 0) - add_memory_hash(pret, size, MEMTYPE_ALLOC, + add_memory_hash(convert_addr_to_hash(pret), size, MEMTYPE_ALLOC, PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); diff --git a/probe_memory/libdanew.cpp b/probe_memory/libdanew.cpp index 10add65..976418f 100644 --- a/probe_memory/libdanew.cpp +++ b/probe_memory/libdanew.cpp @@ -100,7 +100,7 @@ HANDLER_WRAPPERS_THROW(memory_feature, void *, new, (std::bad_alloc), std::size_ pret = newp(size); if(pret != NULL) - add_memory_hash(pret, size, MEMTYPE_NEW, + add_memory_hash(convert_addr_to_hash(pret), size, MEMTYPE_NEW, (unsigned short)PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -137,7 +137,7 @@ HANDLER_WRAPPERS_THROW(memory_feature, void *, new_array, (std::bad_alloc), std: pret = newp(size); if(pret != NULL) - add_memory_hash(pret, size, MEMTYPE_NEW, + add_memory_hash(convert_addr_to_hash(pret), size, MEMTYPE_NEW, PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -174,7 +174,8 @@ HANDLER_WRAPPERS_THROW(memory_feature, void, delete, (), void *, ptr) if(ptr != NULL) { /* TODO call_type rewrited as it was previously. Is it needed? */ - ret = del_memory_hash(ptr, MEMTYPE_DELETE, &ct); + ret = del_memory_hash(convert_addr_to_hash(ptr), + MEMTYPE_DELETE, &ct); if(blockresult == 0 && ret == 0 && ct == EXTERNAL_CALL) { inc_current_event_index(); @@ -216,7 +217,7 @@ HANDLER_WRAPPERS_THROW(memory_feature, void, delete_array, (), void *, ptr) if(ptr != NULL) { /* TODO call_type rewrited as it was previously. Is it needed? */ - ret = del_memory_hash(ptr, MEMTYPE_DELETE, &ct); + ret = del_memory_hash(convert_addr_to_hash(ptr), MEMTYPE_DELETE, &ct); if(blockresult == 0 && ret == 0 && PROBE_GET_CALL_TYPE(probe_data) == EXTERNAL_CALL) { @@ -261,7 +262,7 @@ HANDLER_WRAPPERS_THROW(memory_feature, void *, new_nothrow, (), std::size_t, siz pret = newp(size, nothrow); if(pret != NULL) - add_memory_hash(pret, size, MEMTYPE_NEW, + add_memory_hash(convert_addr_to_hash(pret), size, MEMTYPE_NEW, (unsigned short)PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -300,7 +301,7 @@ HANDLER_WRAPPERS_THROW(memory_feature, void *, new_array_nothrow, (), std::size_ pret = newp(size, nothrow); if(pret != NULL) - add_memory_hash(pret, size, MEMTYPE_NEW, + add_memory_hash(convert_addr_to_hash(pret), size, MEMTYPE_NEW, PROBE_GET_CALL_TYPE(probe_data)); POST_PACK_PROBEBLOCK_BEGIN(); @@ -339,7 +340,7 @@ HANDLER_WRAPPERS_THROW(memory_feature, void, delete_nothrow, (), void *, ptr, if(ptr != NULL) { /* TODO call_type rewrited as it was previously. Is it needed? */ - ret = del_memory_hash(ptr, MEMTYPE_DELETE, &ct); + ret = del_memory_hash(convert_addr_to_hash(ptr), MEMTYPE_DELETE, &ct); if(blockresult == 0 && ret == 0 && PROBE_GET_CALL_TYPE(probe_data) == EXTERNAL_CALL) { @@ -384,7 +385,7 @@ HANDLER_WRAPPERS_THROW(memory_feature, void, delete_array_nothrow, (), void *, p if(ptr != NULL) { /* TODO call_type rewrited as it was previously. Is it needed? */ - ret = del_memory_hash(ptr, MEMTYPE_DELETE, &ct); + ret = del_memory_hash(convert_addr_to_hash(ptr), MEMTYPE_DELETE, &ct); if(blockresult == 0 && ret == 0 && PROBE_GET_CALL_TYPE(probe_data) == EXTERNAL_CALL) { -- 2.7.4