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/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" 27 28 using namespace llvm; 29 30 namespace { 31 32 std::unique_ptr<Module> parseAssembly(const char *Assembly) { 33 SMDiagnostic Error; 34 std::unique_ptr<Module> M = 35 parseAssemblyString(Assembly, Error, getGlobalContext()); 36 37 std::string ErrMsg; 38 raw_string_ostream OS(ErrMsg); 39 Error.print("", OS); 40 41 // A failure here means that the test itself is buggy. 42 if (!M) 43 report_fatal_error(OS.str().c_str()); 44 45 return M; 46 } 47 48 static void writeModuleToBuffer(std::unique_ptr<Module> Mod, 49 SmallVectorImpl<char> &Buffer) { 50 raw_svector_ostream OS(Buffer); 51 WriteBitcodeToFile(Mod.get(), OS); 52 } 53 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()); 63 } 64 65 class BufferDataStreamer : public DataStreamer { 66 std::unique_ptr<MemoryBuffer> Buffer; 67 unsigned Pos = 0; 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); 73 Pos += Len; 74 return Len; 75 } 76 77 public: 78 BufferDataStreamer(std::unique_ptr<MemoryBuffer> Buffer) 79 : Buffer(std::move(Buffer)) {} 80 }; 81 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()); 92 } 93 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); 107 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))); 112 auto OwnedBytes = 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()); 117 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()); 125 126 delete[] Text; 127 } 128 129 TEST(BitReaderTest, MateralizeForwardRefWithStream) { 130 SmallString<1024> Mem; 131 132 LLVMContext Context; 133 std::unique_ptr<Module> M = getStreamedModuleFromAssembly( 134 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 135 "define void @func() {\n" 136 " unreachable\n" 137 "bb:\n" 138 " unreachable\n" 139 "}\n"); 140 EXPECT_FALSE(M->getFunction("func")->empty()); 141 } 142 143 TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) { 144 SmallString<1024> Mem; 145 146 LLVMContext Context; 147 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 148 Context, Mem, "define internal i32 @func() {\n" 149 "ret i32 0\n" 150 "}\n"); 151 152 EXPECT_FALSE(verifyModule(*M, &dbgs())); 153 154 M->getFunction("func")->materialize(); 155 EXPECT_FALSE(M->getFunction("func")->empty()); 156 EXPECT_TRUE(M->getFunction("func")->getLinkage() == 157 GlobalValue::InternalLinkage); 158 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())); 165 } 166 167 // Tests that lazy evaluation can parse functions out of order. 168 TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) { 169 SmallString<1024> Mem; 170 LLVMContext Context; 171 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 172 Context, Mem, "define void @f() {\n" 173 " unreachable\n" 174 "}\n" 175 "define void @g() {\n" 176 " unreachable\n" 177 "}\n" 178 "define void @h() {\n" 179 " unreachable\n" 180 "}\n" 181 "define void @j() {\n" 182 " unreachable\n" 183 "}\n"); 184 EXPECT_FALSE(verifyModule(*M, &dbgs())); 185 186 Function *F = M->getFunction("f"); 187 Function *G = M->getFunction("g"); 188 Function *H = M->getFunction("h"); 189 Function *J = M->getFunction("j"); 190 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())); 197 198 // Materialize h. 199 H->materialize(); 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())); 205 206 // Materialize g. 207 G->materialize(); 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())); 213 214 // Materialize j. 215 J->materialize(); 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())); 221 222 // Materialize f. 223 F->materialize(); 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())); 229 } 230 231 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 232 SmallString<1024> Mem; 233 234 LLVMContext Context; 235 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 236 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 237 "define void @func() {\n" 238 " unreachable\n" 239 "bb:\n" 240 " unreachable\n" 241 "}\n"); 242 EXPECT_FALSE(verifyModule(*M, &dbgs())); 243 244 // Try (and fail) to dematerialize @func. 245 M->getFunction("func")->dematerialize(); 246 EXPECT_FALSE(M->getFunction("func")->empty()); 247 } 248 249 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) { 250 SmallString<1024> Mem; 251 252 LLVMContext Context; 253 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 254 Context, Mem, "define i8* @before() {\n" 255 " ret i8* blockaddress(@func, %bb)\n" 256 "}\n" 257 "define void @other() {\n" 258 " unreachable\n" 259 "}\n" 260 "define void @func() {\n" 261 " unreachable\n" 262 "bb:\n" 263 " unreachable\n" 264 "}\n"); 265 EXPECT_TRUE(M->getFunction("before")->empty()); 266 EXPECT_TRUE(M->getFunction("func")->empty()); 267 EXPECT_FALSE(verifyModule(*M, &dbgs())); 268 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())); 274 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())); 279 } 280 281 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) { 282 SmallString<1024> Mem; 283 284 LLVMContext Context; 285 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 286 Context, Mem, "define void @func() {\n" 287 " unreachable\n" 288 "bb:\n" 289 " unreachable\n" 290 "}\n" 291 "define void @other() {\n" 292 " unreachable\n" 293 "}\n" 294 "define i8* @after() {\n" 295 " ret i8* blockaddress(@func, %bb)\n" 296 "}\n"); 297 EXPECT_TRUE(M->getFunction("after")->empty()); 298 EXPECT_TRUE(M->getFunction("func")->empty()); 299 EXPECT_FALSE(verifyModule(*M, &dbgs())); 300 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())); 306 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())); 311 } 312 313 } // end namespace 314