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/SmallString.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/Bitcode/BitstreamReader.h"
14 #include "llvm/Bitcode/BitstreamWriter.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "gtest/gtest.h"
25 
26 using namespace llvm;
27 
28 namespace {
29 
30 std::unique_ptr<Module> parseAssembly(LLVMContext &Context,
31                                       const char *Assembly) {
32   SMDiagnostic Error;
33   std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
34 
35   std::string ErrMsg;
36   raw_string_ostream OS(ErrMsg);
37   Error.print("", OS);
38 
39   // A failure here means that the test itself is buggy.
40   if (!M)
41     report_fatal_error(OS.str().c_str());
42 
43   return M;
44 }
45 
46 static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
47                                 SmallVectorImpl<char> &Buffer) {
48   raw_svector_ostream OS(Buffer);
49   WriteBitcodeToFile(Mod.get(), OS);
50 }
51 
52 static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
53                                                          SmallString<1024> &Mem,
54                                                          const char *Assembly) {
55   writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
56   ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
57       getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context);
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   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   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   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   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