1 //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/DataStream.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/StreamingMemoryObject.h"
26 #include "gtest/gtest.h"
32 std::unique_ptr<Module> parseAssembly(const char *Assembly) {
34 std::unique_ptr<Module> M =
35 parseAssemblyString(Assembly, Error, getGlobalContext());
38 raw_string_ostream OS(ErrMsg);
41 // A failure here means that the test itself is buggy.
43 report_fatal_error(OS.str().c_str());
48 static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
49 SmallVectorImpl<char> &Buffer) {
50 raw_svector_ostream OS(Buffer);
51 WriteBitcodeToFile(Mod.get(), OS);
54 static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
55 SmallString<1024> &Mem,
56 const char *Assembly) {
57 writeModuleToBuffer(parseAssembly(Assembly), Mem);
58 std::unique_ptr<MemoryBuffer> Buffer =
59 MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
60 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
61 getLazyBitcodeModule(std::move(Buffer), Context);
62 return std::move(ModuleOrErr.get());
65 class BufferDataStreamer : public DataStreamer {
66 std::unique_ptr<MemoryBuffer> Buffer;
68 size_t GetBytes(unsigned char *Out, size_t Len) override {
69 StringRef Buf = Buffer->getBuffer();
70 size_t Left = Buf.size() - Pos;
71 Len = std::min(Left, Len);
72 memcpy(Out, Buffer->getBuffer().substr(Pos).data(), Len);
78 BufferDataStreamer(std::unique_ptr<MemoryBuffer> Buffer)
79 : Buffer(std::move(Buffer)) {}
82 static std::unique_ptr<Module>
83 getStreamedModuleFromAssembly(LLVMContext &Context, SmallString<1024> &Mem,
84 const char *Assembly) {
85 writeModuleToBuffer(parseAssembly(Assembly), Mem);
86 std::unique_ptr<MemoryBuffer> Buffer =
87 MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
88 auto Streamer = llvm::make_unique<BufferDataStreamer>(std::move(Buffer));
89 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
90 getStreamedBitcodeModule("test", std::move(Streamer), Context);
91 return std::move(ModuleOrErr.get());
94 // Checks if we correctly detect eof if we try to read N bits when there are not
95 // enough bits left on the input stream to read N bits, and we are using a data
96 // streamer. In particular, it checks if we properly set the object size when
97 // the eof is reached under such conditions.
98 TEST(BitReaderTest, TestForEofAfterReadFailureOnDataStreamer) {
99 // Note: Because StreamingMemoryObject does a call to method GetBytes in it's
100 // constructor, using internal constant kChunkSize, we must fill the input
101 // with more characters than that amount.
102 static size_t InputSize = StreamingMemoryObject::kChunkSize + 5;
103 char *Text = new char[InputSize];
104 std::memset(Text, 'a', InputSize);
105 Text[InputSize - 1] = '\0';
106 StringRef Input(Text);
108 // Build bitsteam reader using data streamer.
109 auto MemoryBuf = MemoryBuffer::getMemBuffer(Input);
110 std::unique_ptr<DataStreamer> Streamer(
111 new BufferDataStreamer(std::move(MemoryBuf)));
113 llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
114 auto Reader = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
115 BitstreamCursor Cursor;
116 Cursor.init(Reader.get());
118 // Jump to two bytes before end of stream.
119 Cursor.JumpToBit((InputSize - 4) * CHAR_BIT);
120 // Try to read 4 bytes when only 2 are present, resulting in error value 0.
121 const size_t ReadErrorValue = 0;
122 EXPECT_EQ(ReadErrorValue, Cursor.Read(32));
123 // Should be at eof now.
124 EXPECT_TRUE(Cursor.AtEndOfStream());
129 TEST(BitReaderTest, MateralizeForwardRefWithStream) {
130 SmallString<1024> Mem;
133 std::unique_ptr<Module> M = getStreamedModuleFromAssembly(
134 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
135 "define void @func() {\n"
140 EXPECT_FALSE(M->getFunction("func")->empty());
143 TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {
144 SmallString<1024> Mem;
147 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
148 Context, Mem, "define internal i32 @func() {\n"
152 EXPECT_FALSE(verifyModule(*M, &dbgs()));
154 M->getFunction("func")->materialize();
155 EXPECT_FALSE(M->getFunction("func")->empty());
156 EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
157 GlobalValue::InternalLinkage);
159 // Check that the linkage type is preserved after dematerialization.
160 M->getFunction("func")->dematerialize();
161 EXPECT_TRUE(M->getFunction("func")->empty());
162 EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
163 GlobalValue::InternalLinkage);
164 EXPECT_FALSE(verifyModule(*M, &dbgs()));
167 // Tests that lazy evaluation can parse functions out of order.
168 TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
169 SmallString<1024> Mem;
171 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
172 Context, Mem, "define void @f() {\n"
175 "define void @g() {\n"
178 "define void @h() {\n"
181 "define void @j() {\n"
184 EXPECT_FALSE(verifyModule(*M, &dbgs()));
186 Function *F = M->getFunction("f");
187 Function *G = M->getFunction("g");
188 Function *H = M->getFunction("h");
189 Function *J = M->getFunction("j");
191 // Initially all functions are not materialized (no basic blocks).
192 EXPECT_TRUE(F->empty());
193 EXPECT_TRUE(G->empty());
194 EXPECT_TRUE(H->empty());
195 EXPECT_TRUE(J->empty());
196 EXPECT_FALSE(verifyModule(*M, &dbgs()));
200 EXPECT_TRUE(F->empty());
201 EXPECT_TRUE(G->empty());
202 EXPECT_FALSE(H->empty());
203 EXPECT_TRUE(J->empty());
204 EXPECT_FALSE(verifyModule(*M, &dbgs()));
208 EXPECT_TRUE(F->empty());
209 EXPECT_FALSE(G->empty());
210 EXPECT_FALSE(H->empty());
211 EXPECT_TRUE(J->empty());
212 EXPECT_FALSE(verifyModule(*M, &dbgs()));
216 EXPECT_TRUE(F->empty());
217 EXPECT_FALSE(G->empty());
218 EXPECT_FALSE(H->empty());
219 EXPECT_FALSE(J->empty());
220 EXPECT_FALSE(verifyModule(*M, &dbgs()));
224 EXPECT_FALSE(F->empty());
225 EXPECT_FALSE(G->empty());
226 EXPECT_FALSE(H->empty());
227 EXPECT_FALSE(J->empty());
228 EXPECT_FALSE(verifyModule(*M, &dbgs()));
231 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
232 SmallString<1024> Mem;
235 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
236 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
237 "define void @func() {\n"
242 EXPECT_FALSE(verifyModule(*M, &dbgs()));
244 // Try (and fail) to dematerialize @func.
245 M->getFunction("func")->dematerialize();
246 EXPECT_FALSE(M->getFunction("func")->empty());
249 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
250 SmallString<1024> Mem;
253 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
254 Context, Mem, "define i8* @before() {\n"
255 " ret i8* blockaddress(@func, %bb)\n"
257 "define void @other() {\n"
260 "define void @func() {\n"
265 EXPECT_TRUE(M->getFunction("before")->empty());
266 EXPECT_TRUE(M->getFunction("func")->empty());
267 EXPECT_FALSE(verifyModule(*M, &dbgs()));
269 // Materialize @before, pulling in @func.
270 EXPECT_FALSE(M->getFunction("before")->materialize());
271 EXPECT_FALSE(M->getFunction("func")->empty());
272 EXPECT_TRUE(M->getFunction("other")->empty());
273 EXPECT_FALSE(verifyModule(*M, &dbgs()));
275 // Try (and fail) to dematerialize @func.
276 M->getFunction("func")->dematerialize();
277 EXPECT_FALSE(M->getFunction("func")->empty());
278 EXPECT_FALSE(verifyModule(*M, &dbgs()));
281 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
282 SmallString<1024> Mem;
285 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
286 Context, Mem, "define void @func() {\n"
291 "define void @other() {\n"
294 "define i8* @after() {\n"
295 " ret i8* blockaddress(@func, %bb)\n"
297 EXPECT_TRUE(M->getFunction("after")->empty());
298 EXPECT_TRUE(M->getFunction("func")->empty());
299 EXPECT_FALSE(verifyModule(*M, &dbgs()));
301 // Materialize @after, pulling in @func.
302 EXPECT_FALSE(M->getFunction("after")->materialize());
303 EXPECT_FALSE(M->getFunction("func")->empty());
304 EXPECT_TRUE(M->getFunction("other")->empty());
305 EXPECT_FALSE(verifyModule(*M, &dbgs()));
307 // Try (and fail) to dematerialize @func.
308 M->getFunction("func")->dematerialize();
309 EXPECT_FALSE(M->getFunction("func")->empty());
310 EXPECT_FALSE(verifyModule(*M, &dbgs()));