diff --git a/tests/Makefile.tests_common b/tests/Makefile.tests_common index cc8dc55b5cb1..42825c56ddc3 100644 --- a/tests/Makefile.tests_common +++ b/tests/Makefile.tests_common @@ -1,5 +1,12 @@ APPLICATION ?= tests_$(notdir $(patsubst %/,%,$(CURDIR))) +# for these boards, enable asan (Address Sanitizer) +ASAN_BOARDS ?= native native64 +ifneq (, $(filter $(ASAN_BOARDS), $(BOARD))) + CFLAGS += $(CFLAGS_ASAN) + LINKFLAGS += $(LINKFLAGS_ASAN) +endif + ifneq (,$(wildcard $(CURDIR)/tests*/.)) # add interactive test configuration when testing Kconfig DEFAULT_MODULE += test_utils_interactive_sync diff --git a/tests/unittests/Makefile b/tests/unittests/Makefile index cc9bdd02172f..2a56ed81fd70 100644 --- a/tests/unittests/Makefile +++ b/tests/unittests/Makefile @@ -37,13 +37,6 @@ INCLUDES += -I$(RIOTBASE)/tests/unittests/common CFLAGS += -DTHREAD_STACKSIZE_MAIN=THREAD_STACKSIZE_LARGE CFLAGS += -DCONFIG_CORE_EXIT_WITH_MAIN=1 -# for these boards, enable asan (Address Sanitizer) -ASAN_BOARDS ?= native native64 -ifneq (, $(filter $(ASAN_BOARDS), $(BOARD))) - CFLAGS += $(CFLAGS_ASAN) - LINKFLAGS += $(LINKFLAGS_ASAN) -endif - include $(RIOTBASE)/Makefile.include .PHONY: $(UNIT_TESTS) diff --git a/tests/unittests/tests-asan_canary/Makefile b/tests/unittests/tests-asan_canary/Makefile new file mode 100644 index 000000000000..48422e909a47 --- /dev/null +++ b/tests/unittests/tests-asan_canary/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-asan_canary/Makefile.include b/tests/unittests/tests-asan_canary/Makefile.include new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/unittests/tests-asan_canary/tests-asan.c b/tests/unittests/tests-asan_canary/tests-asan.c new file mode 100644 index 000000000000..e68b254ec365 --- /dev/null +++ b/tests/unittests/tests-asan_canary/tests-asan.c @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2024 Christian Amsüss + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + * @author Christian Amsüss + */ + +#include +#include + +#include "tests-asan.h" + +static void __attribute__ ((noinline)) read_10(uint8_t *source) +{ + printf("Read %d\n", source[10]); +} + +static void tests_read_beyond_buf(void) +{ + uint8_t source[5] = {0, }; + read_10(&source[0]); +} + +Test *tests_asan_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(tests_read_beyond_buf), + }; + + EMB_UNIT_TESTCALLER(asan_tests, NULL, NULL, fixtures); + + return (Test *)&asan_tests; +} + +void tests_asan_canary(void) +{ + TESTS_RUN(tests_asan_tests()); +} diff --git a/tests/unittests/tests-asan_canary/tests-asan.h b/tests/unittests/tests-asan_canary/tests-asan.h new file mode 100644 index 000000000000..e1797a1a6f67 --- /dev/null +++ b/tests/unittests/tests-asan_canary/tests-asan.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2024 Christian Amsüss + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @addtogroup unittests + * @{ + * + * @file + * @brief Unittests canary for address sanitizer violations + * + * @author Christian Amsüss + */ +#ifndef TESTS_ASAN_H +#define TESTS_ASAN_H + +#include "embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + * + * If this test passes, the address sanitizer was not active. + */ +void tests_asan(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_ASAN_H */ +/** @} */