# Store load with volatile

**URL:** <https://community.memfault.com/t/store-load-with-volatile/381>\
**Category:** General\
**Created:** [April 14, 2021, 11:20am UTC](https://community.memfault.com/t/store-load-with-volatile/381 "2021-04-14T11:20:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spencer](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@Spencer](https://community.memfault.com/u/Spencer)\
**Post date:** [April 14, 2021, 11:20am UTC](https://community.memfault.com/t/store-load-with-volatile/381/1 "2021-04-14T11:20:49Z")

</div>

Hi everyone, I have a problem about the load and store instructions related to volatile global variables.  
I am using the MPU to constrain the accessible memory range of the unprivileged application code.  
However, I find that if there is one global variable labeled as volatile, and this global variable is pointed by a global pointer, then the unprivileged application code will trigger a memory management fault if access this global variable through the pointer, even if the memory range of that global variable is allowed to access by the MPU.  
It is kind of weird to me. Does anybody know how to fix this bug?  
The compiler I use is Clang, and the MCU I use is ARM CortexM4  
Here is the example code:

```auto
volatile uint32_t FLAG = 0;
uint32_t *ptr = &FLAG;

void foo(void) {
    ...
    *ptr = 1;	// Raises an memory management fault here.
    ...
}

int main(void) {
    foo();
}

```

I really appreciate any help you can provide!

---

<div class="post-metadata">

**Author:** ![francois](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@francois](https://community.memfault.com/u/francois)\
**Post date:** [April 14, 2021, 3:51pm UTC](https://community.memfault.com/t/store-load-with-volatile/381/2 "2021-04-14T15:51:18Z")

</div>

Can you dump the values of the fault register here? IIRC mem manage faults are not necessarily MPU related!

---

<div class="post-metadata">

**Author:** ![Spencer](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@Spencer](https://community.memfault.com/u/Spencer)\
**Post date:** [April 15, 2021, 2:24am UTC](https://community.memfault.com/t/store-load-with-volatile/381/3 "2021-04-15T02:24:08Z")

</div>

The value of the CFSR register is 0x82, indicating that its a Data access violation

---

<div class="post-metadata">

**Author:** ![francois](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@francois](https://community.memfault.com/u/francois)\
**Post date:** [April 15, 2021, 4:24am UTC](https://community.memfault.com/t/store-load-with-volatile/381/4 "2021-04-15T04:24:10Z")

</div>

Hmm, there’s nothing obviously wrong with your code. Next I would look at the disassembled code around that access, and dump the MPU registers for us to inspect. Happy to look at those two things if you can provide them!

---

<div class="post-metadata">

**Author:** ![Spencer](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@Spencer](https://community.memfault.com/u/Spencer)\
**Post date:** [April 16, 2021, 9:07am UTC](https://community.memfault.com/t/store-load-with-volatile/381/5 "2021-04-16T09:07:30Z")

</div>

Thanks for your reply Francois! 😃  
After debugging, I confirmed that the keyword `volatile` is nothing to with this bug.  
In this case, the region number is 3, i.e., `MPU->RNR = 3`.  
The RBAR’s value is `0x2001_fd93`, indicating that the start address of region 3 is `0x2001_fd80`.  
The RASR’s value is `0x1306_000f`, indicating that the region size is `0x100`  
Therefore, the region range is `0x2001_fd80<=addr<=0x2001_fe7f`.  
However, when I tried to dereference a pointer that points to `0x2001_fe00`, `0x80` bytes offset to the region start address `0x2001_fd80`, it raises a memory management fault.  
But the dereference to the memory address `0x2001_fdff` is OK.  
In fact, the region size declared by region 3 is halved from `0x100` to `0x80`.  
Is this a hardware error? Does anybody know why this can happen? 🤔 Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![Spencer](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@Spencer](https://community.memfault.com/u/Spencer)\
**Post date:** [April 16, 2021, 12:42pm UTC](https://community.memfault.com/t/store-load-with-volatile/381/6 "2021-04-16T12:42:16Z")

</div>

I find out the root cause…It’s because the start\_address of region 3 doesn’t align to the region size 😂
