From c982fe768173829686ce904c14c35432b7ea0111 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Thu, 18 Apr 2024 19:40:21 +0200 Subject: [PATCH 01/36] Updated CallstackLibrary --- CallstackLibrary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CallstackLibrary b/CallstackLibrary index f0f42ec..43f8915 160000 --- a/CallstackLibrary +++ b/CallstackLibrary @@ -1 +1 @@ -Subproject commit f0f42ec7609c8cb94ee00e9c3e3daee645bf127d +Subproject commit 43f8915c9543141bf3f880d47ef8ceb58acae587 From 12ad5e13822543bb28190df257dd37965db63b20 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Thu, 18 Apr 2024 19:45:47 +0200 Subject: [PATCH 02/36] Removed the line number hack --- LeakSanitizer.xcodeproj/project.pbxproj | 6 --- include/leaksan.h | 71 ------------------------- include/stdlib.h | 27 ---------- 3 files changed, 104 deletions(-) delete mode 100644 include/leaksan.h delete mode 100644 include/stdlib.h diff --git a/LeakSanitizer.xcodeproj/project.pbxproj b/LeakSanitizer.xcodeproj/project.pbxproj index 14ae173..f62b8fd 100644 --- a/LeakSanitizer.xcodeproj/project.pbxproj +++ b/LeakSanitizer.xcodeproj/project.pbxproj @@ -27,7 +27,6 @@ BF378F9F2919484100A4DAA9 /* signalHandlers.hpp in Headers */ = {isa = PBXBuildFile; fileRef = BF49A5D82899599300BC1FFD /* signalHandlers.hpp */; }; BF378FA52919484800A4DAA9 /* wrap_malloc.hpp in Headers */ = {isa = PBXBuildFile; fileRef = BFBF736928832F8100BC4208 /* wrap_malloc.hpp */; }; BF378FA82919484A00A4DAA9 /* lsan_stats.h in Headers */ = {isa = PBXBuildFile; fileRef = BF23A2DD289ECE6A00B17349 /* lsan_stats.h */; settings = {ATTRIBUTES = (Public, ); }; }; - BF378FAB2919484C00A4DAA9 /* leaksan.h in Headers */ = {isa = PBXBuildFile; fileRef = BF72C3CB2885CB9E00C7AE5C /* leaksan.h */; }; BF378FAE2919484E00A4DAA9 /* lsan_internals.h in Headers */ = {isa = PBXBuildFile; fileRef = BF621FDC28A291DE00414EE4 /* lsan_internals.h */; settings = {ATTRIBUTES = (Public, ); }; }; BF378FB52919496000A4DAA9 /* libcallstack.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BF378FB42919496000A4DAA9 /* libcallstack.a */; }; BF4368F52AACF04700252E2A /* crashWarner.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BF4368F32AACF04700252E2A /* crashWarner.cpp */; }; @@ -67,8 +66,6 @@ BF621FE128A2B61E00414EE4 /* bytePrinter.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = bytePrinter.hpp; sourceTree = ""; }; BF72C3C92885CB9E00C7AE5C /* LeakSani.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = LeakSani.hpp; sourceTree = ""; }; BF72C3CA2885CB9E00C7AE5C /* LeakSani.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LeakSani.cpp; sourceTree = ""; }; - BF72C3CB2885CB9E00C7AE5C /* leaksan.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = leaksan.h; sourceTree = ""; }; - BF72C3CC2885CB9E00C7AE5C /* stdlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = stdlib.h; sourceTree = ""; }; BF84625A2AD1B2D3003B05F5 /* init.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = init.hpp; sourceTree = ""; }; BF84625B2AD1B489003B05F5 /* lsanMisc.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = lsanMisc.cpp; sourceTree = ""; }; BF84625C2AD1B489003B05F5 /* lsanMisc.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = lsanMisc.hpp; sourceTree = ""; }; @@ -141,9 +138,7 @@ isa = PBXGroup; children = ( BF23A2DD289ECE6A00B17349 /* lsan_stats.h */, - BF72C3CB2885CB9E00C7AE5C /* leaksan.h */, BF621FDC28A291DE00414EE4 /* lsan_internals.h */, - BF72C3CC2885CB9E00C7AE5C /* stdlib.h */, BF34157C2A8405AD00DEEF10 /* deprecation.h */, ); path = include; @@ -247,7 +242,6 @@ BF378F932919482E00A4DAA9 /* Stats.hpp in Headers */, BF378F992919483B00A4DAA9 /* MallocInfo.hpp in Headers */, BF378FA52919484800A4DAA9 /* wrap_malloc.hpp in Headers */, - BF378FAB2919484C00A4DAA9 /* leaksan.h in Headers */, BF378F9F2919484100A4DAA9 /* signalHandlers.hpp in Headers */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/include/leaksan.h b/include/leaksan.h deleted file mode 100644 index 42526c4..0000000 --- a/include/leaksan.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * LeakSanitizer - Small library showing information about lost memory. - * - * Copyright (C) 2022, 2024 mhahnFr - * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . - */ - -#ifndef leaksan_h -#define leaksan_h - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#ifdef __APPLE__ - #warning Simply compile your code with debug symbols for line numbers! -#endif - -void * __wrap_malloc(size_t, const char *, int); -void * __wrap_calloc(size_t, size_t, const char *, int); -void * __wrap_realloc(void *, size_t, const char *, int); -void __wrap_free(void *, const char *, int); -void __wrap_exit(int, const char *, int); - -#ifdef __cplusplus -} // extern "C" - -namespace std { -static inline void* __wrap_malloc(size_t size, const char* file, int line) { - ::__wrap_malloc(size, file, line); -} - -static inline void* __wrap_calloc(size_t size, size_t count, const char* file, int line) { - ::__wrap_calloc(size, count, file, line); -} - -static inline void* __wrap_realloc(void* pointer, size_t size, const char* file, int line) { - ::__wrap_realloc(pointer, size, file, line); -} - -static inline void __wrap_free(void* pointer, const char* file, int line) { - ::__wrap_free(pointer, file, line); -} - -static inline void __wrap_exit(int code, const char* file, int line) { - ::__wrap_exit(code, file, line); -} -} -#endif - -#define malloc(size) __wrap_malloc(size, __FILE__, __LINE__) -#define calloc(size, n) __wrap_calloc(size, n, __FILE__, __LINE__) -#define realloc(ptr, size) __wrap_realloc(ptr, size, __FILE__, __LINE__) -#define free(pointer) __wrap_free(pointer, __FILE__, __LINE__) -#define exit(code) __wrap_exit(code, __FILE__, __LINE__) - -#endif /* leaksan_h */ diff --git a/include/stdlib.h b/include/stdlib.h deleted file mode 100644 index 50818df..0000000 --- a/include/stdlib.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * LeakSanitizer - A small library showing informations about lost memory. - * - * Copyright (C) 2022 mhahnFr - * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . - */ - -#ifndef STDLIB_H -#define STDLIB_H - -#include_next - -#include - -#endif From ace27b962fa7088ba85afeab3e6478a9007f7714 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Thu, 18 Apr 2024 19:46:04 +0200 Subject: [PATCH 03/36] Corrected include guards --- src/allocations/wrap_malloc.hpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/allocations/wrap_malloc.hpp b/src/allocations/wrap_malloc.hpp index b25f76c..a5d659e 100644 --- a/src/allocations/wrap_malloc.hpp +++ b/src/allocations/wrap_malloc.hpp @@ -1,24 +1,26 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2023 mhahnFr + * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ -#ifndef wrap_malloc_h -#define wrap_malloc_h +#ifndef wrap_malloc_hpp +#define wrap_malloc_hpp #include @@ -46,4 +48,4 @@ void free(void *); } /* namespace lsan */ #endif /* __linux__ */ -#endif /* wrap_malloc_h */ +#endif /* wrap_malloc_hpp */ From 2553190ae5c85c42de446975a218b1a3ba53b024 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 10:20:22 +0200 Subject: [PATCH 04/36] Activated a new flag of the CallstackLibrary --- CallstackLibrary | 2 +- Makefile | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CallstackLibrary b/CallstackLibrary index 43f8915..d9fc043 160000 --- a/CallstackLibrary +++ b/CallstackLibrary @@ -1 +1 @@ -Subproject commit 43f8915c9543141bf3f880d47ef8ceb58acae587 +Subproject commit d9fc043d92c6a7ab62d7699ca22910c0f74da8cc diff --git a/Makefile b/Makefile index 1a57401..301d4e9 100644 --- a/Makefile +++ b/Makefile @@ -3,18 +3,20 @@ # # Copyright (C) 2022 - 2024 mhahnFr # -# This file is part of the LeakSanitizer. This library is free software: -# you can redistribute it and/or modify it under the terms of the -# GNU General Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version. +# This file is part of the LeakSanitizer. # -# This library is distributed in the hope that it will be useful, +# The LeakSanitizer is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# The LeakSanitizer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# this library, see the file LICENSE. If not, see . +# You should have received a copy of the GNU General Public License along with the +# LeakSanitizer, see the file LICENSE. If not, see . # CORE_NAME = liblsan @@ -25,7 +27,7 @@ DYLIB_NA = $(CORE_NAME).dylib LIBCALLSTACK_NAME = libcallstack LIBCALLSTACK_DIR = ./CallstackLibrary LIBCALLSTACK_A = $(LIBCALLSTACK_DIR)/$(LIBCALLSTACK_NAME).a -LIBCALLSTACK_FLAG = 'CXX_OPTIMIZED=true' 'CXX_FUNCTIONS=true' +LIBCALLSTACK_FLAG = 'CXX_OPTIMIZED=true' 'CXX_FUNCTIONS=true' 'USE_BUILTINS=false' SRC = $(shell find src -name \*.cpp \! -path $(LIBCALLSTACK_DIR)\*) OBJS = $(patsubst %.cpp, %.o, $(SRC)) @@ -78,7 +80,7 @@ install: mkdir -p $(INSTALL_PATH)/lib mkdir -p "$(INSTALL_PATH)/include/lsan" mv $(NAME) $(INSTALL_PATH)/lib - find "include" -name \*.h \! -path \*/stdlib.h \! -path \*/leaksan.h -exec cp {} "$(INSTALL_PATH)/include/lsan" \; + find "include" -name \*.h -exec cp {} "$(INSTALL_PATH)/include/lsan" \; uninstall: - $(RM) $(INSTALL_PATH)/lib/$(NAME) From 28e1f7f150678d75db2817b2e409d30203328679 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 10:26:54 +0200 Subject: [PATCH 05/36] Deprecated a fragmentation stats abbreviation --- include/lsan_stats.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/include/lsan_stats.h b/include/lsan_stats.h index eef1f6a..6665946 100644 --- a/include/lsan_stats.h +++ b/include/lsan_stats.h @@ -1,20 +1,22 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2023 mhahnFr + * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef lsan_stats_h @@ -208,6 +210,7 @@ static inline void __lsan_printFStats() { * * @since 1.2 */ +DEPRECATED("Since v1.? refer to __lsan_printFStats() or __lsan_printFragmentationStats()") static inline void __lsan_printFragStats() { __lsan_printFragmentationStats(); } @@ -240,6 +243,7 @@ static inline void __lsan_printFStatsWithWidth(size_t width) { * @param width The width in characters the printed bar should have. * @since 1.2 */ +DEPRECATED("Since v1.? refer to __lsan_printFStatsWithWidth(size_t) or __lsan_printFragmentationStatsWithWidth(size_t)") static inline void __lsan_printFragStatsWithWidth(size_t width) { __lsan_printFragmentationStatsWithWidth(width); } From e9f72fcc8989f352f0c4c0b9701edb5fd887b213 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 10:34:07 +0200 Subject: [PATCH 06/36] Cleaned up the lsan misc files --- src/lsanMisc.cpp | 22 +++++++++------------- src/lsanMisc.hpp | 17 ++++++++++------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/lsanMisc.cpp b/src/lsanMisc.cpp index 12fdf0d..63c1b27 100644 --- a/src/lsanMisc.cpp +++ b/src/lsanMisc.cpp @@ -3,27 +3,23 @@ * * Copyright (C) 2023 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include -#include - -#ifdef BENCHMARK - #include - #include -#endif #if __has_include() #include diff --git a/src/lsanMisc.hpp b/src/lsanMisc.hpp index 43d1b50..538bff6 100644 --- a/src/lsanMisc.hpp +++ b/src/lsanMisc.hpp @@ -3,24 +3,27 @@ * * Copyright (C) 2023 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef lsanMisc_hpp #define lsanMisc_hpp #include +#include #include "LeakSani.hpp" From bba30f2c3ebe1a6c28c04c80530313f66bb7654c Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 10:49:14 +0200 Subject: [PATCH 07/36] Removed duplicate replacement functions --- src/allocations/wrap_malloc.cpp | 132 +++++++------------------------- 1 file changed, 27 insertions(+), 105 deletions(-) diff --git a/src/allocations/wrap_malloc.cpp b/src/allocations/wrap_malloc.cpp index b5af64c..d6e9ab5 100644 --- a/src/allocations/wrap_malloc.cpp +++ b/src/allocations/wrap_malloc.cpp @@ -3,18 +3,20 @@ * * Copyright (C) 2022 - 2024 mhahnFr and contributors * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include @@ -24,15 +26,12 @@ #include "interpose.hpp" #include "realAlloc.hpp" #include "../LeakSani.hpp" -#include "../formatter.hpp" #include "../lsanMisc.hpp" -#include "../callstacks/callstackHelper.hpp" +#include "../timing.hpp" #include "../crashWarner/crash.hpp" #include "../crashWarner/warn.hpp" #include "../initialization/init.hpp" -#include "../timing.hpp" -#include "../../include/lsan_stats.h" #include "../../include/lsan_internals.h" #ifdef __linux__ @@ -45,109 +44,32 @@ auto operator new(std::size_t size) -> void * { } #endif /* __linux__ */ +/* + * These wrapper functions still reside here to not break compatibility with + * previously compiled programs. + * They will be removed in one of the next version, at latest in version 2. + * + * - mhahnFr + */ namespace lsan { -auto __wrap_malloc(std::size_t size, const char * file, int line) -> void * { - auto ret = real::malloc(size); - - if (ret != nullptr && inited) { - std::lock_guard lock(getInstance().getMutex()); - if (!getIgnoreMalloc()) { - setIgnoreMalloc(true); - if (__lsan_zeroAllocation && size == 0) { - warn("Implementation-defined allocation of size 0", file, line); - } - getInstance().addMalloc(MallocInfo(ret, size, file, line)); - setIgnoreMalloc(false); - } - } - return ret; +auto __wrap_malloc(std::size_t size, const char*, int) -> void* { + return malloc(size); } -auto __wrap_calloc(std::size_t objectSize, std::size_t count, const char * file, int line) -> void * { - auto ret = real::calloc(objectSize, count); - - if (ret != nullptr && inited) { - std::lock_guard lock(getInstance().getMutex()); - if (!getIgnoreMalloc()) { - setIgnoreMalloc(true); - if (__lsan_zeroAllocation && objectSize * count == 0) { - warn("Implementation-defined allocation of size 0", file, line); - } - getInstance().addMalloc(MallocInfo(ret, objectSize * count, file, line)); - setIgnoreMalloc(false); - } - } - return ret; +auto __wrap_calloc(std::size_t objectSize, std::size_t count, const char*, int) -> void* { + return calloc(objectSize, count); } -auto __wrap_realloc(void * pointer, std::size_t size, const char * file, int line) -> void * { - if (!inited) return real::realloc(pointer, size); - - std::lock_guard lock(getInstance().getMutex()); - - auto ignored = getIgnoreMalloc(); - if (!ignored) { - setIgnoreMalloc(true); - } - void * ptr = real::realloc(pointer, size); - if (!ignored) { - if (ptr != nullptr) { - if (pointer != ptr) { - if (pointer != nullptr) { - getInstance().removeMalloc(pointer); - } - getInstance().addMalloc(MallocInfo(ptr, size, file, line)); - } else { - getInstance().changeMalloc(MallocInfo(ptr, size, file, line)); - } - } - setIgnoreMalloc(false); - } - return ptr; +auto __wrap_realloc(void* pointer, std::size_t size, const char*, int) -> void* { + return realloc(pointer, size); } -void __wrap_free(void * pointer, const char * file, int line) { - if (inited) { - std::lock_guard lock(getInstance().getMutex()); - - if (!getIgnoreMalloc()) { - setIgnoreMalloc(true); - if (pointer == nullptr && __lsan_freeNull) { - warn("Free of NULL", file, line); - } - auto [removed, record] = getInstance().removeMalloc(pointer); - if (__lsan_invalidFree && !removed) { - if (__lsan_invalidCrash) { - crash("Invalid free", record); - } else { - warn("Invalid free", record); - } - } - setIgnoreMalloc(false); - } - } - real::free(pointer); +void __wrap_free(void* pointer, const char*, int) { + return free(pointer); } -[[ noreturn ]] void __wrap_exit(int code, const char * file, int line) { - using formatter::Style; - - setIgnoreMalloc(true); - auto & out = getOutputStream(); - out << std::endl - << formatter::format("Exiting") << " at " - << formatter::get << file << ":" << line << formatter::clear - << std::endl; - - if (__lsan_printExitPoint) { - callstackHelper::format(lcs::callstack(), out); - out << std::endl; - } - - out << std::endl << getInstance() - << std::endl << printInformation; - internalCleanUp(); - _Exit(code); +[[ noreturn ]] void __wrap_exit(int code, const char*, int) { + exit(code); } } From 101070a8e328b06069a3bc141967c6ead5d36ba4 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:05:14 +0200 Subject: [PATCH 08/36] Removed the warning and crash helper functions using direct file and line infos --- src/crashWarner/crash.hpp | 35 ++++++----------- src/crashWarner/crashWarner.cpp | 69 +++++++-------------------------- src/crashWarner/warn.hpp | 33 +++++----------- 3 files changed, 36 insertions(+), 101 deletions(-) diff --git a/src/crashWarner/crash.hpp b/src/crashWarner/crash.hpp index e24fb90..f3830e5 100644 --- a/src/crashWarner/crash.hpp +++ b/src/crashWarner/crash.hpp @@ -3,18 +3,20 @@ * * Copyright (C) 2023 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef crash_hpp @@ -23,25 +25,10 @@ #include #include #include -#include #include "../MallocInfo.hpp" namespace lsan { -/** - * @brief Terminates the linked program and prints the given message, the file name - * and the line number and a callstack. - * - * This function does nothing if the generated callstack is not user relevant. - * - * @param message the message to be printed - * @param file the file name - * @param line the line number - */ -void crash(const std::string & message, - const std::string & file, - const int line); - /** * @brief Terminates the linked program and prints the given message and a callstack. * @@ -83,8 +70,8 @@ void crash(const std::string & message); * @param message the message to be printed * @param info the optional allocation record */ -void crash(const std::string & message, - std::optional> info); +void crash(const std::string& message, + const std::optional>& info); /** * This function resets the signal handler for `SIGABRT` and performs the abort. diff --git a/src/crashWarner/crashWarner.cpp b/src/crashWarner/crashWarner.cpp index caf2e0a..12039b4 100644 --- a/src/crashWarner/crashWarner.cpp +++ b/src/crashWarner/crashWarner.cpp @@ -3,18 +3,20 @@ * * Copyright (C) 2023 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include @@ -23,7 +25,6 @@ #include "crash.hpp" #include "warn.hpp" -#include "../LeakSani.hpp" #include "../lsanMisc.hpp" #include "../formatter.hpp" #include "../callstacks/callstackHelper.hpp" @@ -68,33 +69,6 @@ static inline void printer(const std::string & message, lcs::callstack && callst printer(message, callstack); } -/** - * Prints the given message, the file with line number and the given callstack. - * - * @param message the message to be printed - * @param file the file name - * @param line the line number - * @param callstack the callstack to be printed - * @tparam Warning whether to use warning formatting - */ -template -static inline void printer(const std::string & message, - const std::string & file, - const int line, - lcs::callstack & callstack) { - using formatter::Style; - - const auto colour = Warning ? Style::MAGENTA : Style::RED; - - std::cerr << formatter::get - << formatter::format((Warning ? "Warning: " : "") + message) << ", at " - << formatter::get << file << ":" << line - << formatter::clear - << std::endl; - callstackHelper::format(callstack, std::cerr); - std::cerr << std::endl; -} - /** * Prints the given message, the allocation information found in the * optionally provided allocation record and the given callstack. @@ -148,15 +122,9 @@ void warn(const std::string & message) { }); } -void warn(const std::string & message, const std::string & file, int line) { - withCallstack([&] (auto & callstack) { - printer(message, file, line, callstack); - }); -} - -void warn(const std::string & message, - std::optional> info) { - withCallstack([&] (auto & callstack) { +void warn(const std::string& message, + const std::optional>& info) { + withCallstack([&] (auto& callstack) { printer(message, info, callstack); }); } @@ -180,16 +148,9 @@ void crashForce(const std::string& message, const std::optional& re abort(); } -void crash(const std::string & message, const std::string & file, int line) { - withCallstack([&] (auto & callstack) { - printer(message, file, line, callstack); - abort(); - }); -} - -void crash(const std::string & message, - std::optional> info) { - withCallstack([&] (auto & callstack) { +void crash(const std::string& message, + const std::optional>& info) { + withCallstack([&] (auto& callstack) { printer(message, info, callstack); abort(); }); diff --git a/src/crashWarner/warn.hpp b/src/crashWarner/warn.hpp index 66f2cb3..0cf44da 100644 --- a/src/crashWarner/warn.hpp +++ b/src/crashWarner/warn.hpp @@ -3,18 +3,20 @@ * * Copyright (C) 2023 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitzer, see the file LICENSE. If not, see . */ #ifndef warn_hpp @@ -23,25 +25,10 @@ #include #include #include -#include #include "../MallocInfo.hpp" namespace lsan { -/** - * @brief Prints the given message, the file name, the line number and a callstack up - * to the given omitting address. - * - * This function does nothing if the generated callstack is not user relevant. - * - * @param message the message to be printed - * @param file the file name - * @param line the line number - */ -void warn(const std::string & message, - const std::string & file, - const int line); - /** * @brief Prints the given message and a callstack up to the given omitting address. * @@ -61,7 +48,7 @@ void warn(const std::string& message); * @param info the optional allocation record */ void warn(const std::string & message, - std::optional> info); + const std::optional>& info); } #endif /* warn_hpp */ From a8b402b06f62c9146a2a69afbbb49001414e6f68 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:21:34 +0200 Subject: [PATCH 09/36] Removed an unneeded header --- src/lsan_internals.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lsan_internals.cpp b/src/lsan_internals.cpp index 761932c..28bf534 100644 --- a/src/lsan_internals.cpp +++ b/src/lsan_internals.cpp @@ -18,7 +18,6 @@ */ #include -#include #include #include #include From 5e2ef838b36c7673fe03a73e4a3281a3e7034558 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:21:48 +0200 Subject: [PATCH 10/36] Removed the file and line information from the allocation record --- src/MallocInfo.cpp | 27 ++++++------- src/MallocInfo.hpp | 98 ++++++---------------------------------------- 2 files changed, 24 insertions(+), 101 deletions(-) diff --git a/src/MallocInfo.cpp b/src/MallocInfo.cpp index ff5ebdc..bf1ab36 100644 --- a/src/MallocInfo.cpp +++ b/src/MallocInfo.cpp @@ -1,20 +1,22 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2023 mhahnFr + * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include "MallocInfo.hpp" @@ -32,13 +34,8 @@ auto operator<<(std::ostream & stream, const MallocInfo & self) -> std::ostream stream << formatter::get << formatter::format("Leak") << " of size " << formatter::clear - << bytesToString(self.size) << formatter::get << ", "; - if (self.createdInFile.has_value() && self.createdOnLine.has_value()) { - stream << "allocated at " << formatter::format(self.createdInFile.value() + ":" + std::to_string(self.createdOnLine.value())); - } else { - stream << "allocation stacktrace:"; - } - stream << std::endl; + << bytesToString(self.size) << formatter::get << ", allocation stacktrace:" + << std::endl; self.printCreatedCallstack(stream); return stream; } diff --git a/src/MallocInfo.hpp b/src/MallocInfo.hpp index 796e696..53c6a35 100644 --- a/src/MallocInfo.hpp +++ b/src/MallocInfo.hpp @@ -3,18 +3,20 @@ * * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef MallocInfo_hpp @@ -42,18 +44,10 @@ class MallocInfo { void * pointer; /** The size of the allocated piece of memory. */ std::size_t size; - - /** The filename in which this allocation happened. */ - std::optional createdInFile; - /** The line number in which this allocation happened. */ - std::optional createdOnLine; + /** The callstack where this allocation happened. */ mutable lcs::callstack createdCallstack; - - /** The filename in which this allocation was deallocated. */ - std::optional deletedInFile; - /** The line number in which this allocation was deallocated. */ - std::optional deletedOnLine; + /** Indicating whether this allocation has been deallocated. */ bool deleted; /** The callstack where the deallocation happened. */ @@ -65,34 +59,16 @@ class MallocInfo { * * @param pointer the pointer to the allocated piece of memory * @param size the size of the allocated piece of memory - * @param file the filename where the allocation happened - * @param line the line number inside the file where the allocation happened */ inline MallocInfo(void * const pointer, - const std::size_t size, - std::optional file, - std::optional line) + const std::size_t size) : pointer(pointer), size(size), - createdInFile(file), - createdOnLine(line), createdCallstack(), - deletedInFile(std::nullopt), - deletedOnLine(std::nullopt), deleted(false), deletedCallstack(std::nullopt) {} - /** - * Initializes this allocation record using the given information. - * - * @param pointer the pointer to the allocated piece of memory - * @param size the size of the allocated piece of memory - */ - inline MallocInfo(void * const pointer, std::size_t size) - : MallocInfo(pointer, size, std::nullopt, std::nullopt) - {} - /** * Returns the pointer to the allocated piece of memory. * @@ -101,22 +77,6 @@ class MallocInfo { constexpr inline auto getPointer() const -> const void * { return pointer; } - /** - * @brief Returns the filename where the allocation happened. - * - * @return the filename where the allocation happened. - */ - constexpr inline auto getCreatedInFile() const -> const std::optional & { - return createdInFile; - } - /** - * @brief Returns the line number where the allocation happend. - * - * @return the line number where the allocation happend - */ - constexpr inline auto getCreatedOnLine() const -> std::optional { - return createdOnLine; - } /** * Returns the size of the allocated piece of memory. * @@ -126,40 +86,6 @@ class MallocInfo { return size; } - /** - * Sets the filename in which this allocation was deallocated. - * - * @param file the filename - */ - inline void setDeletedInFile(const std::string & file) { - deletedInFile = file; - } - /** - * Returns the filename where this allocation was deallocated. - * - * @return the filename where the deallocation happened - */ - constexpr inline auto getDeletedInFile() const -> const std::optional & { - return deletedInFile; - } - - /** - * Sets the line number where this allocation was deallocated. - * - * @param line the line number - */ - constexpr inline void setDeletedOnLine(int line) { - deletedOnLine = line; - } - /** - * Returns the line number where this allocation was deallocated. - * - * @return the line number where the deallocation happened - */ - constexpr inline auto getDeletedOnLine() const -> std::optional { - return deletedOnLine; - } - /** * Returns whether this allocation has been deallocated. * From 934ad03a85c6d3ee866c62ccbc14b881331b740c Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:40:15 +0200 Subject: [PATCH 11/36] Made the allocation record a structure --- src/LeakSani.cpp | 38 ++++++------ src/MallocInfo.hpp | 102 ++++---------------------------- src/crashWarner/crashWarner.cpp | 2 +- src/statistics/Stats.hpp | 26 ++++---- src/statistics/lsan_stats.cpp | 36 +++++------ 5 files changed, 67 insertions(+), 137 deletions(-) diff --git a/src/LeakSani.cpp b/src/LeakSani.cpp index 1ef1b5a..6f708c9 100644 --- a/src/LeakSani.cpp +++ b/src/LeakSani.cpp @@ -3,18 +3,20 @@ * * Copyright (C) 2022 - 2024 mhahnFr and contributors * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include @@ -98,12 +100,12 @@ auto LSan::removeMalloc(void* pointer) -> MallocInfoRemoved { auto it = infos.find(pointer); if (it == infos.end()) { return MallocInfoRemoved(false, std::nullopt); - } else if (it->second.isDeleted()) { + } else if (it->second.deleted) { return MallocInfoRemoved(false, it->second); } if (__lsan_statsActive) { stats -= it->second; - it->second.setDeleted(true); + it->second.markDeleted(); } else { infos.erase(it); } @@ -113,20 +115,20 @@ auto LSan::removeMalloc(void* pointer) -> MallocInfoRemoved { auto LSan::changeMalloc(const MallocInfo & info) -> bool { std::lock_guard lock(infoMutex); - auto it = infos.find(info.getPointer()); + auto it = infos.find(info.pointer); if (it == infos.end()) { return false; } if (__lsan_statsActive) { - if (it->second.getPointer() != info.getPointer()) { + if (it->second.pointer != info.pointer) { stats -= it->second; stats += info; } else { - stats.replaceMalloc(it->second.getSize(), info.getSize()); + stats.replaceMalloc(it->second.size, info.size); } - it->second.setDeleted(true); + it->second.markDeleted(); } - infos.insert_or_assign(info.getPointer(), info); + infos.insert_or_assign(info.pointer, info); return true; } @@ -137,7 +139,7 @@ void LSan::addMalloc(MallocInfo && info) { stats += info; } - infos.insert_or_assign(info.getPointer(), info); + infos.insert_or_assign(info.pointer, info); } auto LSan::getTotalAllocatedBytes() -> std::size_t { @@ -145,7 +147,7 @@ auto LSan::getTotalAllocatedBytes() -> std::size_t { std::size_t ret = 0; for (const auto & [ptr, info] : infos) { - ret += info.getSize(); + ret += info.size; } return ret; } @@ -243,9 +245,9 @@ std::ostream & operator<<(std::ostream & stream, LSan & self) { std::snprintf(buffer, 7, "%05.2f", static_cast(j) / total * 100); stream << "\rCollecting the leaks: " << formatter::format(buffer) << " %"; } - if (!info.isDeleted() && callstackHelper::getCallstackType(info.getCreatedCallstack()) == callstackHelper::CallstackType::USER) { + if (!info.deleted && callstackHelper::getCallstackType(info.createdCallstack) == callstackHelper::CallstackType::USER) { ++count; - bytes += info.getSize(); + bytes += info.size; if (i < __lsan_leakCount) { if (isATTY()) { stream << "\r"; diff --git a/src/MallocInfo.hpp b/src/MallocInfo.hpp index 53c6a35..d3ef933 100644 --- a/src/MallocInfo.hpp +++ b/src/MallocInfo.hpp @@ -25,7 +25,6 @@ #include #include #include -#include #include "callstacks/callstackHelper.hpp" @@ -39,69 +38,28 @@ namespace lsan { * It features a callstack and the file name and the line number of the allocation. The size and the * pointer are stored as well. */ -class MallocInfo { +struct MallocInfo { /** The pointer to the allocated piece of memory. */ - void * pointer; + void* pointer; /** The size of the allocated piece of memory. */ std::size_t size; - - /** The callstack where this allocation happened. */ - mutable lcs::callstack createdCallstack; - /** Indicating whether this allocation has been deallocated. */ - bool deleted; + bool deleted = false; + /** The callstack where this allocation happened. */ + mutable lcs::callstack createdCallstack; /** The callstack where the deallocation happened. */ mutable std::optional deletedCallstack; - -public: + /** * Initializes this allocation record using the given information. * * @param pointer the pointer to the allocated piece of memory * @param size the size of the allocated piece of memory */ - inline MallocInfo(void * const pointer, - const std::size_t size) - : pointer(pointer), - size(size), - createdCallstack(), - deleted(false), - deletedCallstack(std::nullopt) - {} - - /** - * Returns the pointer to the allocated piece of memory. - * - * @return the pointer to the allocated memory - */ - constexpr inline auto getPointer() const -> const void * { - return pointer; - } - /** - * Returns the size of the allocated piece of memory. - * - * @return the size of the allocated memory block - */ - constexpr inline auto getSize() const -> std::size_t { - return size; - } - - /** - * Returns whether this allocation has been deallocated. - * - * @return whether this allocation is marked as deallocated - */ - constexpr inline auto isDeleted() const -> bool { - return deleted; - } - /** - * Sets whether this alloction has been deallocated. - * - * @param deleted whether this allocation is deallocated - */ - inline void setDeleted(bool deleted) { - this->deleted = deleted; - + inline MallocInfo(void* const pointer, const std::size_t size): pointer(pointer), size(size) {} + + inline void markDeleted() { + deleted = true; deletedCallstack = lcs::callstack(); } @@ -110,7 +68,7 @@ class MallocInfo { * * @param out the output stream to print to */ - inline void printCreatedCallstack(std::ostream & out) const { + inline void printCreatedCallstack(std::ostream& out) const { callstackHelper::format(createdCallstack, out); } /** @@ -118,7 +76,7 @@ class MallocInfo { * * @param out the output stream to print to */ - inline void printDeletedCallstack(std::ostream & out) const { + inline void printDeletedCallstack(std::ostream& out) const { if (!deletedCallstack.has_value()) { throw std::runtime_error("MallocInfo: No deleted callstack! " "Hint: Check using MallocInfo::getDeletedCallstack()::has_value()."); @@ -127,41 +85,7 @@ class MallocInfo { callstackHelper::format(deletedCallstack.value(), out); } - /** - * Returns a reference to the callstack where this allocation was deallocated. - * - * @return the callstack where the deallocation happened - */ - constexpr inline auto getDeletedCallstack() const -> const std::optional & { - return deletedCallstack; - } - /** - * Returns the optionally callstack where the represented allocation - * has been deallocated. - * - * @return the deleted callstack - */ - inline auto getDeletedCallstack() -> std::optional & { - return deletedCallstack; - } - /** - * Returns a reference to the callstack where this allocation was allocated. - * - * @return the callstack where the allocation happened - */ - constexpr inline auto getCreatedCallstack() const -> const lcs::callstack & { - return createdCallstack; - } - /** - * Returns a reference to the callstack where the represented allocation was allocated. - * - * @return the callstack where the allocation happened - */ - inline auto getCreatedCallstack() -> lcs::callstack & { - return createdCallstack; - } - - friend auto operator<<(std::ostream &, const MallocInfo &) -> std::ostream &; + friend auto operator<<(std::ostream&, const MallocInfo&) -> std::ostream&; }; } diff --git a/src/crashWarner/crashWarner.cpp b/src/crashWarner/crashWarner.cpp index 12039b4..2042d9d 100644 --- a/src/crashWarner/crashWarner.cpp +++ b/src/crashWarner/crashWarner.cpp @@ -93,7 +93,7 @@ static inline void printer(const std::string & std::cerr << formatter::format("Previously allocated here:") << std::endl; record.printCreatedCallstack(std::cerr); std::cerr << std::endl; - if (record.getDeletedCallstack().has_value()) { + if (record.deletedCallstack.has_value()) { std::cerr << std::endl << formatter::format("Previously freed here:") << std::endl; record.printDeletedCallstack(std::cerr); std::cerr << std::endl; diff --git a/src/statistics/Stats.hpp b/src/statistics/Stats.hpp index f47206e..a2f326d 100644 --- a/src/statistics/Stats.hpp +++ b/src/statistics/Stats.hpp @@ -1,20 +1,22 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2023 mhahnFr + * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef Stats_hpp @@ -118,8 +120,8 @@ class Stats { * * @param info the allocation record to append */ - inline void addMalloc(const MallocInfo & info) { - addMalloc(info.getSize()); + inline void addMalloc(const MallocInfo& info) { + addMalloc(info.size); } /** @@ -143,8 +145,8 @@ class Stats { * * @param info the allocation record that should be removed from the statistics */ - inline void addFree(const MallocInfo & info) { - addFree(info.getSize()); + inline void addFree(const MallocInfo& info) { + addFree(info.size); } /** diff --git a/src/statistics/lsan_stats.cpp b/src/statistics/lsan_stats.cpp index e5318d8..f332142 100644 --- a/src/statistics/lsan_stats.cpp +++ b/src/statistics/lsan_stats.cpp @@ -3,18 +3,20 @@ * * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include @@ -125,8 +127,8 @@ static inline void __lsan_printFragmentationObjectBar(std::size_t width, std::os loss = fmodf(step, static_cast(step)); float tmpLoss = 0.0f; for (; it != infos.cend(); ++it) { - const std::string fill = it->second.isDeleted() ? formatter::get() - : formatter::get(); + const std::string& fill = it->second.deleted ? formatter::get() + : formatter::get(); tmpLoss += loss; if (tmpLoss >= 1.0f) { out << fill; @@ -156,7 +158,7 @@ static inline void __lsan_printFragmentationObjectBar(std::size_t width, std::os } std::size_t fs = 0; for (; it != e; ++it) { - if (it->second.isDeleted()) { + if (it->second.deleted) { ++fs; } } @@ -208,12 +210,12 @@ static inline void __lsan_printFragmentationByteBar(std::size_t width, std::ostr const auto & infos = getInstance().getFragmentationInfos(); auto it = infos.cbegin(); std::size_t currentBlockBegin = 0, - currentBlockEnd = it->second.getSize(), + currentBlockEnd = it->second.size, b = 0; std::size_t total = 0; for (const auto & [_, info] : infos) { - total += info.getSize(); + total += info.size; } if (total < width) { @@ -222,10 +224,10 @@ static inline void __lsan_printFragmentationByteBar(std::size_t width, std::ostr if (b >= currentBlockEnd) { ++it; currentBlockBegin = b; - currentBlockEnd = currentBlockBegin + it->second.getSize(); + currentBlockEnd = currentBlockBegin + it->second.size; } - const std::string fill = it->second.isDeleted() ? formatter::get() - : formatter::get(); + const std::string& fill = it->second.deleted ? formatter::get() + : formatter::get(); for (std::size_t i = 0; i < step; ++i) { out << fill; } @@ -253,9 +255,9 @@ static inline void __lsan_printFragmentationByteBar(std::size_t width, std::ostr if (b >= currentBlockEnd) { ++it; currentBlockBegin = b; - currentBlockEnd = currentBlockBegin + it->second.getSize(); + currentBlockEnd = currentBlockBegin + it->second.size; } - if (it->second.isDeleted()) { + if (it->second.deleted) { ++fs; } } From e404624f97f09a16c78df002cecaa804433ea442 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:48:30 +0200 Subject: [PATCH 12/36] Added a convenience using clause --- src/LeakSani.cpp | 8 ++++---- src/LeakSani.hpp | 23 +++++++++++------------ src/MallocInfo.hpp | 4 ++++ src/crashWarner/crash.hpp | 3 +-- src/crashWarner/crashWarner.cpp | 10 +++++----- src/crashWarner/warn.hpp | 3 +-- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/LeakSani.cpp b/src/LeakSani.cpp index 6f708c9..b91fca5 100644 --- a/src/LeakSani.cpp +++ b/src/LeakSani.cpp @@ -94,14 +94,14 @@ LSan::LSan(): libName(lsanName().value()) { #endif } -auto LSan::removeMalloc(void* pointer) -> MallocInfoRemoved { +auto LSan::removeMalloc(void* pointer) -> std::pair> { std::lock_guard lock(infoMutex); auto it = infos.find(pointer); if (it == infos.end()) { - return MallocInfoRemoved(false, std::nullopt); + return std::make_pair(false, std::nullopt); } else if (it->second.deleted) { - return MallocInfoRemoved(false, it->second); + return std::make_pair(false, it->second); } if (__lsan_statsActive) { stats -= it->second; @@ -109,7 +109,7 @@ auto LSan::removeMalloc(void* pointer) -> MallocInfoRemoved { } else { infos.erase(it); } - return MallocInfoRemoved(true, std::nullopt); + return std::make_pair(true, std::nullopt); } auto LSan::changeMalloc(const MallocInfo & info) -> bool { diff --git a/src/LeakSani.hpp b/src/LeakSani.hpp index 7dcebef..0e52387 100644 --- a/src/LeakSani.hpp +++ b/src/LeakSani.hpp @@ -3,18 +3,20 @@ * * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef LeakSani_hpp @@ -43,9 +45,6 @@ namespace lsan { * This class manages everything this sanitizer is capable to do. */ class LSan { - /** A pair consisting of a boolean and an optional allocation record. */ - using MallocInfoRemoved = std::pair>>; - /** A map containing all allocation records, sorted by their allocated pointers. */ std::map infos; /** An object holding all statistics. */ @@ -156,8 +155,8 @@ class LSan { * @param pointer the allocation pointer * @return a pair with a boolean indicating the success and optionally the already deleted allocation record */ - auto removeMalloc(void* pointer) -> MallocInfoRemoved; - + auto removeMalloc(void* pointer) -> std::pair>; + /** * Adds the given allocation record. * diff --git a/src/MallocInfo.hpp b/src/MallocInfo.hpp index d3ef933..5c66e66 100644 --- a/src/MallocInfo.hpp +++ b/src/MallocInfo.hpp @@ -22,6 +22,7 @@ #ifndef MallocInfo_hpp #define MallocInfo_hpp +#include #include #include #include @@ -39,6 +40,9 @@ namespace lsan { * pointer are stored as well. */ struct MallocInfo { + using Ref = std::reference_wrapper; + using CRef = std::reference_wrapper; + /** The pointer to the allocated piece of memory. */ void* pointer; /** The size of the allocated piece of memory. */ diff --git a/src/crashWarner/crash.hpp b/src/crashWarner/crash.hpp index f3830e5..10167a4 100644 --- a/src/crashWarner/crash.hpp +++ b/src/crashWarner/crash.hpp @@ -22,7 +22,6 @@ #ifndef crash_hpp #define crash_hpp -#include #include #include @@ -71,7 +70,7 @@ void crash(const std::string & message); * @param info the optional allocation record */ void crash(const std::string& message, - const std::optional>& info); + const std::optional& info); /** * This function resets the signal handler for `SIGABRT` and performs the abort. diff --git a/src/crashWarner/crashWarner.cpp b/src/crashWarner/crashWarner.cpp index 2042d9d..772a32a 100644 --- a/src/crashWarner/crashWarner.cpp +++ b/src/crashWarner/crashWarner.cpp @@ -79,9 +79,9 @@ static inline void printer(const std::string & message, lcs::callstack && callst * @tparam Warning whether to use warning formatting */ template -static inline void printer(const std::string & message, - std::optional> info, - lcs::callstack & callstack) { +static inline void printer(const std::string& message, + const std::optional& info, + lcs::callstack& callstack) { using formatter::Style; printer(message, callstack); @@ -123,7 +123,7 @@ void warn(const std::string & message) { } void warn(const std::string& message, - const std::optional>& info) { + const std::optional& info) { withCallstack([&] (auto& callstack) { printer(message, info, callstack); }); @@ -149,7 +149,7 @@ void crashForce(const std::string& message, const std::optional& re } void crash(const std::string& message, - const std::optional>& info) { + const std::optional& info) { withCallstack([&] (auto& callstack) { printer(message, info, callstack); abort(); diff --git a/src/crashWarner/warn.hpp b/src/crashWarner/warn.hpp index 0cf44da..ee6aa5b 100644 --- a/src/crashWarner/warn.hpp +++ b/src/crashWarner/warn.hpp @@ -22,7 +22,6 @@ #ifndef warn_hpp #define warn_hpp -#include #include #include @@ -48,7 +47,7 @@ void warn(const std::string& message); * @param info the optional allocation record */ void warn(const std::string & message, - const std::optional>& info); + const std::optional& info); } #endif /* warn_hpp */ From 9479ee074209619720bcb065cfd65061e5664c1b Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:55:07 +0200 Subject: [PATCH 13/36] Removed a sentence when printing a leak --- src/LeakSani.cpp | 2 +- src/MallocInfo.cpp | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/LeakSani.cpp b/src/LeakSani.cpp index b91fca5..4e8a8f5 100644 --- a/src/LeakSani.cpp +++ b/src/LeakSani.cpp @@ -250,7 +250,7 @@ std::ostream & operator<<(std::ostream & stream, LSan & self) { bytes += info.size; if (i < __lsan_leakCount) { if (isATTY()) { - stream << "\r"; + stream << "\r \r"; } stream << info << std::endl; ++i; diff --git a/src/MallocInfo.cpp b/src/MallocInfo.cpp index bf1ab36..f491590 100644 --- a/src/MallocInfo.cpp +++ b/src/MallocInfo.cpp @@ -28,14 +28,12 @@ #include "../include/lsan_internals.h" namespace lsan { -auto operator<<(std::ostream & stream, const MallocInfo & self) -> std::ostream & { +auto operator<<(std::ostream& stream, const MallocInfo& self) -> std::ostream& { using formatter::Style; stream << formatter::get << formatter::format("Leak") << " of size " - << formatter::clear - << bytesToString(self.size) << formatter::get << ", allocation stacktrace:" - << std::endl; + << formatter::clear << bytesToString(self.size) << std::endl; self.printCreatedCallstack(stream); return stream; } From 54c3a15640976fae1a3e1b4308f4d0837c569904 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:32:41 +0200 Subject: [PATCH 14/36] Added dSYM bundles to the gitignore --- .gitignore | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 4359c8e..5d9c7fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,21 +1,21 @@ -# # LeakSanitizer - Small library showing information about lost memory. # # Copyright (C) 2022, 2024 mhahnFr # -# This file is part of the LeakSanitizer. This library is free software: -# you can redistribute it and/or modify it under the terms of the -# GNU General Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version. +# This file is part of the LeakSanitizer. +# +# The LeakSanitizer is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. # -# This library is distributed in the hope that it will be useful, +# The LeakSanitizer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# this library, see the file LICENSE. If not, see . -# +# You should have received a copy of the GNU General Public License along with the +# LeakSanitizer, see the file LICENSE. If not, see . # Prerequisites *.d @@ -67,8 +67,9 @@ dkms.conf # Xcode project user data xcuserdata/ -# macOS spcific +# macOS specific .DS_Store +*.dSYM/ *~ .* From ce0f1b2e7a476af2a58e142d8c3df2cc4d404c5f Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Fri, 3 May 2024 17:39:47 +0200 Subject: [PATCH 15/36] Fixed omitting LSan frames if some frames are already printed --- src/callstacks/callstackHelper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/callstacks/callstackHelper.cpp b/src/callstacks/callstackHelper.cpp index 59576d3..171edde 100644 --- a/src/callstacks/callstackHelper.cpp +++ b/src/callstacks/callstackHelper.cpp @@ -187,7 +187,7 @@ void format(lcs::callstack & callstack, std::ostream & stream) { for (i = printed = 0; i < size && printed < __lsan_callstackSize; ++i) { const auto binaryFile = frames[i].binaryFile; - if (binaryFile == nullptr || isInLSan(binaryFile)) { + if (binaryFile == nullptr || (firstPrint && isInLSan(binaryFile))) { continue; } else if (firstHit && isFirstParty(binaryFile)) { stream << formatter::get From 1b953eba9be7183ea58ab44e5a47bb04c3298a6a Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Fri, 3 May 2024 18:06:08 +0200 Subject: [PATCH 16/36] Corrected copyright notice --- src/lsanMisc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lsanMisc.cpp b/src/lsanMisc.cpp index 63c1b27..7ccf54e 100644 --- a/src/lsanMisc.cpp +++ b/src/lsanMisc.cpp @@ -54,7 +54,7 @@ auto getInstance() -> LSan & { */ static inline auto printLicense(std::ostream & out) -> std::ostream & { out << "Copyright (C) 2022 - 2024 mhahnFr and contributors" << std::endl - << "Licensed under the terms of the GPL 3.0." << std::endl + << "Licensed under the terms of the GNU GPL 3.0." << std::endl << std::endl; return out; From 836259957e3676d89bc813071de4f25269b77a53 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 8 May 2024 10:12:28 +0200 Subject: [PATCH 17/36] Updated CallstackLibrary --- CallstackLibrary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CallstackLibrary b/CallstackLibrary index d9fc043..3f5ecf3 160000 --- a/CallstackLibrary +++ b/CallstackLibrary @@ -1 +1 @@ -Subproject commit d9fc043d92c6a7ab62d7699ca22910c0f74da8cc +Subproject commit 3f5ecf3c48ea5e2e346d25a4d5d717366c9abc27 From c11db6301f37d4b69bdde4c33922605126111739 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Sat, 11 May 2024 11:29:22 +0200 Subject: [PATCH 18/36] Updated CallstackLibrary --- CallstackLibrary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CallstackLibrary b/CallstackLibrary index 3f5ecf3..922dc21 160000 --- a/CallstackLibrary +++ b/CallstackLibrary @@ -1 +1 @@ -Subproject commit 3f5ecf3c48ea5e2e346d25a4d5d717366c9abc27 +Subproject commit 922dc2182e7c5827e7b2c3a628357fcf7fa6e23d From a234952582b3f6cde2662cadc96f283024a01510 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Sat, 11 May 2024 11:30:14 +0200 Subject: [PATCH 19/36] Added include paths --- LeakSanitizer.xcodeproj/project.pbxproj | 8 ++++++++ Makefile | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/LeakSanitizer.xcodeproj/project.pbxproj b/LeakSanitizer.xcodeproj/project.pbxproj index f62b8fd..64e1904 100644 --- a/LeakSanitizer.xcodeproj/project.pbxproj +++ b/LeakSanitizer.xcodeproj/project.pbxproj @@ -338,6 +338,10 @@ GCC_ENABLE_CPP_EXCEPTIONS = YES; GCC_ENABLE_CPP_RTTI = YES; GCC_SYMBOLS_PRIVATE_EXTERN = YES; + HEADER_SEARCH_PATHS = ( + CallstackLibrary/include, + include, + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/CallstackLibrary", @@ -363,6 +367,10 @@ GCC_ENABLE_CPP_RTTI = YES; GCC_OPTIMIZATION_LEVEL = fast; GCC_SYMBOLS_PRIVATE_EXTERN = YES; + HEADER_SEARCH_PATHS = ( + CallstackLibrary/include, + include, + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/CallstackLibrary", diff --git a/Makefile b/Makefile index 301d4e9..56d3b8a 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ DEPS = $(patsubst %.cpp, %.d, $(SRC)) BENCHMARK = false LDFLAGS = -ldl -L$(LIBCALLSTACK_DIR) -lcallstack -CXXFLAGS = -std=c++17 -Wall -Wextra -pedantic -fPIC -Ofast +CXXFLAGS = -std=c++17 -Wall -Wextra -pedantic -fPIC -Ofast -I 'include' -I CallstackLibrary/include ifeq ($(BENCHMARK),true) CXXFLAGS += -DBENCHMARK From 3c29634dc89da86d4dd396ec2e3ecb9aee5e9de5 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Sat, 11 May 2024 11:31:27 +0200 Subject: [PATCH 20/36] Adapted the main source files to the new include paths --- src/LeakSani.cpp | 12 ++++++------ src/LeakSani.hpp | 4 ++-- src/MallocInfo.cpp | 4 ++-- src/MallocInfo.hpp | 4 ++-- src/bytePrinter.cpp | 22 +++++++++++++--------- src/formatter.hpp | 22 ++++++++++++---------- src/lsanMisc.cpp | 10 +++++----- src/lsan_internals.cpp | 18 ++++++++++-------- 8 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/LeakSani.cpp b/src/LeakSani.cpp index 4e8a8f5..aa2743a 100644 --- a/src/LeakSani.cpp +++ b/src/LeakSani.cpp @@ -1,7 +1,7 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2024 mhahnFr and contributors + * Copyright (C) 2022 - 2024 mhahnFr * * This file is part of the LeakSanitizer. * @@ -23,6 +23,11 @@ #include +#include +#include + +#include + #include "LeakSani.hpp" #include "bytePrinter.hpp" @@ -32,11 +37,6 @@ #include "signals/signals.hpp" #include "signals/signalHandlers.hpp" -#include "../include/lsan_internals.h" -#include "../include/lsan_stats.h" - -#include "../CallstackLibrary/include/callstack_internals.h" - namespace lsan { /** * Returns an optional containing the runtime name of this library. diff --git a/src/LeakSani.hpp b/src/LeakSani.hpp index 0e52387..9d2e80a 100644 --- a/src/LeakSani.hpp +++ b/src/LeakSani.hpp @@ -29,6 +29,8 @@ #include #include +#include + #include "MallocInfo.hpp" #ifdef BENCHMARK @@ -38,8 +40,6 @@ #include "initialization/init.hpp" #include "statistics/Stats.hpp" -#include "../include/lsan_internals.h" - namespace lsan { /** * This class manages everything this sanitizer is capable to do. diff --git a/src/MallocInfo.cpp b/src/MallocInfo.cpp index f491590..eca6c88 100644 --- a/src/MallocInfo.cpp +++ b/src/MallocInfo.cpp @@ -19,14 +19,14 @@ * LeakSanitizer, see the file LICENSE. If not, see . */ +#include + #include "MallocInfo.hpp" #include "LeakSani.hpp" #include "formatter.hpp" #include "bytePrinter.hpp" -#include "../include/lsan_internals.h" - namespace lsan { auto operator<<(std::ostream& stream, const MallocInfo& self) -> std::ostream& { using formatter::Style; diff --git a/src/MallocInfo.hpp b/src/MallocInfo.hpp index 5c66e66..8737d71 100644 --- a/src/MallocInfo.hpp +++ b/src/MallocInfo.hpp @@ -27,9 +27,9 @@ #include #include -#include "callstacks/callstackHelper.hpp" +#include -#include "../CallstackLibrary/include/callstack.h" +#include "callstacks/callstackHelper.hpp" namespace lsan { /** diff --git a/src/bytePrinter.cpp b/src/bytePrinter.cpp index 69bd98d..d007d07 100644 --- a/src/bytePrinter.cpp +++ b/src/bytePrinter.cpp @@ -1,26 +1,30 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2023 mhahnFr + * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include #include + +#include + #include "bytePrinter.hpp" -#include "../include/lsan_internals.h" namespace lsan { /// Represents exactly 1 EiB. Needed for the calculations as starting point. diff --git a/src/formatter.hpp b/src/formatter.hpp index 7cb76aa..2f36cdb 100644 --- a/src/formatter.hpp +++ b/src/formatter.hpp @@ -1,20 +1,22 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2024 mhahnFr and contributors + * Copyright (C) 2022 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef formatter_hpp @@ -22,9 +24,9 @@ #include -#include "lsanMisc.hpp" +#include -#include "../include/lsan_internals.h" +#include "lsanMisc.hpp" /** * A namespace containing helper functions and classes for formatting diff --git a/src/lsanMisc.cpp b/src/lsanMisc.cpp index 7ccf54e..2fd6451 100644 --- a/src/lsanMisc.cpp +++ b/src/lsanMisc.cpp @@ -27,14 +27,14 @@ #define LSAN_HAS_UNISTD #endif +#include +#include + #include "lsanMisc.hpp" #include "formatter.hpp" #include "callstacks/callstackHelper.hpp" -#include "../include/lsan_internals.h" -#include "../CallstackLibrary/include/callstack.h" - namespace lsan { auto _getIgnoreMalloc() -> bool & { static bool ignore = false; @@ -53,8 +53,8 @@ auto getInstance() -> LSan & { * @return the given output stream */ static inline auto printLicense(std::ostream & out) -> std::ostream & { - out << "Copyright (C) 2022 - 2024 mhahnFr and contributors" << std::endl - << "Licensed under the terms of the GNU GPL 3.0." << std::endl + out << "Copyright (C) 2022 - 2024 mhahnFr and contributors" << std::endl + << "Licensed under the terms of the GNU GPL version 3 or later." << std::endl << std::endl; return out; diff --git a/src/lsan_internals.cpp b/src/lsan_internals.cpp index 28bf534..ab85831 100644 --- a/src/lsan_internals.cpp +++ b/src/lsan_internals.cpp @@ -3,18 +3,20 @@ * * Copyright (C) 2022 - 2024 mhahnFr and contributors * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #include @@ -22,7 +24,7 @@ #include #include -#include "../include/lsan_internals.h" +#include /** * Retrieves the environment variable with the given name. From 309c42fcea40947c26f5fbdae67d7f74298df5a6 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Sat, 11 May 2024 11:39:34 +0200 Subject: [PATCH 21/36] Adapted to the new include paths --- src/allocations/wrap_malloc.cpp | 7 ++++--- src/callstacks/callstackHelper.cpp | 4 ++-- src/callstacks/callstackHelper.hpp | 20 +++++++++++--------- src/crashWarner/exceptionHandler.cpp | 6 +++--- src/signals/signalHandlers.cpp | 9 ++++----- src/statistics/lsan_stats.cpp | 6 +++--- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/allocations/wrap_malloc.cpp b/src/allocations/wrap_malloc.cpp index d6e9ab5..7636cfc 100644 --- a/src/allocations/wrap_malloc.cpp +++ b/src/allocations/wrap_malloc.cpp @@ -1,7 +1,7 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2022 - 2024 mhahnFr and contributors + * Copyright (C) 2022 - 2024 mhahnFr * * This file is part of the LeakSanitizer. * @@ -21,10 +21,13 @@ #include +#include + #include "wrap_malloc.hpp" #include "interpose.hpp" #include "realAlloc.hpp" + #include "../LeakSani.hpp" #include "../lsanMisc.hpp" #include "../timing.hpp" @@ -32,8 +35,6 @@ #include "../crashWarner/warn.hpp" #include "../initialization/init.hpp" -#include "../../include/lsan_internals.h" - #ifdef __linux__ auto operator new(std::size_t size) -> void * { if (size == 0) { diff --git a/src/callstacks/callstackHelper.cpp b/src/callstacks/callstackHelper.cpp index 171edde..7a97e27 100644 --- a/src/callstacks/callstackHelper.cpp +++ b/src/callstacks/callstackHelper.cpp @@ -22,14 +22,14 @@ #include #include +#include + #include "callstackHelper.hpp" #include "../formatter.hpp" #include "../lsanMisc.hpp" #include "../LeakSani.hpp" -#include "../../include/lsan_internals.h" - namespace lsan::callstackHelper { /** * Returns whether the given binary file name is this library. diff --git a/src/callstacks/callstackHelper.hpp b/src/callstacks/callstackHelper.hpp index 30a821f..7d9fba7 100644 --- a/src/callstacks/callstackHelper.hpp +++ b/src/callstacks/callstackHelper.hpp @@ -1,20 +1,22 @@ /* * LeakSanitizer - Small library showing information about lost memory. * - * Copyright (C) 2023 mhahnFr + * Copyright (C) 2023 - 2024 mhahnFr * - * This file is part of the LeakSanitizer. This library is free software: - * you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. + * This file is part of the LeakSanitizer. * - * This library is distributed in the hope that it will be useful, + * The LeakSanitizer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The LeakSanitizer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this library, see the file LICENSE. If not, see . + * You should have received a copy of the GNU General Public License along with the + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef callstackHelper_hpp @@ -23,7 +25,7 @@ #include #include -#include "../../CallstackLibrary/include/callstack.h" +#include /** This namespace includes the helper functions for the callstacks. */ namespace lsan::callstackHelper { diff --git a/src/crashWarner/exceptionHandler.cpp b/src/crashWarner/exceptionHandler.cpp index be65af6..73aa1d9 100644 --- a/src/crashWarner/exceptionHandler.cpp +++ b/src/crashWarner/exceptionHandler.cpp @@ -24,12 +24,12 @@ #include #include -#include "exceptionHandler.hpp" +#include #include "crash.hpp" -#include "../lsanMisc.hpp" +#include "exceptionHandler.hpp" -#include "../../CallstackLibrary/include/callstack_exception.hpp" +#include "../lsanMisc.hpp" namespace lsan { /** diff --git a/src/signals/signalHandlers.cpp b/src/signals/signalHandlers.cpp index c61e608..fef8736 100644 --- a/src/signals/signalHandlers.cpp +++ b/src/signals/signalHandlers.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #ifdef __APPLE__ @@ -30,6 +31,9 @@ #undef _XOPEN_SOURCE #endif /* __APPLE__ */ +#include +#include + #include "signals.hpp" #include "signalHandlers.hpp" @@ -39,11 +43,6 @@ #include "../callstacks/callstackHelper.hpp" #include "../crashWarner/crash.hpp" -#include "../../include/lsan_internals.h" -#include "../../include/lsan_stats.h" - -#include - namespace lsan::signals::handlers { /** * Stringifies the given pointer. diff --git a/src/statistics/lsan_stats.cpp b/src/statistics/lsan_stats.cpp index f332142..791b6e1 100644 --- a/src/statistics/lsan_stats.cpp +++ b/src/statistics/lsan_stats.cpp @@ -23,14 +23,14 @@ #include #include +#include +#include + #include "../formatter.hpp" #include "../bytePrinter.hpp" #include "../lsanMisc.hpp" #include "../LeakSani.hpp" -#include "../../include/lsan_internals.h" -#include "../../include/lsan_stats.h" - using namespace lsan; auto __lsan_getTotalMallocs() -> std::size_t { return getStats().getTotalMallocCount(); } From 22e0e8b4d5c658b257a8084b197d66331a2ccb1d Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Sat, 11 May 2024 11:39:45 +0200 Subject: [PATCH 22/36] Fixed license notice --- src/crashWarner/warn.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crashWarner/warn.hpp b/src/crashWarner/warn.hpp index ee6aa5b..467562d 100644 --- a/src/crashWarner/warn.hpp +++ b/src/crashWarner/warn.hpp @@ -16,7 +16,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with the - * LeakSanitzer, see the file LICENSE. If not, see . + * LeakSanitizer, see the file LICENSE. If not, see . */ #ifndef warn_hpp From e9e87743e17ec40bd3b6bea71a23259878bef4a0 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 15 May 2024 17:20:03 +0200 Subject: [PATCH 23/36] Fixed compile error on Linux --- src/lsan_internals.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lsan_internals.cpp b/src/lsan_internals.cpp index ab85831..49f4971 100644 --- a/src/lsan_internals.cpp +++ b/src/lsan_internals.cpp @@ -20,6 +20,7 @@ */ #include +#include #include #include #include From a6448a00e0bcf9dd408d56dd27a205cf10331769 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Thu, 23 May 2024 12:28:40 +0200 Subject: [PATCH 24/36] Updated CallstackLibrary --- CallstackLibrary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CallstackLibrary b/CallstackLibrary index 922dc21..691e6c8 160000 --- a/CallstackLibrary +++ b/CallstackLibrary @@ -1 +1 @@ -Subproject commit 922dc2182e7c5827e7b2c3a628357fcf7fa6e23d +Subproject commit 691e6c817f1e6f9aa26fdeafcfab8188ef654150 From 450edace5bc22c60f124a284352ce9d77dd306a5 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Sat, 25 May 2024 17:39:50 +0200 Subject: [PATCH 25/36] Updated CallstackLibrary --- CallstackLibrary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CallstackLibrary b/CallstackLibrary index 691e6c8..1126514 160000 --- a/CallstackLibrary +++ b/CallstackLibrary @@ -1 +1 @@ -Subproject commit 691e6c817f1e6f9aa26fdeafcfab8188ef654150 +Subproject commit 1126514c664efd0e662f4b6f803146e8332c5534 From c8861242f6466fb7b8594677e7cec96b563467b0 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:12:22 +0200 Subject: [PATCH 26/36] Added missing documentation --- src/MallocInfo.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/MallocInfo.hpp b/src/MallocInfo.hpp index 8737d71..9ef9e51 100644 --- a/src/MallocInfo.hpp +++ b/src/MallocInfo.hpp @@ -40,7 +40,9 @@ namespace lsan { * pointer are stored as well. */ struct MallocInfo { + /** The preferred reference type of this class. */ using Ref = std::reference_wrapper; + /** The preferred constant reference type of this class. */ using CRef = std::reference_wrapper; /** The pointer to the allocated piece of memory. */ @@ -62,6 +64,11 @@ struct MallocInfo { */ inline MallocInfo(void* const pointer, const std::size_t size): pointer(pointer), size(size) {} + /** + * @brief Marks this allocation record as deleted. + * + * Creates a callstack of the point this function is called. + */ inline void markDeleted() { deleted = true; deletedCallstack = lcs::callstack(); From 3df962934d43df2cd278b9494514b407e0f2b0f2 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:13:41 +0200 Subject: [PATCH 27/36] Updated CallstackLibrary to version 1.2 --- CallstackLibrary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CallstackLibrary b/CallstackLibrary index 1126514..884c1b9 160000 --- a/CallstackLibrary +++ b/CallstackLibrary @@ -1 +1 @@ -Subproject commit 1126514c664efd0e662f4b6f803146e8332c5534 +Subproject commit 884c1b94681e006429d43cbe13e29a3e46329480 From d3341250e02eb75e783917ed13381eb179bc8119 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:52:54 +0200 Subject: [PATCH 28/36] Added optimizing keywords --- src/callstacks/callstackHelper.cpp | 4 ++-- src/crashWarner/crashWarner.cpp | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/callstacks/callstackHelper.cpp b/src/callstacks/callstackHelper.cpp index 7a97e27..8e46cb2 100644 --- a/src/callstacks/callstackHelper.cpp +++ b/src/callstacks/callstackHelper.cpp @@ -141,12 +141,12 @@ static inline auto getCallstackFrameSourceFile(const callstack_frame & frame) -> * @tparam S the style to be used */ template -static inline void formatShared(const struct callstack_frame & frame, std::ostream & out) { +static inline void formatShared(const callstack_frame& frame, std::ostream & out) { using formatter::Style; if (__lsan_printBinaries) { bool reset = false; - if (S == Style::GREYED || S == Style::BOLD) { + if constexpr (S == Style::GREYED || S == Style::BOLD) { reset = true; } out << formatter::format("(" + getCallstackFrameName(frame) + ")") << (reset ? formatter::get() : "") << " "; diff --git a/src/crashWarner/crashWarner.cpp b/src/crashWarner/crashWarner.cpp index 772a32a..4a8fc7e 100644 --- a/src/crashWarner/crashWarner.cpp +++ b/src/crashWarner/crashWarner.cpp @@ -42,8 +42,8 @@ template static inline void printer(const std::string& message, lcs::callstack& callstack, const std::optional& reason = std::nullopt) { using formatter::Style; - const auto colour = Warning ? Style::MAGENTA : Style::RED; - + constexpr const auto colour = Warning ? Style::MAGENTA : Style::RED; + std::cerr << formatter::format((Warning ? "Warning: " : "") + message + "!") << std::endl; if (reason.has_value()) { std::cerr << *reason << "." << std::endl; @@ -65,7 +65,7 @@ static inline void printer(const std::string& message, lcs::callstack& callstack * @tparam Warning whether to use warning formatting */ template -static inline void printer(const std::string & message, lcs::callstack && callstack) { +constexpr static inline void printer(const std::string & message, lcs::callstack && callstack) { printer(message, callstack); } @@ -79,16 +79,16 @@ static inline void printer(const std::string & message, lcs::callstack && callst * @tparam Warning whether to use warning formatting */ template -static inline void printer(const std::string& message, - const std::optional& info, - lcs::callstack& callstack) { +constexpr static inline void printer(const std::string& message, + const std::optional& info, + lcs::callstack& callstack) { using formatter::Style; printer(message, callstack); if (info.has_value()) { - const auto colour = Warning ? Style::MAGENTA : Style::RED; - const auto & record = info.value().get(); + constexpr const auto colour = Warning ? Style::MAGENTA : Style::RED; + const auto& record = info.value().get(); std::cerr << formatter::format("Previously allocated here:") << std::endl; record.printCreatedCallstack(std::cerr); From 3932e3b5c1a1d7d4c0181cd77c8596255f4e7890 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:53:22 +0200 Subject: [PATCH 29/36] Improved double free printing --- src/crashWarner/crashWarner.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/crashWarner/crashWarner.cpp b/src/crashWarner/crashWarner.cpp index 4a8fc7e..b83e155 100644 --- a/src/crashWarner/crashWarner.cpp +++ b/src/crashWarner/crashWarner.cpp @@ -38,8 +38,10 @@ namespace lsan { * @param reason the optional reason for the message * @tparam Warning whether to use warning formatting */ -template -static inline void printer(const std::string& message, lcs::callstack& callstack, const std::optional& reason = std::nullopt) { +template +constexpr static inline void printer(const std::string& message, + lcs::callstack& callstack, + const std::optional& reason = std::nullopt) { using formatter::Style; constexpr const auto colour = Warning ? Style::MAGENTA : Style::RED; @@ -51,7 +53,7 @@ static inline void printer(const std::string& message, lcs::callstack& callstack callstackHelper::format(callstack, std::cerr); std::cerr << std::endl; - if (!Warning) { + if constexpr (!Warning && SizeHint) { getInstance().maybeHintCallstackSize(std::cerr); std::cerr << maybeHintRelativePaths; } @@ -84,8 +86,8 @@ constexpr static inline void printer(const std::string& mess lcs::callstack& callstack) { using formatter::Style; - printer(message, callstack); - + printer(message, callstack); + if (info.has_value()) { constexpr const auto colour = Warning ? Style::MAGENTA : Style::RED; const auto& record = info.value().get(); @@ -94,11 +96,15 @@ constexpr static inline void printer(const std::string& mess record.printCreatedCallstack(std::cerr); std::cerr << std::endl; if (record.deletedCallstack.has_value()) { - std::cerr << std::endl << formatter::format("Previously freed here:") << std::endl; + std::cerr << formatter::format("Previously freed here:") << std::endl; record.printDeletedCallstack(std::cerr); std::cerr << std::endl; } } + if constexpr (!Warning) { + getInstance().maybeHintCallstackSize(std::cerr); + std::cerr << maybeHintRelativePaths; + } } /** From 3c883b386b1c40a387b775b06db60c3a31474807 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:53:45 +0200 Subject: [PATCH 30/36] Fixed an invalid reference --- src/LeakSani.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LeakSani.cpp b/src/LeakSani.cpp index aa2743a..0e30204 100644 --- a/src/LeakSani.cpp +++ b/src/LeakSani.cpp @@ -101,7 +101,7 @@ auto LSan::removeMalloc(void* pointer) -> std::pairsecond.deleted) { - return std::make_pair(false, it->second); + return std::make_pair(false, std::ref(it->second)); } if (__lsan_statsActive) { stats -= it->second; From 3378545e1f46025473f438569d64ce8c0571ad39 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:10:37 +0200 Subject: [PATCH 31/36] Added a missing line, deprecated two functions --- include/lsan_stats.h | 10 ++++++++-- src/crashWarner/crashWarner.cpp | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/lsan_stats.h b/include/lsan_stats.h index 6665946..816da6d 100644 --- a/include/lsan_stats.h +++ b/include/lsan_stats.h @@ -199,6 +199,9 @@ static inline void __lsan_printFStats() { } /** + * @deprecated Since version 1.9 replaced by `__lsan_printFStats()` and `__lsan_printFragmentationStats()`. + * Will be removed in version 2. + * * @brief Prints the statistics of the memory fragmentation. * * The bar has a size of 100 characters, it can be adjusted by using `__lsan_printFragStatsWithWidth(size_t)`. @@ -210,7 +213,7 @@ static inline void __lsan_printFStats() { * * @since 1.2 */ -DEPRECATED("Since v1.? refer to __lsan_printFStats() or __lsan_printFragmentationStats()") +DEPRECATED("Since v1.9 refer to __lsan_printFStats() or __lsan_printFragmentationStats()") static inline void __lsan_printFragStats() { __lsan_printFragmentationStats(); } @@ -232,6 +235,9 @@ static inline void __lsan_printFStatsWithWidth(size_t width) { } /** + * @deprecated Since version 1.9 replaced by `__lsan_printFStatsWithWidth(size_t)` and `__lsan_printFragmentationStatsWithWidth(size_t)`. + * Will be removed in version 2. + * * @brief Prints the statistics of the memory fragmentation. * * The size of the bar is specified by the given argument. The output stream defined by `__lsan_printCout` @@ -243,7 +249,7 @@ static inline void __lsan_printFStatsWithWidth(size_t width) { * @param width The width in characters the printed bar should have. * @since 1.2 */ -DEPRECATED("Since v1.? refer to __lsan_printFStatsWithWidth(size_t) or __lsan_printFragmentationStatsWithWidth(size_t)") +DEPRECATED("Since v1.9 refer to __lsan_printFStatsWithWidth(size_t) or __lsan_printFragmentationStatsWithWidth(size_t)") static inline void __lsan_printFragStatsWithWidth(size_t width) { __lsan_printFragmentationStatsWithWidth(width); } diff --git a/src/crashWarner/crashWarner.cpp b/src/crashWarner/crashWarner.cpp index b83e155..17254c7 100644 --- a/src/crashWarner/crashWarner.cpp +++ b/src/crashWarner/crashWarner.cpp @@ -37,6 +37,7 @@ namespace lsan { * @param callstack the callstack to be printed * @param reason the optional reason for the message * @tparam Warning whether to use warning formatting + * @tparam SizeHint whether to print the size hint if Warning is false */ template constexpr static inline void printer(const std::string& message, From 98e601225cc663e4497f4ca19313a9a10329addc Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:21:31 +0200 Subject: [PATCH 32/36] Increased the version number in the Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 56d3b8a..6bce482 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ endif LINUX_SONAME_FLAG = -Wl,-soname,$(abspath $@) ifeq ($(shell uname -s),Darwin) - LDFLAGS += -current_version 1.8 -compatibility_version 1 -install_name $(abspath $@) + LDFLAGS += -current_version 1.9 -compatibility_version 1 -install_name $(abspath $@) NAME = $(DYLIB_NA) else From 7716517205f166610d77260089f836648647eb40 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:22:21 +0200 Subject: [PATCH 33/36] Updating the tags as well in the update makefile rule --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 6bce482..6d3ac4c 100644 --- a/Makefile +++ b/Makefile @@ -93,6 +93,7 @@ release: update: $(MAKE) fclean + git fetch --tags git pull git submodule update git -C $(LIBCALLSTACK_DIR) submodule update From de9a78594f14ce5b98ea53979956a301c2416691 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:55:00 +0200 Subject: [PATCH 34/36] Consolidated Linux and macOS usage descriptions --- README.md | 65 +++++++++++++++++++------------------------------------ 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 6f6d7ce..5e1e62d 100644 --- a/README.md +++ b/README.md @@ -67,13 +67,11 @@ To use this sanitizer simply link your application against it (recommended) or p #### Linking (_recommended_) - Add `-L` if this sanitizer has not been installed in one of the default directories. -- On Linux add `-rdynamic` to the linking flags. Link with: `-llsan` > [!TIP] -> - Example **macOS**: `-L -llsan` -> - Example **Linux**: `-rdynamic -L -llsan` +> - Example: `-L -llsan` #### Preloading Add this sanitizer's library to the dynamic loader preload environment variable: @@ -108,66 +106,35 @@ int main(void) { free(a); } ``` -Compiled and linked on macOS with `cc main.c -L -llsan` creates the following output: +Compiled and linked on macOS with `cc main.c -g -L -llsan` creates the following output: ``` Exiting -Leak of size 13 B, allocation stacktrace: -At: (/usr/lib/system/libsystem_c.dylib) strdup + 32 -in: (a.out) main + 34 -at: (/usr/lib/dyld) start + 1903 - -Leak of size 1023 B, allocation stacktrace: -In: (a.out) main + 18 -at: (/usr/lib/dyld) start + 1903 - - -Summary: 2 leaks, 1.01 KiB lost. -``` - -#### Line numbers -##### macOS -Simply compile your code with debug symbols to add line numbers to the output. -> [!TIP] -> Usually, the appropriate flag is `-g`. - -The previous example compiled and linked on macOS with `cc -g main.c -L -llsan` creates the following output: - -``` -Exiting - -Leak of size 13 B, allocation stacktrace: +Leak of size 13 B At: (/usr/lib/system/libsystem_c.dylib) strdup + 32 in: (a.out) main (main.c:8:9) at: (/usr/lib/dyld) start + 1903 -Leak of size 1023 B, allocation stacktrace: +Leak of size 1023 B In: (a.out) main (main.c:7:16) at: (/usr/lib/dyld) start + 1903 - + Summary: 2 leaks, 1.01 KiB lost. ``` - -##### Linux -To (partially) add line numbers, you can add `-Wno-gnu-include-next -I/include` to the compiling flags -of your code. - -The previous example compiled and linked on Debian with -`gcc main.c -Wno-gnu-include-next -I/include -rdynamic -L -llsan` creates the following output: - +Compiled and linked on Debian with `gcc main.c -g -L -llsan` creates the following output: ``` Exiting -Leak of size 1023 B, allocated at main.c:7 -In: (a.out) main + 33 +Leak of size 1023 B +In: (a.out) main (main.c:7:16) at: (/usr/lib/x86_64-linux-gnu/libc.so.6) << Unknown >> at: (/usr/lib/x86_64-linux-gnu/libc.so.6) __libc_start_main + 133 at: (a.out) _start + 33 -Leak of size 13 B, allocation stacktrace: +Leak of size 13 B At: (/usr/lib/x86_64-linux-gnu/libc.so.6) __strdup + 26 -in: (a.out) main + 52 +in: (a.out) main (main.c:8:9) at: (/usr/lib/x86_64-linux-gnu/libc.so.6) << Unknown >> at: (/usr/lib/x86_64-linux-gnu/libc.so.6) __libc_start_main + 133 at: (a.out) _start + 33 @@ -176,6 +143,18 @@ at: (a.out) _start + 33 Summary: 2 leaks, 1.01 KiB lost. ``` +#### Line numbers +Simply compile your code with debug symbols to add line numbers to the output. +> [!TIP] +> Usually, the appropriate flag is `-g`. + +Currently, debug symbols in the following formats are supported: +- DWARF in ELF binary files +- DWARF in Mach-O debug maps (using Mach-O object files) +- `.dSYM` Mach-O bundles + +The DWARF parser supports DWARF in version **2**, **3**, **4** and **5**. + ### Behaviour Since version 1.6 the behaviour of this sanitizer can be adjusted by setting the following environment variables to their indicated values: From d1db45edc07332cbe1b300f35065cb93b83f30a9 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:57:33 +0200 Subject: [PATCH 35/36] Corrected license version notice in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e1e62d..56b0a79 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ the detected memory leaks are printed. The backtraces are translated using the [CallstackLibrary][5]. ## Final notes -This project is licensed under the terms of the GPL 3.0. +This project is licensed under the terms of the GNU GPL in version 3 or later. © Copyright 2022 - 2024 [mhahnFr][6] and contributors From 2b0a98e6b550609223b7f94073770b57f9545746 Mon Sep 17 00:00:00 2001 From: mhahnFr <83553794+mhahnFr@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:15:09 +0200 Subject: [PATCH 36/36] Added some publishing notes --- publishNotices/README.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 publishNotices/README.txt diff --git a/publishNotices/README.txt b/publishNotices/README.txt new file mode 100644 index 0000000..156bc34 --- /dev/null +++ b/publishNotices/README.txt @@ -0,0 +1,11 @@ +This distribution of the LeakSanitizer: Copyright (C) 2024 mhahnFr + +This distribution of the LeakSanitizer is licensed under the terms of the +GNU GPL version 3 or later. +See the file LICENSE in the root directory. + +This distribution includes the CallstackLibrary, Copyright (C) 2024 mhahnFr, +which is licensed under the terms of the GNU GPL version 3 or later. The +source code can be found at: +https://github.com/mhahnFr/CallstackLibrary/releases/tag/v1.2 +See the file LICENSE in the root directory.