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

Stack Guard and MPU

Hi, I'm trying to get the nrf_stack_guard and nrf_mpu libraries to catch writes past the end of the stack. My stack is 8kB in size (0x2000E000-0x20010000). This is the log output after initializing the Stack Guard module:

<debug> nrf_mpu: MPU region creating (location: 0x2000E000-0x2000E07F)
<debug> nrf_mpu: MPU region 0 created (location: 0x2000E000-0x2000E07F, access: RO/RO, type: Normal, flags: XN).
<info> stack_guard: Stack Guard: 0x2000E000-0x2000E07F (usable stack area: 8064 bytes)

I'm having a little trouble understanding how the MPU works. I'd expect a write to 0x2000E000 to trigger the HardFault_Handler, but in reality nothing is triggered. The write just happens and silentlly corrupts RAM below the stack.

I'm using the nrf gcc hardfault library implementation and am able to catch NULL dereferences and other faults, so that should be correctly set up.

What am I missing?

Parents
  • Hi,

    I do not immediately see the issue in this case. I suspect  is onto something.

    I did a tiny modification to the Command Line Interface (CLI) Example (which uses stack guard out of the box) but were not able to reproduce the behavior you see.

    diff --git a/examples/peripheral/cli/demo_cli_cmds.c b/examples/peripheral/cli/demo_cli_cmds.c
    index ac7c57e..b6ea4b2 100644
    --- a/examples/peripheral/cli/demo_cli_cmds.c
    +++ b/examples/peripheral/cli/demo_cli_cmds.c
    @@ -376,6 +376,8 @@ static void cmd_nordic(nrf_cli_t const * p_cli, size_t argc, char **argv)
                         "\n");
     
         nrf_cli_print(p_cli, "                Nordic Semiconductor              \n");
    +
    +    *(volatile uint32_t*)0x2000E000 = 0xbadeba11;
     }
     
     /* This function cannot be static otherwise it can be inlined. As a result, variable:
    

    Running the example on a nRF52 DK with the above modification I get the expected hard fault after typing "nordic" in the terminal.

Reply
  • Hi,

    I do not immediately see the issue in this case. I suspect  is onto something.

    I did a tiny modification to the Command Line Interface (CLI) Example (which uses stack guard out of the box) but were not able to reproduce the behavior you see.

    diff --git a/examples/peripheral/cli/demo_cli_cmds.c b/examples/peripheral/cli/demo_cli_cmds.c
    index ac7c57e..b6ea4b2 100644
    --- a/examples/peripheral/cli/demo_cli_cmds.c
    +++ b/examples/peripheral/cli/demo_cli_cmds.c
    @@ -376,6 +376,8 @@ static void cmd_nordic(nrf_cli_t const * p_cli, size_t argc, char **argv)
                         "\n");
     
         nrf_cli_print(p_cli, "                Nordic Semiconductor              \n");
    +
    +    *(volatile uint32_t*)0x2000E000 = 0xbadeba11;
     }
     
     /* This function cannot be static otherwise it can be inlined. As a result, variable:
    

    Running the example on a nRF52 DK with the above modification I get the expected hard fault after typing "nordic" in the terminal.

Children
No Data
Related