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