151 using namespace llvm;
153 #define DEBUG_TYPE "msan" 171 cl::desc(
"Track origins (allocation sites) of poisoned memory"),
175 cl::desc(
"keep going after reporting a UMR"),
179 cl::desc(
"poison uninitialized stack variables"),
183 cl::desc(
"poison uninitialized stack variables with a call"),
187 cl::desc(
"poison uninitialized stack variables with the given pattern"),
195 cl::desc(
"propagate shadow through ICmpEQ and ICmpNE"),
199 cl::desc(
"exact handling of relational integer ICmp"),
210 "msan-handle-asm-conservative",
221 cl::desc(
"report accesses through a pointer which has poisoned shadow"),
225 cl::desc(
"print out instructions with default strict semantics"),
229 "msan-instrumentation-with-call-threshold",
231 "If the function being instrumented requires more than " 232 "this number of checks and origin stores, use callbacks instead of " 233 "inline checks (-1 means never use callbacks)."),
240 cl::desc(
"Insert checks for constant shadow values"),
246 cl::desc(
"Place MSan constructors in comdat sections"),
252 cl::desc(
"Define custom MSan AndMask"),
256 cl::desc(
"Define custom MSan XorMask"),
260 cl::desc(
"Define custom MSan ShadowBase"),
264 cl::desc(
"Define custom MSan OriginBase"),
276 struct MemoryMapParams {
283 struct PlatformMemoryMapParams {
284 const MemoryMapParams *bits32;
285 const MemoryMapParams *bits64;
300 #ifdef MSAN_LINUX_X86_64_OLD_MAPPING 403 MemorySanitizer(
int TrackOrigins = 0,
bool Recover =
false)
408 StringRef getPassName()
const override {
return "MemorySanitizer"; }
415 bool doInitialization(
Module &M)
override;
418 friend struct MemorySanitizerVisitor;
419 friend struct VarArgAMD64Helper;
420 friend struct VarArgMIPS64Helper;
421 friend struct VarArgAArch64Helper;
422 friend struct VarArgPowerPC64Helper;
424 void initializeCallbacks(
Module &M);
425 void createUserspaceApi(
Module &M);
460 bool CallbacksInitialized =
false;
471 Value *MsanSetAllocaOrigin4Fn;
474 Value *MsanPoisonStackFn;
478 Value *MsanChainOriginFn;
481 Value *MemmoveFn, *MemcpyFn, *MemsetFn;
484 const MemoryMapParams *MapParams;
488 MemoryMapParams CustomMapParams;
493 MDNode *OriginStoreWeights;
506 MemorySanitizer,
"msan",
507 "MemorySanitizer: detects uninitialized reads.",
false,
false)
510 MemorySanitizer,
"msan",
511 "MemorySanitizer: detects uninitialized reads.",
false,
false)
514 return new MemorySanitizer(TrackOrigins, Recover);
530 void MemorySanitizer::createUserspaceApi(
Module &M) {
535 StringRef WarningFnName = Recover ?
"__msan_warning" 536 :
"__msan_warning_noreturn";
565 "__msan_va_arg_overflow_size_tls",
nullptr,
573 unsigned AccessSize = 1 << AccessSizeIndex;
574 std::string FunctionName =
"__msan_maybe_warning_" +
itostr(AccessSize);
579 FunctionName =
"__msan_maybe_store_origin_" +
itostr(AccessSize);
594 void MemorySanitizer::initializeCallbacks(
Module &M) {
596 if (CallbacksInitialized)
618 createUserspaceApi(M);
619 CallbacksInitialized =
true;
625 bool MemorySanitizer::doInitialization(
Module &M) {
628 bool ShadowPassed =
ClShadowBase.getNumOccurrences() > 0;
629 bool OriginPassed =
ClOriginBase.getNumOccurrences() > 0;
631 if (ShadowPassed || OriginPassed) {
636 MapParams = &CustomMapParams;
639 switch (TargetTriple.getOS()) {
641 switch (TargetTriple.getArch()) {
653 switch (TargetTriple.getArch()) {
662 switch (TargetTriple.getArch()) {
698 std::tie(MsanCtorFunction, std::ignore) =
704 MsanCtorFunction->setComdat(MsanCtorComdat);
713 IRB.
getInt32(TrackOrigins),
"__msan_track_origins");
717 IRB.
getInt32(Recover),
"__msan_keep_going");
732 struct VarArgHelper {
733 virtual ~VarArgHelper() =
default;
742 virtual void visitVACopyInst(
VACopyInst &I) = 0;
748 virtual void finalizeInstrumentation() = 0;
751 struct MemorySanitizerVisitor;
756 MemorySanitizerVisitor &Visitor);
759 if (TypeSize <= 8)
return 0;
771 struct MemorySanitizerVisitor :
public InstVisitor<MemorySanitizerVisitor> {
776 std::unique_ptr<VarArgHelper> VAHelper;
783 bool PropagateShadow;
786 bool CheckReturnValue;
788 struct ShadowOriginAndInsertPoint {
794 : Shadow(S), Origin(O), OrigIns(I) {}
799 MemorySanitizerVisitor(
Function &F, MemorySanitizer &MS)
801 bool SanitizeFunction = F.
hasFnAttribute(Attribute::SanitizeMemory);
802 InsertChecks = SanitizeFunction;
803 PropagateShadow = SanitizeFunction;
808 CheckReturnValue = SanitizeFunction && (F.
getName() ==
"main");
815 <<
"MemorySanitizer is not inserting checks into '" 820 if (MS.TrackOrigins <= 1)
return V;
821 return IRB.
CreateCall(MS.MsanChainOriginFn, V);
827 if (IntptrSize == kOriginSize)
return Origin;
828 assert(IntptrSize == kOriginSize * 2);
835 unsigned Size,
unsigned Alignment) {
839 assert(IntptrAlignment >= kMinOriginAlignment);
840 assert(IntptrSize >= kOriginSize);
843 unsigned CurrentAlignment = Alignment;
844 if (Alignment >= IntptrAlignment && IntptrSize > kOriginSize) {
845 Value *IntptrOrigin = originToIntptr(IRB, Origin);
846 Value *IntptrOriginPtr =
848 for (
unsigned i = 0; i < Size / IntptrSize; ++i) {
853 CurrentAlignment = IntptrAlignment;
857 for (
unsigned i = Ofs; i < (Size + kOriginSize - 1) / kOriginSize; ++i) {
866 Value *OriginPtr,
unsigned Alignment,
bool AsCall) {
868 unsigned OriginAlignment =
std::max(kMinOriginAlignment, Alignment);
871 paintOrigin(IRB, updateOrigin(Origin, IRB), OriginPtr, StoreSize,
874 Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
875 Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
876 if (ConstantShadow) {
878 paintOrigin(IRB, updateOrigin(Origin, IRB), OriginPtr, StoreSize,
883 unsigned TypeSizeInBits =
887 Value *Fn = MS.MaybeStoreOriginFn[SizeIndex];
889 ConvertedShadow, IRB.
getIntNTy(8 * (1 << SizeIndex)));
895 ConvertedShadow, getCleanShadow(ConvertedShadow),
"_mscmp");
899 paintOrigin(IRBNew, updateOrigin(Origin, IRBNew), OriginPtr, StoreSize,
905 void materializeStores(
bool InstrumentWithCalls) {
908 Value *Val =
SI->getValueOperand();
909 Value *Addr =
SI->getPointerOperand();
910 Value *Shadow =
SI->isAtomic() ? getCleanShadow(Val) : getShadow(Val);
911 Value *ShadowPtr, *OriginPtr;
913 unsigned Alignment =
SI->getAlignment();
914 unsigned OriginAlignment =
std::max(kMinOriginAlignment, Alignment);
915 std::tie(ShadowPtr, OriginPtr) =
916 getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment,
true);
923 SI->setOrdering(addReleaseOrdering(
SI->getOrdering()));
925 if (MS.TrackOrigins && !
SI->isAtomic())
926 storeOrigin(IRB, Addr, Shadow, getOrigin(Val), OriginPtr,
927 OriginAlignment, InstrumentWithCalls);
935 if (MS.TrackOrigins) {
949 Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
952 Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
953 if (ConstantShadow) {
955 insertWarningFn(IRB, Origin);
965 Value *Fn = MS.MaybeWarningFn[SizeIndex];
966 Value *ConvertedShadow2 =
968 IRB.
CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin
973 getCleanShadow(ConvertedShadow),
"_mscmp");
976 !MS.Recover, MS.ColdCallWeights);
979 insertWarningFn(IRB, Origin);
984 void materializeChecks(
bool InstrumentWithCalls) {
985 for (
const auto &ShadowData : InstrumentationList) {
987 Value *Shadow = ShadowData.Shadow;
988 Value *Origin = ShadowData.Origin;
989 materializeOneCheck(OrigIns, Shadow, Origin, InstrumentWithCalls);
1009 for (
PHINode *PN : ShadowPHINodes) {
1010 PHINode *PNS = cast<PHINode>(getShadow(PN));
1011 PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) :
nullptr;
1012 size_t NumValues = PN->getNumIncomingValues();
1013 for (
size_t v = 0; v < NumValues; v++) {
1014 PNS->
addIncoming(getShadow(PN, v), PN->getIncomingBlock(v));
1015 if (PNO) PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v));
1019 VAHelper->finalizeInstrumentation();
1022 InstrumentationList.
size() + StoreList.
size() >
1026 materializeChecks(InstrumentWithCalls);
1030 materializeStores(InstrumentWithCalls);
1037 return getShadowTy(V->
getType());
1050 if (
VectorType *VT = dyn_cast<VectorType>(OrigTy)) {
1053 VT->getNumElements());
1055 if (
ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) {
1057 AT->getNumElements());
1061 for (
unsigned i = 0, n =
ST->getNumElements(); i < n; i++)
1062 Elements.
push_back(getShadowTy(
ST->getElementType(i)));
1073 if (
VectorType *vt = dyn_cast<VectorType>(ty))
1081 Type *NoVecTy = getShadowTyNoVec(Ty);
1082 if (Ty == NoVecTy)
return V;
1093 uint64_t AndMask = MS.MapParams->AndMask;
1098 uint64_t XorMask = MS.MapParams->XorMask;
1110 std::pair<Value *, Value *> getShadowOriginPtrUserspace(
Value *Addr,
1113 unsigned Alignment) {
1114 Value *ShadowOffset = getShadowPtrOffset(Addr, IRB);
1115 Value *ShadowLong = ShadowOffset;
1116 uint64_t ShadowBase = MS.MapParams->ShadowBase;
1117 if (ShadowBase != 0) {
1124 Value *OriginPtr =
nullptr;
1125 if (MS.TrackOrigins) {
1126 Value *OriginLong = ShadowOffset;
1127 uint64_t OriginBase = MS.MapParams->OriginBase;
1128 if (OriginBase != 0)
1131 if (Alignment < kMinOriginAlignment) {
1132 uint64_t
Mask = kMinOriginAlignment - 1;
1139 return std::make_pair(ShadowPtr, OriginPtr);
1142 std::pair<Value *, Value *> getShadowOriginPtr(
Value *Addr,
IRBuilder<> &IRB,
1146 std::pair<Value *, Value *> ret =
1147 getShadowOriginPtrUserspace(Addr, IRB, ShadowTy, Alignment);
1166 if (!MS.TrackOrigins)
return nullptr;
1184 return MS.RetvalOriginTLS;
1189 assert(!ShadowMap.
count(V) &&
"Values may only have one shadow");
1190 ShadowMap[V] = PropagateShadow ? SV : getCleanShadow(V);
1195 if (!MS.TrackOrigins)
return;
1196 assert(!OriginMap.
count(V) &&
"Values may only have one origin");
1197 LLVM_DEBUG(
dbgs() <<
"ORIGIN: " << *V <<
" ==> " << *Origin <<
"\n");
1198 OriginMap[V] = Origin;
1202 Type *ShadowTy = getShadowTy(OrigTy);
1213 return getCleanShadow(V->
getType());
1219 if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy))
1221 if (
ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) {
1223 getPoisonedShadow(AT->getElementType()));
1228 for (
unsigned i = 0, n =
ST->getNumElements(); i < n; i++)
1229 Vals.
push_back(getPoisonedShadow(
ST->getElementType(i)));
1237 Type *ShadowTy = getShadowTy(V);
1240 return getPoisonedShadow(ShadowTy);
1244 Value *getCleanOrigin() {
1253 if (!PropagateShadow)
return getCleanShadow(V);
1255 if (
I->getMetadata(
"nosanitize"))
1256 return getCleanShadow(V);
1258 Value *Shadow = ShadowMap[V];
1260 LLVM_DEBUG(
dbgs() <<
"No shadow: " << *V <<
"\n" << *(
I->getParent()));
1262 assert(Shadow &&
"No shadow for a value");
1266 if (
UndefValue *U = dyn_cast<UndefValue>(V)) {
1267 Value *AllOnes = PoisonUndef ? getPoisonedShadow(V) : getCleanShadow(V);
1268 LLVM_DEBUG(
dbgs() <<
"Undef: " << *U <<
" ==> " << *AllOnes <<
"\n");
1272 if (
Argument *A = dyn_cast<Argument>(V)) {
1274 Value **ShadowPtr = &ShadowMap[V];
1279 unsigned ArgOffset = 0;
1281 for (
auto &FArg : F->
args()) {
1282 if (!FArg.getType()->isSized()) {
1292 Value *
Base = getShadowPtrForArgument(&FArg, EntryIRB, ArgOffset);
1293 if (FArg.hasByValAttr()) {
1297 unsigned ArgAlign = FArg.getParamAlignment();
1298 if (ArgAlign == 0) {
1302 Value *CpShadowPtr =
1303 getShadowOriginPtr(V, EntryIRB, EntryIRB.getInt8Ty(), ArgAlign,
1308 EntryIRB.CreateMemSet(
1312 unsigned CopyAlign = std::min(ArgAlign, kShadowTLSAlignment);
1313 Value *Cpy = EntryIRB.CreateMemCpy(CpShadowPtr, CopyAlign, Base,
1318 *ShadowPtr = getCleanShadow(V);
1322 *ShadowPtr = getCleanShadow(V);
1325 EntryIRB.CreateAlignedLoad(Base, kShadowTLSAlignment);
1329 <<
" ARG: " << FArg <<
" ==> " << **ShadowPtr <<
"\n");
1330 if (MS.TrackOrigins && !Overflow) {
1332 getOriginPtrForArgument(&FArg, EntryIRB, ArgOffset);
1333 setOrigin(A, EntryIRB.CreateLoad(OriginPtr));
1335 setOrigin(A, getCleanOrigin());
1338 ArgOffset +=
alignTo(Size, kShadowTLSAlignment);
1340 assert(*ShadowPtr &&
"Could not find shadow for an argument");
1344 return getCleanShadow(V);
1354 if (!MS.TrackOrigins)
return nullptr;
1355 if (!PropagateShadow)
return getCleanOrigin();
1356 if (isa<Constant>(V))
return getCleanOrigin();
1357 assert((isa<Instruction>(V) || isa<Argument>(V)) &&
1358 "Unexpected value type in getOrigin()");
1361 return getCleanOrigin();
1363 Value *Origin = OriginMap[V];
1364 assert(Origin &&
"Missing origin");
1379 if (!InsertChecks)
return;
1382 assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) &&
1383 "Can only insert checks for integer and vector shadow types");
1386 ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
1395 Value *Shadow, *Origin;
1397 Shadow = getShadow(Val);
1398 if (!Shadow)
return;
1399 Origin = getOrigin(Val);
1401 Shadow = dyn_cast_or_null<Instruction>(getShadow(Val));
1402 if (!Shadow)
return;
1403 Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
1405 insertShadowCheck(Shadow, Origin, OrigIns);
1457 Type *ShadowTy = getShadowTy(&I);
1459 Value *ShadowPtr, *OriginPtr;
1461 if (PropagateShadow) {
1462 std::tie(ShadowPtr, OriginPtr) =
1463 getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment,
false);
1466 setShadow(&I, getCleanShadow(&I));
1475 if (MS.TrackOrigins) {
1476 if (PropagateShadow) {
1477 unsigned OriginAlignment =
std::max(kMinOriginAlignment, Alignment);
1480 setOrigin(&I, getCleanOrigin());
1496 assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I));
1500 Value *ShadowPtr = getShadowOriginPtr(Addr, IRB, I.
getType(),
1505 insertShadowCheck(Addr, &I);
1510 if (isa<AtomicCmpXchgInst>(I))
1515 setShadow(&I, getCleanShadow(&I));
1516 setOrigin(&I, getCleanOrigin());
1533 setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.
getOperand(1),
1535 setOrigin(&I, getOrigin(&I, 0));
1541 setShadow(&I, IRB.CreateInsertElement(getShadow(&I, 0), getShadow(&I, 1),
1543 setOriginForNaryOp(I);
1549 setShadow(&I, IRB.CreateShuffleVector(getShadow(&I, 0), getShadow(&I, 1),
1551 setOriginForNaryOp(I);
1558 setOrigin(&I, getOrigin(&I, 0));
1564 setOrigin(&I, getOrigin(&I, 0));
1570 setOrigin(&I, getOrigin(&I, 0));
1577 if (
auto *CI = dyn_cast<CallInst>(I.
getOperand(0)))
1578 if (CI->isMustTailCall())
1581 setShadow(&I, IRB.
CreateBitCast(getShadow(&I, 0), getShadowTy(&I)));
1582 setOrigin(&I, getOrigin(&I, 0));
1587 setShadow(&I, IRB.
CreateIntCast(getShadow(&I, 0), getShadowTy(&I),
false,
1588 "_msprop_ptrtoint"));
1589 setOrigin(&I, getOrigin(&I, 0));
1594 setShadow(&I, IRB.
CreateIntCast(getShadow(&I, 0), getShadowTy(&I),
false,
1595 "_msprop_inttoptr"));
1596 setOrigin(&I, getOrigin(&I, 0));
1599 void visitFPToSIInst(
CastInst& I) { handleShadowOr(I); }
1600 void visitFPToUIInst(
CastInst& I) { handleShadowOr(I); }
1601 void visitSIToFPInst(
CastInst& I) { handleShadowOr(I); }
1602 void visitUIToFPInst(
CastInst& I) { handleShadowOr(I); }
1603 void visitFPExtInst(
CastInst& I) { handleShadowOr(I); }
1604 void visitFPTruncInst(
CastInst& I) { handleShadowOr(I); }
1618 Value *S1 = getShadow(&I, 0);
1619 Value *S2 = getShadow(&I, 1);
1630 setOriginForNaryOp(I);
1640 Value *S1 = getShadow(&I, 0);
1641 Value *S2 = getShadow(&I, 1);
1652 setOriginForNaryOp(I);
1670 template <
bool CombineShadow>
1672 Value *Shadow =
nullptr;
1673 Value *Origin =
nullptr;
1675 MemorySanitizerVisitor *MSV;
1679 : IRB(IRB), MSV(MSV) {}
1683 if (CombineShadow) {
1688 OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->
getType());
1689 Shadow = IRB.
CreateOr(Shadow, OpShadow,
"_msprop");
1693 if (MSV->MS.TrackOrigins) {
1700 if (!ConstOrigin || !ConstOrigin->
isNullValue()) {
1701 Value *FlatShadow = MSV->convertToShadowTyNoVec(OpShadow, IRB);
1703 IRB.
CreateICmpNE(FlatShadow, MSV->getCleanShadow(FlatShadow));
1713 Value *OpShadow = MSV->getShadow(V);
1714 Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) :
nullptr;
1715 return Add(OpShadow, OpOrigin);
1721 if (CombineShadow) {
1723 Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(I));
1724 MSV->setShadow(I, Shadow);
1726 if (MSV->MS.TrackOrigins) {
1728 MSV->setOrigin(I, Origin);
1738 if (!MS.TrackOrigins)
return;
1740 OriginCombiner
OC(
this, IRB);
1746 size_t VectorOrPrimitiveTypeSizeInBits(
Type *Ty) {
1748 "Vector of pointers is not a valid shadow type");
1759 size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy);
1760 size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy);
1761 if (srcSizeInBits > 1 && dstSizeInBits == 1)
1778 Type *ShadowTy = getShadowTy(V);
1790 ShadowAndOriginCombiner
SC(
this, IRB);
1813 for (
unsigned Idx = 0; Idx < NumElements; ++Idx) {
1816 const APInt &V = Elt->getValue();
1825 if (
ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg)) {
1826 const APInt &V = Elt->getValue();
1836 IRB.
CreateMul(getShadow(OtherArg), ShadowMul,
"msprop_mul_cst"));
1837 setOrigin(&I, getOrigin(OtherArg));
1843 if (constOp0 && !constOp1)
1844 handleMulByConstant(I, constOp0, I.
getOperand(1));
1845 else if (constOp1 && !constOp0)
1846 handleMulByConstant(I, constOp1, I.
getOperand(0));
1862 setShadow(&I, getShadow(&I, 0));
1863 setOrigin(&I, getOrigin(&I, 0));
1880 void handleEqualityComparison(
ICmpInst &I) {
1884 Value *Sa = getShadow(A);
1885 Value *Sb = getShadow(B);
1911 setOriginForNaryOp(I);
1953 void handleRelationalComparisonExact(
ICmpInst &I) {
1957 Value *Sa = getShadow(A);
1958 Value *Sb = getShadow(B);
1971 getLowestPossibleValue(IRB, A, Sa, IsSigned),
1972 getHighestPossibleValue(IRB, B, Sb, IsSigned));
1974 getHighestPossibleValue(IRB, A, Sa, IsSigned),
1975 getLowestPossibleValue(IRB, B, Sb, IsSigned));
1978 setOriginForNaryOp(I);
1985 void handleSignedRelationalComparison(
ICmpInst &I) {
1989 if ((constOp = dyn_cast<Constant>(I.
getOperand(1)))) {
1992 }
else if ((constOp = dyn_cast<Constant>(I.
getOperand(0)))) {
2007 setShadow(&I, Shadow);
2008 setOrigin(&I, getOrigin(op));
2020 handleEqualityComparison(I);
2026 handleRelationalComparisonExact(I);
2030 handleSignedRelationalComparison(I);
2036 handleRelationalComparisonExact(I);
2051 Value *S1 = getShadow(&I, 0);
2052 Value *S2 = getShadow(&I, 1);
2057 setShadow(&I, IRB.
CreateOr(Shift, S2Conv));
2058 setOriginForNaryOp(I);
2079 {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
2080 IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
2081 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2093 {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
2094 IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
2095 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2104 {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
2105 IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false),
2106 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2111 VAHelper->visitVAStartInst(I);
2115 VAHelper->visitVACopyInst(I);
2125 Value *Shadow = getShadow(&I, 1);
2126 Value *ShadowPtr, *OriginPtr;
2130 std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
2131 Addr, IRB, Shadow->
getType(), 1,
true);
2135 insertShadowCheck(Addr, &I);
2138 if (MS.TrackOrigins) IRB.
CreateStore(getOrigin(&I, 1), OriginPtr);
2150 Type *ShadowTy = getShadowTy(&I);
2151 Value *ShadowPtr, *OriginPtr;
2152 if (PropagateShadow) {
2155 unsigned Alignment = 1;
2156 std::tie(ShadowPtr, OriginPtr) =
2157 getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment,
false);
2160 setShadow(&I, getCleanShadow(&I));
2164 insertShadowCheck(Addr, &I);
2166 if (MS.TrackOrigins) {
2167 if (PropagateShadow)
2170 setOrigin(&I, getCleanOrigin());
2190 for (
unsigned i = 0; i < NumArgOperands; ++i) {
2197 ShadowAndOriginCombiner
SC(
this, IRB);
2198 for (
unsigned i = 0; i < NumArgOperands; ++i)
2217 if (NumArgOperands == 0)
2220 if (NumArgOperands == 2 &&
2226 return handleVectorStoreIntrinsic(I);
2229 if (NumArgOperands == 1 &&
2234 return handleVectorLoadIntrinsic(I);
2238 if (maybeHandleSimpleNomemIntrinsic(I))
2251 setShadow(&I, IRB.
CreateCall(BswapFunc, getShadow(Op)));
2252 setOrigin(&I, getOrigin(Op));
2270 void handleVectorConvertIntrinsic(
IntrinsicInst &I,
int NumUsedElements) {
2272 Value *CopyOp, *ConvertOp;
2297 Value *ConvertShadow = getShadow(ConvertOp);
2298 Value *AggShadow =
nullptr;
2302 for (
int i = 1; i < NumUsedElements; ++i) {
2305 AggShadow = IRB.
CreateOr(AggShadow, MoreShadow);
2308 AggShadow = ConvertShadow;
2311 insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I);
2318 Value *ResultShadow = getShadow(CopyOp);
2320 for (
int i = 0; i < NumUsedElements; ++i) {
2325 setShadow(&I, ResultShadow);
2326 setOrigin(&I, getOrigin(CopyOp));
2328 setShadow(&I, getCleanShadow(&I));
2329 setOrigin(&I, getCleanOrigin());
2337 S = CreateShadowCast(IRB, S, IRB.
getInt64Ty(),
true);
2340 return CreateShadowCast(IRB, S2, T,
true);
2348 return CreateShadowCast(IRB, S2, T,
true);
2365 void handleVectorShiftIntrinsic(
IntrinsicInst &I,
bool Variable) {
2370 Value *S1 = getShadow(&I, 0);
2371 Value *S2 = getShadow(&I, 1);
2372 Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2)
2373 : Lower64ShadowExtend(IRB, S2, getShadowTy(&I));
2377 {IRB.CreateBitCast(S1, V1->
getType()), V2});
2378 Shift = IRB.CreateBitCast(Shift, getShadowTy(&I));
2379 setShadow(&I, IRB.CreateOr(Shift, S2Conv));
2380 setOriginForNaryOp(I);
2384 Type *getMMXVectorTy(
unsigned EltSizeInBits) {
2385 const unsigned X86_MMXSizeInBits = 64;
2387 X86_MMXSizeInBits / EltSizeInBits);
2394 case Intrinsic::x86_sse2_packsswb_128:
2395 case Intrinsic::x86_sse2_packuswb_128:
2396 return Intrinsic::x86_sse2_packsswb_128;
2398 case Intrinsic::x86_sse2_packssdw_128:
2399 case Intrinsic::x86_sse41_packusdw:
2400 return Intrinsic::x86_sse2_packssdw_128;
2402 case Intrinsic::x86_avx2_packsswb:
2403 case Intrinsic::x86_avx2_packuswb:
2404 return Intrinsic::x86_avx2_packsswb;
2406 case Intrinsic::x86_avx2_packssdw:
2407 case Intrinsic::x86_avx2_packusdw:
2408 return Intrinsic::x86_avx2_packssdw;
2410 case Intrinsic::x86_mmx_packsswb:
2411 case Intrinsic::x86_mmx_packuswb:
2412 return Intrinsic::x86_mmx_packsswb;
2414 case Intrinsic::x86_mmx_packssdw:
2415 return Intrinsic::x86_mmx_packssdw;
2428 void handleVectorPackIntrinsic(
IntrinsicInst &I,
unsigned EltSizeInBits = 0) {
2432 Value *S1 = getShadow(&I, 0);
2433 Value *S2 = getShadow(&I, 1);
2439 Type *T = isX86_MMX ? getMMXVectorTy(EltSizeInBits) : S1->
getType();
2458 IRB.
CreateCall(ShadowFn, {S1_ext, S2_ext},
"_msprop_vector_pack");
2461 setOriginForNaryOp(I);
2466 const unsigned SignificantBitsPerResultElement = 16;
2469 unsigned ZeroBitsPerResultElement =
2473 Value *S = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2477 S = IRB.
CreateLShr(S, ZeroBitsPerResultElement);
2480 setOriginForNaryOp(I);
2485 unsigned EltSizeInBits = 0) {
2487 Type *ResTy = isX86_MMX ? getMMXVectorTy(EltSizeInBits * 2) : I.
getType();
2489 Value *S = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2495 setOriginForNaryOp(I);
2503 Type *ResTy = getShadowTy(&I);
2504 Value *S0 = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2508 setOriginForNaryOp(I);
2516 Value *S0 = IRB.
CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2517 Value *S = LowerElementShadowExtend(IRB, S0, getShadowTy(&I));
2519 setOriginForNaryOp(I);
2527 getShadowOriginPtr(Addr, IRB, Ty, 1,
true)
2534 insertShadowCheck(Addr, &I);
2538 if (!InsertChecks)
return;
2543 unsigned Alignment = 1;
2544 Value *ShadowPtr, *OriginPtr;
2545 std::tie(ShadowPtr, OriginPtr) =
2546 getShadowOriginPtr(Addr, IRB, Ty, Alignment,
false);
2549 insertShadowCheck(Addr, &I);
2553 MS.TrackOrigins ? IRB.
CreateLoad(OriginPtr) : getCleanOrigin();
2554 insertShadowCheck(Shadow, Origin, &I);
2563 Value *Shadow = getShadow(V);
2567 std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
2571 insertShadowCheck(Addr, &I);
2574 insertShadowCheck(Mask, &I);
2579 if (MS.TrackOrigins) {
2581 paintOrigin(IRB, getOrigin(V), OriginPtr,
2582 DL.getTypeStoreSize(Shadow->
getType()),
2583 std::max(Align, kMinOriginAlignment));
2594 Type *ShadowTy = getShadowTy(&I);
2595 Value *ShadowPtr, *OriginPtr;
2596 if (PropagateShadow) {
2597 std::tie(ShadowPtr, OriginPtr) =
2598 getShadowOriginPtr(Addr, IRB, ShadowTy, Align,
false);
2600 getShadow(PassThru),
"_msmaskedld"));
2602 setShadow(&I, getCleanShadow(&I));
2606 insertShadowCheck(Addr, &I);
2607 insertShadowCheck(Mask, &I);
2610 if (MS.TrackOrigins) {
2611 if (PropagateShadow) {
2627 getOrigin(PassThru), IRB.
CreateLoad(OriginPtr));
2629 setOrigin(&I, Origin);
2631 setOrigin(&I, getCleanOrigin());
2640 case Intrinsic::bswap:
2643 case Intrinsic::masked_store:
2644 handleMaskedStore(I);
2646 case Intrinsic::masked_load:
2647 handleMaskedLoad(I);
2649 case Intrinsic::x86_sse_stmxcsr:
2652 case Intrinsic::x86_sse_ldmxcsr:
2655 case Intrinsic::x86_avx512_vcvtsd2usi64:
2656 case Intrinsic::x86_avx512_vcvtsd2usi32:
2657 case Intrinsic::x86_avx512_vcvtss2usi64:
2658 case Intrinsic::x86_avx512_vcvtss2usi32:
2659 case Intrinsic::x86_avx512_cvttss2usi64:
2660 case Intrinsic::x86_avx512_cvttss2usi:
2661 case Intrinsic::x86_avx512_cvttsd2usi64:
2662 case Intrinsic::x86_avx512_cvttsd2usi:
2663 case Intrinsic::x86_avx512_cvtusi2ss:
2664 case Intrinsic::x86_avx512_cvtusi642sd:
2665 case Intrinsic::x86_avx512_cvtusi642ss:
2666 case Intrinsic::x86_sse2_cvtsd2si64:
2667 case Intrinsic::x86_sse2_cvtsd2si:
2668 case Intrinsic::x86_sse2_cvtsd2ss:
2669 case Intrinsic::x86_sse2_cvttsd2si64:
2670 case Intrinsic::x86_sse2_cvttsd2si:
2671 case Intrinsic::x86_sse_cvtss2si64:
2672 case Intrinsic::x86_sse_cvtss2si:
2673 case Intrinsic::x86_sse_cvttss2si64:
2674 case Intrinsic::x86_sse_cvttss2si:
2675 handleVectorConvertIntrinsic(I, 1);
2677 case Intrinsic::x86_sse_cvtps2pi:
2678 case Intrinsic::x86_sse_cvttps2pi:
2679 handleVectorConvertIntrinsic(I, 2);
2682 case Intrinsic::x86_avx512_psll_w_512:
2683 case Intrinsic::x86_avx512_psll_d_512:
2684 case Intrinsic::x86_avx512_psll_q_512:
2685 case Intrinsic::x86_avx512_pslli_w_512:
2686 case Intrinsic::x86_avx512_pslli_d_512:
2687 case Intrinsic::x86_avx512_pslli_q_512:
2688 case Intrinsic::x86_avx512_psrl_w_512:
2689 case Intrinsic::x86_avx512_psrl_d_512:
2690 case Intrinsic::x86_avx512_psrl_q_512:
2691 case Intrinsic::x86_avx512_psra_w_512:
2692 case Intrinsic::x86_avx512_psra_d_512:
2693 case Intrinsic::x86_avx512_psra_q_512:
2694 case Intrinsic::x86_avx512_psrli_w_512:
2695 case Intrinsic::x86_avx512_psrli_d_512:
2696 case Intrinsic::x86_avx512_psrli_q_512:
2697 case Intrinsic::x86_avx512_psrai_w_512:
2698 case Intrinsic::x86_avx512_psrai_d_512:
2699 case Intrinsic::x86_avx512_psrai_q_512:
2700 case Intrinsic::x86_avx512_psra_q_256:
2701 case Intrinsic::x86_avx512_psra_q_128:
2702 case Intrinsic::x86_avx512_psrai_q_256:
2703 case Intrinsic::x86_avx512_psrai_q_128:
2704 case Intrinsic::x86_avx2_psll_w:
2705 case Intrinsic::x86_avx2_psll_d:
2706 case Intrinsic::x86_avx2_psll_q:
2707 case Intrinsic::x86_avx2_pslli_w:
2708 case Intrinsic::x86_avx2_pslli_d:
2709 case Intrinsic::x86_avx2_pslli_q:
2710 case Intrinsic::x86_avx2_psrl_w:
2711 case Intrinsic::x86_avx2_psrl_d:
2712 case Intrinsic::x86_avx2_psrl_q:
2713 case Intrinsic::x86_avx2_psra_w:
2714 case Intrinsic::x86_avx2_psra_d:
2715 case Intrinsic::x86_avx2_psrli_w:
2716 case Intrinsic::x86_avx2_psrli_d:
2717 case Intrinsic::x86_avx2_psrli_q:
2718 case Intrinsic::x86_avx2_psrai_w:
2719 case Intrinsic::x86_avx2_psrai_d:
2720 case Intrinsic::x86_sse2_psll_w:
2721 case Intrinsic::x86_sse2_psll_d:
2722 case Intrinsic::x86_sse2_psll_q:
2723 case Intrinsic::x86_sse2_pslli_w:
2724 case Intrinsic::x86_sse2_pslli_d:
2725 case Intrinsic::x86_sse2_pslli_q:
2726 case Intrinsic::x86_sse2_psrl_w:
2727 case Intrinsic::x86_sse2_psrl_d:
2728 case Intrinsic::x86_sse2_psrl_q:
2729 case Intrinsic::x86_sse2_psra_w:
2730 case Intrinsic::x86_sse2_psra_d:
2731 case Intrinsic::x86_sse2_psrli_w:
2732 case Intrinsic::x86_sse2_psrli_d:
2733 case Intrinsic::x86_sse2_psrli_q:
2734 case Intrinsic::x86_sse2_psrai_w:
2735 case Intrinsic::x86_sse2_psrai_d:
2736 case Intrinsic::x86_mmx_psll_w:
2737 case Intrinsic::x86_mmx_psll_d:
2738 case Intrinsic::x86_mmx_psll_q:
2739 case Intrinsic::x86_mmx_pslli_w:
2740 case Intrinsic::x86_mmx_pslli_d:
2741 case Intrinsic::x86_mmx_pslli_q:
2742 case Intrinsic::x86_mmx_psrl_w:
2743 case Intrinsic::x86_mmx_psrl_d:
2744 case Intrinsic::x86_mmx_psrl_q:
2745 case Intrinsic::x86_mmx_psra_w:
2746 case Intrinsic::x86_mmx_psra_d:
2747 case Intrinsic::x86_mmx_psrli_w:
2748 case Intrinsic::x86_mmx_psrli_d:
2749 case Intrinsic::x86_mmx_psrli_q:
2750 case Intrinsic::x86_mmx_psrai_w:
2751 case Intrinsic::x86_mmx_psrai_d:
2752 handleVectorShiftIntrinsic(I,
false);
2754 case Intrinsic::x86_avx2_psllv_d:
2755 case Intrinsic::x86_avx2_psllv_d_256:
2756 case Intrinsic::x86_avx512_psllv_d_512:
2757 case Intrinsic::x86_avx2_psllv_q:
2758 case Intrinsic::x86_avx2_psllv_q_256:
2759 case Intrinsic::x86_avx512_psllv_q_512:
2760 case Intrinsic::x86_avx2_psrlv_d:
2761 case Intrinsic::x86_avx2_psrlv_d_256:
2762 case Intrinsic::x86_avx512_psrlv_d_512:
2763 case Intrinsic::x86_avx2_psrlv_q:
2764 case Intrinsic::x86_avx2_psrlv_q_256:
2765 case Intrinsic::x86_avx512_psrlv_q_512:
2766 case Intrinsic::x86_avx2_psrav_d:
2767 case Intrinsic::x86_avx2_psrav_d_256:
2768 case Intrinsic::x86_avx512_psrav_d_512:
2769 case Intrinsic::x86_avx512_psrav_q_128:
2770 case Intrinsic::x86_avx512_psrav_q_256:
2771 case Intrinsic::x86_avx512_psrav_q_512:
2772 handleVectorShiftIntrinsic(I,
true);
2775 case Intrinsic::x86_sse2_packsswb_128:
2776 case Intrinsic::x86_sse2_packssdw_128:
2777 case Intrinsic::x86_sse2_packuswb_128:
2778 case Intrinsic::x86_sse41_packusdw:
2779 case Intrinsic::x86_avx2_packsswb:
2780 case Intrinsic::x86_avx2_packssdw:
2781 case Intrinsic::x86_avx2_packuswb:
2782 case Intrinsic::x86_avx2_packusdw:
2783 handleVectorPackIntrinsic(I);
2786 case Intrinsic::x86_mmx_packsswb:
2787 case Intrinsic::x86_mmx_packuswb:
2788 handleVectorPackIntrinsic(I, 16);
2791 case Intrinsic::x86_mmx_packssdw:
2792 handleVectorPackIntrinsic(I, 32);
2795 case Intrinsic::x86_mmx_psad_bw:
2796 case Intrinsic::x86_sse2_psad_bw:
2797 case Intrinsic::x86_avx2_psad_bw:
2798 handleVectorSadIntrinsic(I);
2801 case Intrinsic::x86_sse2_pmadd_wd:
2802 case Intrinsic::x86_avx2_pmadd_wd:
2803 case Intrinsic::x86_ssse3_pmadd_ub_sw_128:
2804 case Intrinsic::x86_avx2_pmadd_ub_sw:
2805 handleVectorPmaddIntrinsic(I);
2808 case Intrinsic::x86_ssse3_pmadd_ub_sw:
2809 handleVectorPmaddIntrinsic(I, 8);
2812 case Intrinsic::x86_mmx_pmadd_wd:
2813 handleVectorPmaddIntrinsic(I, 16);
2816 case Intrinsic::x86_sse_cmp_ss:
2817 case Intrinsic::x86_sse2_cmp_sd:
2818 case Intrinsic::x86_sse_comieq_ss:
2819 case Intrinsic::x86_sse_comilt_ss:
2820 case Intrinsic::x86_sse_comile_ss:
2821 case Intrinsic::x86_sse_comigt_ss:
2822 case Intrinsic::x86_sse_comige_ss:
2823 case Intrinsic::x86_sse_comineq_ss:
2824 case Intrinsic::x86_sse_ucomieq_ss:
2825 case Intrinsic::x86_sse_ucomilt_ss:
2826 case Intrinsic::x86_sse_ucomile_ss:
2827 case Intrinsic::x86_sse_ucomigt_ss:
2828 case Intrinsic::x86_sse_ucomige_ss:
2829 case Intrinsic::x86_sse_ucomineq_ss:
2830 case Intrinsic::x86_sse2_comieq_sd:
2831 case Intrinsic::x86_sse2_comilt_sd:
2832 case Intrinsic::x86_sse2_comile_sd:
2833 case Intrinsic::x86_sse2_comigt_sd:
2834 case Intrinsic::x86_sse2_comige_sd:
2835 case Intrinsic::x86_sse2_comineq_sd:
2836 case Intrinsic::x86_sse2_ucomieq_sd:
2837 case Intrinsic::x86_sse2_ucomilt_sd:
2838 case Intrinsic::x86_sse2_ucomile_sd:
2839 case Intrinsic::x86_sse2_ucomigt_sd:
2840 case Intrinsic::x86_sse2_ucomige_sd:
2841 case Intrinsic::x86_sse2_ucomineq_sd:
2842 handleVectorCompareScalarIntrinsic(I);
2845 case Intrinsic::x86_sse_cmp_ps:
2846 case Intrinsic::x86_sse2_cmp_pd:
2850 handleVectorComparePackedIntrinsic(I);
2854 if (!handleUnknownIntrinsic(I))
2855 visitInstruction(I);
2872 visitAsmInstruction(I);
2874 visitInstruction(I);
2878 assert(!isa<IntrinsicInst>(&I) &&
"intrinsics are handled elsewhere");
2896 unsigned ArgOffset = 0;
2899 ArgIt != End; ++ArgIt) {
2903 LLVM_DEBUG(
dbgs() <<
"Arg " << i <<
" is not sized: " << I <<
"\n");
2911 Value *ArgShadow = getShadow(A);
2912 Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset);
2914 <<
" Shadow: " << *ArgShadow <<
"\n");
2915 bool ArgIsInitialized =
false;
2919 "ByVal argument is not a pointer!");
2921 if (ArgOffset + Size > kParamTLSSize)
break;
2923 unsigned Alignment = std::min(ParamAlignment, kShadowTLSAlignment);
2928 Store = IRB.
CreateMemCpy(ArgShadowBase, Alignment, AShadowPtr,
2932 if (ArgOffset + Size > kParamTLSSize)
break;
2934 kShadowTLSAlignment);
2936 if (Cst && Cst->
isNullValue()) ArgIsInitialized =
true;
2938 if (MS.TrackOrigins && !ArgIsInitialized)
2940 getOriginPtrForArgument(A, IRB, ArgOffset));
2942 assert(Size != 0 && Store !=
nullptr);
2944 ArgOffset +=
alignTo(Size, 8);
2951 VAHelper->visitCallSite(CS, IRB);
2957 if (CS.
isCall() && cast<CallInst>(&
I)->isMustTailCall())
return;
2960 Value *
Base = getShadowPtrForRetval(&I, IRBBefore);
2967 BasicBlock *NormalDest = cast<InvokeInst>(&
I)->getNormalDest();
2972 setShadow(&I, getCleanShadow(&I));
2973 setOrigin(&I, getCleanOrigin());
2980 "Could not find insertion point for retval shadow load");
2983 Value *RetvalShadow =
2984 IRBAfter.CreateAlignedLoad(getShadowPtrForRetval(&I, IRBAfter),
2985 kShadowTLSAlignment,
"_msret");
2986 setShadow(&I, RetvalShadow);
2987 if (MS.TrackOrigins)
2988 setOrigin(&I, IRBAfter.CreateLoad(getOriginPtrForRetval(IRBAfter)));
2991 bool isAMustTailRetVal(
Value *RetVal) {
2992 if (
auto *I = dyn_cast<BitCastInst>(RetVal)) {
2995 if (
auto *I = dyn_cast<CallInst>(RetVal)) {
3004 if (!RetVal)
return;
3006 if (isAMustTailRetVal(RetVal))
return;
3007 Value *ShadowPtr = getShadowPtrForRetval(RetVal, IRB);
3008 if (CheckReturnValue) {
3009 insertShadowCheck(RetVal, &I);
3010 Value *Shadow = getCleanShadow(RetVal);
3013 Value *Shadow = getShadow(RetVal);
3015 if (MS.TrackOrigins)
3016 IRB.
CreateStore(getOrigin(RetVal), getOriginPtrForRetval(IRB));
3020 void visitPHINode(
PHINode &I) {
3022 if (!PropagateShadow) {
3023 setShadow(&I, getCleanShadow(&I));
3024 setOrigin(&I, getCleanOrigin());
3031 if (MS.TrackOrigins)
3037 setShadow(&I, getCleanShadow(&I));
3038 setOrigin(&I, getCleanOrigin());
3047 {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len});
3057 if (PoisonStack && MS.TrackOrigins) {
3068 StackDescription.
str());
3071 {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len,
3072 IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy()),
3073 IRB.CreatePointerCast(&F, MS.IntptrTy)});
3083 Value *Sb = getShadow(B);
3084 Value *Sc = getShadow(C);
3085 Value *Sd = getShadow(D);
3094 Sa1 = getPoisonedShadow(getShadowTy(I.
getType()));
3102 C = CreateAppToShadowCast(IRB, C);
3103 D = CreateAppToShadowCast(IRB, D);
3110 if (MS.TrackOrigins) {
3132 setShadow(&I, getCleanShadow(&I));
3133 setOrigin(&I, getCleanOrigin());
3137 setShadow(&I, getCleanShadow(&I));
3138 setOrigin(&I, getCleanOrigin());
3142 setShadow(&I, getCleanShadow(&I));
3143 setOrigin(&I, getCleanOrigin());
3154 Value *AggShadow = getShadow(Agg);
3158 setShadow(&I, ResShadow);
3159 setOriginForNaryOp(I);
3172 setOriginForNaryOp(I);
3176 if (
CallInst *CI = dyn_cast<CallInst>(&I)) {
3177 errs() <<
"ZZZ call " << CI->getCalledFunction()->getName() <<
"\n";
3181 errs() <<
"QQQ " << I <<
"\n";
3208 insertShadowCheck(Operand, &I);
3210 setShadow(&I, getCleanShadow(&I));
3211 setOrigin(&I, getCleanOrigin());
3222 Value *ShadowPtr, *OriginPtr;
3223 std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
3224 Operand, IRB, ElType, 1,
true);
3225 Value *CShadow = getCleanShadow(ElType);
3240 insertShadowCheck(Operand, &I);
3242 setShadow(&I, getCleanShadow(&I));
3243 setOrigin(&I, getCleanOrigin());
3248 struct VarArgAMD64Helper :
public VarArgHelper {
3251 static const unsigned AMD64GpEndOffset = 48;
3252 static const unsigned AMD64FpEndOffset = 176;
3255 MemorySanitizer &MS;
3256 MemorySanitizerVisitor &MSV;
3257 Value *VAArgTLSCopy =
nullptr;
3258 Value *VAArgOverflowSize =
nullptr;
3262 enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
3264 VarArgAMD64Helper(
Function &F, MemorySanitizer &MS,
3265 MemorySanitizerVisitor &MSV) :
F(F), MS(MS), MSV(MSV) {}
3267 ArgKind classifyArgument(
Value* arg) {
3271 return AK_FloatingPoint;
3273 return AK_GeneralPurpose;
3275 return AK_GeneralPurpose;
3288 unsigned GpOffset = 0;
3289 unsigned FpOffset = AMD64GpEndOffset;
3290 unsigned OverflowOffset = AMD64FpEndOffset;
3293 ArgIt != End; ++ArgIt) {
3297 bool IsByVal = CS.
paramHasAttr(ArgNo, Attribute::ByVal);
3308 getShadowPtrForVAArgument(RealTy, IRB, OverflowOffset);
3309 OverflowOffset +=
alignTo(ArgSize, 8);
3310 Value *ShadowPtr, *OriginPtr;
3311 std::tie(ShadowPtr, OriginPtr) =
3315 IRB.
CreateMemCpy(ShadowBase, kShadowTLSAlignment, ShadowPtr,
3316 kShadowTLSAlignment, ArgSize);
3318 ArgKind AK = classifyArgument(A);
3319 if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset)
3321 if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset)
3325 case AK_GeneralPurpose:
3326 ShadowBase = getShadowPtrForVAArgument(A->
getType(), IRB, GpOffset);
3329 case AK_FloatingPoint:
3330 ShadowBase = getShadowPtrForVAArgument(A->
getType(), IRB, FpOffset);
3338 getShadowPtrForVAArgument(A->
getType(), IRB, OverflowOffset);
3339 OverflowOffset +=
alignTo(ArgSize, 8);
3351 IRB.
CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
3366 Value *ShadowPtr, *OriginPtr;
3367 unsigned Alignment = 8;
3368 std::tie(ShadowPtr, OriginPtr) =
3369 MSV.getShadowOriginPtr(VAListTag, IRB, IRB.
getInt8Ty(), Alignment,
3375 24, Alignment,
false);
3383 VAStartInstrumentationList.
push_back(&I);
3384 unpoisonVAListTagForInst(I);
3387 void visitVACopyInst(
VACopyInst &I)
override {
3389 unpoisonVAListTagForInst(I);
3392 void finalizeInstrumentation()
override {
3393 assert(!VAArgOverflowSize && !VAArgTLSCopy &&
3394 "finalizeInstrumentation called twice");
3395 if (!VAStartInstrumentationList.
empty()) {
3398 IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
3399 VAArgOverflowSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3404 IRB.
CreateMemCpy(VAArgTLSCopy, 8, MS.VAArgTLS, 8, CopySize);
3409 for (
size_t i = 0, n = VAStartInstrumentationList.
size(); i < n; i++) {
3410 CallInst *OrigInst = VAStartInstrumentationList[i];
3419 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
3420 unsigned Alignment = 16;
3421 std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
3422 MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.
getInt8Ty(),
3424 IRB.
CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
3431 Value *OverflowArgAreaShadowPtr, *OverflowArgAreaOriginPtr;
3432 std::tie(OverflowArgAreaShadowPtr, OverflowArgAreaOriginPtr) =
3433 MSV.getShadowOriginPtr(OverflowArgAreaPtr, IRB, IRB.
getInt8Ty(),
3437 IRB.
CreateMemCpy(OverflowArgAreaShadowPtr, Alignment, SrcPtr, Alignment,
3444 struct VarArgMIPS64Helper :
public VarArgHelper {
3446 MemorySanitizer &MS;
3447 MemorySanitizerVisitor &MSV;
3448 Value *VAArgTLSCopy =
nullptr;
3449 Value *VAArgSize =
nullptr;
3453 VarArgMIPS64Helper(
Function &F, MemorySanitizer &MS,
3454 MemorySanitizerVisitor &MSV) :
F(F), MS(MS), MSV(MSV) {}
3457 unsigned VAArgOffset = 0;
3461 ArgIt != End; ++ArgIt) {
3470 VAArgOffset += (8 - ArgSize);
3472 Base = getShadowPtrForVAArgument(A->getType(), IRB, VAArgOffset);
3473 VAArgOffset += ArgSize;
3474 VAArgOffset =
alignTo(VAArgOffset, 8);
3481 IRB.
CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
3495 VAStartInstrumentationList.
push_back(&I);
3497 Value *ShadowPtr, *OriginPtr;
3498 unsigned Alignment = 8;
3499 std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
3500 VAListTag, IRB, IRB.
getInt8Ty(), Alignment,
true);
3502 8, Alignment,
false);
3505 void visitVACopyInst(
VACopyInst &I)
override {
3507 VAStartInstrumentationList.
push_back(&I);
3509 Value *ShadowPtr, *OriginPtr;
3510 unsigned Alignment = 8;
3511 std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
3512 VAListTag, IRB, IRB.
getInt8Ty(), Alignment,
true);
3514 8, Alignment,
false);
3517 void finalizeInstrumentation()
override {
3518 assert(!VAArgSize && !VAArgTLSCopy &&
3519 "finalizeInstrumentation called twice");
3520 IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
3521 VAArgSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3525 if (!VAStartInstrumentationList.
empty()) {
3529 IRB.
CreateMemCpy(VAArgTLSCopy, 8, MS.VAArgTLS, 8, CopySize);
3534 for (
size_t i = 0, n = VAStartInstrumentationList.
size(); i < n; i++) {
3535 CallInst *OrigInst = VAStartInstrumentationList[i];
3538 Value *RegSaveAreaPtrPtr =
3542 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
3543 unsigned Alignment = 8;
3544 std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
3545 MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.
getInt8Ty(),
3547 IRB.
CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
3554 struct VarArgAArch64Helper :
public VarArgHelper {
3555 static const unsigned kAArch64GrArgSize = 64;
3556 static const unsigned kAArch64VrArgSize = 128;
3558 static const unsigned AArch64GrBegOffset = 0;
3559 static const unsigned AArch64GrEndOffset = kAArch64GrArgSize;
3561 static const unsigned AArch64VrBegOffset = AArch64GrEndOffset;
3562 static const unsigned AArch64VrEndOffset = AArch64VrBegOffset
3563 + kAArch64VrArgSize;
3564 static const unsigned AArch64VAEndOffset = AArch64VrEndOffset;
3567 MemorySanitizer &MS;
3568 MemorySanitizerVisitor &MSV;
3569 Value *VAArgTLSCopy =
nullptr;
3570 Value *VAArgOverflowSize =
nullptr;
3574 enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
3576 VarArgAArch64Helper(
Function &F, MemorySanitizer &MS,
3577 MemorySanitizerVisitor &MSV) :
F(F), MS(MS), MSV(MSV) {}
3579 ArgKind classifyArgument(
Value* arg) {
3582 return AK_FloatingPoint;
3585 return AK_GeneralPurpose;
3599 unsigned GrOffset = AArch64GrBegOffset;
3600 unsigned VrOffset = AArch64VrBegOffset;
3601 unsigned OverflowOffset = AArch64VAEndOffset;
3605 ArgIt != End; ++ArgIt) {
3609 ArgKind AK = classifyArgument(A);
3610 if (AK == AK_GeneralPurpose && GrOffset >= AArch64GrEndOffset)
3612 if (AK == AK_FloatingPoint && VrOffset >= AArch64VrEndOffset)
3616 case AK_GeneralPurpose:
3617 Base = getShadowPtrForVAArgument(A->
getType(), IRB, GrOffset);
3620 case AK_FloatingPoint:
3621 Base = getShadowPtrForVAArgument(A->
getType(), IRB, VrOffset);
3630 Base = getShadowPtrForVAArgument(A->
getType(), IRB, OverflowOffset);
3631 OverflowOffset +=
alignTo(ArgSize, 8);
3642 IRB.
CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
3656 VAStartInstrumentationList.
push_back(&I);
3658 Value *ShadowPtr, *OriginPtr;
3659 unsigned Alignment = 8;
3660 std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
3661 VAListTag, IRB, IRB.
getInt8Ty(), Alignment,
true);
3663 32, Alignment,
false);
3666 void visitVACopyInst(
VACopyInst &I)
override {
3668 VAStartInstrumentationList.
push_back(&I);
3670 Value *ShadowPtr, *OriginPtr;
3671 unsigned Alignment = 8;
3672 std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
3673 VAListTag, IRB, IRB.
getInt8Ty(), Alignment,
true);
3675 32, Alignment,
false);
3680 Value *SaveAreaPtrPtr =
3690 Value *SaveAreaPtr =
3696 return IRB.
CreateSExt(SaveArea32, MS.IntptrTy);
3699 void finalizeInstrumentation()
override {
3700 assert(!VAArgOverflowSize && !VAArgTLSCopy &&
3701 "finalizeInstrumentation called twice");
3702 if (!VAStartInstrumentationList.
empty()) {
3705 IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
3706 VAArgOverflowSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3711 IRB.
CreateMemCpy(VAArgTLSCopy, 8, MS.VAArgTLS, 8, CopySize);
3719 for (
size_t i = 0, n = VAStartInstrumentationList.
size(); i < n; i++) {
3720 CallInst *OrigInst = VAStartInstrumentationList[i];
3739 Value *StackSaveAreaPtr = getVAField64(IRB, VAListTag, 0);
3742 Value *GrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 8);
3743 Value *GrOffSaveArea = getVAField32(IRB, VAListTag, 24);
3745 Value *GrRegSaveAreaPtr = IRB.
CreateAdd(GrTopSaveAreaPtr, GrOffSaveArea);
3748 Value *VrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 16);
3749 Value *VrOffSaveArea = getVAField32(IRB, VAListTag, 28);
3751 Value *VrRegSaveAreaPtr = IRB.
CreateAdd(VrTopSaveAreaPtr, VrOffSaveArea);
3757 Value *GrRegSaveAreaShadowPtrOff =
3758 IRB.
CreateAdd(GrArgSize, GrOffSaveArea);
3760 Value *GrRegSaveAreaShadowPtr =
3761 MSV.getShadowOriginPtr(GrRegSaveAreaPtr, IRB, IRB.
getInt8Ty(),
3766 GrRegSaveAreaShadowPtrOff);
3767 Value *GrCopySize = IRB.
CreateSub(GrArgSize, GrRegSaveAreaShadowPtrOff);
3769 IRB.
CreateMemCpy(GrRegSaveAreaShadowPtr, 8, GrSrcPtr, 8, GrCopySize);
3772 Value *VrRegSaveAreaShadowPtrOff =
3773 IRB.
CreateAdd(VrArgSize, VrOffSaveArea);
3775 Value *VrRegSaveAreaShadowPtr =
3776 MSV.getShadowOriginPtr(VrRegSaveAreaPtr, IRB, IRB.
getInt8Ty(),
3784 VrRegSaveAreaShadowPtrOff);
3785 Value *VrCopySize = IRB.
CreateSub(VrArgSize, VrRegSaveAreaShadowPtrOff);
3787 IRB.
CreateMemCpy(VrRegSaveAreaShadowPtr, 8, VrSrcPtr, 8, VrCopySize);
3790 Value *StackSaveAreaShadowPtr =
3791 MSV.getShadowOriginPtr(StackSaveAreaPtr, IRB, IRB.
getInt8Ty(),
3795 Value *StackSrcPtr =
3799 IRB.
CreateMemCpy(StackSaveAreaShadowPtr, 16, StackSrcPtr, 16,
3806 struct VarArgPowerPC64Helper :
public VarArgHelper {
3808 MemorySanitizer &MS;
3809 MemorySanitizerVisitor &MSV;
3810 Value *VAArgTLSCopy =
nullptr;
3811 Value *VAArgSize =
nullptr;
3815 VarArgPowerPC64Helper(
Function &F, MemorySanitizer &MS,
3816 MemorySanitizerVisitor &MSV) :
F(F), MS(MS), MSV(MSV) {}
3835 unsigned VAArgOffset = VAArgBase;
3838 ArgIt != End; ++ArgIt) {
3842 bool IsByVal = CS.
paramHasAttr(ArgNo, Attribute::ByVal);
3850 VAArgOffset =
alignTo(VAArgOffset, ArgAlign);
3852 Value *
Base = getShadowPtrForVAArgument(RealTy, IRB,
3853 VAArgOffset - VAArgBase);
3854 Value *AShadowPtr, *AOriginPtr;
3855 std::tie(AShadowPtr, AOriginPtr) = MSV.getShadowOriginPtr(
3858 IRB.
CreateMemCpy(Base, kShadowTLSAlignment, AShadowPtr,
3859 kShadowTLSAlignment, ArgSize);
3861 VAArgOffset +=
alignTo(ArgSize, 8);
3865 uint64_t ArgAlign = 8;
3878 VAArgOffset =
alignTo(VAArgOffset, ArgAlign);
3883 VAArgOffset += (8 - ArgSize);
3886 Base = getShadowPtrForVAArgument(A->
getType(), IRB,
3887 VAArgOffset - VAArgBase);
3890 VAArgOffset += ArgSize;
3891 VAArgOffset =
alignTo(VAArgOffset, 8);
3894 VAArgBase = VAArgOffset;
3898 VAArgOffset - VAArgBase);
3901 IRB.
CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
3915 VAStartInstrumentationList.
push_back(&I);
3917 Value *ShadowPtr, *OriginPtr;
3918 unsigned Alignment = 8;
3919 std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
3920 VAListTag, IRB, IRB.
getInt8Ty(), Alignment,
true);
3922 8, Alignment,
false);
3925 void visitVACopyInst(
VACopyInst &I)
override {
3928 Value *ShadowPtr, *OriginPtr;
3929 unsigned Alignment = 8;
3930 std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
3931 VAListTag, IRB, IRB.
getInt8Ty(), Alignment,
true);
3935 8, Alignment,
false);
3938 void finalizeInstrumentation()
override {
3939 assert(!VAArgSize && !VAArgTLSCopy &&
3940 "finalizeInstrumentation called twice");
3941 IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
3942 VAArgSize = IRB.
CreateLoad(MS.VAArgOverflowSizeTLS);
3946 if (!VAStartInstrumentationList.
empty()) {
3950 IRB.
CreateMemCpy(VAArgTLSCopy, 8, MS.VAArgTLS, 8, CopySize);
3955 for (
size_t i = 0, n = VAStartInstrumentationList.
size(); i < n; i++) {
3956 CallInst *OrigInst = VAStartInstrumentationList[i];
3959 Value *RegSaveAreaPtrPtr =
3963 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
3964 unsigned Alignment = 8;
3965 std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
3966 MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.
getInt8Ty(),
3968 IRB.
CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
3975 struct VarArgNoOpHelper :
public VarArgHelper {
3976 VarArgNoOpHelper(
Function &
F, MemorySanitizer &MS,
3977 MemorySanitizerVisitor &MSV) {}
3983 void visitVACopyInst(
VACopyInst &I)
override {}
3985 void finalizeInstrumentation()
override {}
3991 MemorySanitizerVisitor &Visitor) {
3996 return new VarArgAMD64Helper(Func, Msan, Visitor);
3997 else if (TargetTriple.isMIPS64())
3998 return new VarArgMIPS64Helper(Func, Msan, Visitor);
4000 return new VarArgAArch64Helper(Func, Msan, Visitor);
4003 return new VarArgPowerPC64Helper(Func, Msan, Visitor);
4005 return new VarArgNoOpHelper(Func, Msan, Visitor);
4009 if (&F == MsanCtorFunction)
4011 MemorySanitizerVisitor Visitor(F, *
this);
4019 return Visitor.runOnFunction();
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Type * getVectorElementType() const
Return a value (possibly void), from a function.
User::op_iterator arg_iterator
The type of iterator to use when looping over actual arguments at this call site. ...
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. ...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
static const MemoryMapParams Linux_PowerPC64_MemoryMapParams
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
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.
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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.
static const PlatformMemoryMapParams Linux_PowerPC_MemoryMapParams
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="")
Atomic ordering constants.
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.
Compute iterated dominance frontiers using a linear time algorithm.
BinaryOps getOpcode() const
unsigned getParamAlignment(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
bool isAtomic() const
Return true if this instruction has an AtomicOrdering of unordered or higher.
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. ...
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this rmw instruction.
static const MemoryMapParams Linux_I386_MemoryMapParams
Same, but only replaced by something equivalent.
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
static const MemoryMapParams NetBSD_X86_64_MemoryMapParams
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.
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.
static PointerType * getInt32PtrTy(LLVMContext &C, unsigned AS=0)
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...
const Value * getTrueValue() const
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Like Internal, but omit from symbol table.
static VarArgHelper * CreateVarArgHelper(Function &Func, MemorySanitizer &Msan, MemorySanitizerVisitor &Visitor)
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.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
A raw_ostream that writes to an SmallVector or SmallString.
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="")
static unsigned TypeSizeToSizeIndex(unsigned TypeSize)
This class represents a sign extension of integer types.
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
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.
bool isVectorTy() const
True if this is an instance of VectorType.
bool isMustTailCall() const
static Type * getX86_MMXTy(LLVMContext &C)
static cl::opt< unsigned long long > ClXorMask("msan-xor-mask", cl::desc("Define custom MSan XorMask"), cl::Hidden, cl::init(0))
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
static const MemoryMapParams Linux_AArch64_MemoryMapParams
static cl::opt< bool > ClHandleICmp("msan-handle-icmp", cl::desc("propagate shadow through ICmpEQ and ICmpNE"), cl::Hidden, cl::init(true))
static PointerType * getInt64PtrTy(LLVMContext &C, unsigned AS=0)
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
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.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
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.
Type * getPointerElementType() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
unsigned getAlignment() const
Return the alignment of the memory that is being allocated by the instruction.
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.
LLVMContext & getContext() const
Get the global data context.
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))
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
static cl::opt< unsigned long long > ClAndMask("msan-and-mask", cl::desc("Define custom MSan AndMask"), cl::Hidden, cl::init(0))
bool isIntegerTy() const
True if this is an instance of IntegerType.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
IntegerType * getIntPtrTy(const DataLayout &DL, unsigned AddrSpace=0)
Fetch the type representing a pointer to an integer value.
This file contains the simple types necessary to represent the attributes associated with functions a...
static cl::opt< unsigned long long > ClOriginBase("msan-origin-base", cl::desc("Define custom MSan OriginBase"), cl::Hidden, cl::init(0))
InstrTy * getInstruction() const
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.
static cl::opt< bool > ClHandleAsmConservative("msan-handle-asm-conservative", cl::desc("conservative handling of inline assembly"), cl::Hidden, cl::init(false))
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
This file implements a class to represent arbitrary precision integral constant values and operations...
Type * getVoidTy()
Fetch the type representing void.
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="")
ValTy * getCalledValue() const
Return the pointer to function that is being called.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Class to represent function types.
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Type * getType() const
All values are typed, get the type of this value.
This represents the llvm.va_start intrinsic.
static const char *const kMsanInitName
std::string itostr(int64_t X)
AtomicOrdering getSuccessOrdering() const
Returns the success ordering constraint of this cmpxchg instruction.
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.
static bool isStore(int Opcode)
This class represents a no-op cast from one type to another.
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the call or the callee has the given attribute.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Value * getInsertedValueOperand()
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
An instruction for storing to memory.
bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI=nullptr, DeferredDominance *DDT=nullptr)
Remove all blocks that can not be reached from the function's entry.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
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))
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
This class represents a truncation of integer types.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Value * getOperand(unsigned i) const
static const PlatformMemoryMapParams Linux_MIPS_MemoryMapParams
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
bool isCall() const
Return true if a CallInst is enclosed.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
static const unsigned kMinOriginAlignment
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
bool doesNotAccessMemory() const
Determine if the call does not access memory.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Value * getOperand(unsigned i_nocapture) const
bool isZeroValue() const
Return true if the value is negative zero or null value.
bool isVoidTy() const
Return true if this is 'void'.
const BasicBlock & getEntryBlock() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of converting the string to 'bool' fo...
static cl::opt< unsigned long long > ClShadowBase("msan-shadow-base", cl::desc("Define custom MSan ShadowBase"), cl::Hidden, cl::init(0))
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
unsigned getNumOperands() const
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...
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
INITIALIZE_PASS_BEGIN(MemorySanitizer, "msan", "MemorySanitizer: detects uninitialized reads.", false, false) INITIALIZE_PASS_END(MemorySanitizer
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
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.
This is an important class for using LLVM in a threaded context.
const char * getOpcodeName() const
This is an important base class in LLVM.
Resume the propagation of an exception.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
bool isPointerTy() const
True if this is an instance of PointerType.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Represent the analysis usage information of a pass.
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static const PlatformMemoryMapParams Linux_X86_MemoryMapParams
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
FunctionPass class - This class is used to implement most global optimizations.
static const unsigned kShadowTLSAlignment
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Constant * get(StructType *T, ArrayRef< Constant *> V)
Value * getPointerOperand()
bool isX86_MMXTy() const
Return true if this is X86 MMX.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
self_iterator getIterator()
Class to represent integer types.
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
This class represents a cast from an integer to a pointer.
const Value * getCondition() const
static Constant * getAllOnesValue(Type *Ty)
static const PlatformMemoryMapParams Linux_ARM_MemoryMapParams
static const MemoryMapParams FreeBSD_X86_64_MemoryMapParams
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
const Value * getArraySize() const
Get the number of elements allocated.
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this rmw instruction.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Triple - Helper class for working with autoconf configuration names.
bool isInvoke() const
Return true if a InvokeInst is enclosed.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Type * getSequentialElementType() const
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
This is the shared class of boolean and integer constants.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Provides information about what library functions are available for the current target.
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
bool isAggregateType() const
Return true if the type is an aggregate type.
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 ...
CallInst * CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align, Value *Mask)
Create a call to Masked Store intrinsic.
CHAIN = SC CHAIN, Imm128 - System call.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
StringRef str()
Return a StringRef for the vector contents.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
CallInst * CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memcpy between the specified pointers.
This class wraps the llvm.memcpy intrinsic.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
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.
CallInst * CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Load intrinsic.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static const size_t kNumberOfAccessSizes
static GlobalVariable * createPrivateNonConstGlobalForString(Module &M, StringRef Str)
Create a non-const global initialized with the given string.
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.
unsigned getVectorNumElements() const
Class to represent vector types.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class for arbitrary precision integers.
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
const Value * getFalseValue() const
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
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")))
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
void removeAttributes(unsigned i, const AttrBuilder &Attrs)
removes the attributes from the list of attributes.
Predicate getPredicate() const
Return the predicate for this instruction.
bool isInlineAsm() const
Check if this call is an inline asm statement.
static cl::opt< bool > ClHandleICmpExact("msan-handle-icmp-exact", cl::desc("exact handling of relational integer ICmp"), cl::Hidden, cl::init(false))
unsigned getAlignment() const
Return the alignment of the access that is being performed.
LLVM_NODISCARD bool empty() const
unsigned getNumArgOperands() const
Return the number of call arguments.
StringRef getName() const
Return a constant reference to the value's name.
static bool isSigned(Value *V)
Can the given value generate sign bits.
static const MemoryMapParams Linux_X86_64_MemoryMapParams
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 const PlatformMemoryMapParams NetBSD_X86_MemoryMapParams
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
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.
static cl::opt< bool > ClCheckConstantShadow("msan-check-constant-shadow", cl::desc("Insert checks for constant shadow values"), cl::Hidden, cl::init(false))
static const MemoryMapParams Linux_MIPS64_MemoryMapParams
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
static const unsigned kOriginSize
static const MemoryMapParams FreeBSD_I386_MemoryMapParams
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
iterator_range< df_iterator< T > > depth_first(const T &G)
This represents the llvm.va_copy intrinsic.
bool isArrayAllocation() const
Return true if there is an allocation size parameter to the allocation instruction that is not 1...
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
LoadInst * CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name)
Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")' correctly, instead of converting the strin...
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))
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.
ArrayRef< unsigned > getIndices() const
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
FunctionType * getFunctionType() const
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
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.
unsigned getArgumentNo(Value::const_user_iterator I) const
Given a value use iterator, returns the argument that corresponds to it.
BasicBlock::iterator GetInsertPoint() const
FunctionPass * createMemorySanitizerPass(int TrackOrigins=0, bool Recover=false)
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
const Value * getCalledValue() const
Get a pointer to the function that is invoked by this instruction.
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.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
StringRef - Represent a constant reference to a string, i.e.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
static const PlatformMemoryMapParams FreeBSD_X86_MemoryMapParams
Type * getArrayElementType() const
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
The C convention as implemented on Windows/x86-64 and AArch64.
Value * getPointerOperand()
static IntegerType * getInt8Ty(LLVMContext &C)
static const char *const kMsanModuleCtorName
static Constant * get(ArrayRef< Constant *> V)
iterator_range< arg_iterator > args()
bool isArrayTy() const
True if this is an instance of ArrayType.
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const
an instruction to allocate memory on the stack
This instruction inserts a struct field of array element value into an aggregate value.
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)