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