Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setjmp/longjmp and local variables

Tags:

c

My questions aims at the behaviour of setjmp/longjmp concerning local variables.

Example code:

jmp_buf env;   void abc() {   int error;    ...    if(error)     longjmp(env); }   void xyz() {   int v1;           // non-volatile; changed between setjmp and longjmp   int v2;           // non-volatile; not changed between setjmp and longjmp   volatile int v3;  // volatile;     changed between setjmp and longjmp   volatile int v4;  // volatile;     not changed between setjmp and longjmp     ...    if(setjmp(env)) {     // error handling     ...     return;   }    v1++; // change v1   v3++; // change v3    abc(); }   int main(...) {   xyz(); } 

The documentation of setjmp/longjmp says:

"All accessible objects have values as of the time longjmp() was called, except that the values of objects of automatic storage duration which are local to the function containing the invocation of the corresponding setjmp() which do not have volatile-qualified type and which are changed between the setjmp() invocation and longjmp() call are indeterminate."

I see following two possible interpretations:

intepretation1:

Local variables are restored, except those that are both

  • non-volatile and
  • changed

intepretation2:

Local variables are restored, except

  • those that are non-volatile and
  • those that are changed

According to interpretation1 after longjmp only v1 is undefined. v2, v3, v4 are defined. According to interpretation2 after longjmp only v4 is defined. v1, v2, v3 are undefined.

Which one is right?

BTW: I need a general ("portable") answer that is valid for all compilers, i.e. trying out with one particular compiler doesn't help.

like image 496
Curd Avatar asked Sep 08 '09 11:09

Curd


1 Answers

setjmp/longjmp is implemented by saving the registers (including stack and code pointers etc) when first passed, and restoring them when jumping.

Automatic (aka "local", stack-allocated) variables that are not 'volatile' may be stored in registers rather than on the stack.

In these circumstances, the longjmp will restore these registers variables to their value at the point when the setjmp() was first called.

Additionally, a particularly clever compiler might avoid variables that can be inferred from the state of another variable, and calculating them on demand.

However, if the variable is automatic but not been assigned a register, it may be changed by code between the setjmp and the longjmp..

Volatile is explicitly telling the compiler not to store the variable in a register.

So unless you explicitly say a variable is volatile, if you changed the variable between the setjmp/longjmp, its value will depend upon the choices the compiler makes, and is therefore nothing you should rely upon ('indeterminate').

like image 64
Will Avatar answered Sep 25 '22 12:09

Will