@@ -36,12 +36,17 @@ kcb_t *kcb = &kernel_state;
36
36
/* Deferred timer work flag to reduce interrupt latency */
37
37
static volatile bool timer_work_pending = false;
38
38
39
+ #if CONFIG_STACK_PROTECTION
40
+ /* Stack canary checking frequency - check every N context switches */
41
+ #define STACK_CHECK_INTERVAL 32
42
+
39
43
/* Magic number written to both ends of a task's stack for corruption detection.
40
44
*/
41
45
#define STACK_CANARY 0x33333333U
42
46
43
47
/* Stack check counter for periodic validation (reduces overhead). */
44
48
static uint32_t stack_check_counter = 0 ;
49
+ #endif /* CONFIG_STACK_PROTECTION */
45
50
46
51
/* Simple task lookup cache to accelerate frequent ID searches */
47
52
static struct {
@@ -74,6 +79,7 @@ static tcb_t *cache_lookup_task(uint16_t id)
74
79
return NULL ;
75
80
}
76
81
82
+ #if CONFIG_STACK_PROTECTION
77
83
/* Stack integrity check with reduced frequency */
78
84
static void task_stack_check (void )
79
85
{
@@ -104,6 +110,7 @@ static void task_stack_check(void)
104
110
panic (ERR_STACK_CHECK );
105
111
}
106
112
}
113
+ #endif /* CONFIG_STACK_PROTECTION */
107
114
108
115
/* Updates task delay counters and unblocks tasks when delays expire */
109
116
static list_node_t * delay_update (list_node_t * node , void * arg )
@@ -308,9 +315,11 @@ void dispatch(void)
308
315
if (hal_context_save (((tcb_t * ) kcb -> task_current -> data )-> context ) != 0 )
309
316
return ;
310
317
318
+ #if CONFIG_STACK_PROTECTION
311
319
/* Do stack check less frequently to reduce overhead */
312
320
if (unlikely ((kcb -> ticks & (STACK_CHECK_INTERVAL - 1 )) == 0 ))
313
321
task_stack_check ();
322
+ #endif
314
323
315
324
list_foreach (kcb -> tasks , delay_update , NULL );
316
325
@@ -340,7 +349,9 @@ void yield(void)
340
349
if (hal_context_save (((tcb_t * ) kcb -> task_current -> data )-> context ) != 0 )
341
350
return ;
342
351
352
+ #if CONFIG_STACK_PROTECTION
343
353
task_stack_check ();
354
+ #endif
344
355
345
356
/* In cooperative mode, delays are only processed on an explicit yield. */
346
357
if (!kcb -> preemptive )
@@ -363,10 +374,12 @@ static bool init_task_stack(tcb_t *tcb, size_t stack_size)
363
374
return false;
364
375
}
365
376
377
+ #if CONFIG_STACK_PROTECTION
366
378
/* Only initialize essential parts to reduce overhead */
367
379
* (uint32_t * ) stack = STACK_CANARY ;
368
380
* (uint32_t * ) ((uintptr_t ) stack + stack_size - sizeof (uint32_t )) =
369
381
STACK_CANARY ;
382
+ #endif
370
383
371
384
tcb -> stack = stack ;
372
385
tcb -> stack_sz = stack_size ;
0 commit comments