Skip to content

Commit 0547e84

Browse files
authored
[FunctionAttrs] Bail if initializes range overflows 64-bit signed int (#137053)
Otherwise the range doesn't make sense since we interpret it as signed. Fixes #134115
1 parent 6388a7a commit 0547e84

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,13 @@ ArgumentAccessInfo getArgumentAccessInfo(const Instruction *I,
661661
auto TypeSize = DL.getTypeStoreSize(Ty);
662662
if (!TypeSize.isScalable() && Offset) {
663663
int64_t Size = TypeSize.getFixedValue();
664-
return ConstantRange(APInt(64, *Offset, true),
665-
APInt(64, *Offset + Size, true));
664+
APInt Low(64, *Offset, true);
665+
bool Overflow;
666+
APInt High = Low.sadd_ov(APInt(64, Size, true), Overflow);
667+
// Bail if the range overflows signed 64-bit int.
668+
if (Overflow)
669+
return std::nullopt;
670+
return ConstantRange(Low, High);
666671
}
667672
return std::nullopt;
668673
};

llvm/test/Transforms/FunctionAttrs/initializes.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,17 @@ define void @memset_offset_1_size_0(ptr %dst, ptr %src) {
635635
call void @llvm.memmove.p0.p0.i64(ptr %dst.1, ptr %src, i64 0, i1 false)
636636
ret void
637637
}
638+
639+
; We should bail if the range overflows a singed 64-bit int.
640+
define void @range_overflows_signed_64_bit_int(ptr %arg) {
641+
; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write)
642+
; CHECK-LABEL: define void @range_overflows_signed_64_bit_int(
643+
; CHECK-SAME: ptr writeonly captures(none) [[ARG:%.*]]) #[[ATTR0]] {
644+
; CHECK-NEXT: [[GETELEMENTPTR:%.*]] = getelementptr i8, ptr [[ARG]], i64 9223372036854775804
645+
; CHECK-NEXT: store i32 0, ptr [[GETELEMENTPTR]], align 4
646+
; CHECK-NEXT: ret void
647+
;
648+
%getelementptr = getelementptr i8, ptr %arg, i64 9223372036854775804
649+
store i32 0, ptr %getelementptr
650+
ret void
651+
}

0 commit comments

Comments
 (0)