From d783afbda1a52dcbea2d4bacfc28b3e42c2e23aa Mon Sep 17 00:00:00 2001 From: nhmall Date: Wed, 29 Mar 2023 21:39:44 -0400 Subject: [PATCH] support WANT_ASAN=1 on make command line in linux.370 and macOS.370 When the address sanitizer is in use for gcc, clang, or visual studio, (-fsanitize=address) define the NetHack preprocessor symbol USING_ADDRESS_SANITIZER. --- include/global.h | 15 +++++++++++++++ sys/unix/hints/include/compiler.370 | 7 +++++++ sys/unix/hints/linux.370 | 8 ++++++-- sys/unix/hints/macOS.370 | 4 ++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/global.h b/include/global.h index bb49feb77..c734ccaf7 100644 --- a/include/global.h +++ b/include/global.h @@ -146,6 +146,21 @@ typedef uchar nhsym; #include "coord.h" +/* define USING_ADDRESS_SANITIZER if ASAN is in use */ +#if defined(__clang__) +#if defined(__has_feature) +#if __has_feature(address_sanitizer) +#define USING_ADDRESS_SANITIZER +#endif /* __has_feature */ +#endif /* __has_feature(address_sanitizer) */ +#else /* ?__clang__ */ +#if defined(__GNUC__) || defined(_MSC_VER) +#ifdef __SANITIZE_ADDRESS__ +#define USING_ADDRESS_SANITIZER +#endif /* __SANITIZE_ADDRESS__ */ +#endif /* __GNUC__ || _MSC_VER */ +#endif /* !__clang__ */ + /* * Automatic inclusions for the subsidiary files. * Please don't change the order. It does matter. diff --git a/sys/unix/hints/include/compiler.370 b/sys/unix/hints/include/compiler.370 index fe4b3ed0d..7aea39762 100755 --- a/sys/unix/hints/include/compiler.370 +++ b/sys/unix/hints/include/compiler.370 @@ -29,6 +29,9 @@ CCFLAGS = -g # CPLUSPLUS_NEED_DEPSUPPRESS = 1 C++ -Wno-deprecated-copy, # -Wno-deprecated-declarations +#ifeq "$(WANT_ASAN)" "1" +NEED_ASAN=1 +#endif # If you want to override the compiler detection just carried out # uncomment one of the following pairs. Note, however, that @@ -60,6 +63,10 @@ CFLAGS+=-Wmissing-prototypes CFLAGS+=-Wold-style-definition CFLAGS+=-Wstrict-prototypes +#ifeq "$(NEED_ASAN)" "1" +CFLAGS+=-fsanitize=address +#endif + #detection of clang vs gcc CCISCLANG := $(shell echo `$(CC) --version` | grep clang) ifeq "$(CCISCLANG)" "" diff --git a/sys/unix/hints/linux.370 b/sys/unix/hints/linux.370 index 39363b7ed..c6393ad32 100755 --- a/sys/unix/hints/linux.370 +++ b/sys/unix/hints/linux.370 @@ -56,6 +56,10 @@ endif # WANT_WIN_QT # #-INCLUDE multiw-3.370 +ifeq "$(NEED_ASAN)" "1" +LFLAGS+=-fsanitize=address +endif + ifeq "$(USE_CURSESLIB)" "1" # default CURSESLIB = -lncurses -ltinfo @@ -159,7 +163,7 @@ WINX11LIB += -lXpm VARDATND0 += rip.xpm endif WINLIB += $(WINX11LIB) -LFLAGS=-L/opt/X11/lib +LFLAGS+=-L/opt/X11/lib endif # WANT_WIN_X11 ifdef WANT_WIN_QT @@ -294,7 +298,7 @@ endif # GITSUBMODULES DLLIB = -ldl # Only needed for GLIBC stack trace: -LFLAGS=-rdynamic +LFLAGS+=-rdynamic # if TTY_TILES_ESCCODES #WINSRC += tile.c diff --git a/sys/unix/hints/macOS.370 b/sys/unix/hints/macOS.370 index 4500d8a3f..0fa1e067a 100755 --- a/sys/unix/hints/macOS.370 +++ b/sys/unix/hints/macOS.370 @@ -72,6 +72,10 @@ endif # WANT_WIN_QT #-INCLUDE multiw-3.370 +ifeq "$(NEED_ASAN)" "1" +LFLAGS +=-fsanitize=address +endif + # NetHack sources control NHCFLAGS+=-DDLB NHCFLAGS+=-DHACKDIR=\"$(HACKDIR)\"