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