Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sanitizer build options for host #2842

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Components/hostlib/hostlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ size_t getHostAppDir(char* path, size_t bufSize)
char sep = '\\';
#elif defined(__APPLE__)
uint32_t size = bufSize;
size_t len = _NSGetExecutablePath(path, &size) ? 0 : size;
size_t len = _NSGetExecutablePath(path, &size) ? 0 : strlen(path);
char sep = '/';
#else
size_t len = readlink("/proc/self/exe", path, bufSize - 1);
Expand Down
37 changes: 33 additions & 4 deletions Sming/Arch/Host/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,26 @@ 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.:

make run CLI_TARGET_OPTIONS=--help
make run CLI_TARGET_OPTIONS="--debug=0 --cpulimit=2"

Note: These settings are not 'sticky'


.. envvar:: CLANG_BUILD

0: Use GCC (default)
1: Use standard ``clang``
N: Use specific installed version, ``clang-N``

Note: This setting is not 'sticky'


.. envvar:: BUILD64

Expand All @@ -79,6 +81,33 @@ 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

.. note::

If using :envvar:`CLANG_BUILD` then all runtime libraries should already be available.
For GCC you will also need to install ``libasan`` and ``libubsan``.


.. envvar:: SANITIZERS

Selects which sanitizers are used. See :envvar:`ENABLE_SANITIZERS`.


Components
----------

Expand Down
4 changes: 4 additions & 0 deletions Sming/Arch/Host/app.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions Sming/Arch/Host/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Sming/Libraries/Graphics
6 changes: 5 additions & 1 deletion Sming/Wiring/FakePgmSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading