TextStubV3Tests.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //===-- TextStubV3Tests.cpp - TBD V3 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. #include "llvm/TextAPI/MachO/InterfaceFile.h"
  9. #include "llvm/TextAPI/MachO/TextAPIReader.h"
  10. #include "llvm/TextAPI/MachO/TextAPIWriter.h"
  11. #include "gtest/gtest.h"
  12. #include <string>
  13. #include <vector>
  14. using namespace llvm;
  15. using namespace llvm::MachO;
  16. struct ExportedSymbol {
  17. SymbolKind Kind;
  18. std::string Name;
  19. bool WeakDefined;
  20. bool ThreadLocalValue;
  21. };
  22. using ExportedSymbolSeq = std::vector<ExportedSymbol>;
  23. using UUIDs = std::vector<std::pair<Architecture, std::string>>;
  24. inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
  25. return std::tie(lhs.Kind, lhs.Name) < std::tie(rhs.Kind, rhs.Name);
  26. }
  27. inline bool operator==(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
  28. return std::tie(lhs.Kind, lhs.Name, lhs.WeakDefined, lhs.ThreadLocalValue) ==
  29. std::tie(rhs.Kind, rhs.Name, rhs.WeakDefined, rhs.ThreadLocalValue);
  30. }
  31. static ExportedSymbol TBDv3Symbols[] = {
  32. {SymbolKind::GlobalSymbol, "$ld$hide$os9.0$_sym1", false, false},
  33. {SymbolKind::GlobalSymbol, "_sym1", false, false},
  34. {SymbolKind::GlobalSymbol, "_sym2", false, false},
  35. {SymbolKind::GlobalSymbol, "_sym3", false, false},
  36. {SymbolKind::GlobalSymbol, "_sym4", false, false},
  37. {SymbolKind::GlobalSymbol, "_sym5", false, false},
  38. {SymbolKind::GlobalSymbol, "_tlv1", false, true},
  39. {SymbolKind::GlobalSymbol, "_tlv3", false, true},
  40. {SymbolKind::GlobalSymbol, "_weak1", true, false},
  41. {SymbolKind::GlobalSymbol, "_weak2", true, false},
  42. {SymbolKind::GlobalSymbol, "_weak3", true, false},
  43. {SymbolKind::ObjectiveCClass, "class1", false, false},
  44. {SymbolKind::ObjectiveCClass, "class2", false, false},
  45. {SymbolKind::ObjectiveCClass, "class3", false, false},
  46. {SymbolKind::ObjectiveCClassEHType, "class1", false, false},
  47. {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar1", false, false},
  48. {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar2", false, false},
  49. {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar3", false, false},
  50. };
  51. namespace TBDv3 {
  52. TEST(TBDv3, ReadFile) {
  53. static const char tbd_v3_file1[] =
  54. "--- !tapi-tbd-v3\n"
  55. "archs: [ armv7, arm64 ]\n"
  56. "uuids: [ 'armv7: 00000000-0000-0000-0000-000000000000',\n"
  57. " 'arm64: 11111111-1111-1111-1111-111111111111']\n"
  58. "platform: ios\n"
  59. "flags: [ installapi ]\n"
  60. "install-name: Test.dylib\n"
  61. "current-version: 2.3.4\n"
  62. "compatibility-version: 1.0\n"
  63. "swift-abi-version: 1.1\n"
  64. "parent-umbrella: Umbrella.dylib\n"
  65. "exports:\n"
  66. " - archs: [ armv7, arm64 ]\n"
  67. " allowable-clients: [ clientA ]\n"
  68. " re-exports: [ /usr/lib/libfoo.dylib ]\n"
  69. " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
  70. " objc-classes: [ class1, class2 ]\n"
  71. " objc-eh-types: [ class1 ]\n"
  72. " objc-ivars: [ class1._ivar1, class1._ivar2 ]\n"
  73. " weak-def-symbols: [ _weak1, _weak2 ]\n"
  74. " thread-local-symbols: [ _tlv1, _tlv3 ]\n"
  75. " - archs: [ armv7 ]\n"
  76. " symbols: [ _sym5 ]\n"
  77. " objc-classes: [ class3 ]\n"
  78. " objc-ivars: [ class1._ivar3 ]\n"
  79. " weak-def-symbols: [ _weak3 ]\n"
  80. " thread-local-symbols: [ _tlv3 ]\n"
  81. "...\n";
  82. auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v3_file1, "Test.tbd"));
  83. EXPECT_TRUE(!!Result);
  84. auto File = std::move(Result.get());
  85. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  86. auto Archs = AK_armv7 | AK_arm64;
  87. EXPECT_EQ(Archs, File->getArchitectures());
  88. UUIDs uuids = {{AK_armv7, "00000000-0000-0000-0000-000000000000"},
  89. {AK_arm64, "11111111-1111-1111-1111-111111111111"}};
  90. EXPECT_EQ(uuids, File->uuids());
  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::Retain_Release, File->getObjCConstraint());
  97. EXPECT_TRUE(File->isTwoLevelNamespace());
  98. EXPECT_TRUE(File->isApplicationExtensionSafe());
  99. EXPECT_TRUE(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. ExportedSymbolSeq Exports;
  107. for (const auto *Sym : File->symbols()) {
  108. EXPECT_FALSE(Sym->isWeakReferenced());
  109. EXPECT_FALSE(Sym->isUndefined());
  110. Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(),
  111. Sym->isWeakDefined(),
  112. Sym->isThreadLocalValue()});
  113. }
  114. llvm::sort(Exports.begin(), Exports.end());
  115. EXPECT_EQ(sizeof(TBDv3Symbols) / sizeof(ExportedSymbol), Exports.size());
  116. EXPECT_TRUE(
  117. std::equal(Exports.begin(), Exports.end(), std::begin(TBDv3Symbols)));
  118. }
  119. TEST(TBDv3, WriteFile) {
  120. static const char tbd_v3_file3[] =
  121. "--- !tapi-tbd-v3\n"
  122. "archs: [ i386, x86_64 ]\n"
  123. "platform: macosx\n"
  124. "install-name: '/usr/lib/libfoo.dylib'\n"
  125. "current-version: 1.2.3\n"
  126. "compatibility-version: 0\n"
  127. "swift-abi-version: 5\n"
  128. "exports:\n"
  129. " - archs: [ i386 ]\n"
  130. " symbols: [ _sym1 ]\n"
  131. " weak-def-symbols: [ _sym2 ]\n"
  132. " thread-local-symbols: [ _sym3 ]\n"
  133. " - archs: [ x86_64 ]\n"
  134. " allowable-clients: [ clientA ]\n"
  135. " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
  136. " objc-classes: [ Class1 ]\n"
  137. " objc-eh-types: [ Class1 ]\n"
  138. " objc-ivars: [ Class1._ivar1 ]\n"
  139. "...\n";
  140. InterfaceFile File;
  141. File.setPath("libfoo.dylib");
  142. File.setInstallName("/usr/lib/libfoo.dylib");
  143. File.setFileType(FileType::TBD_V3);
  144. File.setArchitectures(AK_i386 | AK_x86_64);
  145. File.setPlatform(PlatformKind::macOS);
  146. File.setCurrentVersion(PackedVersion(1, 2, 3));
  147. File.setTwoLevelNamespace();
  148. File.setApplicationExtensionSafe();
  149. File.setSwiftABIVersion(5);
  150. File.setObjCConstraint(ObjCConstraintType::Retain_Release);
  151. File.addAllowableClient("clientA", AK_x86_64);
  152. File.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64);
  153. File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", AK_i386);
  154. File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", AK_i386,
  155. SymbolFlags::WeakDefined);
  156. File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", AK_i386,
  157. SymbolFlags::ThreadLocalValue);
  158. File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", AK_x86_64);
  159. File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", AK_x86_64);
  160. File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
  161. AK_x86_64);
  162. SmallString<4096> Buffer;
  163. raw_svector_ostream OS(Buffer);
  164. auto Result = TextAPIWriter::writeToStream(OS, File);
  165. EXPECT_FALSE(Result);
  166. EXPECT_STREQ(tbd_v3_file3, Buffer.c_str());
  167. }
  168. TEST(TBDv3, Platform_macOS) {
  169. static const char tbd_v1_platform_macos[] = "--- !tapi-tbd-v3\n"
  170. "archs: [ x86_64 ]\n"
  171. "platform: macosx\n"
  172. "install-name: Test.dylib\n"
  173. "...\n";
  174. auto Result =
  175. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos, "Test.tbd"));
  176. EXPECT_TRUE(!!Result);
  177. auto File = std::move(Result.get());
  178. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  179. EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
  180. }
  181. TEST(TBDv3, Platform_iOS) {
  182. static const char tbd_v1_platform_ios[] = "--- !tapi-tbd-v3\n"
  183. "archs: [ arm64 ]\n"
  184. "platform: ios\n"
  185. "install-name: Test.dylib\n"
  186. "...\n";
  187. auto Result =
  188. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios, "Test.tbd"));
  189. EXPECT_TRUE(!!Result);
  190. auto File = std::move(Result.get());
  191. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  192. EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
  193. }
  194. TEST(TBDv3, Platform_watchOS) {
  195. static const char tbd_v1_platform_watchos[] = "--- !tapi-tbd-v3\n"
  196. "archs: [ armv7k ]\n"
  197. "platform: watchos\n"
  198. "install-name: Test.dylib\n"
  199. "...\n";
  200. auto Result =
  201. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos, "Test.tbd"));
  202. EXPECT_TRUE(!!Result);
  203. auto File = std::move(Result.get());
  204. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  205. EXPECT_EQ(PlatformKind::watchOS, File->getPlatform());
  206. }
  207. TEST(TBDv3, Platform_tvOS) {
  208. static const char tbd_v1_platform_tvos[] = "--- !tapi-tbd-v3\n"
  209. "archs: [ arm64 ]\n"
  210. "platform: tvos\n"
  211. "install-name: Test.dylib\n"
  212. "...\n";
  213. auto Result =
  214. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos, "Test.tbd"));
  215. EXPECT_TRUE(!!Result);
  216. auto File = std::move(Result.get());
  217. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  218. EXPECT_EQ(PlatformKind::tvOS, File->getPlatform());
  219. }
  220. TEST(TBDv3, Platform_bridgeOS) {
  221. static const char tbd_v1_platform_bridgeos[] = "--- !tapi-tbd-v3\n"
  222. "archs: [ armv7k ]\n"
  223. "platform: bridgeos\n"
  224. "install-name: Test.dylib\n"
  225. "...\n";
  226. auto Result =
  227. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos, "Test.tbd"));
  228. EXPECT_TRUE(!!Result);
  229. auto File = std::move(Result.get());
  230. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  231. EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
  232. }
  233. TEST(TBDv3, Swift_1_0) {
  234. static const char tbd_v1_swift_1_0[] = "--- !tapi-tbd-v3\n"
  235. "archs: [ arm64 ]\n"
  236. "platform: ios\n"
  237. "install-name: Test.dylib\n"
  238. "swift-abi-version: 1.0\n"
  239. "...\n";
  240. auto Result =
  241. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0, "Test.tbd"));
  242. EXPECT_TRUE(!!Result);
  243. auto File = std::move(Result.get());
  244. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  245. EXPECT_EQ(1U, File->getSwiftABIVersion());
  246. }
  247. TEST(TBDv3, Swift_1_1) {
  248. static const char tbd_v1_swift_1_1[] = "--- !tapi-tbd-v3\n"
  249. "archs: [ arm64 ]\n"
  250. "platform: ios\n"
  251. "install-name: Test.dylib\n"
  252. "swift-abi-version: 1.1\n"
  253. "...\n";
  254. auto Result =
  255. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1, "Test.tbd"));
  256. EXPECT_TRUE(!!Result);
  257. auto File = std::move(Result.get());
  258. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  259. EXPECT_EQ(2U, File->getSwiftABIVersion());
  260. }
  261. TEST(TBDv3, Swift_2_0) {
  262. static const char tbd_v1_swift_2_0[] = "--- !tapi-tbd-v3\n"
  263. "archs: [ arm64 ]\n"
  264. "platform: ios\n"
  265. "install-name: Test.dylib\n"
  266. "swift-abi-version: 2.0\n"
  267. "...\n";
  268. auto Result =
  269. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0, "Test.tbd"));
  270. EXPECT_TRUE(!!Result);
  271. auto File = std::move(Result.get());
  272. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  273. EXPECT_EQ(3U, File->getSwiftABIVersion());
  274. }
  275. TEST(TBDv3, Swift_3_0) {
  276. static const char tbd_v1_swift_3_0[] = "--- !tapi-tbd-v3\n"
  277. "archs: [ arm64 ]\n"
  278. "platform: ios\n"
  279. "install-name: Test.dylib\n"
  280. "swift-abi-version: 3.0\n"
  281. "...\n";
  282. auto Result =
  283. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0, "Test.tbd"));
  284. EXPECT_TRUE(!!Result);
  285. auto File = std::move(Result.get());
  286. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  287. EXPECT_EQ(4U, File->getSwiftABIVersion());
  288. }
  289. TEST(TBDv3, Swift_4_0) {
  290. static const char tbd_v1_swift_4_0[] = "--- !tapi-tbd-v3\n"
  291. "archs: [ arm64 ]\n"
  292. "platform: ios\n"
  293. "install-name: Test.dylib\n"
  294. "swift-abi-version: 4.0\n"
  295. "...\n";
  296. auto Result =
  297. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0, "Test.tbd"));
  298. EXPECT_FALSE(!!Result);
  299. auto errorMessage = toString(Result.takeError());
  300. EXPECT_EQ("malformed file\nTest.tbd:5:20: error: invalid Swift ABI "
  301. "version.\nswift-abi-version: 4.0\n ^~~\n",
  302. errorMessage);
  303. }
  304. TEST(TBDv3, Swift_5) {
  305. static const char tbd_v1_swift_5[] = "--- !tapi-tbd-v3\n"
  306. "archs: [ arm64 ]\n"
  307. "platform: ios\n"
  308. "install-name: Test.dylib\n"
  309. "swift-abi-version: 5\n"
  310. "...\n";
  311. auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5, "Test.tbd"));
  312. EXPECT_TRUE(!!Result);
  313. auto File = std::move(Result.get());
  314. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  315. EXPECT_EQ(5U, File->getSwiftABIVersion());
  316. }
  317. TEST(TBDv3, Swift_99) {
  318. static const char tbd_v1_swift_99[] = "--- !tapi-tbd-v3\n"
  319. "archs: [ arm64 ]\n"
  320. "platform: ios\n"
  321. "install-name: Test.dylib\n"
  322. "swift-abi-version: 99\n"
  323. "...\n";
  324. auto Result =
  325. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99, "Test.tbd"));
  326. EXPECT_TRUE(!!Result);
  327. auto File = std::move(Result.get());
  328. EXPECT_EQ(FileType::TBD_V3, File->getFileType());
  329. EXPECT_EQ(99U, File->getSwiftABIVersion());
  330. }
  331. TEST(TBDv3, UnknownArchitecture) {
  332. static const char tbd_v3_file_unknown_architecture[] =
  333. "--- !tapi-tbd-v3\n"
  334. "archs: [ foo ]\n"
  335. "platform: macosx\n"
  336. "install-name: Test.dylib\n"
  337. "...\n";
  338. auto Result = TextAPIReader::get(
  339. MemoryBufferRef(tbd_v3_file_unknown_architecture, "Test.tbd"));
  340. EXPECT_TRUE(!!Result);
  341. }
  342. TEST(TBDv3, UnknownPlatform) {
  343. static const char tbd_v3_file_unknown_platform[] = "--- !tapi-tbd-v3\n"
  344. "archs: [ i386 ]\n"
  345. "platform: newOS\n"
  346. "...\n";
  347. auto Result = TextAPIReader::get(
  348. MemoryBufferRef(tbd_v3_file_unknown_platform, "Test.tbd"));
  349. EXPECT_FALSE(!!Result);
  350. auto errorMessage = toString(Result.takeError());
  351. EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
  352. "newOS\n ^~~~~\n",
  353. errorMessage);
  354. }
  355. TEST(TBDv3, MalformedFile1) {
  356. static const char malformed_file1[] = "--- !tapi-tbd-v3\n"
  357. "archs: [ arm64 ]\n"
  358. "foobar: \"Unsupported key\"\n"
  359. "...\n";
  360. auto Result =
  361. TextAPIReader::get(MemoryBufferRef(malformed_file1, "Test.tbd"));
  362. EXPECT_FALSE(!!Result);
  363. auto errorMessage = toString(Result.takeError());
  364. ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
  365. "'platform'\narchs: [ arm64 ]\n^\n",
  366. errorMessage);
  367. }
  368. TEST(TBDv3, MalformedFile2) {
  369. static const char malformed_file2[] = "--- !tapi-tbd-v3\n"
  370. "archs: [ arm64 ]\n"
  371. "platform: ios\n"
  372. "install-name: Test.dylib\n"
  373. "foobar: \"Unsupported key\"\n"
  374. "...\n";
  375. auto Result =
  376. TextAPIReader::get(MemoryBufferRef(malformed_file2, "Test.tbd"));
  377. EXPECT_FALSE(!!Result);
  378. auto errorMessage = toString(Result.takeError());
  379. ASSERT_EQ(
  380. "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
  381. "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
  382. errorMessage);
  383. }
  384. } // namespace TBDv3