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