Skip to content

Commit

Permalink
[AddressSanitizer] Avoid pointer element type accesses
Browse files Browse the repository at this point in the history
Determine masked load/store type based on the value operand and
result types, rather than pointer element type.
  • Loading branch information
nikic committed Jan 26, 2022
1 parent 8bbfdf8 commit c82cb5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Expand Up @@ -26,6 +26,7 @@ class InterestingMemoryOperand {
public:
Use *PtrUse;
bool IsWrite;
Type *OpType;
uint64_t TypeSize;
MaybeAlign Alignment;
// The mask Value, if we're looking at a masked load/store.
Expand All @@ -34,7 +35,8 @@ class InterestingMemoryOperand {
InterestingMemoryOperand(Instruction *I, unsigned OperandNo, bool IsWrite,
class Type *OpType, MaybeAlign Alignment,
Value *MaybeMask = nullptr)
: IsWrite(IsWrite), Alignment(Alignment), MaybeMask(MaybeMask) {
: IsWrite(IsWrite), OpType(OpType), Alignment(Alignment),
MaybeMask(MaybeMask) {
const DataLayout &DL = I->getModule()->getDataLayout();
TypeSize = DL.getTypeStoreSizeInBits(OpType);
PtrUse = &I->getOperandUse(OperandNo);
Expand Down
15 changes: 7 additions & 8 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Expand Up @@ -1547,10 +1547,9 @@ void AddressSanitizer::getInterestingMemoryOperands(
Interesting.emplace_back(I, XCHG->getPointerOperandIndex(), true,
XCHG->getCompareOperand()->getType(), None);
} else if (auto CI = dyn_cast<CallInst>(I)) {
auto *F = CI->getCalledFunction();
if (F && (F->getName().startswith("llvm.masked.load.") ||
F->getName().startswith("llvm.masked.store."))) {
bool IsWrite = F->getName().startswith("llvm.masked.store.");
if (CI->getIntrinsicID() == Intrinsic::masked_load ||
CI->getIntrinsicID() == Intrinsic::masked_store) {
bool IsWrite = CI->getIntrinsicID() == Intrinsic::masked_store;
// Masked store has an initial operand for the value.
unsigned OpOffset = IsWrite ? 1 : 0;
if (IsWrite ? !ClInstrumentWrites : !ClInstrumentReads)
Expand All @@ -1559,7 +1558,7 @@ void AddressSanitizer::getInterestingMemoryOperands(
auto BasePtr = CI->getOperand(OpOffset);
if (ignoreAccess(LI, BasePtr))
return;
auto Ty = BasePtr->getType()->getPointerElementType();
Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
MaybeAlign Alignment = Align(1);
// Otherwise no alignment guarantees. We probably got Undef.
if (auto *Op = dyn_cast<ConstantInt>(CI->getOperand(1 + OpOffset)))
Expand Down Expand Up @@ -1653,10 +1652,10 @@ static void instrumentMaskedLoadOrStore(AddressSanitizer *Pass,
const DataLayout &DL, Type *IntptrTy,
Value *Mask, Instruction *I,
Value *Addr, MaybeAlign Alignment,
unsigned Granularity, uint32_t TypeSize,
unsigned Granularity, Type *OpType,
bool IsWrite, Value *SizeArgument,
bool UseCalls, uint32_t Exp) {
auto *VTy = cast<FixedVectorType>(Addr->getType()->getPointerElementType());
auto *VTy = cast<FixedVectorType>(OpType);
uint64_t ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType());
unsigned Num = VTy->getNumElements();
auto Zero = ConstantInt::get(IntptrTy, 0);
Expand Down Expand Up @@ -1734,7 +1733,7 @@ void AddressSanitizer::instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis,
unsigned Granularity = 1 << Mapping.Scale;
if (O.MaybeMask) {
instrumentMaskedLoadOrStore(this, DL, IntptrTy, O.MaybeMask, O.getInsn(),
Addr, O.Alignment, Granularity, O.TypeSize,
Addr, O.Alignment, Granularity, O.OpType,
O.IsWrite, nullptr, UseCalls, Exp);
} else {
doInstrumentAddress(this, O.getInsn(), O.getInsn(), Addr, O.Alignment,
Expand Down

0 comments on commit c82cb5d

Please sign in to comment.