From d360d03403bd378a3a747289f145eb72317d9bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Sun, 2 May 2021 18:08:03 +0200 Subject: [PATCH] Enable address sanitizer in develop builds (#834) Co-authored-by: ISSOtm --- CMakeLists.txt | 2 +- Makefile | 2 +- src/asm/main.c | 12 ++++++++++++ src/link/main.c | 12 ++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bfbdb0bf..fbf0a735 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ else() -fsanitize=unreachable -fsanitize=vla-bound -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=object-size -fsanitize=bool -fsanitize=enum - -fsanitize=alignment -fsanitize=null) + -fsanitize=alignment -fsanitize=null -fsanitize=address) add_compile_options(${SAN_FLAGS}) link_libraries(${SAN_FLAGS}) endif() diff --git a/Makefile b/Makefile index 23d56703..73792a90 100644 --- a/Makefile +++ b/Makefile @@ -216,7 +216,7 @@ develop: -fsanitize=unreachable -fsanitize=vla-bound \ -fsanitize=signed-integer-overflow -fsanitize=bounds \ -fsanitize=object-size -fsanitize=bool -fsanitize=enum \ - -fsanitize=alignment -fsanitize=null" CFLAGS="-ggdb3 -O0" + -fsanitize=alignment -fsanitize=null -fsanitize=address" CFLAGS="-ggdb3 -O0" # Targets for the project maintainer to easily create Windows exes. # This is not for Windows users! diff --git a/src/asm/main.c b/src/asm/main.c index 57339b2f..b241a6de 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -36,6 +36,18 @@ #include "helpers.h" #include "version.h" +#ifdef __clang__ +#if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__) +#define __SANITIZE_ADDRESS__ +#endif /* __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__) */ +#endif /* __clang__ */ + +#ifdef __SANITIZE_ADDRESS__ +// There are known, non-trivial to fix leaks. We would still like to have `make develop' +// detect memory corruption, though. +const char *__asan_default_options(void) { return "detect_leaks=0"; } +#endif + // Old Bison versions (confirmed for 2.3) do not forward-declare `yyparse` in the generated header // Unfortunately, macOS still ships 2.3, which is from 2008... int yyparse(void); diff --git a/src/link/main.c b/src/link/main.c index fc20b343..41e1051c 100644 --- a/src/link/main.c +++ b/src/link/main.c @@ -28,6 +28,18 @@ #include "extern/getopt.h" #include "version.h" +#ifdef __clang__ +#if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__) +#define __SANITIZE_ADDRESS__ +#endif /* __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__) */ +#endif /* __clang__ */ + +#ifdef __SANITIZE_ADDRESS__ +// There are known, non-trivial to fix leaks. We would still like to have `make develop' +// detect memory corruption, though. +const char *__asan_default_options(void) { return "detect_leaks=0"; } +#endif + bool isDmgMode; /* -d */ char *linkerScriptName; /* -l */ char const *mapFileName; /* -m */