2024-06-04 02:59:36 +00:00
|
|
|
// Standard library includes.
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
2024-11-12 05:38:25 +00:00
|
|
|
#include <mutex>
|
2024-06-04 02:59:36 +00:00
|
|
|
|
|
|
|
// Local includes.
|
|
|
|
#include "another_memcheck.h"
|
|
|
|
|
2024-11-12 06:29:17 +00:00
|
|
|
// Function declaration.
|
|
|
|
void exit_handler_stats();
|
|
|
|
|
2024-06-04 02:59:36 +00:00
|
|
|
namespace SC_AM_Internal {
|
|
|
|
Stats *stats = nullptr;
|
2024-06-06 06:16:42 +00:00
|
|
|
int is_env_status = 0;
|
2024-06-26 05:01:51 +00:00
|
|
|
unsigned long long Malloced::count = 0;
|
2024-06-04 02:59:36 +00:00
|
|
|
|
|
|
|
Stats *get_init_stats() {
|
|
|
|
Stats *stats = reinterpret_cast<SC_AM_Internal::Stats*>(
|
|
|
|
real_malloc(sizeof(Stats)));
|
2024-11-12 07:10:10 +00:00
|
|
|
[[maybe_unused]] void *unused = new(stats) Stats{};
|
2024-06-06 06:16:42 +00:00
|
|
|
is_env_status = getenv("ANOTHER_MEMCHECK_QUIET") != nullptr
|
|
|
|
? ANOTHER_MEMCHECK_QUIET_EXISTS
|
|
|
|
: ANOTHER_MEMCHECK_QUIET_NOT_EXISTS;
|
2024-06-04 02:59:36 +00:00
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *(*real_malloc)(std::size_t) = nullptr;
|
2024-06-04 06:15:05 +00:00
|
|
|
void *(*real_calloc)(std::size_t, std::size_t) = nullptr;
|
2024-06-27 05:45:40 +00:00
|
|
|
void *(*real_realloc)(void*, std::size_t) = nullptr;
|
2024-06-04 02:59:36 +00:00
|
|
|
void (*real_free)(void*) = nullptr;
|
|
|
|
|
2024-06-26 05:01:51 +00:00
|
|
|
Malloced::Malloced() : address(nullptr), size(0), id(0) {}
|
|
|
|
Malloced::Malloced(void *address, std::size_t size) : address(address), size(size), id(0) {}
|
2024-06-04 02:59:36 +00:00
|
|
|
|
2024-06-04 06:08:33 +00:00
|
|
|
void ListNode::add_to_list(ListNode *tail, Malloced *data) {
|
|
|
|
ListNode *new_node = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
|
|
|
|
new_node->next = tail;
|
|
|
|
new_node->prev = tail->prev;
|
|
|
|
tail->prev->next = new_node;
|
|
|
|
tail->prev = new_node;
|
|
|
|
|
|
|
|
new_node->data = data;
|
|
|
|
}
|
|
|
|
|
2024-06-27 05:45:40 +00:00
|
|
|
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) {
|
2024-06-04 06:08:33 +00:00
|
|
|
ListNode *node = head;
|
|
|
|
while (node != nullptr) {
|
|
|
|
node = node->next;
|
2024-06-27 05:00:39 +00:00
|
|
|
if (node && node->data && node->data->address == ptr) {
|
2024-06-26 05:01:51 +00:00
|
|
|
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
|
2024-06-27 05:45:40 +00:00
|
|
|
std::clog << " removing id: " << node->data->id;
|
|
|
|
if (!defer_handler) {
|
|
|
|
std::clog << std::endl;
|
|
|
|
}
|
2024-06-26 05:01:51 +00:00
|
|
|
}
|
2024-06-04 06:08:33 +00:00
|
|
|
node->prev->next = node->next;
|
|
|
|
node->next->prev = node->prev;
|
2024-06-27 05:45:40 +00:00
|
|
|
if (defer_handler) {
|
|
|
|
defer_handler->deferred_node = node;
|
|
|
|
} else {
|
|
|
|
real_free(node->data);
|
|
|
|
real_free(node);
|
|
|
|
}
|
2024-06-04 06:08:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-11-12 05:46:36 +00:00
|
|
|
Stats::Stats() : malloced_list_head(nullptr), malloced_list_tail(nullptr), deferred_node(nullptr), recursive_mutex(nullptr) {
|
2024-06-04 06:08:33 +00:00
|
|
|
malloced_list_head = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
|
|
|
|
malloced_list_tail = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
|
|
|
|
malloced_list_head->next = malloced_list_tail;
|
|
|
|
malloced_list_head->prev = nullptr;
|
|
|
|
malloced_list_tail->next = nullptr;
|
|
|
|
malloced_list_tail->prev = malloced_list_head;
|
|
|
|
malloced_list_head->data = nullptr;
|
|
|
|
malloced_list_tail->data = nullptr;
|
|
|
|
|
2024-11-12 05:57:49 +00:00
|
|
|
deferred_node = nullptr;
|
|
|
|
|
2024-11-12 05:38:25 +00:00
|
|
|
recursive_mutex = real_malloc(sizeof(std::recursive_mutex));
|
2024-11-12 07:10:10 +00:00
|
|
|
[[maybe_unused]] void *unused = new(recursive_mutex) std::recursive_mutex{};
|
2024-06-04 06:40:57 +00:00
|
|
|
|
2024-11-12 06:29:17 +00:00
|
|
|
std::atexit(exit_handler_stats);
|
2024-06-04 02:59:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-12 07:10:10 +00:00
|
|
|
Stats::~Stats() {
|
2024-11-12 05:38:25 +00:00
|
|
|
using std_recursive_mutex = std::recursive_mutex;
|
|
|
|
((std::recursive_mutex*)recursive_mutex)->~std_recursive_mutex();
|
|
|
|
real_free(recursive_mutex);
|
2024-06-04 06:40:57 +00:00
|
|
|
// TODO maybe cleanup list, but it is the end of the program.
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:59:36 +00:00
|
|
|
void *Stats::do_malloc(std::size_t size) {
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->lock();
|
2024-06-04 06:40:57 +00:00
|
|
|
|
2024-06-04 02:59:36 +00:00
|
|
|
void *address = real_malloc(size);
|
|
|
|
if (address != nullptr) {
|
2024-06-04 06:15:05 +00:00
|
|
|
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
|
|
|
|
data->address = address;
|
|
|
|
data->size = size;
|
2024-06-26 05:01:51 +00:00
|
|
|
data->id = data->count++;
|
|
|
|
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
|
|
|
|
std::clog << " id: " << data->id << std::endl;
|
|
|
|
}
|
2024-06-04 06:15:05 +00:00
|
|
|
ListNode::add_to_list(malloced_list_tail, data);
|
|
|
|
}
|
|
|
|
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->unlock();
|
2024-06-04 06:15:05 +00:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Stats::do_calloc(std::size_t n, std::size_t size) {
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->lock();
|
2024-06-04 06:40:57 +00:00
|
|
|
|
2024-06-04 06:15:05 +00:00
|
|
|
void *address = real_calloc(n, size);
|
|
|
|
if (address != nullptr) {
|
2024-06-04 06:08:33 +00:00
|
|
|
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
|
|
|
|
data->address = address;
|
2024-06-04 06:54:45 +00:00
|
|
|
data->size = n * size;
|
2024-06-26 05:01:51 +00:00
|
|
|
data->id = data->count++;
|
|
|
|
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
|
|
|
|
std::clog << " id: " << data->id << std::endl;
|
|
|
|
}
|
2024-06-04 06:08:33 +00:00
|
|
|
ListNode::add_to_list(malloced_list_tail, data);
|
2024-06-04 02:59:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->unlock();
|
2024-06-04 02:59:36 +00:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2024-06-27 05:45:40 +00:00
|
|
|
void *Stats::do_realloc(void *ptr, std::size_t size) {
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->lock();
|
2024-06-27 05:45:40 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->unlock();
|
2024-06-27 05:45:40 +00:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2024-06-26 05:01:51 +00:00
|
|
|
bool Stats::do_free(void *ptr) {
|
|
|
|
bool result = false;
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->lock();
|
2024-06-04 06:40:57 +00:00
|
|
|
|
2024-06-04 02:59:36 +00:00
|
|
|
if (ptr) {
|
2024-06-27 05:45:40 +00:00
|
|
|
if (!ListNode::remove_from_list(malloced_list_head, ptr)) {
|
2024-06-04 06:08:33 +00:00
|
|
|
std::clog << "WARNING: Attempted free of unknown memory location!\n";
|
2024-06-26 05:01:51 +00:00
|
|
|
} else {
|
|
|
|
result = true;
|
2024-06-27 05:00:39 +00:00
|
|
|
real_free(ptr);
|
2024-06-04 02:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-04 06:40:57 +00:00
|
|
|
|
2024-11-12 05:38:25 +00:00
|
|
|
((std::recursive_mutex*)recursive_mutex)->unlock();
|
2024-06-26 05:01:51 +00:00
|
|
|
return result;
|
2024-06-04 02:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stats::print_status() const {
|
2024-06-04 03:17:43 +00:00
|
|
|
// This function intentionally does not free its list of malloced memory as
|
|
|
|
// it is expected for the OS to reclaim memory from stopped processes and
|
|
|
|
// this function only runs at the end of program execution.
|
2024-06-04 02:59:36 +00:00
|
|
|
std::clog << "List of unfreed memory:\n";
|
2024-06-04 06:08:33 +00:00
|
|
|
auto *node = malloced_list_head;
|
2024-06-04 02:59:36 +00:00
|
|
|
while (node->next != nullptr) {
|
|
|
|
node = node->next;
|
|
|
|
|
2024-06-04 06:08:33 +00:00
|
|
|
if (node->data) {
|
2024-06-26 05:01:51 +00:00
|
|
|
std::clog << " " << node->data->address
|
|
|
|
<< ": size " << node->data->size
|
|
|
|
<< " id " << node->data->id << '\n';
|
2024-06-04 06:08:33 +00:00
|
|
|
}
|
2024-06-04 02:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-12 06:29:17 +00:00
|
|
|
// Function definition.
|
|
|
|
void exit_handler_stats() {
|
|
|
|
if (SC_AM_Internal::stats != nullptr) {
|
|
|
|
SC_AM_Internal::stats->print_status();
|
2024-11-12 07:10:10 +00:00
|
|
|
using SCS_AM_INTERNAL_Stats = SC_AM_Internal::Stats;
|
|
|
|
SC_AM_Internal::stats->~SCS_AM_INTERNAL_Stats();
|
2024-11-12 06:29:17 +00:00
|
|
|
SC_AM_Internal::stats = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:59:36 +00:00
|
|
|
// vim: et sw=2 ts=2 sts=2
|