llvm.org GIT mirror llvm / 95e7e91
[TextAPI] Fix & Add tests for tbd files version 3. - There was a simple typo in TextStub code that prevented version 3 files to be read. - Included a version 3 unit test to handle the differences in the format. - Also a typo in Error.h inside the comments. https://reviews.llvm.org/D66041 This patch is from Cyndy Ishida <cyndy_ishida@apple.com>. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368630 91177308-0d34-0410-b5e6-96231b3b80d8 Juergen Ributzka a month ago
4 changed file(s) with 443 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
547547 /// Take ownership of the stored error.
548548 /// After calling this the Expected is in an indeterminate state that can
549549 /// only be safely destructed. No further calls (beside the destructor) should
550 /// be made on the Expected vaule.
550 /// be made on the Expected value.
551551 Error takeError() {
552552 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
553553 Unchecked = false;
533533
534534 // prope file type when reading.
535535 if (!IO.outputting()) {
536 if (IO.mapTag("!tapi-tbd-v2", false))
537 Ctx->FileKind = FileType::TBD_V2;
538 else if (IO.mapTag("!tapi-tbd-v3", false))
536 if (IO.mapTag("!tapi-tbd-v3", false))
537 Ctx->FileKind = FileType::TBD_V3;
538 else if (IO.mapTag("!tapi-tbd-v2", false))
539539 Ctx->FileKind = FileType::TBD_V2;
540540 else if (IO.mapTag("!tapi-tbd-v1", false) ||
541541 IO.mapTag("tag:yaml.org,2002:map", false))
546546 }
547547 }
548548
549 // Set file tyoe when writing.
549 // Set file type when writing.
550550 if (IO.outputting()) {
551551 switch (Ctx->FileKind) {
552552 default:
55 ELFYAMLTest.cpp
66 TextStubV1Tests.cpp
77 TextStubV2Tests.cpp
8 TextStubV3Tests.cpp
89 )
910
10 target_link_libraries(TextAPITests PRIVATE LLVMTestingSupport)
11 target_link_libraries(TextAPITests PRIVATE LLVMTestingSupport)
0 //===-- TextStubV3Tests.cpp - TBD V3 File Test ----------------------------===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===-----------------------------------------------------------------------===/
7
8 #include "llvm/TextAPI/MachO/InterfaceFile.h"
9 #include "llvm/TextAPI/MachO/TextAPIReader.h"
10 #include "llvm/TextAPI/MachO/TextAPIWriter.h"
11 #include "gtest/gtest.h"
12 #include
13 #include
14
15 using namespace llvm;
16 using namespace llvm::MachO;
17
18 struct ExportedSymbol {
19 SymbolKind Kind;
20 std::string Name;
21 bool WeakDefined;
22 bool ThreadLocalValue;
23 };
24 using ExportedSymbolSeq = std::vector;
25 using UUIDs = std::vector>;
26
27 inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
28 return std::tie(lhs.Kind, lhs.Name) < std::tie(rhs.Kind, rhs.Name);
29 }
30
31 inline bool operator==(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
32 return std::tie(lhs.Kind, lhs.Name, lhs.WeakDefined, lhs.ThreadLocalValue) ==
33 std::tie(rhs.Kind, rhs.Name, rhs.WeakDefined, rhs.ThreadLocalValue);
34 }
35
36 static ExportedSymbol TBDv3Symbols[] = {
37 {SymbolKind::GlobalSymbol, "$ld$hide$os9.0$_sym1", false, false},
38 {SymbolKind::GlobalSymbol, "_sym1", false, false},
39 {SymbolKind::GlobalSymbol, "_sym2", false, false},
40 {SymbolKind::GlobalSymbol, "_sym3", false, false},
41 {SymbolKind::GlobalSymbol, "_sym4", false, false},
42 {SymbolKind::GlobalSymbol, "_sym5", false, false},
43 {SymbolKind::GlobalSymbol, "_tlv1", false, true},
44 {SymbolKind::GlobalSymbol, "_tlv3", false, true},
45 {SymbolKind::GlobalSymbol, "_weak1", true, false},
46 {SymbolKind::GlobalSymbol, "_weak2", true, false},
47 {SymbolKind::GlobalSymbol, "_weak3", true, false},
48 {SymbolKind::ObjectiveCClass, "class1", false, false},
49 {SymbolKind::ObjectiveCClass, "class2", false, false},
50 {SymbolKind::ObjectiveCClass, "class3", false, false},
51 {SymbolKind::ObjectiveCClassEHType, "class1", false, false},
52 {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar1", false, false},
53 {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar2", false, false},
54 {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar3", false, false},
55 };
56
57 namespace TBDv3 {
58
59 TEST(TBDv3, ReadFile) {
60 static const char tbd_v3_file1[] =
61 "--- !tapi-tbd-v3\n"
62 "archs: [ armv7, arm64 ]\n"
63 "uuids: [ 'armv7: 00000000-0000-0000-0000-000000000000',\n"
64 " 'arm64: 11111111-1111-1111-1111-111111111111']\n"
65 "platform: ios\n"
66 "flags: [ installapi ]\n"
67 "install-name: Test.dylib\n"
68 "current-version: 2.3.4\n"
69 "compatibility-version: 1.0\n"
70 "swift-abi-version: 1.1\n"
71 "parent-umbrella: Umbrella.dylib\n"
72 "exports:\n"
73 " - archs: [ armv7, arm64 ]\n"
74 " allowable-clients: [ clientA ]\n"
75 " re-exports: [ /usr/lib/libfoo.dylib ]\n"
76 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
77 " objc-classes: [ class1, class2 ]\n"
78 " objc-eh-types: [ class1 ]\n"
79 " objc-ivars: [ class1._ivar1, class1._ivar2 ]\n"
80 " weak-def-symbols: [ _weak1, _weak2 ]\n"
81 " thread-local-symbols: [ _tlv1, _tlv3 ]\n"
82 " - archs: [ armv7 ]\n"
83 " symbols: [ _sym5 ]\n"
84 " objc-classes: [ class3 ]\n"
85 " objc-ivars: [ class1._ivar3 ]\n"
86 " weak-def-symbols: [ _weak3 ]\n"
87 " thread-local-symbols: [ _tlv3 ]\n"
88 "...\n";
89
90 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v3_file1, "Test.tbd");
91 auto Result = TextAPIReader::get(std::move(Buffer));
92 EXPECT_TRUE(!!Result);
93 auto File = std::move(Result.get());
94 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
95 auto Archs = AK_armv7 | AK_arm64;
96 EXPECT_EQ(Archs, File->getArchitectures());
97 UUIDs uuids = {{AK_armv7, "00000000-0000-0000-0000-000000000000"},
98 {AK_arm64, "11111111-1111-1111-1111-111111111111"}};
99 EXPECT_EQ(uuids, File->uuids());
100 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
101 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
102 EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
103 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
104 EXPECT_EQ(2U, File->getSwiftABIVersion());
105 EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
106 EXPECT_TRUE(File->isTwoLevelNamespace());
107 EXPECT_TRUE(File->isApplicationExtensionSafe());
108 EXPECT_TRUE(File->isInstallAPI());
109 InterfaceFileRef client("clientA", Archs);
110 InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Archs);
111 EXPECT_EQ(1U, File->allowableClients().size());
112 EXPECT_EQ(client, File->allowableClients().front());
113 EXPECT_EQ(1U, File->reexportedLibraries().size());
114 EXPECT_EQ(reexport, File->reexportedLibraries().front());
115
116 ExportedSymbolSeq Exports;
117 for (const auto *Sym : File->symbols()) {
118 EXPECT_FALSE(Sym->isWeakReferenced());
119 EXPECT_FALSE(Sym->isUndefined());
120 Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(),
121 Sym->isWeakDefined(),
122 Sym->isThreadLocalValue()});
123 }
124 llvm::sort(Exports.begin(), Exports.end());
125
126 EXPECT_EQ(sizeof(TBDv3Symbols) / sizeof(ExportedSymbol), Exports.size());
127 EXPECT_TRUE(
128 std::equal(Exports.begin(), Exports.end(), std::begin(TBDv3Symbols)));
129 }
130
131 TEST(TBDv3, WriteFile) {
132 static const char tbd_v3_file3[] =
133 "--- !tapi-tbd-v3\n"
134 "archs: [ i386, x86_64 ]\n"
135 "platform: macosx\n"
136 "install-name: '/usr/lib/libfoo.dylib'\n"
137 "current-version: 1.2.3\n"
138 "compatibility-version: 0\n"
139 "swift-abi-version: 5\n"
140 "exports:\n"
141 " - archs: [ i386 ]\n"
142 " symbols: [ _sym1 ]\n"
143 " weak-def-symbols: [ _sym2 ]\n"
144 " thread-local-symbols: [ _sym3 ]\n"
145 " - archs: [ x86_64 ]\n"
146 " allowable-clients: [ clientA ]\n"
147 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
148 " objc-classes: [ Class1 ]\n"
149 " objc-eh-types: [ Class1 ]\n"
150 " objc-ivars: [ Class1._ivar1 ]\n"
151 "...\n";
152
153 InterfaceFile File;
154 File.setPath("libfoo.dylib");
155 File.setInstallName("/usr/lib/libfoo.dylib");
156 File.setFileType(FileType::TBD_V3);
157 File.setArchitectures(AK_i386 | AK_x86_64);
158 File.setPlatform(PlatformKind::macOS);
159 File.setCurrentVersion(PackedVersion(1, 2, 3));
160 File.setTwoLevelNamespace();
161 File.setApplicationExtensionSafe();
162 File.setSwiftABIVersion(5);
163 File.setObjCConstraint(ObjCConstraintType::Retain_Release);
164 File.addAllowableClient("clientA", AK_x86_64);
165 File.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64);
166 File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", AK_i386);
167 File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", AK_i386,
168 SymbolFlags::WeakDefined);
169 File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", AK_i386,
170 SymbolFlags::ThreadLocalValue);
171 File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", AK_x86_64);
172 File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", AK_x86_64);
173 File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
174 AK_x86_64);
175
176 SmallString<4096> Buffer;
177 raw_svector_ostream OS(Buffer);
178 auto Result = TextAPIWriter::writeToStream(OS, File);
179 EXPECT_FALSE(Result);
180 EXPECT_STREQ(tbd_v3_file3, Buffer.c_str());
181 }
182
183 TEST(TBDv3, Platform_macOS) {
184 static const char tbd_v1_platform_macos[] = "--- !tapi-tbd-v3\n"
185 "archs: [ x86_64 ]\n"
186 "platform: macosx\n"
187 "install-name: Test.dylib\n"
188 "...\n";
189
190 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_macos, "Test.tbd");
191 auto Result = TextAPIReader::get(std::move(Buffer));
192 EXPECT_TRUE(!!Result);
193 auto File = std::move(Result.get());
194 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
195 EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
196 }
197
198 TEST(TBDv3, Platform_iOS) {
199 static const char tbd_v1_platform_ios[] = "--- !tapi-tbd-v3\n"
200 "archs: [ arm64 ]\n"
201 "platform: ios\n"
202 "install-name: Test.dylib\n"
203 "...\n";
204
205 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_ios, "Test.tbd");
206 auto Result = TextAPIReader::get(std::move(Buffer));
207 EXPECT_TRUE(!!Result);
208 auto File = std::move(Result.get());
209 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
210 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
211 }
212
213 TEST(TBDv3, Platform_watchOS) {
214 static const char tbd_v1_platform_watchos[] = "--- !tapi-tbd-v3\n"
215 "archs: [ armv7k ]\n"
216 "platform: watchos\n"
217 "install-name: Test.dylib\n"
218 "...\n";
219
220 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_watchos, "Test.tbd");
221 auto Result = TextAPIReader::get(std::move(Buffer));
222 EXPECT_TRUE(!!Result);
223 auto File = std::move(Result.get());
224 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
225 EXPECT_EQ(PlatformKind::watchOS, File->getPlatform());
226 }
227
228 TEST(TBDv3, Platform_tvOS) {
229 static const char tbd_v1_platform_tvos[] = "--- !tapi-tbd-v3\n"
230 "archs: [ arm64 ]\n"
231 "platform: tvos\n"
232 "install-name: Test.dylib\n"
233 "...\n";
234
235 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_tvos, "Test.tbd");
236 auto Result = TextAPIReader::get(std::move(Buffer));
237 EXPECT_TRUE(!!Result);
238 auto File = std::move(Result.get());
239 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
240 EXPECT_EQ(PlatformKind::tvOS, File->getPlatform());
241 }
242
243 TEST(TBDv3, Platform_bridgeOS) {
244 static const char tbd_v1_platform_bridgeos[] = "--- !tapi-tbd-v3\n"
245 "archs: [ armv7k ]\n"
246 "platform: bridgeos\n"
247 "install-name: Test.dylib\n"
248 "...\n";
249
250 auto Buffer =
251 MemoryBuffer::getMemBuffer(tbd_v1_platform_bridgeos, "Test.tbd");
252 auto Result = TextAPIReader::get(std::move(Buffer));
253 EXPECT_TRUE(!!Result);
254 auto File = std::move(Result.get());
255 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
256 EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
257 }
258
259 TEST(TBDv3, Swift_1_0) {
260 static const char tbd_v1_swift_1_0[] = "--- !tapi-tbd-v3\n"
261 "archs: [ arm64 ]\n"
262 "platform: ios\n"
263 "install-name: Test.dylib\n"
264 "swift-abi-version: 1.0\n"
265 "...\n";
266
267 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_1_0, "Test.tbd");
268 auto Result = TextAPIReader::get(std::move(Buffer));
269 EXPECT_TRUE(!!Result);
270 auto File = std::move(Result.get());
271 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
272 EXPECT_EQ(1U, File->getSwiftABIVersion());
273 }
274
275 TEST(TBDv3, Swift_1_1) {
276 static const char tbd_v1_swift_1_1[] = "--- !tapi-tbd-v3\n"
277 "archs: [ arm64 ]\n"
278 "platform: ios\n"
279 "install-name: Test.dylib\n"
280 "swift-abi-version: 1.1\n"
281 "...\n";
282
283 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_1_1, "Test.tbd");
284 auto Result = TextAPIReader::get(std::move(Buffer));
285 EXPECT_TRUE(!!Result);
286 auto File = std::move(Result.get());
287 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
288 EXPECT_EQ(2U, File->getSwiftABIVersion());
289 }
290
291 TEST(TBDv3, Swift_2_0) {
292 static const char tbd_v1_swift_2_0[] = "--- !tapi-tbd-v3\n"
293 "archs: [ arm64 ]\n"
294 "platform: ios\n"
295 "install-name: Test.dylib\n"
296 "swift-abi-version: 2.0\n"
297 "...\n";
298
299 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_2_0, "Test.tbd");
300 auto Result = TextAPIReader::get(std::move(Buffer));
301 EXPECT_TRUE(!!Result);
302 auto File = std::move(Result.get());
303 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
304 EXPECT_EQ(3U, File->getSwiftABIVersion());
305 }
306
307 TEST(TBDv3, Swift_3_0) {
308 static const char tbd_v1_swift_3_0[] = "--- !tapi-tbd-v3\n"
309 "archs: [ arm64 ]\n"
310 "platform: ios\n"
311 "install-name: Test.dylib\n"
312 "swift-abi-version: 3.0\n"
313 "...\n";
314
315 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_3_0, "Test.tbd");
316 auto Result = TextAPIReader::get(std::move(Buffer));
317 EXPECT_TRUE(!!Result);
318 auto File = std::move(Result.get());
319 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
320 EXPECT_EQ(4U, File->getSwiftABIVersion());
321 }
322
323 TEST(TBDv3, Swift_4_0) {
324 static const char tbd_v1_swift_4_0[] = "--- !tapi-tbd-v3\n"
325 "archs: [ arm64 ]\n"
326 "platform: ios\n"
327 "install-name: Test.dylib\n"
328 "swift-abi-version: 4.0\n"
329 "...\n";
330
331 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_4_0, "Test.tbd");
332 auto Result = TextAPIReader::get(std::move(Buffer));
333 EXPECT_FALSE(!!Result);
334 auto errorMessage = toString(Result.takeError());
335 EXPECT_EQ("malformed file\nTest.tbd:5:20: error: invalid Swift ABI "
336 "version.\nswift-abi-version: 4.0\n ^~~\n",
337 errorMessage);
338 }
339
340 TEST(TBDv3, Swift_5) {
341 static const char tbd_v1_swift_5[] = "--- !tapi-tbd-v3\n"
342 "archs: [ arm64 ]\n"
343 "platform: ios\n"
344 "install-name: Test.dylib\n"
345 "swift-abi-version: 5\n"
346 "...\n";
347
348 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_5, "Test.tbd");
349 auto Result = TextAPIReader::get(std::move(Buffer));
350 EXPECT_TRUE(!!Result);
351 auto File = std::move(Result.get());
352 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
353 EXPECT_EQ(5U, File->getSwiftABIVersion());
354 }
355
356 TEST(TBDv3, Swift_99) {
357 static const char tbd_v1_swift_99[] = "--- !tapi-tbd-v3\n"
358 "archs: [ arm64 ]\n"
359 "platform: ios\n"
360 "install-name: Test.dylib\n"
361 "swift-abi-version: 99\n"
362 "...\n";
363
364 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_99, "Test.tbd");
365 auto Result = TextAPIReader::get(std::move(Buffer));
366 EXPECT_TRUE(!!Result);
367 auto File = std::move(Result.get());
368 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
369 EXPECT_EQ(99U, File->getSwiftABIVersion());
370 }
371
372 TEST(TBDv3, UnknownArchitecture) {
373 static const char tbd_v3_file_unknown_architecture[] =
374 "--- !tapi-tbd-v3\n"
375 "archs: [ foo ]\n"
376 "platform: macosx\n"
377 "install-name: Test.dylib\n"
378 "...\n";
379
380 auto Buffer =
381 MemoryBuffer::getMemBuffer(tbd_v3_file_unknown_architecture, "Test.tbd");
382 auto Result = TextAPIReader::get(std::move(Buffer));
383 EXPECT_TRUE(!!Result);
384 }
385
386 TEST(TBDv3, UnknownPlatform) {
387 static const char tbd_v3_file_unknown_platform[] = "--- !tapi-tbd-v3\n"
388 "archs: [ i386 ]\n"
389 "platform: newOS\n"
390 "...\n";
391
392 auto Buffer =
393 MemoryBuffer::getMemBuffer(tbd_v3_file_unknown_platform, "Test.tbd");
394 auto Result = TextAPIReader::get(std::move(Buffer));
395 EXPECT_FALSE(!!Result);
396 auto errorMessage = toString(Result.takeError());
397 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
398 "newOS\n ^~~~~\n",
399 errorMessage);
400 }
401
402 TEST(TBDv3, MalformedFile1) {
403 static const char malformed_file1[] = "--- !tapi-tbd-v3\n"
404 "archs: [ arm64 ]\n"
405 "foobar: \"Unsupported key\"\n"
406 "...\n";
407
408 auto Buffer = MemoryBuffer::getMemBuffer(malformed_file1, "Test.tbd");
409 auto Result = TextAPIReader::get(std::move(Buffer));
410 EXPECT_FALSE(!!Result);
411 auto errorMessage = toString(Result.takeError());
412 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
413 "'platform'\narchs: [ arm64 ]\n^\n",
414 errorMessage);
415 }
416
417 TEST(TBDv3, MalformedFile2) {
418 static const char malformed_file2[] = "--- !tapi-tbd-v3\n"
419 "archs: [ arm64 ]\n"
420 "platform: ios\n"
421 "install-name: Test.dylib\n"
422 "foobar: \"Unsupported key\"\n"
423 "...\n";
424
425 auto Buffer = MemoryBuffer::getMemBuffer(malformed_file2, "Test.tbd");
426 auto Result = TextAPIReader::get(std::move(Buffer));
427 EXPECT_FALSE(!!Result);
428 auto errorMessage = toString(Result.takeError());
429 ASSERT_EQ(
430 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
431 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
432 errorMessage);
433 }
434
435 } // namespace TBDv3