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

Side by Side Diff: lib/Transforms/NaCl/AddSFI.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/Bitcode/NaCl/Analysis/LLVMBuild.txt ('k') | lib/Transforms/NaCl/AllocateDataSegment.cpp » ('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 //===- AddSFI.cpp - Apply SFI sandboxing-----------------------------------===//
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/Function.h"
16 #include "llvm/IR/Instructions.h"
17 // #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20 // #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Transforms/NaCl.h"
22
23 using namespace llvm;
24
25 namespace {
26 // This is a ModulePass so that it can add global variables.
27 class ExpandAllocas : public ModulePass {
28 public:
29 static char ID; // Pass identification, replacement for typeid
30 ExpandAllocas() : ModulePass(ID) {
31 initializeExpandAllocasPass(*PassRegistry::getPassRegistry());
32 }
33
34 virtual bool runOnModule(Module &M);
35 };
36 }
37
38 char ExpandAllocas::ID = 0;
39 INITIALIZE_PASS(ExpandAllocas, "expand-allocas",
40 "Expand out alloca instructions",
41 false, false)
42
43 static void expandAllocas(Function *Func, Type *IntPtrType, Value *StackPtr) {
44 // Skip function declarations.
45 if (Func->empty())
46 return;
47
48 Type *I8Ptr = Type::getInt8PtrTy(Func->getContext());
49 Instruction *FrameTop = NULL;
50
51 BasicBlock *EntryBB = &Func->getEntryBlock();
52 unsigned FrameOffset = 0;
53 for (BasicBlock::iterator Iter = EntryBB->begin(), E = EntryBB->end();
54 Iter != E; ) {
55 Instruction *Inst = Iter++;
56 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Inst)) {
57 // XXX: error reporting
58 assert(Alloca->getType() == I8Ptr);
59 // XXX: error reporting
60 ConstantInt *CI = cast<ConstantInt>(Alloca->getArraySize());
61 // TODO: handle alignment
62 FrameOffset += CI->getZExtValue();
63
64 if (!FrameTop) {
65 FrameTop = new LoadInst(StackPtr, "frame_top");
66 EntryBB->getInstList().push_front(FrameTop);
67 }
68 Value *Var = BinaryOperator::Create(
69 BinaryOperator::Add, FrameTop,
70 ConstantInt::get(IntPtrType, -FrameOffset), "", Alloca);
71 Var = new IntToPtrInst(Var, Alloca->getType(), "", Alloca);
72 Var->takeName(Alloca);
73 Alloca->replaceAllUsesWith(Var);
74 Alloca->eraseFromParent();
75 }
76 }
77 if (FrameTop) {
78 // Adjust stack pointer.
79 // TODO: Could omit this in leaf functions.
80 Instruction *FrameBottom = BinaryOperator::Create(
81 BinaryOperator::Add, FrameTop,
82 ConstantInt::get(IntPtrType, -FrameOffset), "frame_bottom");
83 FrameBottom->insertAfter(FrameTop);
84 (new StoreInst(FrameBottom, StackPtr))->insertAfter(FrameBottom);
85
86 for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) {
87 for (BasicBlock::iterator Inst = BB->begin(), E = BB->end(); Inst != E;
88 ++Inst) {
89 if (isa<AllocaInst>(Inst)) {
90 report_fatal_error("TODO: handle dynamic alloca");
91 } else if (ReturnInst *Ret = dyn_cast<ReturnInst>(Inst)) {
92 // Restore stack pointer.
93 new StoreInst(FrameTop, StackPtr, Ret);
94 }
95 }
96 }
97 }
98 }
99
100 bool ExpandAllocas::runOnModule(Module &M) {
101 Type *IntPtrType = Type::getInt32Ty(M.getContext()); // XXX
102 uint64_t InitialStackPtr = 0x40000000;
103 Value *StackPtr = new GlobalVariable(
104 M, IntPtrType, /*isConstant=*/false, GlobalVariable::InternalLinkage,
105 ConstantInt::get(IntPtrType, InitialStackPtr),
106 "__sfi_stack");
107
108 for (Module::iterator Func = M.begin(), E = M.end(); Func != E; ++Func) {
109 expandAllocas(Func, IntPtrType, StackPtr);
110 }
111 return true;
112 }
113
114 ModulePass *llvm::createExpandAllocasPass() {
115 return new ExpandAllocas();
116 }
OLDNEW
« no previous file with comments | « lib/Bitcode/NaCl/Analysis/LLVMBuild.txt ('k') | lib/Transforms/NaCl/AllocateDataSegment.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698