BitReaderTest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
  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/ADT/STLExtras.h"
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/AsmParser/Parser.h"
  11. #include "llvm/Bitcode/BitcodeReader.h"
  12. #include "llvm/Bitcode/BitcodeWriter.h"
  13. #include "llvm/IR/LLVMContext.h"
  14. #include "llvm/IR/Module.h"
  15. #include "llvm/IR/Verifier.h"
  16. #include "llvm/Support/Debug.h"
  17. #include "llvm/Support/Error.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include "llvm/Support/SourceMgr.h"
  20. #include "gtest/gtest.h"
  21. using namespace llvm;
  22. namespace {
  23. std::unique_ptr<Module> parseAssembly(LLVMContext &Context,
  24. const char *Assembly) {
  25. SMDiagnostic Error;
  26. std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
  27. std::string ErrMsg;
  28. raw_string_ostream OS(ErrMsg);
  29. Error.print("", OS);
  30. // A failure here means that the test itself is buggy.
  31. if (!M)
  32. report_fatal_error(OS.str().c_str());
  33. return M;
  34. }
  35. static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
  36. SmallVectorImpl<char> &Buffer) {
  37. raw_svector_ostream OS(Buffer);
  38. WriteBitcodeToFile(*Mod, OS);
  39. }
  40. static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
  41. SmallString<1024> &Mem,
  42. const char *Assembly) {
  43. writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
  44. Expected<std::unique_ptr<Module>> ModuleOrErr =
  45. getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context);
  46. if (!ModuleOrErr)
  47. report_fatal_error("Could not parse bitcode module");
  48. return std::move(ModuleOrErr.get());
  49. }
  50. // Tests that lazy evaluation can parse functions out of order.
  51. TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
  52. SmallString<1024> Mem;
  53. LLVMContext Context;
  54. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  55. Context, Mem, "define void @f() {\n"
  56. " unreachable\n"
  57. "}\n"
  58. "define void @g() {\n"
  59. " unreachable\n"
  60. "}\n"
  61. "define void @h() {\n"
  62. " unreachable\n"
  63. "}\n"
  64. "define void @j() {\n"
  65. " unreachable\n"
  66. "}\n");
  67. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  68. Function *F = M->getFunction("f");
  69. Function *G = M->getFunction("g");
  70. Function *H = M->getFunction("h");
  71. Function *J = M->getFunction("j");
  72. // Initially all functions are not materialized (no basic blocks).
  73. EXPECT_TRUE(F->empty());
  74. EXPECT_TRUE(G->empty());
  75. EXPECT_TRUE(H->empty());
  76. EXPECT_TRUE(J->empty());
  77. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  78. // Materialize h.
  79. ASSERT_FALSE(H->materialize());
  80. EXPECT_TRUE(F->empty());
  81. EXPECT_TRUE(G->empty());
  82. EXPECT_FALSE(H->empty());
  83. EXPECT_TRUE(J->empty());
  84. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  85. // Materialize g.
  86. ASSERT_FALSE(G->materialize());
  87. EXPECT_TRUE(F->empty());
  88. EXPECT_FALSE(G->empty());
  89. EXPECT_FALSE(H->empty());
  90. EXPECT_TRUE(J->empty());
  91. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  92. // Materialize j.
  93. ASSERT_FALSE(J->materialize());
  94. EXPECT_TRUE(F->empty());
  95. EXPECT_FALSE(G->empty());
  96. EXPECT_FALSE(H->empty());
  97. EXPECT_FALSE(J->empty());
  98. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  99. // Materialize f.
  100. ASSERT_FALSE(F->materialize());
  101. EXPECT_FALSE(F->empty());
  102. EXPECT_FALSE(G->empty());
  103. EXPECT_FALSE(H->empty());
  104. EXPECT_FALSE(J->empty());
  105. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  106. }
  107. TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
  108. SmallString<1024> Mem;
  109. LLVMContext Context;
  110. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  111. Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
  112. "define void @func() {\n"
  113. " unreachable\n"
  114. "bb:\n"
  115. " unreachable\n"
  116. "}\n");
  117. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  118. EXPECT_FALSE(M->getFunction("func")->empty());
  119. }
  120. TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
  121. SmallString<1024> Mem;
  122. LLVMContext Context;
  123. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  124. Context, Mem, "define i8* @before() {\n"
  125. " ret i8* blockaddress(@func, %bb)\n"
  126. "}\n"
  127. "define void @other() {\n"
  128. " unreachable\n"
  129. "}\n"
  130. "define void @func() {\n"
  131. " unreachable\n"
  132. "bb:\n"
  133. " unreachable\n"
  134. "}\n");
  135. EXPECT_TRUE(M->getFunction("before")->empty());
  136. EXPECT_TRUE(M->getFunction("func")->empty());
  137. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  138. // Materialize @before, pulling in @func.
  139. EXPECT_FALSE(M->getFunction("before")->materialize());
  140. EXPECT_FALSE(M->getFunction("func")->empty());
  141. EXPECT_TRUE(M->getFunction("other")->empty());
  142. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  143. }
  144. TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
  145. SmallString<1024> Mem;
  146. LLVMContext Context;
  147. std::unique_ptr<Module> M = getLazyModuleFromAssembly(
  148. Context, Mem, "define void @func() {\n"
  149. " unreachable\n"
  150. "bb:\n"
  151. " unreachable\n"
  152. "}\n"
  153. "define void @other() {\n"
  154. " unreachable\n"
  155. "}\n"
  156. "define i8* @after() {\n"
  157. " ret i8* blockaddress(@func, %bb)\n"
  158. "}\n");
  159. EXPECT_TRUE(M->getFunction("after")->empty());
  160. EXPECT_TRUE(M->getFunction("func")->empty());
  161. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  162. // Materialize @after, pulling in @func.
  163. EXPECT_FALSE(M->getFunction("after")->materialize());
  164. EXPECT_FALSE(M->getFunction("func")->empty());
  165. EXPECT_TRUE(M->getFunction("other")->empty());
  166. EXPECT_FALSE(verifyModule(*M, &dbgs()));
  167. }
  168. } // end namespace