Skip to content

Commit 66e49e3

Browse files
authored
[YAML] Don't validate Fill::Size after error (llvm#123280)
Size is required, so we don't know if it's in uninitialized state after the previous error. Triggers msan on llvm/test/tools/yaml2obj/ELF/custom-fill.yaml NOSIZE test. We have `Fill` Section with Pattern, but no size. Before the fix it produced error: ``` YAML:169:5: error: missing required key 'Size' - Type: Fill ^ YAML:169:5: error: "Size" can't be 0 when "Pattern" is not empty - Type: Fill ``` The same applies to `MachOYAML::Section` fields `content` and `size`. However `MachOYAML::Section` matches size first, so on error, content is not set anyway. Added error checking just in case.
1 parent c7e6ca7 commit 66e49e3

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

llvm/lib/ObjectYAML/ELFYAML.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,9 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
17471747
std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
17481748
IO &io, std::unique_ptr<ELFYAML::Chunk> &C) {
17491749
if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) {
1750-
if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size)
1750+
// Can't check the `Size`, as it's required and may be left uninitialized by
1751+
// previous error.
1752+
if (!io.error() && F->Pattern && F->Pattern->binary_size() != 0 && !F->Size)
17511753
return "\"Size\" can't be 0 when \"Pattern\" is not empty";
17521754
return "";
17531755
}

llvm/lib/ObjectYAML/MachOYAML.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
346346
std::string
347347
MappingTraits<MachOYAML::Section>::validate(IO &IO,
348348
MachOYAML::Section &Section) {
349-
if (Section.content && Section.size < Section.content->binary_size())
349+
// Can't check the `size`, as it's required and may be left uninitialized by
350+
// previous error.
351+
if (!IO.error() && Section.content &&
352+
Section.size < Section.content->binary_size())
350353
return "Section size must be greater than or equal to the content size";
351354
return "";
352355
}

llvm/test/ObjectYAML/MachO/section_data.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,44 @@ LoadCommands:
159159
reserved2: 0x00000000
160160
reserved3: 0x00000000
161161
content: AA
162+
163+
## Case 4: Don't validate if size is missing.
164+
# RUN: not yaml2obj --docnum=4 %s -o %t1 2>&1 | FileCheck %s --check-prefix=CASE4 --implicit-check-not=error:
165+
# CASE4: error: missing required key 'size'
166+
# CASE4: error: failed to parse YAML
167+
168+
--- !mach-o
169+
FileHeader:
170+
magic: 0xFEEDFACF
171+
cputype: 0x01000007
172+
cpusubtype: 0x00000003
173+
filetype: 0x00000001
174+
ncmds: 1
175+
sizeofcmds: 232
176+
flags: 0x00002000
177+
reserved: 0x00000000
178+
LoadCommands:
179+
- cmd: LC_SEGMENT_64
180+
cmdsize: 232
181+
segname: ''
182+
vmaddr: 0
183+
vmsize: 4
184+
fileoff: 392
185+
filesize: 4
186+
maxprot: 7
187+
initprot: 7
188+
nsects: 1
189+
flags: 0
190+
Sections:
191+
- sectname: __data
192+
segname: __DATA
193+
addr: 0x0000000000000000
194+
content: AA
195+
offset: 0x00000188
196+
align: 2
197+
reloff: 0x00000000
198+
nreloc: 0
199+
flags: 0x00000000
200+
reserved1: 0x00000000
201+
reserved2: 0x00000000
202+
reserved3: 0x00000000

llvm/test/tools/yaml2obj/ELF/custom-fill.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ Sections:
156156
Pattern: "BB"
157157

158158
## Check that the "Size" field is mandatory.
159-
# RUN: not yaml2obj --docnum=5 2>&1 %s | FileCheck %s --check-prefix=NOSIZE
159+
# RUN: not yaml2obj --docnum=5 2>&1 %s | FileCheck %s --check-prefix=NOSIZE --implicit-check-not=error:
160160

161-
## NOSIZE: error: missing required key 'Size'
161+
# NOSIZE: error: missing required key 'Size'
162+
# NOSIZE: error: failed to parse YAML
162163

163164
--- !ELF
164165
FileHeader:

0 commit comments

Comments
 (0)