blob: 9904e7bc7358d3b6e1fe4022e64c999a8ab31030 [file] [log] [blame]
Juergen Ributzkab978c512019-08-12 23:01:07 +00001//===-- 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
16using namespace llvm;
17using namespace llvm::MachO;
18
19struct ExportedSymbol {
20 SymbolKind Kind;
21 std::string Name;
22 bool WeakDefined;
23 bool ThreadLocalValue;
24};
25using ExportedSymbolSeq = std::vector<ExportedSymbol>;
26using UUIDs = std::vector<std::pair<Architecture, std::string>>;
27
28inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
29 return std::tie(lhs.Kind, lhs.Name) < std::tie(rhs.Kind, rhs.Name);
30}
31
32inline 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
37static 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
58namespace TBDv3 {
59
60TEST(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
Cyndy Ishida5f865ec2019-08-16 15:30:48 +000091 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v3_file1, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +000092 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
131TEST(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
183TEST(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
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000190 auto Result =
191 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000192 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
198TEST(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
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000205 auto Result =
206 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000207 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
213TEST(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
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000220 auto Result =
221 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000222 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
228TEST(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
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000235 auto Result =
236 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000237 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
243TEST(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
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000250 auto Result =
251 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000252 EXPECT_TRUE(!!Result);
253 auto File = std::move(Result.get());
254 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
255 EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
256}
257
258TEST(TBDv3, Swift_1_0) {
259 static const char tbd_v1_swift_1_0[] = "--- !tapi-tbd-v3\n"
260 "archs: [ arm64 ]\n"
261 "platform: ios\n"
262 "install-name: Test.dylib\n"
263 "swift-abi-version: 1.0\n"
264 "...\n";
265
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000266 auto Result =
267 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000268 EXPECT_TRUE(!!Result);
269 auto File = std::move(Result.get());
270 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
271 EXPECT_EQ(1U, File->getSwiftABIVersion());
272}
273
274TEST(TBDv3, Swift_1_1) {
275 static const char tbd_v1_swift_1_1[] = "--- !tapi-tbd-v3\n"
276 "archs: [ arm64 ]\n"
277 "platform: ios\n"
278 "install-name: Test.dylib\n"
279 "swift-abi-version: 1.1\n"
280 "...\n";
281
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000282 auto Result =
283 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000284 EXPECT_TRUE(!!Result);
285 auto File = std::move(Result.get());
286 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
287 EXPECT_EQ(2U, File->getSwiftABIVersion());
288}
289
290TEST(TBDv3, Swift_2_0) {
291 static const char tbd_v1_swift_2_0[] = "--- !tapi-tbd-v3\n"
292 "archs: [ arm64 ]\n"
293 "platform: ios\n"
294 "install-name: Test.dylib\n"
295 "swift-abi-version: 2.0\n"
296 "...\n";
297
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000298 auto Result =
299 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000300 EXPECT_TRUE(!!Result);
301 auto File = std::move(Result.get());
302 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
303 EXPECT_EQ(3U, File->getSwiftABIVersion());
304}
305
306TEST(TBDv3, Swift_3_0) {
307 static const char tbd_v1_swift_3_0[] = "--- !tapi-tbd-v3\n"
308 "archs: [ arm64 ]\n"
309 "platform: ios\n"
310 "install-name: Test.dylib\n"
311 "swift-abi-version: 3.0\n"
312 "...\n";
313
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000314 auto Result =
315 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000316 EXPECT_TRUE(!!Result);
317 auto File = std::move(Result.get());
318 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
319 EXPECT_EQ(4U, File->getSwiftABIVersion());
320}
321
322TEST(TBDv3, Swift_4_0) {
323 static const char tbd_v1_swift_4_0[] = "--- !tapi-tbd-v3\n"
324 "archs: [ arm64 ]\n"
325 "platform: ios\n"
326 "install-name: Test.dylib\n"
327 "swift-abi-version: 4.0\n"
328 "...\n";
329
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000330 auto Result =
331 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000332 EXPECT_FALSE(!!Result);
333 auto errorMessage = toString(Result.takeError());
334 EXPECT_EQ("malformed file\nTest.tbd:5:20: error: invalid Swift ABI "
335 "version.\nswift-abi-version: 4.0\n ^~~\n",
336 errorMessage);
337}
338
339TEST(TBDv3, Swift_5) {
340 static const char tbd_v1_swift_5[] = "--- !tapi-tbd-v3\n"
341 "archs: [ arm64 ]\n"
342 "platform: ios\n"
343 "install-name: Test.dylib\n"
344 "swift-abi-version: 5\n"
345 "...\n";
346
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000347 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000348 EXPECT_TRUE(!!Result);
349 auto File = std::move(Result.get());
350 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
351 EXPECT_EQ(5U, File->getSwiftABIVersion());
352}
353
354TEST(TBDv3, Swift_99) {
355 static const char tbd_v1_swift_99[] = "--- !tapi-tbd-v3\n"
356 "archs: [ arm64 ]\n"
357 "platform: ios\n"
358 "install-name: Test.dylib\n"
359 "swift-abi-version: 99\n"
360 "...\n";
361
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000362 auto Result =
363 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000364 EXPECT_TRUE(!!Result);
365 auto File = std::move(Result.get());
366 EXPECT_EQ(FileType::TBD_V3, File->getFileType());
367 EXPECT_EQ(99U, File->getSwiftABIVersion());
368}
369
370TEST(TBDv3, UnknownArchitecture) {
371 static const char tbd_v3_file_unknown_architecture[] =
372 "--- !tapi-tbd-v3\n"
373 "archs: [ foo ]\n"
374 "platform: macosx\n"
375 "install-name: Test.dylib\n"
376 "...\n";
377
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000378 auto Result = TextAPIReader::get(
379 MemoryBufferRef(tbd_v3_file_unknown_architecture, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000380 EXPECT_TRUE(!!Result);
381}
382
383TEST(TBDv3, UnknownPlatform) {
384 static const char tbd_v3_file_unknown_platform[] = "--- !tapi-tbd-v3\n"
385 "archs: [ i386 ]\n"
386 "platform: newOS\n"
387 "...\n";
388
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000389 auto Result = TextAPIReader::get(
390 MemoryBufferRef(tbd_v3_file_unknown_platform, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000391 EXPECT_FALSE(!!Result);
392 auto errorMessage = toString(Result.takeError());
393 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
394 "newOS\n ^~~~~\n",
395 errorMessage);
396}
397
398TEST(TBDv3, MalformedFile1) {
399 static const char malformed_file1[] = "--- !tapi-tbd-v3\n"
400 "archs: [ arm64 ]\n"
401 "foobar: \"Unsupported key\"\n"
402 "...\n";
403
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000404 auto Result =
405 TextAPIReader::get(MemoryBufferRef(malformed_file1, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000406 EXPECT_FALSE(!!Result);
407 auto errorMessage = toString(Result.takeError());
408 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
409 "'platform'\narchs: [ arm64 ]\n^\n",
410 errorMessage);
411}
412
413TEST(TBDv3, MalformedFile2) {
414 static const char malformed_file2[] = "--- !tapi-tbd-v3\n"
415 "archs: [ arm64 ]\n"
416 "platform: ios\n"
417 "install-name: Test.dylib\n"
418 "foobar: \"Unsupported key\"\n"
419 "...\n";
420
Cyndy Ishida5f865ec2019-08-16 15:30:48 +0000421 auto Result =
422 TextAPIReader::get(MemoryBufferRef(malformed_file2, "Test.tbd"));
Juergen Ributzkab978c512019-08-12 23:01:07 +0000423 EXPECT_FALSE(!!Result);
424 auto errorMessage = toString(Result.takeError());
425 ASSERT_EQ(
426 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
427 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
428 errorMessage);
429}
430
431} // namespace TBDv3