Skip to content

Commit

Permalink
Replaced instance based allocation tracking flag by global one
Browse files Browse the repository at this point in the history
  • Loading branch information
mhahnFr committed Aug 23, 2023
1 parent a1793be commit f399ea0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 64 deletions.
2 changes: 1 addition & 1 deletion code/LeakSani.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ auto LSan::getTotalLeakedBytes() -> std::size_t {
void LSan::__exit_hook() {
using Formatter::Style;

getTracker().setIgnoreMalloc(true);
setIgnoreMalloc(true);
std::ostream & out = __lsan_printCout ? std::cout : std::cerr;
out << std::endl
<< Formatter::get(Style::GREEN) << "Exiting" << Formatter::clear(Style::GREEN)
Expand Down
7 changes: 3 additions & 4 deletions code/signalHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ static std::string signalString(int signal) {
void callstackSignal(int) {
using Formatter::Style;

auto & instance = LSan::getTracker();
bool ignore = instance.getIgnoreMalloc();
bool ignore = LSan::getIgnoreMalloc();
LSan::setIgnoreMalloc(true);

instance.setIgnoreMalloc(true);
std::ostream & out = __lsan_printCout ? std::cout : std::cerr;
out << Formatter::get(Style::ITALIC)
<< "The current callstack:"
Expand All @@ -78,7 +77,7 @@ void callstackSignal(int) {
MallocInfo::printCallstack(lcs::callstack(), out);
out << std::endl;
if (!ignore) {
instance.setIgnoreMalloc(false);
LSan::setIgnoreMalloc(false);
}
}

Expand Down
16 changes: 6 additions & 10 deletions code/statistics/lsan_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,8 @@ static inline void __lsan_printFragmentationByteBar(size_t width, std::ostream &
void __lsan_printFragmentationStatsWithWidth(size_t width) {
using Formatter::Style;

auto & instance = LSan::getTracker();

bool ignore = instance.getIgnoreMalloc();
instance.setIgnoreMalloc(true);
bool ignore = LSan::getIgnoreMalloc();
LSan::setIgnoreMalloc(true);
std::ostream & out = __lsan_printCout ? std::cout : std::cerr;
if (__lsan_statsActive) {
__lsan_printStatsCore("memory fragmentation", width, out,
Expand All @@ -311,17 +309,15 @@ void __lsan_printFragmentationStatsWithWidth(size_t width) {
<< Formatter::clearAll() << std::endl << std::endl;
}
if (!ignore) {
instance.setIgnoreMalloc(false);
LSan::setIgnoreMalloc(false);
}
}

void __lsan_printStatsWithWidth(size_t width) {
using Formatter::Style;

auto & instance = LSan::getTracker();

bool ignore = instance.getIgnoreMalloc();
instance.setIgnoreMalloc(true);
bool ignore = LSan::getIgnoreMalloc();
LSan::setIgnoreMalloc(true);
std::ostream & out = __lsan_printCout ? std::cout : std::cerr;
if (__lsan_statsActive) {
__lsan_printStatsCore("memory usage", width, out,
Expand All @@ -340,6 +336,6 @@ void __lsan_printStatsWithWidth(size_t width) {
<< Formatter::clearAll() << std::endl << std::endl;
}
if (!ignore) {
instance.setIgnoreMalloc(false);
LSan::setIgnoreMalloc(false);
}
}
88 changes: 39 additions & 49 deletions code/wrap_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,48 @@ bool __lsan_glibc = false;
#endif

auto __wrap_malloc(std::size_t size, const char * file, int line) -> void * {
auto & instance = LSan::getTracker();
auto ret = LSan::malloc(size);
auto ret = LSan::malloc(size);

if (ret != nullptr && !instance.getIgnoreMalloc()) {
instance.setIgnoreMalloc(true);
if (ret != nullptr && !LSan::getIgnoreMalloc()) {
LSan::setIgnoreMalloc(true);
if (size == 0) {
if (!__lsan_invalidCrash || __lsan_glibc) {
warn("Invalid allocation of size 0", file, line, __builtin_return_address(0));
} else {
crash("Invalid allocation of size 0", file, line, __builtin_return_address(0));
}
}
instance.addMalloc(MallocInfo(ret, size, file, line, __builtin_return_address(0)));
instance.setIgnoreMalloc(false);
LSan::getTracker().addMalloc(MallocInfo(ret, size, file, line, __builtin_return_address(0)));
LSan::setIgnoreMalloc(false);
}
return ret;
}

auto __wrap_calloc(std::size_t objectSize, std::size_t count, const char * file, int line) -> void * {
auto & instance = LSan::getTracker();
auto ret = LSan::calloc(objectSize, count);
auto ret = LSan::calloc(objectSize, count);

if (ret != nullptr && !instance.getIgnoreMalloc()) {
instance.setIgnoreMalloc(true);
if (ret != nullptr && !LSan::getIgnoreMalloc()) {
LSan::setIgnoreMalloc(true);
if (objectSize * count == 0) {
if (!__lsan_invalidCrash || __lsan_glibc) {
warn("Invalid allocation of size 0", file, line, __builtin_return_address(0));
} else {
crash("Invalid allocation of size 0", file, line, __builtin_return_address(0));
}
}
instance.addMalloc(MallocInfo(ret, objectSize * count, file, line, __builtin_return_address(0)));
instance.setIgnoreMalloc(false);
LSan::getTracker().addMalloc(MallocInfo(ret, objectSize * count, file, line, __builtin_return_address(0)));
LSan::setIgnoreMalloc(false);
}
return ret;
}

auto __wrap_realloc(void * pointer, std::size_t size, const char * file, int line) -> void * {
auto & instance = LSan::getTracker();
auto ignored = instance.getIgnoreMalloc();
auto ignored = LSan::getIgnoreMalloc();
if (!ignored) {
instance.setIgnoreMalloc(true);
LSan::setIgnoreMalloc(true);
}
void * ptr = LSan::realloc(pointer, size);
auto & instance = LSan::getTracker();
void * ptr = LSan::realloc(pointer, size);
if (!ignored) {
if (ptr != nullptr) {
if (pointer != ptr) {
Expand All @@ -90,38 +88,34 @@ auto __wrap_realloc(void * pointer, std::size_t size, const char * file, int lin
instance.changeMalloc(MallocInfo(ptr, size, file, line, __builtin_return_address(0)));
}
}
instance.setIgnoreMalloc(false);
LSan::setIgnoreMalloc(false);
}
return ptr;
}

void __wrap_free(void * pointer, const char * file, int line) {
auto & instance = LSan::getTracker();

if (!instance.getIgnoreMalloc()) {
instance.setIgnoreMalloc(true);
if (!LSan::getIgnoreMalloc()) {
LSan::setIgnoreMalloc(true);
if (pointer == nullptr && __lsan_freeNull) {
warn("Free of NULL", file, line, __builtin_return_address(0));
}
bool removed = instance.removeMalloc(MallocInfo(pointer, 0, file, line, __builtin_return_address(0)));
bool removed = LSan::getTracker().removeMalloc(MallocInfo(pointer, 0, file, line, __builtin_return_address(0)));
if (__lsan_invalidFree && !removed) {
if (__lsan_invalidCrash) {
crash("Invalid free", file, line, __builtin_return_address(0));
} else {
warn("Invalid free", file, line, __builtin_return_address(0));
}
}
instance.setIgnoreMalloc(false);
LSan::setIgnoreMalloc(false);
}
LSan::free(pointer);
}

[[ noreturn ]] void __wrap_exit(int code, const char * file, int line) {
using Formatter::Style;

auto & instance = LSan::getTracker();

instance.setIgnoreMalloc(true);
LSan::setIgnoreMalloc(true);
std::ostream & out = __lsan_printCout ? std::cout : std::cerr;
out << std::endl
<< Formatter::get(Style::GREEN) << "Exiting" << Formatter::clear(Style::GREEN) << " at "
Expand All @@ -139,50 +133,48 @@ void __wrap_free(void * pointer, const char * file, int line) {
}

auto malloc(std::size_t size) -> void * {
auto & instance = LSan::getTracker();
auto ptr = LSan::malloc(size);
auto ptr = LSan::malloc(size);

if (ptr != nullptr && !instance.getIgnoreMalloc()) {
instance.setIgnoreMalloc(true);
if (ptr != nullptr && !LSan::getIgnoreMalloc()) {
LSan::setIgnoreMalloc(true);
if (size == 0) {
if (!__lsan_invalidCrash || __lsan_glibc) {
warn("Invalid allocation of size 0", __builtin_return_address(0));
} else {
crash("Invalid allocation of size 0", __builtin_return_address(0));
}
}
instance.addMalloc(MallocInfo(ptr, size, __builtin_return_address(0)));
instance.setIgnoreMalloc(false);
LSan::getTracker().addMalloc(MallocInfo(ptr, size, __builtin_return_address(0)));
LSan::setIgnoreMalloc(false);
}
return ptr;
}

auto calloc(std::size_t objectSize, std::size_t count) -> void * {
auto & instance = LSan::getTracker();
auto ptr = LSan::calloc(objectSize, count);
auto ptr = LSan::calloc(objectSize, count);

if (ptr != nullptr && !instance.getIgnoreMalloc()) {
instance.setIgnoreMalloc(true);
if (ptr != nullptr && !LSan::getIgnoreMalloc()) {
LSan::setIgnoreMalloc(true);
if (objectSize * count == 0) {
if (!__lsan_invalidCrash || __lsan_glibc) {
warn("Invalid allocation of size 0", __builtin_return_address(0));
} else {
crash("Invalid allocation of size 0", __builtin_return_address(0));
}
}
instance.addMalloc(MallocInfo(ptr, objectSize * count, __builtin_return_address(0)));
instance.setIgnoreMalloc(false);
LSan::getTracker().addMalloc(MallocInfo(ptr, objectSize * count, __builtin_return_address(0)));
LSan::setIgnoreMalloc(false);
}
return ptr;
}

auto realloc(void * pointer, std::size_t size) -> void * {
auto & instance = LSan::getTracker();
auto ignored = instance.getIgnoreMalloc();
auto ignored = LSan::getIgnoreMalloc();
if (!ignored) {
instance.setIgnoreMalloc(true);
LSan::setIgnoreMalloc(true);
}
void * ptr = LSan::realloc(pointer, size);
auto & instance = LSan::getTracker();
void * ptr = LSan::realloc(pointer, size);
if (!ignored) {
if (ptr != nullptr) {
if (pointer != ptr) {
Expand All @@ -194,28 +186,26 @@ auto realloc(void * pointer, std::size_t size) -> void * {
instance.changeMalloc(MallocInfo(ptr, size, __builtin_return_address(0)));
}
}
instance.setIgnoreMalloc(false);
LSan::setIgnoreMalloc(false);
}
return ptr;
}

void free(void * pointer) {
auto & instance = LSan::getTracker();

if (!instance.getIgnoreMalloc()) {
instance.setIgnoreMalloc(true);
if (!LSan::getIgnoreMalloc()) {
LSan::setIgnoreMalloc(true);
if (pointer == nullptr && __lsan_freeNull) {
warn("Free of NULL", __builtin_return_address(0));
}
bool removed = instance.removeMalloc(pointer);
bool removed = LSan::getTracker().removeMalloc(pointer);
if (__lsan_invalidFree && !removed) {
if (__lsan_invalidCrash) {
crash("Invalid free", __builtin_return_address(0));
} else {
warn("Invalid free", __builtin_return_address(0));
}
}
instance.setIgnoreMalloc(false);
LSan::setIgnoreMalloc(false);
}
LSan::free(pointer);
}

0 comments on commit f399ea0

Please sign in to comment.