Skip to content

Commit d2b6665

Browse files
committed
[DebugInfo] Avoid adding too much indirection to pointer-valued variables
This patch addresses PR41675, where a stack-pointer variable is dereferenced too many times by its location expression, presenting a value on the stack as the pointer to the stack. The difference between a stack *pointer* DBG_VALUE and one that refers to a value on the stack, is currently the indirect flag. However the DWARF backend will also try to guess whether something is a memory location or not, based on whether there is any computation in the location expression. By simply prepending the stack offset to existing expressions, we can accidentally convert a register location into a memory location, which introduces a suprise (and unintended) dereference. The solution is to add DW_OP_stack_value whenever we add a DIExpression computation to a stack *pointer*. It's an implicit location computed on the expression stack, thus needs to be flagged as a stack_value. For the edge case where the offset is zero and the location could be a register location, DIExpression::prepend will still generate opcodes, and thus DW_OP_stack_value must still be added. Differential Revision: https://reviews.llvm.org/D63429 llvm-svn: 364736
1 parent 9d34f45 commit d2b6665

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,10 @@ class DIExpression : public MDNode {
24642464
/// Return whether this is an implicit location description.
24652465
bool isImplicit() const;
24662466

2467+
/// Return whether the location is computed on the expression stack, meaning
2468+
/// it cannot be a simple register location.
2469+
bool isComplex() const;
2470+
24672471
/// Append \p Ops with operations to apply the \p Offset.
24682472
static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
24692473

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,16 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
12001200
MI.getOperand(0).setIsDebug();
12011201

12021202
const DIExpression *DIExpr = MI.getDebugExpression();
1203+
1204+
// If we have a direct DBG_VALUE, and its location expression isn't
1205+
// currently complex, then adding an offset will morph it into a
1206+
// complex location that is interpreted as being a memory address.
1207+
// This changes a pointer-valued variable to dereference that pointer,
1208+
// which is incorrect. Fix by adding DW_OP_stack_value.
1209+
unsigned PrependFlags = DIExpression::ApplyOffset;
1210+
if (!MI.isIndirectDebugValue() && !DIExpr->isComplex())
1211+
PrependFlags |= DIExpression::StackValue;
1212+
12031213
// If we have DBG_VALUE that is indirect and has a Implicit location
12041214
// expression need to insert a deref before prepending a Memory
12051215
// location expression. Also after doing this we change the DBG_VALUE
@@ -1211,8 +1221,7 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
12111221
// Make the DBG_VALUE direct.
12121222
MI.getOperand(1).ChangeToRegister(0, false);
12131223
}
1214-
DIExpr =
1215-
DIExpression::prepend(DIExpr, DIExpression::ApplyOffset, Offset);
1224+
DIExpr = DIExpression::prepend(DIExpr, PrependFlags, Offset);
12161225
MI.getOperand(3).setMetadata(DIExpr);
12171226
continue;
12181227
}

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,27 @@ bool DIExpression::isImplicit() const {
926926
return false;
927927
}
928928

929+
bool DIExpression::isComplex() const {
930+
if (!isValid())
931+
return false;
932+
933+
if (getNumElements() == 0)
934+
return false;
935+
936+
// If there are any elements other than fragment or tag_offset, then some
937+
// kind of complex computation occurs.
938+
for (const auto &It : expr_ops()) {
939+
switch (It.getOp()) {
940+
case dwarf::DW_OP_LLVM_tag_offset:
941+
case dwarf::DW_OP_LLVM_fragment:
942+
continue;
943+
default: return true;
944+
}
945+
}
946+
947+
return false;
948+
}
949+
929950
Optional<DIExpression::FragmentInfo>
930951
DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) {
931952
for (auto I = Start; I != End; ++I)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# RUN: llc %s -x mir -o - -mtriple=x86_64-unknown-unknown -run-pass=prologepilog | FileCheck %s
2+
#
3+
# Check when the DBG_VALUE on a stack slot below (for var "c") has its stack
4+
# slot replaced with $rsp and a complex expression, it has DW_OP_stack_value
5+
# added. A direct reference to the stack slot is considered to be the _address_
6+
# of that stack slot, wheras its contents would be an indirect DBG_VALUE.
7+
#
8+
# Check too that for the same DBG_VALUE inst, with an indirect reference to
9+
# the stack slot, we do _not_ get DW_OP_plus_uconst added. This expression
10+
# should remain indirect, referring to the contents of the stack slot.
11+
#
12+
# CHECK: ![[VAR:[0-9]+]] = !DILocalVariable(name: "c"
13+
# CHECK: ![[VAR2:[0-9]+]] = !DILocalVariable(name: "asdf"
14+
# CHECK: ![[VAR3:[0-9]+]] = !DILocalVariable(name: "bees"
15+
#
16+
# CHECK: LEA64r $rsp
17+
# CHECK-NEXT: DBG_VALUE $rsp, $noreg, ![[VAR]], !DIExpression(DW_OP_plus_uconst, {{[0-9]+}}, DW_OP_stack_value)
18+
# CHECK-NEXT: DBG_VALUE $rsp, $noreg, ![[VAR2]], !DIExpression(DW_OP_plus_uconst, {{[0-9]+}}, DW_OP_stack_value, DW_OP_LLVM_fragment, 1, 2)
19+
# CHECK-NEXT: DBG_VALUE $rsp, $noreg, ![[VAR3]], !DIExpression(DW_OP_plus_uconst, {{[0-9]+}}, DW_OP_LLVM_tag_offset, 0, DW_OP_stack_value)
20+
# CHECK-NEXT: DBG_VALUE 1834104526
21+
# CHECK-NEXT: MOV64mr
22+
# CHECK-NEXT: DBG_VALUE $rsp, 0, ![[VAR]], !DIExpression(DW_OP_plus_uconst, {{[0-9]+}})
23+
24+
--- |
25+
; ModuleID = 'out.ll'
26+
source_filename = "abc.c"
27+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
28+
target triple = "x86_64-unknown-linux-gnu"
29+
30+
@b = common dso_local local_unnamed_addr global i32* null, align 8, !dbg !0
31+
@a = common dso_local local_unnamed_addr global i32 0, align 4, !dbg !6
32+
33+
; Function Attrs: nounwind uwtable
34+
define dso_local i32 @main() local_unnamed_addr !dbg !14 {
35+
entry:
36+
%l_1081 = alloca i32, align 4
37+
%0 = bitcast i32* %l_1081 to i8*, !dbg !20
38+
call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %0), !dbg !20
39+
call void @llvm.dbg.value(metadata i32 1834104526, metadata !18, metadata !DIExpression()), !dbg !21
40+
call void @llvm.dbg.value(metadata i32* %l_1081, metadata !19, metadata !DIExpression()), !dbg !21
41+
store i32* %l_1081, i32** @b, align 8, !dbg !22, !tbaa !23
42+
store i32 9, i32* @a, align 4, !dbg !27, !tbaa !28
43+
store i32 9, i32* %l_1081, align 4, !dbg !30, !tbaa !28
44+
%call = call i32 (...) @optimize_me_not(), !dbg !31
45+
call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %0), !dbg !32
46+
ret i32 0, !dbg !32
47+
}
48+
49+
; Function Attrs: argmemonly nounwind
50+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
51+
52+
declare dso_local i32 @optimize_me_not(...) local_unnamed_addr
53+
54+
; Function Attrs: argmemonly nounwind
55+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
56+
57+
; Function Attrs: nounwind readnone speculatable
58+
declare void @llvm.dbg.value(metadata, metadata, metadata)
59+
60+
!llvm.dbg.cu = !{!2}
61+
!llvm.module.flags = !{!10, !11, !12}
62+
!llvm.ident = !{!13}
63+
64+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
65+
!1 = distinct !DIGlobalVariable(name: "b", scope: !2, file: !3, line: 2, type: !9, isLocal: false, isDefinition: true)
66+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, nameTableKind: None)
67+
!3 = !DIFile(filename: "abc.c", directory: ".")
68+
!4 = !{}
69+
!5 = !{!6, !0}
70+
!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression())
71+
!7 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 1, type: !8, isLocal: false, isDefinition: true)
72+
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
73+
!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !8, size: 64)
74+
!10 = !{i32 2, !"Dwarf Version", i32 4}
75+
!11 = !{i32 2, !"Debug Info Version", i32 3}
76+
!12 = !{i32 1, !"wchar_size", i32 4}
77+
!13 = !{!"clang"}
78+
!14 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 3, type: !15, scopeLine: 3, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !17)
79+
!15 = !DISubroutineType(types: !16)
80+
!16 = !{!8}
81+
!17 = !{!18, !19, !33, !34}
82+
!18 = !DILocalVariable(name: "l_1081", scope: !14, file: !3, line: 4, type: !8)
83+
!19 = !DILocalVariable(name: "c", scope: !14, file: !3, line: 5, type: !9)
84+
!20 = !DILocation(line: 4, column: 3, scope: !14)
85+
!21 = !DILocation(line: 0, scope: !14)
86+
!22 = !DILocation(line: 6, column: 5, scope: !14)
87+
!23 = !{!24, !24, i64 0}
88+
!24 = !{!"any pointer", !25, i64 0}
89+
!25 = !{!"omnipotent char", !26, i64 0}
90+
!26 = !{!"Simple C/C++ TBAA"}
91+
!27 = !DILocation(line: 7, column: 10, scope: !14)
92+
!28 = !{!29, !29, i64 0}
93+
!29 = !{!"int", !25, i64 0}
94+
!30 = !DILocation(line: 7, column: 6, scope: !14)
95+
!31 = !DILocation(line: 8, column: 3, scope: !14)
96+
!32 = !DILocation(line: 9, column: 1, scope: !14)
97+
!33 = !DILocalVariable(name: "asdf", scope: !14, file: !3, line: 4, type: !8)
98+
!34 = !DILocalVariable(name: "bees", scope: !14, file: !3, line: 4, type: !8)
99+
100+
...
101+
---
102+
name: main
103+
alignment: 4
104+
tracksRegLiveness: true
105+
frameInfo:
106+
maxAlignment: 4
107+
hasCalls: true
108+
stack:
109+
- { id: 0, name: l_1081, type: default, offset: 0, size: 4, alignment: 4,
110+
callee-saved-register: '', callee-saved-restored: true,
111+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
112+
body: |
113+
bb.0.entry:
114+
renamable $rax = LEA64r %stack.0.l_1081, 1, $noreg, 0, $noreg
115+
DBG_VALUE %stack.0.l_1081, $noreg, !19, !DIExpression(), debug-location !21
116+
DBG_VALUE %stack.0.l_1081, $noreg, !33, !DIExpression(DW_OP_LLVM_fragment, 1, 2), debug-location !21
117+
DBG_VALUE %stack.0.l_1081, $noreg, !34, !DIExpression(DW_OP_LLVM_tag_offset, 0), debug-location !21
118+
DBG_VALUE 1834104526, $noreg, !18, !DIExpression(), debug-location !21
119+
MOV64mr $rip, 1, $noreg, @b, $noreg, killed renamable $rax, debug-location !22 :: (store 8 into @b, !tbaa !23)
120+
DBG_VALUE %stack.0.l_1081, 0, !19, !DIExpression(), debug-location !21
121+
MOV32mi $rip, 1, $noreg, @a, $noreg, 9, debug-location !27 :: (store 4 into @a, !tbaa !28)
122+
MOV32mi %stack.0.l_1081, 1, $noreg, 0, $noreg, 9, debug-location !30 :: (store 4 into %ir.l_1081, !tbaa !28)
123+
ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !31
124+
dead $eax = MOV32r0 implicit-def dead $eflags, implicit-def $al, debug-location !31
125+
CALL64pcrel32 @optimize_me_not, csr_64, implicit $rsp, implicit $ssp, implicit $al, implicit-def $rsp, implicit-def $ssp, implicit-def dead $eax, debug-location !31
126+
ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !31
127+
$eax = MOV32r0 implicit-def dead $eflags, debug-location !32
128+
RET 0, $eax, debug-location !32
129+
130+
...

0 commit comments

Comments
 (0)