forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathELFYAMLTest.cpp
255 lines (232 loc) · 9.38 KB
/
ELFYAMLTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//===- llvm/unittests/TextAPI/YAMLTest.cpp --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===-----------------------------------------------------------------------===/
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/InterfaceStub/IFSHandler.h"
#include "llvm/InterfaceStub/IFSStub.h"
#include "llvm/Support/Error.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <string>
using namespace llvm;
using namespace llvm::ELF;
using namespace llvm::ifs;
void compareByLine(StringRef LHS, StringRef RHS) {
StringRef Line1;
StringRef Line2;
while (LHS.size() > 0 && RHS.size() > 0) {
std::tie(Line1, LHS) = LHS.split('\n');
std::tie(Line2, RHS) = RHS.split('\n');
// Comparing StringRef objects works, but has messy output when not equal.
// Using STREQ on StringRef.data() doesn't work since these substrings are
// not null terminated.
// This is inefficient, but forces null terminated strings that can be
// cleanly compared.
EXPECT_STREQ(Line1.str().data(), Line2.str().data());
}
}
TEST(ElfYamlTextAPI, YAMLReadableTBE) {
const char Data[] = "--- !ifs-v1\n"
"IfsVersion: 1.0\n"
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: "
"little, BitWidth: 64 }\n"
"NeededLibs: [libc.so, libfoo.so, libbar.so]\n"
"Symbols:\n"
" - { Name: foo, Type: Func, Undefined: true }\n"
"...\n";
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
std::unique_ptr<IFSStub> Stub = std::move(StubOrErr.get());
EXPECT_NE(Stub.get(), nullptr);
EXPECT_FALSE(Stub->SoName.hasValue());
EXPECT_TRUE(Stub->Target.Arch.hasValue());
EXPECT_EQ(Stub->Target.Arch.getValue(), (uint16_t)llvm::ELF::EM_X86_64);
EXPECT_EQ(Stub->NeededLibs.size(), 3u);
EXPECT_STREQ(Stub->NeededLibs[0].c_str(), "libc.so");
EXPECT_STREQ(Stub->NeededLibs[1].c_str(), "libfoo.so");
EXPECT_STREQ(Stub->NeededLibs[2].c_str(), "libbar.so");
}
TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
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: bar, Type: Object, Size: 42 }\n"
" - { 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, "
"Weak: true, Warning: \'All fields populated!\' }\n"
"...\n";
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
std::unique_ptr<IFSStub> Stub = std::move(StubOrErr.get());
EXPECT_NE(Stub.get(), nullptr);
EXPECT_TRUE(Stub->SoName.hasValue());
EXPECT_STREQ(Stub->SoName->c_str(), "test.so");
EXPECT_EQ(Stub->Symbols.size(), 5u);
auto Iterator = Stub->Symbols.begin();
IFSSymbol const &SymBar = *Iterator++;
EXPECT_STREQ(SymBar.Name.c_str(), "bar");
EXPECT_EQ(SymBar.Size, 42u);
EXPECT_EQ(SymBar.Type, IFSSymbolType::Object);
EXPECT_FALSE(SymBar.Undefined);
EXPECT_FALSE(SymBar.Weak);
EXPECT_FALSE(SymBar.Warning.hasValue());
IFSSymbol const &SymBaz = *Iterator++;
EXPECT_STREQ(SymBaz.Name.c_str(), "baz");
EXPECT_EQ(SymBaz.Size, 3u);
EXPECT_EQ(SymBaz.Type, IFSSymbolType::TLS);
EXPECT_FALSE(SymBaz.Undefined);
EXPECT_FALSE(SymBaz.Weak);
EXPECT_FALSE(SymBaz.Warning.hasValue());
IFSSymbol const &SymFoo = *Iterator++;
EXPECT_STREQ(SymFoo.Name.c_str(), "foo");
EXPECT_EQ(SymFoo.Size, 0u);
EXPECT_EQ(SymFoo.Type, IFSSymbolType::Func);
EXPECT_FALSE(SymFoo.Undefined);
EXPECT_FALSE(SymFoo.Weak);
EXPECT_TRUE(SymFoo.Warning.hasValue());
EXPECT_STREQ(SymFoo.Warning->c_str(), "Deprecated!");
IFSSymbol const &SymNor = *Iterator++;
EXPECT_STREQ(SymNor.Name.c_str(), "nor");
EXPECT_EQ(SymNor.Size, 0u);
EXPECT_EQ(SymNor.Type, IFSSymbolType::NoType);
EXPECT_TRUE(SymNor.Undefined);
EXPECT_FALSE(SymNor.Weak);
EXPECT_FALSE(SymNor.Warning.hasValue());
IFSSymbol const &SymNot = *Iterator++;
EXPECT_STREQ(SymNot.Name.c_str(), "not");
EXPECT_EQ(SymNot.Size, 111u);
EXPECT_EQ(SymNot.Type, IFSSymbolType::Unknown);
EXPECT_TRUE(SymNot.Undefined);
EXPECT_TRUE(SymNot.Weak);
EXPECT_TRUE(SymNot.Warning.hasValue());
EXPECT_STREQ(SymNot.Warning->c_str(), "All fields populated!");
}
TEST(ElfYamlTextAPI, YAMLReadsNoTBESyms) {
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"
"...\n";
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
std::unique_ptr<IFSStub> Stub = std::move(StubOrErr.get());
EXPECT_NE(Stub.get(), nullptr);
EXPECT_EQ(0u, Stub->Symbols.size());
}
TEST(ElfYamlTextAPI, YAMLUnreadableTBE) {
// Can't read: wrong format/version.
const char Data[] = "--- !tapi-tbz\n"
"IfsVersion: z.3\n"
"SoName: test.so\n"
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: "
"little, BitWidth: 64 }\n"
"Symbols:\n"
" foo: { Type: Func, Undefined: true }\n";
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
}
TEST(ElfYamlTextAPI, YAMLUnsupportedVersion) {
const char Data[] = "--- !ifs-v1\n"
"IfsVersion: 9.9.9\n"
"SoName: test.so\n"
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: "
"little, BitWidth: 64 }\n"
"Symbols: []\n"
"...\n";
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
std::string ErrorMessage = toString(StubOrErr.takeError());
EXPECT_EQ("IFS version 9.9.9 is unsupported.", ErrorMessage);
}
TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
const char Expected[] =
"--- !ifs-v1\n"
"IfsVersion: 1.0\n"
"Target: { ObjectFormat: ELF, Arch: AArch64, Endianness: "
"little, BitWidth: 64 }\n"
"Symbols:\n"
" - { Name: bar, Type: Func, Weak: true }\n"
" - { Name: foo, Type: NoType, Size: 99, Warning: Does nothing }\n"
" - { Name: nor, Type: Func, Undefined: true }\n"
" - { Name: not, Type: Unknown, Size: 12345678901234 }\n"
"...\n";
IFSStub Stub;
Stub.IfsVersion = VersionTuple(1, 0);
Stub.Target.Arch = ELF::EM_AARCH64;
Stub.Target.BitWidth = IFSBitWidthType::IFS64;
Stub.Target.Endianness = IFSEndiannessType::Little;
Stub.Target.ObjectFormat = "ELF";
IFSSymbol SymBar("bar");
SymBar.Size = 128u;
SymBar.Type = IFSSymbolType::Func;
SymBar.Undefined = false;
SymBar.Weak = true;
IFSSymbol SymFoo("foo");
SymFoo.Size = 99u;
SymFoo.Type = IFSSymbolType::NoType;
SymFoo.Undefined = false;
SymFoo.Weak = false;
SymFoo.Warning = "Does nothing";
IFSSymbol SymNor("nor");
SymNor.Size = 1234u;
SymNor.Type = IFSSymbolType::Func;
SymNor.Undefined = true;
SymNor.Weak = false;
IFSSymbol SymNot("not");
SymNot.Size = 12345678901234u;
SymNot.Type = IFSSymbolType::Unknown;
SymNot.Undefined = false;
SymNot.Weak = false;
// Symbol order is preserved instead of being sorted.
Stub.Symbols.push_back(SymBar);
Stub.Symbols.push_back(SymFoo);
Stub.Symbols.push_back(SymNor);
Stub.Symbols.push_back(SymNot);
// Ensure move constructor works as expected.
IFSStub Moved = std::move(Stub);
std::string Result;
raw_string_ostream OS(Result);
ASSERT_THAT_ERROR(writeIFSToOutputStream(OS, Moved), Succeeded());
Result = OS.str();
compareByLine(Result.c_str(), Expected);
}
TEST(ElfYamlTextAPI, YAMLWritesNoTBESyms) {
const char Expected[] = "--- !ifs-v1\n"
"IfsVersion: 1.0\n"
"SoName: nosyms.so\n"
"Target: { ObjectFormat: ELF, Arch: x86_64, "
"Endianness: little, BitWidth: 64 }\n"
"NeededLibs:\n"
" - libc.so\n"
" - libfoo.so\n"
" - libbar.so\n"
"Symbols: []\n"
"...\n";
IFSStub Stub;
Stub.IfsVersion = VersionTuple(1, 0);
Stub.SoName = "nosyms.so";
Stub.Target.Arch = ELF::EM_X86_64;
Stub.Target.BitWidth = IFSBitWidthType::IFS64;
Stub.Target.Endianness = IFSEndiannessType::Little;
Stub.Target.ObjectFormat = "ELF";
Stub.NeededLibs.push_back("libc.so");
Stub.NeededLibs.push_back("libfoo.so");
Stub.NeededLibs.push_back("libbar.so");
std::string Result;
raw_string_ostream OS(Result);
ASSERT_THAT_ERROR(writeIFSToOutputStream(OS, Stub), Succeeded());
Result = OS.str();
compareByLine(Result.c_str(), Expected);
}