Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading