Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1 | //===-- ShadowStackGC.cpp - GC support for uncooperative targets ----------===// |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 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 | // This file implements lowering for the llvm.gc* intrinsics for targets that do |
| 11 | // not natively support them (which includes the C backend). Note that the code |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 12 | // generated is not quite as efficient as algorithms which generate stack maps |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 13 | // to identify roots. |
| 14 | // |
| 15 | // This pass implements the code transformation described in this paper: |
| 16 | // "Accurate Garbage Collection in an Uncooperative Environment" |
| 17 | // Fergus Henderson, ISMM, 2002 |
| 18 | // |
| 19 | // In runtime/GC/SemiSpace.cpp is a prototype runtime which is compatible with |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 20 | // ShadowStackGC. |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 21 | // |
| 22 | // In order to support this particular transformation, all stack roots are |
| 23 | // coallocated in the stack. This allows a fully target-independent stack map |
| 24 | // while introducing only minor runtime overhead. |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #define DEBUG_TYPE "shadowstackgc" |
Gordon Henriksen | 5a29c9e | 2008-08-17 12:56:54 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/GCs.h" |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Gordon Henriksen | 5a29c9e | 2008-08-17 12:56:54 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/GCStrategy.h" |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 32 | #include "llvm/IntrinsicInst.h" |
| 33 | #include "llvm/Module.h" |
Gabor Greif | 89c4cea | 2010-06-25 08:48:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CallSite.h" |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 35 | #include "llvm/Support/IRBuilder.h" |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace llvm; |
| 38 | |
| 39 | namespace { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 40 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 41 | class ShadowStackGC : public GCStrategy { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 42 | /// RootChain - This is the global linked-list that contains the chain of GC |
| 43 | /// roots. |
| 44 | GlobalVariable *Head; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 45 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 46 | /// StackEntryTy - Abstract type of a link in the shadow stack. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 47 | /// |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 48 | StructType *StackEntryTy; |
| 49 | StructType *FrameMapTy; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 50 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 51 | /// Roots - GC roots in the current function. Each is a pair of the |
| 52 | /// intrinsic call and its corresponding alloca. |
| 53 | std::vector<std::pair<CallInst*,AllocaInst*> > Roots; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 54 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 55 | public: |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 56 | ShadowStackGC(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 57 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 58 | bool initializeCustomLowering(Module &M); |
| 59 | bool performCustomLowering(Function &F); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 60 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 61 | private: |
| 62 | bool IsNullValue(Value *V); |
| 63 | Constant *GetFrameMap(Function &F); |
| 64 | const Type* GetConcreteStackEntryType(Function &F); |
| 65 | void CollectRoots(Function &F); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 66 | static GetElementPtrInst *CreateGEP(LLVMContext &Context, |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 67 | IRBuilder<> &B, Value *BasePtr, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 68 | int Idx1, const char *Name); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 69 | static GetElementPtrInst *CreateGEP(LLVMContext &Context, |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 70 | IRBuilder<> &B, Value *BasePtr, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 71 | int Idx1, int Idx2, const char *Name); |
| 72 | }; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 73 | |
| 74 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 75 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 76 | static GCRegistry::Add<ShadowStackGC> |
| 77 | X("shadow-stack", "Very portable GC for uncooperative code generators"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 78 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 79 | namespace { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 80 | /// EscapeEnumerator - This is a little algorithm to find all escape points |
| 81 | /// from a function so that "finally"-style code can be inserted. In addition |
| 82 | /// to finding the existing return and unwind instructions, it also (if |
| 83 | /// necessary) transforms any call instructions into invokes and sends them to |
| 84 | /// a landing pad. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 85 | /// |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 86 | /// It's wrapped up in a state machine using the same transform C# uses for |
| 87 | /// 'yield return' enumerators, This transform allows it to be non-allocating. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 88 | class EscapeEnumerator { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 89 | Function &F; |
| 90 | const char *CleanupBBName; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 91 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 92 | // State. |
| 93 | int State; |
| 94 | Function::iterator StateBB, StateE; |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 95 | IRBuilder<> Builder; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 96 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 97 | public: |
| 98 | EscapeEnumerator(Function &F, const char *N = "cleanup") |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 99 | : F(F), CleanupBBName(N), State(0), Builder(F.getContext()) {} |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 100 | |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 101 | IRBuilder<> *Next() { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 102 | switch (State) { |
| 103 | default: |
| 104 | return 0; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 105 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 106 | case 0: |
| 107 | StateBB = F.begin(); |
| 108 | StateE = F.end(); |
| 109 | State = 1; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 110 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 111 | case 1: |
| 112 | // Find all 'return' and 'unwind' instructions. |
| 113 | while (StateBB != StateE) { |
| 114 | BasicBlock *CurBB = StateBB++; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 115 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 116 | // Branches and invokes do not escape, only unwind and return do. |
| 117 | TerminatorInst *TI = CurBB->getTerminator(); |
| 118 | if (!isa<UnwindInst>(TI) && !isa<ReturnInst>(TI)) |
| 119 | continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 120 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 121 | Builder.SetInsertPoint(TI->getParent(), TI); |
| 122 | return &Builder; |
| 123 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 124 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 125 | State = 2; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 126 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 127 | // Find all 'call' instructions. |
| 128 | SmallVector<Instruction*,16> Calls; |
| 129 | for (Function::iterator BB = F.begin(), |
| 130 | E = F.end(); BB != E; ++BB) |
| 131 | for (BasicBlock::iterator II = BB->begin(), |
| 132 | EE = BB->end(); II != EE; ++II) |
| 133 | if (CallInst *CI = dyn_cast<CallInst>(II)) |
| 134 | if (!CI->getCalledFunction() || |
| 135 | !CI->getCalledFunction()->getIntrinsicID()) |
| 136 | Calls.push_back(CI); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 137 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 138 | if (Calls.empty()) |
| 139 | return 0; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 140 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 141 | // Create a cleanup block. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 142 | BasicBlock *CleanupBB = BasicBlock::Create(F.getContext(), |
| 143 | CleanupBBName, &F); |
| 144 | UnwindInst *UI = new UnwindInst(F.getContext(), CleanupBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 145 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 146 | // Transform the 'call' instructions into 'invoke's branching to the |
| 147 | // cleanup block. Go in reverse order to make prettier BB names. |
| 148 | SmallVector<Value*,16> Args; |
| 149 | for (unsigned I = Calls.size(); I != 0; ) { |
| 150 | CallInst *CI = cast<CallInst>(Calls[--I]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 151 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 152 | // Split the basic block containing the function call. |
| 153 | BasicBlock *CallBB = CI->getParent(); |
| 154 | BasicBlock *NewBB = |
| 155 | CallBB->splitBasicBlock(CI, CallBB->getName() + ".cont"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 156 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 157 | // Remove the unconditional branch inserted at the end of CallBB. |
| 158 | CallBB->getInstList().pop_back(); |
| 159 | NewBB->getInstList().remove(CI); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 160 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 161 | // Create a new invoke instruction. |
| 162 | Args.clear(); |
Gabor Greif | 89c4cea | 2010-06-25 08:48:19 +0000 | [diff] [blame] | 163 | CallSite CS(CI); |
| 164 | Args.append(CS.arg_begin(), CS.arg_end()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 165 | |
Gabor Greif | a9b2313 | 2010-04-20 13:13:04 +0000 | [diff] [blame] | 166 | InvokeInst *II = InvokeInst::Create(CI->getCalledValue(), |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 167 | NewBB, CleanupBB, |
| 168 | Args.begin(), Args.end(), |
| 169 | CI->getName(), CallBB); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 170 | II->setCallingConv(CI->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 171 | II->setAttributes(CI->getAttributes()); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 172 | CI->replaceAllUsesWith(II); |
| 173 | delete CI; |
| 174 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 175 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 176 | Builder.SetInsertPoint(UI->getParent(), UI); |
| 177 | return &Builder; |
| 178 | } |
| 179 | } |
| 180 | }; |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // ----------------------------------------------------------------------------- |
| 184 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 185 | void llvm::linkShadowStackGC() { } |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 186 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 187 | ShadowStackGC::ShadowStackGC() : Head(0), StackEntryTy(0) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 188 | InitRoots = true; |
| 189 | CustomRoots = true; |
| 190 | } |
| 191 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 192 | Constant *ShadowStackGC::GetFrameMap(Function &F) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 193 | // doInitialization creates the abstract type of this value. |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 194 | const Type *VoidPtr = Type::getInt8PtrTy(F.getContext()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 195 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 196 | // Truncate the ShadowStackDescriptor if some metadata is null. |
| 197 | unsigned NumMeta = 0; |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 198 | SmallVector<Constant*, 16> Metadata; |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 199 | for (unsigned I = 0; I != Roots.size(); ++I) { |
Gabor Greif | 89c4cea | 2010-06-25 08:48:19 +0000 | [diff] [blame] | 200 | Constant *C = cast<Constant>(Roots[I].first->getArgOperand(1)); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 201 | if (!C->isNullValue()) |
| 202 | NumMeta = I + 1; |
| 203 | Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr)); |
| 204 | } |
Jay Foad | 2670108 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 205 | Metadata.resize(NumMeta); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 206 | |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 207 | const Type *Int32Ty = Type::getInt32Ty(F.getContext()); |
| 208 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 209 | Constant *BaseElts[] = { |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 210 | ConstantInt::get(Int32Ty, Roots.size(), false), |
| 211 | ConstantInt::get(Int32Ty, NumMeta, false), |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 212 | }; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 213 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 214 | Constant *DescriptorElts[] = { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 215 | ConstantStruct::get(FrameMapTy, BaseElts), |
Jay Foad | 2670108 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 216 | ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), Metadata) |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 217 | }; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 219 | Type *EltTys[] = { DescriptorElts[0]->getType(),DescriptorElts[1]->getType()}; |
| 220 | StructType *STy = StructType::createNamed("gc_map."+utostr(NumMeta), EltTys); |
| 221 | |
| 222 | Constant *FrameMap = ConstantStruct::get(STy, DescriptorElts); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 223 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 224 | // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems |
| 225 | // that, short of multithreaded LLVM, it should be safe; all that is |
| 226 | // necessary is that a simple Module::iterator loop not be invalidated. |
| 227 | // Appending to the GlobalVariable list is safe in that sense. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 228 | // |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 229 | // All of the output passes emit globals last. The ExecutionEngine |
| 230 | // explicitly supports adding globals to the module after |
| 231 | // initialization. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 232 | // |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 233 | // Still, if it isn't deemed acceptable, then this transformation needs |
| 234 | // to be a ModulePass (which means it cannot be in the 'llc' pipeline |
| 235 | // (which uses a FunctionPassManager (which segfaults (not asserts) if |
| 236 | // provided a ModulePass))). |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 237 | Constant *GV = new GlobalVariable(*F.getParent(), FrameMap->getType(), true, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 238 | GlobalVariable::InternalLinkage, |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 239 | FrameMap, "__gc_" + F.getName()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 240 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 241 | Constant *GEPIndices[2] = { |
| 242 | ConstantInt::get(Type::getInt32Ty(F.getContext()), 0), |
| 243 | ConstantInt::get(Type::getInt32Ty(F.getContext()), 0) |
| 244 | }; |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 245 | return ConstantExpr::getGetElementPtr(GV, GEPIndices, 2); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 248 | const Type* ShadowStackGC::GetConcreteStackEntryType(Function &F) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 249 | // doInitialization creates the generic version of this type. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 250 | std::vector<Type*> EltTys; |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 251 | EltTys.push_back(StackEntryTy); |
| 252 | for (size_t I = 0; I != Roots.size(); I++) |
| 253 | EltTys.push_back(Roots[I].second->getAllocatedType()); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 254 | |
| 255 | return StructType::createNamed("gc_stackentry."+F.getName().str(), EltTys); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /// doInitialization - If this module uses the GC intrinsics, find them now. If |
| 259 | /// not, exit fast. |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 260 | bool ShadowStackGC::initializeCustomLowering(Module &M) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 261 | // struct FrameMap { |
| 262 | // int32_t NumRoots; // Number of roots in stack frame. |
| 263 | // int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots. |
| 264 | // void *Meta[]; // May be absent for roots without metadata. |
| 265 | // }; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 266 | std::vector<Type*> EltTys; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 267 | // 32 bits is ok up to a 32GB stack frame. :) |
| 268 | EltTys.push_back(Type::getInt32Ty(M.getContext())); |
| 269 | // Specifies length of variable length array. |
| 270 | EltTys.push_back(Type::getInt32Ty(M.getContext())); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 271 | FrameMapTy = StructType::createNamed("gc_map", EltTys); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 272 | PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 273 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 274 | // struct StackEntry { |
| 275 | // ShadowStackEntry *Next; // Caller's stack entry. |
| 276 | // FrameMap *Map; // Pointer to constant FrameMap. |
| 277 | // void *Roots[]; // Stack roots (in-place array, so we pretend). |
| 278 | // }; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 279 | |
| 280 | StackEntryTy = StructType::createNamed(M.getContext(), "gc_stackentry"); |
| 281 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 282 | EltTys.clear(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 283 | EltTys.push_back(PointerType::getUnqual(StackEntryTy)); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 284 | EltTys.push_back(FrameMapPtrTy); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 285 | StackEntryTy->setBody(EltTys); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 286 | const PointerType *StackEntryPtrTy = PointerType::getUnqual(StackEntryTy); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 287 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 288 | // Get the root chain if it already exists. |
| 289 | Head = M.getGlobalVariable("llvm_gc_root_chain"); |
| 290 | if (!Head) { |
| 291 | // If the root chain does not exist, insert a new one with linkonce |
| 292 | // linkage! |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 293 | Head = new GlobalVariable(M, StackEntryPtrTy, false, |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 294 | GlobalValue::LinkOnceAnyLinkage, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 295 | Constant::getNullValue(StackEntryPtrTy), |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 296 | "llvm_gc_root_chain"); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 297 | } else if (Head->hasExternalLinkage() && Head->isDeclaration()) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 298 | Head->setInitializer(Constant::getNullValue(StackEntryPtrTy)); |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 299 | Head->setLinkage(GlobalValue::LinkOnceAnyLinkage); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 300 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 301 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 302 | return true; |
| 303 | } |
| 304 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 305 | bool ShadowStackGC::IsNullValue(Value *V) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 306 | if (Constant *C = dyn_cast<Constant>(V)) |
| 307 | return C->isNullValue(); |
| 308 | return false; |
| 309 | } |
| 310 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 311 | void ShadowStackGC::CollectRoots(Function &F) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 312 | // FIXME: Account for original alignment. Could fragment the root array. |
| 313 | // Approach 1: Null initialize empty slots at runtime. Yuck. |
| 314 | // Approach 2: Emit a map of the array instead of just a count. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 315 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 316 | assert(Roots.empty() && "Not cleaned up?"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 317 | |
Gabor Greif | 89c4cea | 2010-06-25 08:48:19 +0000 | [diff] [blame] | 318 | SmallVector<std::pair<CallInst*, AllocaInst*>, 16> MetaRoots; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 319 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 320 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 321 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) |
| 322 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) |
| 323 | if (Function *F = CI->getCalledFunction()) |
| 324 | if (F->getIntrinsicID() == Intrinsic::gcroot) { |
Gabor Greif | 89c4cea | 2010-06-25 08:48:19 +0000 | [diff] [blame] | 325 | std::pair<CallInst*, AllocaInst*> Pair = std::make_pair( |
| 326 | CI, cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts())); |
| 327 | if (IsNullValue(CI->getArgOperand(1))) |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 328 | Roots.push_back(Pair); |
| 329 | else |
| 330 | MetaRoots.push_back(Pair); |
| 331 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 332 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 333 | // Number roots with metadata (usually empty) at the beginning, so that the |
| 334 | // FrameMap::Meta array can be elided. |
| 335 | Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end()); |
| 336 | } |
| 337 | |
| 338 | GetElementPtrInst * |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 339 | ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr, |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 340 | int Idx, int Idx2, const char *Name) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 341 | Value *Indices[] = { ConstantInt::get(Type::getInt32Ty(Context), 0), |
| 342 | ConstantInt::get(Type::getInt32Ty(Context), Idx), |
| 343 | ConstantInt::get(Type::getInt32Ty(Context), Idx2) }; |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 344 | Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 345 | |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 346 | assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 347 | |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 348 | return dyn_cast<GetElementPtrInst>(Val); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | GetElementPtrInst * |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 352 | ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr, |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 353 | int Idx, const char *Name) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 354 | Value *Indices[] = { ConstantInt::get(Type::getInt32Ty(Context), 0), |
| 355 | ConstantInt::get(Type::getInt32Ty(Context), Idx) }; |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 356 | Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 357 | |
Duncan Sands | 89f6d88 | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 358 | assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); |
| 359 | |
| 360 | return dyn_cast<GetElementPtrInst>(Val); |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /// runOnFunction - Insert code to maintain the shadow stack. |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 364 | bool ShadowStackGC::performCustomLowering(Function &F) { |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 365 | LLVMContext &Context = F.getContext(); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 366 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 367 | // Find calls to llvm.gcroot. |
| 368 | CollectRoots(F); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 369 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 370 | // If there are no roots in this function, then there is no need to add a |
| 371 | // stack map entry for it. |
| 372 | if (Roots.empty()) |
| 373 | return false; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 374 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 375 | // Build the constant map and figure the type of the shadow stack entry. |
| 376 | Value *FrameMap = GetFrameMap(F); |
| 377 | const Type *ConcreteStackEntryTy = GetConcreteStackEntryType(F); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 378 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 379 | // Build the shadow stack entry at the very start of the function. |
| 380 | BasicBlock::iterator IP = F.getEntryBlock().begin(); |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 381 | IRBuilder<> AtEntry(IP->getParent(), IP); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 382 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 383 | Instruction *StackEntry = AtEntry.CreateAlloca(ConcreteStackEntryTy, 0, |
| 384 | "gc_frame"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 385 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 386 | while (isa<AllocaInst>(IP)) ++IP; |
| 387 | AtEntry.SetInsertPoint(IP->getParent(), IP); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 388 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 389 | // Initialize the map pointer and load the current head of the shadow stack. |
| 390 | Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead"); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 391 | Instruction *EntryMapPtr = CreateGEP(Context, AtEntry, StackEntry, |
| 392 | 0,1,"gc_frame.map"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 393 | AtEntry.CreateStore(FrameMap, EntryMapPtr); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 394 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 395 | // After all the allocas... |
| 396 | for (unsigned I = 0, E = Roots.size(); I != E; ++I) { |
| 397 | // For each root, find the corresponding slot in the aggregate... |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 398 | Value *SlotPtr = CreateGEP(Context, AtEntry, StackEntry, 1 + I, "gc_root"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 399 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 400 | // And use it in lieu of the alloca. |
| 401 | AllocaInst *OriginalAlloca = Roots[I].second; |
| 402 | SlotPtr->takeName(OriginalAlloca); |
| 403 | OriginalAlloca->replaceAllUsesWith(SlotPtr); |
| 404 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 405 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 406 | // Move past the original stores inserted by GCStrategy::InitRoots. This isn't |
| 407 | // really necessary (the collector would never see the intermediate state at |
| 408 | // runtime), but it's nicer not to push the half-initialized entry onto the |
| 409 | // shadow stack. |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 410 | while (isa<StoreInst>(IP)) ++IP; |
| 411 | AtEntry.SetInsertPoint(IP->getParent(), IP); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 412 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 413 | // Push the entry onto the shadow stack. |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 414 | Instruction *EntryNextPtr = CreateGEP(Context, AtEntry, |
| 415 | StackEntry,0,0,"gc_frame.next"); |
| 416 | Instruction *NewHeadVal = CreateGEP(Context, AtEntry, |
| 417 | StackEntry, 0, "gc_newhead"); |
| 418 | AtEntry.CreateStore(CurrentHead, EntryNextPtr); |
| 419 | AtEntry.CreateStore(NewHeadVal, Head); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 420 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 421 | // For each instruction that escapes... |
| 422 | EscapeEnumerator EE(F, "gc_cleanup"); |
Eric Christopher | 7a61d70 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 423 | while (IRBuilder<> *AtExit = EE.Next()) { |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 424 | // Pop the entry from the shadow stack. Don't reuse CurrentHead from |
| 425 | // AtEntry, since that would make the value live for the entire function. |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 426 | Instruction *EntryNextPtr2 = CreateGEP(Context, *AtExit, StackEntry, 0, 0, |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 427 | "gc_frame.next"); |
| 428 | Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead"); |
| 429 | AtExit->CreateStore(SavedHead, Head); |
| 430 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 431 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 432 | // Delete the original allocas (which are no longer used) and the intrinsic |
| 433 | // calls (which are no longer valid). Doing this last avoids invalidating |
| 434 | // iterators. |
| 435 | for (unsigned I = 0, E = Roots.size(); I != E; ++I) { |
| 436 | Roots[I].first->eraseFromParent(); |
| 437 | Roots[I].second->eraseFromParent(); |
| 438 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 439 | |
Gordon Henriksen | 8fa8929 | 2008-01-07 01:30:53 +0000 | [diff] [blame] | 440 | Roots.clear(); |
| 441 | return true; |
| 442 | } |