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 Buffer
= MemoryBuffer::getMemBuffer(tbd_v2_file1
, "Test.tbd");
88 auto Result
= TextAPIReader::get(std::move(Buffer
));
89 EXPECT_TRUE(!!Result
);
90 auto File
= std::move(Result
.get());
91 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
92 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
93 EXPECT_EQ(Archs
, File
->getArchitectures());
94 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
95 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
96 EXPECT_EQ(PackedVersion(2, 3, 4), File
->getCurrentVersion());
97 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
98 EXPECT_EQ(2U, File
->getSwiftABIVersion());
99 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
100 EXPECT_TRUE(File
->isTwoLevelNamespace());
101 EXPECT_TRUE(File
->isApplicationExtensionSafe());
102 EXPECT_TRUE(File
->isInstallAPI());
103 InterfaceFileRef
client("clientA", Archs
);
104 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Archs
);
105 EXPECT_EQ(1U, File
->allowableClients().size());
106 EXPECT_EQ(client
, File
->allowableClients().front());
107 EXPECT_EQ(1U, File
->reexportedLibraries().size());
108 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
110 ExportedSymbolSeq Exports
;
111 for (const auto *Sym
: File
->symbols()) {
112 EXPECT_FALSE(Sym
->isWeakReferenced());
113 EXPECT_FALSE(Sym
->isUndefined());
114 Exports
.emplace_back(ExportedSymbol
{Sym
->getKind(), Sym
->getName(),
115 Sym
->isWeakDefined(),
116 Sym
->isThreadLocalValue()});
118 llvm::sort(Exports
.begin(), Exports
.end());
120 EXPECT_EQ(sizeof(TBDv2Symbols
) / sizeof(ExportedSymbol
), Exports
.size());
122 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv2Symbols
)));
125 TEST(TBDv2
, ReadFile2
) {
126 static const char tbd_v2_file2
[] =
128 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
130 "flags: [ flat_namespace, not_app_extension_safe ]\n"
131 "install-name: Test.dylib\n"
132 "swift-version: 1.1\n"
134 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
135 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
136 " objc-classes: [ _class1, _class2 ]\n"
137 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
138 " weak-def-symbols: [ _weak1, _weak2 ]\n"
139 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
140 " - archs: [ armv7, armv7s, armv7k ]\n"
141 " symbols: [ _sym5 ]\n"
142 " objc-classes: [ _class3 ]\n"
143 " objc-ivars: [ _class1._ivar3 ]\n"
144 " weak-def-symbols: [ _weak3 ]\n"
145 " thread-local-symbols: [ _tlv3 ]\n"
147 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
148 " symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
149 " objc-classes: [ _undefClass1, _undefClass2 ]\n"
150 " objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
151 " weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
154 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v2_file2
, "Test.tbd");
155 auto Result
= TextAPIReader::get(std::move(Buffer
));
156 EXPECT_TRUE(!!Result
);
157 auto File
= std::move(Result
.get());
158 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
159 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
160 EXPECT_EQ(Archs
, File
->getArchitectures());
161 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
162 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
163 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCurrentVersion());
164 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
165 EXPECT_EQ(2U, File
->getSwiftABIVersion());
166 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
167 EXPECT_FALSE(File
->isTwoLevelNamespace());
168 EXPECT_FALSE(File
->isApplicationExtensionSafe());
169 EXPECT_FALSE(File
->isInstallAPI());
170 EXPECT_EQ(0U, File
->allowableClients().size());
171 EXPECT_EQ(0U, File
->reexportedLibraries().size());
174 // Disable test for windows.
176 TEST(TBDv2
, WriteFile
) {
177 static const char tbd_v2_file3
[] =
179 "archs: [ i386, x86_64 ]\n"
181 "install-name: '/usr/lib/libfoo.dylib'\n"
182 "current-version: 1.2.3\n"
183 "compatibility-version: 0\n"
186 " - archs: [ i386 ]\n"
187 " symbols: [ _sym1 ]\n"
188 " weak-def-symbols: [ _sym2 ]\n"
189 " thread-local-symbols: [ _sym3 ]\n"
190 " - archs: [ x86_64 ]\n"
191 " allowable-clients: [ clientA ]\n"
192 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
193 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
194 " objc-classes: [ _Class1 ]\n"
195 " objc-ivars: [ _Class1._ivar1 ]\n"
199 File
.setPath("libfoo.dylib");
200 File
.setInstallName("/usr/lib/libfoo.dylib");
201 File
.setFileType(FileType::TBD_V2
);
202 File
.setArchitectures(AK_i386
| AK_x86_64
);
203 File
.setPlatform(PlatformKind::macOS
);
204 File
.setCurrentVersion(PackedVersion(1, 2, 3));
205 File
.setTwoLevelNamespace();
206 File
.setApplicationExtensionSafe();
207 File
.setSwiftABIVersion(5);
208 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
209 File
.addAllowableClient("clientA", AK_x86_64
);
210 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64
);
211 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym1", AK_i386
);
212 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym2", AK_i386
,
213 SymbolFlags::WeakDefined
);
214 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym3", AK_i386
,
215 SymbolFlags::ThreadLocalValue
);
216 File
.addSymbol(SymbolKind::ObjectiveCClass
, "Class1", AK_x86_64
);
217 File
.addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", AK_x86_64
);
218 File
.addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
221 SmallString
<4096> Buffer
;
222 raw_svector_ostream
OS(Buffer
);
223 auto Result
= TextAPIWriter::writeToStream(OS
, File
);
224 EXPECT_FALSE(Result
);
225 EXPECT_STREQ(tbd_v2_file3
, Buffer
.c_str());
229 TEST(TBDv2
, Platform_macOS
) {
230 static const char tbd_v1_platform_macos
[] = "--- !tapi-tbd-v2\n"
231 "archs: [ x86_64 ]\n"
233 "install-name: Test.dylib\n"
236 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_macos
, "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_V2
, File
->getFileType());
241 EXPECT_EQ(PlatformKind::macOS
, File
->getPlatform());
244 TEST(TBDv2
, Platform_iOS
) {
245 static const char tbd_v1_platform_ios
[] = "--- !tapi-tbd-v2\n"
248 "install-name: Test.dylib\n"
251 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_ios
, "Test.tbd");
252 auto Result
= TextAPIReader::get(std::move(Buffer
));
253 EXPECT_TRUE(!!Result
);
254 auto File
= std::move(Result
.get());
255 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
256 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
259 TEST(TBDv2
, Platform_watchOS
) {
260 static const char tbd_v1_platform_watchos
[] = "--- !tapi-tbd-v2\n"
261 "archs: [ armv7k ]\n"
262 "platform: watchos\n"
263 "install-name: Test.dylib\n"
266 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_watchos
, "Test.tbd");
267 auto Result
= TextAPIReader::get(std::move(Buffer
));
268 EXPECT_TRUE(!!Result
);
269 auto File
= std::move(Result
.get());
270 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
271 EXPECT_EQ(PlatformKind::watchOS
, File
->getPlatform());
274 TEST(TBDv2
, Platform_tvOS
) {
275 static const char tbd_v1_platform_tvos
[] = "--- !tapi-tbd-v2\n"
278 "install-name: Test.dylib\n"
281 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_tvos
, "Test.tbd");
282 auto Result
= TextAPIReader::get(std::move(Buffer
));
283 EXPECT_TRUE(!!Result
);
284 auto File
= std::move(Result
.get());
285 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
286 EXPECT_EQ(PlatformKind::tvOS
, File
->getPlatform());
289 TEST(TBDv2
, Platform_bridgeOS
) {
290 static const char tbd_v1_platform_bridgeos
[] = "--- !tapi-tbd-v2\n"
291 "archs: [ armv7k ]\n"
292 "platform: bridgeos\n"
293 "install-name: Test.dylib\n"
297 MemoryBuffer::getMemBuffer(tbd_v1_platform_bridgeos
, "Test.tbd");
298 auto Result
= TextAPIReader::get(std::move(Buffer
));
299 EXPECT_TRUE(!!Result
);
300 auto File
= std::move(Result
.get());
301 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
302 EXPECT_EQ(PlatformKind::bridgeOS
, File
->getPlatform());
305 TEST(TBDv2
, Swift_1_0
) {
306 static const char tbd_v1_swift_1_0
[] = "--- !tapi-tbd-v2\n"
309 "install-name: Test.dylib\n"
310 "swift-version: 1.0\n"
313 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_1_0
, "Test.tbd");
314 auto Result
= TextAPIReader::get(std::move(Buffer
));
315 EXPECT_TRUE(!!Result
);
316 auto File
= std::move(Result
.get());
317 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
318 EXPECT_EQ(1U, File
->getSwiftABIVersion());
321 TEST(TBDv2
, Swift_1_1
) {
322 static const char tbd_v1_swift_1_1
[] = "--- !tapi-tbd-v2\n"
325 "install-name: Test.dylib\n"
326 "swift-version: 1.1\n"
329 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_1_1
, "Test.tbd");
330 auto Result
= TextAPIReader::get(std::move(Buffer
));
331 EXPECT_TRUE(!!Result
);
332 auto File
= std::move(Result
.get());
333 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
334 EXPECT_EQ(2U, File
->getSwiftABIVersion());
337 TEST(TBDv2
, Swift_2_0
) {
338 static const char tbd_v1_swift_2_0
[] = "--- !tapi-tbd-v2\n"
341 "install-name: Test.dylib\n"
342 "swift-version: 2.0\n"
345 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_2_0
, "Test.tbd");
346 auto Result
= TextAPIReader::get(std::move(Buffer
));
347 EXPECT_TRUE(!!Result
);
348 auto File
= std::move(Result
.get());
349 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
350 EXPECT_EQ(3U, File
->getSwiftABIVersion());
353 TEST(TBDv2
, Swift_3_0
) {
354 static const char tbd_v1_swift_3_0
[] = "--- !tapi-tbd-v2\n"
357 "install-name: Test.dylib\n"
358 "swift-version: 3.0\n"
361 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_3_0
, "Test.tbd");
362 auto Result
= TextAPIReader::get(std::move(Buffer
));
363 EXPECT_TRUE(!!Result
);
364 auto File
= std::move(Result
.get());
365 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
366 EXPECT_EQ(4U, File
->getSwiftABIVersion());
369 TEST(TBDv2
, Swift_4_0
) {
370 static const char tbd_v1_swift_4_0
[] = "--- !tapi-tbd-v2\n"
373 "install-name: Test.dylib\n"
374 "swift-version: 4.0\n"
377 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_4_0
, "Test.tbd");
378 auto Result
= TextAPIReader::get(std::move(Buffer
));
379 EXPECT_FALSE(!!Result
);
380 auto errorMessage
= toString(Result
.takeError());
381 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
382 "version.\nswift-version: 4.0\n ^~~\n",
386 TEST(TBDv2
, Swift_5
) {
387 static const char tbd_v1_swift_5
[] = "--- !tapi-tbd-v2\n"
390 "install-name: Test.dylib\n"
394 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_5
, "Test.tbd");
395 auto Result
= TextAPIReader::get(std::move(Buffer
));
396 EXPECT_TRUE(!!Result
);
397 auto File
= std::move(Result
.get());
398 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
399 EXPECT_EQ(5U, File
->getSwiftABIVersion());
402 TEST(TBDv2
, Swift_99
) {
403 static const char tbd_v1_swift_99
[] = "--- !tapi-tbd-v2\n"
406 "install-name: Test.dylib\n"
407 "swift-version: 99\n"
410 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_99
, "Test.tbd");
411 auto Result
= TextAPIReader::get(std::move(Buffer
));
412 EXPECT_TRUE(!!Result
);
413 auto File
= std::move(Result
.get());
414 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
415 EXPECT_EQ(99U, File
->getSwiftABIVersion());
418 TEST(TBDv2
, UnknownArchitecture
) {
419 static const char tbd_v2_file_unknown_architecture
[] =
423 "install-name: Test.dylib\n"
427 MemoryBuffer::getMemBuffer(tbd_v2_file_unknown_architecture
, "Test.tbd");
428 auto Result
= TextAPIReader::get(std::move(Buffer
));
429 EXPECT_TRUE(!!Result
);
432 TEST(TBDv2
, UnknownPlatform
) {
433 static const char tbd_v2_file_unknown_platform
[] = "--- !tapi-tbd-v2\n"
439 MemoryBuffer::getMemBuffer(tbd_v2_file_unknown_platform
, "Test.tbd");
440 auto Result
= TextAPIReader::get(std::move(Buffer
));
441 EXPECT_FALSE(!!Result
);
442 auto errorMessage
= toString(Result
.takeError());
443 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
448 TEST(TBDv2
, MalformedFile1
) {
449 static const char malformed_file1
[] = "--- !tapi-tbd-v2\n"
451 "foobar: \"Unsupported key\"\n"
454 auto Buffer
= MemoryBuffer::getMemBuffer(malformed_file1
, "Test.tbd");
455 auto Result
= TextAPIReader::get(std::move(Buffer
));
456 EXPECT_FALSE(!!Result
);
457 auto errorMessage
= toString(Result
.takeError());
458 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
459 "'platform'\narchs: [ arm64 ]\n^\n",
463 TEST(TBDv2
, MalformedFile2
) {
464 static const char malformed_file2
[] = "--- !tapi-tbd-v2\n"
467 "install-name: Test.dylib\n"
468 "foobar: \"Unsupported key\"\n"
471 auto Buffer
= MemoryBuffer::getMemBuffer(malformed_file2
, "Test.tbd");
472 auto Result
= TextAPIReader::get(std::move(Buffer
));
473 EXPECT_FALSE(!!Result
);
474 auto errorMessage
= toString(Result
.takeError());
476 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
477 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",