From 61cd32eeac2694dfbad6c759ae6d8e9c2bfd3d21 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 26 Jun 2024 13:27:25 +0100 Subject: [PATCH] Add sanitizer build options for host --- Sming/Arch/Host/README.rst | 32 ++++++++++++++++++++++++++++---- Sming/Arch/Host/app.mk | 4 ++++ Sming/Arch/Host/build.mk | 16 ++++++++++++++++ Sming/Wiring/FakePgmSpace.cpp | 6 +++++- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/Sming/Arch/Host/README.rst b/Sming/Arch/Host/README.rst index e3a90e4fa1..641149a33a 100644 --- a/Sming/Arch/Host/README.rst +++ b/Sming/Arch/Host/README.rst @@ -52,6 +52,12 @@ To find out what options are in force, use ``make list-config``. Configuration ------------- +.. note:: + + The following settings are for debugging purposes and are not 'sticky'. + Where used, they should generally be defined globally using ``export``. + + .. envvar:: CLI_TARGET_OPTIONS Use this to add any custom options to the emulator command line. e.g.: @@ -59,8 +65,6 @@ Configuration make run CLI_TARGET_OPTIONS=--help make run CLI_TARGET_OPTIONS="--debug=0 --cpulimit=2" - Note: These settings are not 'sticky' - .. envvar:: CLANG_BUILD @@ -68,8 +72,6 @@ Configuration 1: Use standard ``clang`` N: Use specific installed version, ``clang-N`` - Note: This setting is not 'sticky' - .. envvar:: BUILD64 @@ -79,6 +81,28 @@ Configuration On MacOS builds are 64-bit only. Default for other systems is 32-bit. +.. envvar:: ENABLE_SANITIZERS + + default: 0 (off) + + Enable this option to build with lots of runtime checking. + + This provides some of the capabilities of valgrind but by instrumenting + the code when it is compiled, rather than patching at runtime. + + It also links in some additional runtime support libraries. + + Run a full rebuild after changing this setting (or :envvar:`SANITIZERS`):: + + make clean components-clean + make + + +.. envvar:: SANITIZERS + + Selects which sanitizers are used. See :envvar:`ENABLE_SANITIZERS`. + + Components ---------- diff --git a/Sming/Arch/Host/app.mk b/Sming/Arch/Host/app.mk index cf1d452e3d..3d0540d206 100644 --- a/Sming/Arch/Host/app.mk +++ b/Sming/Arch/Host/app.mk @@ -9,6 +9,10 @@ ifneq ($(BUILD64),1) LDFLAGS += -m32 endif +ifeq ($(ENABLE_SANITIZERS),1) +LDFLAGS += $(foreach s,$(SANITIZERS),-fsanitize=$s) +endif + # Executable TARGET_OUT_0 := $(FW_BASE)/$(APP_NAME)$(TOOL_EXT) diff --git a/Sming/Arch/Host/build.mk b/Sming/Arch/Host/build.mk index 885e6f22b2..3758752fc8 100644 --- a/Sming/Arch/Host/build.mk +++ b/Sming/Arch/Host/build.mk @@ -54,6 +54,22 @@ CPPFLAGS += \ -D_FILE_OFFSET_BITS=64 \ -D_TIME_BITS=64 +# Sanitizers +DEBUG_VARS += ENABLE_SANITIZERS SANITIZERS +ENABLE_SANITIZERS ?= 0 +SANITIZERS ?= \ + address \ + pointer-compare \ + pointer-subtract \ + leak \ + undefined +ifeq ($(ENABLE_SANITIZERS),1) +CPPFLAGS += \ + -fstack-protector-all \ + -fsanitize-address-use-after-scope \ + $(foreach s,$(SANITIZERS),-fsanitize=$s) +endif + # => Tools MEMANALYZER = size diff --git a/Sming/Wiring/FakePgmSpace.cpp b/Sming/Wiring/FakePgmSpace.cpp index 156a31df6c..0a032c5453 100644 --- a/Sming/Wiring/FakePgmSpace.cpp +++ b/Sming/Wiring/FakePgmSpace.cpp @@ -19,7 +19,11 @@ void* memcpy_aligned(void* dst, const void* src, unsigned len) { assert(IS_ALIGNED(dst) && IS_ALIGNED(src)); - memcpy(dst, src, ALIGNUP4(len)); +#ifndef ARCH_HOST + // Address sanitisers get tripped if we do this in Host builds + len = ALIGNUP4(len); +#endif + memcpy(dst, src, len); return dst; }