184#include "llvm/IR/IntrinsicsAArch64.h"
185#include "llvm/IR/IntrinsicsX86.h"
215#define DEBUG_TYPE "msan"
218 "Controls which checks to insert");
221 "Controls which instruction to instrument");
239 "msan-track-origins",
244 cl::desc(
"keep going after reporting a UMR"),
253 "msan-poison-stack-with-call",
258 "msan-poison-stack-pattern",
259 cl::desc(
"poison uninitialized stack variables with the given pattern"),
264 cl::desc(
"Print name of local stack variable"),
273 cl::desc(
"propagate shadow through ICmpEQ and ICmpNE"),
278 cl::desc(
"exact handling of relational integer ICmp"),
282 "msan-handle-lifetime-intrinsics",
284 "when possible, poison scoped variables at the beginning of the scope "
285 "(slower, but more precise)"),
296 "msan-handle-asm-conservative",
307 "msan-check-access-address",
308 cl::desc(
"report accesses through a pointer which has poisoned shadow"),
313 cl::desc(
"check arguments and return values at function call boundaries"),
317 "msan-dump-strict-instructions",
318 cl::desc(
"print out instructions with default strict semantics"),
322 "msan-instrumentation-with-call-threshold",
324 "If the function being instrumented requires more than "
325 "this number of checks and origin stores, use callbacks instead of "
326 "inline checks (-1 means never use callbacks)."),
331 cl::desc(
"Enable KernelMemorySanitizer instrumentation"),
341 cl::desc(
"Insert checks for constant shadow values"),
348 cl::desc(
"Place MSan constructors in comdat sections"),
354 cl::desc(
"Define custom MSan AndMask"),
358 cl::desc(
"Define custom MSan XorMask"),
362 cl::desc(
"Define custom MSan ShadowBase"),
366 cl::desc(
"Define custom MSan OriginBase"),
371 cl::desc(
"Define threshold for number of checks per "
372 "debug location to force origin update."),
384struct MemoryMapParams {
391struct PlatformMemoryMapParams {
392 const MemoryMapParams *bits32;
393 const MemoryMapParams *bits64;
539class MemorySanitizer {
548 MemorySanitizer(MemorySanitizer &&) =
delete;
549 MemorySanitizer &operator=(MemorySanitizer &&) =
delete;
550 MemorySanitizer(
const MemorySanitizer &) =
delete;
551 MemorySanitizer &operator=(
const MemorySanitizer &) =
delete;
556 friend struct MemorySanitizerVisitor;
557 friend struct VarArgHelperBase;
558 friend struct VarArgAMD64Helper;
559 friend struct VarArgMIPS64Helper;
560 friend struct VarArgAArch64Helper;
561 friend struct VarArgPowerPC64Helper;
562 friend struct VarArgSystemZHelper;
564 void initializeModule(
Module &M);
569 template <
typename... ArgsTy>
596 Value *ParamOriginTLS;
602 Value *RetvalOriginTLS;
608 Value *VAArgOriginTLS;
611 Value *VAArgOverflowSizeTLS;
614 bool CallbacksInitialized =
false;
659 Value *MsanMetadataAlloca;
665 const MemoryMapParams *MapParams;
669 MemoryMapParams CustomMapParams;
674 MDNode *OriginStoreWeights;
677void insertModuleCtor(
Module &M) {
705 Recover(getOptOrDefault(
ClKeepGoing, Kernel || R)),
723 MemorySanitizer Msan(*
F.getParent(),
Options);
742 OS, MapClassName2PassName);
749 OS <<
"eager-checks;";
750 OS <<
"track-origins=" <<
Options.TrackOrigins;
766template <
typename... ArgsTy>
774 std::forward<ArgsTy>(Args)...);
777 return M.getOrInsertFunction(
Name, MsanMetadata,
778 std::forward<ArgsTy>(Args)...);
787 RetvalOriginTLS =
nullptr;
789 ParamOriginTLS =
nullptr;
791 VAArgOriginTLS =
nullptr;
792 VAArgOverflowSizeTLS =
nullptr;
794 WarningFn =
M.getOrInsertFunction(
"__msan_warning",
796 IRB.getVoidTy(), IRB.getInt32Ty());
807 MsanGetContextStateFn =
M.getOrInsertFunction(
813 for (
int ind = 0, size = 1; ind < 4; ind++,
size <<= 1) {
814 std::string name_load =
815 "__msan_metadata_ptr_for_load_" + std::to_string(size);
816 std::string name_store =
817 "__msan_metadata_ptr_for_store_" + std::to_string(size);
818 MsanMetadataPtrForLoad_1_8[ind] = getOrInsertMsanMetadataFunction(
820 MsanMetadataPtrForStore_1_8[ind] = getOrInsertMsanMetadataFunction(
824 MsanMetadataPtrForLoadN = getOrInsertMsanMetadataFunction(
827 MsanMetadataPtrForStoreN = getOrInsertMsanMetadataFunction(
828 M,
"__msan_metadata_ptr_for_store_n",
832 MsanPoisonAllocaFn =
M.getOrInsertFunction(
833 "__msan_poison_alloca", IRB.getVoidTy(), PtrTy, IntptrTy, PtrTy);
834 MsanUnpoisonAllocaFn =
M.getOrInsertFunction(
835 "__msan_unpoison_alloca", IRB.getVoidTy(), PtrTy, IntptrTy);
839 return M.getOrInsertGlobal(
Name, Ty, [&] {
841 nullptr,
Name,
nullptr,
854 StringRef WarningFnName = Recover ?
"__msan_warning_with_origin"
855 :
"__msan_warning_with_origin_noreturn";
856 WarningFn =
M.getOrInsertFunction(WarningFnName,
858 IRB.getVoidTy(), IRB.getInt32Ty());
861 Recover ?
"__msan_warning" :
"__msan_warning_noreturn";
862 WarningFn =
M.getOrInsertFunction(WarningFnName, IRB.getVoidTy());
888 VAArgOverflowSizeTLS =
893 unsigned AccessSize = 1 << AccessSizeIndex;
894 std::string FunctionName =
"__msan_maybe_warning_" + itostr(AccessSize);
895 MaybeWarningFn[AccessSizeIndex] =
M.getOrInsertFunction(
897 IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), IRB.getInt32Ty());
899 FunctionName =
"__msan_maybe_store_origin_" + itostr(AccessSize);
900 MaybeStoreOriginFn[AccessSizeIndex] =
M.getOrInsertFunction(
902 IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), PtrTy,
906 MsanSetAllocaOriginWithDescriptionFn =
907 M.getOrInsertFunction(
"__msan_set_alloca_origin_with_descr",
908 IRB.getVoidTy(), PtrTy, IntptrTy, PtrTy, PtrTy);
909 MsanSetAllocaOriginNoDescriptionFn =
910 M.getOrInsertFunction(
"__msan_set_alloca_origin_no_descr",
911 IRB.getVoidTy(), PtrTy, IntptrTy, PtrTy);
912 MsanPoisonStackFn =
M.getOrInsertFunction(
"__msan_poison_stack",
913 IRB.getVoidTy(), PtrTy, IntptrTy);
919 if (CallbacksInitialized)
925 MsanChainOriginFn =
M.getOrInsertFunction(
926 "__msan_chain_origin",
929 MsanSetOriginFn =
M.getOrInsertFunction(
931 IRB.getVoidTy(), PtrTy, IntptrTy, IRB.getInt32Ty());
933 M.getOrInsertFunction(
"__msan_memmove", PtrTy, PtrTy, PtrTy, IntptrTy);
935 M.getOrInsertFunction(
"__msan_memcpy", PtrTy, PtrTy, PtrTy, IntptrTy);
936 MemsetFn =
M.getOrInsertFunction(
"__msan_memset",
938 PtrTy, PtrTy, IRB.getInt32Ty(), IntptrTy);
940 MsanInstrumentAsmStoreFn =
941 M.getOrInsertFunction(
"__msan_instrument_asm_store", IRB.getVoidTy(),
945 createKernelApi(M, TLI);
947 createUserspaceApi(M, TLI);
949 CallbacksInitialized =
true;
955 isStore ? MsanMetadataPtrForStore_1_8 : MsanMetadataPtrForLoad_1_8;
973void MemorySanitizer::initializeModule(
Module &M) {
974 auto &
DL =
M.getDataLayout();
976 TargetTriple =
Triple(
M.getTargetTriple());
978 bool ShadowPassed =
ClShadowBase.getNumOccurrences() > 0;
979 bool OriginPassed =
ClOriginBase.getNumOccurrences() > 0;
981 if (ShadowPassed || OriginPassed) {
986 MapParams = &CustomMapParams;
988 switch (TargetTriple.getOS()) {
990 switch (TargetTriple.getArch()) {
1005 switch (TargetTriple.getArch()) {
1014 switch (TargetTriple.getArch()) {
1048 C = &(
M.getContext());
1050 IntptrTy = IRB.getIntPtrTy(
DL);
1051 OriginTy = IRB.getInt32Ty();
1052 PtrTy = IRB.getPtrTy();
1057 if (!CompileKernel) {
1059 M.getOrInsertGlobal(
"__msan_track_origins", IRB.getInt32Ty(), [&] {
1060 return new GlobalVariable(
1061 M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage,
1062 IRB.getInt32(TrackOrigins),
"__msan_track_origins");
1066 M.getOrInsertGlobal(
"__msan_keep_going", IRB.getInt32Ty(), [&] {
1067 return new GlobalVariable(M, IRB.getInt32Ty(), true,
1068 GlobalValue::WeakODRLinkage,
1069 IRB.getInt32(Recover),
"__msan_keep_going");
1084struct VarArgHelper {
1085 virtual ~VarArgHelper() =
default;
1100 virtual void finalizeInstrumentation() = 0;
1103struct MemorySanitizerVisitor;
1108 MemorySanitizerVisitor &Visitor);
1115 if (TypeSizeFixed <= 8)
1124class NextNodeIRBuilder :
public IRBuilder<> {
1137struct MemorySanitizerVisitor :
public InstVisitor<MemorySanitizerVisitor> {
1139 MemorySanitizer &MS;
1142 std::unique_ptr<VarArgHelper> VAHelper;
1150 bool PropagateShadow;
1154 struct ShadowOriginAndInsertPoint {
1160 : Shadow(S), Origin(
O), OrigIns(
I) {}
1168 int64_t SplittableBlocksCount = 0;
1170 MemorySanitizerVisitor(
Function &
F, MemorySanitizer &MS,
1173 bool SanitizeFunction =
1175 InsertChecks = SanitizeFunction;
1176 PropagateShadow = SanitizeFunction;
1186 MS.initializeCallbacks(*
F.getParent(), TLI);
1187 FnPrologueEnd =
IRBuilder<>(
F.getEntryBlock().getFirstNonPHI())
1190 if (MS.CompileKernel) {
1192 insertKmsanPrologue(IRB);
1196 <<
"MemorySanitizer is not inserting checks into '"
1197 <<
F.getName() <<
"'\n");
1200 bool instrumentWithCalls(
Value *V) {
1202 if (isa<Constant>(V))
1205 ++SplittableBlocksCount;
1211 return I.getParent() == FnPrologueEnd->
getParent() &&
1212 (&
I == FnPrologueEnd ||
I.comesBefore(FnPrologueEnd));
1220 if (MS.TrackOrigins <= 1)
1222 return IRB.
CreateCall(MS.MsanChainOriginFn, V);
1227 unsigned IntptrSize =
DL.getTypeStoreSize(MS.IntptrTy);
1239 const Align IntptrAlignment =
DL.getABITypeAlign(MS.IntptrTy);
1240 unsigned IntptrSize =
DL.getTypeStoreSize(MS.IntptrTy);
1252 auto [InsertPt,
Index] =
1264 Align CurrentAlignment = Alignment;
1265 if (Alignment >= IntptrAlignment && IntptrSize >
kOriginSize) {
1266 Value *IntptrOrigin = originToIntptr(IRB, Origin);
1267 Value *IntptrOriginPtr =
1269 for (
unsigned i = 0; i <
Size / IntptrSize; ++i) {
1274 CurrentAlignment = IntptrAlignment;
1292 Value *ConvertedShadow = convertShadowToScalar(Shadow, IRB);
1293 if (
auto *ConstantShadow = dyn_cast<Constant>(ConvertedShadow)) {
1301 paintOrigin(IRB, updateOrigin(Origin, IRB), OriginPtr, StoreSize,
1310 if (instrumentWithCalls(ConvertedShadow) &&
1313 Value *ConvertedShadow2 =
1319 Value *
Cmp = convertToBool(ConvertedShadow, IRB,
"_mscmp");
1323 paintOrigin(IRBNew, updateOrigin(Origin, IRBNew), OriginPtr, StoreSize,
1328 void materializeStores() {
1331 Value *Val =
SI->getValueOperand();
1333 Value *Shadow =
SI->isAtomic() ? getCleanShadow(Val) : getShadow(Val);
1334 Value *ShadowPtr, *OriginPtr;
1336 const Align Alignment =
SI->getAlign();
1338 std::tie(ShadowPtr, OriginPtr) =
1339 getShadowOriginPtr(
Addr, IRB, ShadowTy, Alignment,
true);
1348 if (MS.TrackOrigins && !
SI->isAtomic())
1349 storeOrigin(IRB,
Addr, Shadow, getOrigin(Val), OriginPtr,
1356 if (MS.TrackOrigins < 2)
1359 if (LazyWarningDebugLocationCount.
empty())
1360 for (
const auto &
I : InstrumentationList)
1361 ++LazyWarningDebugLocationCount[
I.OrigIns->getDebugLoc()];
1375 if (
Instruction *OI = dyn_cast_or_null<Instruction>(Origin)) {
1377 auto NewDebugLoc = OI->getDebugLoc();
1384 IRBOrigin.SetCurrentDebugLocation(NewDebugLoc);
1385 Origin = updateOrigin(Origin, IRBOrigin);
1390 if (MS.CompileKernel || MS.TrackOrigins)
1404 if (instrumentWithCalls(ConvertedShadow) &&
1408 ConvertedShadow = convertShadowToScalar(ConvertedShadow, IRB);
1409 Value *ConvertedShadow2 =
1412 Fn, {ConvertedShadow2,
1413 MS.TrackOrigins && Origin ? Origin : (
Value *)IRB.
getInt32(0)});
1417 Value *
Cmp = convertToBool(ConvertedShadow, IRB,
"_mscmp");
1420 !MS.Recover, MS.ColdCallWeights);
1423 insertWarningFn(IRB, Origin);
1428 void materializeInstructionChecks(
1433 bool Combine = !MS.TrackOrigins;
1435 Value *Shadow =
nullptr;
1436 for (
const auto &ShadowData : InstructionChecks) {
1440 Value *ConvertedShadow = ShadowData.Shadow;
1442 if (
auto *ConstantShadow = dyn_cast<Constant>(ConvertedShadow)) {
1449 insertWarningFn(IRB, ShadowData.Origin);
1459 materializeOneCheck(IRB, ConvertedShadow, ShadowData.Origin);
1464 Shadow = ConvertedShadow;
1468 Shadow = convertToBool(Shadow, IRB,
"_mscmp");
1469 ConvertedShadow = convertToBool(ConvertedShadow, IRB,
"_mscmp");
1470 Shadow = IRB.
CreateOr(Shadow, ConvertedShadow,
"_msor");
1476 materializeOneCheck(IRB, Shadow,
nullptr);
1480 void materializeChecks() {
1486 for (
auto I = InstrumentationList.begin();
1487 I != InstrumentationList.end();) {
1488 auto OrigIns =
I->OrigIns;
1492 auto J = std::find_if(
I + 1, InstrumentationList.end(),
1493 [OrigIns](
const ShadowOriginAndInsertPoint &R) {
1494 return OrigIns != R.OrigIns;
1508 MS.ParamTLS = IRB.
CreateGEP(MS.MsanContextStateTy, ContextState,
1509 {Zero, IRB.getInt32(0)},
"param_shadow");
1510 MS.RetvalTLS = IRB.
CreateGEP(MS.MsanContextStateTy, ContextState,
1511 {Zero, IRB.getInt32(1)},
"retval_shadow");
1512 MS.VAArgTLS = IRB.
CreateGEP(MS.MsanContextStateTy, ContextState,
1513 {Zero, IRB.getInt32(2)},
"va_arg_shadow");
1514 MS.VAArgOriginTLS = IRB.
CreateGEP(MS.MsanContextStateTy, ContextState,
1515 {Zero, IRB.getInt32(3)},
"va_arg_origin");
1516 MS.VAArgOverflowSizeTLS =
1517 IRB.
CreateGEP(MS.MsanContextStateTy, ContextState,
1518 {Zero, IRB.getInt32(4)},
"va_arg_overflow_size");
1519 MS.ParamOriginTLS = IRB.
CreateGEP(MS.MsanContextStateTy, ContextState,
1520 {Zero, IRB.getInt32(5)},
"param_origin");
1521 MS.RetvalOriginTLS =
1522 IRB.
CreateGEP(MS.MsanContextStateTy, ContextState,
1523 {Zero, IRB.getInt32(6)},
"retval_origin");
1525 MS.MsanMetadataAlloca = IRB.
CreateAlloca(MS.MsanMetadata, 0u);
1542 for (
PHINode *PN : ShadowPHINodes) {
1543 PHINode *PNS = cast<PHINode>(getShadow(PN));
1544 PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) : nullptr;
1545 size_t NumValues = PN->getNumIncomingValues();
1546 for (
size_t v = 0;
v < NumValues;
v++) {
1547 PNS->
addIncoming(getShadow(PN, v), PN->getIncomingBlock(v));
1549 PNO->
addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v));
1553 VAHelper->finalizeInstrumentation();
1557 if (InstrumentLifetimeStart) {
1558 for (
auto Item : LifetimeStartList) {
1559 instrumentAlloca(*Item.second, Item.first);
1560 AllocaSet.
remove(Item.second);
1566 instrumentAlloca(*AI);
1569 materializeChecks();
1573 materializeStores();
1579 Type *getShadowTy(
Value *V) {
return getShadowTy(
V->getType()); }
1591 if (
VectorType *VT = dyn_cast<VectorType>(OrigTy)) {
1592 uint32_t EltSize =
DL.getTypeSizeInBits(VT->getElementType());
1594 VT->getElementCount());
1596 if (
ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) {
1597 return ArrayType::get(getShadowTy(AT->getElementType()),
1598 AT->getNumElements());
1600 if (
StructType *ST = dyn_cast<StructType>(OrigTy)) {
1602 for (
unsigned i = 0, n =
ST->getNumElements(); i < n; i++)
1603 Elements.push_back(getShadowTy(
ST->getElementType(i)));
1605 LLVM_DEBUG(
dbgs() <<
"getShadowTy: " << *ST <<
" ===> " << *Res <<
"\n");
1621 Value *ShadowBool = convertToBool(ShadowItem, IRB);
1623 if (Aggregator != FalseVal)
1624 Aggregator = IRB.
CreateOr(Aggregator, ShadowBool);
1626 Aggregator = ShadowBool;
1635 if (!
Array->getNumElements())
1639 Value *Aggregator = convertShadowToScalar(FirstItem, IRB);
1643 Value *ShadowInner = convertShadowToScalar(ShadowItem, IRB);
1644 Aggregator = IRB.
CreateOr(Aggregator, ShadowInner);
1654 return collapseStructShadow(
Struct, V, IRB);
1655 if (
ArrayType *Array = dyn_cast<ArrayType>(
V->getType()))
1656 return collapseArrayShadow(Array, V, IRB);
1657 if (isa<VectorType>(
V->getType())) {
1658 if (isa<ScalableVectorType>(
V->getType()))
1661 V->getType()->getPrimitiveSizeInBits().getFixedValue();
1669 Type *VTy =
V->getType();
1671 return convertToBool(convertShadowToScalar(V, IRB), IRB,
name);
1678 Type *ptrToIntPtrType(
Type *PtrTy)
const {
1679 if (
VectorType *VectTy = dyn_cast<VectorType>(PtrTy)) {
1680 return VectorType::get(ptrToIntPtrType(VectTy->getElementType()),
1681 VectTy->getElementCount());
1687 Type *getPtrToShadowPtrType(
Type *IntPtrTy,
Type *ShadowTy)
const {
1688 if (
VectorType *VectTy = dyn_cast<VectorType>(IntPtrTy)) {
1689 return VectorType::get(
1690 getPtrToShadowPtrType(VectTy->getElementType(), ShadowTy),
1691 VectTy->getElementCount());
1693 assert(IntPtrTy == MS.IntptrTy);
1694 return PointerType::get(*MS.C, 0);
1698 if (
VectorType *VectTy = dyn_cast<VectorType>(IntPtrTy)) {
1700 VectTy->getElementCount(), constToIntPtr(VectTy->getElementType(),
C));
1702 assert(IntPtrTy == MS.IntptrTy);
1703 return ConstantInt::get(MS.IntptrTy,
C);
1714 Type *IntptrTy = ptrToIntPtrType(
Addr->getType());
1717 if (
uint64_t AndMask = MS.MapParams->AndMask)
1718 OffsetLong = IRB.
CreateAnd(OffsetLong, constToIntPtr(IntptrTy, ~AndMask));
1720 if (
uint64_t XorMask = MS.MapParams->XorMask)
1721 OffsetLong = IRB.
CreateXor(OffsetLong, constToIntPtr(IntptrTy, XorMask));
1733 std::pair<Value *, Value *>
1740 assert(VectTy->getElementType()->isPointerTy());
1742 Type *IntptrTy = ptrToIntPtrType(
Addr->getType());
1743 Value *ShadowOffset = getShadowPtrOffset(
Addr, IRB);
1744 Value *ShadowLong = ShadowOffset;
1745 if (
uint64_t ShadowBase = MS.MapParams->ShadowBase) {
1747 IRB.
CreateAdd(ShadowLong, constToIntPtr(IntptrTy, ShadowBase));
1750 ShadowLong, getPtrToShadowPtrType(IntptrTy, ShadowTy));
1752 Value *OriginPtr =
nullptr;
1753 if (MS.TrackOrigins) {
1754 Value *OriginLong = ShadowOffset;
1755 uint64_t OriginBase = MS.MapParams->OriginBase;
1756 if (OriginBase != 0)
1758 IRB.
CreateAdd(OriginLong, constToIntPtr(IntptrTy, OriginBase));
1761 OriginLong = IRB.
CreateAnd(OriginLong, constToIntPtr(IntptrTy, ~Mask));
1764 OriginLong, getPtrToShadowPtrType(IntptrTy, MS.OriginTy));
1766 return std::make_pair(ShadowPtr, OriginPtr);
1769 template <
typename... ArgsTy>
1774 {MS.MsanMetadataAlloca, std::forward<ArgsTy>(Args)...});
1775 return IRB.
CreateLoad(MS.MsanMetadata, MS.MsanMetadataAlloca);
1778 return IRB.
CreateCall(Callee, {std::forward<ArgsTy>(Args)...});
1781 std::pair<Value *, Value *> getShadowOriginPtrKernelNoVec(
Value *
Addr,
1785 Value *ShadowOriginPtrs;
1793 ShadowOriginPtrs = createMetadataCall(IRB, Getter, AddrCast);
1795 Value *SizeVal = ConstantInt::get(MS.IntptrTy,
Size);
1796 ShadowOriginPtrs = createMetadataCall(
1798 isStore ? MS.MsanMetadataPtrForStoreN : MS.MsanMetadataPtrForLoadN,
1805 return std::make_pair(ShadowPtr, OriginPtr);
1811 std::pair<Value *, Value *> getShadowOriginPtrKernel(
Value *
Addr,
1818 return getShadowOriginPtrKernelNoVec(
Addr, IRB, ShadowTy,
isStore);
1822 unsigned NumElements = cast<FixedVectorType>(VectTy)->getNumElements();
1823 Value *ShadowPtrs = ConstantInt::getNullValue(
1825 Value *OriginPtrs =
nullptr;
1826 if (MS.TrackOrigins)
1827 OriginPtrs = ConstantInt::getNullValue(
1829 for (
unsigned i = 0; i < NumElements; ++i) {
1832 auto [ShadowPtr, OriginPtr] =
1833 getShadowOriginPtrKernelNoVec(OneAddr, IRB, ShadowTy,
isStore);
1836 ShadowPtrs, ShadowPtr, ConstantInt::get(IRB.
getInt32Ty(), i));
1837 if (MS.TrackOrigins)
1839 OriginPtrs, OriginPtr, ConstantInt::get(IRB.
getInt32Ty(), i));
1841 return {ShadowPtrs, OriginPtrs};
1848 if (MS.CompileKernel)
1849 return getShadowOriginPtrKernel(
Addr, IRB, ShadowTy,
isStore);
1850 return getShadowOriginPtrUserspace(
Addr, IRB, ShadowTy, Alignment);
1865 if (!MS.TrackOrigins)
1879 Value *getOriginPtrForRetval() {
1881 return MS.RetvalOriginTLS;
1886 assert(!ShadowMap.
count(V) &&
"Values may only have one shadow");
1887 ShadowMap[
V] = PropagateShadow ? SV : getCleanShadow(V);
1892 if (!MS.TrackOrigins)
1894 assert(!OriginMap.
count(V) &&
"Values may only have one origin");
1895 LLVM_DEBUG(
dbgs() <<
"ORIGIN: " << *V <<
" ==> " << *Origin <<
"\n");
1896 OriginMap[
V] = Origin;
1900 Type *ShadowTy = getShadowTy(OrigTy);
1910 Constant *getCleanShadow(
Value *V) {
return getCleanShadow(
V->getType()); }
1915 if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy))
1917 if (
ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) {
1919 getPoisonedShadow(AT->getElementType()));
1922 if (
StructType *ST = dyn_cast<StructType>(ShadowTy)) {
1924 for (
unsigned i = 0, n =
ST->getNumElements(); i < n; i++)
1925 Vals.
push_back(getPoisonedShadow(
ST->getElementType(i)));
1933 Type *ShadowTy = getShadowTy(V);
1936 return getPoisonedShadow(ShadowTy);
1948 if (!PropagateShadow ||
I->getMetadata(LLVMContext::MD_nosanitize))
1949 return getCleanShadow(V);
1951 Value *Shadow = ShadowMap[
V];
1953 LLVM_DEBUG(
dbgs() <<
"No shadow: " << *V <<
"\n" << *(
I->getParent()));
1955 assert(Shadow &&
"No shadow for a value");
1959 if (
UndefValue *U = dyn_cast<UndefValue>(V)) {
1960 Value *
AllOnes = (PropagateShadow && PoisonUndef) ? getPoisonedShadow(V)
1961 : getCleanShadow(V);
1966 if (
Argument *
A = dyn_cast<Argument>(V)) {
1968 Value *&ShadowPtr = ShadowMap[
V];
1973 unsigned ArgOffset = 0;
1975 for (
auto &FArg :
F->args()) {
1976 if (!FArg.getType()->isSized() || FArg.getType()->isScalableTy()) {
1978 ?
"vscale not fully supported\n"
1979 :
"Arg is not sized\n"));
1981 ShadowPtr = getCleanShadow(V);
1982 setOrigin(
A, getCleanOrigin());
1988 unsigned Size = FArg.hasByValAttr()
1989 ?
DL.getTypeAllocSize(FArg.getParamByValType())
1990 :
DL.getTypeAllocSize(FArg.getType());
1994 if (FArg.hasByValAttr()) {
1998 const Align ArgAlign =
DL.getValueOrABITypeAlignment(
1999 FArg.getParamAlign(), FArg.getParamByValType());
2000 Value *CpShadowPtr, *CpOriginPtr;
2001 std::tie(CpShadowPtr, CpOriginPtr) =
2002 getShadowOriginPtr(V, EntryIRB, EntryIRB.getInt8Ty(), ArgAlign,
2004 if (!PropagateShadow || Overflow) {
2006 EntryIRB.CreateMemSet(
2010 Value *
Base = getShadowPtrForArgument(EntryIRB, ArgOffset);
2012 Value *Cpy = EntryIRB.CreateMemCpy(CpShadowPtr, CopyAlign,
Base,
2017 if (MS.TrackOrigins) {
2019 getOriginPtrForArgument(EntryIRB, ArgOffset);
2023 EntryIRB.CreateMemCpy(
2032 if (!PropagateShadow || Overflow || FArg.hasByValAttr() ||
2033 (MS.EagerChecks && FArg.hasAttribute(Attribute::NoUndef))) {
2034 ShadowPtr = getCleanShadow(V);
2035 setOrigin(
A, getCleanOrigin());
2038 Value *
Base = getShadowPtrForArgument(EntryIRB, ArgOffset);
2039 ShadowPtr = EntryIRB.CreateAlignedLoad(getShadowTy(&FArg),
Base,
2041 if (MS.TrackOrigins) {
2043 getOriginPtrForArgument(EntryIRB, ArgOffset);
2044 setOrigin(
A, EntryIRB.CreateLoad(MS.OriginTy, OriginPtr));
2048 <<
" ARG: " << FArg <<
" ==> " << *ShadowPtr <<
"\n");
2054 assert(ShadowPtr &&
"Could not find shadow for an argument");
2058 return getCleanShadow(V);
2063 return getShadow(
I->getOperand(i));
2068 if (!MS.TrackOrigins)
2070 if (!PropagateShadow || isa<Constant>(V) || isa<InlineAsm>(V))
2071 return getCleanOrigin();
2072 assert((isa<Instruction>(V) || isa<Argument>(V)) &&
2073 "Unexpected value type in getOrigin()");
2075 if (
I->getMetadata(LLVMContext::MD_nosanitize))
2076 return getCleanOrigin();
2078 Value *Origin = OriginMap[
V];
2079 assert(Origin &&
"Missing origin");
2085 return getOrigin(
I->getOperand(i));
2098 LLVM_DEBUG(
dbgs() <<
"Skipping check of " << *Shadow <<
" before "
2099 << *OrigIns <<
"\n");
2104 assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy) ||
2105 isa<StructType>(ShadowTy) || isa<ArrayType>(ShadowTy)) &&
2106 "Can only insert checks for integer, vector, and aggregate shadow "
2109 InstrumentationList.push_back(
2110 ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
2119 Value *Shadow, *Origin;
2121 Shadow = getShadow(Val);
2124 Origin = getOrigin(Val);
2126 Shadow = dyn_cast_or_null<Instruction>(getShadow(Val));
2129 Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
2131 insertShadowCheck(Shadow, Origin, OrigIns);
2136 case AtomicOrdering::NotAtomic:
2137 return AtomicOrdering::NotAtomic;
2138 case AtomicOrdering::Unordered:
2139 case AtomicOrdering::Monotonic:
2140 case AtomicOrdering::Release:
2141 return AtomicOrdering::Release;
2142 case AtomicOrdering::Acquire:
2143 case AtomicOrdering::AcquireRelease:
2144 return AtomicOrdering::AcquireRelease;
2145 case AtomicOrdering::SequentiallyConsistent:
2146 return AtomicOrdering::SequentiallyConsistent;
2152 constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
2153 uint32_t OrderingTable[NumOrderings] = {};
2155 OrderingTable[(int)AtomicOrderingCABI::relaxed] =
2156 OrderingTable[(
int)AtomicOrderingCABI::release] =
2157 (int)AtomicOrderingCABI::release;
2158 OrderingTable[(int)AtomicOrderingCABI::consume] =
2159 OrderingTable[(
int)AtomicOrderingCABI::acquire] =
2160 OrderingTable[(int)AtomicOrderingCABI::acq_rel] =
2161 (
int)AtomicOrderingCABI::acq_rel;
2162 OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
2163 (
int)AtomicOrderingCABI::seq_cst;
2170 case AtomicOrdering::NotAtomic:
2171 return AtomicOrdering::NotAtomic;
2172 case AtomicOrdering::Unordered:
2173 case AtomicOrdering::Monotonic:
2174 case AtomicOrdering::Acquire:
2175 return AtomicOrdering::Acquire;
2176 case AtomicOrdering::Release:
2177 case AtomicOrdering::AcquireRelease:
2178 return AtomicOrdering::AcquireRelease;
2179 case AtomicOrdering::SequentiallyConsistent:
2180 return AtomicOrdering::SequentiallyConsistent;
2186 constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
2187 uint32_t OrderingTable[NumOrderings] = {};
2189 OrderingTable[(int)AtomicOrderingCABI::relaxed] =
2190 OrderingTable[(
int)AtomicOrderingCABI::acquire] =
2191 OrderingTable[(int)AtomicOrderingCABI::consume] =
2192 (
int)AtomicOrderingCABI::acquire;
2193 OrderingTable[(int)AtomicOrderingCABI::release] =
2194 OrderingTable[(
int)AtomicOrderingCABI::acq_rel] =
2195 (int)AtomicOrderingCABI::acq_rel;
2196 OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
2197 (
int)AtomicOrderingCABI::seq_cst;
2205 if (
I.getMetadata(LLVMContext::MD_nosanitize))
2208 if (isInPrologue(
I))
2213 setShadow(&
I, getCleanShadow(&
I));
2214 setOrigin(&
I, getCleanOrigin());
2226 assert(
I.getType()->isSized() &&
"Load type must have size");
2227 assert(!
I.getMetadata(LLVMContext::MD_nosanitize));
2228 NextNodeIRBuilder IRB(&
I);
2229 Type *ShadowTy = getShadowTy(&
I);
2231 Value *ShadowPtr =
nullptr, *OriginPtr =
nullptr;
2232 const Align Alignment =
I.getAlign();
2233 if (PropagateShadow) {
2234 std::tie(ShadowPtr, OriginPtr) =
2235 getShadowOriginPtr(
Addr, IRB, ShadowTy, Alignment,
false);
2239 setShadow(&
I, getCleanShadow(&
I));
2243 insertShadowCheck(
I.getPointerOperand(), &
I);
2248 if (MS.TrackOrigins) {
2249 if (PropagateShadow) {
2254 setOrigin(&
I, getCleanOrigin());
2264 StoreList.push_back(&
I);
2266 insertShadowCheck(
I.getPointerOperand(), &
I);
2270 assert(isa<AtomicRMWInst>(
I) || isa<AtomicCmpXchgInst>(
I));
2274 Value *Val =
I.getOperand(1);
2275 Value *ShadowPtr = getShadowOriginPtr(
Addr, IRB, getShadowTy(Val),
Align(1),
2280 insertShadowCheck(
Addr, &
I);
2285 if (isa<AtomicCmpXchgInst>(
I))
2286 insertShadowCheck(Val, &
I);
2290 setShadow(&
I, getCleanShadow(&
I));
2291 setOrigin(&
I, getCleanOrigin());
2306 insertShadowCheck(
I.getOperand(1), &
I);
2310 setOrigin(&
I, getOrigin(&
I, 0));
2314 insertShadowCheck(
I.getOperand(2), &
I);
2316 auto *Shadow0 = getShadow(&
I, 0);
2317 auto *Shadow1 = getShadow(&
I, 1);
2320 setOriginForNaryOp(
I);
2325 auto *Shadow0 = getShadow(&
I, 0);
2326 auto *Shadow1 = getShadow(&
I, 1);
2329 setOriginForNaryOp(
I);
2335 setShadow(&
I, IRB.
CreateSExt(getShadow(&
I, 0),
I.getType(),
"_msprop"));
2336 setOrigin(&
I, getOrigin(&
I, 0));
2341 setShadow(&
I, IRB.
CreateZExt(getShadow(&
I, 0),
I.getType(),
"_msprop"));
2342 setOrigin(&
I, getOrigin(&
I, 0));
2347 setShadow(&
I, IRB.
CreateTrunc(getShadow(&
I, 0),
I.getType(),
"_msprop"));
2348 setOrigin(&
I, getOrigin(&
I, 0));
2355 if (
auto *CI = dyn_cast<CallInst>(
I.getOperand(0)))
2356 if (CI->isMustTailCall())
2360 setOrigin(&
I, getOrigin(&
I, 0));
2366 "_msprop_ptrtoint"));
2367 setOrigin(&
I, getOrigin(&
I, 0));
2373 "_msprop_inttoptr"));
2374 setOrigin(&
I, getOrigin(&
I, 0));
2377 void visitFPToSIInst(
CastInst &
I) { handleShadowOr(
I); }
2378 void visitFPToUIInst(
CastInst &
I) { handleShadowOr(
I); }
2379 void visitSIToFPInst(
CastInst &
I) { handleShadowOr(
I); }
2380 void visitUIToFPInst(
CastInst &
I) { handleShadowOr(
I); }
2381 void visitFPExtInst(
CastInst &
I) { handleShadowOr(
I); }
2382 void visitFPTruncInst(
CastInst &
I) { handleShadowOr(
I); }
2397 Value *S2 = getShadow(&
I, 1);
2398 Value *V1 =
I.getOperand(0);
2407 setShadow(&
I, IRB.
CreateOr({S1S2, V1S2, S1V2}));
2408 setOriginForNaryOp(
I);
2419 Value *S2 = getShadow(&
I, 1);
2429 setShadow(&
I, IRB.
CreateOr({S1S2, V1S2, S1V2}));
2430 setOriginForNaryOp(
I);
2448 template <
bool CombineShadow>
class Combiner {
2449 Value *Shadow =
nullptr;
2450 Value *Origin =
nullptr;
2452 MemorySanitizerVisitor *MSV;
2456 : IRB(IRB), MSV(MSV) {}
2460 if (CombineShadow) {
2465 OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->getType());
2466 Shadow = IRB.
CreateOr(Shadow, OpShadow,
"_msprop");
2470 if (MSV->MS.TrackOrigins) {
2475 Constant *ConstOrigin = dyn_cast<Constant>(OpOrigin);
2477 if (!ConstOrigin || !ConstOrigin->
isNullValue()) {
2478 Value *
Cond = MSV->convertToBool(OpShadow, IRB);
2488 Value *OpShadow = MSV->getShadow(V);
2489 Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) :
nullptr;
2490 return Add(OpShadow, OpOrigin);
2496 if (CombineShadow) {
2498 Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(
I));
2499 MSV->setShadow(
I, Shadow);
2501 if (MSV->MS.TrackOrigins) {
2503 MSV->setOrigin(
I, Origin);
2510 if (MSV->MS.TrackOrigins) {
2522 if (!MS.TrackOrigins)
2525 OriginCombiner
OC(
this, IRB);
2526 for (
Use &
Op :
I.operands())
2531 size_t VectorOrPrimitiveTypeSizeInBits(
Type *Ty) {
2533 "Vector of pointers is not a valid shadow type");
2534 return Ty->
isVectorTy() ? cast<FixedVectorType>(Ty)->getNumElements() *
2543 Type *srcTy =
V->getType();
2546 size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy);
2547 size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy);
2548 if (srcSizeInBits > 1 && dstSizeInBits == 1)
2554 cast<VectorType>(dstTy)->getElementCount() ==
2555 cast<VectorType>(srcTy)->getElementCount())
2566 Type *ShadowTy = getShadowTy(V);
2567 if (
V->getType() == ShadowTy)
2569 if (
V->getType()->isPtrOrPtrVectorTy())
2578 ShadowAndOriginCombiner
SC(
this, IRB);
2579 for (
Use &
Op :
I.operands())
2599 if (
auto *VTy = dyn_cast<VectorType>(Ty)) {
2600 unsigned NumElements = cast<FixedVectorType>(VTy)->getNumElements();
2601 Type *EltTy = VTy->getElementType();
2603 for (
unsigned Idx = 0;
Idx < NumElements; ++
Idx) {
2606 const APInt &
V = Elt->getValue();
2608 Elements.push_back(ConstantInt::get(EltTy, V2));
2610 Elements.push_back(ConstantInt::get(EltTy, 1));
2615 if (
ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg)) {
2616 const APInt &
V = Elt->getValue();
2618 ShadowMul = ConstantInt::get(Ty, V2);
2620 ShadowMul = ConstantInt::get(Ty, 1);
2626 IRB.
CreateMul(getShadow(OtherArg), ShadowMul,
"msprop_mul_cst"));
2627 setOrigin(&
I, getOrigin(OtherArg));
2631 Constant *constOp0 = dyn_cast<Constant>(
I.getOperand(0));
2632 Constant *constOp1 = dyn_cast<Constant>(
I.getOperand(1));
2633 if (constOp0 && !constOp1)
2634 handleMulByConstant(
I, constOp0,
I.getOperand(1));
2635 else if (constOp1 && !constOp0)
2636 handleMulByConstant(
I, constOp1,
I.getOperand(0));
2651 insertShadowCheck(
I.getOperand(1), &
I);
2652 setShadow(&
I, getShadow(&
I, 0));
2653 setOrigin(&
I, getOrigin(&
I, 0));
2670 void handleEqualityComparison(
ICmpInst &
I) {
2674 Value *Sa = getShadow(
A);
2675 Value *Sb = getShadow(
B);
2701 setOriginForNaryOp(
I);
2743 void handleRelationalComparisonExact(
ICmpInst &
I) {
2747 Value *Sa = getShadow(
A);
2748 Value *Sb = getShadow(
B);
2759 bool IsSigned =
I.isSigned();
2761 getLowestPossibleValue(IRB,
A, Sa, IsSigned),
2762 getHighestPossibleValue(IRB,
B, Sb, IsSigned));
2764 getHighestPossibleValue(IRB,
A, Sa, IsSigned),
2765 getLowestPossibleValue(IRB,
B, Sb, IsSigned));
2768 setOriginForNaryOp(
I);
2775 void handleSignedRelationalComparison(
ICmpInst &
I) {
2779 if ((constOp = dyn_cast<Constant>(
I.getOperand(1)))) {
2780 op =
I.getOperand(0);
2781 pre =
I.getPredicate();
2782 }
else if ((constOp = dyn_cast<Constant>(
I.getOperand(0)))) {
2783 op =
I.getOperand(1);
2784 pre =
I.getSwappedPredicate();
2797 setShadow(&
I, Shadow);
2798 setOrigin(&
I, getOrigin(
op));
2809 if (
I.isEquality()) {
2810 handleEqualityComparison(
I);
2816 handleRelationalComparisonExact(
I);
2820 handleSignedRelationalComparison(
I);
2825 if ((isa<Constant>(
I.getOperand(0)) || isa<Constant>(
I.getOperand(1)))) {
2826 handleRelationalComparisonExact(
I);
2833 void visitFCmpInst(
FCmpInst &
I) { handleShadowOr(
I); }
2840 Value *S2 = getShadow(&
I, 1);
2845 setShadow(&
I, IRB.
CreateOr(Shift, S2Conv));
2846 setOriginForNaryOp(
I);
2857 Value *S0 = getShadow(&
I, 0);
2859 Value *S2 = getShadow(&
I, 2);
2864 I.getModule(),
I.getIntrinsicID(), S2Conv->
getType());
2866 setShadow(&
I, IRB.
CreateOr(Shift, S2Conv));
2867 setOriginForNaryOp(
I);
2881 getShadow(
I.getArgOperand(1));
2884 {I.getArgOperand(0), I.getArgOperand(1),
2885 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2886 I.eraseFromParent();
2904 getShadow(
I.getArgOperand(1));
2907 {I.getArgOperand(0), I.getArgOperand(1),
2908 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2909 I.eraseFromParent();
2917 {I.getArgOperand(0),
2918 IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false),
2919 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2920 I.eraseFromParent();
2923 void visitVAStartInst(
VAStartInst &
I) { VAHelper->visitVAStartInst(
I); }
2925 void visitVACopyInst(
VACopyInst &
I) { VAHelper->visitVACopyInst(
I); }
2934 Value *Shadow = getShadow(&
I, 1);
2935 Value *ShadowPtr, *OriginPtr;
2939 std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
2944 insertShadowCheck(
Addr, &
I);
2947 if (MS.TrackOrigins)
2960 Type *ShadowTy = getShadowTy(&
I);
2961 Value *ShadowPtr =
nullptr, *OriginPtr =
nullptr;
2962 if (PropagateShadow) {
2966 std::tie(ShadowPtr, OriginPtr) =
2967 getShadowOriginPtr(
Addr, IRB, ShadowTy, Alignment,
false);
2971 setShadow(&
I, getCleanShadow(&
I));
2975 insertShadowCheck(
Addr, &
I);
2977 if (MS.TrackOrigins) {
2978 if (PropagateShadow)
2979 setOrigin(&
I, IRB.
CreateLoad(MS.OriginTy, OriginPtr));
2981 setOrigin(&
I, getCleanOrigin());
2994 if (!(
RetTy->isIntOrIntVectorTy() ||
RetTy->isFPOrFPVectorTy()))
2997 unsigned NumArgOperands =
I.arg_size();
2998 for (
unsigned i = 0; i < NumArgOperands; ++i) {
2999 Type *Ty =
I.getArgOperand(i)->getType();
3005 ShadowAndOriginCombiner
SC(
this, IRB);
3006 for (
unsigned i = 0; i < NumArgOperands; ++i)
3007 SC.Add(
I.getArgOperand(i));
3024 unsigned NumArgOperands =
I.arg_size();
3025 if (NumArgOperands == 0)
3028 if (NumArgOperands == 2 &&
I.getArgOperand(0)->getType()->isPointerTy() &&
3029 I.getArgOperand(1)->getType()->isVectorTy() &&
3030 I.getType()->isVoidTy() && !
I.onlyReadsMemory()) {
3032 return handleVectorStoreIntrinsic(
I);
3035 if (NumArgOperands == 1 &&
I.getArgOperand(0)->getType()->isPointerTy() &&
3036 I.getType()->isVectorTy() &&
I.onlyReadsMemory()) {
3038 return handleVectorLoadIntrinsic(
I);
3041 if (
I.doesNotAccessMemory())
3042 if (maybeHandleSimpleNomemIntrinsic(
I))
3050 setShadow(&
I, getShadow(&
I, 0));
3051 setOrigin(&
I, getOrigin(&
I, 0));
3059 InstrumentLifetimeStart =
false;
3060 LifetimeStartList.push_back(std::make_pair(&
I, AI));
3066 Type *OpType =
Op->getType();
3068 F.getParent(), Intrinsic::bswap,
ArrayRef(&OpType, 1));
3070 setOrigin(&
I, getOrigin(
Op));
3075 Value *Src =
I.getArgOperand(0);
3081 Constant *IsZeroPoison = cast<Constant>(
I.getOperand(1));
3084 BoolShadow = IRB.
CreateOr(BoolShadow, BoolZeroPoison,
"_mscz_bs");
3087 Value *OutputShadow =
3088 IRB.
CreateSExt(BoolShadow, getShadowTy(Src),
"_mscz_os");
3090 setShadow(&
I, OutputShadow);
3091 setOriginForNaryOp(
I);
3109 void handleVectorConvertIntrinsic(
IntrinsicInst &
I,
int NumUsedElements,
3110 bool HasRoundingMode =
false) {
3112 Value *CopyOp, *ConvertOp;
3114 assert((!HasRoundingMode ||
3115 isa<ConstantInt>(
I.getArgOperand(
I.arg_size() - 1))) &&
3116 "Invalid rounding mode");
3118 switch (
I.arg_size() - HasRoundingMode) {
3120 CopyOp =
I.getArgOperand(0);
3121 ConvertOp =
I.getArgOperand(1);
3124 ConvertOp =
I.getArgOperand(0);
3138 Value *ConvertShadow = getShadow(ConvertOp);
3139 Value *AggShadow =
nullptr;
3142 ConvertShadow, ConstantInt::get(IRB.
getInt32Ty(), 0));
3143 for (
int i = 1; i < NumUsedElements; ++i) {
3145 ConvertShadow, ConstantInt::get(IRB.
getInt32Ty(), i));
3146 AggShadow = IRB.
CreateOr(AggShadow, MoreShadow);
3149 AggShadow = ConvertShadow;
3152 insertShadowCheck(AggShadow, getOrigin(ConvertOp), &
I);
3159 Value *ResultShadow = getShadow(CopyOp);
3160 Type *EltTy = cast<VectorType>(ResultShadow->
getType())->getElementType();
3161 for (
int i = 0; i < NumUsedElements; ++i) {
3163 ResultShadow, ConstantInt::getNullValue(EltTy),
3166 setShadow(&
I, ResultShadow);
3167 setOrigin(&
I, getOrigin(CopyOp));
3169 setShadow(&
I, getCleanShadow(&
I));
3170 setOrigin(&
I, getCleanOrigin());
3178 S = CreateShadowCast(IRB, S, IRB.
getInt64Ty(),
true);
3181 return CreateShadowCast(IRB, S2,
T,
true);
3189 return CreateShadowCast(IRB, S2,
T,
true);
3206 void handleVectorShiftIntrinsic(
IntrinsicInst &
I,
bool Variable) {
3212 Value *S2 = getShadow(&
I, 1);
3213 Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2)
3214 : Lower64ShadowExtend(IRB, S2, getShadowTy(&
I));
3215 Value *V1 =
I.getOperand(0);
3218 {IRB.CreateBitCast(S1, V1->getType()), V2});
3220 setShadow(&
I, IRB.
CreateOr(Shift, S2Conv));
3221 setOriginForNaryOp(
I);
3225 Type *getMMXVectorTy(
unsigned EltSizeInBits) {
3226 const unsigned X86_MMXSizeInBits = 64;
3227 assert(EltSizeInBits != 0 && (X86_MMXSizeInBits % EltSizeInBits) == 0 &&
3228 "Illegal MMX vector element size");
3230 X86_MMXSizeInBits / EltSizeInBits);
3237 case Intrinsic::x86_sse2_packsswb_128:
3238 case Intrinsic::x86_sse2_packuswb_128:
3239 return Intrinsic::x86_sse2_packsswb_128;
3241 case Intrinsic::x86_sse2_packssdw_128:
3242 case Intrinsic::x86_sse41_packusdw:
3243 return Intrinsic::x86_sse2_packssdw_128;
3245 case Intrinsic::x86_avx2_packsswb:
3246 case Intrinsic::x86_avx2_packuswb:
3247 return Intrinsic::x86_avx2_packsswb;
3249 case Intrinsic::x86_avx2_packssdw:
3250 case Intrinsic::x86_avx2_packusdw:
3251 return Intrinsic::x86_avx2_packssdw;
3253 case Intrinsic::x86_mmx_packsswb:
3254 case Intrinsic::x86_mmx_packuswb:
3255 return Intrinsic::x86_mmx_packsswb;
3257 case Intrinsic::x86_mmx_packssdw:
3258 return Intrinsic::x86_mmx_packssdw;
3272 unsigned MMXEltSizeInBits = 0) {
3276 Value *S2 = getShadow(&
I, 1);
3277 assert(
S1->getType()->isVectorTy());
3283 MMXEltSizeInBits ? getMMXVectorTy(MMXEltSizeInBits) :
S1->
getType();
3284 if (MMXEltSizeInBits) {
3292 if (MMXEltSizeInBits) {
3298 F.getParent(), getSignedPackIntrinsic(
I.getIntrinsicID()));
3301 IRB.
CreateCall(ShadowFn, {S1_ext, S2_ext},
"_msprop_vector_pack");
3302 if (MMXEltSizeInBits)
3305 setOriginForNaryOp(
I);
3309 Constant *createDppMask(
unsigned Width,
unsigned Mask) {
3322 const unsigned Width =
3323 cast<FixedVectorType>(S->
getType())->getNumElements();
3329 Value *DstMaskV = createDppMask(Width, DstMask);
3349 Value *S0 = getShadow(&
I, 0);
3353 const unsigned Width =
3354 cast<FixedVectorType>(S->
getType())->getNumElements();
3355 assert(Width == 2 || Width == 4 || Width == 8);
3357 const unsigned Mask = cast<ConstantInt>(
I.getArgOperand(2))->getZExtValue();
3358 const unsigned SrcMask =
Mask >> 4;
3359 const unsigned DstMask =
Mask & 0xf;
3362 Value *SI1 = findDppPoisonedOutput(IRB, S, SrcMask, DstMask);
3367 SI1, findDppPoisonedOutput(IRB, S, SrcMask << 4, DstMask << 4));
3374 setOriginForNaryOp(
I);
3378 C = CreateAppToShadowCast(IRB,
C);
3392 Value *Sc = getShadow(&
I, 2);
3393 Value *Oc = MS.TrackOrigins ? getOrigin(
C) : nullptr;
3398 C = convertBlendvToSelectMask(IRB,
C);
3399 Sc = convertBlendvToSelectMask(IRB, Sc);
3405 handleSelectLikeInst(
I,
C,
T,
F);
3409 void handleVectorSadIntrinsic(
IntrinsicInst &
I,
bool IsMMX =
false) {
3410 const unsigned SignificantBitsPerResultElement = 16;
3412 unsigned ZeroBitsPerResultElement =
3416 auto *Shadow0 = getShadow(&
I, 0);
3417 auto *Shadow1 = getShadow(&
I, 1);
3422 S = IRB.
CreateLShr(S, ZeroBitsPerResultElement);
3425 setOriginForNaryOp(
I);
3430 unsigned MMXEltSizeInBits = 0) {
3432 MMXEltSizeInBits ? getMMXVectorTy(MMXEltSizeInBits * 2) :
I.
getType();
3434 auto *Shadow0 = getShadow(&
I, 0);
3435 auto *Shadow1 = getShadow(&
I, 1);
3442 setOriginForNaryOp(
I);
3450 Type *ResTy = getShadowTy(&
I);
3451 auto *Shadow0 = getShadow(&
I, 0);
3452 auto *Shadow1 = getShadow(&
I, 1);
3457 setOriginForNaryOp(
I);
3465 auto *Shadow0 = getShadow(&
I, 0);
3466 auto *Shadow1 = getShadow(&
I, 1);
3468 Value *S = LowerElementShadowExtend(IRB, S0, getShadowTy(&
I));
3470 setOriginForNaryOp(
I);
3479 setOrigin(&
I, getOrigin(&
I, 0));
3487 Value *OperandShadow = getShadow(&
I, 0);
3489 Value *OperandUnsetOrPoison = IRB.
CreateOr(OperandUnsetBits, OperandShadow);
3497 setOrigin(&
I, getOrigin(&
I, 0));
3505 Value *OperandShadow = getShadow(&
I, 0);
3506 Value *OperandSetOrPoison = IRB.
CreateOr(
I.getOperand(0), OperandShadow);
3514 setOrigin(&
I, getOrigin(&
I, 0));
3522 getShadowOriginPtr(
Addr, IRB, Ty,
Align(1),
true).first;
3527 insertShadowCheck(
Addr, &
I);
3538 Value *ShadowPtr, *OriginPtr;
3539 std::tie(ShadowPtr, OriginPtr) =
3540 getShadowOriginPtr(
Addr, IRB, Ty, Alignment,
false);
3543 insertShadowCheck(
Addr, &
I);
3546 Value *Origin = MS.TrackOrigins ? IRB.
CreateLoad(MS.OriginTy, OriginPtr)
3548 insertShadowCheck(Shadow, Origin, &
I);
3555 Value *PassThru =
I.getArgOperand(2);
3558 insertShadowCheck(
Ptr, &
I);
3559 insertShadowCheck(Mask, &
I);
3562 if (!PropagateShadow) {
3563 setShadow(&
I, getCleanShadow(&
I));
3564 setOrigin(&
I, getCleanOrigin());
3568 Type *ShadowTy = getShadowTy(&
I);
3569 Type *ElementShadowTy = cast<VectorType>(ShadowTy)->getElementType();
3570 auto [ShadowPtr, OriginPtr] =
3571 getShadowOriginPtr(
Ptr, IRB, ElementShadowTy, {},
false);
3574 ShadowTy, ShadowPtr, Mask, getShadow(PassThru),
"_msmaskedexpload");
3576 setShadow(&
I, Shadow);
3579 setOrigin(&
I, getCleanOrigin());
3584 Value *Values =
I.getArgOperand(0);
3589 insertShadowCheck(
Ptr, &
I);
3590 insertShadowCheck(Mask, &
I);
3593 Value *Shadow = getShadow(Values);
3594 Type *ElementShadowTy =
3595 getShadowTy(cast<VectorType>(Values->
getType())->getElementType());
3596 auto [ShadowPtr, OriginPtrs] =
3597 getShadowOriginPtr(
Ptr, IRB, ElementShadowTy, {},
true);
3606 Value *Ptrs =
I.getArgOperand(0);
3607 const Align Alignment(
3608 cast<ConstantInt>(
I.getArgOperand(1))->getZExtValue());
3610 Value *PassThru =
I.getArgOperand(3);
3612 Type *PtrsShadowTy = getShadowTy(Ptrs);
3614 insertShadowCheck(Mask, &
I);
3618 insertShadowCheck(MaskedPtrShadow, getOrigin(Ptrs), &
I);
3621 if (!PropagateShadow) {
3622 setShadow(&
I, getCleanShadow(&
I));
3623 setOrigin(&
I, getCleanOrigin());
3627 Type *ShadowTy = getShadowTy(&
I);
3628 Type *ElementShadowTy = cast<VectorType>(ShadowTy)->getElementType();
3629 auto [ShadowPtrs, OriginPtrs] = getShadowOriginPtr(
3630 Ptrs, IRB, ElementShadowTy, Alignment,
false);
3634 getShadow(PassThru),
"_msmaskedgather");
3636 setShadow(&
I, Shadow);
3639 setOrigin(&
I, getCleanOrigin());
3644 Value *Values =
I.getArgOperand(0);
3645 Value *Ptrs =
I.getArgOperand(1);
3646 const Align Alignment(
3647 cast<ConstantInt>(
I.getArgOperand(2))->getZExtValue());
3650 Type *PtrsShadowTy = getShadowTy(Ptrs);
3652 insertShadowCheck(Mask, &
I);
3656 insertShadowCheck(MaskedPtrShadow, getOrigin(Ptrs), &
I);
3659 Value *Shadow = getShadow(Values);
3660 Type *ElementShadowTy =
3661 getShadowTy(cast<VectorType>(Values->
getType())->getElementType());
3662 auto [ShadowPtrs, OriginPtrs] = getShadowOriginPtr(
3663 Ptrs, IRB, ElementShadowTy, Alignment,
true);
3672 Value *
V =
I.getArgOperand(0);
3674 const Align Alignment(
3675 cast<ConstantInt>(
I.getArgOperand(2))->getZExtValue());
3677 Value *Shadow = getShadow(V);
3680 insertShadowCheck(
Ptr, &
I);
3681 insertShadowCheck(Mask, &
I);
3686 std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
3687 Ptr, IRB, Shadow->
getType(), Alignment,
true);
3691 if (!MS.TrackOrigins)
3694 auto &
DL =
F.getDataLayout();
3695 paintOrigin(IRB, getOrigin(V), OriginPtr,
3703 const Align Alignment(
3704 cast<ConstantInt>(
I.getArgOperand(1))->getZExtValue());
3706 Value *PassThru =
I.getArgOperand(3);
3709 insertShadowCheck(
Ptr, &
I);
3710 insertShadowCheck(Mask, &
I);
3713 if (!PropagateShadow) {
3714 setShadow(&
I, getCleanShadow(&
I));
3715 setOrigin(&
I, getCleanOrigin());
3719 Type *ShadowTy = getShadowTy(&
I);
3720 Value *ShadowPtr, *OriginPtr;
3721 std::tie(ShadowPtr, OriginPtr) =
3722 getShadowOriginPtr(
Ptr, IRB, ShadowTy, Alignment,
false);
3724 getShadow(PassThru),
"_msmaskedld"));
3726 if (!MS.TrackOrigins)
3733 Value *NotNull = convertToBool(MaskedPassThruShadow, IRB,
"_mscmp");
3738 setOrigin(&
I, Origin);
3748 Type *ShadowTy = getShadowTy(&
I);
3751 Value *SMask = getShadow(&
I, 1);
3756 {getShadow(&I, 0), I.getOperand(1)});
3759 setOriginForNaryOp(
I);
3764 for (
unsigned X = OddElements ? 1 : 0;
X < Width;
X += 2) {
3781 cast<FixedVectorType>(
I.getArgOperand(0)->getType())->getNumElements();
3782 assert(isa<ConstantInt>(
I.getArgOperand(2)) &&
3783 "pclmul 3rd operand must be a constant");
3784 unsigned Imm = cast<ConstantInt>(
I.getArgOperand(2))->getZExtValue();
3786 getPclmulMask(Width, Imm & 0x01));
3788 getPclmulMask(Width, Imm & 0x10));
3789 ShadowAndOriginCombiner SOC(
this, IRB);
3790 SOC.Add(Shuf0, getOrigin(&
I, 0));
3791 SOC.Add(Shuf1, getOrigin(&
I, 1));
3799 cast<FixedVectorType>(
I.getArgOperand(0)->getType())->getNumElements();
3801 Value *Second = getShadow(&
I, 1);
3804 Mask.push_back(Width);
3805 for (
unsigned i = 1; i < Width; i++)
3809 setShadow(&
I, Shadow);
3810 setOriginForNaryOp(
I);
3815 Value *Shadow0 = getShadow(&
I, 0);
3816 Value *Shadow1 = getShadow(&
I, 1);
3822 setShadow(&
I, Shadow);
3823 setOriginForNaryOp(
I);
3829 cast<FixedVectorType>(
I.getArgOperand(0)->getType())->getNumElements();
3831 Value *Second = getShadow(&
I, 1);
3835 Mask.push_back(Width);
3836 for (
unsigned i = 1; i < Width; i++)
3840 setShadow(&
I, Shadow);
3841 setOriginForNaryOp(
I);
3848 assert(
I.getType()->isIntOrIntVectorTy());
3849 assert(
I.getArgOperand(0)->getType() ==
I.getType());
3853 setShadow(&
I, getShadow(&
I, 0));
3854 setOrigin(&
I, getOrigin(&
I, 0));
3859 Value *Shadow = getShadow(&
I, 0);
3860 setShadow(&
I, IRB.
CreateICmpNE(Shadow, getCleanShadow(Shadow)));
3861 setOrigin(&
I, getOrigin(&
I, 0));
3866 Value *Shadow0 = getShadow(&
I, 0);
3867 Value *Shadow1 = getShadow(&
I, 1);
3870 IRB.
CreateICmpNE(ShadowElt0, getCleanShadow(ShadowElt0));
3876 setShadow(&
I, Shadow);
3877 setOriginForNaryOp(
I);
3894 void handleNEONVectorStoreIntrinsic(
IntrinsicInst &
I,
bool useLane) {
3898 int numArgOperands =
I.arg_size();
3901 assert(numArgOperands >= 1);
3902 Value *
Addr =
I.getArgOperand(numArgOperands - 1);
3904 int skipTrailingOperands = 1;
3907 insertShadowCheck(
Addr, &
I);
3911 skipTrailingOperands++;
3912 assert(numArgOperands >=
static_cast<int>(skipTrailingOperands));
3914 I.getArgOperand(numArgOperands - skipTrailingOperands)->getType()));
3919 for (
int i = 0; i < numArgOperands - skipTrailingOperands; i++) {
3920 assert(isa<FixedVectorType>(
I.getArgOperand(i)->getType()));
3921 Value *Shadow = getShadow(&
I, i);
3922 ShadowArgs.
append(1, Shadow);
3937 cast<FixedVectorType>(
I.getArgOperand(0)->getType())->getElementType(),
3938 cast<FixedVectorType>(
I.getArgOperand(0)->getType())->getNumElements() *
3939 (numArgOperands - skipTrailingOperands));
3940 Type *OutputShadowTy = getShadowTy(OutputVectorTy);
3944 I.getArgOperand(numArgOperands - skipTrailingOperands));
3946 Value *OutputShadowPtr, *OutputOriginPtr;
3948 std::tie(OutputShadowPtr, OutputOriginPtr) = getShadowOriginPtr(
3949 Addr, IRB, OutputShadowTy,
Align(1),
true);
3950 ShadowArgs.
append(1, OutputShadowPtr);
3956 if (MS.TrackOrigins) {
3964 OriginCombiner
OC(
this, IRB);
3965 for (
int i = 0; i < numArgOperands - skipTrailingOperands; i++)
3966 OC.Add(
I.getArgOperand(i));
3969 OC.DoneAndStoreOrigin(
DL.getTypeStoreSize(OutputVectorTy),
3975 switch (
I.getIntrinsicID()) {
3976 case Intrinsic::uadd_with_overflow:
3977 case Intrinsic::sadd_with_overflow:
3978 case Intrinsic::usub_with_overflow:
3979 case Intrinsic::ssub_with_overflow:
3980 case Intrinsic::umul_with_overflow:
3981 case Intrinsic::smul_with_overflow:
3982 handleArithmeticWithOverflow(
I);
3984 case Intrinsic::abs:
3985 handleAbsIntrinsic(
I);
3987 case Intrinsic::is_fpclass:
3990 case Intrinsic::lifetime_start:
3991 handleLifetimeStart(
I);
3993 case Intrinsic::launder_invariant_group:
3994 case Intrinsic::strip_invariant_group:
3995 handleInvariantGroup(
I);
3997 case Intrinsic::bswap:
4000 case Intrinsic::ctlz:
4001 case Intrinsic::cttz:
4002 handleCountZeroes(
I);
4004 case Intrinsic::masked_compressstore:
4005 handleMaskedCompressStore(
I);
4007 case Intrinsic::masked_expandload:
4008 handleMaskedExpandLoad(
I);
4010 case Intrinsic::masked_gather:
4011 handleMaskedGather(
I);
4013 case Intrinsic::masked_scatter:
4014 handleMaskedScatter(
I);
4016 case Intrinsic::masked_store:
4017 handleMaskedStore(
I);
4019 case Intrinsic::masked_load:
4020 handleMaskedLoad(
I);
4022 case Intrinsic::vector_reduce_and:
4023 handleVectorReduceAndIntrinsic(
I);
4025 case Intrinsic::vector_reduce_or:
4026 handleVectorReduceOrIntrinsic(
I);
4028 case Intrinsic::vector_reduce_add:
4029 case Intrinsic::vector_reduce_xor:
4030 case Intrinsic::vector_reduce_mul:
4031 handleVectorReduceIntrinsic(
I);
4033 case Intrinsic::x86_sse_stmxcsr:
4036 case Intrinsic::x86_sse_ldmxcsr:
4039 case Intrinsic::x86_avx512_vcvtsd2usi64:
4040 case Intrinsic::x86_avx512_vcvtsd2usi32:
4041 case Intrinsic::x86_avx512_vcvtss2usi64:
4042 case Intrinsic::x86_avx512_vcvtss2usi32:
4043 case Intrinsic::x86_avx512_cvttss2usi64:
4044 case Intrinsic::x86_avx512_cvttss2usi:
4045 case Intrinsic::x86_avx512_cvttsd2usi64:
4046 case Intrinsic::x86_avx512_cvttsd2usi:
4047 case Intrinsic::x86_avx512_cvtusi2ss:
4048 case Intrinsic::x86_avx512_cvtusi642sd:
4049 case Intrinsic::x86_avx512_cvtusi642ss:
4050 handleVectorConvertIntrinsic(
I, 1,
true);
4052 case Intrinsic::x86_sse2_cvtsd2si64:
4053 case Intrinsic::x86_sse2_cvtsd2si:
4054 case Intrinsic::x86_sse2_cvtsd2ss:
4055 case Intrinsic::x86_sse2_cvttsd2si64:
4056 case Intrinsic::x86_sse2_cvttsd2si:
4057 case Intrinsic::x86_sse_cvtss2si64:
4058 case Intrinsic::x86_sse_cvtss2si:
4059 case Intrinsic::x86_sse_cvttss2si64:
4060 case Intrinsic::x86_sse_cvttss2si:
4061 handleVectorConvertIntrinsic(
I, 1);
4063 case Intrinsic::x86_sse_cvtps2pi:
4064 case Intrinsic::x86_sse_cvttps2pi:
4065 handleVectorConvertIntrinsic(
I, 2);
4068 case Intrinsic::x86_avx512_psll_w_512:
4069 case Intrinsic::x86_avx512_psll_d_512:
4070 case Intrinsic::x86_avx512_psll_q_512:
4071 case Intrinsic::x86_avx512_pslli_w_512:
4072 case Intrinsic::x86_avx512_pslli_d_512:
4073 case Intrinsic::x86_avx512_pslli_q_512:
4074 case Intrinsic::x86_avx512_psrl_w_512:
4075 case Intrinsic::x86_avx512_psrl_d_512:
4076 case Intrinsic::x86_avx512_psrl_q_512:
4077 case Intrinsic::x86_avx512_psra_w_512:
4078 case Intrinsic::x86_avx512_psra_d_512:
4079 case Intrinsic::x86_avx512_psra_q_512:
4080 case Intrinsic::x86_avx512_psrli_w_512:
4081 case Intrinsic::x86_avx512_psrli_d_512:
4082 case Intrinsic::x86_avx512_psrli_q_512:
4083 case Intrinsic::x86_avx512_psrai_w_512:
4084 case Intrinsic::x86_avx512_psrai_d_512:
4085 case Intrinsic::x86_avx512_psrai_q_512:
4086 case Intrinsic::x86_avx512_psra_q_256:
4087 case Intrinsic::x86_avx512_psra_q_128:
4088 case Intrinsic::x86_avx512_psrai_q_256:
4089 case Intrinsic::x86_avx512_psrai_q_128:
4090 case Intrinsic::x86_avx2_psll_w:
4091 case Intrinsic::x86_avx2_psll_d:
4092 case Intrinsic::x86_avx2_psll_q:
4093 case Intrinsic::x86_avx2_pslli_w:
4094 case Intrinsic::x86_avx2_pslli_d:
4095 case Intrinsic::x86_avx2_pslli_q:
4096 case Intrinsic::x86_avx2_psrl_w:
4097 case Intrinsic::x86_avx2_psrl_d:
4098 case Intrinsic::x86_avx2_psrl_q:
4099 case Intrinsic::x86_avx2_psra_w:
4100 case Intrinsic::x86_avx2_psra_d:
4101 case Intrinsic::x86_avx2_psrli_w:
4102 case Intrinsic::x86_avx2_psrli_d:
4103 case Intrinsic::x86_avx2_psrli_q:
4104 case Intrinsic::x86_avx2_psrai_w:
4105 case Intrinsic::x86_avx2_psrai_d:
4106 case Intrinsic::x86_sse2_psll_w:
4107 case Intrinsic::x86_sse2_psll_d:
4108 case Intrinsic::x86_sse2_psll_q:
4109 case Intrinsic::x86_sse2_pslli_w:
4110 case Intrinsic::x86_sse2_pslli_d:
4111 case Intrinsic::x86_sse2_pslli_q:
4112 case Intrinsic::x86_sse2_psrl_w:
4113 case Intrinsic::x86_sse2_psrl_d:
4114 case Intrinsic::x86_sse2_psrl_q:
4115 case Intrinsic::x86_sse2_psra_w:
4116 case Intrinsic::x86_sse2_psra_d:
4117 case Intrinsic::x86_sse2_psrli_w:
4118 case Intrinsic::x86_sse2_psrli_d:
4119 case Intrinsic::x86_sse2_psrli_q:
4120 case Intrinsic::x86_sse2_psrai_w:
4121 case Intrinsic::x86_sse2_psrai_d:
4122 case Intrinsic::x86_mmx_psll_w:
4123 case Intrinsic::x86_mmx_psll_d:
4124 case Intrinsic::x86_mmx_psll_q:
4125 case Intrinsic::x86_mmx_pslli_w:
4126 case Intrinsic::x86_mmx_pslli_d:
4127 case Intrinsic::x86_mmx_pslli_q:
4128 case Intrinsic::x86_mmx_psrl_w:
4129 case Intrinsic::x86_mmx_psrl_d:
4130 case Intrinsic::x86_mmx_psrl_q:
4131 case Intrinsic::x86_mmx_psra_w:
4132 case Intrinsic::x86_mmx_psra_d:
4133 case Intrinsic::x86_mmx_psrli_w:
4134 case Intrinsic::x86_mmx_psrli_d:
4135 case Intrinsic::x86_mmx_psrli_q:
4136 case Intrinsic::x86_mmx_psrai_w:
4137 case Intrinsic::x86_mmx_psrai_d:
4138 case Intrinsic::aarch64_neon_rshrn:
4139 case Intrinsic::aarch64_neon_sqrshl:
4140 case Intrinsic::aarch64_neon_sqrshrn:
4141 case Intrinsic::aarch64_neon_sqrshrun:
4142 case Intrinsic::aarch64_neon_sqshl:
4143 case Intrinsic::aarch64_neon_sqshlu:
4144 case Intrinsic::aarch64_neon_sqshrn:
4145 case Intrinsic::aarch64_neon_sqshrun:
4146 case Intrinsic::aarch64_neon_srshl:
4147 case Intrinsic::aarch64_neon_sshl:
4148 case Intrinsic::aarch64_neon_uqrshl:
4149 case Intrinsic::aarch64_neon_uqrshrn:
4150 case Intrinsic::aarch64_neon_uqshl:
4151 case Intrinsic::aarch64_neon_uqshrn:
4152 case Intrinsic::aarch64_neon_urshl:
4153 case Intrinsic::aarch64_neon_ushl:
4155 handleVectorShiftIntrinsic(
I,
false);
4157 case Intrinsic::x86_avx2_psllv_d:
4158 case Intrinsic::x86_avx2_psllv_d_256:
4159 case Intrinsic::x86_avx512_psllv_d_512:
4160 case Intrinsic::x86_avx2_psllv_q:
4161 case Intrinsic::x86_avx2_psllv_q_256:
4162 case Intrinsic::x86_avx512_psllv_q_512:
4163 case Intrinsic::x86_avx2_psrlv_d:
4164 case Intrinsic::x86_avx2_psrlv_d_256:
4165 case Intrinsic::x86_avx512_psrlv_d_512:
4166 case Intrinsic::x86_avx2_psrlv_q:
4167 case Intrinsic::x86_avx2_psrlv_q_256:
4168 case Intrinsic::x86_avx512_psrlv_q_512:
4169 case Intrinsic::x86_avx2_psrav_d:
4170 case Intrinsic::x86_avx2_psrav_d_256:
4171 case Intrinsic::x86_avx512_psrav_d_512:
4172 case Intrinsic::x86_avx512_psrav_q_128:
4173 case Intrinsic::x86_avx512_psrav_q_256:
4174 case Intrinsic::x86_avx512_psrav_q_512:
4175 handleVectorShiftIntrinsic(
I,
true);
4178 case Intrinsic::x86_sse2_packsswb_128:
4179 case Intrinsic::x86_sse2_packssdw_128:
4180 case Intrinsic::x86_sse2_packuswb_128:
4181 case Intrinsic::x86_sse41_packusdw:
4182 case Intrinsic::x86_avx2_packsswb:
4183 case Intrinsic::x86_avx2_packssdw:
4184 case Intrinsic::x86_avx2_packuswb:
4185 case Intrinsic::x86_avx2_packusdw:
4186 handleVectorPackIntrinsic(
I);
4189 case Intrinsic::x86_sse41_pblendvb:
4190 case Intrinsic::x86_sse41_blendvpd:
4191 case Intrinsic::x86_sse41_blendvps:
4192 case Intrinsic::x86_avx_blendv_pd_256:
4193 case Intrinsic::x86_avx_blendv_ps_256:
4194 case Intrinsic::x86_avx2_pblendvb:
4195 handleBlendvIntrinsic(
I);
4198 case Intrinsic::x86_avx_dp_ps_256:
4199 case Intrinsic::x86_sse41_dppd:
4200 case Intrinsic::x86_sse41_dpps:
4201 handleDppIntrinsic(
I);
4204 case Intrinsic::x86_mmx_packsswb:
4205 case Intrinsic::x86_mmx_packuswb:
4206 handleVectorPackIntrinsic(
I, 16);
4209 case Intrinsic::x86_mmx_packssdw:
4210 handleVectorPackIntrinsic(
I, 32);
4213 case Intrinsic::x86_mmx_psad_bw:
4214 handleVectorSadIntrinsic(
I,
true);
4216 case Intrinsic::x86_sse2_psad_bw:
4217 case Intrinsic::x86_avx2_psad_bw:
4218 handleVectorSadIntrinsic(
I);
4221 case Intrinsic::x86_sse2_pmadd_wd:
4222 case Intrinsic::x86_avx2_pmadd_wd:
4223 case Intrinsic::x86_ssse3_pmadd_ub_sw_128:
4224 case Intrinsic::x86_avx2_pmadd_ub_sw:
4225 handleVectorPmaddIntrinsic(
I);
4228 case Intrinsic::x86_ssse3_pmadd_ub_sw:
4229 handleVectorPmaddIntrinsic(
I, 8);
4232 case Intrinsic::x86_mmx_pmadd_wd:
4233 handleVectorPmaddIntrinsic(
I, 16);
4236 case Intrinsic::x86_sse_cmp_ss:
4237 case Intrinsic::x86_sse2_cmp_sd:
4238 case Intrinsic::x86_sse_comieq_ss:
4239 case Intrinsic::x86_sse_comilt_ss:
4240 case Intrinsic::x86_sse_comile_ss:
4241 case Intrinsic::x86_sse_comigt_ss:
4242 case Intrinsic::x86_sse_comige_ss:
4243 case Intrinsic::x86_sse_comineq_ss:
4244 case Intrinsic::x86_sse_ucomieq_ss:
4245 case Intrinsic::x86_sse_ucomilt_ss:
4246 case Intrinsic::x86_sse_ucomile_ss:
4247 case Intrinsic::x86_sse_ucomigt_ss:
4248 case Intrinsic::x86_sse_ucomige_ss:
4249 case Intrinsic::x86_sse_ucomineq_ss:
4250 case Intrinsic::x86_sse2_comieq_sd:
4251 case Intrinsic::x86_sse2_comilt_sd:
4252 case Intrinsic::x86_sse2_comile_sd:
4253 case Intrinsic::x86_sse2_comigt_sd:
4254 case Intrinsic::x86_sse2_comige_sd:
4255 case Intrinsic::x86_sse2_comineq_sd:
4256 case Intrinsic::x86_sse2_ucomieq_sd:
4257 case Intrinsic::x86_sse2_ucomilt_sd:
4258 case Intrinsic::x86_sse2_ucomile_sd:
4259 case Intrinsic::x86_sse2_ucomigt_sd:
4260 case Intrinsic::x86_sse2_ucomige_sd:
4261 case Intrinsic::x86_sse2_ucomineq_sd:
4262 handleVectorCompareScalarIntrinsic(
I);
4265 case Intrinsic::x86_avx_cmp_pd_256:
4266 case Intrinsic::x86_avx_cmp_ps_256:
4267 case Intrinsic::x86_sse2_cmp_pd:
4268 case Intrinsic::x86_sse_cmp_ps:
4269 handleVectorComparePackedIntrinsic(
I);
4272 case Intrinsic::x86_bmi_bextr_32:
4273 case Intrinsic::x86_bmi_bextr_64:
4274 case Intrinsic::x86_bmi_bzhi_32:
4275 case Intrinsic::x86_bmi_bzhi_64:
4276 case Intrinsic::x86_bmi_pdep_32:
4277 case Intrinsic::x86_bmi_pdep_64:
4278 case Intrinsic::x86_bmi_pext_32:
4279 case Intrinsic::x86_bmi_pext_64:
4280 handleBmiIntrinsic(
I);
4283 case Intrinsic::x86_pclmulqdq:
4284 case Intrinsic::x86_pclmulqdq_256:
4285 case Intrinsic::x86_pclmulqdq_512:
4286 handlePclmulIntrinsic(
I);
4289 case Intrinsic::x86_sse41_round_sd:
4290 case Intrinsic::x86_sse41_round_ss:
4291 handleUnarySdSsIntrinsic(
I);
4293 case Intrinsic::x86_sse2_max_sd:
4294 case Intrinsic::x86_sse_max_ss:
4295 case Intrinsic::x86_sse2_min_sd:
4296 case Intrinsic::x86_sse_min_ss:
4297 handleBinarySdSsIntrinsic(
I);
4300 case Intrinsic::x86_avx_vtestc_pd:
4301 case Intrinsic::x86_avx_vtestc_pd_256:
4302 case Intrinsic::x86_avx_vtestc_ps:
4303 case Intrinsic::x86_avx_vtestc_ps_256:
4304 case Intrinsic::x86_avx_vtestnzc_pd:
4305 case Intrinsic::x86_avx_vtestnzc_pd_256:
4306 case Intrinsic::x86_avx_vtestnzc_ps:
4307 case Intrinsic::x86_avx_vtestnzc_ps_256:
4308 case Intrinsic::x86_avx_vtestz_pd:
4309 case Intrinsic::x86_avx_vtestz_pd_256:
4310 case Intrinsic::x86_avx_vtestz_ps:
4311 case Intrinsic::x86_avx_vtestz_ps_256:
4312 case Intrinsic::x86_avx_ptestc_256:
4313 case Intrinsic::x86_avx_ptestnzc_256:
4314 case Intrinsic::x86_avx_ptestz_256:
4315 case Intrinsic::x86_sse41_ptestc:
4316 case Intrinsic::x86_sse41_ptestnzc:
4317 case Intrinsic::x86_sse41_ptestz:
4318 handleVtestIntrinsic(
I);
4321 case Intrinsic::fshl:
4322 case Intrinsic::fshr:
4323 handleFunnelShift(
I);
4326 case Intrinsic::is_constant:
4328 setShadow(&
I, getCleanShadow(&
I));
4329 setOrigin(&
I, getCleanOrigin());
4332 case Intrinsic::aarch64_neon_st1x2:
4333 case Intrinsic::aarch64_neon_st1x3:
4334 case Intrinsic::aarch64_neon_st1x4:
4335 case Intrinsic::aarch64_neon_st2:
4336 case Intrinsic::aarch64_neon_st3:
4337 case Intrinsic::aarch64_neon_st4: {
4338 handleNEONVectorStoreIntrinsic(
I,
false);
4342 case Intrinsic::aarch64_neon_st2lane:
4343 case Intrinsic::aarch64_neon_st3lane:
4344 case Intrinsic::aarch64_neon_st4lane: {
4345 handleNEONVectorStoreIntrinsic(
I,
true);
4350 if (!handleUnknownIntrinsic(
I))
4351 visitInstruction(
I);
4356 void visitLibAtomicLoad(
CallBase &CB) {
4358 assert(isa<CallInst>(CB));
4367 Value *NewOrdering =
4371 NextNodeIRBuilder NextIRB(&CB);
4372 Value *SrcShadowPtr, *SrcOriginPtr;
4373 std::tie(SrcShadowPtr, SrcOriginPtr) =
4374 getShadowOriginPtr(SrcPtr, NextIRB, NextIRB.getInt8Ty(),
Align(1),
4376 Value *DstShadowPtr =
4377 getShadowOriginPtr(DstPtr, NextIRB, NextIRB.getInt8Ty(),
Align(1),
4381 NextIRB.CreateMemCpy(DstShadowPtr,
Align(1), SrcShadowPtr,
Align(1),
Size);
4382 if (MS.TrackOrigins) {
4383 Value *SrcOrigin = NextIRB.CreateAlignedLoad(MS.OriginTy, SrcOriginPtr,
4385 Value *NewOrigin = updateOrigin(SrcOrigin, NextIRB);
4386 NextIRB.CreateCall(MS.MsanSetOriginFn, {DstPtr, Size, NewOrigin});
4390 void visitLibAtomicStore(
CallBase &CB) {
4397 Value *NewOrdering =
4401 Value *DstShadowPtr =
4419 visitAsmInstruction(CB);
4421 visitInstruction(CB);
4430 case LibFunc_atomic_load:
4431 if (!isa<CallInst>(CB)) {
4432 llvm::errs() <<
"MSAN -- cannot instrument invoke of libatomic load."
4436 visitLibAtomicLoad(CB);
4438 case LibFunc_atomic_store:
4439 visitLibAtomicStore(CB);
4446 if (
auto *Call = dyn_cast<CallInst>(&CB)) {
4447 assert(!isa<IntrinsicInst>(Call) &&
"intrinsics are handled elsewhere");
4455 B.addAttribute(Attribute::Memory).addAttribute(Attribute::Speculatable);
4457 Call->removeFnAttrs(
B);
4459 Func->removeFnAttrs(
B);
4465 bool MayCheckCall = MS.EagerChecks;
4469 MayCheckCall &= !
Func->getName().starts_with(
"__sanitizer_unaligned_");
4472 unsigned ArgOffset = 0;
4475 if (!
A->getType()->isSized()) {
4476 LLVM_DEBUG(
dbgs() <<
"Arg " << i <<
" is not sized: " << CB <<
"\n");
4480 if (
A->getType()->isScalableTy()) {
4481 LLVM_DEBUG(
dbgs() <<
"Arg " << i <<
" is vscale: " << CB <<
"\n");
4483 insertShadowCheck(
A, &CB);
4492 bool EagerCheck = MayCheckCall && !ByVal && NoUndef;
4495 insertShadowCheck(
A, &CB);
4496 Size =
DL.getTypeAllocSize(
A->getType());
4502 Value *ArgShadow = getShadow(
A);
4503 Value *ArgShadowBase = getShadowPtrForArgument(IRB, ArgOffset);
4505 <<
" Shadow: " << *ArgShadow <<
"\n");
4509 assert(
A->getType()->isPointerTy() &&
4510 "ByVal argument is not a pointer!");
4518 Value *AShadowPtr, *AOriginPtr;
4519 std::tie(AShadowPtr, AOriginPtr) =
4520 getShadowOriginPtr(
A, IRB, IRB.
getInt8Ty(), Alignment,
4522 if (!PropagateShadow) {
4529 if (MS.TrackOrigins) {
4530 Value *ArgOriginBase = getOriginPtrForArgument(IRB, ArgOffset);
4544 Size =
DL.getTypeAllocSize(
A->getType());
4549 Constant *Cst = dyn_cast<Constant>(ArgShadow);
4550 if (MS.TrackOrigins && !(Cst && Cst->
isNullValue())) {
4552 getOriginPtrForArgument(IRB, ArgOffset));
4556 assert(Store !=
nullptr);
4565 if (FT->isVarArg()) {
4566 VAHelper->visitCallBase(CB, IRB);
4573 if (isa<CallInst>(CB) && cast<CallInst>(CB).isMustTailCall())
4576 if (MayCheckCall && CB.
hasRetAttr(Attribute::NoUndef)) {
4577 setShadow(&CB, getCleanShadow(&CB));
4578 setOrigin(&CB, getCleanOrigin());
4584 Value *
Base = getShadowPtrForRetval(IRBBefore);
4585 IRBBefore.CreateAlignedStore(getCleanShadow(&CB),
Base,
4588 if (isa<CallInst>(CB)) {
4592 BasicBlock *NormalDest = cast<InvokeInst>(CB).getNormalDest();
4597 setShadow(&CB, getCleanShadow(&CB));
4598 setOrigin(&CB, getCleanOrigin());
4605 "Could not find insertion point for retval shadow load");
4608 Value *RetvalShadow = IRBAfter.CreateAlignedLoad(
4609 getShadowTy(&CB), getShadowPtrForRetval(IRBAfter),
4611 setShadow(&CB, RetvalShadow);
4612 if (MS.TrackOrigins)
4613 setOrigin(&CB, IRBAfter.CreateLoad(MS.OriginTy,
4614 getOriginPtrForRetval()));
4618 if (
auto *
I = dyn_cast<BitCastInst>(RetVal)) {
4619 RetVal =
I->getOperand(0);
4621 if (
auto *
I = dyn_cast<CallInst>(RetVal)) {
4622 return I->isMustTailCall();
4629 Value *RetVal =
I.getReturnValue();
4635 Value *ShadowPtr = getShadowPtrForRetval(IRB);
4636 bool HasNoUndef =
F.hasRetAttribute(Attribute::NoUndef);
4637 bool StoreShadow = !(MS.EagerChecks && HasNoUndef);
4640 bool EagerCheck = (MS.EagerChecks && HasNoUndef) || (
F.getName() ==
"main");
4642 Value *Shadow = getShadow(RetVal);
4643 bool StoreOrigin =
true;
4645 insertShadowCheck(RetVal, &
I);
4646 Shadow = getCleanShadow(RetVal);
4647 StoreOrigin =
false;
4654 if (MS.TrackOrigins && StoreOrigin)
4655 IRB.
CreateStore(getOrigin(RetVal), getOriginPtrForRetval());
4661 if (!PropagateShadow) {
4662 setShadow(&
I, getCleanShadow(&
I));
4663 setOrigin(&
I, getCleanOrigin());
4667 ShadowPHINodes.push_back(&
I);
4668 setShadow(&
I, IRB.
CreatePHI(getShadowTy(&
I),
I.getNumIncomingValues(),
4670 if (MS.TrackOrigins)
4672 &
I, IRB.
CreatePHI(MS.OriginTy,
I.getNumIncomingValues(),
"_msphi_o"));
4689 IRB.
CreateCall(MS.MsanPoisonStackFn, {&I, Len});
4691 Value *ShadowBase, *OriginBase;
4692 std::tie(ShadowBase, OriginBase) = getShadowOriginPtr(
4699 if (PoisonStack && MS.TrackOrigins) {
4700 Value *Idptr = getLocalVarIdptr(
I);
4702 Value *Descr = getLocalVarDescription(
I);
4703 IRB.
CreateCall(MS.MsanSetAllocaOriginWithDescriptionFn,
4704 {&I, Len, Idptr, Descr});
4706 IRB.
CreateCall(MS.MsanSetAllocaOriginNoDescriptionFn, {&I, Len, Idptr});
4712 Value *Descr = getLocalVarDescription(
I);
4714 IRB.
CreateCall(MS.MsanPoisonAllocaFn, {&I, Len, Descr});
4716 IRB.
CreateCall(MS.MsanUnpoisonAllocaFn, {&I, Len});
4723 NextNodeIRBuilder IRB(InsPoint);
4725 TypeSize TS =
DL.getTypeAllocSize(
I.getAllocatedType());
4727 if (
I.isArrayAllocation())
4731 if (MS.CompileKernel)
4732 poisonAllocaKmsan(
I, IRB, Len);
4734 poisonAllocaUserspace(
I, IRB, Len);
4738 setShadow(&
I, getCleanShadow(&
I));
4739 setOrigin(&
I, getCleanOrigin());
4751 handleSelectLikeInst(
I,
B,
C,
D);
4757 Value *Sb = getShadow(
B);
4758 Value *Sc = getShadow(
C);
4759 Value *Sd = getShadow(
D);
4761 Value *Ob = MS.TrackOrigins ? getOrigin(
B) : nullptr;
4762 Value *Oc = MS.TrackOrigins ? getOrigin(
C) : nullptr;
4763 Value *Od = MS.TrackOrigins ? getOrigin(
D) : nullptr;
4768 if (
I.getType()->isAggregateType()) {
4772 Sa1 = getPoisonedShadow(getShadowTy(
I.getType()));
4780 C = CreateAppToShadowCast(IRB,
C);
4781 D = CreateAppToShadowCast(IRB,
D);
4788 if (MS.TrackOrigins) {
4791 if (
B->getType()->isVectorTy()) {
4792 B = convertToBool(
B, IRB);
4793 Sb = convertToBool(Sb, IRB);
4804 setShadow(&
I, getCleanShadow(&
I));
4805 setOrigin(&
I, getCleanOrigin());
4809 setShadow(&
I, getCleanShadow(&
I));
4810 setOrigin(&
I, getCleanOrigin());
4814 setShadow(&
I, getCleanShadow(&
I));
4815 setOrigin(&
I, getCleanOrigin());
4822 Value *Agg =
I.getAggregateOperand();
4824 Value *AggShadow = getShadow(Agg);
4828 setShadow(&
I, ResShadow);
4829 setOriginForNaryOp(
I);
4835 Value *AggShadow = getShadow(
I.getAggregateOperand());
4836 Value *InsShadow = getShadow(
I.getInsertedValueOperand());
4842 setOriginForNaryOp(
I);
4846 if (
CallInst *CI = dyn_cast<CallInst>(&
I)) {
4849 errs() <<
"ZZZ " <<
I.getOpcodeName() <<
"\n";
4851 errs() <<
"QQQ " <<
I <<
"\n";
4878 insertShadowCheck(Operand, &
I);
4885 auto Size =
DL.getTypeStoreSize(ElemTy);
4887 if (MS.CompileKernel) {
4888 IRB.
CreateCall(MS.MsanInstrumentAsmStoreFn, {Operand, SizeVal});
4894 auto [ShadowPtr,
_] =
4895 getShadowOriginPtrUserspace(Operand, IRB, IRB.
getInt8Ty(),
Align(1));
4906 int NumRetOutputs = 0;
4908 Type *
RetTy = cast<Value>(CB)->getType();
4909 if (!
RetTy->isVoidTy()) {
4911 auto *
ST = dyn_cast<StructType>(
RetTy);
4913 NumRetOutputs =
ST->getNumElements();
4919 switch (
Info.Type) {
4927 return NumOutputs - NumRetOutputs;
4950 int OutputArgs = getNumOutputArgs(IA, CB);