[llvm] [yaml2obj] Don't use uninitialized Type (PR #123274)
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 23 11:18:12 PST 2025
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/123274
>From fbd205b9fc1ecd652f9d0fff2f471b4c1bd9e4c8 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 16 Jan 2025 18:40:51 -0800
Subject: [PATCH 01/10] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20?=
=?UTF-8?q?changes=20to=20main=20this=20commit=20is=20based=20on?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
llvm/lib/ObjectYAML/ELFYAML.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 7e94d01a971534..83e6cf76dd746f 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1588,7 +1588,7 @@ static bool isInteger(StringRef Val) {
void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
- ELFYAML::ELF_SHT Type;
+ ELFYAML::ELF_SHT Type = ELF::ET_NONE;
StringRef TypeStr;
if (IO.outputting()) {
if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
>From 877e727d2c9a40f012ebd78ff07cabac348b60bb Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Thu, 16 Jan 2025 18:42:37 -0800
Subject: [PATCH 02/10] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20?=
=?UTF-8?q?changes=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
llvm/lib/ObjectYAML/ELFYAML.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 83e6cf76dd746f..7e94d01a971534 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1588,7 +1588,7 @@ static bool isInteger(StringRef Val) {
void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
- ELFYAML::ELF_SHT Type = ELF::ET_NONE;
+ ELFYAML::ELF_SHT Type;
StringRef TypeStr;
if (IO.outputting()) {
if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
>From a1558db7c994e18b08e86056970b3d823fba0355 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 16 Jan 2025 19:39:39 -0800
Subject: [PATCH 03/10] error()
Created using spr 1.3.4
---
llvm/include/llvm/Support/YAMLTraits.h | 4 +++-
llvm/lib/ObjectYAML/ELFYAML.cpp | 2 +-
llvm/lib/Support/YAMLTraits.cpp | 4 ++++
3 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index eca26e90845bf6..e707a445012b51 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -819,6 +819,7 @@ class IO {
virtual NodeKind getNodeKind() = 0;
virtual void setError(const Twine &) = 0;
+ virtual std::error_code error() = 0;
virtual void setAllowUnknownKeys(bool Allow);
template <typename T>
@@ -1448,7 +1449,7 @@ class Input : public IO {
~Input() override;
// Check if there was an syntax or semantic error during parsing.
- std::error_code error();
+ std::error_code error() override;
private:
bool outputting() const override;
@@ -1631,6 +1632,7 @@ class Output : public IO {
void scalarTag(std::string &) override;
NodeKind getNodeKind() override;
void setError(const Twine &message) override;
+ std::error_code error() override;
bool canElideEmptySequence() override;
// These are only used by operator<<. They could be private
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 961815392a1348..e0e941cff94c52 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1601,7 +1601,7 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
TypeStr = getStringValue(IO, "Type");
if (TypeStr.starts_with("SHT_") || isInteger(TypeStr)) {
IO.mapRequired("Type", Type);
- if (static_cast<Input&>(IO).error())
+ if (IO.error())
Type = ELF::SHT_NULL;
}
}
diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp
index f326422138488c..08c0ae4b1e332d 100644
--- a/llvm/lib/Support/YAMLTraits.cpp
+++ b/llvm/lib/Support/YAMLTraits.cpp
@@ -750,6 +750,10 @@ void Output::scalarTag(std::string &Tag) {
void Output::setError(const Twine &message) {
}
+std::error_code Output::error() {
+ return {};
+}
+
bool Output::canElideEmptySequence() {
// Normally, with an optional key/value where the value is an empty sequence,
// the whole key/value can be not written. But, that produces wrong yaml
>From 94d1be74e2d7f928e45be8c70d7581037c17bde9 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sat, 18 Jan 2025 12:28:08 -0800
Subject: [PATCH 04/10] update
Created using spr 1.3.4
---
llvm/include/llvm/Support/YAMLTraits.h | 4 +---
llvm/lib/Support/YAMLTraits.cpp | 2 --
2 files changed, 1 insertion(+), 5 deletions(-)
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index e707a445012b51..eca26e90845bf6 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -819,7 +819,6 @@ class IO {
virtual NodeKind getNodeKind() = 0;
virtual void setError(const Twine &) = 0;
- virtual std::error_code error() = 0;
virtual void setAllowUnknownKeys(bool Allow);
template <typename T>
@@ -1449,7 +1448,7 @@ class Input : public IO {
~Input() override;
// Check if there was an syntax or semantic error during parsing.
- std::error_code error() override;
+ std::error_code error();
private:
bool outputting() const override;
@@ -1632,7 +1631,6 @@ class Output : public IO {
void scalarTag(std::string &) override;
NodeKind getNodeKind() override;
void setError(const Twine &message) override;
- std::error_code error() override;
bool canElideEmptySequence() override;
// These are only used by operator<<. They could be private
diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp
index 28642e004c4f43..f326422138488c 100644
--- a/llvm/lib/Support/YAMLTraits.cpp
+++ b/llvm/lib/Support/YAMLTraits.cpp
@@ -750,8 +750,6 @@ void Output::scalarTag(std::string &Tag) {
void Output::setError(const Twine &message) {
}
-std::error_code Output::error() { return {}; }
-
bool Output::canElideEmptySequence() {
// Normally, with an optional key/value where the value is an empty sequence,
// the whole key/value can be not written. But, that produces wrong yaml
>From b340b41911de3c9f4baf9f499272c2e3e41d96fe Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 20 Jan 2025 23:01:09 -0800
Subject: [PATCH 05/10] remove comment and move initialization
Created using spr 1.3.4
---
llvm/lib/ObjectYAML/ELFYAML.cpp | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 4b6e77783aaaf0..7c99e971adbad2 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1588,7 +1588,7 @@ static bool isInteger(StringRef Val) {
void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
- ELFYAML::ELF_SHT Type;
+ ELFYAML::ELF_SHT Type = ELF::SHT_NULL;
StringRef TypeStr;
if (IO.outputting()) {
if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
@@ -1599,8 +1599,6 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
// When the Type string does not have a "SHT_" prefix, we know it is not a
// description of a regular ELF output section.
TypeStr = getStringValue(IO, "Type");
- // To for fallback to `default` in switch below on error.
- Type = ELF::SHT_NULL;
if (TypeStr.starts_with("SHT_") || isInteger(TypeStr))
IO.mapRequired("Type", Type);
}
>From 14fb5e44b79f6d58cc81f47b9a54500dcab46a4b Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 21 Jan 2025 09:19:37 -0800
Subject: [PATCH 06/10] test
Created using spr 1.3.4
---
.../tools/yaml2obj/ELF/section-headers.yaml | 12 -------
.../test/tools/yaml2obj/ELF/section-type.yaml | 32 +++++++++++++++++--
2 files changed, 30 insertions(+), 14 deletions(-)
diff --git a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
index 619ad864b6f20f..4b61920f139a06 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
@@ -455,15 +455,3 @@ Sections:
# LINK-NEXT: [ 3] .ref.last PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]] 4
# LINK-NEXT: [ 4] .shstrtab
-# RUN: not yaml2obj %s --docnum=15 -o %t15 2>&1 | FileCheck %s --check-prefix=UNKNOWN_SECTION
-
---- !ELF
-FileHeader:
- Class: ELFCLASS64
- Data: ELFDATA2LSB
- Type: ET_REL
-Sections:
- - Name: .foo
- Type: FOO_SECTION
-
-# UNKNOWN_SECTION: error: invalid hex32 number
diff --git a/llvm/test/tools/yaml2obj/ELF/section-type.yaml b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
index ad2edd942cc2aa..63c4b9b0e583b8 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-type.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
@@ -1,5 +1,5 @@
-# RUN: yaml2obj %s -o %t
-# RUN: llvm-readobj --sections %t | FileCheck %s
+# RUN: yaml2obj %s --docnum=1 -o %t1
+# RUN: llvm-readobj --sections %t1 | FileCheck %s
# CHECK: Name: enum
# CHECK: Type: SHT_PROGBITS
@@ -25,3 +25,31 @@ Sections:
Type: 0xabcd
- Name: decimal
Type: 1234
+
+
+# RUN: not yaml2obj %s --docnum=2 -o %t2 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+Sections:
+ - Name: .foo
+ Type: FOO_SECTION
+
+# UNKNOWN-TYPE: error: invalid hex32 number
+
+
+# RUN: not yaml2obj %s --docnum=3 -o %t3 -DMACHINE=EM_NONE 2>&1 | FileCheck %s --check-prefix=UNKNOWN-SHT-TYPE
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+Sections:
+ - Name: .foo
+ Type: SHT_MIPS_ABIFLAGS
+
+# UNKNOWN-SHT-TYPE: error: invalid hex32 number
>From 36ce09c3bffae3a9f1d79a4a76247f8a63f272ce Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 21 Jan 2025 10:20:46 -0800
Subject: [PATCH 07/10] rebase
Created using spr 1.3.4
---
llvm/test/tools/yaml2obj/ELF/section-headers.yaml | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
index 4b61920f139a06..5bc6ac5496f4d4 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
@@ -454,4 +454,3 @@ Sections:
# LINK-NEXT: [ 2] .ref.first PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]] 1
# LINK-NEXT: [ 3] .ref.last PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]] 4
# LINK-NEXT: [ 4] .shstrtab
-
>From cd55931ef82e8b3a10cf186b6044d3736bc62089 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 22 Jan 2025 10:44:46 -0800
Subject: [PATCH 08/10] test comments
Created using spr 1.3.4
---
llvm/test/tools/yaml2obj/ELF/section-type.yaml | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/test/tools/yaml2obj/ELF/section-type.yaml b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
index 63c4b9b0e583b8..e389f2030c223f 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-type.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
@@ -26,6 +26,7 @@ Sections:
- Name: decimal
Type: 1234
+## Check that we can handle unknown chunk types.
# RUN: not yaml2obj %s --docnum=2 -o %t2 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
@@ -36,12 +37,13 @@ FileHeader:
Type: ET_REL
Sections:
- Name: .foo
- Type: FOO_SECTION
+ Type: UNKNOWN_TYPE
# UNKNOWN-TYPE: error: invalid hex32 number
+## Check that we can handle unknown section types.
-# RUN: not yaml2obj %s --docnum=3 -o %t3 -DMACHINE=EM_NONE 2>&1 | FileCheck %s --check-prefix=UNKNOWN-SHT-TYPE
+# RUN: not yaml2obj %s --docnum=3 -o %t3 2>&1 | FileCheck %s --check-prefix=UNKNOWN-SHT-TYPE
--- !ELF
FileHeader:
@@ -50,6 +52,6 @@ FileHeader:
Type: ET_REL
Sections:
- Name: .foo
- Type: SHT_MIPS_ABIFLAGS
+ Type: SHT_UNKNOWN_TYPE
# UNKNOWN-SHT-TYPE: error: invalid hex32 number
>From 74f37d714fdc1e05d29e803dd8453c2322ba7f0a Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 22 Jan 2025 10:48:08 -0800
Subject: [PATCH 09/10] remove check line
Created using spr 1.3.4
---
llvm/test/tools/yaml2obj/ELF/section-type.yaml | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/test/tools/yaml2obj/ELF/section-type.yaml b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
index e389f2030c223f..9c4163a712c876 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-type.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
@@ -43,7 +43,7 @@ Sections:
## Check that we can handle unknown section types.
-# RUN: not yaml2obj %s --docnum=3 -o %t3 2>&1 | FileCheck %s --check-prefix=UNKNOWN-SHT-TYPE
+# RUN: not yaml2obj %s --docnum=3 -o %t3 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
--- !ELF
FileHeader:
@@ -54,4 +54,3 @@ Sections:
- Name: .foo
Type: SHT_UNKNOWN_TYPE
-# UNKNOWN-SHT-TYPE: error: invalid hex32 number
>From 95a6943cbc4ec641fad19caa045124fa62507ba7 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 22 Jan 2025 10:52:58 -0800
Subject: [PATCH 10/10] dedup
Created using spr 1.3.4
---
.../test/tools/yaml2obj/ELF/section-type.yaml | 21 ++++---------------
1 file changed, 4 insertions(+), 17 deletions(-)
diff --git a/llvm/test/tools/yaml2obj/ELF/section-type.yaml b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
index 9c4163a712c876..f9af7c4c2c4e14 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-type.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
@@ -26,9 +26,10 @@ Sections:
- Name: decimal
Type: 1234
-## Check that we can handle unknown chunk types.
+## Check that we can handle unknown section and chunk types.
-# RUN: not yaml2obj %s --docnum=2 -o %t2 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
+# RUN: not yaml2obj %s --docnum=2 -DSECTION_TYPE=UNKNOWN_TYPE -o %t2 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
+# RUN: not yaml2obj %s --docnum=2 -DSECTION_TYPE=SHT_UNKNOWN_TYPE -o %t2 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
--- !ELF
FileHeader:
@@ -37,20 +38,6 @@ FileHeader:
Type: ET_REL
Sections:
- Name: .foo
- Type: UNKNOWN_TYPE
+ Type: [[SECTION_TYPE]]
# UNKNOWN-TYPE: error: invalid hex32 number
-
-## Check that we can handle unknown section types.
-
-# RUN: not yaml2obj %s --docnum=3 -o %t3 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
-
---- !ELF
-FileHeader:
- Class: ELFCLASS64
- Data: ELFDATA2LSB
- Type: ET_REL
-Sections:
- - Name: .foo
- Type: SHT_UNKNOWN_TYPE
-
More information about the llvm-commits
mailing list