carmreentrancy

Is register value corruption a possibility on reentrant functions in ARM C?


Maybe I'm overthinking this since I've been researching for a couple of hours. I have the concept and rules of reentrancy pretty clear now but since I'm doing this for ARM (Cortex-M4), another question came to mind that is not touched on the reentrancy resources I've found, nor in its rules.

I understand that the following function is reentrant from the rules of reentrancy:

void foo(int x, int y)
{
    printf("x = %d - y = %d", x, y);
}

The thing here is that, at least on ARM where I've verified this, x and y are passed on registers r2 and r3 and not in foo's stack. If they were passed in the stack then there would not be any confusion for me because I know that is preserved across function calls, however, since x and y are passed in registers, what would happen if a second call to foo is made from another RTOS task context right after the first call has been made?

void foo(int x, int y)
{ <-------------------------------------------- Second call made here
    printf("x = %d - y = %d\n", x, y);
}

Wouldn't that corrupt r2 and r3 values?

I haven't been able to test this because I have no idea how to make it happen.


Solution

  • Whenever the operating system switches to another thread it has to save all the register values from the old thread and load the register values for the new thread.