clang_runtimes_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/ostream.h"
  11. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/Object/Binary.h"
  14. #include "llvm/Object/ObjectFile.h"
  15. #include "llvm/Support/ThreadPool.h"
  16. #include "llvm/Support/Threading.h"
  17. #include "llvm/Support/VirtualFileSystem.h"
  18. #include "llvm/TargetParser/Host.h"
  19. #include "llvm/TargetParser/Triple.h"
  20. #include "testing/base/capture_std_streams.h"
  21. #include "testing/base/global_exe_path.h"
  22. #include "toolchain/base/llvm_tools.h"
  23. #include "toolchain/driver/clang_runner.h"
  24. #include "toolchain/driver/llvm_runner.h"
  25. #include "toolchain/driver/runtimes_cache.h"
  26. #include "toolchain/install/install_paths.h"
  27. namespace Carbon {
  28. namespace {
  29. using ::testing::Eq;
  30. using ::testing::HasSubstr;
  31. using ::testing::IsSupersetOf;
  32. // NOLINTNEXTLINE(modernize-use-trailing-return-type): Macro based function.
  33. MATCHER_P(TextSymbolNamed, name_matcher, "") {
  34. llvm::Expected<llvm::StringRef> name = arg.getName();
  35. if (auto error = name.takeError()) {
  36. *result_listener << "with an error instead of a name: " << error;
  37. return false;
  38. }
  39. if (!testing::ExplainMatchResult(name_matcher, *name, result_listener)) {
  40. return false;
  41. }
  42. // We have to dig out the section to determine if this was a text symbol.
  43. auto expected_section_it = arg.getSection();
  44. if (auto error = expected_section_it.takeError()) {
  45. *result_listener << "without a section: " << error;
  46. return false;
  47. }
  48. llvm::object::SectionRef section = **expected_section_it;
  49. if (!section.isText()) {
  50. *result_listener << "in the non-text section: " << *section.getName();
  51. return false;
  52. }
  53. return true;
  54. }
  55. class ClangRuntimesTest : public ::testing::Test {
  56. public:
  57. InstallPaths install_paths_ =
  58. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  59. Runtimes::Cache runtimes_cache_ =
  60. *Runtimes::Cache::MakeSystem(install_paths_);
  61. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs_ =
  62. llvm::vfs::getRealFileSystem();
  63. };
  64. TEST_F(ClangRuntimesTest, ResourceDir) {
  65. ClangRunner runner(&install_paths_, vfs_, &llvm::errs());
  66. // Note that we can't test arbitrary targets here as we need to be able to
  67. // compile the builtin functions for the target. We use the default target as
  68. // the most likely to pass.
  69. std::string target = llvm::sys::getDefaultTargetTriple();
  70. llvm::Triple target_triple(target);
  71. Runtimes::Cache::Features features = {.target = target};
  72. auto runtimes = *runtimes_cache_.Lookup(features);
  73. llvm::DefaultThreadPool threads(llvm::optimal_concurrency());
  74. ClangResourceDirBuilder resource_dir_builder(&runner, &threads, target_triple,
  75. &runtimes);
  76. auto build_result = std::move(resource_dir_builder).Wait();
  77. ASSERT_TRUE(build_result.ok()) << build_result.error();
  78. std::filesystem::path resource_dir_path = std::move(*build_result);
  79. // For Linux we can directly check the CRT begin/end object files.
  80. if (target_triple.isOSLinux()) {
  81. std::filesystem::path crt_begin_path =
  82. resource_dir_path / "lib" / target / "clang_rt.crtbegin.o";
  83. ASSERT_TRUE(std::filesystem::is_regular_file(crt_begin_path));
  84. auto begin_result =
  85. llvm::object::ObjectFile::createObjectFile(crt_begin_path.native());
  86. llvm::object::ObjectFile& crtbegin = *begin_result->getBinary();
  87. EXPECT_TRUE(crtbegin.isELF());
  88. EXPECT_TRUE(crtbegin.isObject());
  89. EXPECT_THAT(crtbegin.getArch(), Eq(target_triple.getArch()));
  90. llvm::SmallVector<llvm::object::SymbolRef> symbols(crtbegin.symbols());
  91. // The first symbol should come from the source file.
  92. EXPECT_THAT(*symbols.front().getName(), Eq("crtbegin.c"));
  93. // Check for representative symbols of `crtbegin.o` -- we always use
  94. // `.init_array` in our runtimes build so we have predictable functions.
  95. EXPECT_THAT(symbols, IsSupersetOf({TextSymbolNamed("__do_init"),
  96. TextSymbolNamed("__do_fini")}));
  97. std::filesystem::path crt_end_path =
  98. resource_dir_path / "lib" / target / "clang_rt.crtend.o";
  99. ASSERT_TRUE(std::filesystem::is_regular_file(crt_end_path));
  100. auto end_result =
  101. llvm::object::ObjectFile::createObjectFile(crt_end_path.native());
  102. llvm::object::ObjectFile& crtend = *end_result->getBinary();
  103. EXPECT_TRUE(crtend.isELF());
  104. EXPECT_TRUE(crtend.isObject());
  105. EXPECT_THAT(crtend.getArch(), Eq(target_triple.getArch()));
  106. // Just check the source file symbol, not much of interest in the end.
  107. llvm::object::SymbolRef crtend_front_symbol = *crtend.symbol_begin();
  108. EXPECT_THAT(*crtend_front_symbol.getName(), Eq("crtend.c"));
  109. }
  110. // Across all targets, check that the builtins archive exists, and contains a
  111. // relevant symbol by running the `llvm-nm` tool over it. Using `nm` rather
  112. // than directly inspecting the objects is a bit awkward, but lets us easily
  113. // ignore the wrapping in an archive file.
  114. std::filesystem::path builtins_path =
  115. resource_dir_path / "lib" / target / "libclang_rt.builtins.a";
  116. LLVMRunner llvm_runner(&install_paths_, &llvm::errs());
  117. std::string out;
  118. std::string err;
  119. EXPECT_TRUE(Testing::CallWithCapturedOutput(out, err, [&] {
  120. return llvm_runner.Run(LLVMTool::Nm, {builtins_path.native()});
  121. }));
  122. // Check that we found a definition of `__mulodi4`, a builtin function
  123. // provided by Compiler-RT, but not `libgcc` historically. Note that on macOS
  124. // there is a leading `_` due to mangling.
  125. EXPECT_THAT(out, HasSubstr(target_triple.isMacOSX() ? "T ___mulodi4\n"
  126. : "T __mulodi4\n"));
  127. // Check that we don't include the `chkstk` builtins outside of Windows.
  128. if (!target_triple.isOSWindows()) {
  129. EXPECT_THAT(out, Not(HasSubstr("chkstk")));
  130. }
  131. }
  132. } // namespace
  133. } // namespace Carbon