Medium
Return prediction after a longjmp out of a deep call chain
caller() calls handler(), which calls setjmp and then descends f1 -> f2 -> f3. Assume every call is a real call instruction (no inlining or tail calls). f3 calls longjmp, which restores the stack pointer to handler's frame and jumps back into it with an indirect jump. Nothing executes a ret for the abandoned frames. handler then returns normally to caller, and caller returns to its own parent. The core predicts returns with a return stack buffer (RSB): every call pushes its return address and every ret pops the top entry as its predicted target. What happens to the returns that follow the longjmp?
main.cpp
#include <csetjmp>
std::jmp_buf env;
void f3() { std::longjmp(env, 1); } // call longjmp pushes an RSB entry too
void f2() { f3(); }
void f1() { f2(); }
int handler() {
if (setjmp(env) == 0) f1();
return 0; // ret back into caller()
}
void caller() { handler(); /* ... */ } // later ret to caller's parent34 of 47 in Computer Architecture