Skip to content

Commit 7629ec5

Browse files
committed
Allow stack checking to be conditionally compiled
The 'CONFIG_STACK_PROTECTION' build-time configuration is used to enable or disable stack canary protection. It is enabled by default and can be disabled to reduce interrupt latency in performance builds.
1 parent a857b70 commit 7629ec5

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

arch/riscv/build.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ F_TICK := 100
1414

1515
DEFINES := -DF_CPU=$(F_CLK) \
1616
-DUSART_BAUD=$(SERIAL_BAUDRATE) \
17-
-DF_TIMER=$(F_TICK)
17+
-DF_TIMER=$(F_TICK) \
18+
-include config.h
1819

1920
ASFLAGS = -march=rv32imzicsr -mabi=ilp32
2021
CFLAGS += -Wall -Wextra -Wshadow -Wno-unused-parameter -Werror

config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
/* Stack Overflow Detection Configuration */
4+
#ifndef CONFIG_STACK_PROTECTION
5+
#define CONFIG_STACK_PROTECTION 1 /* Default: enabled for safety */
6+
#endif

include/sys/task.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ extern kcb_t *kcb;
100100
/* Minimum stack size to prevent stack overflow. */
101101
#define MIN_TASK_STACK_SIZE 256
102102

103-
/* Stack canary checking frequency - check every N context switches to reduce
104-
* overhead.
105-
*/
106-
#define STACK_CHECK_INTERVAL 32
107-
108103
/* Task lookup cache size for frequently accessed tasks */
109104
#define TASK_CACHE_SIZE 4
110105

kernel/task.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ kcb_t *kcb = &kernel_state;
3636
/* Deferred timer work flag to reduce interrupt latency */
3737
static volatile bool timer_work_pending = false;
3838

39+
#if CONFIG_STACK_PROTECTION
40+
/* Stack canary checking frequency - check every N context switches */
41+
#define STACK_CHECK_INTERVAL 32
42+
3943
/* Magic number written to both ends of a task's stack for corruption detection.
4044
*/
4145
#define STACK_CANARY 0x33333333U
4246

4347
/* Stack check counter for periodic validation (reduces overhead). */
4448
static uint32_t stack_check_counter = 0;
49+
#endif /* CONFIG_STACK_PROTECTION */
4550

4651
/* Simple task lookup cache to accelerate frequent ID searches */
4752
static struct {
@@ -74,6 +79,7 @@ static tcb_t *cache_lookup_task(uint16_t id)
7479
return NULL;
7580
}
7681

82+
#if CONFIG_STACK_PROTECTION
7783
/* Stack integrity check with reduced frequency */
7884
static void task_stack_check(void)
7985
{
@@ -104,6 +110,7 @@ static void task_stack_check(void)
104110
panic(ERR_STACK_CHECK);
105111
}
106112
}
113+
#endif /* CONFIG_STACK_PROTECTION */
107114

108115
/* Updates task delay counters and unblocks tasks when delays expire */
109116
static list_node_t *delay_update(list_node_t *node, void *arg)
@@ -308,9 +315,11 @@ void dispatch(void)
308315
if (hal_context_save(((tcb_t *) kcb->task_current->data)->context) != 0)
309316
return;
310317

318+
#if CONFIG_STACK_PROTECTION
311319
/* Do stack check less frequently to reduce overhead */
312320
if (unlikely((kcb->ticks & (STACK_CHECK_INTERVAL - 1)) == 0))
313321
task_stack_check();
322+
#endif
314323

315324
list_foreach(kcb->tasks, delay_update, NULL);
316325

@@ -340,7 +349,9 @@ void yield(void)
340349
if (hal_context_save(((tcb_t *) kcb->task_current->data)->context) != 0)
341350
return;
342351

352+
#if CONFIG_STACK_PROTECTION
343353
task_stack_check();
354+
#endif
344355

345356
/* In cooperative mode, delays are only processed on an explicit yield. */
346357
if (!kcb->preemptive)
@@ -363,10 +374,12 @@ static bool init_task_stack(tcb_t *tcb, size_t stack_size)
363374
return false;
364375
}
365376

377+
#if CONFIG_STACK_PROTECTION
366378
/* Only initialize essential parts to reduce overhead */
367379
*(uint32_t *) stack = STACK_CANARY;
368380
*(uint32_t *) ((uintptr_t) stack + stack_size - sizeof(uint32_t)) =
369381
STACK_CANARY;
382+
#endif
370383

371384
tcb->stack = stack;
372385
tcb->stack_sz = stack_size;

0 commit comments

Comments
 (0)