[llvm] 8e9c531 - [llvm-ifs] Treat unknown symbol types as error. (#75872)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 11 17:02:02 PST 2024


Author: Haowei
Date: 2024-01-11T17:01:57-08:00
New Revision: 8e9c531922c4f9a1ee583ef3553b8529bb8e9a9a

URL: https://github.com/llvm/llvm-project/commit/8e9c531922c4f9a1ee583ef3553b8529bb8e9a9a
DIFF: https://github.com/llvm/llvm-project/commit/8e9c531922c4f9a1ee583ef3553b8529bb8e9a9a.diff

LOG: [llvm-ifs] Treat unknown symbol types as error. (#75872)

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.

Added: 
    llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test

Modified: 
    llvm/lib/InterfaceStub/IFSHandler.cpp
    llvm/lib/InterfaceStub/IFSStub.cpp
    llvm/unittests/InterfaceStub/ELFYAMLTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/InterfaceStub/IFSHandler.cpp b/llvm/lib/InterfaceStub/IFSHandler.cpp
index da46592bd3819e..e80a59a572d888 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..49e2b31600bcef
--- /dev/null
+++ b/llvm/test/tools/llvm-ifs/ifs-read-invalid-symbol-type.test
@@ -0,0 +1,11 @@
+# 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: 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..f18eea07508187 100644
--- a/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
+++ b/llvm/unittests/InterfaceStub/ELFYAMLTest.cpp
@@ -56,6 +56,23 @@ 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(),
+      FailedWithMessage("IFS symbol type for symbol 'not' is unsupported"));
+}
+
 TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
   const char Data[] =
       "--- !ifs-v1\n"
@@ -68,7 +85,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 +133,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);


        


More information about the llvm-commits mailing list