llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang -O3 reuses stack slot for escaped local across setjmp/longjmp

Open IshwaraK opened this issue 6 months ago • 2 comments

On x86_64, Clang -O3 folds two distinct local objects into the same stack slot in the presence of setjmp/longjmp, even though the first object’s address escapes before longjmp and its lifetime continues after control returns.

Minimal source:

  #include <setjmp.h>

  typedef struct {
    int f[100];
  } T;

  void stash(jmp_buf *, T *);
  void use(T *);

  void bar(void) {
    T t;
    jmp_buf j;

    if (setjmp(j) == 0) {
      stash(&j, &t);
    } else {
      T t1;
      use(&t1);
    }
  }

Observed clang -O3 codegen, In the generated x86_64 assembly:

  bar():
          sub     rsp, 600
          mov     rdi, rsp
          call    _setjmp@PLT
          test    eax, eax
          je      .LBB1_1
          lea     rdi, [rsp + 200]
          call    use(T*)@PLT
          add     rsp, 600
          ret
  .LBB1_1:
          mov     rdi, rsp
          lea     rsi, [rsp + 200]
          call    stash(__jmp_buf_tag (*) [1], T*)@PLT
          add     rsp, 600
          ret

Both objects use the same address [rsp + 200]:

  • stash(..., &t) gets [rsp + 200]
  • use(&t1) also gets [rsp + 200]

Why this is wrong The 't' is declared in bar’s outer block, so its lifetime extends until bar returns. After longjmp, execution resumes in the same bar activation record; 't' is still alive when 't1' is created.

Because stash(&j, &t) lets &t escape, the compiler cannot assume that storage is dead or can be reused for another distinct object. Reusing the same slot makes 't1' alias the escaped 't', which changes observable behavior.

This is not a “volatile value after longjmp” issue. Even if t’s stored value is indeterminate after longjmp, its object lifetime still has not ended, and its escaped address must not be recycled for t1.

Expected behavior Clang should assign disjoint storage to t and t1, or otherwise preserve their non-aliasing object identity across the setjmp/longjmp edge.

Comparison GCC -O3 keeps them separate for the same source:

  • t at one stack offset
  • t1 at a different stack offset

So this appears to be a Clang/LLVM optimization bug rather than source ambiguity.

IshwaraK avatar May 08 '26 04:05 IshwaraK

An additional observation is that the miscompile disappears if t is explicitly initialized:

T t = {0};

With that change, Clang -O3 no longer assigns the same stack slot to t and t1. This suggests the bad stack-slot reuse is specifically triggered when t is an uninitialized escaped local across the setjmp/longjmp path

IshwaraK avatar May 08 '26 05:05 IshwaraK

Seems to be related to the recurring StackColoring issue (https://github.com/llvm/llvm-project/issues/109204). The pass has concluded that the two slot ranges do not overlap and has merged them (the stack slot fi#2 is dead and all the %stack.2.t1 uses have been rewritten to %stack.0.t). I guess turning off -stackcoloring-lifetime-start-on-first-use is still not possible nowadays. Perhaps we could just solve this by checking exposesReturnsTwice() for the time being.

antoniofrighetto avatar May 08 '26 10:05 antoniofrighetto