From f08989a3c8d40f44abc299041d95b33c3b564ad1 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 3 Nov 2021 21:38:22 +0100 Subject: [PATCH 1/4] core: make SCHED_TEST_STACK boolean and default to 1 with DEVELHELP --- core/include/sched.h | 9 +++++++++ core/include/thread.h | 4 ++-- core/sched.c | 2 +- core/thread.c | 4 ++-- cpu/esp_common/thread_arch.c | 2 +- tests/bench_sizeof_coretypes/main.c | 2 +- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/include/sched.h b/core/include/sched.h index db9fad5463..23cfc56d75 100644 --- a/core/include/sched.h +++ b/core/include/sched.h @@ -119,6 +119,15 @@ extern "C" { */ #define PRIkernel_pid PRIi16 +#if defined(DEVELHELP) || defined(DOXYGEN) +/** + * Enables detection of stack overflows and measures stack usage when != 0 + */ +#ifndef SCHED_TEST_STACK +#define SCHED_TEST_STACK 1 +#endif /* SCHED_TEST_STACK */ +#endif /* DEVELHELP */ + /** * Unique process identifier */ diff --git a/core/include/thread.h b/core/include/thread.h index 1636953817..a2872256b9 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -193,7 +193,7 @@ struct _thread { msg_t *msg_array; /**< memory holding messages sent to this thread's message queue */ #endif -#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \ +#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \ || defined(MODULE_MPU_STACK_GUARD) || defined(DOXYGEN) char *stack_start; /**< thread's stack start address */ #endif @@ -545,7 +545,7 @@ const char *thread_state_to_string(thread_status_t state); */ static inline void *thread_get_stackstart(const thread_t *thread) { -#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \ +#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \ || defined(MODULE_MPU_STACK_GUARD) return thread->stack_start; #else diff --git a/core/sched.c b/core/sched.c index b6eb15c28f..0afbdea0b7 100644 --- a/core/sched.c +++ b/core/sched.c @@ -119,7 +119,7 @@ static void _unschedule(thread_t *active_thread) active_thread->status = STATUS_PENDING; } -#ifdef SCHED_TEST_STACK +#if IS_ACTIVE(SCHED_TEST_STACK) if (*((uintptr_t *)active_thread->stack_start) != (uintptr_t)active_thread->stack_start) { LOG_WARNING( diff --git a/core/thread.c b/core/thread.c index 4c56608ca3..fa2e49130b 100644 --- a/core/thread.c +++ b/core/thread.c @@ -233,7 +233,7 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, _init_tls(thread->tls); #endif -#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) +#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) if (flags & THREAD_CREATE_STACKTEST) { /* assign each int of the stack the value of it's address. Alignment * has been handled above, so silence -Wcast-align */ @@ -274,7 +274,7 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, thread->pid = pid; thread->sp = thread_stack_init(function, arg, stack, stacksize); -#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || \ +#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) || \ defined(MODULE_MPU_STACK_GUARD) thread->stack_start = stack; #endif diff --git a/cpu/esp_common/thread_arch.c b/cpu/esp_common/thread_arch.c index f6aded6725..de04cdc247 100644 --- a/cpu/esp_common/thread_arch.c +++ b/cpu/esp_common/thread_arch.c @@ -150,7 +150,7 @@ char* thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_sta sp = (uint8_t*)(((uintptr_t)(top_of_stack + 1) - XT_STK_FRMSZ - XT_CP_SIZE) & ~0xf); /* Clear whole stack with a known value to assist debugging */ - #if !defined(DEVELHELP) && !defined(SCHED_TEST_STACK) + #if !defined(DEVELHELP) && !IS_ACTIVE(SCHED_TEST_STACK) /* Unfortunately, this affects thread_measure_stack_free function */ memset(stack_start, 0, stack_size); #else diff --git a/tests/bench_sizeof_coretypes/main.c b/tests/bench_sizeof_coretypes/main.c index 099f735ac0..37221a1185 100644 --- a/tests/bench_sizeof_coretypes/main.c +++ b/tests/bench_sizeof_coretypes/main.c @@ -101,7 +101,7 @@ int main(void) P(msg_queue); P(msg_array); #endif -#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD) +#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD) P(stack_start); #endif #ifdef DEVELHELP From e27fd3018e34f83994be2e7315762a280b149c5f Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 3 Nov 2021 21:50:28 +0100 Subject: [PATCH 2/4] core/thread.h: fix new uncrustify error --- core/include/thread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/include/thread.h b/core/include/thread.h index a2872256b9..ca75c3f689 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -513,7 +513,7 @@ static inline thread_status_t thread_get_status(const thread_t *thread) * @param thread thread to work on * @returns priority of thread */ -static inline uint8_t thread_get_priority(const thread_t *thread) +static inline uint8_t thread_get_priority(const thread_t *thread) { return thread->priority; } From 098eeb1a2c810811694044e65366feb6482c442c Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 3 Nov 2021 21:53:34 +0100 Subject: [PATCH 3/4] cpu/esp_common: suppress cppcheck false positives --- cpu/esp_common/thread_arch.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cpu/esp_common/thread_arch.c b/cpu/esp_common/thread_arch.c index de04cdc247..10cf207a3e 100644 --- a/cpu/esp_common/thread_arch.c +++ b/cpu/esp_common/thread_arch.c @@ -360,6 +360,8 @@ void thread_isr_stack_init(void) int thread_isr_stack_usage(void) { + /* cppcheck-suppress comparePointers + * (reason: comes from ESP-SDK, so should be valid) */ return &port_IntStackTop - &port_IntStack - thread_measure_stack_free((char*)&port_IntStack); } @@ -379,6 +381,8 @@ void *thread_isr_stack_start(void) void thread_isr_stack_print(void) { printf("Printing current ISR\n"); + /* cppcheck-suppress comparePointers + * (reason: comes from ESP-SDK, so should be valid) */ esp_hexdump(&port_IntStack, &port_IntStackTop-&port_IntStack, 'w', 8); } From 2955288b8be4e5d0fcb595bb96e576760eabb8eb Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Thu, 4 Nov 2021 09:54:59 +0100 Subject: [PATCH 4/4] tests: update Makefile.ci for SCHED_STACK_TEST extension of DEVELHELP --- tests/driver_dose/Makefile.ci | 1 + tests/gnrc_sixlowpan_frag_minfwd/Makefile.ci | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tests/driver_dose/Makefile.ci b/tests/driver_dose/Makefile.ci index 2eb937ad22..a98193f422 100644 --- a/tests/driver_dose/Makefile.ci +++ b/tests/driver_dose/Makefile.ci @@ -4,6 +4,7 @@ BOARD_INSUFFICIENT_MEMORY := \ arduino-mega2560 \ arduino-nano \ arduino-uno \ + atxmega-a3bu-xplained \ atmega328p \ atmega328p-xplained-mini \ bluepill-stm32f030c8 \ diff --git a/tests/gnrc_sixlowpan_frag_minfwd/Makefile.ci b/tests/gnrc_sixlowpan_frag_minfwd/Makefile.ci index 6d37cd67b1..22951acd27 100644 --- a/tests/gnrc_sixlowpan_frag_minfwd/Makefile.ci +++ b/tests/gnrc_sixlowpan_frag_minfwd/Makefile.ci @@ -8,6 +8,8 @@ BOARD_INSUFFICIENT_MEMORY := \ atmega328p \ atmega328p-xplained-mini \ atxmega-a3bu-xplained \ + blackpill \ + bluepill \ bluepill-stm32f030c8 \ derfmega128 \ hifive1 \