1 //===-- TextStubV1Tests.cpp - TBD V1 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 16 using namespace llvm; 17 using namespace llvm::MachO; 18 19 struct ExportedSymbol { 20 SymbolKind Kind; 21 std::string Name; 22 bool WeakDefined; 23 bool ThreadLocalValue; 24 }; 25 using ExportedSymbolSeq = std::vector<ExportedSymbol>; 26 27 inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) { 28 return std::tie(lhs.Kind, lhs.Name) < std::tie(rhs.Kind, rhs.Name); 29 } 30 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); 34 } 35 36 static ExportedSymbol TBDv1Symbols[] = { 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}, 55 }; 56 57 namespace TBDv1 { 58 59 TEST(TBDv1, ReadFile) { 60 static const char tbd_v1_file1[] = 61 "---\n" 62 "archs: [ armv7, armv7s, armv7k, arm64 ]\n" 63 "platform: ios\n" 64 "install-name: Test.dylib\n" 65 "current-version: 2.3.4\n" 66 "compatibility-version: 1.0\n" 67 "swift-version: 1.1\n" 68 "exports:\n" 69 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n" 70 " allowed-clients: [ clientA ]\n" 71 " re-exports: [ /usr/lib/libfoo.dylib ]\n" 72 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n" 73 " objc-classes: [ _class1, _class2 ]\n" 74 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n" 75 " weak-def-symbols: [ _weak1, _weak2 ]\n" 76 " thread-local-symbols: [ _tlv1, _tlv2 ]\n" 77 " - archs: [ armv7, armv7s, armv7k ]\n" 78 " symbols: [ _sym5 ]\n" 79 " objc-classes: [ _class3 ]\n" 80 " objc-ivars: [ _class1._ivar3 ]\n" 81 " weak-def-symbols: [ _weak3 ]\n" 82 " thread-local-symbols: [ _tlv3 ]\n" 83 "...\n"; 84 85 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_file1, "Test.tbd")); 86 EXPECT_TRUE(!!Result); 87 auto File = std::move(Result.get()); 88 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 89 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64; 90 EXPECT_EQ(Archs, File->getArchitectures()); 91 EXPECT_EQ(PlatformKind::iOS, File->getPlatform()); 92 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName()); 93 EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion()); 94 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion()); 95 EXPECT_EQ(2U, File->getSwiftABIVersion()); 96 EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint()); 97 EXPECT_TRUE(File->isTwoLevelNamespace()); 98 EXPECT_TRUE(File->isApplicationExtensionSafe()); 99 EXPECT_FALSE(File->isInstallAPI()); 100 InterfaceFileRef client("clientA", Archs); 101 InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Archs); 102 EXPECT_EQ(1U, File->allowableClients().size()); 103 EXPECT_EQ(client, File->allowableClients().front()); 104 EXPECT_EQ(1U, File->reexportedLibraries().size()); 105 EXPECT_EQ(reexport, File->reexportedLibraries().front()); 106 107 ExportedSymbolSeq Exports; 108 for (const auto *Sym : File->symbols()) { 109 EXPECT_FALSE(Sym->isWeakReferenced()); 110 EXPECT_FALSE(Sym->isUndefined()); 111 Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(), 112 Sym->isWeakDefined(), 113 Sym->isThreadLocalValue()}); 114 } 115 llvm::sort(Exports.begin(), Exports.end()); 116 117 EXPECT_EQ(sizeof(TBDv1Symbols) / sizeof(ExportedSymbol), Exports.size()); 118 EXPECT_TRUE( 119 std::equal(Exports.begin(), Exports.end(), std::begin(TBDv1Symbols))); 120 } 121 122 TEST(TBDv1, ReadFile2) { 123 static const char tbd_v1_file2[] = "--- !tapi-tbd-v1\n" 124 "archs: [ armv7, armv7s, armv7k, arm64 ]\n" 125 "platform: ios\n" 126 "install-name: Test.dylib\n" 127 "...\n"; 128 129 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_file2, "Test.tbd")); 130 EXPECT_TRUE(!!Result); 131 auto File = std::move(Result.get()); 132 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 133 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64; 134 EXPECT_EQ(Archs, File->getArchitectures()); 135 EXPECT_EQ(PlatformKind::iOS, File->getPlatform()); 136 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName()); 137 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion()); 138 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion()); 139 EXPECT_EQ(0U, File->getSwiftABIVersion()); 140 EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint()); 141 EXPECT_TRUE(File->isTwoLevelNamespace()); 142 EXPECT_TRUE(File->isApplicationExtensionSafe()); 143 EXPECT_FALSE(File->isInstallAPI()); 144 EXPECT_EQ(0U, File->allowableClients().size()); 145 EXPECT_EQ(0U, File->reexportedLibraries().size()); 146 } 147 148 TEST(TBDv1, WriteFile) { 149 static const char tbd_v1_file3[] = 150 "---\n" 151 "archs: [ i386, x86_64 ]\n" 152 "platform: macosx\n" 153 "install-name: '/usr/lib/libfoo.dylib'\n" 154 "current-version: 1.2.3\n" 155 "compatibility-version: 0\n" 156 "swift-version: 5\n" 157 "objc-constraint: retain_release\n" 158 "exports:\n" 159 " - archs: [ i386 ]\n" 160 " symbols: [ _sym1 ]\n" 161 " weak-def-symbols: [ _sym2 ]\n" 162 " thread-local-symbols: [ _sym3 ]\n" 163 " - archs: [ x86_64 ]\n" 164 " allowed-clients: [ clientA ]\n" 165 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n" 166 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n" 167 " objc-classes: [ _Class1 ]\n" 168 " objc-ivars: [ _Class1._ivar1 ]\n" 169 "...\n"; 170 171 InterfaceFile File; 172 File.setPath("libfoo.dylib"); 173 File.setInstallName("/usr/lib/libfoo.dylib"); 174 File.setFileType(FileType::TBD_V1); 175 File.setArchitectures(AK_i386 | AK_x86_64); 176 File.setPlatform(PlatformKind::macOS); 177 File.setCurrentVersion(PackedVersion(1, 2, 3)); 178 File.setSwiftABIVersion(5); 179 File.setObjCConstraint(ObjCConstraintType::Retain_Release); 180 File.addAllowableClient("clientA", AK_x86_64); 181 File.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64); 182 File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", AK_i386); 183 File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", AK_i386, 184 SymbolFlags::WeakDefined); 185 File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", AK_i386, 186 SymbolFlags::ThreadLocalValue); 187 File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", AK_x86_64); 188 File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", AK_x86_64); 189 File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1", 190 AK_x86_64); 191 192 SmallString<4096> Buffer; 193 raw_svector_ostream OS(Buffer); 194 auto Result = TextAPIWriter::writeToStream(OS, File); 195 EXPECT_FALSE(Result); 196 EXPECT_STREQ(tbd_v1_file3, Buffer.c_str()); 197 } 198 199 TEST(TBDv1, Platform_macOS) { 200 static const char tbd_v1_platform_macos[] = "---\n" 201 "archs: [ x86_64 ]\n" 202 "platform: macosx\n" 203 "install-name: Test.dylib\n" 204 "...\n"; 205 206 auto Result = 207 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos, "Test.tbd")); 208 EXPECT_TRUE(!!Result); 209 auto File = std::move(Result.get()); 210 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 211 EXPECT_EQ(PlatformKind::macOS, File->getPlatform()); 212 } 213 214 TEST(TBDv1, Platform_iOS) { 215 static const char tbd_v1_platform_ios[] = "---\n" 216 "archs: [ arm64 ]\n" 217 "platform: ios\n" 218 "install-name: Test.dylib\n" 219 "...\n"; 220 221 auto Result = 222 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios, "Test.tbd")); 223 EXPECT_TRUE(!!Result); 224 auto File = std::move(Result.get()); 225 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 226 EXPECT_EQ(PlatformKind::iOS, File->getPlatform()); 227 } 228 229 TEST(TBDv1, Platform_watchOS) { 230 static const char tbd_v1_platform_watchos[] = "---\n" 231 "archs: [ armv7k ]\n" 232 "platform: watchos\n" 233 "install-name: Test.dylib\n" 234 "...\n"; 235 236 auto Result = 237 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos, "Test.tbd")); 238 EXPECT_TRUE(!!Result); 239 auto File = std::move(Result.get()); 240 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 241 EXPECT_EQ(PlatformKind::watchOS, File->getPlatform()); 242 } 243 244 TEST(TBDv1, Platform_tvOS) { 245 static const char tbd_v1_platform_tvos[] = "---\n" 246 "archs: [ arm64 ]\n" 247 "platform: tvos\n" 248 "install-name: Test.dylib\n" 249 "...\n"; 250 251 auto Result = 252 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos, "Test.tbd")); 253 EXPECT_TRUE(!!Result); 254 auto File = std::move(Result.get()); 255 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 256 EXPECT_EQ(PlatformKind::tvOS, File->getPlatform()); 257 } 258 259 TEST(TBDv1, Platform_bridgeOS) { 260 static const char tbd_v1_platform_bridgeos[] = "---\n" 261 "archs: [ armv7k ]\n" 262 "platform: bridgeos\n" 263 "install-name: Test.dylib\n" 264 "...\n"; 265 266 auto Result = 267 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos, "Test.tbd")); 268 EXPECT_TRUE(!!Result); 269 auto File = std::move(Result.get()); 270 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 271 EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform()); 272 } 273 274 TEST(TBDv1, Swift_1_0) { 275 static const char tbd_v1_swift_1_0[] = "---\n" 276 "archs: [ arm64 ]\n" 277 "platform: ios\n" 278 "install-name: Test.dylib\n" 279 "swift-version: 1.0\n" 280 "...\n"; 281 282 auto Result = 283 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0, "Test.tbd")); 284 EXPECT_TRUE(!!Result); 285 auto File = std::move(Result.get()); 286 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 287 EXPECT_EQ(1U, File->getSwiftABIVersion()); 288 } 289 290 TEST(TBDv1, Swift_1_1) { 291 static const char tbd_v1_swift_1_1[] = "---\n" 292 "archs: [ arm64 ]\n" 293 "platform: ios\n" 294 "install-name: Test.dylib\n" 295 "swift-version: 1.1\n" 296 "...\n"; 297 298 auto Result = 299 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1, "Test.tbd")); 300 EXPECT_TRUE(!!Result); 301 auto File = std::move(Result.get()); 302 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 303 EXPECT_EQ(2U, File->getSwiftABIVersion()); 304 } 305 306 TEST(TBDv1, Swift_2_0) { 307 static const char tbd_v1_swift_2_0[] = "---\n" 308 "archs: [ arm64 ]\n" 309 "platform: ios\n" 310 "install-name: Test.dylib\n" 311 "swift-version: 2.0\n" 312 "...\n"; 313 314 auto Result = 315 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0, "Test.tbd")); 316 EXPECT_TRUE(!!Result); 317 auto File = std::move(Result.get()); 318 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 319 EXPECT_EQ(3U, File->getSwiftABIVersion()); 320 } 321 322 TEST(TBDv1, Swift_3_0) { 323 static const char tbd_v1_swift_3_0[] = "---\n" 324 "archs: [ arm64 ]\n" 325 "platform: ios\n" 326 "install-name: Test.dylib\n" 327 "swift-version: 3.0\n" 328 "...\n"; 329 330 auto Result = 331 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0, "Test.tbd")); 332 EXPECT_TRUE(!!Result); 333 auto File = std::move(Result.get()); 334 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 335 EXPECT_EQ(4U, File->getSwiftABIVersion()); 336 } 337 338 TEST(TBDv1, Swift_4_0) { 339 static const char tbd_v1_swift_4_0[] = "---\n" 340 "archs: [ arm64 ]\n" 341 "platform: ios\n" 342 "install-name: Test.dylib\n" 343 "swift-version: 4.0\n" 344 "...\n"; 345 346 auto Result = 347 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0, "Test.tbd")); 348 EXPECT_FALSE(!!Result); 349 auto errorMessage = toString(Result.takeError()); 350 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI " 351 "version.\nswift-version: 4.0\n ^~~\n", 352 errorMessage); 353 } 354 355 TEST(TBDv1, Swift_5) { 356 static const char tbd_v1_swift_5[] = "---\n" 357 "archs: [ arm64 ]\n" 358 "platform: ios\n" 359 "install-name: Test.dylib\n" 360 "swift-version: 5\n" 361 "...\n"; 362 363 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5, "Test.tbd")); 364 EXPECT_TRUE(!!Result); 365 auto File = std::move(Result.get()); 366 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 367 EXPECT_EQ(5U, File->getSwiftABIVersion()); 368 } 369 370 TEST(TBDv1, Swift_99) { 371 static const char tbd_v1_swift_99[] = "---\n" 372 "archs: [ arm64 ]\n" 373 "platform: ios\n" 374 "install-name: Test.dylib\n" 375 "swift-version: 99\n" 376 "...\n"; 377 378 auto Result = 379 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99, "Test.tbd")); 380 EXPECT_TRUE(!!Result); 381 auto File = std::move(Result.get()); 382 EXPECT_EQ(FileType::TBD_V1, File->getFileType()); 383 EXPECT_EQ(99U, File->getSwiftABIVersion()); 384 } 385 386 TEST(TBDv1, UnknownArchitecture) { 387 static const char tbd_v1_file_unknown_architecture[] = 388 "---\n" 389 "archs: [ foo ]\n" 390 "platform: macosx\n" 391 "install-name: Test.dylib\n" 392 "...\n"; 393 394 auto Result = TextAPIReader::get( 395 MemoryBufferRef(tbd_v1_file_unknown_architecture, "Test.tbd")); 396 EXPECT_TRUE(!!Result); 397 } 398 399 TEST(TBDv1, UnknownPlatform) { 400 static const char tbd_v1_file_unknown_platform[] = "---\n" 401 "archs: [ i386 ]\n" 402 "platform: newOS\n" 403 "...\n"; 404 405 auto Result = TextAPIReader::get( 406 MemoryBufferRef(tbd_v1_file_unknown_platform, "Test.tbd")); 407 EXPECT_FALSE(!!Result); 408 auto errorMessage = toString(Result.takeError()); 409 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: " 410 "newOS\n ^~~~~\n", 411 errorMessage); 412 } 413 414 TEST(TBDv1, MalformedFile1) { 415 static const char malformed_file1[] = "---\n" 416 "archs: [ arm64 ]\n" 417 "foobar: \"Unsupported key\"\n" 418 "...\n"; 419 420 auto Result = 421 TextAPIReader::get(MemoryBufferRef(malformed_file1, "Test.tbd")); 422 EXPECT_FALSE(!!Result); 423 auto errorMessage = toString(Result.takeError()); 424 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key " 425 "'platform'\narchs: [ arm64 ]\n^\n", 426 errorMessage); 427 } 428 429 TEST(TBDv1, MalformedFile2) { 430 static const char malformed_file2[] = "---\n" 431 "archs: [ arm64 ]\n" 432 "platform: ios\n" 433 "install-name: Test.dylib\n" 434 "foobar: \"Unsupported key\"\n" 435 "...\n"; 436 437 auto Result = 438 TextAPIReader::get(MemoryBufferRef(malformed_file2, "Test.tbd")); 439 EXPECT_FALSE(!!Result); 440 auto errorMessage = toString(Result.takeError()); 441 ASSERT_EQ( 442 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: " 443 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n", 444 errorMessage); 445 } 446 447 } // end namespace TBDv1. 448