From f2360e6c9e9d620d28014a71d4a40247353b7764 Mon Sep 17 00:00:00 2001 From: chrysn Date: Sat, 6 Apr 2024 12:06:18 +0200 Subject: [PATCH 1/3] tests: Enable ASAN on native for all tests not just unit tests --- tests/Makefile.tests_common | 7 +++++++ tests/unittests/Makefile | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) 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) From 67350f6b2bd6036ee1483dd836721a15b8b6ca5f Mon Sep 17 00:00:00 2001 From: chrysn Date: Sat, 6 Apr 2024 12:06:34 +0200 Subject: [PATCH 2/3] DO NOT MERGE: Introduce ASAN errors in unit tests This verifies that unit tests still run under ASAN (they currently do but are special-cased) --- tests/unittests/tests-asan_canary/Makefile | 1 + .../tests-asan_canary/Makefile.include | 0 .../unittests/tests-asan_canary/tests-asan.c | 46 +++++++++++++++++++ .../unittests/tests-asan_canary/tests-asan.h | 39 ++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 tests/unittests/tests-asan_canary/Makefile create mode 100644 tests/unittests/tests-asan_canary/Makefile.include create mode 100644 tests/unittests/tests-asan_canary/tests-asan.c create mode 100644 tests/unittests/tests-asan_canary/tests-asan.h 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..46922caf428f --- /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 */ +/** @} */ From 474681127fc732595c3f671924ac1f770024c1d9 Mon Sep 17 00:00:00 2001 From: chrysn Date: Sat, 27 Apr 2024 22:21:43 +0200 Subject: [PATCH 3/3] fixup! Co-authored-by: Teufelchen <9516484+Teufelchen1@users.noreply.github.com> --- tests/unittests/tests-asan_canary/tests-asan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittests/tests-asan_canary/tests-asan.c b/tests/unittests/tests-asan_canary/tests-asan.c index 46922caf428f..e68b254ec365 100644 --- a/tests/unittests/tests-asan_canary/tests-asan.c +++ b/tests/unittests/tests-asan_canary/tests-asan.c @@ -27,8 +27,8 @@ 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) {