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 constexpr size_t ReadErrorValue = 0; 122 EXPECT_EQ(ReadErrorValue, Cursor.Read(32)); 123 // Should be at eof now. 124 EXPECT_TRUE(Cursor.AtEndOfStream()); 125 } 126 127 TEST(BitReaderTest, MateralizeForwardRefWithStream) { 128 SmallString<1024> Mem; 129 130 LLVMContext Context; 131 std::unique_ptr<Module> M = getStreamedModuleFromAssembly( 132 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 133 "define void @func() {\n" 134 " unreachable\n" 135 "bb:\n" 136 " unreachable\n" 137 "}\n"); 138 EXPECT_FALSE(M->getFunction("func")->empty()); 139 } 140 141 TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) { 142 SmallString<1024> Mem; 143 144 LLVMContext Context; 145 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 146 Context, Mem, "define internal i32 @func() {\n" 147 "ret i32 0\n" 148 "}\n"); 149 150 EXPECT_FALSE(verifyModule(*M, &dbgs())); 151 152 M->getFunction("func")->materialize(); 153 EXPECT_FALSE(M->getFunction("func")->empty()); 154 EXPECT_TRUE(M->getFunction("func")->getLinkage() == 155 GlobalValue::InternalLinkage); 156 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())); 163 } 164 165 // Tests that lazy evaluation can parse functions out of order. 166 TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) { 167 SmallString<1024> Mem; 168 LLVMContext Context; 169 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 170 Context, Mem, "define void @f() {\n" 171 " unreachable\n" 172 "}\n" 173 "define void @g() {\n" 174 " unreachable\n" 175 "}\n" 176 "define void @h() {\n" 177 " unreachable\n" 178 "}\n" 179 "define void @j() {\n" 180 " unreachable\n" 181 "}\n"); 182 EXPECT_FALSE(verifyModule(*M, &dbgs())); 183 184 Function *F = M->getFunction("f"); 185 Function *G = M->getFunction("g"); 186 Function *H = M->getFunction("h"); 187 Function *J = M->getFunction("j"); 188 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())); 195 196 // Materialize h. 197 H->materialize(); 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())); 203 204 // Materialize g. 205 G->materialize(); 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())); 211 212 // Materialize j. 213 J->materialize(); 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())); 219 220 // Materialize f. 221 F->materialize(); 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())); 227 } 228 229 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 230 SmallString<1024> Mem; 231 232 LLVMContext Context; 233 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 234 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 235 "define void @func() {\n" 236 " unreachable\n" 237 "bb:\n" 238 " unreachable\n" 239 "}\n"); 240 EXPECT_FALSE(verifyModule(*M, &dbgs())); 241 242 // Try (and fail) to dematerialize @func. 243 M->getFunction("func")->dematerialize(); 244 EXPECT_FALSE(M->getFunction("func")->empty()); 245 } 246 247 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) { 248 SmallString<1024> Mem; 249 250 LLVMContext Context; 251 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 252 Context, Mem, "define i8* @before() {\n" 253 " ret i8* blockaddress(@func, %bb)\n" 254 "}\n" 255 "define void @other() {\n" 256 " unreachable\n" 257 "}\n" 258 "define void @func() {\n" 259 " unreachable\n" 260 "bb:\n" 261 " unreachable\n" 262 "}\n"); 263 EXPECT_TRUE(M->getFunction("before")->empty()); 264 EXPECT_TRUE(M->getFunction("func")->empty()); 265 EXPECT_FALSE(verifyModule(*M, &dbgs())); 266 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())); 272 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())); 277 } 278 279 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) { 280 SmallString<1024> Mem; 281 282 LLVMContext Context; 283 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 284 Context, Mem, "define void @func() {\n" 285 " unreachable\n" 286 "bb:\n" 287 " unreachable\n" 288 "}\n" 289 "define void @other() {\n" 290 " unreachable\n" 291 "}\n" 292 "define i8* @after() {\n" 293 " ret i8* blockaddress(@func, %bb)\n" 294 "}\n"); 295 EXPECT_TRUE(M->getFunction("after")->empty()); 296 EXPECT_TRUE(M->getFunction("func")->empty()); 297 EXPECT_FALSE(verifyModule(*M, &dbgs())); 298 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())); 304 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())); 309 } 310 311 } // end namespace 312