Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: unittests/Bitcode/NaClBitReaderTest.cpp

Issue 940243003: PNaCl localmod mods in LLVM to 223109 (local files only) (Closed)
Patch Set: xx Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- llvm/unittest/Bitcode/NaClBitReaderTest.cpp - Tests for BitReader --===// 1 //===- llvm/unittest/Bitcode/NaClBitReaderTest.cpp - Tests for BitReader --===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "llvm/ADT/SmallString.h" 10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Bitcode/BitstreamWriter.h" 11 #include "llvm/Bitcode/BitstreamWriter.h"
12 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" 12 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
13 #include "llvm/IR/Constants.h" 13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/Instructions.h" 14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h" 15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h" 16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/Verifier.h" 17 #include "llvm/IR/Verifier.h"
18 #include "llvm/PassManager.h" 18 #include "llvm/PassManager.h"
19 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/MemoryBuffer.h" 20 #include "llvm/Support/MemoryBuffer.h"
20 #include "gtest/gtest.h" 21 #include "gtest/gtest.h"
21 22
22 namespace llvm { 23 namespace llvm {
23 namespace { 24 namespace {
24 25
25 static Module *makeLLVMModule() { 26 static std::unique_ptr<Module> makeLLVMModule() {
26 Module* Mod = new Module("test-mem", getGlobalContext()); 27 std::unique_ptr<Module> Mod(new Module("test-mem", getGlobalContext()));
27 28
28 FunctionType* FuncTy = 29 FunctionType* FuncTy =
29 FunctionType::get(Type::getVoidTy(Mod->getContext()), false); 30 FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
30 Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage, 31 Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
31 "func", Mod); 32 "func", Mod.get());
32 33
33 BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func); 34 BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
34 new UnreachableInst(Mod->getContext(), Entry); 35 new UnreachableInst(Mod->getContext(), Entry);
35 36
36 BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func); 37 BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
37 new UnreachableInst(Mod->getContext(), BB); 38 new UnreachableInst(Mod->getContext(), BB);
38 39
39 return Mod; 40 return Mod;
40 } 41 }
41 42
42 static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) { 43 static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) {
43 std::unique_ptr<Module> Mod(makeLLVMModule()); 44 std::unique_ptr<Module> Mod = makeLLVMModule();
44 raw_svector_ostream OS(Buffer); 45 raw_svector_ostream OS(Buffer);
45 NaClWriteBitcodeToFile(Mod.get(), OS); 46 NaClWriteBitcodeToFile(Mod.get(), OS);
46 } 47 }
47 48
48 // Check that we can parse a good bitcode file. 49 // Check that we can parse a good bitcode file.
49 TEST(NaClBitReaderTest, MaterializeSimpleModule) { 50 TEST(NaClBitReaderTest, MaterializeSimpleModule) {
50 SmallString<1024> Mem; 51 SmallString<1024> Mem;
51 writeModuleToBuffer(Mem); 52 writeModuleToBuffer(Mem);
52 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); 53 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Mem.str(), " test", false);
53 ErrorOr<Module *> ModuleOrErr = 54 ErrorOr<Module *> ModuleOrErr =
54 getNaClLazyBitcodeModule(Buffer, getGlobalContext()); 55 getNaClLazyBitcodeModule(std::move(Buffer), getGlobalContext());
55 EXPECT_EQ(true, bool(ModuleOrErr)); 56 EXPECT_EQ(true, bool(ModuleOrErr));
56 // Do something with the module just to make sure it was built. 57 // Do something with the module just to make sure it was built.
57 std::unique_ptr<Module> m(ModuleOrErr.get()); 58 std::unique_ptr<Module> M(ModuleOrErr.get());
58 EXPECT_NE((Module *)nullptr, m.get()); 59 EXPECT_NE((Module *)nullptr, M.get());
59 PassManager passes; 60 M->getFunction("func")->materialize();
60 passes.add(createVerifierPass()); 61 EXPECT_FALSE(verifyModule(*M, &dbgs()));
61 passes.run(*m);
62 } 62 }
63 63
64 // Test that we catch bad stuff at the end of a bitcode file. 64 // Test that we catch bad stuff at the end of a bitcode file.
65 TEST(NaClBitReaderTest, BadDataAfterModule) { 65 TEST(NaClBitReaderTest, BadDataAfterModule) {
66 SmallString<1024> Mem; 66 SmallString<1024> Mem;
67 writeModuleToBuffer(Mem); 67 writeModuleToBuffer(Mem);
68 Mem.append("more"); // Length must be divisible by 4! 68 Mem.append("more"); // Length must be divisible by 4!
69 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); 69 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Mem.str(), " test", false);
70 ErrorOr<Module *> ModuleOrErr = 70 ErrorOr<Module *> ModuleOrErr =
71 getNaClLazyBitcodeModule(Buffer, getGlobalContext()); 71 getNaClLazyBitcodeModule(std::move(Buffer), getGlobalContext());
72 EXPECT_EQ(false, bool(ModuleOrErr)); 72 EXPECT_EQ(false, bool(ModuleOrErr));
73 std::string BadMessage("Invalid data after module"); 73 std::string BadMessage("Invalid data after module");
74 EXPECT_EQ(BadMessage, ModuleOrErr.getError().message()); 74 EXPECT_EQ(BadMessage, ModuleOrErr.getError().message());
75 } 75 }
76 76
77 } 77 }
78 } 78 }
OLDNEW
« tools/pnacl-llc/pnacl-llc.cpp ('K') | « tools/pnacl-thaw/pnacl-thaw.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698