Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
43c0aa9
boards: 96b_nitrogen: Add support for flash/debug with pyOCD
galak Jun 13, 2017
8ad631a
scripts: bossa-flash.sh: fix variable usage
galak Jun 13, 2017
6c79278
scripts: pyocd.sh: Add support for passing board_id to pyocd commands
galak Jun 15, 2017
9f9f210
board: frdm_k64f: allow overriding default debug/flash scripts
galak Jun 15, 2017
7a78fe8
boards: sam_e70_xplained: allow flashing via JTAG header
mnkp Jun 14, 2017
fb10290
arm: soc: stm32: make mpu f4 config useable for other family
jamike Jun 9, 2017
01c0d83
arm: soc: stm32: l4: add MPU capability for series
jamike Jun 9, 2017
d8111b3
boards: nucleo_l476rg: enable MPU
jamike Jun 9, 2017
4316839
boards: disco_l475_iot1: enable MPU
jamike Jun 9, 2017
eae347e
arm: soc: stm32: f3: add MPU capability
jamike Jun 16, 2017
e59a33d
arm: stm32f1: Add support for STM32F103x8 SoC
sidcha May 21, 2017
ca448bd
boards: arm: Add support for STM32 Minimum Development Board
sidcha May 21, 2017
ffe9dc3
dts: Rename k64f-uart to kinetis-uart
MaureenHelm Jun 20, 2017
51f9307
dts: Rename kw41z-lpuart to kinetis-lpuart
MaureenHelm Jun 20, 2017
3443430
dts: Rename k64f-pinmux to kinetis-pinmux
MaureenHelm Jun 20, 2017
120d53e
dts: Rename k64f-gpio to kinetis-gpio
MaureenHelm Jun 20, 2017
29b86da
dts: Rename k64sim to nxp,k64f-sim
MaureenHelm Jun 20, 2017
1e7388f
doc: fix doc errors in stm32_min_dev.rst (take 2)
dbkinder Jun 23, 2017
c1fed51
dts: disco_l475_iot1: add flash partition
jamike Jun 23, 2017
480cd47
boards; cc2650_sensortag: Get building with sanitycheck
galak Jun 23, 2017
c4c11de
dts: nucleo_f401re: add partition support for bootloader
jamike Jun 20, 2017
e81d29c
arm: mpu: fix: align stack for mpu stack guard
jamike Jun 27, 2017
95c6e6a
mpu_stack_guard_test: use thread macro to define stack
jamike Jun 27, 2017
f2d2305
mpu_stack_guard_test: add more thread for test
jamike Jun 27, 2017
1a620de
mpu_stack_guard_test: update log in README.rst
jamike Jun 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions samples/mpu/mpu_stack_guard_test/prj_stack_guard.conf
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
55 changes: 36 additions & 19 deletions samples/mpu/mpu_stack_guard_test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

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,
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}