Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDB: fix circular dependency on malloc in std::vector #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/bonus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ extern "C"
#include "check.hpp"
#include "gnl.hpp"

//https://github.com/arhuaco/ram-is-mine/blob/master/src/ram_is_mine.c
extern bool is_initializing;

int iTest = 1;
int main(void)
{
is_initializing = false;
signal(SIGSEGV, sigsegv); int fd[4];
title("[BUFFER_SIZE = " << BUFFER_SIZE << "]: " << ENDL)

Expand Down
4 changes: 4 additions & 0 deletions tests/mandatory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ extern "C"
#include "check.hpp"
#include "gnl.hpp"

//https://github.com/arhuaco/ram-is-mine/blob/master/src/ram_is_mine.c
extern bool is_initializing;

int iTest = 1;
int main(void)
{
is_initializing = false;
signal(SIGSEGV, sigsegv); int fd;
title("[BUFFER_SIZE = " << BUFFER_SIZE << "]: " << ENDL)
title("Invalid fd: ")
Expand Down
11 changes: 9 additions & 2 deletions utils/color.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#include "color.hpp"

extern bool is_initializing;

std::ostream &
operator<<(std::ostream & os, Color c)
{
return os << "\e[" << static_cast<int>(c) << "m";
}
bool old_is_initializing = is_initializing;
is_initializing = true;
std::basic_ostream<char, std::char_traits<char> > &basicOstream =
os << "\e[" << static_cast<int>(c) << "m";
is_initializing = old_is_initializing;
return basicOstream;
}
16 changes: 15 additions & 1 deletion utils/gnl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
# include "leaks.hpp"

//Don't do that at home
#define TEST(x) {int status = 0; int test = fork(); if (test == 0) {x showLeaks(); exit(EXIT_SUCCESS);} else {usleep(TIMEOUT_US); if (waitpid(test, &status, WNOHANG) == 0) {kill(test, 9); cout << FG_RED << "TIMEOUT";}}}
#define TEST(x) { \
int status = 0; \
int test = fork();\
if (test == 0) { \
x \
showLeaks(); \
exit(EXIT_SUCCESS); \
} else { \
usleep(TIMEOUT_US); \
if (waitpid(test, &status, WNOHANG) == 0) { \
kill(test, 9); \
cout << FG_RED << "TIMEOUT";\
} \
} \
}

void gnl(int fd, char const * s);

Expand Down
49 changes: 36 additions & 13 deletions utils/leaks.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#include <cstdio>
#include "leaks.hpp"

std::vector<ptr> mallocList;

static void *(*libc_malloc)(size_t) = NULL;
bool is_initializing;
// https://stackoverflow.com/questions/6083337
static void mtrace_init(void)
{
is_initializing = true;
libc_malloc = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
if (NULL == libc_malloc)
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
}

bool operator==(ptr const & p1, ptr const & p2)
{
return (p1.p == p2.p);
Expand All @@ -14,10 +26,12 @@ void * malloc(size_t size)
void * malloc(size_t size) throw()
#endif
{
void *(*libc_malloc)(size_t) = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
void * p = libc_malloc(size);
mallocListAdd(p, size);
return (p);
if(!libc_malloc)
mtrace_init();
void * p = libc_malloc(size);
if (!is_initializing)
mallocListAdd(p, size);
return (p);
}

#ifdef __APPLE__
Expand All @@ -34,7 +48,10 @@ void free(void * p) throw()

void mallocListAdd(void * p, size_t size)
{
mallocList.push_back(ptr(p, size));
bool old_is_initializing = is_initializing;
is_initializing = true;
mallocList.push_back(ptr(p, size));
is_initializing = old_is_initializing;
}

void mallocListRemove(void * p)
Expand All @@ -46,12 +63,18 @@ void mallocListRemove(void * p)

void showLeaks(void)
{
if (mallocList.size() != 0)
{
std::ostringstream ss; ss << FG_RED << "LEAKS.KO "; write(1, ss.str().c_str(), ss.str().size());
std::vector<ptr>::iterator it = mallocList.begin(); std::vector<ptr>::iterator ite = mallocList.end();
for (; it != ite; ++it)
{std::ostringstream ss; ss << "[" << it->p << " : " << it->size << "] "; write(1, ss.str().c_str(), ss.str().size());}
}
mallocList.clear();
if (mallocList.size() != 0)
{
std::ostringstream ss;
ss << FG_RED << "LEAKS.KO ";
write(1, ss.str().c_str(), ss.str().size());

std::vector<ptr>::iterator it = mallocList.begin();
std::vector<ptr>::iterator ite = mallocList.end();
for (; it != ite; ++it) {
ss << "[" << it->p << " : " << it->size << "] ";
write(1, ss.str().c_str(), ss.str().size());
}
}
mallocList.clear();
}