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