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

Side by Side Diff: lib/Transforms/NaCl/AllocateDataSegment.cpp

Issue 29743003: Add passes for applying SFI sandboxing at the LLVM IR level Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Retry upload Created 6 years, 6 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
« no previous file with comments | « lib/Transforms/NaCl/AddSFI.cpp ('k') | lib/Transforms/NaCl/CMakeLists.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //===- AllocateDataSegment.cpp - Create template for data segment----------===//
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 // XXX
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 // #include "llvm/IR/Function.h"
17 // #include "llvm/IR/Instructions.h"
18 // #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 // #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Transforms/NaCl.h"
23
24 using namespace llvm;
25
26 namespace {
27 // This is a ModulePass because it modifies global variables.
28 class AllocateDataSegment : public ModulePass {
29 public:
30 static char ID; // Pass identification, replacement for typeid
31 AllocateDataSegment() : ModulePass(ID) {
32 initializeAllocateDataSegmentPass(*PassRegistry::getPassRegistry());
33 }
34
35 virtual bool runOnModule(Module &M);
36 };
37 }
38
39 char AllocateDataSegment::ID = 0;
40 INITIALIZE_PASS(AllocateDataSegment, "allocate-data-segment",
41 "Create template for data segment",
42 false, false)
43
44 bool AllocateDataSegment::runOnModule(Module &M) {
45 Type *I32 = Type::getInt32Ty(M.getContext());
46 Type *IntPtrType = I32; // XXX
47 unsigned BaseAddr = 0x10000;
48
49 SmallVector<Type *, 20> Types;
50 for (Module::global_iterator GV = M.global_begin(), E = M.global_end();
51 GV != E;
52 ++GV) {
53 if (!GV->hasInitializer()) {
54 report_fatal_error(std::string("Variable ") + GV->getName() +
55 " has no initializer");
56 }
57 Types.push_back(GV->getInitializer()->getType());
58 }
59 StructType *TemplateType = StructType::create(M.getContext(), "data_template") ;
60 // TODO: add padding manually and set isPacked=true.
61 TemplateType->setBody(Types);
62
63 // In order to handle global variable initializers that refer to other
64 // global variables, this pass cannot delete any global variables yet.
65 Constant *BasePtr = ConstantExpr::getIntToPtr(
66 ConstantInt::get(IntPtrType, BaseAddr),
67 TemplateType->getPointerTo());
68 unsigned Index = 0;
69 for (Module::global_iterator GV = M.global_begin(), E = M.global_end();
70 GV != E;
71 ++GV) {
72 Constant *Indexes[] = {
73 ConstantInt::get(I32, 0),
74 ConstantInt::get(I32, Index)
75 };
76 Value *Ptr = ConstantExpr::getGetElementPtr(BasePtr, Indexes);
77 GV->replaceAllUsesWith(Ptr);
78 Index++;
79 }
80
81 SmallVector<Constant *, 20> Fields;
82 for (Module::global_iterator Iter = M.global_begin(), E = M.global_end();
83 Iter != E; ) {
84 GlobalVariable *GV = Iter++;
85 Fields.push_back(GV->getInitializer());
86 GV->eraseFromParent();
87 }
88 Constant *Template = ConstantStruct::get(TemplateType, Fields);
89 new GlobalVariable(M, TemplateType, /*isConstant=*/true,
90 GlobalVariable::ExternalLinkage, Template,
91 "__sfi_data_segment");
92
93 DataLayout DL(&M);
94 Constant *TemplateSize =
95 ConstantInt::get(IntPtrType, DL.getTypeAllocSize(TemplateType));
96 new GlobalVariable(M, TemplateSize->getType(), /*isConstant=*/true,
97 GlobalVariable::ExternalLinkage, TemplateSize,
98 "__sfi_data_segment_size");
99
100 return true;
101 }
102
103 ModulePass *llvm::createAllocateDataSegmentPass() {
104 return new AllocateDataSegment();
105 }
OLDNEW
« no previous file with comments | « lib/Transforms/NaCl/AddSFI.cpp ('k') | lib/Transforms/NaCl/CMakeLists.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698