Search code examples
ctestingembeddedfault

Inject a memory exception by accessing forbidden memory location


I want to test a exception handler function that I have written for an embedded system and want to write a test code that injects an access to memory that forbidden.

void Test_Mem_exception
    {
    __asm(
    "LDR R0, =0xA0100000\n\t"
    "MOV R1, 0x77777777\n\t"
    "STR R1, [R0,#0]"
);

This is the code I want to write that access memory location at 0xA010000. Somehow this does not seem a generic test code to me.

Is there a standard way of writing such test codes in C or C++. By Generic I mean a code that is independent of the memory map of the system that it runs on.


Solution

  • I wouldn't use asm for this, simply use a pointer.

    void Test_Mem_exception
    {
      /* volatile, to suppress optimizing/removing the read statement */
      volatile int *ptr = 0xC0C0C0C0;
      int value = *ptr;
    }
    

    This won't always result to an exception, because reading from address 0 can be valid on some systems.

    The same applies to any other address, there doesn't exist any address that will fail on all systems.