From f2720449b3ede0299da61dfaff9fce6b8db78f75 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 28 Jul 2018 10:32:58 +0100 Subject: Enable sanitize target for new gccs and clang --- makefiles/Makefile.clang | 11 +++++++++++ makefiles/Makefile.gcc | 11 +++++++++++ makefiles/Makefile.tools | 19 +++++++++++++++++++ makefiles/Makefile.top | 6 ++++++ 4 files changed, 47 insertions(+) diff --git a/makefiles/Makefile.clang b/makefiles/Makefile.clang index 4006a92..2a11a5b 100644 --- a/makefiles/Makefile.clang +++ b/makefiles/Makefile.clang @@ -28,6 +28,17 @@ ARFLG := cru # Definitions of various attributes CFLAGS := $(CFLAGS) -D_ALIGNED="__attribute__((aligned))" +# Extensions for sanitize target +ifeq ($(MAKECMDGOALS),sanitize) + SANCFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover + SANCXXFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover + SANLDFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover + + CFLAGS := $(CFLAGS) $(SANCFLAGS) + CXXFLAGS := $(CXXFLAGS) $(SANCXXFLAGS) + LDFLAGS := $(LDFLAGS) $(SANLDFLAGS) +endif + ############################################################################### # Mac OS X Universal Binaries ############################################################################### diff --git a/makefiles/Makefile.gcc b/makefiles/Makefile.gcc index 5f2bd4d..6ca7ba1 100644 --- a/makefiles/Makefile.gcc +++ b/makefiles/Makefile.gcc @@ -49,6 +49,17 @@ ifeq ($(MAKECMDGOALS),profile) LDFLAGS := $(LDFLAGS) $(PROFLDFLAGS) endif +# Extensions for sanitize target +ifeq ($(MAKECMDGOALS),sanitize) + SANCFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover + SANCXXFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover + SANLDFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover + + CFLAGS := $(CFLAGS) $(SANCFLAGS) + CXXFLAGS := $(CXXFLAGS) $(SANCXXFLAGS) + LDFLAGS := $(LDFLAGS) $(SANLDFLAGS) +endif + # RISC OS module extensions ifeq ($(COMPONENT_TYPE),riscos-module) ifneq ($(HOST),arm-unknown-riscos) diff --git a/makefiles/Makefile.tools b/makefiles/Makefile.tools index 86fac4c..33d6add 100644 --- a/makefiles/Makefile.tools +++ b/makefiles/Makefile.tools @@ -399,6 +399,25 @@ ifeq ($(toolchain),) $(error Unable to detect toolchain) endif +# Detect if the toolchain ought to support sanitizers +SANITIZE_OK=no +ifeq ($(toolchain),gcc) + GCC_VERSION := $(shell $(CC) -dumpversion) + GCC_MAJOR := $(word 1,$(subst ., ,$(GCC_VERSION))) + ifeq ($(shell expr $(GCC_MAJOR) \>= 6),1) + SANITIZE_OK=yes + endif +else ifeq ($(toolchain),clang) + SANITIZE_OK=yes +endif + +# And fail if we can't sanitize and yet the user asked for it +ifeq ($(MAKECMDGOALS),sanitize) + ifeq ($(SANITIZE_OK),no) + $(error Unable to build with sanitizers enabled, compiler not compatible + endif +endif + # TODO: It would be nice to avoid this hard-coded path include $(NSBUILD)/Makefile.$(toolchain) diff --git a/makefiles/Makefile.top b/makefiles/Makefile.top index 9d3c680..caac166 100644 --- a/makefiles/Makefile.top +++ b/makefiles/Makefile.top @@ -48,6 +48,7 @@ # test Build and run test suite, using current settings. # coverage Determine test suite coverage (requires lcov) # profile Build with profiling support enabled (requires gprof) +# sanitize Build and run test suite, using ASAN and UBSAN # docs Produce documentation (requires doxygen) # dist Produce release tarball from latest git tag # clean Clean the build @@ -123,6 +124,8 @@ else ifeq ($(MAKECMDGOALS),profile) WANT_TEST := yes else ifeq ($(MAKECMDGOALS),coverage) WANT_TEST := yes +else ifeq ($(MAKECMDGOALS),sanitize) + WANT_TEST := yes else WANT_TEST := no endif @@ -231,6 +234,9 @@ coverage: __precov test $(Q)$(GENHTML) -o $(COVERAGEDIR) --num-spaces 2 \ $(COVERAGEDIR)/$(COMPONENT).info +# Build and test sanitizers +sanitize: test + # Build for profiling profile: __partial_clean test -- cgit v1.2.3