[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
Thu Sep 17 02:21:47 PDT 2026
================
@@ -0,0 +1,105 @@
+; Test that BasicAA conservatively returns MayAlias for local allocas in
+; functions containing returns_twice calls (e.g. setjmp/sigsetjmp), to prevent
+; miscompilation via longjmp re-entry paths invisible in the forward CFG.
+;
+; Reproduces: https://github.com/llvm/llvm-project/issues/198967
+;
+; Without the fix, GVN incorrectly concludes that the store through %p_val
+; (which may alias %i via a longjmp re-entry) cannot modify %i, then DSE
+; eliminates the "store i32 13, ptr %i" as dead. The fix makes
+; EarliestEscapeAnalysis conservatively bail out at the point where
+; isNotCapturedBefore would otherwise return true, if the function contains
+; any returns_twice call.
+
+; RUN: opt < %s -aa-pipeline=basic-aa -passes=gvn,dse -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at x = external global i32
+ at ii = external global i32
+ at p = external global ptr
+
+declare void @redo()
+declare void @checkpoint() #0
+
+; Control case: function has the same CFG structure but calls no
+; returns_twice function. GVN/DSE can legally eliminate "store i32 13, ptr %i"
+; because in the forward CFG the only path to if.else does not pass through
+; if.then (where &i is stored to @p). BasicAA correctly returns NoAlias for
+; (%i, %p_val) here.
+;
+; Fix case CHECK directives are listed here so that CHECK-NOT is correctly
+; scoped between the two CHECK-LABELs: it checks that the store is absent in
+; bar_no_returns_twice and present in bar_with_returns_twice.
+;
+; CHECK-LABEL: define void @bar_no_returns_twice(
+; CHECK-NOT: store i32 13, ptr %i
+; CHECK-LABEL: define void @bar_with_returns_twice(
+; CHECK: store i32 13, ptr %i
+define void @bar_no_returns_twice() {
+entry:
+ %i = alloca i32, align 4
+ %x_val = load i32, ptr @x
+ %cond = icmp ne i32 %x_val, 0
+ br i1 %cond, label %if.then, label %if.else
+
+if.then:
+ store ptr %i, ptr @p
+ call void @redo()
+ br label %if.end
+
+if.else:
+ ; Bug: without the fix, GVN sees "store i32 42, ptr %p_val" as
+ ; NoAlias with %i (the capture in if.then is not visible in the forward
+ ; CFG), so it forwards 13 through the load and DSE removes this store.
----------------
midhuncodes7 wrote:
Removed comment
https://github.com/llvm/llvm-project/pull/212297
More information about the cfe-commits
mailing list