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 Buffer
= MemoryBuffer::getMemBuffer(tbd_v3_file1
, "Test.tbd");
92 auto Result
= TextAPIReader::get(std::move(Buffer
));
93 EXPECT_TRUE(!!Result
);
94 auto File
= std::move(Result
.get());
95 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
96 auto Archs
= AK_armv7
| AK_arm64
;
97 EXPECT_EQ(Archs
, File
->getArchitectures());
98 UUIDs uuids
= {{AK_armv7
, "00000000-0000-0000-0000-000000000000"},
99 {AK_arm64
, "11111111-1111-1111-1111-111111111111"}};
100 EXPECT_EQ(uuids
, File
->uuids());
101 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
102 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
103 EXPECT_EQ(PackedVersion(2, 3, 4), File
->getCurrentVersion());
104 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
105 EXPECT_EQ(2U, File
->getSwiftABIVersion());
106 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
107 EXPECT_TRUE(File
->isTwoLevelNamespace());
108 EXPECT_TRUE(File
->isApplicationExtensionSafe());
109 EXPECT_TRUE(File
->isInstallAPI());
110 InterfaceFileRef
client("clientA", Archs
);
111 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Archs
);
112 EXPECT_EQ(1U, File
->allowableClients().size());
113 EXPECT_EQ(client
, File
->allowableClients().front());
114 EXPECT_EQ(1U, File
->reexportedLibraries().size());
115 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
117 ExportedSymbolSeq Exports
;
118 for (const auto *Sym
: File
->symbols()) {
119 EXPECT_FALSE(Sym
->isWeakReferenced());
120 EXPECT_FALSE(Sym
->isUndefined());
121 Exports
.emplace_back(ExportedSymbol
{Sym
->getKind(), Sym
->getName(),
122 Sym
->isWeakDefined(),
123 Sym
->isThreadLocalValue()});
125 llvm::sort(Exports
.begin(), Exports
.end());
127 EXPECT_EQ(sizeof(TBDv3Symbols
) / sizeof(ExportedSymbol
), Exports
.size());
129 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv3Symbols
)));
132 TEST(TBDv3
, WriteFile
) {
133 static const char tbd_v3_file3
[] =
135 "archs: [ i386, x86_64 ]\n"
137 "install-name: '/usr/lib/libfoo.dylib'\n"
138 "current-version: 1.2.3\n"
139 "compatibility-version: 0\n"
140 "swift-abi-version: 5\n"
142 " - archs: [ i386 ]\n"
143 " symbols: [ _sym1 ]\n"
144 " weak-def-symbols: [ _sym2 ]\n"
145 " thread-local-symbols: [ _sym3 ]\n"
146 " - archs: [ x86_64 ]\n"
147 " allowable-clients: [ clientA ]\n"
148 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
149 " objc-classes: [ Class1 ]\n"
150 " objc-eh-types: [ Class1 ]\n"
151 " objc-ivars: [ Class1._ivar1 ]\n"
155 File
.setPath("libfoo.dylib");
156 File
.setInstallName("/usr/lib/libfoo.dylib");
157 File
.setFileType(FileType::TBD_V3
);
158 File
.setArchitectures(AK_i386
| AK_x86_64
);
159 File
.setPlatform(PlatformKind::macOS
);
160 File
.setCurrentVersion(PackedVersion(1, 2, 3));
161 File
.setTwoLevelNamespace();
162 File
.setApplicationExtensionSafe();
163 File
.setSwiftABIVersion(5);
164 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
165 File
.addAllowableClient("clientA", AK_x86_64
);
166 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64
);
167 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym1", AK_i386
);
168 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym2", AK_i386
,
169 SymbolFlags::WeakDefined
);
170 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym3", AK_i386
,
171 SymbolFlags::ThreadLocalValue
);
172 File
.addSymbol(SymbolKind::ObjectiveCClass
, "Class1", AK_x86_64
);
173 File
.addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", AK_x86_64
);
174 File
.addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
177 SmallString
<4096> Buffer
;
178 raw_svector_ostream
OS(Buffer
);
179 auto Result
= TextAPIWriter::writeToStream(OS
, File
);
180 EXPECT_FALSE(Result
);
181 EXPECT_STREQ(tbd_v3_file3
, Buffer
.c_str());
184 TEST(TBDv3
, Platform_macOS
) {
185 static const char tbd_v1_platform_macos
[] = "--- !tapi-tbd-v3\n"
186 "archs: [ x86_64 ]\n"
188 "install-name: Test.dylib\n"
191 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_macos
, "Test.tbd");
192 auto Result
= TextAPIReader::get(std::move(Buffer
));
193 EXPECT_TRUE(!!Result
);
194 auto File
= std::move(Result
.get());
195 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
196 EXPECT_EQ(PlatformKind::macOS
, File
->getPlatform());
199 TEST(TBDv3
, Platform_iOS
) {
200 static const char tbd_v1_platform_ios
[] = "--- !tapi-tbd-v3\n"
203 "install-name: Test.dylib\n"
206 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_ios
, "Test.tbd");
207 auto Result
= TextAPIReader::get(std::move(Buffer
));
208 EXPECT_TRUE(!!Result
);
209 auto File
= std::move(Result
.get());
210 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
211 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
214 TEST(TBDv3
, Platform_watchOS
) {
215 static const char tbd_v1_platform_watchos
[] = "--- !tapi-tbd-v3\n"
216 "archs: [ armv7k ]\n"
217 "platform: watchos\n"
218 "install-name: Test.dylib\n"
221 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_watchos
, "Test.tbd");
222 auto Result
= TextAPIReader::get(std::move(Buffer
));
223 EXPECT_TRUE(!!Result
);
224 auto File
= std::move(Result
.get());
225 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
226 EXPECT_EQ(PlatformKind::watchOS
, File
->getPlatform());
229 TEST(TBDv3
, Platform_tvOS
) {
230 static const char tbd_v1_platform_tvos
[] = "--- !tapi-tbd-v3\n"
233 "install-name: Test.dylib\n"
236 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_tvos
, "Test.tbd");
237 auto Result
= TextAPIReader::get(std::move(Buffer
));
238 EXPECT_TRUE(!!Result
);
239 auto File
= std::move(Result
.get());
240 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
241 EXPECT_EQ(PlatformKind::tvOS
, File
->getPlatform());
244 TEST(TBDv3
, Platform_bridgeOS
) {
245 static const char tbd_v1_platform_bridgeos
[] = "--- !tapi-tbd-v3\n"
246 "archs: [ armv7k ]\n"
247 "platform: bridgeos\n"
248 "install-name: Test.dylib\n"
252 MemoryBuffer::getMemBuffer(tbd_v1_platform_bridgeos
, "Test.tbd");
253 auto Result
= TextAPIReader::get(std::move(Buffer
));
254 EXPECT_TRUE(!!Result
);
255 auto File
= std::move(Result
.get());
256 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
257 EXPECT_EQ(PlatformKind::bridgeOS
, File
->getPlatform());
260 TEST(TBDv3
, Swift_1_0
) {
261 static const char tbd_v1_swift_1_0
[] = "--- !tapi-tbd-v3\n"
264 "install-name: Test.dylib\n"
265 "swift-abi-version: 1.0\n"
268 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_1_0
, "Test.tbd");
269 auto Result
= TextAPIReader::get(std::move(Buffer
));
270 EXPECT_TRUE(!!Result
);
271 auto File
= std::move(Result
.get());
272 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
273 EXPECT_EQ(1U, File
->getSwiftABIVersion());
276 TEST(TBDv3
, Swift_1_1
) {
277 static const char tbd_v1_swift_1_1
[] = "--- !tapi-tbd-v3\n"
280 "install-name: Test.dylib\n"
281 "swift-abi-version: 1.1\n"
284 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_1_1
, "Test.tbd");
285 auto Result
= TextAPIReader::get(std::move(Buffer
));
286 EXPECT_TRUE(!!Result
);
287 auto File
= std::move(Result
.get());
288 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
289 EXPECT_EQ(2U, File
->getSwiftABIVersion());
292 TEST(TBDv3
, Swift_2_0
) {
293 static const char tbd_v1_swift_2_0
[] = "--- !tapi-tbd-v3\n"
296 "install-name: Test.dylib\n"
297 "swift-abi-version: 2.0\n"
300 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_2_0
, "Test.tbd");
301 auto Result
= TextAPIReader::get(std::move(Buffer
));
302 EXPECT_TRUE(!!Result
);
303 auto File
= std::move(Result
.get());
304 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
305 EXPECT_EQ(3U, File
->getSwiftABIVersion());
308 TEST(TBDv3
, Swift_3_0
) {
309 static const char tbd_v1_swift_3_0
[] = "--- !tapi-tbd-v3\n"
312 "install-name: Test.dylib\n"
313 "swift-abi-version: 3.0\n"
316 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_3_0
, "Test.tbd");
317 auto Result
= TextAPIReader::get(std::move(Buffer
));
318 EXPECT_TRUE(!!Result
);
319 auto File
= std::move(Result
.get());
320 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
321 EXPECT_EQ(4U, File
->getSwiftABIVersion());
324 TEST(TBDv3
, Swift_4_0
) {
325 static const char tbd_v1_swift_4_0
[] = "--- !tapi-tbd-v3\n"
328 "install-name: Test.dylib\n"
329 "swift-abi-version: 4.0\n"
332 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_4_0
, "Test.tbd");
333 auto Result
= TextAPIReader::get(std::move(Buffer
));
334 EXPECT_FALSE(!!Result
);
335 auto errorMessage
= toString(Result
.takeError());
336 EXPECT_EQ("malformed file\nTest.tbd:5:20: error: invalid Swift ABI "
337 "version.\nswift-abi-version: 4.0\n ^~~\n",
341 TEST(TBDv3
, Swift_5
) {
342 static const char tbd_v1_swift_5
[] = "--- !tapi-tbd-v3\n"
345 "install-name: Test.dylib\n"
346 "swift-abi-version: 5\n"
349 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_5
, "Test.tbd");
350 auto Result
= TextAPIReader::get(std::move(Buffer
));
351 EXPECT_TRUE(!!Result
);
352 auto File
= std::move(Result
.get());
353 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
354 EXPECT_EQ(5U, File
->getSwiftABIVersion());
357 TEST(TBDv3
, Swift_99
) {
358 static const char tbd_v1_swift_99
[] = "--- !tapi-tbd-v3\n"
361 "install-name: Test.dylib\n"
362 "swift-abi-version: 99\n"
365 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_99
, "Test.tbd");
366 auto Result
= TextAPIReader::get(std::move(Buffer
));
367 EXPECT_TRUE(!!Result
);
368 auto File
= std::move(Result
.get());
369 EXPECT_EQ(FileType::TBD_V3
, File
->getFileType());
370 EXPECT_EQ(99U, File
->getSwiftABIVersion());
373 TEST(TBDv3
, UnknownArchitecture
) {
374 static const char tbd_v3_file_unknown_architecture
[] =
378 "install-name: Test.dylib\n"
382 MemoryBuffer::getMemBuffer(tbd_v3_file_unknown_architecture
, "Test.tbd");
383 auto Result
= TextAPIReader::get(std::move(Buffer
));
384 EXPECT_TRUE(!!Result
);
387 TEST(TBDv3
, UnknownPlatform
) {
388 static const char tbd_v3_file_unknown_platform
[] = "--- !tapi-tbd-v3\n"
394 MemoryBuffer::getMemBuffer(tbd_v3_file_unknown_platform
, "Test.tbd");
395 auto Result
= TextAPIReader::get(std::move(Buffer
));
396 EXPECT_FALSE(!!Result
);
397 auto errorMessage
= toString(Result
.takeError());
398 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
403 TEST(TBDv3
, MalformedFile1
) {
404 static const char malformed_file1
[] = "--- !tapi-tbd-v3\n"
406 "foobar: \"Unsupported key\"\n"
409 auto Buffer
= MemoryBuffer::getMemBuffer(malformed_file1
, "Test.tbd");
410 auto Result
= TextAPIReader::get(std::move(Buffer
));
411 EXPECT_FALSE(!!Result
);
412 auto errorMessage
= toString(Result
.takeError());
413 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
414 "'platform'\narchs: [ arm64 ]\n^\n",
418 TEST(TBDv3
, MalformedFile2
) {
419 static const char malformed_file2
[] = "--- !tapi-tbd-v3\n"
422 "install-name: Test.dylib\n"
423 "foobar: \"Unsupported key\"\n"
426 auto Buffer
= MemoryBuffer::getMemBuffer(malformed_file2
, "Test.tbd");
427 auto Result
= TextAPIReader::get(std::move(Buffer
));
428 EXPECT_FALSE(!!Result
);
429 auto errorMessage
= toString(Result
.takeError());
431 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
432 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",