Issue 237976
Summary [ASan] invoke of a returns_twice function (setjmp) does not disable fake stack / dynamic-alloca frame
Labels new issue
Assignees
Reporter zjy-dev
    `FunctionStackPoisoner` must not use stack-malloc (fake stack) or a dynamic `MyAlloca` frame in a
function with a `returns_twice` call (AddressSanitizer.cpp:3636-3643). The comment there says it
"doesn't play well with introduced indirect register-relative calculation of local variable
addresses". `visitCallBase` sets `HasReturnsTwiceCall` only for `CallInst`, so a `setjmp` that
clang emits as `invoke` (any C++ scope with a cleanup or `try`) is ignored. Such a function then
gets the fake-stack frame.

```cpp
typedef long jmp_buf[48];
extern "C" int setjmp(jmp_buf) __attribute__((returns_twice));
void use(int *);
struct Guard { ~Guard(); };

int with_cleanup(jmp_buf jb) { int a[8]; use(a); Guard g; return setjmp(jb); } // invoke
int no_cleanup(jmp_buf jb) { int a[8]; use(a);          return setjmp(jb); } // call
```

Compile flag: `clang++ --target=x86_64-linux-gnu -O1 -fsanitize=address -fsanitize-address-use-after-return=always -S -emit-llvm`

```llvm
define i32 @_Z12with_cleanupPl(ptr %jb) personality ptr @__gxx_personality_v0
  %0 = call i64 @__asan_stack_malloc_always_1(i64 128)   ; fake stack used
 %MyAlloca = alloca i8, i64 128, align 32               ; dynamic frame
 %call = invoke i32 @setjmp(ptr noundef %jb)
define i32 @_Z10no_cleanupPl(ptr %jb)                     ; control
  %MyAlloca = alloca [96 x i8], align 32 ; static frame, no stack_malloc
  %call = call i32 @setjmp(ptr noundef %jb)
```

The same happens with the default `-fsanitize-address-use-after-return=runtime`
(`__asan_stack_malloc_1`) and on aarch64. Godbolt clang trunk (2026-11-01) gives the same output.

**Root cause.** `llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp:1283-1288`:

```cpp
 void visitCallBase(CallBase &CB) {
    if (CallInst *CI = dyn_cast<CallInst>(&CB)) {
      HasInlineAsm |= CI->isInlineAsm() && &CB != ASan.LocalDynamicShadow;
      HasReturnsTwiceCall |= CI->canReturnTwice();
    }
  }
```

`canReturnTwice()` is defined on `CallBase`, and `InvokeInst` reaches `visitCallBase`, but it is filtered out
by the `CallInst` cast. Only the plain-call form is tested
(`llvm/test/Instrumentation/AddressSanitizer/stack_dynamic_alloca.ll`, `@Func3`).

**Suggested fix.** `HasReturnsTwiceCall |= CB.canReturnTwice();` for every `CallBase` (keep the
`CallInst` cast only for `isInlineAsm`), and add an `invoke` variant of `@Func3`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to