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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/Transforms/NaCl/AddSFI.cpp ('k') | lib/Transforms/NaCl/CMakeLists.txt » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/Transforms/NaCl/AllocateDataSegment.cpp
diff --git a/lib/Transforms/NaCl/AllocateDataSegment.cpp b/lib/Transforms/NaCl/AllocateDataSegment.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..9afefbddcf15922375bc8c5d2315155a825f85ae
--- /dev/null
+++ b/lib/Transforms/NaCl/AllocateDataSegment.cpp
@@ -0,0 +1,105 @@
+//===- AllocateDataSegment.cpp - Create template for data segment----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// XXX
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/DataLayout.h"
+// #include "llvm/IR/Function.h"
+// #include "llvm/IR/Instructions.h"
+// #include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Pass.h"
+// #include "llvm/Support/raw_ostream.h"
+#include "llvm/Transforms/NaCl.h"
+
+using namespace llvm;
+
+namespace {
+ // This is a ModulePass because it modifies global variables.
+ class AllocateDataSegment : public ModulePass {
+ public:
+ static char ID; // Pass identification, replacement for typeid
+ AllocateDataSegment() : ModulePass(ID) {
+ initializeAllocateDataSegmentPass(*PassRegistry::getPassRegistry());
+ }
+
+ virtual bool runOnModule(Module &M);
+ };
+}
+
+char AllocateDataSegment::ID = 0;
+INITIALIZE_PASS(AllocateDataSegment, "allocate-data-segment",
+ "Create template for data segment",
+ false, false)
+
+bool AllocateDataSegment::runOnModule(Module &M) {
+ Type *I32 = Type::getInt32Ty(M.getContext());
+ Type *IntPtrType = I32; // XXX
+ unsigned BaseAddr = 0x10000;
+
+ SmallVector<Type *, 20> Types;
+ for (Module::global_iterator GV = M.global_begin(), E = M.global_end();
+ GV != E;
+ ++GV) {
+ if (!GV->hasInitializer()) {
+ report_fatal_error(std::string("Variable ") + GV->getName() +
+ " has no initializer");
+ }
+ Types.push_back(GV->getInitializer()->getType());
+ }
+ StructType *TemplateType = StructType::create(M.getContext(), "data_template");
+ // TODO: add padding manually and set isPacked=true.
+ TemplateType->setBody(Types);
+
+ // In order to handle global variable initializers that refer to other
+ // global variables, this pass cannot delete any global variables yet.
+ Constant *BasePtr = ConstantExpr::getIntToPtr(
+ ConstantInt::get(IntPtrType, BaseAddr),
+ TemplateType->getPointerTo());
+ unsigned Index = 0;
+ for (Module::global_iterator GV = M.global_begin(), E = M.global_end();
+ GV != E;
+ ++GV) {
+ Constant *Indexes[] = {
+ ConstantInt::get(I32, 0),
+ ConstantInt::get(I32, Index)
+ };
+ Value *Ptr = ConstantExpr::getGetElementPtr(BasePtr, Indexes);
+ GV->replaceAllUsesWith(Ptr);
+ Index++;
+ }
+
+ SmallVector<Constant *, 20> Fields;
+ for (Module::global_iterator Iter = M.global_begin(), E = M.global_end();
+ Iter != E; ) {
+ GlobalVariable *GV = Iter++;
+ Fields.push_back(GV->getInitializer());
+ GV->eraseFromParent();
+ }
+ Constant *Template = ConstantStruct::get(TemplateType, Fields);
+ new GlobalVariable(M, TemplateType, /*isConstant=*/true,
+ GlobalVariable::ExternalLinkage, Template,
+ "__sfi_data_segment");
+
+ DataLayout DL(&M);
+ Constant *TemplateSize =
+ ConstantInt::get(IntPtrType, DL.getTypeAllocSize(TemplateType));
+ new GlobalVariable(M, TemplateSize->getType(), /*isConstant=*/true,
+ GlobalVariable::ExternalLinkage, TemplateSize,
+ "__sfi_data_segment_size");
+
+ return true;
+}
+
+ModulePass *llvm::createAllocateDataSegmentPass() {
+ return new AllocateDataSegment();
+}
« 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