[clang] [llvm] [BasicAA] Fix miscompilation with setjmp/longjmp due to missing longjmp re-entry paths in alias analysis (PR #212297)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 14 03:23:10 PDT 2026
================
@@ -257,7 +257,17 @@ CaptureComponents EarliestEscapeAnalysis::getCapturesBefore(
return isNotInCycle(I, &DT, LI, CI);
}
- return !isPotentiallyReachable(CaptureInst, I, nullptr, &DT, LI, CI);
+ if (isPotentiallyReachable(CaptureInst, I, nullptr, &DT, LI, CI))
+ return false;
+
+ // A `longjmp` may re-enter the function at any `returns_twice` call
+ // (e.g. `setjmp`), If the function contains such a call, conservatively
+ // treat the object as captured.
+ if (DT.getRoot()->getParent()->hasFnAttribute(
+ Attribute::ContainsReturnsTwiceCall))
+ return false;
----------------
midhuncodes7 wrote:
I checked something that sharpens it further: the three other callers of `callsFunctionThatReturnsTwice()` (CFGuardLongjmp.cpp, TailRecursionElimination.cpp, ObjCARCContract.cpp) each call it exactly once per function-pass invocation, not in a per-query hot loop. So they were never actually paying the cost the attribute was meant to solve. The only hot caller is `EarliestEscapeAnalysis`, which already caches other per-function state (EarliestEscapes, Inst2Obj) - a lazily computed cached bool there would give the same O(1) win with none of the Clang/Attributes.td/bitcode/CodeExtractor surface.
@nikic @efriedma-quic @hubert-reinterpretcast - since the `contains_returns_twice_call` direction came out of our discussion, wanted to get your read before reworking: keep the attribute as-is (it does buy amortization across pass runs, at the cost of the wider surface above), or switch to the EEA local cache?
https://github.com/llvm/llvm-project/pull/212297
More information about the cfe-commits
mailing list