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