Skip to content

Commit

Permalink
libheaptrace: Trace reallocarray function
Browse files Browse the repository at this point in the history
Add reallocarray function to be traced
reallocarray is a function in the OpenBSD C library
And is not part of the standard C library

Signed-off-by: Bojun Seo <[email protected]>
  • Loading branch information
Bojun-Seo authored and honggyukim committed Jan 4, 2023
1 parent 0842ccb commit 70c11fb
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions libheaptrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern "C" __weak int __posix_memalign(void **memptr, size_t alignment, size_t
extern "C" __weak void* __aligned_alloc(size_t alignment, size_t size);
extern "C" __weak void* __pvalloc(size_t size);
extern "C" __weak void* __valloc(size_t size);
extern "C" __weak void* __reallocarray(void* ptr, size_t nmemb, size_t size);

typedef void* (*MallocFunction)(size_t size);
typedef void (*FreeFunction)(void *ptr);
Expand All @@ -40,6 +41,7 @@ typedef int (*PosixMemalignFunction)(void **memptr, size_t alignment, size_t s
typedef void* (*AlignedAllocFunction)(size_t alignment, size_t size);
typedef void* (*PVallocFunction)(size_t size);
typedef void* (*VallocFunction)(size_t size);
typedef void* (*ReallocArrayFunction)(void* ptr, size_t nmemb, size_t size);
typedef void* (*MmapFunction)(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
typedef int (*MunmapFunction)(void *addr, size_t length);

Expand All @@ -52,6 +54,7 @@ static PosixMemalignFunction real_posix_memalign;
static AlignedAllocFunction real_aligned_alloc;
static PVallocFunction real_pvalloc;
static VallocFunction real_valloc;
static ReallocArrayFunction real_reallocarray;

thread_local struct thread_flags_t thread_flags;

Expand Down Expand Up @@ -79,6 +82,7 @@ static void heaptrace_init()
real_aligned_alloc = (AlignedAllocFunction)dlsym(RTLD_NEXT, "aligned_alloc");
real_pvalloc = (PVallocFunction)dlsym(RTLD_NEXT, "pvalloc");
real_valloc = (VallocFunction)dlsym(RTLD_NEXT, "valloc");
real_reallocarray = (ReallocArrayFunction)dlsym(RTLD_NEXT, "reallocarray");

// initialize signal handlers
sighandler_init();
Expand Down Expand Up @@ -381,3 +385,23 @@ void *valloc(size_t size)

return p;
}

extern "C" __visible_default
void *reallocarray(void *ptr, size_t nmemb, size_t size)
{
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
return real_reallocarray(ptr, nmemb, size);

tfs->hook_guard = true;

void* p = real_reallocarray(ptr, nmemb, size);
pr_dbg("reallocarray(%p, %zd, %zd) = %p\n", ptr, nmemb, size, p);
release_backtrace(ptr);
record_backtrace(nmemb * size, p);

tfs->hook_guard = false;

return p;
}

0 comments on commit 70c11fb

Please sign in to comment.