From eb716bf65ab2475f3dcbc512596556192e94e6b2 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 4 Jun 2024 15:15:05 +0900 Subject: [PATCH] Impl. checking calloc as well --- src/another_memcheck.cc | 13 +++++++++++++ src/another_memcheck.h | 2 ++ src/another_memcheck_external.cc | 15 +++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/another_memcheck.cc b/src/another_memcheck.cc index cdc93b6..0df3595 100644 --- a/src/another_memcheck.cc +++ b/src/another_memcheck.cc @@ -18,6 +18,7 @@ namespace SC_AM_Internal { } void *(*real_malloc)(std::size_t) = nullptr; + void *(*real_calloc)(std::size_t, std::size_t) = nullptr; void (*real_free)(void*) = nullptr; Malloced::Malloced() : address(nullptr), size(0) {} @@ -80,6 +81,18 @@ namespace SC_AM_Internal { return address; } + void *Stats::do_calloc(std::size_t n, std::size_t size) { + void *address = real_calloc(n, size); + if (address != nullptr) { + Malloced *data = reinterpret_cast(real_malloc(sizeof(Malloced))); + data->address = address; + data->size = size; + ListNode::add_to_list(malloced_list_tail, data); + } + + return address; + } + void Stats::do_free(void *ptr) { if (ptr) { if(!ListNode::remove_from_list(malloced_list_head, ptr)) { diff --git a/src/another_memcheck.h b/src/another_memcheck.h index 6e96a33..aab76d4 100644 --- a/src/another_memcheck.h +++ b/src/another_memcheck.h @@ -13,6 +13,7 @@ namespace SC_AM_Internal { constexpr unsigned int DATA_ITEMS_SIZE = 500; extern void *(*real_malloc)(std::size_t size); + extern void *(*real_calloc)(std::size_t n, std::size_t size); extern void (*real_free)(void *ptr); struct Malloced { @@ -45,6 +46,7 @@ namespace SC_AM_Internal { void initialize(); void *do_malloc(std::size_t size); + void *do_calloc(std::size_t n, std::size_t size); void do_free(void *ptr); void print_status() const; diff --git a/src/another_memcheck_external.cc b/src/another_memcheck_external.cc index dc604f0..268f666 100644 --- a/src/another_memcheck_external.cc +++ b/src/another_memcheck_external.cc @@ -22,6 +22,21 @@ extern "C" { return SC_AM_Internal::stats->do_malloc(size); } + void *calloc(std::size_t n, std::size_t size) { + std::clog << "attempting to calloc size: " << size << "...\n"; + + if (SC_AM_Internal::real_calloc == nullptr) { + SC_AM_Internal::real_calloc = reinterpret_cast( + dlsym(RTLD_NEXT, "calloc")); + } + + if (SC_AM_Internal::stats == nullptr) { + SC_AM_Internal::stats = SC_AM_Internal::get_init_stats(); + } + + return SC_AM_Internal::stats->do_calloc(n, size); + } + void free(void *ptr) { std::clog << "attempting to free...\n";