Exactly what "state of the program" does setjmp save do?

I read that setjmp "saves the state of the program" in the jmp_buf variable, but I did not find a description of what exactly this entails. Does it make a copy of the entire application memory? Only registers? Stack?

+8
c longjmp setjmp
source share
4 answers

These are just registers that need to be kept during function call in accordance with ABI platforms.

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

+3
source share

The following is a brief summary of C : Peter Prince and Tony Crawford:

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

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

The setjmp macro call environment consists of enough information to call the longjmp function to return to the correct block and call this block if it were called recursively. It does not include the state of floating point status flags, open files, or any other component of an abstract machine.

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

One of the dangers [of the setjmp implementation] is evaluating the expression. A typical computer has a number of registers that it uses to store intermediate results when evaluating an expression. However, write a rather complicated expression and you can run out of available registers ... setjmp should guess how much the β€œcall context" to store in the jmp_buf data jmp_buf . It is a safe bet that certain registers should be kept.

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 seems to me that the "current environment" remains before implementation.

+2
source share

The setjmp () function saves the contents of most general registers in the same way as they will be stored in any function entry. It also stores the stack pointer and return address. All of them are buffered. Then it arranges the function to return zero.

The longjmp () function restores the general registers and the stack pointer, and then jumps to the previously saved return address. In practice, this can be done explicitly, or by setting the stack and performing the normal return function. In this case, the function returns a nonzero value.

The principle is the same, but the details have slightly changed across the many different processors that I have encountered.

+1
source share

From the setjmp man page

  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 location of the stack and the registration status. When you call longjmp, you return back to the same program counter, and the stack location with some additional registers is restored.

They are often referred to as "non-local gotos". They do not look like a plug that copies the state of memory or something like that.

0
source share

All Articles