Skip to content

Commit

Permalink
Reapply [Verifier] Sanity check alloca size against DILocalVariable f…
Browse files Browse the repository at this point in the history
…ragment size

Reapply after fixing a clang bug this exposed in D158972 and
adjusting a number of tests that failed for 32-bit targets.

-----

Add a check that the DILocalVariable fragment size in dbg.declare
does not exceed the size of the alloca.

This would have caught the invalid debuginfo regenerated by rustc
in #64149.

Differential Revision: https://reviews.llvm.org/D158743
  • Loading branch information
nikic committed Sep 15, 2023
1 parent 303eb50 commit 47324cf
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 204 deletions.
14 changes: 14 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6298,6 +6298,20 @@ void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) {
CheckDI(isType(Var->getRawType()), "invalid type ref", Var,
Var->getRawType());
verifyFnArgs(DII);

if (auto *Declare = dyn_cast<DbgDeclareInst>(&DII)) {
if (auto *Alloca = dyn_cast_or_null<AllocaInst>(Declare->getAddress())) {
DIExpression *Expr = Declare->getExpression();
std::optional<uint64_t> FragSize = Declare->getFragmentSizeInBits();
std::optional<TypeSize> AllocSize = Alloca->getAllocationSizeInBits(DL);
if (FragSize && AllocSize && !AllocSize->isScalable() &&
!Expr->isComplex()) {
CheckDI(*FragSize <= AllocSize->getFixedValue(),
"llvm.dbg.declare has larger fragment size than alloca size ",
&DII);
}
}
}
}

void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/CodeGen/ARM/stack-frame-layout-remarks.ll
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,19 @@ attributes #2 = { ssp "stack-protector-buffer-size"="5" "frame-pointer"="all" }
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !4, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "dot.c", directory: "")
!4 = !{!5, !6, !10, !13}
!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Array", file: !3, line: 3, size: 128, elements: !8)
!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 32)
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 32)
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Array", file: !3, line: 3, size: 64, elements: !8)
!8 = !{!9, !12}
!9 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !7, file: !3, line: 4, baseType: !10, size: 64)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
!9 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !7, file: !3, line: 4, baseType: !10, size: 32)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 32)
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!12 = !DIDerivedType(tag: DW_TAG_member, name: "size", scope: !7, file: !3, line: 5, baseType: !11, size: 32, offset: 64)
!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Result", file: !3, line: 8, size: 128, elements: !15)
!12 = !DIDerivedType(tag: DW_TAG_member, name: "size", scope: !7, file: !3, line: 5, baseType: !11, size: 32, offset: 32)
!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 32)
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Result", file: !3, line: 8, size: 64, elements: !15)
!15 = !{!16, !17}
!16 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !14, file: !3, line: 9, baseType: !6, size: 64)
!17 = !DIDerivedType(tag: DW_TAG_member, name: "sum", scope: !14, file: !3, line: 10, baseType: !11, size: 32, offset: 64)
!16 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !14, file: !3, line: 9, baseType: !6, size: 32)
!17 = !DIDerivedType(tag: DW_TAG_member, name: "sum", scope: !14, file: !3, line: 10, baseType: !11, size: 32, offset: 32)
!18 = !{i32 7, !"Dwarf Version", i32 5}
!19 = !{i32 2, !"Debug Info Version", i32 3}
!20 = !{i32 1, !"wchar_size", i32 4}
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/CodeGen/BPF/warn-stack.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

;; CHECK-NOT: nowarn
define void @nowarn() local_unnamed_addr #0 !dbg !6 {
%1 = alloca [504 x i8], align 1
call void @llvm.lifetime.start.p0(i64 504, ptr nonnull %1) #4, !dbg !15
%1 = alloca [511 x i8], align 1
call void @llvm.lifetime.start.p0(i64 511, ptr nonnull %1) #4, !dbg !15
tail call void @llvm.dbg.declare(metadata ptr %1, metadata !10, metadata !16), !dbg !17
call void @doit(ptr nonnull %1) #4, !dbg !18
call void @llvm.lifetime.end.p0(i64 504, ptr nonnull %1) #4, !dbg !19
call void @llvm.lifetime.end.p0(i64 511, ptr nonnull %1) #4, !dbg !19
ret void, !dbg !19
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
; Test remark output for stack-frame-layout

; ensure basic output works
; RUN: llc -mcpu=corei7 -O1 -pass-remarks-analysis=stack-frame-layout < %s 2>&1 >/dev/null | FileCheck %s

; check additional slots are displayed when stack is not optimized
; RUN: llc -mcpu=corei7 -O0 -pass-remarks-analysis=stack-frame-layout < %s 2>&1 >/dev/null | FileCheck %s --check-prefix=NO_COLORING

; check more complex cases
; RUN: llc %s -pass-remarks-analysis=stack-frame-layout -o /dev/null --march=x86 -mcpu=i386 2>&1 | FileCheck %s --check-prefix=BOTH --check-prefix=DEBUG

; check output without debug info
; RUN: opt %s -passes=strip -S | llc -pass-remarks-analysis=stack-frame-layout -o /dev/null --march=x86 -mcpu=i386 2>&1 | FileCheck %s --check-prefix=BOTH --check-prefix=STRIPPED

target triple = "x86_64-unknown-linux-gnu"
target triple = "i386-unknown-linux-gnu"

@.str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
declare i32 @printf(ptr, ...)

; CHECK: Function: stackSizeWarning
; CHECK: Offset: [SP-88], Type: Variable, Align: 16, Size: 80
; CHECK: buffer @ frame-diags.c:30
; NO_COLORING: Offset: [SP-168], Type: Variable, Align: 16, Size: 80
; CHECK: buffer2 @ frame-diags.c:33
define void @stackSizeWarning() {
entry:
%buffer = alloca [80 x i8], align 16
%buffer2 = alloca [80 x i8], align 16
call void @llvm.dbg.declare(metadata ptr %buffer, metadata !25, metadata !DIExpression()), !dbg !39
call void @llvm.dbg.declare(metadata ptr %buffer2, metadata !31, metadata !DIExpression()), !dbg !40
ret void
}

; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0

Expand Down Expand Up @@ -208,7 +188,7 @@ entry:
}

; uselistorder directives
uselistorder ptr @llvm.dbg.declare, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 18 }
uselistorder ptr @llvm.dbg.declare, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16 }

attributes #0 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
attributes #1 = { "frame-pointer"="all" }
Expand All @@ -222,19 +202,19 @@ attributes #2 = { ssp "stack-protector-buffer-size"="5" "frame-pointer"="all" }
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !4, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "dot.c", directory: "")
!4 = !{!5, !6, !10, !13}
!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Array", file: !3, line: 3, size: 128, elements: !8)
!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 32)
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 32)
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Array", file: !3, line: 3, size: 64, elements: !8)
!8 = !{!9, !12}
!9 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !7, file: !3, line: 4, baseType: !10, size: 64)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
!9 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !7, file: !3, line: 4, baseType: !10, size: 32)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 32)
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!12 = !DIDerivedType(tag: DW_TAG_member, name: "size", scope: !7, file: !3, line: 5, baseType: !11, size: 32, offset: 64)
!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Result", file: !3, line: 8, size: 128, elements: !15)
!12 = !DIDerivedType(tag: DW_TAG_member, name: "size", scope: !7, file: !3, line: 5, baseType: !11, size: 32, offset: 32)
!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 32)
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Result", file: !3, line: 8, size: 64, elements: !15)
!15 = !{!16, !17}
!16 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !14, file: !3, line: 9, baseType: !6, size: 64)
!17 = !DIDerivedType(tag: DW_TAG_member, name: "sum", scope: !14, file: !3, line: 10, baseType: !11, size: 32, offset: 64)
!16 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !14, file: !3, line: 9, baseType: !6, size: 32)
!17 = !DIDerivedType(tag: DW_TAG_member, name: "sum", scope: !14, file: !3, line: 10, baseType: !11, size: 32, offset: 32)
!18 = !{i32 7, !"Dwarf Version", i32 5}
!19 = !{i32 2, !"Debug Info Version", i32 3}
!20 = !{i32 1, !"wchar_size", i32 4}
Expand Down
76 changes: 76 additions & 0 deletions llvm/test/CodeGen/X86/stack-frame-layout-remarks-64.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
; Test remark output for stack-frame-layout

; ensure basic output works
; RUN: llc -mcpu=corei7 -O1 -pass-remarks-analysis=stack-frame-layout < %s 2>&1 >/dev/null | FileCheck %s

; check additional slots are displayed when stack is not optimized
; RUN: llc -mcpu=corei7 -O0 -pass-remarks-analysis=stack-frame-layout < %s 2>&1 >/dev/null | FileCheck %s --check-prefix=NO_COLORING

target triple = "x86_64-unknown-linux-gnu"

@.str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
declare i32 @printf(ptr, ...)

; CHECK: Function: stackSizeWarning
; CHECK: Offset: [SP-88], Type: Variable, Align: 16, Size: 80
; CHECK: buffer @ frame-diags.c:30
; NO_COLORING: Offset: [SP-168], Type: Variable, Align: 16, Size: 80
; CHECK: buffer2 @ frame-diags.c:33
define void @stackSizeWarning() {
entry:
%buffer = alloca [80 x i8], align 16
%buffer2 = alloca [80 x i8], align 16
call void @llvm.dbg.declare(metadata ptr %buffer, metadata !25, metadata !DIExpression()), !dbg !39
call void @llvm.dbg.declare(metadata ptr %buffer2, metadata !31, metadata !DIExpression()), !dbg !40
ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.dbg.cu = !{!0, !2}
!llvm.module.flags = !{!18, !19, !20, !21, !22, !23, !24}

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "frame-diags.c", directory: "")
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !4, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "dot.c", directory: "")
!4 = !{!5, !6, !10, !13}
!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Array", file: !3, line: 3, size: 128, elements: !8)
!8 = !{!9, !12}
!9 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !7, file: !3, line: 4, baseType: !10, size: 64)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!12 = !DIDerivedType(tag: DW_TAG_member, name: "size", scope: !7, file: !3, line: 5, baseType: !11, size: 32, offset: 64)
!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Result", file: !3, line: 8, size: 128, elements: !15)
!15 = !{!16, !17}
!16 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !14, file: !3, line: 9, baseType: !6, size: 64)
!17 = !DIDerivedType(tag: DW_TAG_member, name: "sum", scope: !14, file: !3, line: 10, baseType: !11, size: 32, offset: 64)
!18 = !{i32 7, !"Dwarf Version", i32 5}
!19 = !{i32 2, !"Debug Info Version", i32 3}
!20 = !{i32 1, !"wchar_size", i32 4}
!21 = !{i32 8, !"PIC Level", i32 2}
!22 = !{i32 7, !"PIE Level", i32 2}
!23 = !{i32 7, !"uwtable", i32 2}
!24 = !{i32 7, !"frame-pointer", i32 2}
!25 = !DILocalVariable(name: "buffer", scope: !26, file: !1, line: 30, type: !32)
!26 = distinct !DILexicalBlock(scope: !27, file: !1, line: 29, column: 3)
!27 = distinct !DISubprogram(name: "stackSizeWarning", scope: !1, file: !1, line: 28, type: !28, scopeLine: 28, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !30)
!28 = !DISubroutineType(types: !29)
!29 = !{null}
!30 = !{!25, !31, !36, !37}
!31 = !DILocalVariable(name: "buffer2", scope: !27, file: !1, line: 33, type: !32)
!32 = !DICompositeType(tag: DW_TAG_array_type, baseType: !33, size: 640, elements: !34)
!33 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!34 = !{!35}
!35 = !DISubrange(count: 80)
!36 = !DILocalVariable(name: "a", scope: !27, file: !1, line: 34, type: !11)
!37 = !DILocalVariable(name: "b", scope: !27, file: !1, line: 35, type: !38)
!38 = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed)
!39 = !DILocation(line: 30, column: 10, scope: !26)
!40 = !DILocation(line: 33, column: 8, scope: !27)
4 changes: 2 additions & 2 deletions llvm/test/DebugInfo/Generic/discriminated-union.ll
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
; CHECK: DW_AT_alignment
; CHECK: DW_AT_data_member_location [DW_FORM_data1] (0x00)

%F = type { [0 x i8], ptr, [8 x i8] }
%F = type { [0 x i8], i64, [8 x i8] }
%"F::Nope" = type {}

define internal void @_ZN2e34main17h934ff72f9a38d4bbE() unnamed_addr #0 !dbg !5 {
start:
%qq = alloca %F, align 8
call void @llvm.dbg.declare(metadata ptr %qq, metadata !10, metadata !28), !dbg !29
store ptr null, ptr %qq, !dbg !29
store i64 0, ptr %qq, !dbg !29
ret void, !dbg !30
}

Expand Down

0 comments on commit 47324cf

Please sign in to comment.