BitReaderTest.cpp revision 972cc0d54ac9ddbaa4d22a4f36db5269b3b4bbde
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/Analysis/Verifier.h"
11#include "llvm/Bitcode/BitstreamWriter.h"
12#include "llvm/Bitcode/ReaderWriter.h"
13#include "llvm/Constants.h"
14#include "llvm/Instructions.h"
15#include "llvm/LLVMContext.h"
16#include "llvm/Module.h"
17#include "llvm/PassManager.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "gtest/gtest.h"
20
21namespace llvm {
22namespace {
23
24static Module *makeLLVMModule() {
25  Module* Mod = new Module("test-mem", getGlobalContext());
26
27  FunctionType* FuncTy =
28    FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
29  Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
30                                    "func", Mod);
31
32  BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
33  new UnreachableInst(Mod->getContext(), Entry);
34
35  BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
36  new UnreachableInst(Mod->getContext(), BB);
37
38  PointerType* Int8Ptr = Type::getInt8PtrTy(Mod->getContext());
39  new GlobalVariable(*Mod, Int8Ptr, /*isConstant=*/true,
40                     GlobalValue::ExternalLinkage,
41                     BlockAddress::get(BB), "table");
42
43  return Mod;
44}
45
46static void writeModuleToBuffer(std::vector<unsigned char> &Buffer) {
47  Module *Mod = makeLLVMModule();
48  BitstreamWriter Stream(Buffer);
49  WriteBitcodeToStream(Mod, Stream);
50}
51
52TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
53  std::vector<unsigned char> Mem;
54  writeModuleToBuffer(Mem);
55  StringRef Data((const char*)&Mem[0], Mem.size());
56  MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Data, "test", false);
57  std::string errMsg;
58  Module *m = getLazyBitcodeModule(Buffer, getGlobalContext(), &errMsg);
59  PassManager passes;
60  passes.add(createVerifierPass());
61  passes.run(*m);
62}
63
64}
65}
66