This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Stack Guard.

I have a question on stack guard that I tried to enable on nRF52840 and hope you can give me some pointer to verify it is functioning properly.
Basically, I call nrf_stack_guard_init() and compare the behavior of code writing into the stack area. I do not see any difference in behavior. It seems the stack guard was not set up even the function return NRF_SUCCESS.
The stack of my system is at:
0x000000002003c000                __StackLimit = (__StackTop - SIZEOF (.stack_dummy))
0x0000000020040000                PROVIDE (__stack = __StackTop)
 My test code looks like this: 
 
auto* stack_limit_test_address = (uint8_t*) 0x2003c000;
    *stack_limit_test_address = 0;
    stack_limit_test_address = (uint8_t*) 0x2003c001;
    *stack_limit_test_address = 0;
    stack_limit_test_address = (uint8_t*) 0x2003d000;
    *stack_limit_test_address = 1;
    stack_limit_test_address = (uint8_t*) 0x2003e000;
    *stack_limit_test_address = 2;
    stack_limit_test_address = (uint8_t*) 0x2003f000;
    *stack_limit_test_address = 3;
    stack_limit_test_address = (uint8_t*) 0x2003ff00;
    *stack_limit_test_address = 4;
    stack_limit_test_address = (uint8_t*) 0x2003fff0;
    *stack_limit_test_address = 5;
    stack_limit_test_address = (uint8_t*) 0x2003ff00;
    *stack_limit_test_address = 6;
    stack_limit_test_address = (uint8_t*) 0x2003ffff;
    *stack_limit_test_address = 7;
    //stack_limit_test_address = (uint8_t*) 0x20040000;
    //*stack_limit_test_address = 8;                    // Next instruction after this line is executed triggers reset
    //stack_limit_test_address = (uint8_t*) 0x2003f000; // System reset when this line is enabled and executed.
    //*stack_limit_test_address = 9;

    nrf_stack_guard_init();

    stack_limit_test_address = (uint8_t*) 0x2003c000;
    *stack_limit_test_address = 0;
    stack_limit_test_address = (uint8_t*) 0x2003c001;
    *stack_limit_test_address = 0;
    stack_limit_test_address = (uint8_t*) 0x2003d000;
    *stack_limit_test_address = 1;
    stack_limit_test_address = (uint8_t*) 0x2003e000;
    *stack_limit_test_address = 2;
    stack_limit_test_address = (uint8_t*) 0x2003f000;
    *stack_limit_test_address = 3;
    stack_limit_test_address = (uint8_t*) 0x2003ff00;
    *stack_limit_test_address = 4;
    stack_limit_test_address = (uint8_t*) 0x2003fff0;
    *stack_limit_test_address = 5;
    stack_limit_test_address = (uint8_t*) 0x2003ff00;
    *stack_limit_test_address = 6;
    stack_limit_test_address = (uint8_t*) 0x2003ffff;
    *stack_limit_test_address = 7;
    //stack_limit_test_address = (uint8_t*) 0x20040000; // Same behavior as in the case before nrf_stack_guard_init() is called
    //*stack_limit_test_address = 8;                    // Instruction followed this line triggers a reset
    stack_limit_test_address = (uint8_t*) 0x2003f000;
    *stack_limit_test_address = 9;
My stack guard size should have been 128 bytes since NRF_STACK_GUARD_CONFIG_SIZE is kept at default value of 7.
NRF_STACK_GUARD_ENABLED is 1 ( enabled ).
Any idea what I am doing wrong? Am I missing some steps?
Related