Norg74
Norg74

Reputation: 276

Exactly what "program state" does setjmp save?

I've read that setjmp "saves the program state" in the passed-in jmp_buf variable, but I haven't found any description of exactly what that entails. Does it make a copy of all the application's memory? Just the registers? The stack?

Upvotes: 10

Views: 2877

Answers (4)

embedded_guy
embedded_guy

Reputation: 1967

The following is from C in a Nutshell by Peter Prinz and Tony Crawford:

The setjmp() macro saves the current environment at the time of the call in a buffer specified by its argument. The environment includes the stack, and with it all variables that have automatic storage duration.

Here is what ISO/IEC 9899:TC2 has to say in section 7.13:

The environment of a call to the setjmp macro consists of information sufficient for a call to the longjmp function to return execution to the correct block and invocation of that block, were it called recursively. It does not include the state of the floating-point status flags, of open files, or of any other component of the abstract machine.

Here is an interesting reference by P.J. Plauger in his book, The Standard C Library:

One of the dangers [of implementing setjmp] lies in expression evaluation. A typical computer has some number of registers that it uses to hold intermediate results while evaluating an expression. Write a sufficiently complex expression, however, and you may exhaust the available registers... setjmp must guess how much "calling context" to store in the jmp_buf data object. It is a safe bet that certain registers must be saved.

And finally, from Expert C Programming by Peter Van Der Linden.

Setjmp saves a copy of the program counter and the current pointer to the top of the stack.

Based on the above information, it looks to me like the "current environment" is left up to the implementation.

Upvotes: 5

dohashi
dohashi

Reputation: 1841

From the setjmp manpage

   setjmp()  and  longjmp(3)  are  useful for dealing with errors and interrupts encountered in a
   low-level subroutine of a program.  setjmp() saves the stack context/environment  in  env  for
   later  use  by longjmp(3).  The stack context will be invalidated if the function which called
   setjmp() returns.

Essentially, it remembers the current stack location and register state. When you call longjmp, you jump back to the same program counter and stack location with some additional registers are restored.

These are often referred to as "non-local gotos". They are not like a fork that would copy memory state or anything like that.

Upvotes: 1

david.pfx
david.pfx

Reputation: 10853

The setjmp() function saves the contents of most of the general purpose registers, in the same way as they would be saved on any function entry. It also saves the stack pointer and the return address. All these are placed in the buffer. It then arranges for the function to return zero.

The longjmp() function restores the general purpose registers and the stack pointer and then does a jump to the previously saved return address. In practice it may do this explicitly, or by setting up the stack and executing a normal function return. On this occasion the function returns a non-zero value.

The principle is the same, but the detail has varied quite a bit across the many different CPUs I have encountered.

Upvotes: 1

jtlim
jtlim

Reputation: 4339

It's just the registers that need to be preserved across a function call according to the platforms ABI.

Source: disassembling setjmp on x86, x64, arm32, arm64 on various operating systems.

Upvotes: 5

Related Questions