Merge pull request #5456 from TeroJaasko/fix_cpp_alloc_wrappers

platform: make C++ allocation wrappers log the correct caller address
pull/5862/head
Martin Kojtal 2018-01-15 15:29:54 +00:00 committed by GitHub
commit 280d491ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 4 deletions

View File

@ -81,12 +81,18 @@ extern "C" {
void * __real__realloc_r(struct _reent * r, void * ptr, size_t size); void * __real__realloc_r(struct _reent * r, void * ptr, size_t size);
void __real__free_r(struct _reent * r, void * ptr); void __real__free_r(struct _reent * r, void * ptr);
void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size); void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size);
void* malloc_wrapper(struct _reent * r, size_t size, void * caller);
void free_wrapper(struct _reent * r, void * ptr, void* caller);
} }
// TODO: memory tracing doesn't work with uVisor enabled. // TODO: memory tracing doesn't work with uVisor enabled.
#if !defined(FEATURE_UVISOR) #if !defined(FEATURE_UVISOR)
extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) { extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
return malloc_wrapper(r, size, MBED_CALLER_ADDR());
}
extern "C" void * malloc_wrapper(struct _reent * r, size_t size, void * caller) {
void *ptr = NULL; void *ptr = NULL;
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock(); mbed_mem_trace_lock();
@ -111,7 +117,7 @@ extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
ptr = __real__malloc_r(r, size); ptr = __real__malloc_r(r, size);
#endif // #ifdef MBED_HEAP_STATS_ENABLED #endif // #ifdef MBED_HEAP_STATS_ENABLED
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR()); mbed_mem_trace_malloc(ptr, size, caller);
mbed_mem_trace_unlock(); mbed_mem_trace_unlock();
#endif // #ifdef MBED_MEM_TRACING_ENABLED #endif // #ifdef MBED_MEM_TRACING_ENABLED
return ptr; return ptr;
@ -160,6 +166,10 @@ extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size)
} }
extern "C" void __wrap__free_r(struct _reent * r, void * ptr) { extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
free_wrapper(r, ptr, MBED_CALLER_ADDR());
}
extern "C" void free_wrapper(struct _reent * r, void * ptr, void * caller) {
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock(); mbed_mem_trace_lock();
#endif #endif
@ -177,7 +187,7 @@ extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
__real__free_r(r, ptr); __real__free_r(r, ptr);
#endif // #ifdef MBED_HEAP_STATS_ENABLED #endif // #ifdef MBED_HEAP_STATS_ENABLED
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR()); mbed_mem_trace_free(ptr, caller);
mbed_mem_trace_unlock(); mbed_mem_trace_unlock();
#endif // #ifdef MBED_MEM_TRACING_ENABLED #endif // #ifdef MBED_MEM_TRACING_ENABLED
} }
@ -245,9 +255,16 @@ extern "C" {
void *SUPER_REALLOC(void *ptr, size_t size); void *SUPER_REALLOC(void *ptr, size_t size);
void *SUPER_CALLOC(size_t nmemb, size_t size); void *SUPER_CALLOC(size_t nmemb, size_t size);
void SUPER_FREE(void *ptr); void SUPER_FREE(void *ptr);
void *malloc_wrapper(size_t size, void* caller);
void free_wrapper(void *ptr, void* caller);
} }
extern "C" void* SUB_MALLOC(size_t size) { extern "C" void* SUB_MALLOC(size_t size) {
return malloc_wrapper(size, MBED_CALLER_ADDR());
}
extern "C" void* malloc_wrapper(size_t size, void* caller) {
void *ptr = NULL; void *ptr = NULL;
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock(); mbed_mem_trace_lock();
@ -272,12 +289,13 @@ extern "C" void* SUB_MALLOC(size_t size) {
ptr = SUPER_MALLOC(size); ptr = SUPER_MALLOC(size);
#endif // #ifdef MBED_HEAP_STATS_ENABLED #endif // #ifdef MBED_HEAP_STATS_ENABLED
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR()); mbed_mem_trace_malloc(ptr, size, caller);
mbed_mem_trace_unlock(); mbed_mem_trace_unlock();
#endif // #ifdef MBED_MEM_TRACING_ENABLED #endif // #ifdef MBED_MEM_TRACING_ENABLED
return ptr; return ptr;
} }
extern "C" void* SUB_REALLOC(void *ptr, size_t size) { extern "C" void* SUB_REALLOC(void *ptr, size_t size) {
void *new_ptr = NULL; void *new_ptr = NULL;
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
@ -337,6 +355,10 @@ extern "C" void *SUB_CALLOC(size_t nmemb, size_t size) {
} }
extern "C" void SUB_FREE(void *ptr) { extern "C" void SUB_FREE(void *ptr) {
free_wrapper(ptr, MBED_CALLER_ADDR());
}
extern "C" void free_wrapper(void *ptr, void* caller) {
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock(); mbed_mem_trace_lock();
#endif #endif
@ -354,7 +376,7 @@ extern "C" void SUB_FREE(void *ptr) {
SUPER_FREE(ptr); SUPER_FREE(ptr);
#endif // #ifdef MBED_HEAP_STATS_ENABLED #endif // #ifdef MBED_HEAP_STATS_ENABLED
#ifdef MBED_MEM_TRACING_ENABLED #ifdef MBED_MEM_TRACING_ENABLED
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR()); mbed_mem_trace_free(ptr, caller);
mbed_mem_trace_unlock(); mbed_mem_trace_unlock();
#endif // #ifdef MBED_MEM_TRACING_ENABLED #endif // #ifdef MBED_MEM_TRACING_ENABLED
} }

View File

@ -1039,6 +1039,99 @@ extern "C" void __cxa_guard_abort(int *guard_object_p)
#endif #endif
#if defined(MBED_MEM_TRACING_ENABLED) && (defined(__CC_ARM) || defined(__ICCARM__))
// If the memory tracing is enabled, the wrappers in mbed_alloc_wrappers.cpp
// provide the implementation for these. Note: this needs to use the wrappers
// instead of malloc()/free() as the caller address would point to wrappers,
// not the caller of "new" or "delete".
extern "C" void* malloc_wrapper(size_t size, const void* caller);
extern "C" void free_wrapper(void *ptr, const void* caller);
void *operator new(std::size_t count)
{
void *buffer = malloc_wrapper(count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new out of memory\r\n");
}
return buffer;
}
void *operator new[](std::size_t count)
{
void *buffer = malloc_wrapper(count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new[] out of memory\r\n");
}
return buffer;
}
void *operator new(std::size_t count, const std::nothrow_t& tag)
{
return malloc_wrapper(count, MBED_CALLER_ADDR());
}
void *operator new[](std::size_t count, const std::nothrow_t& tag)
{
return malloc_wrapper(count, MBED_CALLER_ADDR());
}
void operator delete(void *ptr)
{
free_wrapper(ptr, MBED_CALLER_ADDR());
}
void operator delete[](void *ptr)
{
free_wrapper(ptr, MBED_CALLER_ADDR());
}
#elif defined(MBED_MEM_TRACING_ENABLED) && defined(__GNUC__)
#include <reent.h>
extern "C" void* malloc_wrapper(struct _reent * r, size_t size, void * caller);
extern "C" void free_wrapper(struct _reent * r, void * ptr, void * caller);
void *operator new(std::size_t count)
{
void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new out of memory\r\n");
}
return buffer;
}
void *operator new[](std::size_t count)
{
void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new[] out of memory\r\n");
}
return buffer;
}
void *operator new(std::size_t count, const std::nothrow_t& tag)
{
return malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
}
void *operator new[](std::size_t count, const std::nothrow_t& tag)
{
return malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
}
void operator delete(void *ptr)
{
free_wrapper(_REENT, ptr, MBED_CALLER_ADDR());
}
void operator delete[](void *ptr)
{
free_wrapper(_REENT, ptr, MBED_CALLER_ADDR());
}
#else
void *operator new(std::size_t count) void *operator new(std::size_t count)
{ {
void *buffer = malloc(count); void *buffer = malloc(count);
@ -1076,6 +1169,8 @@ void operator delete[](void *ptr)
free(ptr); free(ptr);
} }
#endif
/* @brief standard c library clock() function. /* @brief standard c library clock() function.
* *
* This function returns the number of clock ticks elapsed since the start of the program. * This function returns the number of clock ticks elapsed since the start of the program.