[cfe-dev] x86 frame pointer and __builtin_setjmp/__builtin_longjmp

Langmuir, Ben ben.langmuir at intel.com
Wed May 22 11:11:49 PDT 2013


Hi,

I'm trying to understand how __builtin_setjmp/longjmp are supposed to interact with the frame pointer on x86_64.  In particular, what is the expected behavior when the compiler chooses not to use rsp or rbp to address local variables?

When built with Clang, the following program will segfault, but it is fine when built with GCC.  The target is x86_64 linux.

int main(int argc, char *argv[]) {
  void *buf[20];

  __attribute__((__aligned__(64))) char q; // realign the stack
  char *p = __builtin_alloca(argc);                   // dynamic alloca

  if (__builtin_setjmp(buf)) {
    *p = 'p';
    q = 'q';
    return 0;
  }

  asm("movq $0, %rbx");
  __builtin_longjmp(buf, 1);
}

LLVM is choosing to use rbx as a base pointer to access p and q, but during builtin_setjmp, rbx is not saved; when the longjmp is executed rbx may have a garbage value.  GCC on the other hand, is using rbp, which is saved in the jump buffer.  Is this a bug in LLVM, or am I using  __builtin_setjmp/longjmp incorrectly?

Note: I'm explicitly clobbering rbx, but the compiler can clobber it on its own if __builtin_longjmp is called from another function.

Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130522/a6b7cee4/attachment.html>


More information about the cfe-dev mailing list