llvm.org GIT mirror llvm / 04e1efb
[Transforms][ASan] Move findAllocaForValue() to Utils/Local.cpp. NFC Summary: Factor out findAllocaForValue() from ASan so that we can use it in MSan to handle lifetime intrinsics. Reviewers: eugenis, pcc Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D60615 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358380 91177308-0d34-0410-b5e6-96231b3b80d8 Alexander Potapenko 6 months ago
3 changed file(s) with 45 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
334334 void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
335335 DIBuilder &Builder, int Offset = 0);
336336
337 /// Finds alloca where the value comes from.
338 AllocaInst *findAllocaForValue(Value *V,
339 DenseMap &AllocaForValue);
340
337341 /// Assuming the instruction \p I is going to be deleted, attempt to salvage
338342 /// debug users of \p I by writing the effect of \p I in a DIExpression.
339343 /// Returns true if any debug users were updated.
10371037 !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
10381038 return;
10391039 // Find alloca instruction that corresponds to llvm.lifetime argument.
1040 AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
1040 AllocaInst *AI =
1041 llvm::findAllocaForValue(II.getArgOperand(1), AllocaForValue);
1042 // We're interested only in allocas we can handle.
10411043 if (!AI || !ASan.isInterestingAlloca(*AI))
10421044 return;
10431045 bool DoPoison = (ID == Intrinsic::lifetime_end);
10601062
10611063 // ---------------------- Helpers.
10621064 void initializeCallbacks(Module &M);
1063
1064 /// Finds alloca where the value comes from.
1065 AllocaInst *findAllocaForValue(Value *V);
10661065
10671066 // Copies bytes from ShadowBytes into shadow memory for indexes where
10681067 // ShadowMask is not zero. If ShadowMask[i] is zero, we assume that
32273226 // variable may go in and out of scope several times, e.g. in loops).
32283227 // (3) if we poisoned at least one %alloca in a function,
32293228 // unpoison the whole stack frame at function exit.
3230
3231 AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) {
3232 if (AllocaInst *AI = dyn_cast(V))
3233 // We're interested only in allocas we can handle.
3234 return ASan.isInterestingAlloca(*AI) ? AI : nullptr;
3235 // See if we've already calculated (or started to calculate) alloca for a
3236 // given value.
3237 AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
3238 if (I != AllocaForValue.end()) return I->second;
3239 // Store 0 while we're calculating alloca for value V to avoid
3240 // infinite recursion if the value references itself.
3241 AllocaForValue[V] = nullptr;
3242 AllocaInst *Res = nullptr;
3243 if (CastInst *CI = dyn_cast(V))
3244 Res = findAllocaForValue(CI->getOperand(0));
3245 else if (PHINode *PN = dyn_cast(V)) {
3246 for (Value *IncValue : PN->incoming_values()) {
3247 // Allow self-referencing phi-nodes.
3248 if (IncValue == PN) continue;
3249 AllocaInst *IncValueAI = findAllocaForValue(IncValue);
3250 // AI for incoming values should exist and should all be equal.
3251 if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res))
3252 return nullptr;
3253 Res = IncValueAI;
3254 }
3255 } else if (GetElementPtrInst *EP = dyn_cast(V)) {
3256 Res = findAllocaForValue(EP->getPointerOperand());
3257 } else {
3258 LLVM_DEBUG(dbgs() << "Alloca search canceled on unknown instruction: " << *V
3259 << "\n");
3260 }
3261 if (Res) AllocaForValue[V] = Res;
3262 return Res;
3263 }
3264
32653229 void FunctionStackPoisoner::handleDynamicAllocaCall(AllocaInst *AI) {
32663230 IRBuilder<> IRB(AI);
32673231
28852885 return true;
28862886 }
28872887 }
2888
2889 using AllocaForValueMapTy = DenseMap;
2890 AllocaInst *llvm::findAllocaForValue(Value *V,
2891 AllocaForValueMapTy &AllocaForValue) {
2892 if (AllocaInst *AI = dyn_cast(V))
2893 return AI;
2894 // See if we've already calculated (or started to calculate) alloca for a
2895 // given value.
2896 AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
2897 if (I != AllocaForValue.end())
2898 return I->second;
2899 // Store 0 while we're calculating alloca for value V to avoid
2900 // infinite recursion if the value references itself.
2901 AllocaForValue[V] = nullptr;
2902 AllocaInst *Res = nullptr;
2903 if (CastInst *CI = dyn_cast(V))
2904 Res = findAllocaForValue(CI->getOperand(0), AllocaForValue);
2905 else if (PHINode *PN = dyn_cast(V)) {
2906 for (Value *IncValue : PN->incoming_values()) {
2907 // Allow self-referencing phi-nodes.
2908 if (IncValue == PN)
2909 continue;
2910 AllocaInst *IncValueAI = findAllocaForValue(IncValue, AllocaForValue);
2911 // AI for incoming values should exist and should all be equal.
2912 if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res))
2913 return nullptr;
2914 Res = IncValueAI;
2915 }
2916 } else if (GetElementPtrInst *EP = dyn_cast(V)) {
2917 Res = findAllocaForValue(EP->getPointerOperand(), AllocaForValue);
2918 } else {
2919 LLVM_DEBUG(dbgs() << "Alloca search cancelled on unknown instruction: "
2920 << *V << "\n");
2921 }
2922 if (Res)
2923 AllocaForValue[V] = Res;
2924 return Res;
2925 }