Issue 240681
Summary [HWASan/MTE] StackInfoBuilder::visit Uses dyn_cast<CallInst> Instead of CallBase for canReturnTwice(), Leaving Scoped Stack Allocas Tagged After invoke @setjmp + longjmp
Labels new issue
Assignees
Reporter zjy-dev
    Both HWAddressSanitizer (`HWAddressSanitizer.cpp:1537-1548`) and AArch64 MTE stack tagging (`AArch64StackTagging.cpp:602-612`) rely on `memtag::StackInfoBuilder::visit` (`llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp:122-126`) to set `Info.CallsReturnTwice = true` whenever a function calls `setjmp`/`sigsetjmp`, forcing all tagged stack allocas to be untagged at every `ret` instruction (`SInfo.RetVec`) rather than only at `llvm.lifetime.end` (which `longjmp` can bypass). However, `StackInfoBuilder::visit` writes `if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { if (CI->canReturnTwice()) Info.CallsReturnTwice = true; }`. When `setjmp` is called inside a C++ `try` block without `noexcept`, Clang emits `invoke i32 @setjmp(...)` (`InvokeInst`, a subclass of `CallBase`, not `CallInst`). `dyn_cast<CallInst>` fails, leaving `Info.CallsReturnTwice = false`. Consequently, scoped stack allocas entered after `invoke @setjmp` are untagged only at their lexical `llvm.lifetime.end` and NOT at function return on the `setjmp != 0` (`longjmp`) path, leaving stale non-zero HWASan/MTE tags on the stack after the function returns.

### Reproducer (`trigger.cpp`)

```cpp
typedef long jmp_buf[32];
extern "C" __attribute__((returns_twice)) int setjmp(jmp_buf env);
extern "C" void may_throw(int *p, jmp_buf env);
int with_invoke(int x) {
  jmp_buf env;
  try {
    if (setjmp(env) == 0) {
      int scoped_buf[16];
      scoped_buf[0] = x;
      may_throw(scoped_buf, env);
    }
  } catch (...) {
    return -1;
  }
  return 0;
}
```

Compile / reproduction command (`['--target=aarch64-unknown-linux-gnu', '-fsanitize=hwaddress', '-fexceptions', '-fcxx-exceptions', '-O1', '-S', '-emit-llvm']`):

```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon /opt/llvm23/bin/clang++ --target=aarch64-unknown-linux-gnu -fsanitize=hwaddress -fexceptions -fcxx-exceptions -O1 -x c++ - -S -emit-llvm -o -
```

Observed output on LLVM 23.1.2:

```text
%38 = invoke i32 @setjmp(ptr noundef nonnull %25) #12
          to label %39 unwind label %53
39: ; preds = %1
  %40 = icmp eq i32 %38, 0
  br i1 %40, label %41, label %66
41:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(ptr nonnull %26) #11
 ...
  call void @llvm.memset.p0.i64(ptr align 1 %46, i8 %42, i64 4, i1 false)
...
66:                                               ; preds = %47, %39, %62
  %67 = phi i32 [ -1, %62 ], [ 0, %39 ], [ 0, %47 ]
  ; Only %19 (env) is untagged here; %26 (scoped_buf) is left tagged on the %39 -> %66 longjmp return path!
  call void @llvm.memset.p0.i64(ptr align 1 %72, i8 %68, i64 16, i1 false)
  call void @llvm.lifetime.end.p0(ptr nonnull %19) #11
  ret i32 %67
```

### Expected behavior

`StackInfoBuilder::visit` in `MemoryTaggingSupport.cpp:122` must use `dyn_cast<CallBase>(&Inst)` instead of `dyn_cast<CallInst>(&Inst)` so that `InvokeInst` calls to `returns_twice` functions set `Info.CallsReturnTwice = true` and untag all instrumented stack allocas at every function return.

### Root cause & impact

- **Source location**: `llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp:122-126; llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:1537-1563; llvm/lib/Target/AArch64/AArch64StackTagging.cpp:602-612`
- **Impact**: Leaves stale HWASan shadow tags or AArch64 hardware MTE allocation tags on the stack after a function returns via `longjmp` to an `invoke @setjmp` site, causing spurious tag-mismatch crashes (`SIGABRT` / `SIGSEGV` `SEGV_MTESERR`) in subsequent stack frames that reuse that stack memory.

### Suggested fix

In `llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp:122`, change `if (CallInst *CI = dyn_cast<CallInst>(&Inst))` to `if (CallBase *CB = dyn_cast<CallBase>(&Inst))`.

Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `aarch64, riscv64, x86_64`.

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

Reply via email to