1 //===- ShadowStackGCLowering.cpp - Custom lowering for shadow-stack gc ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the custom lowering code required by the shadow-stack GC
10 // strategy.
11 //
12 // This pass implements the code transformation described in this paper:
13 //   "Accurate Garbage Collection in an Uncooperative Environment"
14 //   Fergus Henderson, ISMM, 2002
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Analysis/DomTreeUpdater.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constant.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/IR/GlobalVariable.h"
30 #include "llvm/IR/IRBuilder.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/Intrinsics.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/IR/Value.h"
37 #include "llvm/InitializePasses.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Transforms/Utils/EscapeEnumerator.h"
41 #include <cassert>
42 #include <string>
43 #include <utility>
44 #include <vector>
45 
46 using namespace llvm;
47 
48 #define DEBUG_TYPE "shadow-stack-gc-lowering"
49 
50 namespace {
51 
52 class ShadowStackGCLowering : public FunctionPass {
53   /// RootChain - This is the global linked-list that contains the chain of GC
54   /// roots.
55   GlobalVariable *Head = nullptr;
56 
57   /// StackEntryTy - Abstract type of a link in the shadow stack.
58   StructType *StackEntryTy = nullptr;
59   StructType *FrameMapTy = nullptr;
60 
61   /// Roots - GC roots in the current function. Each is a pair of the
62   /// intrinsic call and its corresponding alloca.
63   std::vector<std::pair<CallInst *, AllocaInst *>> Roots;
64 
65 public:
66   static char ID;
67 
68   ShadowStackGCLowering();
69 
70   bool doInitialization(Module &M) override;
71   void getAnalysisUsage(AnalysisUsage &AU) const override;
72   bool runOnFunction(Function &F) override;
73 
74 private:
75   bool IsNullValue(Value *V);
76   Constant *GetFrameMap(Function &F);
77   Type *GetConcreteStackEntryType(Function &F);
78   void CollectRoots(Function &F);
79 
80   static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
81                                       Type *Ty, Value *BasePtr, int Idx1,
82                                       const char *Name);
83   static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
84                                       Type *Ty, Value *BasePtr, int Idx1, int Idx2,
85                                       const char *Name);
86 };
87 
88 } // end anonymous namespace
89 
90 char ShadowStackGCLowering::ID = 0;
91 char &llvm::ShadowStackGCLoweringID = ShadowStackGCLowering::ID;
92 
93 INITIALIZE_PASS_BEGIN(ShadowStackGCLowering, DEBUG_TYPE,
94                       "Shadow Stack GC Lowering", false, false)
95 INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
96 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
97 INITIALIZE_PASS_END(ShadowStackGCLowering, DEBUG_TYPE,
98                     "Shadow Stack GC Lowering", false, false)
99 
100 FunctionPass *llvm::createShadowStackGCLoweringPass() { return new ShadowStackGCLowering(); }
101 
102 ShadowStackGCLowering::ShadowStackGCLowering() : FunctionPass(ID) {
103   initializeShadowStackGCLoweringPass(*PassRegistry::getPassRegistry());
104 }
105 
106 Constant *ShadowStackGCLowering::GetFrameMap(Function &F) {
107   // doInitialization creates the abstract type of this value.
108   Type *VoidPtr = Type::getInt8PtrTy(F.getContext());
109 
110   // Truncate the ShadowStackDescriptor if some metadata is null.
111   unsigned NumMeta = 0;
112   SmallVector<Constant *, 16> Metadata;
113   for (unsigned I = 0; I != Roots.size(); ++I) {
114     Constant *C = cast<Constant>(Roots[I].first->getArgOperand(1));
115     if (!C->isNullValue())
116       NumMeta = I + 1;
117     Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr));
118   }
119   Metadata.resize(NumMeta);
120 
121   Type *Int32Ty = Type::getInt32Ty(F.getContext());
122 
123   Constant *BaseElts[] = {
124       ConstantInt::get(Int32Ty, Roots.size(), false),
125       ConstantInt::get(Int32Ty, NumMeta, false),
126   };
127 
128   Constant *DescriptorElts[] = {
129       ConstantStruct::get(FrameMapTy, BaseElts),
130       ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), Metadata)};
131 
132   Type *EltTys[] = {DescriptorElts[0]->getType(), DescriptorElts[1]->getType()};
133   StructType *STy = StructType::create(EltTys, "gc_map." + utostr(NumMeta));
134 
135   Constant *FrameMap = ConstantStruct::get(STy, DescriptorElts);
136 
137   // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems
138   //        that, short of multithreaded LLVM, it should be safe; all that is
139   //        necessary is that a simple Module::iterator loop not be invalidated.
140   //        Appending to the GlobalVariable list is safe in that sense.
141   //
142   //        All of the output passes emit globals last. The ExecutionEngine
143   //        explicitly supports adding globals to the module after
144   //        initialization.
145   //
146   //        Still, if it isn't deemed acceptable, then this transformation needs
147   //        to be a ModulePass (which means it cannot be in the 'llc' pipeline
148   //        (which uses a FunctionPassManager (which segfaults (not asserts) if
149   //        provided a ModulePass))).
150   Constant *GV = new GlobalVariable(*F.getParent(), FrameMap->getType(), true,
151                                     GlobalVariable::InternalLinkage, FrameMap,
152                                     "__gc_" + F.getName());
153 
154   Constant *GEPIndices[2] = {
155       ConstantInt::get(Type::getInt32Ty(F.getContext()), 0),
156       ConstantInt::get(Type::getInt32Ty(F.getContext()), 0)};
157   return ConstantExpr::getGetElementPtr(FrameMap->getType(), GV, GEPIndices);
158 }
159 
160 Type *ShadowStackGCLowering::GetConcreteStackEntryType(Function &F) {
161   // doInitialization creates the generic version of this type.
162   std::vector<Type *> EltTys;
163   EltTys.push_back(StackEntryTy);
164   for (const std::pair<CallInst *, AllocaInst *> &Root : Roots)
165     EltTys.push_back(Root.second->getAllocatedType());
166 
167   return StructType::create(EltTys, ("gc_stackentry." + F.getName()).str());
168 }
169 
170 /// doInitialization - If this module uses the GC intrinsics, find them now. If
171 /// not, exit fast.
172 bool ShadowStackGCLowering::doInitialization(Module &M) {
173   bool Active = false;
174   for (Function &F : M) {
175     if (F.hasGC() && F.getGC() == std::string("shadow-stack")) {
176       Active = true;
177       break;
178     }
179   }
180   if (!Active)
181     return false;
182 
183   // struct FrameMap {
184   //   int32_t NumRoots; // Number of roots in stack frame.
185   //   int32_t NumMeta;  // Number of metadata descriptors. May be < NumRoots.
186   //   void *Meta[];     // May be absent for roots without metadata.
187   // };
188   std::vector<Type *> EltTys;
189   // 32 bits is ok up to a 32GB stack frame. :)
190   EltTys.push_back(Type::getInt32Ty(M.getContext()));
191   // Specifies length of variable length array.
192   EltTys.push_back(Type::getInt32Ty(M.getContext()));
193   FrameMapTy = StructType::create(EltTys, "gc_map");
194   PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy);
195 
196   // struct StackEntry {
197   //   ShadowStackEntry *Next; // Caller's stack entry.
198   //   FrameMap *Map;          // Pointer to constant FrameMap.
199   //   void *Roots[];          // Stack roots (in-place array, so we pretend).
200   // };
201 
202   StackEntryTy = StructType::create(M.getContext(), "gc_stackentry");
203 
204   EltTys.clear();
205   EltTys.push_back(PointerType::getUnqual(StackEntryTy));
206   EltTys.push_back(FrameMapPtrTy);
207   StackEntryTy->setBody(EltTys);
208   PointerType *StackEntryPtrTy = PointerType::getUnqual(StackEntryTy);
209 
210   // Get the root chain if it already exists.
211   Head = M.getGlobalVariable("llvm_gc_root_chain");
212   if (!Head) {
213     // If the root chain does not exist, insert a new one with linkonce
214     // linkage!
215     Head = new GlobalVariable(
216         M, StackEntryPtrTy, false, GlobalValue::LinkOnceAnyLinkage,
217         Constant::getNullValue(StackEntryPtrTy), "llvm_gc_root_chain");
218   } else if (Head->hasExternalLinkage() && Head->isDeclaration()) {
219     Head->setInitializer(Constant::getNullValue(StackEntryPtrTy));
220     Head->setLinkage(GlobalValue::LinkOnceAnyLinkage);
221   }
222 
223   return true;
224 }
225 
226 bool ShadowStackGCLowering::IsNullValue(Value *V) {
227   if (Constant *C = dyn_cast<Constant>(V))
228     return C->isNullValue();
229   return false;
230 }
231 
232 void ShadowStackGCLowering::CollectRoots(Function &F) {
233   // FIXME: Account for original alignment. Could fragment the root array.
234   //   Approach 1: Null initialize empty slots at runtime. Yuck.
235   //   Approach 2: Emit a map of the array instead of just a count.
236 
237   assert(Roots.empty() && "Not cleaned up?");
238 
239   SmallVector<std::pair<CallInst *, AllocaInst *>, 16> MetaRoots;
240 
241   for (BasicBlock &BB : F)
242     for (Instruction &I : BB)
243       if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(&I))
244         if (Function *F = CI->getCalledFunction())
245           if (F->getIntrinsicID() == Intrinsic::gcroot) {
246             std::pair<CallInst *, AllocaInst *> Pair = std::make_pair(
247                 CI,
248                 cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts()));
249             if (IsNullValue(CI->getArgOperand(1)))
250               Roots.push_back(Pair);
251             else
252               MetaRoots.push_back(Pair);
253           }
254 
255   // Number roots with metadata (usually empty) at the beginning, so that the
256   // FrameMap::Meta array can be elided.
257   Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end());
258 }
259 
260 GetElementPtrInst *ShadowStackGCLowering::CreateGEP(LLVMContext &Context,
261                                                     IRBuilder<> &B, Type *Ty,
262                                                     Value *BasePtr, int Idx,
263                                                     int Idx2,
264                                                     const char *Name) {
265   Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0),
266                       ConstantInt::get(Type::getInt32Ty(Context), Idx),
267                       ConstantInt::get(Type::getInt32Ty(Context), Idx2)};
268   Value *Val = B.CreateGEP(Ty, BasePtr, Indices, Name);
269 
270   assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant");
271 
272   return dyn_cast<GetElementPtrInst>(Val);
273 }
274 
275 GetElementPtrInst *ShadowStackGCLowering::CreateGEP(LLVMContext &Context,
276                                             IRBuilder<> &B, Type *Ty, Value *BasePtr,
277                                             int Idx, const char *Name) {
278   Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0),
279                       ConstantInt::get(Type::getInt32Ty(Context), Idx)};
280   Value *Val = B.CreateGEP(Ty, BasePtr, Indices, Name);
281 
282   assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant");
283 
284   return dyn_cast<GetElementPtrInst>(Val);
285 }
286 
287 void ShadowStackGCLowering::getAnalysisUsage(AnalysisUsage &AU) const {
288   AU.addPreserved<DominatorTreeWrapperPass>();
289 }
290 
291 /// runOnFunction - Insert code to maintain the shadow stack.
292 bool ShadowStackGCLowering::runOnFunction(Function &F) {
293   // Quick exit for functions that do not use the shadow stack GC.
294   if (!F.hasGC() ||
295       F.getGC() != std::string("shadow-stack"))
296     return false;
297 
298   LLVMContext &Context = F.getContext();
299 
300   // Find calls to llvm.gcroot.
301   CollectRoots(F);
302 
303   // If there are no roots in this function, then there is no need to add a
304   // stack map entry for it.
305   if (Roots.empty())
306     return false;
307 
308   Optional<DomTreeUpdater> DTU;
309   if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
310     DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
311 
312   // Build the constant map and figure the type of the shadow stack entry.
313   Value *FrameMap = GetFrameMap(F);
314   Type *ConcreteStackEntryTy = GetConcreteStackEntryType(F);
315 
316   // Build the shadow stack entry at the very start of the function.
317   BasicBlock::iterator IP = F.getEntryBlock().begin();
318   IRBuilder<> AtEntry(IP->getParent(), IP);
319 
320   Instruction *StackEntry =
321       AtEntry.CreateAlloca(ConcreteStackEntryTy, nullptr, "gc_frame");
322 
323   while (isa<AllocaInst>(IP))
324     ++IP;
325   AtEntry.SetInsertPoint(IP->getParent(), IP);
326 
327   // Initialize the map pointer and load the current head of the shadow stack.
328   Instruction *CurrentHead =
329       AtEntry.CreateLoad(StackEntryTy->getPointerTo(), Head, "gc_currhead");
330   Instruction *EntryMapPtr = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
331                                        StackEntry, 0, 1, "gc_frame.map");
332   AtEntry.CreateStore(FrameMap, EntryMapPtr);
333 
334   // After all the allocas...
335   for (unsigned I = 0, E = Roots.size(); I != E; ++I) {
336     // For each root, find the corresponding slot in the aggregate...
337     Value *SlotPtr = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
338                                StackEntry, 1 + I, "gc_root");
339 
340     // And use it in lieu of the alloca.
341     AllocaInst *OriginalAlloca = Roots[I].second;
342     SlotPtr->takeName(OriginalAlloca);
343     OriginalAlloca->replaceAllUsesWith(SlotPtr);
344   }
345 
346   // Move past the original stores inserted by GCStrategy::InitRoots. This isn't
347   // really necessary (the collector would never see the intermediate state at
348   // runtime), but it's nicer not to push the half-initialized entry onto the
349   // shadow stack.
350   while (isa<StoreInst>(IP))
351     ++IP;
352   AtEntry.SetInsertPoint(IP->getParent(), IP);
353 
354   // Push the entry onto the shadow stack.
355   Instruction *EntryNextPtr = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
356                                         StackEntry, 0, 0, "gc_frame.next");
357   Instruction *NewHeadVal = CreateGEP(Context, AtEntry, ConcreteStackEntryTy,
358                                       StackEntry, 0, "gc_newhead");
359   AtEntry.CreateStore(CurrentHead, EntryNextPtr);
360   AtEntry.CreateStore(NewHeadVal, Head);
361 
362   // For each instruction that escapes...
363   EscapeEnumerator EE(F, "gc_cleanup", /*HandleExceptions=*/true,
364                       DTU ? DTU.getPointer() : nullptr);
365   while (IRBuilder<> *AtExit = EE.Next()) {
366     // Pop the entry from the shadow stack. Don't reuse CurrentHead from
367     // AtEntry, since that would make the value live for the entire function.
368     Instruction *EntryNextPtr2 =
369         CreateGEP(Context, *AtExit, ConcreteStackEntryTy, StackEntry, 0, 0,
370                   "gc_frame.next");
371     Value *SavedHead = AtExit->CreateLoad(StackEntryTy->getPointerTo(),
372                                           EntryNextPtr2, "gc_savedhead");
373     AtExit->CreateStore(SavedHead, Head);
374   }
375 
376   // Delete the original allocas (which are no longer used) and the intrinsic
377   // calls (which are no longer valid). Doing this last avoids invalidating
378   // iterators.
379   for (std::pair<CallInst *, AllocaInst *> &Root : Roots) {
380     Root.first->eraseFromParent();
381     Root.second->eraseFromParent();
382   }
383 
384   Roots.clear();
385   return true;
386 }
387