Skip to content

Commit 8e9c531

Browse files
authored
[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.
1 parent f626b1f commit 8e9c531

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

llvm/lib/InterfaceStub/IFSHandler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ Expected<std::unique_ptr<IFSStub>> ifs::readIFSFromBuffer(StringRef Buf) {
201201
"IFS arch '" + *Stub->Target.ArchString + "' is unsupported");
202202
Stub->Target.Arch = eMachine;
203203
}
204+
for (const auto &Item : Stub->Symbols) {
205+
if (Item.Type == IFSSymbolType::Unknown)
206+
return createStringError(
207+
std::make_error_code(std::errc::invalid_argument),
208+
"IFS symbol type for symbol '" + Item.Name + "' is unsupported");
209+
}
204210
return std::move(Stub);
205211
}
206212

llvm/lib/InterfaceStub/IFSStub.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ uint8_t ifs::convertIFSSymbolTypeToELF(IFSSymbolType SymbolType) {
8989
case IFSSymbolType::TLS:
9090
return ELF::STT_TLS;
9191
case IFSSymbolType::NoType:
92-
default:
9392
return ELF::STT_NOTYPE;
93+
default:
94+
llvm_unreachable("unknown symbol type");
9495
}
9596
}
9697

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# RUN: not llvm-ifs --output-ifs=- %s 2>&1 | FileCheck %s
2+
3+
--- !ifs-v1
4+
SoName: somelib.so
5+
IfsVersion: 3.0
6+
Target: { ObjectFormat: ELF, Arch: Aarch64, Endianness: little, BitWidth: 64 }
7+
Symbols:
8+
- { Name: nor, Type: bogus, Undefined: true }
9+
...
10+
11+
# CHECK: error: IFS symbol type for symbol 'nor' is unsupported

llvm/unittests/InterfaceStub/ELFYAMLTest.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ TEST(ElfYamlTextAPI, YAMLReadableTBE) {
5656
EXPECT_STREQ(Stub->NeededLibs[2].c_str(), "libbar.so");
5757
}
5858

59+
TEST(ElfYamlTextAPI, YAMLReadsInvalidSymbols) {
60+
const char Data[] =
61+
"--- !ifs-v1\n"
62+
"IfsVersion: 1.0\n"
63+
"SoName: test.so\n"
64+
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: little, "
65+
"BitWidth: 64 }\n"
66+
"Symbols:\n"
67+
" - { Name: not, Type: File, Undefined: true, Size: 111, "
68+
"Weak: true, Warning: \'All fields populated!\' }\n"
69+
"...\n";
70+
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
71+
ASSERT_THAT_ERROR(
72+
StubOrErr.takeError(),
73+
FailedWithMessage("IFS symbol type for symbol 'not' is unsupported"));
74+
}
75+
5976
TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
6077
const char Data[] =
6178
"--- !ifs-v1\n"
@@ -68,7 +85,7 @@ TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
6885
" - { Name: baz, Type: TLS, Size: 3 }\n"
6986
" - { Name: foo, Type: Func, Warning: \"Deprecated!\" }\n"
7087
" - { Name: nor, Type: NoType, Undefined: true }\n"
71-
" - { Name: not, Type: File, Undefined: true, Size: 111, "
88+
" - { Name: not, Type: NoType, Undefined: true, Size: 111, "
7289
"Weak: true, Warning: \'All fields populated!\' }\n"
7390
"...\n";
7491
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
@@ -116,7 +133,7 @@ TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
116133
IFSSymbol const &SymNot = *Iterator++;
117134
EXPECT_STREQ(SymNot.Name.c_str(), "not");
118135
EXPECT_EQ(*SymNot.Size, 111u);
119-
EXPECT_EQ(SymNot.Type, IFSSymbolType::Unknown);
136+
EXPECT_EQ(SymNot.Type, IFSSymbolType::NoType);
120137
EXPECT_TRUE(SymNot.Undefined);
121138
EXPECT_TRUE(SymNot.Weak);
122139
EXPECT_TRUE(SymNot.Warning);

0 commit comments

Comments
 (0)