1 //===-- TextStubV2Tests.cpp - TBD V2 File Test ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "llvm/TextAPI/MachO/InterfaceFile.h"
10 #include "llvm/TextAPI/MachO/TextAPIReader.h"
11 #include "llvm/TextAPI/MachO/TextAPIWriter.h"
12 #include "gtest/gtest.h"
13 #include <string>
14 #include <vector>
15 
16 using namespace llvm;
17 using namespace llvm::MachO;
18 
19 using ExportedSymbol = std::tuple<SymbolKind, std::string, bool, bool>;
20 using ExportedSymbolSeq = std::vector<ExportedSymbol>;
21 
22 inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
23   return std::get<1>(lhs) < std::get<1>(rhs);
24 }
25 
26 static ExportedSymbolSeq TBDv2Symbols = {
27     {SymbolKind::GlobalSymbol, "$ld$hide$os9.0$_sym1", false, false},
28     {SymbolKind::GlobalSymbol, "_sym1", false, false},
29     {SymbolKind::GlobalSymbol, "_sym2", false, false},
30     {SymbolKind::GlobalSymbol, "_sym3", false, false},
31     {SymbolKind::GlobalSymbol, "_sym4", false, false},
32     {SymbolKind::GlobalSymbol, "_sym5", false, false},
33     {SymbolKind::GlobalSymbol, "_tlv1", false, true},
34     {SymbolKind::GlobalSymbol, "_tlv2", false, true},
35     {SymbolKind::GlobalSymbol, "_tlv3", false, true},
36     {SymbolKind::GlobalSymbol, "_weak1", true, false},
37     {SymbolKind::GlobalSymbol, "_weak2", true, false},
38     {SymbolKind::GlobalSymbol, "_weak3", true, false},
39     {SymbolKind::ObjectiveCClass, "class1", false, false},
40     {SymbolKind::ObjectiveCClass, "class2", false, false},
41     {SymbolKind::ObjectiveCClass, "class3", false, false},
42     {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar1", false, false},
43     {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar2", false, false},
44     {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar3", false, false},
45 };
46 
47 namespace TBDv2 {
48 
49 TEST(TBDv2, ReadFile) {
50   static const char tbd_v2_file1[] =
51       "--- !tapi-tbd-v2\n"
52       "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
53       "platform: ios\n"
54       "flags: [ installapi ]\n"
55       "install-name: Test.dylib\n"
56       "current-version: 2.3.4\n"
57       "compatibility-version: 1.0\n"
58       "swift-version: 1.1\n"
59       "parent-umbrella: Umbrella.dylib\n"
60       "exports:\n"
61       "  - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
62       "    allowable-clients: [ clientA ]\n"
63       "    re-exports: [ /usr/lib/libfoo.dylib ]\n"
64       "    symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
65       "    objc-classes: [ _class1, _class2 ]\n"
66       "    objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
67       "    weak-def-symbols: [ _weak1, _weak2 ]\n"
68       "    thread-local-symbols: [ _tlv1, _tlv2 ]\n"
69       "  - archs: [ armv7, armv7s, armv7k ]\n"
70       "    symbols: [ _sym5 ]\n"
71       "    objc-classes: [ _class3 ]\n"
72       "    objc-ivars: [ _class1._ivar3 ]\n"
73       "    weak-def-symbols: [ _weak3 ]\n"
74       "    thread-local-symbols: [ _tlv3 ]\n"
75       "...\n";
76 
77   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v2_file1, "Test.tbd");
78   auto Result = TextAPIReader::get(std::move(Buffer));
79   EXPECT_TRUE(!!Result);
80   auto File = std::move(Result.get());
81   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
82   auto Archs = Architecture::armv7 | Architecture::armv7s |
83                Architecture::armv7k | Architecture::arm64;
84   EXPECT_EQ(Archs, File->getArchitectures());
85   EXPECT_EQ(Platform::iOS, File->getPlatform());
86   EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
87   EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
88   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
89   EXPECT_EQ(2U, File->getSwiftABIVersion());
90   EXPECT_EQ(ObjCConstraint::Retain_Release, File->getObjCConstraint());
91   EXPECT_TRUE(File->isTwoLevelNamespace());
92   EXPECT_TRUE(File->isApplicationExtensionSafe());
93   EXPECT_TRUE(File->isInstallAPI());
94   InterfaceFileRef client("clientA", Archs);
95   InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Archs);
96   EXPECT_EQ(1U, File->allowableClients().size());
97   EXPECT_EQ(client, File->allowableClients().front());
98   EXPECT_EQ(1U, File->reexportedLibraries().size());
99   EXPECT_EQ(reexport, File->reexportedLibraries().front());
100 
101   ExportedSymbolSeq Exports;
102   for (const auto *Sym : File->symbols()) {
103     EXPECT_FALSE(Sym->isWeakReferenced());
104     EXPECT_FALSE(Sym->isUndefined());
105     Exports.emplace_back(Sym->getKind(), Sym->getName(), Sym->isWeakDefined(),
106                          Sym->isThreadLocalValue());
107   }
108   llvm::sort(Exports.begin(), Exports.end());
109 
110   EXPECT_EQ(TBDv2Symbols.size(), Exports.size());
111   EXPECT_TRUE(std::equal(Exports.begin(), Exports.end(), TBDv2Symbols.begin()));
112 }
113 
114 TEST(TBDv2, ReadFile2) {
115   static const char tbd_v2_file2[] =
116       "--- !tapi-tbd-v2\n"
117       "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
118       "platform: ios\n"
119       "flags: [ flat_namespace, not_app_extension_safe ]\n"
120       "install-name: Test.dylib\n"
121       "swift-version: 1.1\n"
122       "exports:\n"
123       "  - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
124       "    symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
125       "    objc-classes: [ _class1, _class2 ]\n"
126       "    objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
127       "    weak-def-symbols: [ _weak1, _weak2 ]\n"
128       "    thread-local-symbols: [ _tlv1, _tlv2 ]\n"
129       "  - archs: [ armv7, armv7s, armv7k ]\n"
130       "    symbols: [ _sym5 ]\n"
131       "    objc-classes: [ _class3 ]\n"
132       "    objc-ivars: [ _class1._ivar3 ]\n"
133       "    weak-def-symbols: [ _weak3 ]\n"
134       "    thread-local-symbols: [ _tlv3 ]\n"
135       "undefineds:\n"
136       "  - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
137       "    symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
138       "    objc-classes: [ _undefClass1, _undefClass2 ]\n"
139       "    objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
140       "    weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
141       "...\n";
142 
143   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v2_file2, "Test.tbd");
144   auto Result = TextAPIReader::get(std::move(Buffer));
145   EXPECT_TRUE(!!Result);
146   auto File = std::move(Result.get());
147   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
148   auto Archs = Architecture::armv7 | Architecture::armv7s |
149                Architecture::armv7k | Architecture::arm64;
150   EXPECT_EQ(Archs, File->getArchitectures());
151   EXPECT_EQ(Platform::iOS, File->getPlatform());
152   EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
153   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
154   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
155   EXPECT_EQ(2U, File->getSwiftABIVersion());
156   EXPECT_EQ(ObjCConstraint::Retain_Release, File->getObjCConstraint());
157   EXPECT_FALSE(File->isTwoLevelNamespace());
158   EXPECT_FALSE(File->isApplicationExtensionSafe());
159   EXPECT_FALSE(File->isInstallAPI());
160   EXPECT_EQ(0U, File->allowableClients().size());
161   EXPECT_EQ(0U, File->reexportedLibraries().size());
162 }
163 
164 TEST(TBDv2, WriteFile) {
165   static const char tbd_v2_file3[] =
166       "--- !tapi-tbd-v2\n"
167       "archs:           [ i386, x86_64 ]\n"
168       "platform:        macosx\n"
169       "install-name:    '/usr/lib/libfoo.dylib'\n"
170       "current-version: 1.2.3\n"
171       "compatibility-version: 0\n"
172       "swift-version:   5\n"
173       "exports:         \n"
174       "  - archs:           [ i386 ]\n"
175       "    symbols:         [ _sym1 ]\n"
176       "    weak-def-symbols: [ _sym2 ]\n"
177       "    thread-local-symbols: [ _sym3 ]\n"
178       "  - archs:           [ x86_64 ]\n"
179       "    allowable-clients: [ clientA ]\n"
180       "    re-exports:      [ '/usr/lib/libfoo.dylib' ]\n"
181       "    symbols:         [ '_OBJC_EHTYPE_$_Class1' ]\n"
182       "    objc-classes:    [ _Class1 ]\n"
183       "    objc-ivars:      [ _Class1._ivar1 ]\n"
184       "...\n";
185 
186   InterfaceFile File;
187   File.setPath("libfoo.dylib");
188   File.setInstallName("/usr/lib/libfoo.dylib");
189   File.setFileType(FileType::TBD_V2);
190   File.setArchitectures(Architecture::i386 | Architecture::x86_64);
191   File.setPlatform(Platform::macOS);
192   File.setCurrentVersion(PackedVersion(1, 2, 3));
193   File.setTwoLevelNamespace();
194   File.setApplicationExtensionSafe();
195   File.setSwiftABIVersion(5);
196   File.setObjCConstraint(ObjCConstraint::Retain_Release);
197   File.addAllowableClient("clientA", Architecture::x86_64);
198   File.addReexportedLibrary("/usr/lib/libfoo.dylib", Architecture::x86_64);
199   File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", Architecture::i386);
200   File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", Architecture::i386,
201                  SymbolFlags::WeakDefined);
202   File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", Architecture::i386,
203                  SymbolFlags::ThreadLocalValue);
204   File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", Architecture::x86_64);
205   File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1",
206                  Architecture::x86_64);
207   File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
208                  Architecture::x86_64);
209 
210   SmallString<4096> Buffer;
211   raw_svector_ostream OS(Buffer);
212   auto Result = TextAPIWriter::writeToStream(OS, File);
213   EXPECT_FALSE(Result);
214   EXPECT_STREQ(tbd_v2_file3, Buffer.c_str());
215 }
216 
217 TEST(TBDv2, Platform_macOS) {
218   static const char tbd_v1_platform_macos[] = "--- !tapi-tbd-v2\n"
219                                               "archs: [ x86_64 ]\n"
220                                               "platform: macosx\n"
221                                               "install-name: Test.dylib\n"
222                                               "...\n";
223 
224   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_macos, "Test.tbd");
225   auto Result = TextAPIReader::get(std::move(Buffer));
226   EXPECT_TRUE(!!Result);
227   auto File = std::move(Result.get());
228   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
229   EXPECT_EQ(Platform::macOS, File->getPlatform());
230 }
231 
232 TEST(TBDv2, Platform_iOS) {
233   static const char tbd_v1_platform_ios[] = "--- !tapi-tbd-v2\n"
234                                             "archs: [ arm64 ]\n"
235                                             "platform: ios\n"
236                                             "install-name: Test.dylib\n"
237                                             "...\n";
238 
239   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_ios, "Test.tbd");
240   auto Result = TextAPIReader::get(std::move(Buffer));
241   EXPECT_TRUE(!!Result);
242   auto File = std::move(Result.get());
243   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
244   EXPECT_EQ(Platform::iOS, File->getPlatform());
245 }
246 
247 TEST(TBDv2, Platform_watchOS) {
248   static const char tbd_v1_platform_watchos[] = "--- !tapi-tbd-v2\n"
249                                                 "archs: [ armv7k ]\n"
250                                                 "platform: watchos\n"
251                                                 "install-name: Test.dylib\n"
252                                                 "...\n";
253 
254   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_watchos, "Test.tbd");
255   auto Result = TextAPIReader::get(std::move(Buffer));
256   EXPECT_TRUE(!!Result);
257   auto File = std::move(Result.get());
258   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
259   EXPECT_EQ(Platform::watchOS, File->getPlatform());
260 }
261 
262 TEST(TBDv2, Platform_tvOS) {
263   static const char tbd_v1_platform_tvos[] = "--- !tapi-tbd-v2\n"
264                                              "archs: [ arm64 ]\n"
265                                              "platform: tvos\n"
266                                              "install-name: Test.dylib\n"
267                                              "...\n";
268 
269   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_tvos, "Test.tbd");
270   auto Result = TextAPIReader::get(std::move(Buffer));
271   EXPECT_TRUE(!!Result);
272   auto File = std::move(Result.get());
273   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
274   EXPECT_EQ(Platform::tvOS, File->getPlatform());
275 }
276 
277 TEST(TBDv2, Platform_bridgeOS) {
278   static const char tbd_v1_platform_bridgeos[] = "--- !tapi-tbd-v2\n"
279                                                  "archs: [ armv7k ]\n"
280                                                  "platform: bridgeos\n"
281                                                  "install-name: Test.dylib\n"
282                                                  "...\n";
283 
284   auto Buffer =
285       MemoryBuffer::getMemBuffer(tbd_v1_platform_bridgeos, "Test.tbd");
286   auto Result = TextAPIReader::get(std::move(Buffer));
287   EXPECT_TRUE(!!Result);
288   auto File = std::move(Result.get());
289   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
290   EXPECT_EQ(Platform::bridgeOS, File->getPlatform());
291 }
292 
293 TEST(TBDv2, Swift_1_0) {
294   static const char tbd_v1_swift_1_0[] = "--- !tapi-tbd-v2\n"
295                                          "archs: [ arm64 ]\n"
296                                          "platform: ios\n"
297                                          "install-name: Test.dylib\n"
298                                          "swift-version: 1.0\n"
299                                          "...\n";
300 
301   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_1_0, "Test.tbd");
302   auto Result = TextAPIReader::get(std::move(Buffer));
303   EXPECT_TRUE(!!Result);
304   auto File = std::move(Result.get());
305   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
306   EXPECT_EQ(1U, File->getSwiftABIVersion());
307 }
308 
309 TEST(TBDv2, Swift_1_1) {
310   static const char tbd_v1_swift_1_1[] = "--- !tapi-tbd-v2\n"
311                                          "archs: [ arm64 ]\n"
312                                          "platform: ios\n"
313                                          "install-name: Test.dylib\n"
314                                          "swift-version: 1.1\n"
315                                          "...\n";
316 
317   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_1_1, "Test.tbd");
318   auto Result = TextAPIReader::get(std::move(Buffer));
319   EXPECT_TRUE(!!Result);
320   auto File = std::move(Result.get());
321   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
322   EXPECT_EQ(2U, File->getSwiftABIVersion());
323 }
324 
325 TEST(TBDv2, Swift_2_0) {
326   static const char tbd_v1_swift_2_0[] = "--- !tapi-tbd-v2\n"
327                                          "archs: [ arm64 ]\n"
328                                          "platform: ios\n"
329                                          "install-name: Test.dylib\n"
330                                          "swift-version: 2.0\n"
331                                          "...\n";
332 
333   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_2_0, "Test.tbd");
334   auto Result = TextAPIReader::get(std::move(Buffer));
335   EXPECT_TRUE(!!Result);
336   auto File = std::move(Result.get());
337   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
338   EXPECT_EQ(3U, File->getSwiftABIVersion());
339 }
340 
341 TEST(TBDv2, Swift_3_0) {
342   static const char tbd_v1_swift_3_0[] = "--- !tapi-tbd-v2\n"
343                                          "archs: [ arm64 ]\n"
344                                          "platform: ios\n"
345                                          "install-name: Test.dylib\n"
346                                          "swift-version: 3.0\n"
347                                          "...\n";
348 
349   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_3_0, "Test.tbd");
350   auto Result = TextAPIReader::get(std::move(Buffer));
351   EXPECT_TRUE(!!Result);
352   auto File = std::move(Result.get());
353   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
354   EXPECT_EQ(4U, File->getSwiftABIVersion());
355 }
356 
357 TEST(TBDv2, Swift_4_0) {
358   static const char tbd_v1_swift_4_0[] = "--- !tapi-tbd-v2\n"
359                                          "archs: [ arm64 ]\n"
360                                          "platform: ios\n"
361                                          "install-name: Test.dylib\n"
362                                          "swift-version: 4.0\n"
363                                          "...\n";
364 
365   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_4_0, "Test.tbd");
366   auto Result = TextAPIReader::get(std::move(Buffer));
367   EXPECT_FALSE(!!Result);
368   auto errorMessage = toString(Result.takeError());
369   EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
370             "version.\nswift-version: 4.0\n               ^~~\n",
371             errorMessage);
372 }
373 
374 TEST(TBDv2, Swift_5) {
375   static const char tbd_v1_swift_5[] = "--- !tapi-tbd-v2\n"
376                                        "archs: [ arm64 ]\n"
377                                        "platform: ios\n"
378                                        "install-name: Test.dylib\n"
379                                        "swift-version: 5\n"
380                                        "...\n";
381 
382   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_5, "Test.tbd");
383   auto Result = TextAPIReader::get(std::move(Buffer));
384   EXPECT_TRUE(!!Result);
385   auto File = std::move(Result.get());
386   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
387   EXPECT_EQ(5U, File->getSwiftABIVersion());
388 }
389 
390 TEST(TBDv2, Swift_99) {
391   static const char tbd_v1_swift_99[] = "--- !tapi-tbd-v2\n"
392                                         "archs: [ arm64 ]\n"
393                                         "platform: ios\n"
394                                         "install-name: Test.dylib\n"
395                                         "swift-version: 99\n"
396                                         "...\n";
397 
398   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_99, "Test.tbd");
399   auto Result = TextAPIReader::get(std::move(Buffer));
400   EXPECT_TRUE(!!Result);
401   auto File = std::move(Result.get());
402   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
403   EXPECT_EQ(99U, File->getSwiftABIVersion());
404 }
405 
406 TEST(TBDv2, UnknownArchitecture) {
407   static const char tbd_v2_file_unknown_architecture[] =
408       "--- !tapi-tbd-v2\n"
409       "archs: [ foo ]\n"
410       "platform: macosx\n"
411       "install-name: Test.dylib\n"
412       "...\n";
413 
414   auto Buffer =
415       MemoryBuffer::getMemBuffer(tbd_v2_file_unknown_architecture, "Test.tbd");
416   auto Result = TextAPIReader::get(std::move(Buffer));
417   EXPECT_TRUE(!!Result);
418 }
419 
420 TEST(TBDv2, UnknownPlatform) {
421   static const char tbd_v2_file_unknown_platform[] = "--- !tapi-tbd-v2\n"
422                                                      "archs: [ i386 ]\n"
423                                                      "platform: newOS\n"
424                                                      "...\n";
425 
426   auto Buffer =
427       MemoryBuffer::getMemBuffer(tbd_v2_file_unknown_platform, "Test.tbd");
428   auto Result = TextAPIReader::get(std::move(Buffer));
429   EXPECT_FALSE(!!Result);
430   auto errorMessage = toString(Result.takeError());
431   EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
432             "newOS\n          ^~~~~\n",
433             errorMessage);
434 }
435 
436 TEST(TBDv2, MalformedFile1) {
437   static const char malformed_file1[] = "--- !tapi-tbd-v2\n"
438                                         "archs: [ arm64 ]\n"
439                                         "foobar: \"Unsupported key\"\n"
440                                         "...\n";
441 
442   auto Buffer = MemoryBuffer::getMemBuffer(malformed_file1, "Test.tbd");
443   auto Result = TextAPIReader::get(std::move(Buffer));
444   EXPECT_FALSE(!!Result);
445   auto errorMessage = toString(Result.takeError());
446   ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
447             "'platform'\narchs: [ arm64 ]\n^\n",
448             errorMessage);
449 }
450 
451 TEST(TBDv2, MalformedFile2) {
452   static const char malformed_file2[] = "--- !tapi-tbd-v2\n"
453                                         "archs: [ arm64 ]\n"
454                                         "platform: ios\n"
455                                         "install-name: Test.dylib\n"
456                                         "foobar: \"Unsupported key\"\n"
457                                         "...\n";
458 
459   auto Buffer = MemoryBuffer::getMemBuffer(malformed_file2, "Test.tbd");
460   auto Result = TextAPIReader::get(std::move(Buffer));
461   EXPECT_FALSE(!!Result);
462   auto errorMessage = toString(Result.takeError());
463   ASSERT_EQ(
464       "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
465       "\"Unsupported key\"\n        ^~~~~~~~~~~~~~~~~\n",
466       errorMessage);
467 }
468 
469 } // namespace TBDv2
470