Skip to content

Commit cbfc102

Browse files
authored
Fix lld crash caused by dynamic bit offset patch (#146701)
PR #141106 changed the debuginfo metdata to allow dynamic bit offsets and sizes. This caused a crash in lld when using LTO. The problem is that lazyLoadOneMetadata assumes that the metadata in question can be cast to MDNode; but in the typical case where the offset is a constant, this is not true. This patch changes this spot to allow non-MDNodes through. The included test case comes from the report in #141106.
1 parent 50f40a5 commit cbfc102

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

lld/test/ELF/lto/lazy-debug.ll

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
; REQUIRES: aarch64
2+
;; Regression test for a case in lazy debuginfo loading.
3+
;; The bug would cause ld.lld to crash.
4+
5+
; RUN: split-file %s %t
6+
; RUN: llvm-as %t/hda_codec.s -o %t/hda_codec.o
7+
; RUN: llvm-as %t/hda_bind.s -o %t/hda_bind.o
8+
; RUN: ld.lld -EL -maarch64elf -r %t/hda_bind.o %t/hda_codec.o -o %t/hda_codec
9+
10+
;--- hda_codec.s
11+
; ModuleID = 'hda_codec.o'
12+
source_filename = "hda_codec.i"
13+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
14+
target triple = "aarch64-unknown-linux-gnu"
15+
16+
%struct.anon = type { i32 }
17+
18+
@hda_set_power_state_codec = hidden local_unnamed_addr global %struct.anon zeroinitializer, align 4, !dbg !0
19+
20+
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
21+
define hidden void @snd_hda_codec_shutdown() local_unnamed_addr #0 !dbg !19 {
22+
entry:
23+
ret void, !dbg !22
24+
}
25+
26+
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable "frame-pointer"="non-leaf" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fp-armv8,+neon,+v8a,-fmv" }
27+
28+
!llvm.dbg.cu = !{!2}
29+
!llvm.module.flags = !{!10, !11, !12, !13, !14, !15, !16, !17}
30+
!llvm.ident = !{!18}
31+
32+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
33+
!1 = distinct !DIGlobalVariable(name: "hda_set_power_state_codec", scope: !2, file: !5, line: 3, type: !6, isLocal: false, isDefinition: true)
34+
!2 = distinct !DICompileUnit(language: DW_LANG_C11, file: !3, producer: "clang version 21.0.0git (git@github.com:llvm/llvm-project.git 93849a39c432827473ca6c676f1500da69b3aaa0)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
35+
!3 = !DIFile(filename: "hda_codec.i", directory: "/tmp")
36+
!4 = !{!0}
37+
!5 = !DIFile(filename: "hda_codec.i", directory: "/tmp", checksumkind: CSK_MD5, checksum: "c192644b468953345ff9647026173a7b")
38+
!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !5, line: 1, size: i64 32, offset: i64 0, elements: !7)
39+
!7 = !{!8}
40+
!8 = !DIDerivedType(tag: DW_TAG_member, name: "mfg", scope: !6, file: !5, line: 2, baseType: !9, size: i64 32, offset: i64 0)
41+
!9 = !DIBasicType(name: "int", size: i64 32, encoding: DW_ATE_signed)
42+
!10 = !{i32 7, !"Dwarf Version", i32 5}
43+
!11 = !{i32 2, !"Debug Info Version", i32 3}
44+
!12 = !{i32 1, !"wchar_size", i32 4}
45+
!13 = !{i32 8, !"PIC Level", i32 2}
46+
!14 = !{i32 7, !"PIE Level", i32 2}
47+
!15 = !{i32 7, !"uwtable", i32 2}
48+
!16 = !{i32 7, !"frame-pointer", i32 1}
49+
!17 = !{i32 1, !"EnableSplitLTOUnit", i32 1}
50+
!18 = !{!"clang version 21.0.0git (git@github.com:llvm/llvm-project.git 93849a39c432827473ca6c676f1500da69b3aaa0)"}
51+
!19 = distinct !DISubprogram(name: "snd_hda_codec_shutdown", scope: !5, file: !5, line: 4, type: !20, scopeLine: 4, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2)
52+
!20 = !DISubroutineType(types: !21)
53+
!21 = !{null}
54+
!22 = !DILocation(line: 4, column: 32, scope: !19)
55+
56+
^0 = module: (path: "hda_codec.o", hash: (1120894731, 3099354915, 309166549, 2100129435, 1932081428))
57+
^1 = gv: (name: "snd_hda_codec_shutdown", summaries: (function: (module: ^0, flags: (linkage: external, visibility: hidden, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0, importType: definition), insts: 1, funcFlags: (readNone: 1, readOnly: 0, noRecurse: 1, returnDoesNotAlias: 0, noInline: 0, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0)))) ; guid = 1539195202824839354
58+
^2 = gv: (name: "hda_set_power_state_codec", summaries: (variable: (module: ^0, flags: (linkage: external, visibility: hidden, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0, importType: definition), varFlags: (readonly: 1, writeonly: 1, constant: 0)))) ; guid = 10300548032946263328
59+
^3 = flags: 8
60+
^4 = blockcount: 0
61+
62+
;--- hda_bind.s
63+
; ModuleID = 'hda_bind.o'
64+
source_filename = "hda_bind.i"
65+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
66+
target triple = "aarch64-unknown-linux-gnu"
67+
68+
; Function Attrs: nounwind uwtable
69+
define hidden void @hda_codec_driver_shutdown() local_unnamed_addr #0 !dbg !11 {
70+
entry:
71+
tail call void @snd_hda_codec_shutdown() #2, !dbg !15
72+
ret void, !dbg !16
73+
}
74+
75+
declare void @snd_hda_codec_shutdown(...) local_unnamed_addr #1
76+
77+
attributes #0 = { nounwind uwtable "frame-pointer"="non-leaf" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fp-armv8,+neon,+v8a,-fmv" }
78+
attributes #1 = { "frame-pointer"="non-leaf" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fp-armv8,+neon,+v8a,-fmv" }
79+
attributes #2 = { nounwind }
80+
81+
!llvm.dbg.cu = !{!0}
82+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8, !9}
83+
!llvm.ident = !{!10}
84+
85+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 21.0.0git (git@github.com:llvm/llvm-project.git 93849a39c432827473ca6c676f1500da69b3aaa0)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
86+
!1 = !DIFile(filename: "hda_bind.i", directory: "/tmp")
87+
!2 = !{i32 7, !"Dwarf Version", i32 5}
88+
!3 = !{i32 2, !"Debug Info Version", i32 3}
89+
!4 = !{i32 1, !"wchar_size", i32 4}
90+
!5 = !{i32 8, !"PIC Level", i32 2}
91+
!6 = !{i32 7, !"PIE Level", i32 2}
92+
!7 = !{i32 7, !"uwtable", i32 2}
93+
!8 = !{i32 7, !"frame-pointer", i32 1}
94+
!9 = !{i32 1, !"EnableSplitLTOUnit", i32 1}
95+
!10 = !{!"clang version 21.0.0git (git@github.com:llvm/llvm-project.git 93849a39c432827473ca6c676f1500da69b3aaa0)"}
96+
!11 = distinct !DISubprogram(name: "hda_codec_driver_shutdown", scope: !12, file: !12, line: 2, type: !13, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
97+
!12 = !DIFile(filename: "hda_bind.i", directory: "/tmp", checksumkind: CSK_MD5, checksum: "5907dd04e8964940b57448f37db201c6")
98+
!13 = !DISubroutineType(types: !14)
99+
!14 = !{null}
100+
!15 = !DILocation(line: 2, column: 36, scope: !11)
101+
!16 = !DILocation(line: 2, column: 62, scope: !11)
102+
103+
^0 = module: (path: "hda_bind.o", hash: (1958332034, 2012675483, 855691486, 2017350850, 2779827776))
104+
^1 = gv: (name: "snd_hda_codec_shutdown") ; guid = 1539195202824839354
105+
^2 = gv: (name: "hda_codec_driver_shutdown", summaries: (function: (module: ^0, flags: (linkage: external, visibility: hidden, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0, importType: definition), insts: 2, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 0, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), calls: ((callee: ^1, tail: 1))))) ; guid = 12817427500962331703
106+
^3 = flags: 8
107+
^4 = blockcount: 0

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,10 @@ void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
11561156
assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
11571157
// Lookup first if the metadata hasn't already been loaded.
11581158
if (auto *MD = MetadataList.lookup(ID)) {
1159-
auto *N = cast<MDNode>(MD);
1160-
if (!N->isTemporary())
1159+
auto *N = dyn_cast<MDNode>(MD);
1160+
// If the node is not an MDNode, or if it is not temporary, then
1161+
// we're done.
1162+
if (!N || !N->isTemporary())
11611163
return;
11621164
}
11631165
SmallVector<uint64_t, 64> Record;

0 commit comments

Comments
 (0)