1 //===-- TextStubV2Tests.cpp - TBD V2 File Test ----------------------------===//
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
7 //===-----------------------------------------------------------------------===/
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"
17 using namespace llvm::MachO
;
19 struct ExportedSymbol
{
23 bool ThreadLocalValue
;
25 using ExportedSymbolSeq
= std::vector
<ExportedSymbol
>;
27 inline bool operator<(const ExportedSymbol
&lhs
, const ExportedSymbol
&rhs
) {
28 return std::tie(lhs
.Kind
, lhs
.Name
) < std::tie(rhs
.Kind
, rhs
.Name
);
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
);
36 static ExportedSymbol TBDv2Symbols
[] = {
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},
59 TEST(TBDv2
, ReadFile
) {
60 static const char tbd_v2_file1
[] =
62 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
64 "flags: [ installapi ]\n"
65 "install-name: Test.dylib\n"
66 "current-version: 2.3.4\n"
67 "compatibility-version: 1.0\n"
68 "swift-version: 1.1\n"
69 "parent-umbrella: Umbrella.dylib\n"
71 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
72 " allowable-clients: [ clientA ]\n"
73 " re-exports: [ /usr/lib/libfoo.dylib ]\n"
74 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
75 " objc-classes: [ _class1, _class2 ]\n"
76 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
77 " weak-def-symbols: [ _weak1, _weak2 ]\n"
78 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
79 " - archs: [ armv7, armv7s, armv7k ]\n"
80 " symbols: [ _sym5 ]\n"
81 " objc-classes: [ _class3 ]\n"
82 " objc-ivars: [ _class1._ivar3 ]\n"
83 " weak-def-symbols: [ _weak3 ]\n"
84 " thread-local-symbols: [ _tlv3 ]\n"
87 auto Result
= TextAPIReader::get(MemoryBufferRef(tbd_v2_file1
, "Test.tbd"));
88 EXPECT_TRUE(!!Result
);
89 auto File
= std::move(Result
.get());
90 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
91 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
92 auto Platform
= PlatformKind::iOS
;
94 for (auto &&arch
: Archs
)
95 Targets
.emplace_back(Target(arch
, Platform
));
96 EXPECT_EQ(Archs
, File
->getArchitectures());
97 EXPECT_EQ(File
->getPlatforms().size(), 1U);
98 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
99 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
100 EXPECT_EQ(PackedVersion(2, 3, 4), File
->getCurrentVersion());
101 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
102 EXPECT_EQ(2U, File
->getSwiftABIVersion());
103 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
104 EXPECT_TRUE(File
->isTwoLevelNamespace());
105 EXPECT_TRUE(File
->isApplicationExtensionSafe());
106 EXPECT_TRUE(File
->isInstallAPI());
107 InterfaceFileRef
client("clientA", Targets
);
108 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Targets
);
109 EXPECT_EQ(1U, File
->allowableClients().size());
110 EXPECT_EQ(client
, File
->allowableClients().front());
111 EXPECT_EQ(1U, File
->reexportedLibraries().size());
112 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
114 ExportedSymbolSeq Exports
;
115 for (const auto *Sym
: File
->symbols()) {
116 EXPECT_FALSE(Sym
->isWeakReferenced());
117 EXPECT_FALSE(Sym
->isUndefined());
118 Exports
.emplace_back(ExportedSymbol
{Sym
->getKind(), Sym
->getName(),
119 Sym
->isWeakDefined(),
120 Sym
->isThreadLocalValue()});
122 llvm::sort(Exports
.begin(), Exports
.end());
124 EXPECT_EQ(sizeof(TBDv2Symbols
) / sizeof(ExportedSymbol
), Exports
.size());
126 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv2Symbols
)));
129 TEST(TBDv2
, ReadFile2
) {
130 static const char tbd_v2_file2
[] =
132 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
134 "flags: [ flat_namespace, not_app_extension_safe ]\n"
135 "install-name: Test.dylib\n"
136 "swift-version: 1.1\n"
138 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
139 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
140 " objc-classes: [ _class1, _class2 ]\n"
141 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
142 " weak-def-symbols: [ _weak1, _weak2 ]\n"
143 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
144 " - archs: [ armv7, armv7s, armv7k ]\n"
145 " symbols: [ _sym5 ]\n"
146 " objc-classes: [ _class3 ]\n"
147 " objc-ivars: [ _class1._ivar3 ]\n"
148 " weak-def-symbols: [ _weak3 ]\n"
149 " thread-local-symbols: [ _tlv3 ]\n"
151 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
152 " symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
153 " objc-classes: [ _undefClass1, _undefClass2 ]\n"
154 " objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
155 " weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
158 auto Result
= TextAPIReader::get(MemoryBufferRef(tbd_v2_file2
, "Test.tbd"));
159 EXPECT_TRUE(!!Result
);
160 auto File
= std::move(Result
.get());
161 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
162 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
163 auto Platform
= PlatformKind::iOS
;
165 for (auto &&arch
: Archs
)
166 Targets
.emplace_back(Target(arch
, Platform
));
167 EXPECT_EQ(Archs
, File
->getArchitectures());
168 EXPECT_EQ(File
->getPlatforms().size(), 1U);
169 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
170 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
171 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCurrentVersion());
172 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
173 EXPECT_EQ(2U, File
->getSwiftABIVersion());
174 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
175 EXPECT_FALSE(File
->isTwoLevelNamespace());
176 EXPECT_FALSE(File
->isApplicationExtensionSafe());
177 EXPECT_FALSE(File
->isInstallAPI());
178 EXPECT_EQ(0U, File
->allowableClients().size());
179 EXPECT_EQ(0U, File
->reexportedLibraries().size());
182 TEST(TBDv2
, WriteFile
) {
183 static const char tbd_v2_file3
[] =
185 "archs: [ i386, x86_64 ]\n"
187 "install-name: '/usr/lib/libfoo.dylib'\n"
188 "current-version: 1.2.3\n"
189 "compatibility-version: 0\n"
192 " - archs: [ i386 ]\n"
193 " symbols: [ _sym1 ]\n"
194 " weak-def-symbols: [ _sym2 ]\n"
195 " thread-local-symbols: [ _sym3 ]\n"
196 " - archs: [ x86_64 ]\n"
197 " allowable-clients: [ clientA ]\n"
198 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
199 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
200 " objc-classes: [ _Class1 ]\n"
201 " objc-ivars: [ _Class1._ivar1 ]\n"
206 for (auto &&arch
: AK_i386
| AK_x86_64
)
207 Targets
.emplace_back(Target(arch
, PlatformKind::macOS
));
208 File
.setPath("libfoo.dylib");
209 File
.setInstallName("/usr/lib/libfoo.dylib");
210 File
.setFileType(FileType::TBD_V2
);
211 File
.addTargets(Targets
);
212 File
.setCurrentVersion(PackedVersion(1, 2, 3));
213 File
.setTwoLevelNamespace();
214 File
.setApplicationExtensionSafe();
215 File
.setSwiftABIVersion(5);
216 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
217 File
.addAllowableClient("clientA", Targets
[1]);
218 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets
[1]);
219 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym1", {Targets
[0]});
220 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym2", {Targets
[0]},
221 SymbolFlags::WeakDefined
);
222 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym3", {Targets
[0]},
223 SymbolFlags::ThreadLocalValue
);
224 File
.addSymbol(SymbolKind::ObjectiveCClass
, "Class1", {Targets
[1]});
225 File
.addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", {Targets
[1]});
226 File
.addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
229 SmallString
<4096> Buffer
;
230 raw_svector_ostream
OS(Buffer
);
231 auto Result
= TextAPIWriter::writeToStream(OS
, File
);
232 EXPECT_FALSE(Result
);
233 EXPECT_STREQ(tbd_v2_file3
, Buffer
.c_str());
236 TEST(TBDv2
, Platform_macOS
) {
237 static const char tbd_v1_platform_macos
[] = "--- !tapi-tbd-v2\n"
238 "archs: [ x86_64 ]\n"
240 "install-name: Test.dylib\n"
244 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos
, "Test.tbd"));
245 EXPECT_TRUE(!!Result
);
246 auto File
= std::move(Result
.get());
247 auto Platform
= PlatformKind::macOS
;
248 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
249 EXPECT_EQ(File
->getPlatforms().size(), 1U);
250 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
253 TEST(TBDv2
, Platform_iOS
) {
254 static const char tbd_v1_platform_ios
[] = "--- !tapi-tbd-v2\n"
257 "install-name: Test.dylib\n"
261 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios
, "Test.tbd"));
262 EXPECT_TRUE(!!Result
);
263 auto Platform
= PlatformKind::iOS
;
264 auto File
= std::move(Result
.get());
265 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
266 EXPECT_EQ(File
->getPlatforms().size(), 1U);
267 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
270 TEST(TBDv2
, Platform_watchOS
) {
271 static const char tbd_v1_platform_watchos
[] = "--- !tapi-tbd-v2\n"
272 "archs: [ armv7k ]\n"
273 "platform: watchos\n"
274 "install-name: Test.dylib\n"
278 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos
, "Test.tbd"));
279 EXPECT_TRUE(!!Result
);
280 auto Platform
= PlatformKind::watchOS
;
281 auto File
= std::move(Result
.get());
282 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
283 EXPECT_EQ(File
->getPlatforms().size(), 1U);
284 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
287 TEST(TBDv2
, Platform_tvOS
) {
288 static const char tbd_v1_platform_tvos
[] = "--- !tapi-tbd-v2\n"
291 "install-name: Test.dylib\n"
295 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos
, "Test.tbd"));
296 EXPECT_TRUE(!!Result
);
297 auto Platform
= PlatformKind::tvOS
;
298 auto File
= std::move(Result
.get());
299 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
300 EXPECT_EQ(File
->getPlatforms().size(), 1U);
301 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
304 TEST(TBDv2
, Platform_bridgeOS
) {
305 static const char tbd_v1_platform_bridgeos
[] = "--- !tapi-tbd-v2\n"
306 "archs: [ armv7k ]\n"
307 "platform: bridgeos\n"
308 "install-name: Test.dylib\n"
312 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos
, "Test.tbd"));
313 EXPECT_TRUE(!!Result
);
314 auto Platform
= PlatformKind::bridgeOS
;
315 auto File
= std::move(Result
.get());
316 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
317 EXPECT_EQ(File
->getPlatforms().size(), 1U);
318 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
321 TEST(TBDv2
, Swift_1_0
) {
322 static const char tbd_v1_swift_1_0
[] = "--- !tapi-tbd-v2\n"
325 "install-name: Test.dylib\n"
326 "swift-version: 1.0\n"
330 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0
, "Test.tbd"));
331 EXPECT_TRUE(!!Result
);
332 auto File
= std::move(Result
.get());
333 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
334 EXPECT_EQ(1U, File
->getSwiftABIVersion());
337 TEST(TBDv2
, Swift_1_1
) {
338 static const char tbd_v1_swift_1_1
[] = "--- !tapi-tbd-v2\n"
341 "install-name: Test.dylib\n"
342 "swift-version: 1.1\n"
346 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1
, "Test.tbd"));
347 EXPECT_TRUE(!!Result
);
348 auto File
= std::move(Result
.get());
349 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
350 EXPECT_EQ(2U, File
->getSwiftABIVersion());
353 TEST(TBDv2
, Swift_2_0
) {
354 static const char tbd_v1_swift_2_0
[] = "--- !tapi-tbd-v2\n"
357 "install-name: Test.dylib\n"
358 "swift-version: 2.0\n"
362 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0
, "Test.tbd"));
363 EXPECT_TRUE(!!Result
);
364 auto File
= std::move(Result
.get());
365 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
366 EXPECT_EQ(3U, File
->getSwiftABIVersion());
369 TEST(TBDv2
, Swift_3_0
) {
370 static const char tbd_v1_swift_3_0
[] = "--- !tapi-tbd-v2\n"
373 "install-name: Test.dylib\n"
374 "swift-version: 3.0\n"
378 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0
, "Test.tbd"));
379 EXPECT_TRUE(!!Result
);
380 auto File
= std::move(Result
.get());
381 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
382 EXPECT_EQ(4U, File
->getSwiftABIVersion());
385 TEST(TBDv2
, Swift_4_0
) {
386 static const char tbd_v1_swift_4_0
[] = "--- !tapi-tbd-v2\n"
389 "install-name: Test.dylib\n"
390 "swift-version: 4.0\n"
394 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0
, "Test.tbd"));
395 EXPECT_FALSE(!!Result
);
396 auto errorMessage
= toString(Result
.takeError());
397 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
398 "version.\nswift-version: 4.0\n ^~~\n",
402 TEST(TBDv2
, Swift_5
) {
403 static const char tbd_v1_swift_5
[] = "--- !tapi-tbd-v2\n"
406 "install-name: Test.dylib\n"
410 auto Result
= TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5
, "Test.tbd"));
411 EXPECT_TRUE(!!Result
);
412 auto File
= std::move(Result
.get());
413 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
414 EXPECT_EQ(5U, File
->getSwiftABIVersion());
417 TEST(TBDv2
, Swift_99
) {
418 static const char tbd_v1_swift_99
[] = "--- !tapi-tbd-v2\n"
421 "install-name: Test.dylib\n"
422 "swift-version: 99\n"
426 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99
, "Test.tbd"));
427 EXPECT_TRUE(!!Result
);
428 auto File
= std::move(Result
.get());
429 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
430 EXPECT_EQ(99U, File
->getSwiftABIVersion());
433 TEST(TBDv2
, UnknownArchitecture
) {
434 static const char tbd_v2_file_unknown_architecture
[] =
438 "install-name: Test.dylib\n"
440 auto Result
= TextAPIReader::get(
441 MemoryBufferRef(tbd_v2_file_unknown_architecture
, "Test.tbd"));
442 EXPECT_TRUE(!!Result
);
445 TEST(TBDv2
, UnknownPlatform
) {
446 static const char tbd_v2_file_unknown_platform
[] = "--- !tapi-tbd-v2\n"
451 auto Result
= TextAPIReader::get(
452 MemoryBufferRef(tbd_v2_file_unknown_platform
, "Test.tbd"));
453 EXPECT_FALSE(!!Result
);
454 auto errorMessage
= toString(Result
.takeError());
455 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
460 TEST(TBDv2
, InvalidPlatform
) {
461 static const char tbd_v2_file_invalid_platform
[] =
465 "install-name: Test.dylib\n"
468 auto Result
= TextAPIReader::get(
469 MemoryBufferRef(tbd_v2_file_invalid_platform
, "Test.tbd"));
470 EXPECT_FALSE(!!Result
);
471 auto errorMessage
= toString(Result
.takeError());
472 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: invalid platform\nplatform: "
477 TEST(TBDv2
, MalformedFile1
) {
478 static const char malformed_file1
[] = "--- !tapi-tbd-v2\n"
480 "foobar: \"Unsupported key\"\n"
484 TextAPIReader::get(MemoryBufferRef(malformed_file1
, "Test.tbd"));
485 EXPECT_FALSE(!!Result
);
486 auto errorMessage
= toString(Result
.takeError());
487 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
488 "'platform'\narchs: [ arm64 ]\n^\n",
492 TEST(TBDv2
, MalformedFile2
) {
493 static const char malformed_file2
[] = "--- !tapi-tbd-v2\n"
496 "install-name: Test.dylib\n"
497 "foobar: \"Unsupported key\"\n"
501 TextAPIReader::get(MemoryBufferRef(malformed_file2
, "Test.tbd"));
502 EXPECT_FALSE(!!Result
);
503 auto errorMessage
= toString(Result
.takeError());
505 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
506 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",