From 9a2387d721468013aa46efdae52423b5ec245eab Mon Sep 17 00:00:00 2001 From: Dylan Laduranty Date: Sat, 27 Apr 2024 18:55:57 +0200 Subject: [PATCH] tests/cpu: add basic test for cortexm_stack_limit module Signed-off-by: Dylan Laduranty --- tests/cpu/cortexm_stack_limit/Makefile | 11 +++ tests/cpu/cortexm_stack_limit/main.c | 99 ++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 tests/cpu/cortexm_stack_limit/Makefile create mode 100644 tests/cpu/cortexm_stack_limit/main.c diff --git a/tests/cpu/cortexm_stack_limit/Makefile b/tests/cpu/cortexm_stack_limit/Makefile new file mode 100644 index 0000000000..acc9c1ab58 --- /dev/null +++ b/tests/cpu/cortexm_stack_limit/Makefile @@ -0,0 +1,11 @@ +BOARD ?= nrf5340dk-app + +include ../Makefile.cpu_common + +FEATURES_REQUIRED += cortexm_stack_limit + +include $(RIOTBASE)/Makefile.include + +ifeq (llvm,$(TOOLCHAIN)) + CFLAGS += -Wno-infinite-recursion +endif diff --git a/tests/cpu/cortexm_stack_limit/main.c b/tests/cpu/cortexm_stack_limit/main.c new file mode 100644 index 0000000000..1f303a019f --- /dev/null +++ b/tests/cpu/cortexm_stack_limit/main.c @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2024 Mesotic SAS + * + * 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. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for the cortexm_stack_limit pseudo-module + * + * @author Dylan Laduranty + * + * @} + */ + +#include + +#include "cpu.h" +#include "thread.h" +#include "board.h" + +#define CANARY_VALUE 0xdeadbeef + +static struct { + unsigned int canary; + char stack[THREAD_STACKSIZE_MAIN]; +} buf; + +/* Tell modern GCC (12.x) to not complain that this infinite recursion is + * bound to overflow the stack - this is exactly what this test wants to do :) + * + * Also, tell older versions of GCC that do not know about -Winfinit-recursion + * that it is safe to ignore `GCC diagnostics ignored "-Winfinit-recursion"`. + * They behave as intended in this case :) + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Winfinite-recursion" +static int recurse(int counter) +{ + if (buf.canary != CANARY_VALUE) { +#ifdef LED0_ON + LED0_ON; +#endif +#ifdef LED1_ON + LED1_ON; +#endif +#ifdef LED2_ON + LED2_ON; +#endif +#ifdef LED3_ON + LED3_ON; +#endif + + for (;;) { + thread_sleep(); + } + } + + counter++; + /* Recursing twice here prevents the compiler from optimizing-out the recursion. */ + return recurse(counter) + recurse(counter); +} +#pragma GCC diagnostic pop + +static void *thread(void *arg) +{ + (void) arg; + + recurse(0); + + return NULL; +} + +int main(void) +{ + puts("\nCortex-M Stack limit test\n"); + + puts("If the test fails, all onboard LEDs will be on"); + +#ifdef MODULE_CORTEXM_STACK_LIMIT + puts("The cortexm_stack_limit module is present. Expect the board to crash"\ + " without onboard LEDs on.\n"); +#else + puts("The cortexm_stack_limit module is missing! Expect the test to crash"\ + " with all onboard LEDs on\n"); +#endif + + buf.canary = CANARY_VALUE; + thread_create(buf.stack, sizeof(buf.stack), THREAD_PRIORITY_MAIN + 1, + 0, thread, NULL, "thread"); + + return 0; +}