Skip to content

Commit fa42955

Browse files
committed
[msan] Generalize handleIntrinsicByApplyingToShadow to allow alternative intrinsic for shadows
#124159 uses handleIntrinsicByApplyingToShadow for horizontal add/sub, but Vitaly recommends always using the add version to avoid false negatives for fully uninitialized data (#124662). This patch lays the groundwork by generalizing handleIntrinsicByApplyingToShadow to allow using a different intrinsic (of the same type as the original intrinsic) for the shadow. Planned work will apply it to horizontal sub.
1 parent ef232a7 commit fa42955

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4049,7 +4049,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
40494049
// consider this an acceptable tradeoff for performance.
40504050
// To make shadow propagation precise, we want the equivalent of
40514051
// "horizontal OR", but this is not available.
4052-
return handleIntrinsicByApplyingToShadow(I, /* trailingVerbatimArgs */ 0);
4052+
return handleIntrinsicByApplyingToShadow(
4053+
I, /*trailingVerbatimArgs*/ 0, /*shadowIntrinsicID=*/std::nullopt);
40534054
}
40544055

40554056
/// Handle Arm NEON vector store intrinsics (vst{2,3,4}, vst1x_{2,3,4},
@@ -4156,6 +4157,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
41564157
/// shadow[out] =
41574158
/// intrinsic(shadow[var1], shadow[var2], opType) | shadow[opType]
41584159
///
4160+
/// Optionally, the intrinsic for the shadow can be replaced with another
4161+
/// intrinsic of the same type.
4162+
///
41594163
/// CAUTION: this assumes that the intrinsic will handle arbitrary
41604164
/// bit-patterns (for example, if the intrinsic accepts floats for
41614165
/// var1, we require that it doesn't care if inputs are NaNs).
@@ -4164,8 +4168,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
41644168
/// (tbl{1,2,3,4}).
41654169
///
41664170
/// The origin is approximated using setOriginForNaryOp.
4167-
void handleIntrinsicByApplyingToShadow(IntrinsicInst &I,
4168-
unsigned int trailingVerbatimArgs) {
4171+
void handleIntrinsicByApplyingToShadow(
4172+
IntrinsicInst &I, unsigned int trailingVerbatimArgs,
4173+
std::optional<Intrinsic::ID> shadowIntrinsicID) {
41694174
IRBuilder<> IRB(&I);
41704175

41714176
assert(trailingVerbatimArgs < I.arg_size());
@@ -4187,8 +4192,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
41874192
ShadowArgs.push_back(Arg);
41884193
}
41894194

4190-
CallInst *CI =
4191-
IRB.CreateIntrinsic(I.getType(), I.getIntrinsicID(), ShadowArgs);
4195+
CallInst *CI = IRB.CreateIntrinsic(
4196+
I.getType(), shadowIntrinsicID.value_or(I.getIntrinsicID()),
4197+
ShadowArgs);
41924198
Value *CombinedShadow = CI;
41934199

41944200
// Combine the computed shadow with the shadow of trailing args
@@ -4664,7 +4670,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
46644670
case Intrinsic::aarch64_neon_tbx3:
46654671
case Intrinsic::aarch64_neon_tbx4: {
46664672
// The last trailing argument (index register) should be handled verbatim
4667-
handleIntrinsicByApplyingToShadow(I, 1);
4673+
handleIntrinsicByApplyingToShadow(I, /*trailingVerbatimArgs*/ 1,
4674+
/*shadowIntrinsicID=*/std::nullopt);
46684675
break;
46694676
}
46704677

0 commit comments

Comments
 (0)