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