Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions llvm/include/llvm/Analysis/InterestingMemoryOperand.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ class InterestingMemoryOperand {
Value *MaybeEVL;
// The Stride Value, if we're looking at a strided load/store.
Value *MaybeStride;
// The Offset Value, if we're looking at a indexed load/store. The
// offset actually means byte-offset instead of array index.
Value *MaybeByteOffset;

InterestingMemoryOperand(Instruction *I, unsigned OperandNo, bool IsWrite,
class Type *OpType, MaybeAlign Alignment,
Value *MaybeMask = nullptr,
Value *MaybeEVL = nullptr,
Value *MaybeStride = nullptr)
Value *MaybeStride = nullptr,
Value *MaybeByteOffset = nullptr)
: IsWrite(IsWrite), OpType(OpType), Alignment(Alignment),
MaybeMask(MaybeMask), MaybeEVL(MaybeEVL), MaybeStride(MaybeStride) {
MaybeMask(MaybeMask), MaybeEVL(MaybeEVL), MaybeStride(MaybeStride),
MaybeByteOffset(MaybeByteOffset) {
const DataLayout &DL = I->getDataLayout();
TypeStoreSize = DL.getTypeStoreSizeInBits(OpType);
PtrUse = &I->getOperandUse(OperandNo);
Expand Down
38 changes: 38 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2785,6 +2785,44 @@ bool RISCVTTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
Alignment, Mask, EVL, Stride);
return true;
}
case Intrinsic::riscv_vloxei_mask:
case Intrinsic::riscv_vluxei_mask:
case Intrinsic::riscv_vsoxei_mask:
case Intrinsic::riscv_vsuxei_mask:
HasMask = true;
[[fallthrough]];
case Intrinsic::riscv_vloxei:
case Intrinsic::riscv_vluxei:
case Intrinsic::riscv_vsoxei:
case Intrinsic::riscv_vsuxei: {
// Intrinsic interface (only listed ordered version):
// riscv_vloxei(merge, ptr, index, vl)
// riscv_vloxei_mask(merge, ptr, index, mask, vl, policy)
// riscv_vsoxei(val, ptr, index, vl)
// riscv_vsoxei_mask(val, ptr, index, mask, vl, policy)
bool IsWrite = Inst->getType()->isVoidTy();
Type *Ty = IsWrite ? Inst->getArgOperand(0)->getType() : Inst->getType();
const auto *RVVIInfo = RISCVVIntrinsicsTable::getRISCVVIntrinsicInfo(IID);
unsigned VLIndex = RVVIInfo->VLOperand;
unsigned PtrOperandNo = VLIndex - 2 - HasMask;
Value *Mask;
if (HasMask) {
Mask = Inst->getArgOperand(VLIndex - 1);
} else {
// Mask cannot be nullptr here: vector GEP produces <vscale x N x ptr>,
// and casting that to scalar i64 triggers a vector/scalar mismatch
// assertion in CreatePointerCast. Use an all-true mask so ASan lowers it
// via extractelement instead.
Type *MaskType = Ty->getWithNewType(Type::getInt1Ty(C));
Mask = ConstantInt::getTrue(MaskType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we use Mask = nullptr here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we set Mask = nullptr, AddressSanitizer would directly call doInstrumentAddress. However, in our case the address is computed via a getelementptr

%5 = getelementptr i8, ptr %0, <vscale x 1 x i64> %4  

which uses a vector index and therefore produces a VectorType of pointers. When this value is passed to CreatePointerCast, the assertion in llvm/lib/IR/Instructions.cpp is triggered because the source is a VectorType while the target IntptrTy is a scalar i64. In other words, the vector/scalar type mismatch is what causes the crash, so we cannot use nullptr here.

If we set the mask to <vscale x 1 x i1> splat (i1 true), AddressSanitizer will take the instrumentMaskedLoadOrStore path. In this case it generates

%12 = extractelement <vscale x 1 x ptr> %5, i64 %iv

to extract the element, which avoids the assertion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this into a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Thanks for the suggestion.

}
Value *EVL = Inst->getArgOperand(VLIndex);
Value *OffsetOp = Inst->getArgOperand(PtrOperandNo + 1);
Info.InterestingOperands.emplace_back(Inst, PtrOperandNo, IsWrite, Ty,
Align(1), Mask, EVL,
/* Stride */ nullptr, OffsetOp);
return true;
}
}
return false;
}
Expand Down
19 changes: 19 additions & 0 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,25 @@ void AddressSanitizer::instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis,
else
NumInstrumentedReads++;

if (O.MaybeByteOffset) {
Type *Ty = Type::getInt8Ty(*C);
IRBuilder IB(O.getInsn());

Value *OffsetOp = O.MaybeByteOffset;
if (TargetTriple.isRISCV()) {
Type *OffsetTy = OffsetOp->getType();
// RVV indexed loads/stores zero-extend offset operands which are narrower
// than XLEN to XLEN.
if (OffsetTy->getScalarType()->getIntegerBitWidth() <
static_cast<unsigned>(LongSize)) {
VectorType *OrigType = cast<VectorType>(OffsetTy);
Type *ExtendTy = VectorType::get(IntptrTy, OrigType);
OffsetOp = IB.CreateZExt(OffsetOp, ExtendTy);
}
}
Addr = IB.CreateGEP(Ty, Addr, {OffsetOp});
}

unsigned Granularity = 1 << Mapping.Scale;
if (O.MaybeMask) {
instrumentMaskedLoadOrStore(this, DL, IntptrTy, O.MaybeMask, O.MaybeEVL,
Expand Down
Loading