comparison lib/CodeGen/ShadowStackGCLowering.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children c2174574ed3a
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
1 //===-- ShadowStackGCLowering.cpp - Custom lowering for shadow-stack gc ---===// 1 //===- ShadowStackGCLowering.cpp - Custom lowering for shadow-stack gc ----===//
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.
14 // "Accurate Garbage Collection in an Uncooperative Environment" 14 // "Accurate Garbage Collection in an Uncooperative Environment"
15 // Fergus Henderson, ISMM, 2002 15 // Fergus Henderson, ISMM, 2002
16 // 16 //
17 //===----------------------------------------------------------------------===// 17 //===----------------------------------------------------------------------===//
18 18
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/CodeGen/Passes.h" 21 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/CodeGen/GCStrategy.h" 23 #include "llvm/IR/Constant.h"
22 #include "llvm/IR/CallSite.h" 24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/IRBuilder.h" 29 #include "llvm/IR/IRBuilder.h"
30 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h" 31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/Intrinsics.h"
25 #include "llvm/IR/Module.h" 33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/IR/Value.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/Casting.h"
26 #include "llvm/Transforms/Utils/EscapeEnumerator.h" 38 #include "llvm/Transforms/Utils/EscapeEnumerator.h"
39 #include <cassert>
40 #include <cstddef>
41 #include <string>
42 #include <utility>
43 #include <vector>
27 44
28 using namespace llvm; 45 using namespace llvm;
29 46
30 #define DEBUG_TYPE "shadowstackgclowering" 47 #define DEBUG_TYPE "shadow-stack-gc-lowering"
31 48
32 namespace { 49 namespace {
33 50
34 class ShadowStackGCLowering : public FunctionPass { 51 class ShadowStackGCLowering : public FunctionPass {
35 /// RootChain - This is the global linked-list that contains the chain of GC 52 /// RootChain - This is the global linked-list that contains the chain of GC
36 /// roots. 53 /// roots.
37 GlobalVariable *Head; 54 GlobalVariable *Head = nullptr;
38 55
39 /// StackEntryTy - Abstract type of a link in the shadow stack. 56 /// StackEntryTy - Abstract type of a link in the shadow stack.
40 /// 57 StructType *StackEntryTy = nullptr;
41 StructType *StackEntryTy; 58 StructType *FrameMapTy = nullptr;
42 StructType *FrameMapTy;
43 59
44 /// Roots - GC roots in the current function. Each is a pair of the 60 /// Roots - GC roots in the current function. Each is a pair of the
45 /// intrinsic call and its corresponding alloca. 61 /// intrinsic call and its corresponding alloca.
46 std::vector<std::pair<CallInst *, AllocaInst *>> Roots; 62 std::vector<std::pair<CallInst *, AllocaInst *>> Roots;
47 63
48 public: 64 public:
49 static char ID; 65 static char ID;
66
50 ShadowStackGCLowering(); 67 ShadowStackGCLowering();
51 68
52 bool doInitialization(Module &M) override; 69 bool doInitialization(Module &M) override;
53 bool runOnFunction(Function &F) override; 70 bool runOnFunction(Function &F) override;
54 71
55 private: 72 private:
56 bool IsNullValue(Value *V); 73 bool IsNullValue(Value *V);
57 Constant *GetFrameMap(Function &F); 74 Constant *GetFrameMap(Function &F);
58 Type *GetConcreteStackEntryType(Function &F); 75 Type *GetConcreteStackEntryType(Function &F);
59 void CollectRoots(Function &F); 76 void CollectRoots(Function &F);
77
60 static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B, 78 static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
61 Type *Ty, Value *BasePtr, int Idx1, 79 Type *Ty, Value *BasePtr, int Idx1,
62 const char *Name); 80 const char *Name);
63 static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B, 81 static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
64 Type *Ty, Value *BasePtr, int Idx1, int Idx2, 82 Type *Ty, Value *BasePtr, int Idx1, int Idx2,
65 const char *Name); 83 const char *Name);
66 }; 84 };
67 } 85
68 86 } // end anonymous namespace
69 INITIALIZE_PASS_BEGIN(ShadowStackGCLowering, "shadow-stack-gc-lowering", 87
88 char ShadowStackGCLowering::ID = 0;
89
90 INITIALIZE_PASS_BEGIN(ShadowStackGCLowering, DEBUG_TYPE,
70 "Shadow Stack GC Lowering", false, false) 91 "Shadow Stack GC Lowering", false, false)
71 INITIALIZE_PASS_DEPENDENCY(GCModuleInfo) 92 INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
72 INITIALIZE_PASS_END(ShadowStackGCLowering, "shadow-stack-gc-lowering", 93 INITIALIZE_PASS_END(ShadowStackGCLowering, DEBUG_TYPE,
73 "Shadow Stack GC Lowering", false, false) 94 "Shadow Stack GC Lowering", false, false)
74 95
75 FunctionPass *llvm::createShadowStackGCLoweringPass() { return new ShadowStackGCLowering(); } 96 FunctionPass *llvm::createShadowStackGCLoweringPass() { return new ShadowStackGCLowering(); }
76 97
77 char ShadowStackGCLowering::ID = 0; 98 ShadowStackGCLowering::ShadowStackGCLowering() : FunctionPass(ID) {
78
79 ShadowStackGCLowering::ShadowStackGCLowering()
80 : FunctionPass(ID), Head(nullptr), StackEntryTy(nullptr),
81 FrameMapTy(nullptr) {
82 initializeShadowStackGCLoweringPass(*PassRegistry::getPassRegistry()); 99 initializeShadowStackGCLoweringPass(*PassRegistry::getPassRegistry());
83 } 100 }
84 101
85 Constant *ShadowStackGCLowering::GetFrameMap(Function &F) { 102 Constant *ShadowStackGCLowering::GetFrameMap(Function &F) {
86 // doInitialization creates the abstract type of this value. 103 // doInitialization creates the abstract type of this value.