Questions tagged [setjmp]

Anything related to the setjmp() and longjmp() routines in the C standard library (provided by the setjmp.h header file), which provide control flow that modify the usual call and return sequence of a subroutine.

The setjmp and longjmp pair of functions allow for the creation of non-local jumping in C programs—it is essentially a non-local goto that can jump across functions. This is a feature which is basically equivalent to exceptions in later languages, and has actually been used to write exception-like systems for C.

It works by loading the environmental state (jmp_buf) saved by an earlier setjmp over the current state. In the process, it also returns a value at the site of the original setjmp call. In practice, the environmental state is tied in with the stack, and this can cause some trouble.

For example, if the frame in which setjmp returns, then jumping to that particular frame invokes undefined behavior because that frame no longer exists. Another consequence of longjmp is that no stack unwinding occurs, so open files will not be closed and heap variables will not be freed.

In most C standard libraries, it can be found under setjmp.h. It has the call signature:

void longjmp(jmp_buf env, int val);

If you do not understand any of the above, then please do not use longjmp! It is very much a tool that can bite you if you are inexperienced. This is especially true for C++, which has exceptions which are harder to abuse than longjmp is.

175 questions
1
vote
2 answers

Is it safe to longjmp through a try block?

I have the following lua_CFunction, written in C++: int my_function(lua_State* L) { int x = 0; try { x = do_cpp_stuff_that_invokes_lua_API_as_well(); } catch(const std::exception& ex) { lua_pushstring(ex.what().c_str()); …
dennis90
  • 239
  • 1
  • 4
  • 12
1
vote
0 answers

How does environment get saved by setjmp and restored by longjmp?

Here is my experimental code: #include #include static jmp_buf buf; int main() { volatile int b = 3; if (setjmp(buf) != 0) { printf("%d\n", b); return 0; } b = 5; longjmp(buf, 1); } My…
my_question
  • 3,075
  • 2
  • 27
  • 44
1
vote
3 answers

Use of set_jmp/longjmp in C++ is not working

I am trying to implement simple user level thread library in c.when one thread start and this thread call second thread. this second thread run correctly but when it exit program crash.here is my coding.…
yasiriqbal776
  • 406
  • 3
  • 15
1
vote
1 answer

How to restore stack frame in gcc?

I want to build my own checkpoint library. I'm able to save the stack frame to a file calling checkpoint_here(stack pointer) and that can be restored later via calling recover(stack pointer) function. Here is my problem: I'm able to jump from…
KKR
  • 11
  • 1
1
vote
2 answers

Exception handling in C - making try catch work across functions

I am writing an exception handling library in C and i ran into a bump: #define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: #define FINALLY break; } default: #define CATCH(x) break; case x: #define ETRY } }while(0) #define THROW(x)…
Jean-Luc Nacif Coelho
  • 1,006
  • 3
  • 14
  • 30
1
vote
2 answers

C macros with opening and closing tags?

I just started reading this article about exception handling in c with the use of setjmp( jmp_buf ) and longjmp( jmp_buf, int ). So I basically build the linked list that uses the local variables from type xRecord and links it to the list. (Example…
Redweasel
  • 29
  • 4
0
votes
1 answer

GDB crashing in Eclipse on longjmp

I am using c in eclipse to write a program. I need to use GDB to debug it step by step. However on a longjmp command (when i try to save the context of a stack) GDB crashes inside eclipse (because there is a breakpoint there?) Is there any way…
Falcata
  • 679
  • 1
  • 15
  • 23
0
votes
1 answer

setjmp / longjmp does not jump where I think it should

I would like to understand how setjmp / longjmp works, so I created an example program, where routineA prints even, routineB prints odd numbers and they jump to each other with longjmp: #include #include #define COUNTER_BEGIN…
z32a7ul
  • 3,695
  • 3
  • 21
  • 45
0
votes
1 answer

alternative to mangling jmp_buf in c for a context switch

In setjmp.h library in linux system jmp_buf is encrypted to decrypt it we use mangle function */static long int i64_ptr_mangle(long int p) { long int ret; asm(" mov %1, %%rax;\n" " xor %%fs:0x30, %%rax;" " rol $0x11, %%rax;" …
0
votes
1 answer

setjmp/longjmp and the state of open files

The C standard says that All accessible objects have values, and all other components of the abstract machine218) have state, as of the time the longjmp function was called, except that the values of objects of automatic …
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
0
votes
1 answer

call empty function with address of local variable before setjmp, what for?

I read code of C library and can not understand what is going on: struct Foo *foo = NULL; lib_var((void *)&foo); if (setjmp(get_jmp_buf()) == 0) { foo = ...; // other calculation that may cause longjmp } else { //something bad happens …
user1244932
  • 7,352
  • 5
  • 46
  • 103
0
votes
1 answer

Assembly early return on a recursive function

This is more an academic exercise than anything else, but I'm looking to write a recursive function in assembly, that, if it receives and "interrupt signal" it returns to the main function, and not just the function that invoked it (which is usually…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
0
votes
1 answer

C difference between main thread and other threads

Is there a difference between the first thread and other threads created during runtime. Because I have a program where to abort longjmp is used and a thread should be able to terminate the program (exit or abort don't work in my case). Could I…
0
votes
1 answer

custom setjmp/longjmp implemetation segmentation fault

I'm trying to implement custom setjmp/longjmp for x64 windows. I have following code: contextSave: xorq %rdx,%rdx movq %rdx,(%rcx) movq %rbx,0x8(%rcx) leaq 0x8(%rsp),%rax movq %rax,0x10(%rcx) movq %rbp,0x18(%rcx) movq %rsi,0x20(%rcx) …
Risa123
  • 1
  • 3
0
votes
1 answer

How to use longjmp from main function to other functions?

#include #include jmp_buf jmp; int test() { setjmp(jmp); puts("Birds are crying"); return 0; } int main() { longjmp(jmp,1); return 0; } Above code doesn't work and crashes, why? I am using the GCC…