Fix HAL MPU test on Cortex M7 CPUs #418
                
     Merged
            
            
          
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Summary of changes
Did some checking into finally fixing this annoying test failure that has been happening for years. The hal-mpu stack fault test has been dying on most MCUs 100% of the time, and it wasn't super obvious why.
When debugging the test, I noticed that it was dying because it was executing a garbage instruction (NOT because it was executing from RAM -- that's an intended part of the test). Why was this happening? Well, stepping through the disassembly of
mpu_fault_test_stack(), it looked like the compiler had reordered the instructions, and was executing line 141 before line 147! So it was executing the RAM instruction before said instruction had been initialized with a value.Why was that happening? Well, I think that the cast that this test does breaks aliasing rules in C++ -- it's casting an integer into a function pointer, and the optimizer is allowed by the standard to not have to consider the case where a function pointer points to a regular variable. So it does not make the connection that the argument to
mpu_fault_test()is actually used by said function.Luckily, fixing this is pretty straightforward. We just have to mark the variable as volatile (and apply the same fix to
mpu_fault_test_heap()). That way the compiler isn't allowed to reorder the access in this manner.Impact of changes
Migration actions required
Documentation
None
Pull request type
Test results