Skip to content

Commit bd45077

Browse files
Merge pull request #5158 from adrian-prantl/98799670
Fix UB in DIExpression::appendOffset()
2 parents b672c77 + 0420579 commit bd45077

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,10 @@ void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops,
13851385
Ops.push_back(Offset);
13861386
} else if (Offset < 0) {
13871387
Ops.push_back(dwarf::DW_OP_constu);
1388-
Ops.push_back(-Offset);
1388+
// Avoid UB when encountering LLONG_MIN, because in 2's complement
1389+
// abs(LLONG_MIN) is LLONG_MAX+1.
1390+
uint64_t AbsMinusOne = -(Offset+1);
1391+
Ops.push_back(AbsMinusOne + 1);
13891392
Ops.push_back(dwarf::DW_OP_minus);
13901393
}
13911394
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; RUN: opt %s -sroa -early-cse -S | FileCheck %s
2+
; CHECK: DIExpression(DW_OP_constu, 9223372036854775808, DW_OP_minus, DW_OP_stack_value)
3+
; Created from the following C input (and then delta-reduced the IR):
4+
;
5+
; extern unsigned long long use(unsigned long long);
6+
; void f(unsigned long long x) {
7+
; for (; x > 0; x --) {
8+
; unsigned long long y = x + 0x8000000000000000;
9+
; use(x);
10+
; }
11+
; }
12+
13+
define void @f(i64 noundef %x) #0 !dbg !9 {
14+
entry:
15+
%x.addr = alloca i64, align 8
16+
%y = alloca i64, align 8
17+
br label %for.cond
18+
for.cond: ; preds = %for.inc, %entry
19+
%0 = load i64, i64* %x.addr, align 8
20+
%1 = bitcast i64* %y to i8*
21+
call void @llvm.dbg.declare(metadata i64* %y, metadata !15, metadata !DIExpression())
22+
, !dbg !29
23+
%2 = load i64, i64* %x.addr, align 8
24+
%add = add i64 %2, -9223372036854775808
25+
store i64 %add, i64* %y, align 8
26+
br label %for.cond
27+
}
28+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
29+
30+
!llvm.module.flags = !{!3,!7}
31+
32+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None, sysroot: "/")
33+
!1 = !DIFile(filename: "t.c", directory: "/")
34+
!3 = !{i32 2, !"Debug Info Version", i32 3}
35+
!7 = !{i32 7, !"frame-pointer", i32 2}
36+
!9 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 2, type: !10, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !13)
37+
!10 = !DISubroutineType(types: !11)
38+
!11 = !{null, !12}
39+
!12 = !DIBasicType(name: "unsigned long long", size: 64, encoding: DW_ATE_unsigned)
40+
!13 = !{}
41+
!15 = !DILocalVariable(name: "y", scope: !16, file: !1, line: 4, type: !12)
42+
!16 = distinct !DILexicalBlock(scope: !17, file: !1, line: 3, column: 23)
43+
!17 = distinct !DILexicalBlock(scope: !18, file: !1, line: 3, column: 3)
44+
!18 = distinct !DILexicalBlock(scope: !9, file: !1, line: 3, column: 3)
45+
!29 = !DILocation(line: 4, column: 24, scope: !16)

0 commit comments

Comments
 (0)