Skip to content

Commit

Permalink
Add tests to reproduce pointer/index width confusion crashes
Browse files Browse the repository at this point in the history
Some calls to GEPOperator::accumulateConstantOffset(APInt) passed the
pointer bitwidth as the width of the APInt, while the function asserts
that the width of its argument is equal to the index width of the GEP
pointer input. These values are almost always the same, so mixing up
which call to use doesn't usually cause issues. However, when dealing
with data layouts where these values are different, the passes tested
here can crash.

This will be fixed in D143437 .

Differential Revision: https://reviews.llvm.org/D144673
  • Loading branch information
krzysz00 committed Feb 23, 2023
1 parent f78c343 commit 5a4f193
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions llvm/test/Transforms/LowerTypeTests/distinct-index-width-crash.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; RUN: not --crash opt -passes=lowertypetests %s -disable-output
target datalayout = "e-p:64:64:64:32"

@a = constant i32 1, !type !0
@b = constant [2 x i32] [i32 2, i32 3], !type !1

!0 = !{i32 0, !"typeid1"}
!1 = !{i32 4, !"typeid1"}

declare i1 @llvm.type.test(ptr %ptr, metadata %bitset) nounwind readnone

; CHECK: @bar(
define i1 @bar() {
; CHECK: ret i1 true
%x = call i1 @llvm.type.test(ptr getelementptr ([2 x i32], ptr @b, i32 0, i32 1), metadata !"typeid1")
ret i1 %x
}
13 changes: 13 additions & 0 deletions llvm/test/Transforms/MergeFunc/different-index-width-gep-crash.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; RUN: not --crash opt -disable-output -passes=mergefunc -S < %s

target datalayout = "e-p:64:64-p2:128:128:128:32"

define void @foo(ptr addrspace(2) %x) {
%tmp = getelementptr i32, ptr addrspace(2) %x, i32 1
ret void
}

define void @bar(ptr addrspace(2) %x) {
%tmp = getelementptr i32, ptr addrspace(2) %x, i32 1
ret void
}
32 changes: 32 additions & 0 deletions llvm/test/Transforms/MergeICmps/X86/distinct-index-width-crash.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; RUN: not --crash opt < %s -passes=mergeicmps -verify-dom-info -disable-output

target triple = "x86_64"

; This is very much not an x86 ABI, in current use, but we're testing
; that we've fixed a bug where accumulateConstantOffset() was called incorrectly.
target datalayout = "e-p:64:64:64:32"

; Define a cunstom data layout that has index width < pointer width
; and make sure that doesn't mreak anything
define void @fat_ptrs(ptr dereferenceable(16) %a, ptr dereferenceable(16) %b) {
bb0:
%ptr_a1 = getelementptr inbounds [2 x i64], ptr %a, i64 0, i64 1
%ptr_b1 = getelementptr inbounds [2 x i64], ptr %b, i64 0, i64 1
br label %bb1

bb1: ; preds = %bb0
%a0 = load i64, ptr %a
%b0 = load i64, ptr %b
%cond0 = icmp eq i64 %a0, %b0
br i1 %cond0, label %bb2, label %bb3

bb2: ; preds = %bb1
%a1 = load i64, ptr %ptr_a1
%b1 = load i64, ptr %ptr_b1
%cond1 = icmp eq i64 %a1, %b1
br label %bb3

bb3: ; preds = %bb2, %bb1
%necessary = phi i1 [ %cond1, %bb2 ], [ false, %bb1 ]
ret void
}

0 comments on commit 5a4f193

Please sign in to comment.