clang_runtimes_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/driver/clang_runtimes.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <filesystem>
  8. #include <string>
  9. #include <utility>
  10. #include "common/check.h"
  11. #include "common/ostream.h"
  12. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/Object/Binary.h"
  15. #include "llvm/Object/ObjectFile.h"
  16. #include "llvm/Support/ThreadPool.h"
  17. #include "llvm/Support/Threading.h"
  18. #include "llvm/Support/VirtualFileSystem.h"
  19. #include "llvm/TargetParser/Host.h"
  20. #include "llvm/TargetParser/Triple.h"
  21. #include "testing/base/capture_std_streams.h"
  22. #include "testing/base/global_exe_path.h"
  23. #include "toolchain/base/install_paths.h"
  24. #include "toolchain/base/llvm_tools.h"
  25. #include "toolchain/driver/clang_runner.h"
  26. #include "toolchain/driver/llvm_runner.h"
  27. #include "toolchain/driver/runtimes_cache.h"
  28. namespace Carbon {
  29. namespace {
  30. using ::testing::Each;
  31. using ::testing::Eq;
  32. using ::testing::HasSubstr;
  33. using ::testing::IsSupersetOf;
  34. // NOLINTNEXTLINE(modernize-use-trailing-return-type): Macro based function.
  35. MATCHER_P(TextSymbolNamed, name_matcher, "") {
  36. llvm::Expected<llvm::StringRef> name = arg.getName();
  37. if (auto error = name.takeError()) {
  38. *result_listener << "with an error instead of a name: " << error;
  39. return false;
  40. }
  41. if (!testing::ExplainMatchResult(name_matcher, *name, result_listener)) {
  42. return false;
  43. }
  44. // We have to dig out the section to determine if this was a text symbol.
  45. auto expected_section_it = arg.getSection();
  46. if (auto error = expected_section_it.takeError()) {
  47. *result_listener << "without a section: " << error;
  48. return false;
  49. }
  50. llvm::object::SectionRef section = **expected_section_it;
  51. if (!section.isText()) {
  52. *result_listener << "in the non-text section: " << *section.getName();
  53. return false;
  54. }
  55. return true;
  56. }
  57. // NOLINTNEXTLINE(modernize-use-trailing-return-type): Macro based function.
  58. MATCHER(IsBasename, "") {
  59. std::filesystem::path path = arg;
  60. return path == path.filename();
  61. }
  62. class ClangRuntimesTest : public ::testing::Test {
  63. public:
  64. // Helper to get the `llvm-nm` listing of defined symbols for an archive.
  65. //
  66. // TODO: It would be nice to use a library API and matchers instead of
  67. // `llvm-nm` and matching text on the output.
  68. auto NmListDefinedSymbols(const std::filesystem::path& archive)
  69. -> std::string {
  70. LLVMRunner llvm_runner(&install_paths_, &llvm::errs());
  71. std::string out;
  72. std::string err;
  73. bool result = Testing::CallWithCapturedOutput(out, err, [&] {
  74. return llvm_runner.Run(
  75. LLVMTool::Nm, {"--format=just-symbols", "--defined-only", "--quiet",
  76. archive.native()});
  77. });
  78. CARBON_CHECK(result, "Unable to run `llvm-nm`:\n{1}", err);
  79. return out;
  80. }
  81. // Helper to expect a specific symbol in the `llvm-nm` list.
  82. //
  83. // This handles platform-specific formatting of symbols.
  84. auto ExpectSymbol(llvm::StringRef nm_list, llvm::StringRef symbol) -> void {
  85. std::string symbol_substr = llvm::formatv(
  86. target_triple_.isMacOSX() ? "\n_{0}\n" : "\n{0}\n", symbol);
  87. // Do the actual match with `HasSubstr` so it can explain failures.
  88. EXPECT_THAT(nm_list, HasSubstr(symbol_substr));
  89. }
  90. // Helper to get the names of archive members.
  91. auto ListArchiveMemberNames(const std::filesystem::path& archive_path)
  92. -> llvm::SmallVector<std::string> {
  93. llvm::SmallVector<std::string> result;
  94. auto archive_buffer_result =
  95. llvm::MemoryBuffer::getFile(archive_path.native());
  96. CARBON_CHECK(!archive_buffer_result.getError(), "Unable to open {0}: {1}",
  97. archive_path, archive_buffer_result.getError().message());
  98. auto archive = llvm::cantFail(llvm::object::Archive::create(
  99. archive_buffer_result.get()->getMemBufferRef()));
  100. llvm::Error error = llvm::Error::success();
  101. for (const auto& child : archive->children(error)) {
  102. result.push_back(child.getName()->str());
  103. }
  104. CARBON_CHECK(!error, "Error reading members of archive {0}: {1}",
  105. archive_path, error);
  106. return result;
  107. }
  108. InstallPaths install_paths_ =
  109. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  110. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs_ =
  111. llvm::vfs::getRealFileSystem();
  112. // Note that for debugging, you can pass `llvm::errs()` as the vlog stream,
  113. // but this makes the output both very verbose and hard to use with multiple
  114. // threads.
  115. ClangRunner runner_{&install_paths_, vfs_};
  116. // Note that we can't test arbitrary targets here as we need to be able to
  117. // compile the builtin functions for the target. We use the default target as
  118. // the most likely to pass.
  119. std::string target_ = llvm::sys::getDefaultTargetTriple();
  120. llvm::Triple target_triple_{target_};
  121. Runtimes::Cache runtimes_cache_ =
  122. *Runtimes::Cache::MakeSystem(install_paths_);
  123. Runtimes::Cache::Features features = {.target = target_};
  124. Runtimes runtimes_ = *runtimes_cache_.Lookup(features);
  125. // Note that for debugging it may be useful to replace this with a
  126. // single-threaded thread pool. However the test will be _much_ slower.
  127. llvm::DefaultThreadPool threads_{llvm::optimal_concurrency()};
  128. };
  129. TEST_F(ClangRuntimesTest, ResourceDir) {
  130. ClangResourceDirBuilder resource_dir_builder(&runner_, &threads_,
  131. target_triple_, &runtimes_);
  132. auto build_result = std::move(resource_dir_builder).Wait();
  133. ASSERT_TRUE(build_result.ok()) << build_result.error();
  134. std::filesystem::path resource_dir_path = std::move(*build_result);
  135. // For Linux we can directly check the CRT begin/end object files.
  136. if (target_triple_.isOSLinux()) {
  137. std::filesystem::path crt_begin_path =
  138. resource_dir_path / "lib" / target_ / "clang_rt.crtbegin.o";
  139. ASSERT_TRUE(std::filesystem::is_regular_file(crt_begin_path));
  140. auto begin_result =
  141. llvm::object::ObjectFile::createObjectFile(crt_begin_path.native());
  142. llvm::object::ObjectFile& crtbegin = *begin_result->getBinary();
  143. EXPECT_TRUE(crtbegin.isELF());
  144. EXPECT_TRUE(crtbegin.isObject());
  145. EXPECT_THAT(crtbegin.getArch(), Eq(target_triple_.getArch()));
  146. llvm::SmallVector<llvm::object::SymbolRef> symbols(crtbegin.symbols());
  147. // The first symbol should come from the source file.
  148. EXPECT_THAT(*symbols.front().getName(), Eq("crtbegin.c"));
  149. // Check for representative symbols of `crtbegin.o` -- we always use
  150. // `.init_array` in our runtimes build so we have predictable functions.
  151. EXPECT_THAT(symbols, IsSupersetOf({TextSymbolNamed("__do_init"),
  152. TextSymbolNamed("__do_fini")}));
  153. std::filesystem::path crt_end_path =
  154. resource_dir_path / "lib" / target_ / "clang_rt.crtend.o";
  155. ASSERT_TRUE(std::filesystem::is_regular_file(crt_end_path));
  156. auto end_result =
  157. llvm::object::ObjectFile::createObjectFile(crt_end_path.native());
  158. llvm::object::ObjectFile& crtend = *end_result->getBinary();
  159. EXPECT_TRUE(crtend.isELF());
  160. EXPECT_TRUE(crtend.isObject());
  161. EXPECT_THAT(crtend.getArch(), Eq(target_triple_.getArch()));
  162. // Just check the source file symbol, not much of interest in the end.
  163. llvm::object::SymbolRef crtend_front_symbol = *crtend.symbol_begin();
  164. EXPECT_THAT(*crtend_front_symbol.getName(), Eq("crtend.c"));
  165. }
  166. // Across all targets, check that the builtins archive exists, and contains a
  167. // relevant symbol by running the `llvm-nm` tool over it. Using `nm` rather
  168. // than directly inspecting the objects is a bit awkward, but lets us easily
  169. // ignore the wrapping in an archive file.
  170. std::filesystem::path builtins_path =
  171. resource_dir_path / "lib" / target_ / "libclang_rt.builtins.a";
  172. std::string builtins_symbols = NmListDefinedSymbols(builtins_path);
  173. // Check that we found a definition of `__mulodi4`, a builtin function
  174. // provided by Compiler-RT.
  175. ExpectSymbol(builtins_symbols, "__mulodi4");
  176. // Check that we don't include the `chkstk` builtins outside of Windows.
  177. if (!target_triple_.isOSWindows()) {
  178. EXPECT_THAT(builtins_symbols, Not(HasSubstr("chkstk")));
  179. }
  180. // Check that member names don't contain full paths, as that is the
  181. // canonical format produced by `ar`.
  182. auto member_names = ListArchiveMemberNames(builtins_path);
  183. EXPECT_THAT(member_names, Each(IsBasename()));
  184. }
  185. TEST_F(ClangRuntimesTest, Libunwind) {
  186. LibunwindBuilder libunwind_builder(&runner_, &threads_, target_triple_,
  187. &runtimes_);
  188. auto build_result = std::move(libunwind_builder).Wait();
  189. ASSERT_TRUE(build_result.ok()) << build_result.error();
  190. std::filesystem::path runtimes_path = std::move(*build_result);
  191. std::filesystem::path libunwind_path = runtimes_path / "lib/libunwind.a";
  192. std::string libunwind_symbols = NmListDefinedSymbols(libunwind_path);
  193. // Check a few of the main exported symbols here. The set here is somewhat
  194. // arbitrary, but chosen to be among the more stable names and have at least
  195. // one from most of the object files that should be linked into the archive.
  196. ExpectSymbol(libunwind_symbols, "_Unwind_Resume");
  197. ExpectSymbol(libunwind_symbols, "_Unwind_Backtrace");
  198. ExpectSymbol(libunwind_symbols, "__unw_getcontext");
  199. ExpectSymbol(libunwind_symbols, "__unw_get_proc_info");
  200. // Check that member names don't contain full paths, as that is the
  201. // canonical format produced by `ar`.
  202. auto member_names = ListArchiveMemberNames(libunwind_path);
  203. EXPECT_THAT(member_names, Each(IsBasename()));
  204. }
  205. TEST_F(ClangRuntimesTest, Libcxx) {
  206. #if __has_feature(address_sanitizer)
  207. // ASan causes Clang and LLVM to be _egregiously_ inefficient at compiling
  208. // libc++, taking 5x - 10x longer than without ASan. Rough estimate is that it
  209. // would take 5-10 minutes on GitHub's Linux runner. Given the limited utility
  210. // of this test coverage, skip it in that configuration. This also misses
  211. // assert-coverage for building libc++, but we don't really expect issues
  212. // there. Misconfiguration and other common issues should still be covered in
  213. // fully optimized builds at much lower cost.
  214. GTEST_SKIP() << "Skipping build of libc++ with an ASan-itized Clang";
  215. #endif
  216. LibcxxBuilder libcxx_builder(&runner_, &threads_, target_triple_, &runtimes_);
  217. auto build_result = std::move(libcxx_builder).Wait();
  218. ASSERT_TRUE(build_result.ok()) << build_result.error();
  219. std::filesystem::path runtimes_path = std::move(*build_result);
  220. std::filesystem::path libcxx_path = runtimes_path / "lib/libc++.a";
  221. std::string libcxx_symbols = NmListDefinedSymbols(libcxx_path);
  222. // First check a few fundamental symbols from libc++.a, including symbols both
  223. // within the ABI namespace and outside of it.
  224. ExpectSymbol(libcxx_symbols, "_ZNKSt12bad_any_cast4whatEv");
  225. ExpectSymbol(libcxx_symbols, "_ZNSt2_C8to_charsEPcS0_d");
  226. ExpectSymbol(libcxx_symbols, "_ZSt17current_exceptionv");
  227. ExpectSymbol(libcxx_symbols, "_ZNKSt2_C10filesystem4path10__filenameEv");
  228. // Check that several of the libc++abi object files are also included in the
  229. // archive.
  230. ExpectSymbol(libcxx_symbols, "__cxa_bad_cast");
  231. ExpectSymbol(libcxx_symbols, "__cxa_new_handler");
  232. ExpectSymbol(libcxx_symbols, "__cxa_demangle");
  233. ExpectSymbol(libcxx_symbols, "__cxa_get_globals");
  234. ExpectSymbol(libcxx_symbols, "_ZSt9terminatev");
  235. // Check that member names don't contain full paths, as that is the
  236. // canonical format produced by `ar`.
  237. auto member_names = ListArchiveMemberNames(libcxx_path);
  238. EXPECT_THAT(member_names, Each(IsBasename()));
  239. }
  240. } // namespace
  241. } // namespace Carbon