Skip to content

Commit 9cefa3e

Browse files
authored
[msan] Generalize handleIntrinsicByApplyingToShadow by adding bitcasting (#123474)
`handleIntrinsicByApplyingToShadow` (introduced in #114490) requires that the intrinsic supports integer-ish operands; this is not the case for all intrinsics. This patch generalizes the function to bitcast the shadow arguments to be the same type as the original intrinsic, thus guaranteeing that the intrinsic exists. Additionally, it casts the computed shadow to be an appropriate shadow type. This function assumes that the intrinsic will handle arbitrary bit-patterns (for example, if the intrinsic accepts floats for var1, we assume that it works normally even if inputs are NaNs etc.).
1 parent 15c2d4b commit 9cefa3e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4008,6 +4008,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
40084008
/// shadow[out] =
40094009
/// intrinsic(shadow[var1], shadow[var2], opType) | shadow[opType]
40104010
///
4011+
/// CAUTION: this assumes that the intrinsic will handle arbitrary
4012+
/// bit-patterns (for example, if the intrinsic accepts floats for
4013+
/// var1, we require that it doesn't care if inputs are NaNs).
4014+
///
40114015
/// For example, this can be applied to the Arm NEON vector table intrinsics
40124016
/// (tbl{1,2,3,4}).
40134017
///
@@ -4022,7 +4026,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
40224026
// Don't use getNumOperands() because it includes the callee
40234027
for (unsigned int i = 0; i < I.arg_size() - trailingVerbatimArgs; i++) {
40244028
Value *Shadow = getShadow(&I, i);
4025-
ShadowArgs.push_back(Shadow);
4029+
4030+
// Shadows are integer-ish types but some intrinsics require a
4031+
// different (e.g., floating-point) type.
4032+
ShadowArgs.push_back(
4033+
IRB.CreateBitCast(Shadow, I.getArgOperand(i)->getType()));
40264034
}
40274035

40284036
for (unsigned int i = I.arg_size() - trailingVerbatimArgs; i < I.arg_size();
@@ -4043,7 +4051,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
40434051
CombinedShadow = IRB.CreateOr(Shadow, CombinedShadow, "_msprop");
40444052
}
40454053

4046-
setShadow(&I, CombinedShadow);
4054+
setShadow(&I, IRB.CreateBitCast(CombinedShadow, getShadowTy(&I)));
40474055

40484056
setOriginForNaryOp(I);
40494057
}

0 commit comments

Comments
 (0)