[llvm] [llvm-ifs] Treat unknown symbol types as error. (PR #75872)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 20 14:44:02 PST 2023
https://github.com/zeroomega updated https://github.com/llvm/llvm-project/pull/75872
>From 3374f89d95e7888e0edb01a205cb2b4c85185265 Mon Sep 17 00:00:00 2001
From: Haowei Wu <haowei at google.com>
Date: Mon, 18 Dec 2023 15:04:57 -0800
Subject: [PATCH 1/3] [llvm-ifs] Treat unknown symbol types as error.
Before this patch, when an unknown symbol type is used in IFS stub, it
will be treated as a NO_TYPE and parsed without error. This patch makes
llvm-ifs throw an error when this scenario happens.
---
llvm/lib/InterfaceStub/IFSHandler.cpp | 6 ++++++
llvm/lib/InterfaceStub/IFSStub.cpp | 3 ++-
.../llvm-ifs/ifs-read-invalid-symbol-type.test | 15 +++++++++++++++
llvm/unittests/InterfaceStub/ELFYAMLTest.cpp | 4 ++--
4 files changed, 25 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test
diff --git a/llvm/lib/InterfaceStub/IFSHandler.cpp b/llvm/lib/InterfaceStub/IFSHandler.cpp
index da46592bd3819e..0178ff882b93bb 100644
--- a/llvm/lib/InterfaceStub/IFSHandler.cpp
+++ b/llvm/lib/InterfaceStub/IFSHandler.cpp
@@ -201,6 +201,12 @@ Expected<std::unique_ptr<IFSStub>> ifs::readIFSFromBuffer(StringRef Buf) {
"IFS arch '" + *Stub->Target.ArchString + "' is unsupported");
Stub->Target.Arch = eMachine;
}
+ for (const auto &item : Stub->Symbols) {
+ if (item.Type == IFSSymbolType::Unknown)
+ return createStringError(
+ std::make_error_code(std::errc::invalid_argument),
+ "IFS symbol type for symbol '" + item.Name + "' is unsupported");
+ }
return std::move(Stub);
}
diff --git a/llvm/lib/InterfaceStub/IFSStub.cpp b/llvm/lib/InterfaceStub/IFSStub.cpp
index f043f7e9e383f2..cde15ccfdba069 100644
--- a/llvm/lib/InterfaceStub/IFSStub.cpp
+++ b/llvm/lib/InterfaceStub/IFSStub.cpp
@@ -89,8 +89,9 @@ uint8_t ifs::convertIFSSymbolTypeToELF(IFSSymbolType SymbolType) {
case IFSSymbolType::TLS:
return ELF::STT_TLS;
case IFSSymbolType::NoType:
- default:
return ELF::STT_NOTYPE;
+ default:
+ llvm_unreachable("unknown symbol type");
}
}
diff --git a/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test b/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test
new file mode 100644
index 00000000000000..b4b8ede7f7853c
--- /dev/null
+++ b/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test
@@ -0,0 +1,15 @@
+# RUN: not llvm-ifs --output-ifs=- %s 2>&1 | FileCheck %s
+
+--- !ifs-v1
+SoName: somelib.so
+IfsVersion: 3.0
+Target: { ObjectFormat: ELF, Arch: Aarch64, Endianness: little, BitWidth: 64 }
+Symbols:
+ - { Name: foo, Type: Func }
+ - { Name: bar, Type: Object, Size: 42 }
+ - { Name: baz, Type: Object, Size: 8 }
+ - { Name: not, Type: Object, Size: 128, Undefined: true }
+ - { Name: nor, Type: bogus, Undefined: true }
+...
+
+# CHECK: error: IFS symbol type for symbol 'nor' is unsupported
diff --git a/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp b/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
index 5008f053888629..83d1e5e09fc0b3 100644
--- a/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
+++ b/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
@@ -68,7 +68,7 @@ TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
" - { Name: baz, Type: TLS, Size: 3 }\n"
" - { Name: foo, Type: Func, Warning: \"Deprecated!\" }\n"
" - { Name: nor, Type: NoType, Undefined: true }\n"
- " - { Name: not, Type: File, Undefined: true, Size: 111, "
+ " - { Name: not, Type: NoType, Undefined: true, Size: 111, "
"Weak: true, Warning: \'All fields populated!\' }\n"
"...\n";
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
@@ -116,7 +116,7 @@ TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
IFSSymbol const &SymNot = *Iterator++;
EXPECT_STREQ(SymNot.Name.c_str(), "not");
EXPECT_EQ(*SymNot.Size, 111u);
- EXPECT_EQ(SymNot.Type, IFSSymbolType::Unknown);
+ EXPECT_EQ(SymNot.Type, IFSSymbolType::NoType);
EXPECT_TRUE(SymNot.Undefined);
EXPECT_TRUE(SymNot.Weak);
EXPECT_TRUE(SymNot.Warning);
>From 8212180c5250a57b153efcb2dabcb8ad0c5b22a0 Mon Sep 17 00:00:00 2001
From: Haowei Wu <haowei at google.com>
Date: Tue, 19 Dec 2023 11:11:26 -0800
Subject: [PATCH 2/3] fixup! [llvm-ifs] Treat unknown symbol types as error.
---
.../llvm-ifs/ifs-read-invalid-symbol-type.test | 4 ----
llvm/unittests/InterfaceStub/ELFYAMLTest.cpp | 15 +++++++++++++++
2 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test b/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test
index b4b8ede7f7853c..49e2b31600bcef 100644
--- a/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test
+++ b/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test
@@ -5,10 +5,6 @@ SoName: somelib.so
IfsVersion: 3.0
Target: { ObjectFormat: ELF, Arch: Aarch64, Endianness: little, BitWidth: 64 }
Symbols:
- - { Name: foo, Type: Func }
- - { Name: bar, Type: Object, Size: 42 }
- - { Name: baz, Type: Object, Size: 8 }
- - { Name: not, Type: Object, Size: 128, Undefined: true }
- { Name: nor, Type: bogus, Undefined: true }
...
diff --git a/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp b/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
index 83d1e5e09fc0b3..ef67edadf713db 100644
--- a/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
+++ b/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
@@ -56,6 +56,21 @@ TEST(ElfYamlTextAPI, YAMLReadableTBE) {
EXPECT_STREQ(Stub->NeededLibs[2].c_str(), "libbar.so");
}
+TEST(ElfYamlTextAPI, YAMLReadsInvalidSymbols) {
+ const char Data[] =
+ "--- !ifs-v1\n"
+ "IfsVersion: 1.0\n"
+ "SoName: test.so\n"
+ "Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: little, "
+ "BitWidth: 64 }\n"
+ "Symbols:\n"
+ " - { Name: not, Type: File, Undefined: true, Size: 111, "
+ "Weak: true, Warning: \'All fields populated!\' }\n"
+ "...\n";
+ Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
+ ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
+}
+
TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
const char Data[] =
"--- !ifs-v1\n"
>From 868c98d77f266dfa75675c992140991c845bc926 Mon Sep 17 00:00:00 2001
From: Haowei Wu <haowei at google.com>
Date: Wed, 20 Dec 2023 14:43:05 -0800
Subject: [PATCH 3/3] fixup! fixup! [llvm-ifs] Treat unknown symbol types as
error.
---
llvm/lib/InterfaceStub/IFSHandler.cpp | 6 +++---
llvm/unittests/InterfaceStub/ELFYAMLTest.cpp | 4 +++-
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/InterfaceStub/IFSHandler.cpp b/llvm/lib/InterfaceStub/IFSHandler.cpp
index 0178ff882b93bb..e80a59a572d888 100644
--- a/llvm/lib/InterfaceStub/IFSHandler.cpp
+++ b/llvm/lib/InterfaceStub/IFSHandler.cpp
@@ -201,11 +201,11 @@ Expected<std::unique_ptr<IFSStub>> ifs::readIFSFromBuffer(StringRef Buf) {
"IFS arch '" + *Stub->Target.ArchString + "' is unsupported");
Stub->Target.Arch = eMachine;
}
- for (const auto &item : Stub->Symbols) {
- if (item.Type == IFSSymbolType::Unknown)
+ for (const auto &Item : Stub->Symbols) {
+ if (Item.Type == IFSSymbolType::Unknown)
return createStringError(
std::make_error_code(std::errc::invalid_argument),
- "IFS symbol type for symbol '" + item.Name + "' is unsupported");
+ "IFS symbol type for symbol '" + Item.Name + "' is unsupported");
}
return std::move(Stub);
}
diff --git a/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp b/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
index ef67edadf713db..f18eea07508187 100644
--- a/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
+++ b/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
@@ -68,7 +68,9 @@ TEST(ElfYamlTextAPI, YAMLReadsInvalidSymbols) {
"Weak: true, Warning: \'All fields populated!\' }\n"
"...\n";
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
- ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
+ ASSERT_THAT_ERROR(
+ StubOrErr.takeError(),
+ FailedWithMessage("IFS symbol type for symbol 'not' is unsupported"));
}
TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
More information about the llvm-commits
mailing list