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

tests: Use ASAN for all (native) tests #20550

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions tests/Makefile.tests_common
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 0 additions & 7 deletions tests/unittests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/tests-asan_canary/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
Empty file.
46 changes: 46 additions & 0 deletions tests/unittests/tests-asan_canary/tests-asan.c
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @file
* @file
* @brief Provide a true-positive (failing) test case for ASAN

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only the "DO NOT MERGE" that will be removed once verified -- that commit will be removed again because AFAICT we don't have infrastructure for required-to-fail-to-build tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bold words for someone working that late on the weekend ;)

* @author Christian Amsüss <[email protected]>
*/

#include <stdint.h>
#include <stdio.h>

#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());
}
39 changes: 39 additions & 0 deletions tests/unittests/tests-asan_canary/tests-asan.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
#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 */
/** @} */
Loading