Skip to content

Commit 100483b

Browse files
committed
[DWARFDebugLine] Check for (EOF) errors when parsing v5 content descriptors
Summary: Without that we could be silently reading zeroes, as that's the default DataExtractor behavior. The entire parse would still most likely fail, but it would do that with a seemingly unrelated/nonsensical error message. Reviewers: dblaikie, probinson, jhenderson Subscribers: hiraditya, MaskRay, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D77554
1 parent f8a42bc commit 100483b

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,21 +219,27 @@ parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
219219
static llvm::Expected<ContentDescriptors>
220220
parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
221221
DWARFDebugLine::ContentTypeTracker *ContentTypes) {
222+
Error Err = Error::success();
222223
ContentDescriptors Descriptors;
223-
int FormatCount = DebugLineData.getU8(OffsetPtr);
224+
int FormatCount = DebugLineData.getU8(OffsetPtr, &Err);
224225
bool HasPath = false;
225-
for (int I = 0; I != FormatCount; ++I) {
226+
for (int I = 0; I != FormatCount && !Err; ++I) {
226227
ContentDescriptor Descriptor;
227228
Descriptor.Type =
228-
dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
229-
Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
229+
dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr, &Err));
230+
Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr, &Err));
230231
if (Descriptor.Type == dwarf::DW_LNCT_path)
231232
HasPath = true;
232233
if (ContentTypes)
233234
ContentTypes->trackContentType(Descriptor.Type);
234235
Descriptors.push_back(Descriptor);
235236
}
236237

238+
if (Err)
239+
return createStringError(errc::invalid_argument,
240+
"failed to parse entry content descriptors: %s",
241+
toString(std::move(Err)).c_str());
242+
237243
if (!HasPath)
238244
return createStringError(errc::invalid_argument,
239245
"failed to parse entry content descriptions"
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Test cases when we run into the end of section while parsing a line table
2+
## prologue.
3+
4+
# RUN: llvm-mc -triple x86_64-pc-linux %s -filetype=obj --defsym CASE=0 -o %t0
5+
# RUN: llvm-dwarfdump -debug-line %t0 2>&1 | FileCheck %s --check-prefixes=ALL,C0
6+
7+
# RUN: llvm-mc -triple x86_64-pc-linux %s -filetype=obj --defsym CASE=1 -o %t1
8+
# RUN: llvm-dwarfdump -debug-line %t1 2>&1 | FileCheck %s --check-prefixes=ALL,C1
9+
10+
# RUN: llvm-mc -triple x86_64-pc-linux %s -filetype=obj --defsym CASE=2 -o %t2
11+
# RUN: llvm-dwarfdump -debug-line %t2 2>&1 | FileCheck %s --check-prefixes=ALL,C2
12+
13+
# RUN: llvm-mc -triple x86_64-pc-linux %s -filetype=obj --defsym CASE=3 -o %t3
14+
# RUN: llvm-dwarfdump -debug-line %t3 2>&1 | FileCheck %s --check-prefixes=ALL,OK
15+
16+
# ALL: debug_line[0x00000000]
17+
# C0-NEXT: warning: parsing line table prologue at 0x00000000 found an invalid directory or file table description at 0x00000027
18+
# C0-NEXT: warning: failed to parse entry content descriptors: unexpected end of data at offset 0x27
19+
# C1-NEXT: warning: parsing line table prologue at 0x00000000 found an invalid directory or file table description at 0x0000002a
20+
# C1-NEXT: warning: failed to parse entry content descriptors: malformed uleb128, extends past end
21+
# C2-NEXT: warning: parsing line table prologue at 0x00000000 found an invalid directory or file table description at 0x0000002b
22+
# C2-NEXT: warning: failed to parse entry content descriptors: malformed uleb128, extends past end
23+
# ALL: include_directories[ 0] = "/tmp"
24+
# OK: file_names[ 0]:
25+
# OK-NEXT: name: "foo"
26+
# OK-NEXT: dir_index: 0
27+
28+
.section .debug_line,"",@progbits
29+
.long .Lend-.Lstart # Length of Unit
30+
.Lstart:
31+
.short 5 # DWARF version number
32+
.byte 8 # Address Size
33+
.byte 0 # Segment Selector Size
34+
.long .Lprologue_end-.Lprologue_start # Length of Prologue
35+
.Lprologue_start:
36+
.byte 1 # Minimum Instruction Length
37+
.byte 1 # Maximum Operations per Instruction
38+
.byte 1 # Default is_stmt
39+
.byte -5 # Line Base
40+
.byte 14 # Line Range
41+
.byte 13 # Opcode Base
42+
.byte 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 # Standard Opcode Lengths
43+
# Directory table format
44+
.byte 1 # One element per directory entry
45+
.byte 1 # DW_LNCT_path
46+
.byte 0x08 # DW_FORM_string
47+
# Directory table entries
48+
.byte 1 # 1 directory
49+
.asciz "/tmp"
50+
# File table format
51+
.if CASE >= 1
52+
.byte 2 # 2 elements per file entry
53+
.byte 1 # DW_LNCT_path
54+
.byte 8 # DW_FORM_string
55+
.if CASE >= 2
56+
.byte 2 # DW_LNCT_directory_index
57+
.if CASE >= 3
58+
.byte 11 # DW_FORM_data1
59+
# File table entries
60+
.byte 1
61+
.asciz "foo"
62+
.byte 0
63+
.endif
64+
.endif
65+
.endif
66+
67+
.Lprologue_end:
68+
.Lend:

0 commit comments

Comments
 (0)