Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
///
/// This location will be later instrumented with a check that will print a
/// UMR warning in runtime if the shadow value is not 0.
void insertShadowCheck(Value *Shadow, Value *Origin, Instruction *OrigIns) {
void insertCheckShadow(Value *Shadow, Value *Origin, Instruction *OrigIns) {
assert(Shadow);
if (!InsertChecks)
return;
Expand All @@ -2201,11 +2201,12 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
}

/// Remember the place where a shadow check should be inserted.
/// Get shadow for value, and remember the place where a shadow check should
/// be inserted.
///
/// This location will be later instrumented with a check that will print a
/// UMR warning in runtime if the value is not fully defined.
void insertShadowCheck(Value *Val, Instruction *OrigIns) {
void insertCheckShadowOf(Value *Val, Instruction *OrigIns) {
assert(Val);
Value *Shadow, *Origin;
if (ClCheckConstantShadow) {
Expand All @@ -2219,7 +2220,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
return;
Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
}
insertShadowCheck(Shadow, Origin, OrigIns);
insertCheckShadow(Shadow, Origin, OrigIns);
}

AtomicOrdering addReleaseOrdering(AtomicOrdering a) {
Expand Down Expand Up @@ -2331,7 +2332,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
}

if (ClCheckAccessAddress)
insertShadowCheck(I.getPointerOperand(), &I);
insertCheckShadowOf(I.getPointerOperand(), &I);

if (I.isAtomic())
I.setOrdering(addAcquireOrdering(I.getOrdering()));
Expand All @@ -2354,7 +2355,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
void visitStoreInst(StoreInst &I) {
StoreList.push_back(&I);
if (ClCheckAccessAddress)
insertShadowCheck(I.getPointerOperand(), &I);
insertCheckShadowOf(I.getPointerOperand(), &I);
}

void handleCASOrRMW(Instruction &I) {
Expand All @@ -2368,13 +2369,13 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
.first;

if (ClCheckAccessAddress)
insertShadowCheck(Addr, &I);
insertCheckShadowOf(Addr, &I);

// Only test the conditional argument of cmpxchg instruction.
// The other argument can potentially be uninitialized, but we can not
// detect this situation reliably without possible false positives.
if (isa<AtomicCmpXchgInst>(I))
insertShadowCheck(Val, &I);
insertCheckShadowOf(Val, &I);

IRB.CreateStore(getCleanShadow(Val), ShadowPtr);

Expand All @@ -2394,15 +2395,15 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {

// Vector manipulation.
void visitExtractElementInst(ExtractElementInst &I) {
insertShadowCheck(I.getOperand(1), &I);
insertCheckShadowOf(I.getOperand(1), &I);
IRBuilder<> IRB(&I);
setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.getOperand(1),
"_msprop"));
setOrigin(&I, getOrigin(&I, 0));
}

void visitInsertElementInst(InsertElementInst &I) {
insertShadowCheck(I.getOperand(2), &I);
insertCheckShadowOf(I.getOperand(2), &I);
IRBuilder<> IRB(&I);
auto *Shadow0 = getShadow(&I, 0);
auto *Shadow1 = getShadow(&I, 1);
Expand Down Expand Up @@ -2884,7 +2885,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
void handleIntegerDiv(Instruction &I) {
IRBuilder<> IRB(&I);
// Strict on the second argument.
insertShadowCheck(I.getOperand(1), &I);
insertCheckShadowOf(I.getOperand(1), &I);
setShadow(&I, getShadow(&I, 0));
setOrigin(&I, getOrigin(&I, 0));
}
Expand Down Expand Up @@ -3162,7 +3163,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
IRB.CreateAlignedStore(Shadow, ShadowPtr, Align(1));

if (ClCheckAccessAddress)
insertShadowCheck(Addr, &I);
insertCheckShadowOf(Addr, &I);

// FIXME: factor out common code from materializeStores
if (MS.TrackOrigins)
Expand Down Expand Up @@ -3195,7 +3196,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
}

if (ClCheckAccessAddress)
insertShadowCheck(Addr, &I);
insertCheckShadowOf(Addr, &I);

if (MS.TrackOrigins) {
if (PropagateShadow)
Expand Down Expand Up @@ -3519,7 +3520,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
AggShadow = ConvertShadow;
}
assert(AggShadow->getType()->isIntegerTy());
insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I);
insertCheckShadow(AggShadow, getOrigin(ConvertOp), &I);

// Build result shadow by zero-filling parts of CopyOp shadow that come from
// ConvertOp.
Expand Down Expand Up @@ -3926,7 +3927,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
IRB.CreateStore(getCleanShadow(Ty), ShadowPtr);

if (ClCheckAccessAddress)
insertShadowCheck(Addr, &I);
insertCheckShadowOf(Addr, &I);
}

void handleLdmxcsr(IntrinsicInst &I) {
Expand All @@ -3942,12 +3943,12 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
getShadowOriginPtr(Addr, IRB, Ty, Alignment, /*isStore*/ false);

if (ClCheckAccessAddress)
insertShadowCheck(Addr, &I);
insertCheckShadowOf(Addr, &I);

Value *Shadow = IRB.CreateAlignedLoad(Ty, ShadowPtr, Alignment, "_ldmxcsr");
Value *Origin = MS.TrackOrigins ? IRB.CreateLoad(MS.OriginTy, OriginPtr)
: getCleanOrigin();
insertShadowCheck(Shadow, Origin, &I);
insertCheckShadow(Shadow, Origin, &I);
}

void handleMaskedExpandLoad(IntrinsicInst &I) {
Expand All @@ -3958,8 +3959,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
Value *PassThru = I.getArgOperand(2);

if (ClCheckAccessAddress) {
insertShadowCheck(Ptr, &I);
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Ptr, &I);
insertCheckShadowOf(Mask, &I);
}

if (!PropagateShadow) {
Expand Down Expand Up @@ -3991,8 +3992,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
Value *Mask = I.getArgOperand(2);

if (ClCheckAccessAddress) {
insertShadowCheck(Ptr, &I);
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Ptr, &I);
insertCheckShadowOf(Mask, &I);
}

Value *Shadow = getShadow(Values);
Expand All @@ -4016,11 +4017,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {

Type *PtrsShadowTy = getShadowTy(Ptrs);
if (ClCheckAccessAddress) {
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Mask, &I);
Value *MaskedPtrShadow = IRB.CreateSelect(
Mask, getShadow(Ptrs), Constant::getNullValue((PtrsShadowTy)),
"_msmaskedptrs");
insertShadowCheck(MaskedPtrShadow, getOrigin(Ptrs), &I);
insertCheckShadow(MaskedPtrShadow, getOrigin(Ptrs), &I);
}

if (!PropagateShadow) {
Expand Down Expand Up @@ -4054,11 +4055,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {

Type *PtrsShadowTy = getShadowTy(Ptrs);
if (ClCheckAccessAddress) {
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Mask, &I);
Value *MaskedPtrShadow = IRB.CreateSelect(
Mask, getShadow(Ptrs), Constant::getNullValue((PtrsShadowTy)),
"_msmaskedptrs");
insertShadowCheck(MaskedPtrShadow, getOrigin(Ptrs), &I);
insertCheckShadow(MaskedPtrShadow, getOrigin(Ptrs), &I);
}

Value *Shadow = getShadow(Values);
Expand Down Expand Up @@ -4086,8 +4087,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
Value *Shadow = getShadow(V);

if (ClCheckAccessAddress) {
insertShadowCheck(Ptr, &I);
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Ptr, &I);
insertCheckShadowOf(Mask, &I);
}

Value *ShadowPtr;
Expand Down Expand Up @@ -4119,8 +4120,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
Value *PassThru = I.getArgOperand(3);

if (ClCheckAccessAddress) {
insertShadowCheck(Ptr, &I);
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Ptr, &I);
insertCheckShadowOf(Mask, &I);
}

if (!PropagateShadow) {
Expand Down Expand Up @@ -4183,8 +4184,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
Value *SrcShadow = getShadow(Src);

if (ClCheckAccessAddress) {
insertShadowCheck(Dst, &I);
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Dst, &I);
insertCheckShadowOf(Mask, &I);
}

Value *DstShadowPtr;
Expand Down Expand Up @@ -4244,7 +4245,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
const Align Alignment = Align(1);

if (ClCheckAccessAddress) {
insertShadowCheck(Mask, &I);
insertCheckShadowOf(Mask, &I);
}

Type *SrcShadowTy = getShadowTy(Src);
Expand Down Expand Up @@ -4289,7 +4290,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
assert(V->getType() == IndexBits->getType());
V = IRB.CreateOr(V, IRB.CreateAnd(V, IndexBits));
}
insertShadowCheck(V, getOrigin(Idx), I);
insertCheckShadow(V, getOrigin(Idx), I);
}

// Instrument AVX permutation intrinsic.
Expand Down Expand Up @@ -4578,8 +4579,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
// Technically, we could probably just check whether the LSB is
// initialized, but intuitively it feels like a partly uninitialized mask
// is unintended, and we should warn the user immediately.
insertShadowCheck(Mask, &I);
insertShadowCheck(RoundingMode, &I);
insertCheckShadowOf(Mask, &I);
insertCheckShadowOf(RoundingMode, &I);

assert(isa<FixedVectorType>(A->getType()));
unsigned NumElements =
Expand Down Expand Up @@ -4660,7 +4661,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
ShadowArgs.push_back(LaneNumber);

// TODO: blend shadow of lane number into output shadow?
insertShadowCheck(LaneNumber, &I);
insertCheckShadowOf(LaneNumber, &I);
}

Value *Src = I.getArgOperand(numArgs - 1);
Expand Down Expand Up @@ -4712,7 +4713,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
int skipTrailingOperands = 1;

if (ClCheckAccessAddress)
insertShadowCheck(Addr, &I);
insertCheckShadowOf(Addr, &I);

// Second-last operand is the lane number (for vst{2,3,4}lane)
if (useLane) {
Expand Down Expand Up @@ -5687,7 +5688,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
if (A->getType()->isScalableTy()) {
LLVM_DEBUG(dbgs() << "Arg " << i << " is vscale: " << CB << "\n");
// Handle as noundef, but don't reserve tls slots.
insertShadowCheck(A, &CB);
insertCheckShadowOf(A, &CB);
continue;
}

Expand All @@ -5699,7 +5700,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
bool EagerCheck = MayCheckCall && !ByVal && NoUndef;

if (EagerCheck) {
insertShadowCheck(A, &CB);
insertCheckShadowOf(A, &CB);
Size = DL.getTypeAllocSize(A->getType());
} else {
[[maybe_unused]] Value *Store = nullptr;
Expand Down Expand Up @@ -5847,7 +5848,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
Value *Shadow = getShadow(RetVal);
bool StoreOrigin = true;
if (EagerCheck) {
insertShadowCheck(RetVal, &I);
insertCheckShadowOf(RetVal, &I);
Shadow = getCleanShadow(RetVal);
StoreOrigin = false;
}
Expand Down Expand Up @@ -6080,7 +6081,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
// Each such pointer is instrumented with a call to the runtime library.
Type *OpType = Operand->getType();
// Check the operand value itself.
insertShadowCheck(Operand, &I);
insertCheckShadowOf(Operand, &I);
if (!OpType->isPointerTy() || !isOutput) {
assert(!isOutput);
return;
Expand Down Expand Up @@ -6190,7 +6191,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
for (size_t i = 0, n = I.getNumOperands(); i < n; i++) {
Value *Operand = I.getOperand(i);
if (Operand->getType()->isSized())
insertShadowCheck(Operand, &I);
insertCheckShadowOf(Operand, &I);
}
setShadow(&I, getCleanShadow(&I));
setOrigin(&I, getCleanOrigin());
Expand Down
Loading