118 using namespace llvm;
120 #define DEBUG_TYPE "msan"
138 cl::desc(
"Track origins (allocation sites) of poisoned memory"),
141 cl::desc(
"keep going after reporting a UMR"),
144 cl::desc(
"poison uninitialized stack variables"),
147 cl::desc(
"poison uninitialized stack variables with a call"),
150 cl::desc(
"poison uninitialized stack variables with the given pattern"),
157 cl::desc(
"propagate shadow through ICmpEQ and ICmpNE"),
161 cl::desc(
"exact handling of relational integer ICmp"),
171 cl::desc(
"report accesses through a pointer which has poisoned shadow"),
175 cl::desc(
"print out instructions with default strict semantics"),
179 "msan-instrumentation-with-call-threshold",
181 "If the function being instrumented requires more than "
182 "this number of checks and origin stores, use callbacks instead of "
183 "inline checks (-1 means never use callbacks)."),
190 cl::desc(
"Insert checks for constant shadow values"),
196 cl::desc(
"Place MSan constructors in comdat sections"),
208 struct MemoryMapParams {
215 struct PlatformMemoryMapParams {
216 const MemoryMapParams *bits32;
217 const MemoryMapParams *bits64;
221 static const MemoryMapParams Linux_I386_MemoryMapParams = {
229 static const MemoryMapParams Linux_X86_64_MemoryMapParams = {
230 #ifdef MSAN_LINUX_X86_64_OLD_MAPPING
244 static const MemoryMapParams Linux_MIPS64_MemoryMapParams = {
252 static const MemoryMapParams Linux_PowerPC64_MemoryMapParams = {
260 static const MemoryMapParams Linux_AArch64_MemoryMapParams = {
268 static const MemoryMapParams FreeBSD_I386_MemoryMapParams = {
276 static const MemoryMapParams FreeBSD_X86_64_MemoryMapParams = {
283 static const PlatformMemoryMapParams Linux_X86_MemoryMapParams = {
284 &Linux_I386_MemoryMapParams,
285 &Linux_X86_64_MemoryMapParams,
288 static const PlatformMemoryMapParams Linux_MIPS_MemoryMapParams = {
290 &Linux_MIPS64_MemoryMapParams,
293 static const PlatformMemoryMapParams Linux_PowerPC_MemoryMapParams = {
295 &Linux_PowerPC64_MemoryMapParams,
298 static const PlatformMemoryMapParams Linux_ARM_MemoryMapParams = {
300 &Linux_AArch64_MemoryMapParams,
303 static const PlatformMemoryMapParams FreeBSD_X86_MemoryMapParams = {
304 &FreeBSD_I386_MemoryMapParams,
305 &FreeBSD_X86_64_MemoryMapParams,
315 MemorySanitizer(
int TrackOrigins = 0,
bool Recover =
false)
319 WarningFn(
nullptr) {}
320 StringRef getPassName()
const override {
return "MemorySanitizer"; }
324 bool runOnFunction(
Function &
F)
override;
325 bool doInitialization(
Module &M)
override;
329 void initializeCallbacks(
Module &M);
364 Value *MsanSetAllocaOrigin4Fn;
366 Value *MsanPoisonStackFn;
369 Value *MsanChainOriginFn;
371 Value *MemmoveFn, *MemcpyFn, *MemsetFn;
374 const MemoryMapParams *MapParams;
378 MDNode *OriginStoreWeights;
383 friend struct MemorySanitizerVisitor;
384 friend struct VarArgAMD64Helper;
385 friend struct VarArgMIPS64Helper;
386 friend struct VarArgAArch64Helper;
387 friend struct VarArgPowerPC64Helper;
393 MemorySanitizer,
"msan",
394 "MemorySanitizer: detects uninitialized reads.",
false,
false)
398 "MemorySanitizer: detects uninitialized reads.",
false, false)
401 return new MemorySanitizer(TrackOrigins, Recover);
417 void MemorySanitizer::initializeCallbacks(
Module &M) {
426 StringRef WarningFnName = Recover ?
"__msan_warning"
427 :
"__msan_warning_noreturn";
432 unsigned AccessSize = 1 << AccessSizeIndex;
433 std::string FunctionName =
"__msan_maybe_warning_" +
itostr(AccessSize);
435 FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
438 FunctionName =
"__msan_maybe_store_origin_" +
itostr(AccessSize);
440 FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
441 IRB.getInt8PtrTy(), IRB.getInt32Ty());
445 "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
446 IRB.getInt8PtrTy(), IntptrTy);
449 IRB.getInt8PtrTy(), IntptrTy);
451 "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty());
453 "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
454 IRB.getInt8PtrTy(), IntptrTy);
456 "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
459 "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
486 "__msan_va_arg_overflow_size_tls",
nullptr,
501 bool MemorySanitizer::doInitialization(
Module &M) {
505 switch (TargetTriple.getOS()) {
507 switch (TargetTriple.getArch()) {
509 MapParams = FreeBSD_X86_MemoryMapParams.bits64;
512 MapParams = FreeBSD_X86_MemoryMapParams.bits32;
519 switch (TargetTriple.getArch()) {
521 MapParams = Linux_X86_MemoryMapParams.bits64;
524 MapParams = Linux_X86_MemoryMapParams.bits32;
528 MapParams = Linux_MIPS_MemoryMapParams.bits64;
532 MapParams = Linux_PowerPC_MemoryMapParams.bits64;
536 MapParams = Linux_ARM_MemoryMapParams.bits64;
548 IntptrTy = IRB.getIntPtrTy(
DL);
549 OriginTy = IRB.getInt32Ty();
554 std::tie(MsanCtorFunction, std::ignore) =
560 MsanCtorFunction->setComdat(MsanCtorComdat);
569 IRB.getInt32(TrackOrigins),
"__msan_track_origins");
573 IRB.getInt32(Recover),
"__msan_keep_going");
588 struct VarArgHelper {
602 virtual void finalizeInstrumentation() = 0;
604 virtual ~VarArgHelper() {}
607 struct MemorySanitizerVisitor;
610 CreateVarArgHelper(
Function &Func, MemorySanitizer &Msan,
611 MemorySanitizerVisitor &Visitor);
614 if (TypeSize <= 8)
return 0;
624 struct MemorySanitizerVisitor :
public InstVisitor<MemorySanitizerVisitor> {
629 std::unique_ptr<VarArgHelper> VAHelper;
635 bool PropagateShadow;
638 bool CheckReturnValue;
640 struct ShadowOriginAndInsertPoint {
645 : Shadow(S), Origin(O), OrigIns(I) { }
650 MemorySanitizerVisitor(
Function &
F, MemorySanitizer &MS)
651 : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)) {
652 bool SanitizeFunction = F.
hasFnAttribute(Attribute::SanitizeMemory);
653 InsertChecks = SanitizeFunction;
654 PropagateShadow = SanitizeFunction;
659 CheckReturnValue = SanitizeFunction && (F.
getName() ==
"main");
662 DEBUG(
if (!InsertChecks)
663 dbgs() <<
"MemorySanitizer is not inserting checks into '"
668 if (MS.TrackOrigins <= 1)
return V;
669 return IRB.
CreateCall(MS.MsanChainOriginFn, V);
683 unsigned Size,
unsigned Alignment) {
691 unsigned CurrentAlignment = Alignment;
692 if (Alignment >= IntptrAlignment && IntptrSize >
kOriginSize) {
693 Value *IntptrOrigin = originToIntptr(IRB, Origin);
694 Value *IntptrOriginPtr =
696 for (
unsigned i = 0; i < Size / IntptrSize; ++i) {
701 CurrentAlignment = IntptrAlignment;
714 unsigned Alignment,
bool AsCall) {
719 paintOrigin(IRB, updateOrigin(Origin, IRB),
720 getOriginPtr(Addr, IRB, Alignment), StoreSize,
723 Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
724 Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
725 if (ConstantShadow) {
727 paintOrigin(IRB, updateOrigin(Origin, IRB),
728 getOriginPtr(Addr, IRB, Alignment), StoreSize,
733 unsigned TypeSizeInBits =
736 if (AsCall && SizeIndex < kNumberOfAccessSizes) {
737 Value *Fn = MS.MaybeStoreOriginFn[SizeIndex];
739 ConvertedShadow, IRB.
getIntNTy(8 * (1 << SizeIndex)));
745 ConvertedShadow, getCleanShadow(ConvertedShadow),
"_mscmp");
749 paintOrigin(IRBNew, updateOrigin(Origin, IRBNew),
750 getOriginPtr(Addr, IRBNew, Alignment), StoreSize,
756 void materializeStores(
bool InstrumentWithCalls) {
759 Value *Val =
SI->getValueOperand();
760 Value *Addr =
SI->getPointerOperand();
761 Value *Shadow =
SI->isAtomic() ? getCleanShadow(Val) : getShadow(Val);
762 Value *ShadowPtr = getShadowPtr(Addr, Shadow->
getType(), IRB);
766 DEBUG(
dbgs() <<
" STORE: " << *NewSI <<
"\n");
770 insertShadowCheck(Addr, SI);
773 SI->setOrdering(addReleaseOrdering(
SI->getOrdering()));
775 if (MS.TrackOrigins && !
SI->isAtomic())
776 storeOrigin(IRB, Addr, Shadow, getOrigin(Val),
SI->getAlignment(),
777 InstrumentWithCalls);
784 DEBUG(
dbgs() <<
" SHAD0 : " << *Shadow <<
"\n");
785 Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
786 DEBUG(
dbgs() <<
" SHAD1 : " << *ConvertedShadow <<
"\n");
788 Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
789 if (ConstantShadow) {
791 if (MS.TrackOrigins) {
808 if (AsCall && SizeIndex < kNumberOfAccessSizes) {
809 Value *Fn = MS.MaybeWarningFn[SizeIndex];
810 Value *ConvertedShadow2 =
812 IRB.
CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin
817 getCleanShadow(ConvertedShadow),
"_mscmp");
820 !MS.Recover, MS.ColdCallWeights);
823 if (MS.TrackOrigins) {
829 DEBUG(
dbgs() <<
" CHECK: " << *Cmp <<
"\n");
833 void materializeChecks(
bool InstrumentWithCalls) {
834 for (
const auto &ShadowData : InstrumentationList) {
836 Value *Shadow = ShadowData.Shadow;
837 Value *Origin = ShadowData.Origin;
838 materializeOneCheck(OrigIns, Shadow, Origin, InstrumentWithCalls);
844 bool runOnFunction() {
861 for (
PHINode *PN : ShadowPHINodes) {
862 PHINode *PNS = cast<PHINode>(getShadow(PN));
863 PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) :
nullptr;
864 size_t NumValues = PN->getNumIncomingValues();
865 for (
size_t v = 0; v < NumValues; v++) {
866 PNS->
addIncoming(getShadow(PN, v), PN->getIncomingBlock(v));
867 if (PNO) PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v));
871 VAHelper->finalizeInstrumentation();
874 InstrumentationList.size() + StoreList.size() >
879 materializeStores(InstrumentWithCalls);
882 materializeChecks(InstrumentWithCalls);
889 return getShadowTy(V->
getType());
902 if (
VectorType *VT = dyn_cast<VectorType>(OrigTy)) {
904 return VectorType::get(IntegerType::get(*MS.C, EltSize),
905 VT->getNumElements());
907 if (
ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) {
908 return ArrayType::get(getShadowTy(AT->getElementType()),
909 AT->getNumElements());
913 for (
unsigned i = 0, n =
ST->getNumElements(); i < n; i++)
914 Elements.
push_back(getShadowTy(
ST->getElementType(i)));
915 StructType *Res = StructType::get(*MS.C, Elements,
ST->isPacked());
916 DEBUG(
dbgs() <<
"getShadowTy: " << *
ST <<
" ===> " << *Res <<
"\n");
920 return IntegerType::get(*MS.C, TypeSize);
925 if (
VectorType *vt = dyn_cast<VectorType>(ty))
926 return IntegerType::get(*MS.C, vt->getBitWidth());
933 Type *NoVecTy = getShadowTyNoVec(Ty);
934 if (Ty == NoVecTy)
return V;
945 uint64_t AndMask = MS.MapParams->AndMask;
948 IRB.
CreateAnd(OffsetLong, ConstantInt::get(MS.IntptrTy, ~AndMask));
950 uint64_t XorMask = MS.MapParams->XorMask;
953 IRB.
CreateXor(OffsetLong, ConstantInt::get(MS.IntptrTy, XorMask));
963 Value *ShadowLong = getShadowPtrOffset(Addr, IRB);
964 uint64_t ShadowBase = MS.MapParams->ShadowBase;
968 ConstantInt::get(MS.IntptrTy, ShadowBase));
969 return IRB.
CreateIntToPtr(ShadowLong, PointerType::get(ShadowTy, 0));
977 Value *OriginLong = getShadowPtrOffset(Addr, IRB);
978 uint64_t OriginBase = MS.MapParams->OriginBase;
982 ConstantInt::get(MS.IntptrTy, OriginBase));
986 ConstantInt::get(MS.IntptrTy, ~Mask));
998 Base = IRB.
CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
999 return IRB.
CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0),
1006 if (!MS.TrackOrigins)
return nullptr;
1008 Base = IRB.
CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
1009 return IRB.
CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0),
1016 return IRB.
CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0),
1023 return MS.RetvalOriginTLS;
1028 assert(!ShadowMap.count(V) &&
"Values may only have one shadow");
1029 ShadowMap[V] = PropagateShadow ? SV : getCleanShadow(V);
1034 if (!MS.TrackOrigins)
return;
1035 assert(!OriginMap.count(V) &&
"Values may only have one origin");
1036 DEBUG(
dbgs() <<
"ORIGIN: " << *V <<
" ==> " << *Origin <<
"\n");
1037 OriginMap[V] = Origin;
1041 Type *ShadowTy = getShadowTy(OrigTy);
1044 return Constant::getNullValue(ShadowTy);
1052 return getCleanShadow(V->
getType());
1058 if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy))
1059 return Constant::getAllOnesValue(ShadowTy);
1060 if (
ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) {
1062 getPoisonedShadow(AT->getElementType()));
1063 return ConstantArray::get(AT, Vals);
1067 for (
unsigned i = 0, n =
ST->getNumElements(); i < n; i++)
1068 Vals.
push_back(getPoisonedShadow(
ST->getElementType(i)));
1069 return ConstantStruct::get(
ST, Vals);
1076 Type *ShadowTy = getShadowTy(V);
1079 return getPoisonedShadow(ShadowTy);
1083 Value *getCleanOrigin() {
1084 return Constant::getNullValue(MS.OriginTy);
1092 if (!PropagateShadow)
return getCleanShadow(V);
1095 Value *Shadow = ShadowMap[V];
1097 DEBUG(
dbgs() <<
"No shadow: " << *V <<
"\n" << *(
I->getParent()));
1099 assert(Shadow &&
"No shadow for a value");
1103 if (
UndefValue *U = dyn_cast<UndefValue>(V)) {
1104 Value *AllOnes = PoisonUndef ? getPoisonedShadow(V) : getCleanShadow(V);
1105 DEBUG(
dbgs() <<
"Undef: " << *U <<
" ==> " << *AllOnes <<
"\n");
1109 if (
Argument *A = dyn_cast<Argument>(V)) {
1111 Value **ShadowPtr = &ShadowMap[V];
1116 unsigned ArgOffset = 0;
1118 for (
auto &FArg : F->
args()) {
1119 if (!FArg.getType()->isSized()) {
1129 Value *Base = getShadowPtrForArgument(&FArg, EntryIRB, ArgOffset);
1130 if (FArg.hasByValAttr()) {
1134 unsigned ArgAlign = FArg.getParamAlignment();
1135 if (ArgAlign == 0) {
1141 EntryIRB.CreateMemSet(
1142 getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB),
1143 Constant::getNullValue(EntryIRB.getInt8Ty()), Size, ArgAlign);
1146 Value *Cpy = EntryIRB.CreateMemCpy(
1147 getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), Base, Size,
1149 DEBUG(
dbgs() <<
" ByValCpy: " << *Cpy <<
"\n");
1152 *ShadowPtr = getCleanShadow(V);
1156 *ShadowPtr = getCleanShadow(V);
1162 DEBUG(
dbgs() <<
" ARG: " << FArg <<
" ==> " <<
1163 **ShadowPtr <<
"\n");
1164 if (MS.TrackOrigins && !Overflow) {
1166 getOriginPtrForArgument(&FArg, EntryIRB, ArgOffset);
1167 setOrigin(A, EntryIRB.CreateLoad(OriginPtr));
1169 setOrigin(A, getCleanOrigin());
1174 assert(*ShadowPtr &&
"Could not find shadow for an argument");
1178 return getCleanShadow(V);
1188 if (!MS.TrackOrigins)
return nullptr;
1189 if (!PropagateShadow)
return getCleanOrigin();
1190 if (isa<Constant>(V))
return getCleanOrigin();
1191 assert((isa<Instruction>(V) || isa<Argument>(V)) &&
1192 "Unexpected value type in getOrigin()");
1193 Value *Origin = OriginMap[V];
1194 assert(Origin &&
"Missing origin");
1209 if (!InsertChecks)
return;
1212 assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) &&
1213 "Can only insert checks for integer and vector shadow types");
1215 InstrumentationList.push_back(
1216 ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
1225 Value *Shadow, *Origin;
1227 Shadow = getShadow(Val);
1228 if (!Shadow)
return;
1229 Origin = getOrigin(Val);
1231 Shadow = dyn_cast_or_null<Instruction>(getShadow(Val));
1232 if (!Shadow)
return;
1233 Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
1235 insertShadowCheck(Shadow, Origin, OrigIns);
1240 case AtomicOrdering::NotAtomic:
1241 return AtomicOrdering::NotAtomic;
1242 case AtomicOrdering::Unordered:
1243 case AtomicOrdering::Monotonic:
1244 case AtomicOrdering::Release:
1245 return AtomicOrdering::Release;
1246 case AtomicOrdering::Acquire:
1247 case AtomicOrdering::AcquireRelease:
1248 return AtomicOrdering::AcquireRelease;
1249 case AtomicOrdering::SequentiallyConsistent:
1250 return AtomicOrdering::SequentiallyConsistent;
1257 case AtomicOrdering::NotAtomic:
1258 return AtomicOrdering::NotAtomic;
1259 case AtomicOrdering::Unordered:
1260 case AtomicOrdering::Monotonic:
1261 case AtomicOrdering::Acquire:
1262 return AtomicOrdering::Acquire;
1263 case AtomicOrdering::Release:
1264 case AtomicOrdering::AcquireRelease:
1265 return AtomicOrdering::AcquireRelease;
1266 case AtomicOrdering::SequentiallyConsistent:
1267 return AtomicOrdering::SequentiallyConsistent;
1281 Type *ShadowTy = getShadowTy(&I);
1283 if (PropagateShadow && !I.
getMetadata(
"nosanitize")) {
1284 Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB);
1288 setShadow(&I, getCleanShadow(&I));
1297 if (MS.TrackOrigins) {
1298 if (PropagateShadow) {
1304 setOrigin(&I, getCleanOrigin());
1314 StoreList.push_back(&I);
1318 assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I));
1322 Value *ShadowPtr = getShadowPtr(Addr, I.
getType(), IRB);
1325 insertShadowCheck(Addr, &I);
1330 if (isa<AtomicCmpXchgInst>(I))
1335 setShadow(&I, getCleanShadow(&I));
1336 setOrigin(&I, getCleanOrigin());
1355 setOrigin(&I, getOrigin(&I, 0));
1363 setOriginForNaryOp(I);
1371 setOriginForNaryOp(I);
1378 setOrigin(&I, getOrigin(&I, 0));
1384 setOrigin(&I, getOrigin(&I, 0));
1390 setOrigin(&I, getOrigin(&I, 0));
1397 if (
auto *CI = dyn_cast<CallInst>(I.
getOperand(0)))
1398 if (CI->isMustTailCall())
1401 setShadow(&I, IRB.
CreateBitCast(getShadow(&I, 0), getShadowTy(&I)));
1402 setOrigin(&I, getOrigin(&I, 0));
1407 setShadow(&I, IRB.
CreateIntCast(getShadow(&I, 0), getShadowTy(&I),
false,
1408 "_msprop_ptrtoint"));
1409 setOrigin(&I, getOrigin(&I, 0));
1414 setShadow(&I, IRB.
CreateIntCast(getShadow(&I, 0), getShadowTy(&I),
false,
1415 "_msprop_inttoptr"));
1416 setOrigin(&I, getOrigin(&I, 0));
1419 void visitFPToSIInst(
CastInst& I) { handleShadowOr(I); }
1420 void visitFPToUIInst(
CastInst& I) { handleShadowOr(I); }
1421 void visitSIToFPInst(
CastInst& I) { handleShadowOr(I); }
1422 void visitUIToFPInst(
CastInst& I) { handleShadowOr(I); }
1423 void visitFPExtInst(
CastInst& I) { handleShadowOr(I); }
1424 void visitFPTruncInst(
CastInst& I) { handleShadowOr(I); }
1438 Value *S1 = getShadow(&I, 0);
1439 Value *S2 = getShadow(&I, 1);
1450 setOriginForNaryOp(I);
1460 Value *S1 = getShadow(&I, 0);
1461 Value *S2 = getShadow(&I, 1);
1472 setOriginForNaryOp(I);
1490 template <
bool CombineShadow>
1495 MemorySanitizerVisitor *MSV;
1498 Combiner(MemorySanitizerVisitor *MSV,
IRBuilder<> &IRB) :
1499 Shadow(nullptr), Origin(nullptr), IRB(IRB), MSV(MSV) {}
1503 if (CombineShadow) {
1508 OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->
getType());
1509 Shadow = IRB.
CreateOr(Shadow, OpShadow,
"_msprop");
1513 if (MSV->MS.TrackOrigins) {
1520 if (!ConstOrigin || !ConstOrigin->
isNullValue()) {
1521 Value *FlatShadow = MSV->convertToShadowTyNoVec(OpShadow, IRB);
1523 IRB.
CreateICmpNE(FlatShadow, MSV->getCleanShadow(FlatShadow));
1533 Value *OpShadow = MSV->getShadow(V);
1534 Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) :
nullptr;
1535 return Add(OpShadow, OpOrigin);
1541 if (CombineShadow) {
1543 Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(I));
1544 MSV->setShadow(I, Shadow);
1546 if (MSV->MS.TrackOrigins) {
1548 MSV->setOrigin(I, Origin);
1553 typedef Combiner<true> ShadowAndOriginCombiner;
1554 typedef Combiner<false> OriginCombiner;
1558 if (!MS.TrackOrigins)
return;
1560 OriginCombiner
OC(
this, IRB);
1561 for (Instruction::op_iterator OI = I.
op_begin(); OI != I.
op_end(); ++OI)
1566 size_t VectorOrPrimitiveTypeSizeInBits(
Type *Ty) {
1568 "Vector of pointers is not a valid shadow type");
1579 size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy);
1580 size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy);
1581 if (srcSizeInBits > 1 && dstSizeInBits == 1)
1598 Type *ShadowTy = getShadowTy(V);
1610 ShadowAndOriginCombiner
SC(
this, IRB);
1611 for (Instruction::op_iterator OI = I.
op_begin(); OI != I.
op_end(); ++OI)
1633 for (
unsigned Idx = 0; Idx < NumElements; ++Idx) {
1636 const APInt &V = Elt->getValue();
1638 Elements.
push_back(ConstantInt::get(EltTy, V2));
1640 Elements.
push_back(ConstantInt::get(EltTy, 1));
1643 ShadowMul = ConstantVector::get(Elements);
1645 if (
ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg)) {
1646 const APInt &V = Elt->getValue();
1648 ShadowMul = ConstantInt::get(Ty, V2);
1650 ShadowMul = ConstantInt::get(Ty, 1);
1656 IRB.
CreateMul(getShadow(OtherArg), ShadowMul,
"msprop_mul_cst"));
1657 setOrigin(&I, getOrigin(OtherArg));
1663 if (constOp0 && !constOp1)
1664 handleMulByConstant(I, constOp0, I.
getOperand(1));
1665 else if (constOp1 && !constOp0)
1666 handleMulByConstant(I, constOp1, I.
getOperand(0));
1682 setShadow(&I, getShadow(&I, 0));
1683 setOrigin(&I, getOrigin(&I, 0));
1697 void handleEqualityComparison(
ICmpInst &I) {
1701 Value *Sa = getShadow(A);
1702 Value *Sb = getShadow(B);
1721 Value *MinusOne = Constant::getAllOnesValue(Sc->
getType());
1728 setOriginForNaryOp(I);
1770 void handleRelationalComparisonExact(
ICmpInst &I) {
1774 Value *Sa = getShadow(A);
1775 Value *Sb = getShadow(B);
1788 getLowestPossibleValue(IRB, A, Sa, IsSigned),
1789 getHighestPossibleValue(IRB, B, Sb, IsSigned));
1791 getHighestPossibleValue(IRB, A, Sa, IsSigned),
1792 getLowestPossibleValue(IRB, B, Sb, IsSigned));
1795 setOriginForNaryOp(I);
1802 void handleSignedRelationalComparison(
ICmpInst &I) {
1806 if ((constOp = dyn_cast<Constant>(I.
getOperand(1)))) {
1809 }
else if ((constOp = dyn_cast<Constant>(I.
getOperand(0)))) {
1818 (pre == CmpInst::ICMP_SLT || pre == CmpInst::ICMP_SGE)) ||
1820 (pre == CmpInst::ICMP_SGT || pre == CmpInst::ICMP_SLE))) {
1824 setShadow(&I, Shadow);
1825 setOrigin(&I, getOrigin(op));
1837 handleEqualityComparison(I);
1843 handleRelationalComparisonExact(I);
1847 handleSignedRelationalComparison(I);
1853 handleRelationalComparisonExact(I);
1868 Value *S1 = getShadow(&I, 0);
1869 Value *S2 = getShadow(&I, 1);
1874 setShadow(&I, IRB.
CreateOr(Shift, S2Conv));
1875 setOriginForNaryOp(I);
1928 VAHelper->visitVAStartInst(I);
1932 VAHelper->visitVACopyInst(I);
1942 Value *Shadow = getShadow(&I, 1);
1943 Value *ShadowPtr = getShadowPtr(Addr, Shadow->
getType(), IRB);
1950 insertShadowCheck(Addr, &I);
1953 if (MS.TrackOrigins)
1954 IRB.
CreateStore(getOrigin(&I, 1), getOriginPtr(Addr, IRB, 1));
1966 Type *ShadowTy = getShadowTy(&I);
1967 if (PropagateShadow) {
1968 Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB);
1973 setShadow(&I, getCleanShadow(&I));
1977 insertShadowCheck(Addr, &I);
1979 if (MS.TrackOrigins) {
1980 if (PropagateShadow)
1981 setOrigin(&I, IRB.
CreateLoad(getOriginPtr(Addr, IRB, 1)));
1983 setOrigin(&I, getCleanOrigin());
2003 for (
unsigned i = 0; i < NumArgOperands; ++i) {
2010 ShadowAndOriginCombiner
SC(
this, IRB);
2011 for (
unsigned i = 0; i < NumArgOperands; ++i)
2030 if (NumArgOperands == 0)
2033 if (NumArgOperands == 2 &&
2039 return handleVectorStoreIntrinsic(I);
2042 if (NumArgOperands == 1 &&
2047 return handleVectorLoadIntrinsic(I);
2051 if (maybeHandleSimpleNomemIntrinsic(I))
2064 setShadow(&I, IRB.
CreateCall(BswapFunc, getShadow(Op)));
2065 setOrigin(&I, getOrigin(Op));
2083 void handleVectorConvertIntrinsic(
IntrinsicInst &I,
int NumUsedElements) {
2085 Value *CopyOp, *ConvertOp;
2110 Value *ConvertShadow = getShadow(ConvertOp);
2111 Value *AggShadow =
nullptr;
2114 ConvertShadow, ConstantInt::get(IRB.
getInt32Ty(), 0));
2115 for (
int i = 1; i < NumUsedElements; ++i) {
2117 ConvertShadow, ConstantInt::get(IRB.
getInt32Ty(), i));
2118 AggShadow = IRB.
CreateOr(AggShadow, MoreShadow);
2121 AggShadow = ConvertShadow;
2124 insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I);
2131 Value *ResultShadow = getShadow(CopyOp);
2133 for (
int i = 0; i < NumUsedElements; ++i) {
2135 ResultShadow, ConstantInt::getNullValue(EltTy),
2138 setShadow(&I, ResultShadow);
2139 setOrigin(&I, getOrigin(CopyOp));
2141 setShadow(&I, getCleanShadow(&I));
2142 setOrigin(&I, getCleanOrigin());
2150 S = CreateShadowCast(IRB, S, IRB.
getInt64Ty(),
true);
2153 return CreateShadowCast(IRB, S2, T,
true);
2161 return CreateShadowCast(IRB, S2, T,
true);
2178 void handleVectorShiftIntrinsic(
IntrinsicInst &I,
bool Variable) {
2183 Value *S1 = getShadow(&I, 0);
2184 Value *S2 = getShadow(&I, 1);
2185 Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2)
2186 : Lower64ShadowExtend(IRB, S2, getShadowTy(&I));
2192 setShadow(&I, IRB.
CreateOr(Shift, S2Conv));
2193 setOriginForNaryOp(I);
2197 Type *getMMXVectorTy(
unsigned EltSizeInBits) {
2198 const unsigned X86_MMXSizeInBits = 64;
2199 return VectorType::get(IntegerType::get(*MS.C, EltSizeInBits),
2200 X86_MMXSizeInBits / EltSizeInBits);
2207 case llvm::Intrinsic::x86_sse2_packsswb_128:
2208 case llvm::Intrinsic::x86_sse2_packuswb_128:
2209 return llvm::Intrinsic::x86_sse2_packsswb_128;
2211 case llvm::Intrinsic::x86_sse2_packssdw_128:
2212 case llvm::Intrinsic::x86_sse41_packusdw:
2213 return llvm::Intrinsic::x86_sse2_packssdw_128;
2215 case llvm::Intrinsic::x86_avx2_packsswb:
2216 case llvm::Intrinsic::x86_avx2_packuswb:
2217 return llvm::Intrinsic::x86_avx2_packsswb;
2219 case llvm::Intrinsic::x86_avx2_packssdw:
2220 case llvm::Intrinsic::x86_avx2_packusdw:
2221 return llvm::Intrinsic::x86_avx2_packssdw;
2223 case llvm::Intrinsic::x86_mmx_packsswb:
2224 case llvm::Intrinsic::x86_mmx_packuswb:
2225 return llvm::Intrinsic::x86_mmx_packsswb;
2227 case llvm::Intrinsic::x86_mmx_packssdw:
2228 return llvm::Intrinsic::x86_mmx_packssdw;
2241 void handleVectorPackIntrinsic(
IntrinsicInst &I,
unsigned EltSizeInBits = 0) {
2245 Value *S1 = getShadow(&I, 0);
2246 Value *S2 = getShadow(&I, 1);
2252 Type *T = isX86_MMX ? getMMXVectorTy(EltSizeInBits) : S1->
getType();
2262 Type *X86_MMXTy = Type::getX86_MMXTy(*MS.C);
2271 IRB.
CreateCall(ShadowFn, {S1_ext, S2_ext},
"_msprop_vector_pack");
2274 setOriginForNaryOp(I);
2279 const unsigned SignificantBitsPerResultElement = 16;
2281 Type *ResTy = isX86_MMX ? IntegerType::get(*MS.C, 64) : I.
getType();
2282 unsigned ZeroBitsPerResultElement =
2286 Value *S = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2290 S = IRB.
CreateLShr(S, ZeroBitsPerResultElement);
2293 setOriginForNaryOp(I);
2298 unsigned EltSizeInBits = 0) {
2300 Type *ResTy = isX86_MMX ? getMMXVectorTy(EltSizeInBits * 2) : I.
getType();
2302 Value *S = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2308 setOriginForNaryOp(I);
2316 Type *ResTy = getShadowTy(&I);
2317 Value *S0 = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2319 IRB.
CreateICmpNE(S0, Constant::getNullValue(ResTy)), ResTy);
2321 setOriginForNaryOp(I);
2329 Value *S0 = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2330 Value *S = LowerElementShadowExtend(IRB, S0, getShadowTy(&I));
2332 setOriginForNaryOp(I);
2339 Value *ShadowPtr = getShadowPtr(Addr, Ty, IRB);
2345 insertShadowCheck(Addr, &I);
2349 if (!InsertChecks)
return;
2354 unsigned Alignment = 1;
2357 insertShadowCheck(Addr, &I);
2360 Alignment,
"_ldmxcsr");
2361 Value *Origin = MS.TrackOrigins
2362 ? IRB.
CreateLoad(getOriginPtr(Addr, IRB, Alignment))
2364 insertShadowCheck(Shadow, Origin, &I);
2369 case llvm::Intrinsic::bswap:
2372 case llvm::Intrinsic::x86_sse_stmxcsr:
2375 case llvm::Intrinsic::x86_sse_ldmxcsr:
2378 case llvm::Intrinsic::x86_avx512_vcvtsd2usi64:
2379 case llvm::Intrinsic::x86_avx512_vcvtsd2usi32:
2380 case llvm::Intrinsic::x86_avx512_vcvtss2usi64:
2381 case llvm::Intrinsic::x86_avx512_vcvtss2usi32:
2382 case llvm::Intrinsic::x86_avx512_cvttss2usi64:
2383 case llvm::Intrinsic::x86_avx512_cvttss2usi:
2384 case llvm::Intrinsic::x86_avx512_cvttsd2usi64:
2385 case llvm::Intrinsic::x86_avx512_cvttsd2usi:
2386 case llvm::Intrinsic::x86_avx512_cvtusi2sd:
2387 case llvm::Intrinsic::x86_avx512_cvtusi2ss:
2388 case llvm::Intrinsic::x86_avx512_cvtusi642sd:
2389 case llvm::Intrinsic::x86_avx512_cvtusi642ss:
2390 case llvm::Intrinsic::x86_sse2_cvtsd2si64:
2391 case llvm::Intrinsic::x86_sse2_cvtsd2si:
2392 case llvm::Intrinsic::x86_sse2_cvtsd2ss:
2393 case llvm::Intrinsic::x86_sse2_cvtsi2sd:
2394 case llvm::Intrinsic::x86_sse2_cvtsi642sd:
2395 case llvm::Intrinsic::x86_sse2_cvtss2sd:
2396 case llvm::Intrinsic::x86_sse2_cvttsd2si64:
2397 case llvm::Intrinsic::x86_sse2_cvttsd2si:
2398 case llvm::Intrinsic::x86_sse_cvtsi2ss:
2399 case llvm::Intrinsic::x86_sse_cvtsi642ss:
2400 case llvm::Intrinsic::x86_sse_cvtss2si64:
2401 case llvm::Intrinsic::x86_sse_cvtss2si:
2402 case llvm::Intrinsic::x86_sse_cvttss2si64:
2403 case llvm::Intrinsic::x86_sse_cvttss2si:
2404 handleVectorConvertIntrinsic(I, 1);
2406 case llvm::Intrinsic::x86_sse_cvtps2pi:
2407 case llvm::Intrinsic::x86_sse_cvttps2pi:
2408 handleVectorConvertIntrinsic(I, 2);
2411 case llvm::Intrinsic::x86_avx512_psll_w_512:
2412 case llvm::Intrinsic::x86_avx512_psll_d_512:
2413 case llvm::Intrinsic::x86_avx512_psll_q_512:
2414 case llvm::Intrinsic::x86_avx512_pslli_w_512:
2415 case llvm::Intrinsic::x86_avx512_pslli_d_512:
2416 case llvm::Intrinsic::x86_avx512_pslli_q_512:
2417 case llvm::Intrinsic::x86_avx512_psrl_w_512:
2418 case llvm::Intrinsic::x86_avx512_psrl_d_512:
2419 case llvm::Intrinsic::x86_avx512_psrl_q_512:
2420 case llvm::Intrinsic::x86_avx512_psra_w_512:
2421 case llvm::Intrinsic::x86_avx512_psra_d_512:
2422 case llvm::Intrinsic::x86_avx512_psra_q_512:
2423 case llvm::Intrinsic::x86_avx512_psrli_w_512:
2424 case llvm::Intrinsic::x86_avx512_psrli_d_512:
2425 case llvm::Intrinsic::x86_avx512_psrli_q_512:
2426 case llvm::Intrinsic::x86_avx512_psrai_w_512:
2427 case llvm::Intrinsic::x86_avx512_psrai_d_512:
2428 case llvm::Intrinsic::x86_avx512_psrai_q_512:
2429 case llvm::Intrinsic::x86_avx512_psra_q_256:
2430 case llvm::Intrinsic::x86_avx512_psra_q_128:
2431 case llvm::Intrinsic::x86_avx512_psrai_q_256:
2432 case llvm::Intrinsic::x86_avx512_psrai_q_128:
2433 case llvm::Intrinsic::x86_avx2_psll_w:
2434 case llvm::Intrinsic::x86_avx2_psll_d:
2435 case llvm::Intrinsic::x86_avx2_psll_q:
2436 case llvm::Intrinsic::x86_avx2_pslli_w:
2437 case llvm::Intrinsic::x86_avx2_pslli_d:
2438 case llvm::Intrinsic::x86_avx2_pslli_q:
2439 case llvm::Intrinsic::x86_avx2_psrl_w:
2440 case llvm::Intrinsic::x86_avx2_psrl_d:
2441 case llvm::Intrinsic::x86_avx2_psrl_q:
2442 case llvm::Intrinsic::x86_avx2_psra_w:
2443 case llvm::Intrinsic::x86_avx2_psra_d:
2444 case llvm::Intrinsic::x86_avx2_psrli_w:
2445 case llvm::Intrinsic::x86_avx2_psrli_d:
2446 case llvm::Intrinsic::x86_avx2_psrli_q:
2447 case llvm::Intrinsic::x86_avx2_psrai_w:
2448 case llvm::Intrinsic::x86_avx2_psrai_d:
2449 case llvm::Intrinsic::x86_sse2_psll_w:
2450 case llvm::Intrinsic::x86_sse2_psll_d:
2451 case llvm::Intrinsic::x86_sse2_psll_q:
2452 case llvm::Intrinsic::x86_sse2_pslli_w:
2453 case llvm::Intrinsic::x86_sse2_pslli_d:
2454 case llvm::Intrinsic::x86_sse2_pslli_q:
2455 case llvm::Intrinsic::x86_sse2_psrl_w:
2456 case llvm::Intrinsic::x86_sse2_psrl_d:
2457 case llvm::Intrinsic::x86_sse2_psrl_q:
2458 case llvm::Intrinsic::x86_sse2_psra_w:
2459 case llvm::Intrinsic::x86_sse2_psra_d:
2460 case llvm::Intrinsic::x86_sse2_psrli_w:
2461 case llvm::Intrinsic::x86_sse2_psrli_d:
2462 case llvm::Intrinsic::x86_sse2_psrli_q:
2463 case llvm::Intrinsic::x86_sse2_psrai_w:
2464 case llvm::Intrinsic::x86_sse2_psrai_d:
2465 case llvm::Intrinsic::x86_mmx_psll_w:
2466 case llvm::Intrinsic::x86_mmx_psll_d:
2467 case llvm::Intrinsic::x86_mmx_psll_q:
2468 case llvm::Intrinsic::x86_mmx_pslli_w:
2469 case llvm::Intrinsic::x86_mmx_pslli_d:
2470 case llvm::Intrinsic::x86_mmx_pslli_q:
2471 case llvm::Intrinsic::x86_mmx_psrl_w:
2472 case llvm::Intrinsic::x86_mmx_psrl_d:
2473 case llvm::Intrinsic::x86_mmx_psrl_q:
2474 case llvm::Intrinsic::x86_mmx_psra_w:
2475 case llvm::Intrinsic::x86_mmx_psra_d:
2476 case llvm::Intrinsic::x86_mmx_psrli_w:
2477 case llvm::Intrinsic::x86_mmx_psrli_d:
2478 case llvm::Intrinsic::x86_mmx_psrli_q:
2479 case llvm::Intrinsic::x86_mmx_psrai_w:
2480 case llvm::Intrinsic::x86_mmx_psrai_d:
2481 handleVectorShiftIntrinsic(I,
false);
2483 case llvm::Intrinsic::x86_avx2_psllv_d:
2484 case llvm::Intrinsic::x86_avx2_psllv_d_256:
2485 case llvm::Intrinsic::x86_avx512_psllv_d_512:
2486 case llvm::Intrinsic::x86_avx2_psllv_q:
2487 case llvm::Intrinsic::x86_avx2_psllv_q_256:
2488 case llvm::Intrinsic::x86_avx512_psllv_q_512:
2489 case llvm::Intrinsic::x86_avx2_psrlv_d:
2490 case llvm::Intrinsic::x86_avx2_psrlv_d_256:
2491 case llvm::Intrinsic::x86_avx512_psrlv_d_512:
2492 case llvm::Intrinsic::x86_avx2_psrlv_q:
2493 case llvm::Intrinsic::x86_avx2_psrlv_q_256:
2494 case llvm::Intrinsic::x86_avx512_psrlv_q_512:
2495 case llvm::Intrinsic::x86_avx2_psrav_d:
2496 case llvm::Intrinsic::x86_avx2_psrav_d_256:
2497 case llvm::Intrinsic::x86_avx512_psrav_d_512:
2498 case llvm::Intrinsic::x86_avx512_psrav_q_128:
2499 case llvm::Intrinsic::x86_avx512_psrav_q_256:
2500 case llvm::Intrinsic::x86_avx512_psrav_q_512:
2501 handleVectorShiftIntrinsic(I,
true);
2504 case llvm::Intrinsic::x86_sse2_packsswb_128:
2505 case llvm::Intrinsic::x86_sse2_packssdw_128:
2506 case llvm::Intrinsic::x86_sse2_packuswb_128:
2507 case llvm::Intrinsic::x86_sse41_packusdw:
2508 case llvm::Intrinsic::x86_avx2_packsswb:
2509 case llvm::Intrinsic::x86_avx2_packssdw:
2510 case llvm::Intrinsic::x86_avx2_packuswb:
2511 case llvm::Intrinsic::x86_avx2_packusdw:
2512 handleVectorPackIntrinsic(I);
2515 case llvm::Intrinsic::x86_mmx_packsswb:
2516 case llvm::Intrinsic::x86_mmx_packuswb:
2517 handleVectorPackIntrinsic(I, 16);
2520 case llvm::Intrinsic::x86_mmx_packssdw:
2521 handleVectorPackIntrinsic(I, 32);
2524 case llvm::Intrinsic::x86_mmx_psad_bw:
2525 case llvm::Intrinsic::x86_sse2_psad_bw:
2526 case llvm::Intrinsic::x86_avx2_psad_bw:
2527 handleVectorSadIntrinsic(I);
2530 case llvm::Intrinsic::x86_sse2_pmadd_wd:
2531 case llvm::Intrinsic::x86_avx2_pmadd_wd:
2532 case llvm::Intrinsic::x86_ssse3_pmadd_ub_sw_128:
2533 case llvm::Intrinsic::x86_avx2_pmadd_ub_sw:
2534 handleVectorPmaddIntrinsic(I);
2537 case llvm::Intrinsic::x86_ssse3_pmadd_ub_sw:
2538 handleVectorPmaddIntrinsic(I, 8);
2541 case llvm::Intrinsic::x86_mmx_pmadd_wd:
2542 handleVectorPmaddIntrinsic(I, 16);
2545 case llvm::Intrinsic::x86_sse_cmp_ss:
2546 case llvm::Intrinsic::x86_sse2_cmp_sd:
2547 case llvm::Intrinsic::x86_sse_comieq_ss:
2548 case llvm::Intrinsic::x86_sse_comilt_ss:
2549 case llvm::Intrinsic::x86_sse_comile_ss:
2550 case llvm::Intrinsic::x86_sse_comigt_ss:
2551 case llvm::Intrinsic::x86_sse_comige_ss:
2552 case llvm::Intrinsic::x86_sse_comineq_ss:
2553 case llvm::Intrinsic::x86_sse_ucomieq_ss:
2554 case llvm::Intrinsic::x86_sse_ucomilt_ss:
2555 case llvm::Intrinsic::x86_sse_ucomile_ss:
2556 case llvm::Intrinsic::x86_sse_ucomigt_ss:
2557 case llvm::Intrinsic::x86_sse_ucomige_ss:
2558 case llvm::Intrinsic::x86_sse_ucomineq_ss:
2559 case llvm::Intrinsic::x86_sse2_comieq_sd:
2560 case llvm::Intrinsic::x86_sse2_comilt_sd:
2561 case llvm::Intrinsic::x86_sse2_comile_sd:
2562 case llvm::Intrinsic::x86_sse2_comigt_sd:
2563 case llvm::Intrinsic::x86_sse2_comige_sd:
2564 case llvm::Intrinsic::x86_sse2_comineq_sd:
2565 case llvm::Intrinsic::x86_sse2_ucomieq_sd:
2566 case llvm::Intrinsic::x86_sse2_ucomilt_sd:
2567 case llvm::Intrinsic::x86_sse2_ucomile_sd:
2568 case llvm::Intrinsic::x86_sse2_ucomigt_sd:
2569 case llvm::Intrinsic::x86_sse2_ucomige_sd:
2570 case llvm::Intrinsic::x86_sse2_ucomineq_sd:
2571 handleVectorCompareScalarIntrinsic(I);
2574 case llvm::Intrinsic::x86_sse_cmp_ps:
2575 case llvm::Intrinsic::x86_sse2_cmp_pd:
2579 handleVectorComparePackedIntrinsic(I);
2583 if (!handleUnknownIntrinsic(I))
2584 visitInstruction(I);
2599 visitInstruction(I);
2603 assert(!isa<IntrinsicInst>(&I) &&
"intrinsics are handled elsewhere");
2621 unsigned ArgOffset = 0;
2622 DEBUG(
dbgs() <<
" CallSite: " << I <<
"\n");
2624 ArgIt !=
End; ++ArgIt) {
2628 DEBUG(
dbgs() <<
"Arg " << i <<
" is not sized: " << I <<
"\n");
2636 Value *ArgShadow = getShadow(A);
2637 Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset);
2638 DEBUG(
dbgs() <<
" Arg#" << i <<
": " << *A <<
2639 " Shadow: " << *ArgShadow <<
"\n");
2640 bool ArgIsInitialized =
false;
2644 "ByVal argument is not a pointer!");
2646 if (ArgOffset + Size > kParamTLSSize)
break;
2650 getShadowPtr(A, Type::getInt8Ty(*MS.C), IRB),
2654 if (ArgOffset + Size > kParamTLSSize)
break;
2658 if (Cst && Cst->
isNullValue()) ArgIsInitialized =
true;
2660 if (MS.TrackOrigins && !ArgIsInitialized)
2662 getOriginPtrForArgument(A, IRB, ArgOffset));
2664 assert(Size != 0 && Store !=
nullptr);
2665 DEBUG(
dbgs() <<
" Param:" << *Store <<
"\n");
2666 ArgOffset +=
alignTo(Size, 8);
2668 DEBUG(
dbgs() <<
" done with call args\n");
2673 VAHelper->visitCallSite(CS, IRB);
2679 if (CS.
isCall() && cast<CallInst>(&
I)->isMustTailCall())
return;
2682 Value *Base = getShadowPtrForRetval(&I, IRBBefore);
2689 BasicBlock *NormalDest = cast<InvokeInst>(&
I)->getNormalDest();
2694 setShadow(&I, getCleanShadow(&I));
2695 setOrigin(&I, getCleanOrigin());
2700 "Could not find insertion point for retval shadow load");
2703 Value *RetvalShadow =
2704 IRBAfter.CreateAlignedLoad(getShadowPtrForRetval(&I, IRBAfter),
2706 setShadow(&I, RetvalShadow);
2707 if (MS.TrackOrigins)
2708 setOrigin(&I, IRBAfter.CreateLoad(getOriginPtrForRetval(IRBAfter)));
2711 bool isAMustTailRetVal(
Value *RetVal) {
2712 if (
auto *I = dyn_cast<BitCastInst>(RetVal)) {
2715 if (
auto *I = dyn_cast<CallInst>(RetVal)) {
2716 return I->isMustTailCall();
2724 if (!RetVal)
return;
2726 if (isAMustTailRetVal(RetVal))
return;
2727 Value *ShadowPtr = getShadowPtrForRetval(RetVal, IRB);
2728 if (CheckReturnValue) {
2729 insertShadowCheck(RetVal, &I);
2730 Value *Shadow = getCleanShadow(RetVal);
2733 Value *Shadow = getShadow(RetVal);
2735 if (MS.TrackOrigins)
2736 IRB.
CreateStore(getOrigin(RetVal), getOriginPtrForRetval(IRB));
2740 void visitPHINode(
PHINode &I) {
2742 if (!PropagateShadow) {
2743 setShadow(&I, getCleanShadow(&I));
2744 setOrigin(&I, getCleanOrigin());
2748 ShadowPHINodes.push_back(&I);
2751 if (MS.TrackOrigins)
2757 setShadow(&I, getCleanShadow(&I));
2758 setOrigin(&I, getCleanOrigin());
2762 Value *Len = ConstantInt::get(MS.IntptrTy, TypeSize);
2769 Value *ShadowBase = getShadowPtr(&I, Type::getInt8PtrTy(*MS.C), IRB);
2774 if (PoisonStack && MS.TrackOrigins) {
2785 StackDescription.str());
2800 Value *Sb = getShadow(B);
2801 Value *Sc = getShadow(C);
2802 Value *Sd = getShadow(D);
2811 Sa1 = getPoisonedShadow(getShadowTy(I.
getType()));
2819 C = CreateAppToShadowCast(IRB, C);
2820 D = CreateAppToShadowCast(IRB, D);
2827 if (MS.TrackOrigins) {
2833 ConstantInt::getNullValue(FlatTy));
2835 ConstantInt::getNullValue(FlatTy));
2849 setShadow(&I, getCleanShadow(&I));
2850 setOrigin(&I, getCleanOrigin());
2854 setShadow(&I, getCleanShadow(&I));
2855 setOrigin(&I, getCleanOrigin());
2859 setShadow(&I, getCleanShadow(&I));
2860 setOrigin(&I, getCleanOrigin());
2870 DEBUG(
dbgs() <<
"ExtractValue: " << I <<
"\n");
2871 Value *AggShadow = getShadow(Agg);
2872 DEBUG(
dbgs() <<
" AggShadow: " << *AggShadow <<
"\n");
2874 DEBUG(
dbgs() <<
" ResShadow: " << *ResShadow <<
"\n");
2875 setShadow(&I, ResShadow);
2876 setOriginForNaryOp(I);
2881 DEBUG(
dbgs() <<
"InsertValue: " << I <<
"\n");
2884 DEBUG(
dbgs() <<
" AggShadow: " << *AggShadow <<
"\n");
2885 DEBUG(
dbgs() <<
" InsShadow: " << *InsShadow <<
"\n");
2887 DEBUG(
dbgs() <<
" Res: " << *Res <<
"\n");
2889 setOriginForNaryOp(I);
2893 if (
CallInst *CI = dyn_cast<CallInst>(&I)) {
2894 errs() <<
"ZZZ call " << CI->getCalledFunction()->getName() <<
"\n";
2898 errs() <<
"QQQ " << I <<
"\n";
2902 DEBUG(
dbgs() <<
"Resume: " << I <<
"\n");
2907 DEBUG(
dbgs() <<
"CleanupReturn: " << CRI <<
"\n");
2912 DEBUG(
dbgs() <<
"CatchReturn: " << CRI <<
"\n");
2920 DEBUG(
dbgs() <<
"DEFAULT: " << I <<
"\n");
2924 insertShadowCheck(Operand, &I);
2926 setShadow(&I, getCleanShadow(&I));
2927 setOrigin(&I, getCleanOrigin());
2932 struct VarArgAMD64Helper :
public VarArgHelper {
2935 static const unsigned AMD64GpEndOffset = 48;
2936 static const unsigned AMD64FpEndOffset = 176;
2939 MemorySanitizer &MS;
2940 MemorySanitizerVisitor &MSV;
2941 Value *VAArgTLSCopy;
2942 Value *VAArgOverflowSize;
2946 VarArgAMD64Helper(
Function &F, MemorySanitizer &MS,
2947 MemorySanitizerVisitor &MSV)
2948 : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(nullptr),
2949 VAArgOverflowSize(nullptr) {}
2951 enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
2953 ArgKind classifyArgument(
Value* arg) {
2957 return AK_FloatingPoint;
2959 return AK_GeneralPurpose;
2961 return AK_GeneralPurpose;
2974 unsigned GpOffset = 0;
2975 unsigned FpOffset = AMD64GpEndOffset;
2976 unsigned OverflowOffset = AMD64FpEndOffset;
2979 ArgIt !=
End; ++ArgIt) {
2983 bool IsByVal = CS.
paramHasAttr(ArgNo, Attribute::ByVal);
2993 Value *Base = getShadowPtrForVAArgument(RealTy, IRB, OverflowOffset);
2994 OverflowOffset +=
alignTo(ArgSize, 8);
2998 ArgKind AK = classifyArgument(A);
2999 if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset)
3001 if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset)
3005 case AK_GeneralPurpose:
3006 Base = getShadowPtrForVAArgument(A->
getType(), IRB, GpOffset);
3009 case AK_FloatingPoint:
3010 Base = getShadowPtrForVAArgument(A->
getType(), IRB, FpOffset);
3017 Base = getShadowPtrForVAArgument(A->
getType(), IRB, OverflowOffset);
3018 OverflowOffset +=
alignTo(ArgSize, 8);
3028 ConstantInt::get(IRB.
getInt64Ty(), OverflowOffset - AMD64FpEndOffset);
3029 IRB.
CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
3036 Base = IRB.
CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3037 return IRB.
CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3045 VAStartInstrumentationList.push_back(&I);
3047 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3055 void visitVACopyInst(
VACopyInst &I)
override {
3060 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3068 void finalizeInstrumentation()
override {
3069 assert(!VAArgOverflowSize && !VAArgTLSCopy &&
3070 "finalizeInstrumentation called twice");
3071 if (!VAStartInstrumentationList.empty()) {
3075 VAArgOverflowSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3077 IRB.
CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset),
3079 VAArgTLSCopy = IRB.
CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3080 IRB.
CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3085 for (
size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3086 CallInst *OrigInst = VAStartInstrumentationList[i];
3090 Value *RegSaveAreaPtrPtr =
3093 ConstantInt::get(MS.IntptrTy, 16)),
3094 Type::getInt64PtrTy(*MS.C));
3096 Value *RegSaveAreaShadowPtr =
3097 MSV.getShadowPtr(RegSaveAreaPtr, IRB.
getInt8Ty(), IRB);
3099 AMD64FpEndOffset, 16);
3101 Value *OverflowArgAreaPtrPtr =
3104 ConstantInt::get(MS.IntptrTy, 8)),
3105 Type::getInt64PtrTy(*MS.C));
3107 Value *OverflowArgAreaShadowPtr =
3108 MSV.getShadowPtr(OverflowArgAreaPtr, IRB.
getInt8Ty(), IRB);
3111 IRB.
CreateMemCpy(OverflowArgAreaShadowPtr, SrcPtr, VAArgOverflowSize, 16);
3117 struct VarArgMIPS64Helper :
public VarArgHelper {
3119 MemorySanitizer &MS;
3120 MemorySanitizerVisitor &MSV;
3121 Value *VAArgTLSCopy;
3126 VarArgMIPS64Helper(
Function &F, MemorySanitizer &MS,
3127 MemorySanitizerVisitor &MSV)
3128 : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(nullptr),
3129 VAArgSize(nullptr) {}
3132 unsigned VAArgOffset = 0;
3134 for (CallSite::arg_iterator ArgIt = CS.
arg_begin() +
3136 ArgIt !=
End; ++ArgIt) {
3145 VAArgOffset += (8 - ArgSize);
3147 Base = getShadowPtrForVAArgument(A->getType(), IRB, VAArgOffset);
3148 VAArgOffset += ArgSize;
3149 VAArgOffset =
alignTo(VAArgOffset, 8);
3156 IRB.
CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
3163 Base = IRB.
CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3164 return IRB.
CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3170 VAStartInstrumentationList.push_back(&I);
3172 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3177 void visitVACopyInst(
VACopyInst &I)
override {
3180 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3187 void finalizeInstrumentation()
override {
3188 assert(!VAArgSize && !VAArgTLSCopy &&
3189 "finalizeInstrumentation called twice");
3191 VAArgSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3192 Value *CopySize = IRB.
CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
3195 if (!VAStartInstrumentationList.empty()) {
3198 VAArgTLSCopy = IRB.
CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3199 IRB.
CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3204 for (
size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3205 CallInst *OrigInst = VAStartInstrumentationList[i];
3208 Value *RegSaveAreaPtrPtr =
3210 Type::getInt64PtrTy(*MS.C));
3212 Value *RegSaveAreaShadowPtr =
3213 MSV.getShadowPtr(RegSaveAreaPtr, IRB.
getInt8Ty(), IRB);
3214 IRB.
CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy, CopySize, 8);
3221 struct VarArgAArch64Helper :
public VarArgHelper {
3222 static const unsigned kAArch64GrArgSize = 64;
3223 static const unsigned kAArch64VrArgSize = 128;
3225 static const unsigned AArch64GrBegOffset = 0;
3226 static const unsigned AArch64GrEndOffset = kAArch64GrArgSize;
3228 static const unsigned AArch64VrBegOffset = AArch64GrEndOffset;
3229 static const unsigned AArch64VrEndOffset = AArch64VrBegOffset
3230 + kAArch64VrArgSize;
3231 static const unsigned AArch64VAEndOffset = AArch64VrEndOffset;
3234 MemorySanitizer &MS;
3235 MemorySanitizerVisitor &MSV;
3236 Value *VAArgTLSCopy;
3237 Value *VAArgOverflowSize;
3241 VarArgAArch64Helper(
Function &F, MemorySanitizer &MS,
3242 MemorySanitizerVisitor &MSV)
3243 : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(nullptr),
3244 VAArgOverflowSize(nullptr) {}
3246 enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
3248 ArgKind classifyArgument(
Value* arg) {
3251 return AK_FloatingPoint;
3254 return AK_GeneralPurpose;
3268 unsigned GrOffset = AArch64GrBegOffset;
3269 unsigned VrOffset = AArch64VrBegOffset;
3270 unsigned OverflowOffset = AArch64VAEndOffset;
3274 ArgIt !=
End; ++ArgIt) {
3278 ArgKind AK = classifyArgument(A);
3279 if (AK == AK_GeneralPurpose && GrOffset >= AArch64GrEndOffset)
3281 if (AK == AK_FloatingPoint && VrOffset >= AArch64VrEndOffset)
3285 case AK_GeneralPurpose:
3286 Base = getShadowPtrForVAArgument(A->
getType(), IRB, GrOffset);
3289 case AK_FloatingPoint:
3290 Base = getShadowPtrForVAArgument(A->
getType(), IRB, VrOffset);
3299 Base = getShadowPtrForVAArgument(A->
getType(), IRB, OverflowOffset);
3300 OverflowOffset +=
alignTo(ArgSize, 8);
3310 ConstantInt::get(IRB.
getInt64Ty(), OverflowOffset - AArch64VAEndOffset);
3311 IRB.
CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
3318 Base = IRB.
CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3319 return IRB.
CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3325 VAStartInstrumentationList.push_back(&I);
3327 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3334 void visitVACopyInst(
VACopyInst &I)
override {
3337 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3346 Value *SaveAreaPtrPtr =
3349 ConstantInt::get(MS.IntptrTy, offset)),
3350 Type::getInt64PtrTy(*MS.C));
3356 Value *SaveAreaPtr =
3359 ConstantInt::get(MS.IntptrTy, offset)),
3360 Type::getInt32PtrTy(*MS.C));
3362 return IRB.
CreateSExt(SaveArea32, MS.IntptrTy);
3365 void finalizeInstrumentation()
override {
3366 assert(!VAArgOverflowSize && !VAArgTLSCopy &&
3367 "finalizeInstrumentation called twice");
3368 if (!VAStartInstrumentationList.empty()) {
3372 VAArgOverflowSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3374 IRB.
CreateAdd(ConstantInt::get(MS.IntptrTy, AArch64VAEndOffset),
3376 VAArgTLSCopy = IRB.
CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3377 IRB.
CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3380 Value *GrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64GrArgSize);
3381 Value *VrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64VrArgSize);
3385 for (
size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3386 CallInst *OrigInst = VAStartInstrumentationList[i];
3405 Value *StackSaveAreaPtr = getVAField64(IRB, VAListTag, 0);
3408 Value *GrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 8);
3409 Value *GrOffSaveArea = getVAField32(IRB, VAListTag, 24);
3411 Value *GrRegSaveAreaPtr = IRB.
CreateAdd(GrTopSaveAreaPtr, GrOffSaveArea);
3414 Value *VrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 16);
3415 Value *VrOffSaveArea = getVAField32(IRB, VAListTag, 28);
3417 Value *VrRegSaveAreaPtr = IRB.
CreateAdd(VrTopSaveAreaPtr, VrOffSaveArea);
3423 Value *GrRegSaveAreaShadowPtrOff =
3424 IRB.
CreateAdd(GrArgSize, GrOffSaveArea);
3426 Value *GrRegSaveAreaShadowPtr =
3427 MSV.getShadowPtr(GrRegSaveAreaPtr, IRB.
getInt8Ty(), IRB);
3430 GrRegSaveAreaShadowPtrOff);
3431 Value *GrCopySize = IRB.
CreateSub(GrArgSize, GrRegSaveAreaShadowPtrOff);
3433 IRB.
CreateMemCpy(GrRegSaveAreaShadowPtr, GrSrcPtr, GrCopySize, 8);
3436 Value *VrRegSaveAreaShadowPtrOff =
3437 IRB.
CreateAdd(VrArgSize, VrOffSaveArea);
3439 Value *VrRegSaveAreaShadowPtr =
3440 MSV.getShadowPtr(VrRegSaveAreaPtr, IRB.
getInt8Ty(), IRB);
3446 VrRegSaveAreaShadowPtrOff);
3447 Value *VrCopySize = IRB.
CreateSub(VrArgSize, VrRegSaveAreaShadowPtrOff);
3449 IRB.
CreateMemCpy(VrRegSaveAreaShadowPtr, VrSrcPtr, VrCopySize, 8);
3452 Value *StackSaveAreaShadowPtr =
3453 MSV.getShadowPtr(StackSaveAreaPtr, IRB.
getInt8Ty(), IRB);
3455 Value *StackSrcPtr =
3460 VAArgOverflowSize, 16);
3466 struct VarArgPowerPC64Helper :
public VarArgHelper {
3468 MemorySanitizer &MS;
3469 MemorySanitizerVisitor &MSV;
3470 Value *VAArgTLSCopy;
3475 VarArgPowerPC64Helper(
Function &F, MemorySanitizer &MS,
3476 MemorySanitizerVisitor &MSV)
3477 : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(nullptr),
3478 VAArgSize(nullptr) {}
3497 unsigned VAArgOffset = VAArgBase;
3500 ArgIt !=
End; ++ArgIt) {
3504 bool IsByVal = CS.
paramHasAttr(ArgNo, Attribute::ByVal);
3512 VAArgOffset =
alignTo(VAArgOffset, ArgAlign);
3514 Value *Base = getShadowPtrForVAArgument(RealTy, IRB,
3515 VAArgOffset - VAArgBase);
3519 VAArgOffset +=
alignTo(ArgSize, 8);
3523 uint64_t ArgAlign = 8;
3536 VAArgOffset =
alignTo(VAArgOffset, ArgAlign);
3541 VAArgOffset += (8 - ArgSize);
3544 Base = getShadowPtrForVAArgument(A->
getType(), IRB,
3545 VAArgOffset - VAArgBase);
3548 VAArgOffset += ArgSize;
3549 VAArgOffset =
alignTo(VAArgOffset, 8);
3552 VAArgBase = VAArgOffset;
3556 VAArgOffset - VAArgBase);
3559 IRB.
CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
3566 Base = IRB.
CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3567 return IRB.
CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3573 VAStartInstrumentationList.push_back(&I);
3575 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3580 void visitVACopyInst(
VACopyInst &I)
override {
3583 Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.
getInt8Ty(), IRB);
3590 void finalizeInstrumentation()
override {
3591 assert(!VAArgSize && !VAArgTLSCopy &&
3592 "finalizeInstrumentation called twice");
3594 VAArgSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3595 Value *CopySize = IRB.
CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
3598 if (!VAStartInstrumentationList.empty()) {
3601 VAArgTLSCopy = IRB.
CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3602 IRB.
CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3607 for (
size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3608 CallInst *OrigInst = VAStartInstrumentationList[i];
3611 Value *RegSaveAreaPtrPtr =
3613 Type::getInt64PtrTy(*MS.C));
3615 Value *RegSaveAreaShadowPtr =
3616 MSV.getShadowPtr(RegSaveAreaPtr, IRB.
getInt8Ty(), IRB);
3617 IRB.
CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy, CopySize, 8);
3623 struct VarArgNoOpHelper :
public VarArgHelper {
3624 VarArgNoOpHelper(
Function &F, MemorySanitizer &MS,
3625 MemorySanitizerVisitor &MSV) {}
3631 void visitVACopyInst(
VACopyInst &I)
override {}
3633 void finalizeInstrumentation()
override {}
3636 VarArgHelper *CreateVarArgHelper(
Function &Func, MemorySanitizer &Msan,
3637 MemorySanitizerVisitor &Visitor) {
3642 return new VarArgAMD64Helper(Func, Msan, Visitor);
3645 return new VarArgMIPS64Helper(Func, Msan, Visitor);
3647 return new VarArgAArch64Helper(Func, Msan, Visitor);
3650 return new VarArgPowerPC64Helper(Func, Msan, Visitor);
3652 return new VarArgNoOpHelper(Func, Msan, Visitor);
3657 bool MemorySanitizer::runOnFunction(
Function &F) {
3658 if (&F == MsanCtorFunction)
3660 MemorySanitizerVisitor Visitor(F, *
this);
3668 return Visitor.runOnFunction();
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7")))
Return a value (possibly void), from a function.
const Value * getCalledValue() const
Get a pointer to the function that is invoked by this instruction.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
A parsed version of the target data layout string in and methods for querying it. ...
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name="")
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
BasicBlock::iterator GetInsertPoint() const
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents an incoming formal argument to a Function.
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Base class for instruction visitors.
Value * getAggregateOperand()
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
ArrayRef< unsigned > getIndices() const
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
A Module instance is used to store all the information related to an LLVM module. ...
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this rmw instruction.
AtomicOrdering getSuccessOrdering() const
Returns the success ordering constraint of this cmpxchg instruction.
Same, but only replaced by something equivalent.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
INITIALIZE_PASS_BEGIN(MemorySanitizer,"msan","MemorySanitizer: detects uninitialized reads.", false, false) INITIALIZE_PASS_END(MemorySanitizer
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
Type * getSequentialElementType() const
static cl::opt< bool > ClPoisonStackWithCall("msan-poison-stack-with-call", cl::desc("poison uninitialized stack variables with a call"), cl::Hidden, cl::init(false))
This class represents zero extension of integer types.
unsigned getNumOperands() const
static const unsigned kRetvalTLSSize
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
This class represents a function call, abstracting a target machine's calling convention.
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this load instruction.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
bool isSigned() const
Determine if this instruction is using a signed comparison.
Like Internal, but omit from symbol table.
This instruction constructs a fixed permutation of two input vectors.
static cl::opt< bool > ClWithComdat("msan-with-comdat", cl::desc("Place MSan constructors in comdat sections"), cl::Hidden, cl::init(false))
Externally visible function.
A raw_ostream that writes to an SmallVector or SmallString.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
This class wraps the llvm.memset intrinsic.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
This class represents a sign extension of integer types.
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memset to the specified pointer and the specified value.
An instruction for reading from memory.
AttrBuilder & addAttribute(Attribute::AttrKind Val)
Add an attribute to the builder.
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
static cl::opt< bool > ClHandleICmp("msan-handle-icmp", cl::desc("propagate shadow through ICmpEQ and ICmpNE"), cl::Hidden, cl::init(true))
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Type * getPointerElementType() const
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
Value * CreateNot(Value *V, const Twine &Name="")
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile=false)
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
bool isArrayAllocation() const
Return true if there is an allocation size parameter to the allocation instruction that is not 1...
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
static cl::opt< int > ClPoisonStackPattern("msan-poison-stack-pattern", cl::desc("poison uninitialized stack variables with the given pattern"), cl::Hidden, cl::init(0xff))
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static cl::opt< bool > ClDumpStrictInstructions("msan-dump-strict-instructions", cl::desc("print out instructions with default strict semantics"), cl::Hidden, cl::init(false))
This class represents the LLVM 'select' instruction.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
This is the base class for all instructions that perform data casts.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
'undef' values are things that do not have specified contents.
This class wraps the llvm.memmove intrinsic.
Class to represent struct types.
Type * getArrayElementType() const
static cl::opt< bool > ClCheckAccessAddress("msan-check-access-address", cl::desc("report accesses through a pointer which has poisoned shadow"), cl::Hidden, cl::init(true))
ValTy * getCalledValue() const
Return the pointer to function that is being called.
unsigned getNumArgOperands() const
Return the number of call arguments.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
bool isCall() const
Return true if a CallInst is enclosed.
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
void setName(const Twine &Name)
Change the name of the value.
Type * getVectorElementType() const
This class represents a cast from a pointer to an integer.
AtomicOrdering
Atomic ordering for LLVM's memory model.
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Class to represent function types.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this rmw instruction.
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
This represents the llvm.va_start intrinsic.
static const char *const kMsanInitName
static cl::opt< int > ClTrackOrigins("msan-track-origins", cl::desc("Track origins (allocation sites) of poisoned memory"), cl::Hidden, cl::init(0))
Track origins of uninitialized values.
Class to represent array types.
This instruction compares its operands according to the predicate given to the constructor.
This class represents a no-op cast from one type to another.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Value * getInsertedValueOperand()
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
An instruction for storing to memory.
bool isArrayTy() const
True if this is an instance of ArrayType.
static const unsigned kParamTLSSize
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
static cl::opt< bool > ClPoisonStack("msan-poison-stack", cl::desc("poison uninitialized stack variables"), cl::Hidden, cl::init(true))
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
const char * getOpcodeName() const
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
This class represents a truncation of integer types.
bool isAtomic() const
Return true if this instruction has an AtomicOrdering of unordered or higher.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
unsigned getParamAlignment(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
static const unsigned kMinOriginAlignment
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
bool isInvoke() const
Return true if a InvokeInst is enclosed.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
LoadInst * CreateLoad(Value *Ptr, const char *Name)
bool isX86_MMXTy() const
Return true if this is X86 MMX.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
initializer< Ty > init(const Ty &Val)
This instruction inserts a single (scalar) element into a VectorType value.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
FunctionType * getFunctionType() const
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
unsigned getArgumentNo(Value::const_user_iterator I) const
Given a value use iterator, returns the argument that corresponds to it.
This is an important class for using LLVM in a threaded context.
bool isVectorTy() const
True if this is an instance of VectorType.
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
This is an important base class in LLVM.
const Value * getCondition() const
Resume the propagation of an exception.
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
unsigned getAlignment() const
Return the alignment of the memory that is being allocated by the instruction.
bool isZeroValue() const
Return true if the value is negative zero or null value.
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
unsigned getBitWidth() const
Return the number of bits in the APInt.
static const unsigned End
bool doesNotAccessMemory() const
Determine if the call does not access memory.
FunctionPass class - This class is used to implement most global optimizations.
static const unsigned kShadowTLSAlignment
Value * getOperand(unsigned i) const
Value * getPointerOperand()
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
self_iterator getIterator()
Class to represent integer types.
Predicate getPredicate() const
Return the predicate for this instruction.
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
This class represents a cast from an integer to a pointer.
bool isPointerTy() const
True if this is an instance of PointerType.
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
static std::string itostr(int64_t X)
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
static wasm::ValType getType(const TargetRegisterClass *RC)
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
const Value * getTrueValue() const
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Triple - Helper class for working with autoconf configuration names.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
std::pair< Function *, Function * > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, StringRef VersionCheckName=StringRef())
Creates sanitizer constructor function, and calls sanitizer's init function from it.
BinaryOps getOpcode() const
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
InstrTy * getInstruction() const
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI=nullptr)
Remove all blocks that can not be reached from the function's entry.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
AttrBuilder & removeAttributes(AttributeList A, uint64_t WithoutIndex)
Remove the attributes from the builder.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static size_t TypeSizeToSizeIndex(uint32_t TypeSize)
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Provides information about what library functions are available for the current target.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
static cl::opt< int > ClInstrumentationWithCallThreshold("msan-instrumentation-with-call-threshold", cl::desc("If the function being instrumented requires more than ""this number of checks and origin stores, use callbacks instead of ""inline checks (-1 means never use callbacks)."), cl::Hidden, cl::init(3500))
TerminatorInst * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
CHAIN = SC CHAIN, Imm128 - System call.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
This class wraps the llvm.memcpy intrinsic.
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
const BasicBlock & getEntryBlock() const
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
static const size_t kNumberOfAccessSizes
static GlobalVariable * createPrivateNonConstGlobalForString(Module &M, StringRef Str)
Create a non-const global initialized with the given string.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
static cl::opt< bool > ClKeepGoing("msan-keep-going", cl::desc("keep going after reporting a UMR"), cl::Hidden, cl::init(false))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
static int alignTo(int Num, int PowOf2)
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class to represent vector types.
Class for arbitrary precision integers.
bool isIntegerTy() const
True if this is an instance of IntegerType.
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
unsigned getVectorNumElements() const
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
bool isInlineAsm() const
Check if this call is an inline asm statement.
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
The C convention as implemented on Windows/x86-64 and AArch64.
llvm_unreachable("Unknown RegBank!")
void removeAttributes(unsigned i, const AttrBuilder &Attrs)
removes the attributes from the list of attributes.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
static cl::opt< bool > ClHandleICmpExact("msan-handle-icmp-exact", cl::desc("exact handling of relational integer ICmp"), cl::Hidden, cl::init(false))
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
bool isAggregateType() const
Return true if the type is an aggregate type.
CallInst * CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memcpy between the specified pointers.
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the call or the callee has the given attribute.
unsigned getAlignment() const
Return the alignment of the access that is being performed.
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, const TargetLibraryInfo *TLI)
Given a CallInst, check if it calls a string function known to CodeGen, and mark it with NoBuiltin if...
static InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT)
InlineAsm::get - Return the specified uniqued inline asm string.
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
static cl::opt< bool > ClCheckConstantShadow("msan-check-constant-shadow", cl::desc("Insert checks for constant shadow values"), cl::Hidden, cl::init(false))
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
static const unsigned kOriginSize
iterator_range< df_iterator< T > > depth_first(const T &G)
bool isUnsigned() const
Determine if this instruction is using an unsigned comparison.
This represents the llvm.va_copy intrinsic.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LoadInst * CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name)
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
void setSuccessOrdering(AtomicOrdering Ordering)
Sets the success ordering constraint of this cmpxchg instruction.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
const Value * getArraySize() const
Get the number of elements allocated.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
FunctionPass * createMemorySanitizerPass(int TrackOrigins=0, bool Recover=false)
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
static cl::opt< bool > ClPoisonUndef("msan-poison-undef", cl::desc("poison undef temps"), cl::Hidden, cl::init(true))
ConstantInt * getInt8(uint8_t C)
Get a constant 8-bit value.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
const Value * getFalseValue() const
StringRef - Represent a constant reference to a string, i.e.
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
static const char *const kMsanModuleCtorName
const BasicBlock * getParent() const
iterator_range< arg_iterator > args()
A wrapper class for inspecting calls to intrinsic functions.
LLVMContext & getContext() const
Get the global data context.
bool isVoidTy() const
Return true if this is 'void'.
an instruction to allocate memory on the stack
This instruction inserts a struct field of array element value into an aggregate value.