Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BasicAA] Gracefully handle large LocationSize #138528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
Merged

Conversation

nikic
Copy link
Contributor

@nikic nikic commented May 5, 2025

If the LocationSize is larger than the index space of the pointer type, bail out instead of triggering an APInt assertion.

Fixes the issue reported at #119365 (comment).

If the LocationSize is larger than the index space of the pointer
type, bail out instead of triggering an APInt assertion.
@llvmbot
Copy link
Member

llvmbot commented May 5, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Nikita Popov (nikic)

Changes

If the LocationSize is larger than the index space of the pointer type, bail out instead of triggering an APInt assertion.

Fixes the issue reported at #119365 (comment).


Full diff: https://github.com/llvm/llvm-project/pull/138528.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/BasicAliasAnalysis.cpp (+5-3)
  • (added) llvm/test/Analysis/BasicAA/size-overflow.ll (+14)
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index 2de9bb502baf4..30222b87ea467 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1237,8 +1237,11 @@ AliasResult BasicAAResult::aliasGEP(
   if (V1Size.isScalable() || V2Size.isScalable())
     return AliasResult::MayAlias;
 
-  // We need to know both acess sizes for all the following heuristics.
-  if (!V1Size.hasValue() || !V2Size.hasValue())
+  // We need to know both access sizes for all the following heuristics. Don't
+  // try to reason about sizes larger than the index space.
+  unsigned BW = DecompGEP1.Offset.getBitWidth();
+  if (!V1Size.hasValue() || !V2Size.hasValue() ||
+      !isUIntN(BW, V1Size.getValue()) || !isUIntN(BW, V2Size.getValue()))
     return AliasResult::MayAlias;
 
   APInt GCD;
@@ -1293,7 +1296,6 @@ AliasResult BasicAAResult::aliasGEP(
 
   // Compute ranges of potentially accessed bytes for both accesses. If the
   // interseciton is empty, there can be no overlap.
-  unsigned BW = OffsetRange.getBitWidth();
   ConstantRange Range1 = OffsetRange.add(
       ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
   ConstantRange Range2 =
diff --git a/llvm/test/Analysis/BasicAA/size-overflow.ll b/llvm/test/Analysis/BasicAA/size-overflow.ll
new file mode 100644
index 0000000000000..18791ba20ef5f
--- /dev/null
+++ b/llvm/test/Analysis/BasicAA/size-overflow.ll
@@ -0,0 +1,14 @@
+; RUN: opt -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
+
+target datalayout = "p:32:32"
+
+; Make sure that using a LocationSize larget than the index space does not
+; assert.
+
+; Just Mod:  Ptr: i32* %gep	<->  call void @llvm.memset.p0.i64(ptr %p, i8 0, i64 68719476736, i1 false)
+define void @test(ptr %p, i32 %idx) {
+  %gep = getelementptr i8, ptr %p, i32 %idx
+  load i32, ptr %gep
+  call void @llvm.memset.i64(ptr %p, i8 0, i64 u0x100000000, i1 false)
+  ret void
+}

; Make sure that using a LocationSize larget than the index space does not
; assert.

; Just Mod: Ptr: i32* %gep <-> call void @llvm.memset.p0.i64(ptr %p, i8 0, i64 68719476736, i1 false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing CHECK below?

Suggested change
; Just Mod: Ptr: i32* %gep <-> call void @llvm.memset.p0.i64(ptr %p, i8 0, i64 68719476736, i1 false)
; CHECK: Just Mod: Ptr: i32* %gep <-> call void @llvm.memset.p0.i64(ptr %p, i8 0, i64 68719476736, i1 false)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, looks like I never actually ran the new test...

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@nikic nikic merged commit 027b203 into llvm:main May 6, 2025
9 of 11 checks passed
@nikic nikic deleted the basicaa-apint-fix branch May 6, 2025 12:19
@nikic nikic added this to the LLVM 20.X Release milestone May 6, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status May 6, 2025
@nikic
Copy link
Contributor Author

nikic commented May 6, 2025

/cherry-pick 027b203

@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

/pull-request #138681

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status May 6, 2025
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
If the LocationSize is larger than the index space of the pointer type,
bail out instead of triggering an APInt assertion.

Fixes the issue reported at
llvm#119365 (comment).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

3 participants