9

The following code summarizes the problem I have at the moment. My current execution flow is as follows and a I'm running in GCC 4.3.

jmp_buf a_buf;
jmp_buf b_buf;

void b_helper()
{
    printf("entering b_helper");
    if(setjmp(b_buf) == 0)
    {
        printf("longjmping to a_buf");
        longjmp(a_buf, 1);
    }
    printf("returning from b_helper");
    return; //segfaults right here
}
void b()
{
    b_helper();
}
void a()
{
    printf("setjmping a_buf");
    if(setjmp(a_buf) == 0)
    {
        printf("calling b");
        b();
    }
    printf("longjmping to b_buf");
    longjmp(b_buf, 1);
}
int main()
{
    a();
}

The above execution flow creates a segfault right after the return in b_helper. It's almost as if only the b_helper stack frame is valid, and the stacks below it are erased.

Can anyone explain why this is happening? I'm guessing it's a GCC optimization that's erasing unused stack frames or something.

Thanks.

2 Answers 2

16

You can only longjmp() back up the call stack. The call to longjmp(b_buf, 1) is where things start to go wrong, because the stack frame referenced by b_buf no longer exists after the longjmp(a_buf).

From the documentation for longjmp:

The longjmp() routines may not be called after the routine which called the setjmp() routines returns.

This includes "returning" through a longjmp() out of the function.

8
  • 2
    Once a part of the stack is released, it's completely invalid (other function calls, interrupts or whatever might overwrite the memory). Sep 4, 2009 at 23:34
  • 4
    You can think of a longjmp() as an "extended return". A successful longjmp() works like a series of successive returns, unwinding the call stack until it reaches the corresponding setjmp(). Once the call stack frames are unwound, they are no longer valid. This is in contrast to implementations of coroutines (eg. Modula-2) or continuations (eg. Scheme) where the call stack remains valid after jumping somewhere else. C and C++ only support a single linear call stack, unless you use threads where you create multiple independent call stacks. Sep 4, 2009 at 23:37
  • 1
    @jameszhao00 - I think that to have any hope of doing something like what it seems you're after will require dropping into assembly code. Whether it's a good idea to pursue is another question altogether. Sep 4, 2009 at 23:38
  • 2
    @jameszhao00: but if you are looking to do coroutines in C, take a look at chiark.greenend.org.uk/~sgtatham/coroutines.html whcih describes using state variables and switch statements to get a similar effect. Sep 4, 2009 at 23:43
  • 4
    The point is the b_helper() frame is not valid, and b_buf refers to an invalid frame. The fact that execution continues to the end of b_helper() is an accident. Sep 4, 2009 at 23:46
6

The standard says this about longjmp() (7.13.2.1 The longjmp function):

The longjmp function restores the environment saved by the most recent invocation of the setjmp macro in the same invocation of the program with the corresponding jmp_buf argument. If there has been no such invocation, or if the function containing the invocation of the setjmp macro has terminated execution in the interim

with a footnote that clarifies this a bit:

For example, by executing a return statement or because another longjmp call has caused a transfer to a setjmp invocation in a function earlier in the set of nested calls.

So you can't longjmp() back & forth across nested setjmp/longjmp sets.

1
  • If there are multiple nested scopes within a function, and setjmp() is performed within a nested scope, may one legitimately longjmp() back there if one has left the scope with the setjmp() but stayed within the same function? Would that imply that volatile variables in a nested scope are required to hold their values if execution leaves and re-enters that scope (but stays within the function)?
    – supercat
    Nov 3, 2011 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.