-
Notifications
You must be signed in to change notification settings - Fork 7.4k
mpu stack guard: fix stack alignement #500
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
Changes from 1 commit
43c0aa9
8ad631a
6c79278
9f9f210
7a78fe8
fb10290
01c0d83
d8111b3
4316839
eae347e
e59a33d
ca448bd
ffe9dc3
51f9307
3443430
120d53e
29b86da
1e7388f
c1fed51
480cd47
c4c11de
e81d29c
95c6e6a
f2d2305
1a620de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
CONFIG_STDOUT_CONSOLE=y | ||
CONFIG_MPU_STACK_GUARD=y | ||
CONFIG_SYS_LOG=y | ||
CONFIG_SYS_LOG_DEFAULT_LEVEL=4 | ||
CONFIG_SYS_LOG_OVERRIDE_LEVEL=4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,62 +10,79 @@ | |
#include <misc/printk.h> | ||
|
||
/* size of stack area used by each thread */ | ||
#define STACKSIZE 1024 | ||
#define STACKSIZE 512 | ||
|
||
/* scheduling priority used by each thread */ | ||
#define PRIORITY 7 | ||
|
||
/* delay between greetings (in ms) */ | ||
#define SLEEPTIME 500 | ||
|
||
/* delay between greetings (in ms) */ | ||
#define SCHEDTIME 30 | ||
|
||
/* Focaccia tastes better than DEEDBEEF ;-) */ | ||
#define STACK_GUARD_CANARY 0xF0CACC1A | ||
|
||
struct stack_guard_buffer { | ||
/* Make sure canary is not optimized by the compiler */ | ||
struct k_thread thread; | ||
volatile u32_t canary; | ||
K_THREAD_STACK_MEMBER(stack, STACKSIZE); | ||
}; | ||
|
||
struct stack_guard_buffer buf; | ||
struct k_thread test_thread; | ||
#define LAST_THREAD_NUM 3 | ||
struct stack_guard_buffer buf[LAST_THREAD_NUM+1]; | ||
|
||
u32_t recursive_loop(u32_t counter) | ||
u32_t recursive_loop(u32_t counter, int num, void *dummy) | ||
{ | ||
if (buf.canary != STACK_GUARD_CANARY) { | ||
printk("Canary = 0x%08x\tTest not passed.\n", buf.canary); | ||
|
||
if (buf[num].canary != STACK_GUARD_CANARY) { | ||
printk("Canary = 0x%08x\tTest not passed.\n", buf[num].canary); | ||
|
||
while (1) { | ||
k_sleep(SLEEPTIME); | ||
} | ||
} | ||
|
||
k_sleep(SCHEDTIME); | ||
counter++; | ||
|
||
return recursive_loop(counter) + recursive_loop(counter); | ||
if (dummy == 0) | ||
return counter; | ||
return recursive_loop(counter, num, dummy) | ||
+recursive_loop(counter, num, dummy); | ||
} | ||
|
||
|
||
/* stack_guard_thread is a dynamic thread */ | ||
void stack_guard_thread(void *dummy1, void *dummy2, void *dummy3) | ||
{ | ||
ARG_UNUSED(dummy1); | ||
ARG_UNUSED(dummy2); | ||
ARG_UNUSED(dummy3); | ||
while (1) | ||
recursive_loop(0, (int)dummy1, dummy2); | ||
|
||
u32_t result = recursive_loop(0); | ||
|
||
/* We will never arrive here */ | ||
printk("Result: %d", result); | ||
} | ||
|
||
void main(void) | ||
{ | ||
buf.canary = STACK_GUARD_CANARY; | ||
int i; | ||
bool failed; | ||
|
||
printk("STACK_ALIGN %x\n", STACK_ALIGN); | ||
for (i = 0; i <= LAST_THREAD_NUM; i++) { | ||
buf[i].canary = STACK_GUARD_CANARY; | ||
failed = (i == LAST_THREAD_NUM) ? true : false; | ||
if (failed) { | ||
printk("MPU STACK GUARD Test\n"); | ||
printk("Canary Initial Value = 0x%x threads %p\n", | ||
buf[i].canary, &buf[i].thread); | ||
} | ||
|
||
printk("MPU STACK GUARD Test\n"); | ||
printk("Canary Initial Value = 0x%x\n", buf.canary); | ||
/* create stack_guard_thread */ | ||
k_thread_create(&buf[i].thread, buf[i].stack, STACKSIZE, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to use K_THREAD_STACK_SIZEOF instead of STACKSIZE There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah. That adjusts for the alignment adjustment. Otherwise you might get unlucky and bad things will happen. Good catch. |
||
stack_guard_thread, (void *)i, | ||
(void *)failed, NULL, PRIORITY, 0, K_NO_WAIT); | ||
} | ||
|
||
/* spawn stack_guard_thread */ | ||
k_thread_create(&test_thread, buf.stack, STACKSIZE, stack_guard_thread, | ||
NULL, NULL, NULL, PRIORITY, 0, K_NO_WAIT); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please prepend the value with 0x
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed