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 constexpr size_t ReadErrorValue = 0;
122 EXPECT_EQ(ReadErrorValue, Cursor.Read(32));
123 // Should be at eof now.
124 EXPECT_TRUE(Cursor.AtEndOfStream());
127 TEST(BitReaderTest, MateralizeForwardRefWithStream) {
128 SmallString<1024> Mem;
131 std::unique_ptr<Module> M = getStreamedModuleFromAssembly(
132 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
133 "define void @func() {\n"
138 EXPECT_FALSE(M->getFunction("func")->empty());
141 TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {
142 SmallString<1024> Mem;
145 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
146 Context, Mem, "define internal i32 @func() {\n"
150 EXPECT_FALSE(verifyModule(*M, &dbgs()));
152 M->getFunction("func")->materialize();
153 EXPECT_FALSE(M->getFunction("func")->empty());
154 EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
155 GlobalValue::InternalLinkage);
157 // Check that the linkage type is preserved after dematerialization.
158 M->getFunction("func")->dematerialize();
159 EXPECT_TRUE(M->getFunction("func")->empty());
160 EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
161 GlobalValue::InternalLinkage);
162 EXPECT_FALSE(verifyModule(*M, &dbgs()));
165 // Tests that lazy evaluation can parse functions out of order.
166 TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
167 SmallString<1024> Mem;
169 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
170 Context, Mem, "define void @f() {\n"
173 "define void @g() {\n"
176 "define void @h() {\n"
179 "define void @j() {\n"
182 EXPECT_FALSE(verifyModule(*M, &dbgs()));
184 Function *F = M->getFunction("f");
185 Function *G = M->getFunction("g");
186 Function *H = M->getFunction("h");
187 Function *J = M->getFunction("j");
189 // Initially all functions are not materialized (no basic blocks).
190 EXPECT_TRUE(F->empty());
191 EXPECT_TRUE(G->empty());
192 EXPECT_TRUE(H->empty());
193 EXPECT_TRUE(J->empty());
194 EXPECT_FALSE(verifyModule(*M, &dbgs()));
198 EXPECT_TRUE(F->empty());
199 EXPECT_TRUE(G->empty());
200 EXPECT_FALSE(H->empty());
201 EXPECT_TRUE(J->empty());
202 EXPECT_FALSE(verifyModule(*M, &dbgs()));
206 EXPECT_TRUE(F->empty());
207 EXPECT_FALSE(G->empty());
208 EXPECT_FALSE(H->empty());
209 EXPECT_TRUE(J->empty());
210 EXPECT_FALSE(verifyModule(*M, &dbgs()));
214 EXPECT_TRUE(F->empty());
215 EXPECT_FALSE(G->empty());
216 EXPECT_FALSE(H->empty());
217 EXPECT_FALSE(J->empty());
218 EXPECT_FALSE(verifyModule(*M, &dbgs()));
222 EXPECT_FALSE(F->empty());
223 EXPECT_FALSE(G->empty());
224 EXPECT_FALSE(H->empty());
225 EXPECT_FALSE(J->empty());
226 EXPECT_FALSE(verifyModule(*M, &dbgs()));
229 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
230 SmallString<1024> Mem;
233 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
234 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
235 "define void @func() {\n"
240 EXPECT_FALSE(verifyModule(*M, &dbgs()));
242 // Try (and fail) to dematerialize @func.
243 M->getFunction("func")->dematerialize();
244 EXPECT_FALSE(M->getFunction("func")->empty());
247 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
248 SmallString<1024> Mem;
251 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
252 Context, Mem, "define i8* @before() {\n"
253 " ret i8* blockaddress(@func, %bb)\n"
255 "define void @other() {\n"
258 "define void @func() {\n"
263 EXPECT_TRUE(M->getFunction("before")->empty());
264 EXPECT_TRUE(M->getFunction("func")->empty());
265 EXPECT_FALSE(verifyModule(*M, &dbgs()));
267 // Materialize @before, pulling in @func.
268 EXPECT_FALSE(M->getFunction("before")->materialize());
269 EXPECT_FALSE(M->getFunction("func")->empty());
270 EXPECT_TRUE(M->getFunction("other")->empty());
271 EXPECT_FALSE(verifyModule(*M, &dbgs()));
273 // Try (and fail) to dematerialize @func.
274 M->getFunction("func")->dematerialize();
275 EXPECT_FALSE(M->getFunction("func")->empty());
276 EXPECT_FALSE(verifyModule(*M, &dbgs()));
279 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
280 SmallString<1024> Mem;
283 std::unique_ptr<Module> M = getLazyModuleFromAssembly(
284 Context, Mem, "define void @func() {\n"
289 "define void @other() {\n"
292 "define i8* @after() {\n"
293 " ret i8* blockaddress(@func, %bb)\n"
295 EXPECT_TRUE(M->getFunction("after")->empty());
296 EXPECT_TRUE(M->getFunction("func")->empty());
297 EXPECT_FALSE(verifyModule(*M, &dbgs()));
299 // Materialize @after, pulling in @func.
300 EXPECT_FALSE(M->getFunction("after")->materialize());
301 EXPECT_FALSE(M->getFunction("func")->empty());
302 EXPECT_TRUE(M->getFunction("other")->empty());
303 EXPECT_FALSE(verifyModule(*M, &dbgs()));
305 // Try (and fail) to dematerialize @func.
306 M->getFunction("func")->dematerialize();
307 EXPECT_FALSE(M->getFunction("func")->empty());
308 EXPECT_FALSE(verifyModule(*M, &dbgs()));