1 //===-- TextStubV3Tests.cpp - TBD V3 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
>;
26 using UUIDs
= std::vector
<std::pair
<Architecture
, std::string
>>;
28 inline bool operator<(const ExportedSymbol
&lhs
, const ExportedSymbol
&rhs
) {
29 return std::tie(lhs
.Kind
, lhs
.Name
) < std::tie(rhs
.Kind
, rhs
.Name
);
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
);
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},
60 TEST(TBDv3
, ReadFile
) {
61 static const char tbd_v3_file1
[] =
63 "archs: [ armv7, arm64 ]\n"
64 "uuids: [ 'armv7: 00000000-0000-0000-0000-000000000000',\n"
65 " 'arm64: 11111111-1111-1111-1111-111111111111']\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"
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"
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 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());
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()});
124 llvm::sort(Exports
.begin(), Exports
.end());
126 EXPECT_EQ(sizeof(TBDv3Symbols
) / sizeof(ExportedSymbol
), Exports
.size());
128 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv3Symbols
)));
131 TEST(TBDv3
, WriteFile
) {
132 static const char tbd_v3_file3
[] =
134 "archs: [ i386, x86_64 ]\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"
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"
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",
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());
183 TEST(TBDv3
, Platform_macOS
) {
184 static const char tbd_v1_platform_macos
[] = "--- !tapi-tbd-v3\n"
185 "archs: [ x86_64 ]\n"
187 "install-name: Test.dylib\n"
191 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos
, "Test.tbd"));
192 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());
198 TEST(TBDv3
, Platform_iOS
) {
199 static const char tbd_v1_platform_ios
[] = "--- !tapi-tbd-v3\n"
202 "install-name: Test.dylib\n"
206 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios
, "Test.tbd"));
207 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());
213 TEST(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"
221 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos
, "Test.tbd"));
222 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());
228 TEST(TBDv3
, Platform_tvOS
) {
229 static const char tbd_v1_platform_tvos
[] = "--- !tapi-tbd-v3\n"
232 "install-name: Test.dylib\n"
236 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos
, "Test.tbd"));
237 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());
243 TEST(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"
251 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos
, "Test.tbd"));
252 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());
258 TEST(TBDv3
, Swift_1_0
) {
259 static const char tbd_v1_swift_1_0
[] = "--- !tapi-tbd-v3\n"
262 "install-name: Test.dylib\n"
263 "swift-abi-version: 1.0\n"
267 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0
, "Test.tbd"));
268 EXPECT_TRUE(!!Result
);
269 auto File
= std::move(Result
.get());
270 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
271 EXPECT_EQ(1U, File
->getSwiftABIVersion());
274 TEST(TBDv3
, Swift_1_1
) {
275 static const char tbd_v1_swift_1_1
[] = "--- !tapi-tbd-v3\n"
278 "install-name: Test.dylib\n"
279 "swift-abi-version: 1.1\n"
283 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1
, "Test.tbd"));
284 EXPECT_TRUE(!!Result
);
285 auto File
= std::move(Result
.get());
286 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
287 EXPECT_EQ(2U, File
->getSwiftABIVersion());
290 TEST(TBDv3
, Swift_2_0
) {
291 static const char tbd_v1_swift_2_0
[] = "--- !tapi-tbd-v3\n"
294 "install-name: Test.dylib\n"
295 "swift-abi-version: 2.0\n"
299 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0
, "Test.tbd"));
300 EXPECT_TRUE(!!Result
);
301 auto File
= std::move(Result
.get());
302 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
303 EXPECT_EQ(3U, File
->getSwiftABIVersion());
306 TEST(TBDv3
, Swift_3_0
) {
307 static const char tbd_v1_swift_3_0
[] = "--- !tapi-tbd-v3\n"
310 "install-name: Test.dylib\n"
311 "swift-abi-version: 3.0\n"
315 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0
, "Test.tbd"));
316 EXPECT_TRUE(!!Result
);
317 auto File
= std::move(Result
.get());
318 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
319 EXPECT_EQ(4U, File
->getSwiftABIVersion());
322 TEST(TBDv3
, Swift_4_0
) {
323 static const char tbd_v1_swift_4_0
[] = "--- !tapi-tbd-v3\n"
326 "install-name: Test.dylib\n"
327 "swift-abi-version: 4.0\n"
331 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0
, "Test.tbd"));
332 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",
339 TEST(TBDv3
, Swift_5
) {
340 static const char tbd_v1_swift_5
[] = "--- !tapi-tbd-v3\n"
343 "install-name: Test.dylib\n"
344 "swift-abi-version: 5\n"
347 auto Result
= TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5
, "Test.tbd"));
348 EXPECT_TRUE(!!Result
);
349 auto File
= std::move(Result
.get());
350 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
351 EXPECT_EQ(5U, File
->getSwiftABIVersion());
354 TEST(TBDv3
, Swift_99
) {
355 static const char tbd_v1_swift_99
[] = "--- !tapi-tbd-v3\n"
358 "install-name: Test.dylib\n"
359 "swift-abi-version: 99\n"
363 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99
, "Test.tbd"));
364 EXPECT_TRUE(!!Result
);
365 auto File
= std::move(Result
.get());
366 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
367 EXPECT_EQ(99U, File
->getSwiftABIVersion());
370 TEST(TBDv3
, UnknownArchitecture
) {
371 static const char tbd_v3_file_unknown_architecture
[] =
375 "install-name: Test.dylib\n"
378 auto Result
= TextAPIReader::get(
379 MemoryBufferRef(tbd_v3_file_unknown_architecture
, "Test.tbd"));
380 EXPECT_TRUE(!!Result
);
383 TEST(TBDv3
, UnknownPlatform
) {
384 static const char tbd_v3_file_unknown_platform
[] = "--- !tapi-tbd-v3\n"
389 auto Result
= TextAPIReader::get(
390 MemoryBufferRef(tbd_v3_file_unknown_platform
, "Test.tbd"));
391 EXPECT_FALSE(!!Result
);
392 auto errorMessage
= toString(Result
.takeError());
393 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
398 TEST(TBDv3
, MalformedFile1
) {
399 static const char malformed_file1
[] = "--- !tapi-tbd-v3\n"
401 "foobar: \"Unsupported key\"\n"
405 TextAPIReader::get(MemoryBufferRef(malformed_file1
, "Test.tbd"));
406 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",
413 TEST(TBDv3
, MalformedFile2
) {
414 static const char malformed_file2
[] = "--- !tapi-tbd-v3\n"
417 "install-name: Test.dylib\n"
418 "foobar: \"Unsupported key\"\n"
422 TextAPIReader::get(MemoryBufferRef(malformed_file2
, "Test.tbd"));
423 EXPECT_FALSE(!!Result
);
424 auto errorMessage
= toString(Result
.takeError());
426 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
427 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",