Compare commits

..

No commits in common. "d4c47f871bb101f093cba5d93f4cdb6f42f49edf" and "23b851d8f39e9ed2e617e181911f01ac54290367" have entirely different histories.

5 changed files with 8 additions and 92 deletions

View file

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.7)
project(AnotherMemCheck)
set(AnotherMemCheck_VERSION 2.5)
set(AnotherMemCheck_VERSION 2.4)
set(AnotherMemCheck_SOVERSION 2)
set(AnotherMemCheck_SOURCES

View file

@ -1,9 +1,5 @@
# Changelog
## Version 2.5
Added support for realloc().
## Version 2.4
Fixed free() not actually calling real_free().

View file

@ -24,7 +24,6 @@ namespace SC_AM_Internal {
void *(*real_malloc)(std::size_t) = nullptr;
void *(*real_calloc)(std::size_t, std::size_t) = nullptr;
void *(*real_realloc)(void*, std::size_t) = nullptr;
void (*real_free)(void*) = nullptr;
Malloced::Malloced() : address(nullptr), size(0), id(0) {}
@ -40,34 +39,18 @@ namespace SC_AM_Internal {
new_node->data = data;
}
void ListNode::add_to_list(ListNode *tail, ListNode *node) {
node->next = tail;
node->prev = tail->prev;
tail->prev->next = node;
tail->prev = node;
}
bool ListNode::remove_from_list(ListNode *head,
void *ptr,
Stats *defer_handler) {
bool ListNode::remove_from_list(ListNode *head, void *ptr) {
ListNode *node = head;
while (node != nullptr) {
node = node->next;
if (node && node->data && node->data->address == ptr) {
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << " removing id: " << node->data->id;
if (!defer_handler) {
std::clog << std::endl;
}
std::clog << " id: " << node->data->id << std::endl;
}
node->prev->next = node->next;
node->next->prev = node->prev;
if (defer_handler) {
defer_handler->deferred_node = node;
} else {
real_free(node->data);
real_free(node);
}
return true;
}
}
@ -75,7 +58,7 @@ namespace SC_AM_Internal {
return false;
}
Stats::Stats() : malloced_list_head(nullptr), malloced_list_tail(nullptr), deferred_node(nullptr), pthread_mutex{.__align=0} {
Stats::Stats() : malloced_list_head(nullptr), malloced_list_tail(nullptr), pthread_mutex{.__align=0} {
}
void Stats::initialize() {
@ -88,8 +71,6 @@ namespace SC_AM_Internal {
malloced_list_head->data = nullptr;
malloced_list_tail->data = nullptr;
deferred_node = nullptr;
pthread_mutex_init(&pthread_mutex, nullptr);
on_exit([] ([[maybe_unused]] int status, void *ptr) {
@ -148,45 +129,6 @@ namespace SC_AM_Internal {
return address;
}
void *Stats::do_realloc(void *ptr, std::size_t size) {
if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) {
std::clog << "ERROR: pthread mutex not properly initialized!\n";
return nullptr;
}
if (ptr) {
if (!ListNode::remove_from_list(malloced_list_head, ptr, this)) {
std::clog << "WARNING: Attempted free of unknown memory location!\n";
}
}
void *address = real_realloc(ptr, size);
if (address != nullptr) {
if (deferred_node) {
deferred_node->data->address = address;
deferred_node->data->size = size;
deferred_node->data->id = deferred_node->data->count++;
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << ", adding id: " << deferred_node->data->id << std::endl;
}
ListNode::add_to_list(malloced_list_tail, deferred_node);
deferred_node = nullptr;
} else {
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
data->address = address;
data->size = size;
data->id = data->count++;
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << ", adding id: " << data->id << std::endl;
}
ListNode::add_to_list(malloced_list_tail, data);
}
}
pthread_mutex_unlock(&pthread_mutex);
return address;
}
bool Stats::do_free(void *ptr) {
bool result = false;
if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) {

View file

@ -21,7 +21,6 @@ namespace SC_AM_Internal {
extern void *(*real_malloc)(std::size_t size);
extern void *(*real_calloc)(std::size_t n, std::size_t size);
extern void *(*real_realloc)(void *ptr, std::size_t size);
extern void (*real_free)(void *ptr);
struct Malloced {
@ -38,11 +37,8 @@ namespace SC_AM_Internal {
ListNode() : next(nullptr), prev(nullptr), data(nullptr) {}
static void add_to_list(ListNode *tail, Malloced *data);
static void add_to_list(ListNode *tail, ListNode *node);
/// Returns true if removed.
static bool remove_from_list(ListNode *head,
void *ptr,
Stats *defer_handler = nullptr);
static bool remove_from_list(ListNode *head, void *ptr);
ListNode *next;
ListNode *prev;
@ -55,7 +51,6 @@ namespace SC_AM_Internal {
ListNode *malloced_list_head;
ListNode *malloced_list_tail;
ListNode *deferred_node;
pthread_mutex_t pthread_mutex;
void initialize();
@ -63,7 +58,6 @@ namespace SC_AM_Internal {
void *do_malloc(std::size_t size);
void *do_calloc(std::size_t n, std::size_t size);
void *do_realloc(void*, std::size_t size);
/// true on success.
bool do_free(void *ptr);

View file

@ -41,22 +41,6 @@ extern "C" {
return SC_AM_Internal::stats->do_calloc(n, size);
}
void *realloc(void *ptr, std::size_t size) {
if (SC_AM_Internal::real_realloc == nullptr) {
SC_AM_Internal::real_realloc = reinterpret_cast<void*(*)(void*, std::size_t)>(dlsym(RTLD_NEXT, "realloc"));
}
if (SC_AM_Internal::stats == nullptr) {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to realloc size: " << size;
}
return SC_AM_Internal::stats->do_realloc(ptr, size);
}
void free(void *ptr) {
if (SC_AM_Internal::real_free == nullptr) {
SC_AM_Internal::real_free = reinterpret_cast<void(*)(void*)>(dlsym(RTLD_NEXT, "free"));