Skip to content

Commit 03ed866

Browse files
arsenmgit-crd
authored andcommitted
CodeGen: Fix CodeView crashes with empty llvm.dbg.cu (llvm#163286)
1 parent 6681e21 commit 03ed866

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,23 @@ void CodeViewDebug::beginModule(Module *M) {
628628
// When emitting only compiler information, we may have only NoDebug CUs,
629629
// which would be skipped by debug_compile_units_begin.
630630
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
631+
if (CUs->operands().empty()) {
632+
Asm = nullptr;
633+
return;
634+
}
631635
Node = *CUs->operands().begin();
632636
}
633-
const auto *CU = cast<DICompileUnit>(Node);
634-
DISourceLanguageName Lang = CU->getSourceLanguage();
637+
638+
TheCU = cast<DICompileUnit>(Node);
639+
DISourceLanguageName Lang = TheCU->getSourceLanguage();
635640
CurrentSourceLanguage =
636641
Lang.hasVersionedName()
637642
? MapDWARFLanguageToCVLang(
638643
static_cast<dwarf::SourceLanguageName>(Lang.getName()))
639644
: MapDWARFLanguageToCVLang(
640645
static_cast<dwarf::SourceLanguage>(Lang.getName()));
641646
if (!M->getCodeViewFlag() ||
642-
CU->getEmissionKind() == DICompileUnit::NoDebug) {
647+
TheCU->getEmissionKind() == DICompileUnit::NoDebug) {
643648
Asm = nullptr;
644649
return;
645650
}
@@ -900,11 +905,10 @@ void CodeViewDebug::emitCompilerInformation() {
900905
OS.AddComment("CPUType");
901906
OS.emitInt16(static_cast<uint64_t>(TheCPU));
902907

903-
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
904-
const MDNode *Node = *CUs->operands().begin();
905-
const auto *CU = cast<DICompileUnit>(Node);
908+
StringRef CompilerVersion = "0";
909+
if (TheCU)
910+
CompilerVersion = TheCU->getProducer();
906911

907-
StringRef CompilerVersion = CU->getProducer();
908912
Version FrontVer = parseVersion(CompilerVersion);
909913
OS.AddComment("Frontend version");
910914
for (int N : FrontVer.Part) {

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
9898
/// The codeview CPU type used by the translation unit.
9999
codeview::CPUType TheCPU;
100100

101+
const DICompileUnit *TheCU = nullptr;
102+
101103
/// The AsmPrinter used for emitting compiler metadata. When only compiler
102104
/// info is being emitted, DebugHandlerBase::Asm may be null.
103105
AsmPrinter *CompilerInfoAsm = nullptr;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s
2+
3+
; CHECK: .file "<stdin>"
4+
; CHECK-NEXT: .section .debug$S,"dr"
5+
; CHECK-NEXT: .p2align 2, 0x0
6+
; CHECK-NEXT: .long 4 # Debug section magic
7+
; CHECK-NEXT: .long 241
8+
; CHECK-NEXT: .long .Ltmp1-.Ltmp0 # Subsection size
9+
; CHECK-NEXT: .Ltmp0:
10+
; CHECK-NEXT: .short .Ltmp3-.Ltmp2 # Record length
11+
; CHECK-NEXT: .Ltmp2:
12+
; CHECK-NEXT: .short 4353 # Record kind: S_OBJNAME
13+
; CHECK-NEXT: .long 0 # Signature
14+
; CHECK-NEXT: .byte 0 # Object name
15+
; CHECK-NEXT: .p2align 2, 0x0
16+
; CHECK-NEXT: .Ltmp3:
17+
; CHECK-NEXT: .short .Ltmp5-.Ltmp4 # Record length
18+
; CHECK-NEXT: .Ltmp4:
19+
; CHECK-NEXT: .short 4412 # Record kind: S_COMPILE3
20+
; CHECK-NEXT: .long 3 # Flags and language
21+
; CHECK-NEXT: .short 208 # CPUType
22+
; CHECK-NEXT: .short 0 # Frontend version
23+
; CHECK-NEXT: .short 0
24+
; CHECK-NEXT: .short 0
25+
; CHECK-NEXT: .short 0
26+
; CHECK-NEXT: .short 22000 # Backend version
27+
; CHECK-NEXT: .short 0
28+
; CHECK-NEXT: .short 0
29+
; CHECK-NEXT: .short 0
30+
; CHECK-NEXT: .asciz "0" # Null-terminated compiler version string
31+
; CHECK-NEXT: .p2align 2, 0x0
32+
; CHECK-NEXT: .Ltmp5:
33+
; CHECK-NEXT: .Ltmp1:
34+
; CHECK-NEXT: .p2align 2, 0x0
35+
36+
!llvm.dbg.cu = !{}
37+
!llvm.module.flags = !{!0}
38+
39+
!0 = !{i32 2, !"Debug Info Version", i32 3}

0 commit comments

Comments
 (0)