Skip to content

Instantly share code, notes, and snippets.

@fzakaria
Created November 23, 2021 03:31
Show Gist options
  • Save fzakaria/eab8d14695549ddefdf9ba09038016ad to your computer and use it in GitHub Desktop.
Save fzakaria/eab8d14695549ddefdf9ba09038016ad to your computer and use it in GitHub Desktop.
Add support to Nix when building with Clang to use the Address Sanitizer (ASan)
diff --git a/Makefile b/Makefile
index 5040d2884..ac29e2526 100644
--- a/Makefile
+++ b/Makefile
@@ -27,11 +27,14 @@ makefiles = \
OPTIMIZE = 1
ifeq ($(OPTIMIZE), 1)
- GLOBAL_CXXFLAGS += -O3
+ GLOBAL_CXXFLAGS += -O1
else
GLOBAL_CXXFLAGS += -O0 -U_FORTIFY_SOURCE
endif
include mk/lib.mk
-GLOBAL_CXXFLAGS += -g -Wall -include config.h -std=c++17 -I src
+# enable ASan - https://clang.llvm.org/docs/AddressSanitizer.html
+GLOBAL_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls -g -Wall -include config.h -std=c++17 -I src
+
+GLOBAL_LDFLAGS += -fsanitize=address
\ No newline at end of file
diff --git a/Makefile.config.in b/Makefile.config.in
index c8c4446b4..d9b1a2cd3 100644
--- a/Makefile.config.in
+++ b/Makefile.config.in
@@ -12,7 +12,8 @@ ENABLE_S3 = @ENABLE_S3@
GTEST_LIBS = @GTEST_LIBS@
HAVE_LIBCPUID = @HAVE_LIBCPUID@
HAVE_SECCOMP = @HAVE_SECCOMP@
-LDFLAGS = @LDFLAGS@
+LDFLAGS = @LDFLAGS@ -fsanitize=address
+LD = @CXX@
LIBARCHIVE_LIBS = @LIBARCHIVE_LIBS@
LIBBROTLI_LIBS = @LIBBROTLI_LIBS@
LIBCURL_LIBS = @LIBCURL_LIBS@
diff --git a/mk/libraries.mk b/mk/libraries.mk
index ffd7b5610..bdd595d8a 100644
--- a/mk/libraries.mk
+++ b/mk/libraries.mk
@@ -79,7 +79,8 @@ define build-library
else
ifndef HOST_DARWIN
ifndef HOST_CYGWIN
- $(1)_LDFLAGS += -Wl,-z,defs
+ # This is known to cause problems for AddressSanitizer
+ # $(1)_LDFLAGS += -Wl,-z,defs
endif
endif
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment