[llvm] 66e49e3 - [YAML] Don't validate `Fill::Size` after error (#123280)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 23 09:52:37 PST 2025


Author: Vitaly Buka
Date: 2025-01-23T09:52:34-08:00
New Revision: 66e49e38aeed92c48ba175f31e12b07a8c526d11

URL: https://github.com/llvm/llvm-project/commit/66e49e38aeed92c48ba175f31e12b07a8c526d11
DIFF: https://github.com/llvm/llvm-project/commit/66e49e38aeed92c48ba175f31e12b07a8c526d11.diff

LOG: [YAML] Don't validate `Fill::Size` after error (#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.

Added: 
    

Modified: 
    llvm/lib/ObjectYAML/ELFYAML.cpp
    llvm/lib/ObjectYAML/MachOYAML.cpp
    llvm/test/ObjectYAML/MachO/section_data.yaml
    llvm/test/tools/yaml2obj/ELF/custom-fill.yaml

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 7e94d01a971534..24f426a9aa1f7c 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1747,7 +1747,9 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
 std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
     IO &io, std::unique_ptr<ELFYAML::Chunk> &C) {
   if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) {
-    if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size)
+    // Can't check the `Size`, as it's required and may be left uninitialized by
+    // previous error.
+    if (!io.error() && F->Pattern && F->Pattern->binary_size() != 0 && !F->Size)
       return "\"Size\" can't be 0 when \"Pattern\" is not empty";
     return "";
   }

diff  --git a/llvm/lib/ObjectYAML/MachOYAML.cpp b/llvm/lib/ObjectYAML/MachOYAML.cpp
index 4857b5911ff2ef..b7eda97c22ae04 100644
--- a/llvm/lib/ObjectYAML/MachOYAML.cpp
+++ b/llvm/lib/ObjectYAML/MachOYAML.cpp
@@ -346,7 +346,10 @@ void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
 std::string
 MappingTraits<MachOYAML::Section>::validate(IO &IO,
                                             MachOYAML::Section &Section) {
-  if (Section.content && Section.size < Section.content->binary_size())
+  // Can't check the `size`, as it's required and may be left uninitialized by
+  // previous error.
+  if (!IO.error() && Section.content &&
+      Section.size < Section.content->binary_size())
     return "Section size must be greater than or equal to the content size";
   return "";
 }

diff  --git a/llvm/test/ObjectYAML/MachO/section_data.yaml b/llvm/test/ObjectYAML/MachO/section_data.yaml
index 87c5bc803ee1a2..a2d9a3b7e1675b 100644
--- a/llvm/test/ObjectYAML/MachO/section_data.yaml
+++ b/llvm/test/ObjectYAML/MachO/section_data.yaml
@@ -159,3 +159,44 @@ LoadCommands:
         reserved2:       0x00000000
         reserved3:       0x00000000
         content:         AA
+
+## Case 4: Don't validate if size is missing.
+# RUN: not yaml2obj --docnum=4 %s -o %t1 2>&1 | FileCheck %s --check-prefix=CASE4 --implicit-check-not=error:
+# CASE4: error: missing required key 'size'
+# CASE4: error: failed to parse YAML
+
+--- !mach-o
+FileHeader:
+  magic:           0xFEEDFACF
+  cputype:         0x01000007
+  cpusubtype:      0x00000003
+  filetype:        0x00000001
+  ncmds:           1
+  sizeofcmds:      232
+  flags:           0x00002000
+  reserved:        0x00000000
+LoadCommands:
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         232
+    segname:         ''
+    vmaddr:          0
+    vmsize:          4
+    fileoff:         392
+    filesize:        4
+    maxprot:         7
+    initprot:        7
+    nsects:          1
+    flags:           0
+    Sections:
+      - sectname:        __data
+        segname:         __DATA
+        addr:            0x0000000000000000
+        content:         AA
+        offset:          0x00000188
+        align:           2
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000

diff  --git a/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml b/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
index d770fdb9825327..cdb9a97889ac12 100644
--- a/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
@@ -156,9 +156,10 @@ Sections:
     Pattern: "BB"
 
 ## Check that the "Size" field is mandatory.
-# RUN: not yaml2obj --docnum=5 2>&1 %s | FileCheck %s --check-prefix=NOSIZE
+# RUN: not yaml2obj --docnum=5 2>&1 %s | FileCheck %s --check-prefix=NOSIZE --implicit-check-not=error:
 
-## NOSIZE: error: missing required key 'Size'
+# NOSIZE: error: missing required key 'Size'
+# NOSIZE: error: failed to parse YAML
 
 --- !ELF
 FileHeader:


        


More information about the llvm-commits mailing list