Skip to content

Commit c09e51a

Browse files
authored
[msan][NFCI] Add arg_size() assertions (#125907)
This prevents the handlers from being called with blatantly inappropriate intrinsics. Currently, if the handlers are called with an intrinsic that doesn't have enough arguments, it may abort; that is bad, but visible. The more insidious risk is that a handler is called with an intrinsic that has more arguments than expected; that will not visibly fail.
1 parent e335ca7 commit c09e51a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,6 +2933,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
29332933
/// Instrument intrinsics that look like a simple SIMD store: writes memory,
29342934
/// has 1 pointer argument and 1 vector argument, returns void.
29352935
bool handleVectorStoreIntrinsic(IntrinsicInst &I) {
2936+
assert(I.arg_size() == 2);
2937+
29362938
IRBuilder<> IRB(&I);
29372939
Value *Addr = I.getArgOperand(0);
29382940
Value *Shadow = getShadow(&I, 1);
@@ -2958,6 +2960,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
29582960
/// Instrument intrinsics that look like a simple SIMD load: reads memory,
29592961
/// has 1 pointer argument, returns a vector.
29602962
bool handleVectorLoadIntrinsic(IntrinsicInst &I) {
2963+
assert(I.arg_size() == 1);
2964+
29612965
IRBuilder<> IRB(&I);
29622966
Value *Addr = I.getArgOperand(0);
29632967

@@ -3497,6 +3501,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
34973501
// The return type does not need to be the same type as the fields
34983502
// e.g., declare i32 @llvm.aarch64.neon.uaddv.i32.v16i8(<16 x i8>)
34993503
void handleVectorReduceIntrinsic(IntrinsicInst &I) {
3504+
assert(I.arg_size() == 1);
3505+
35003506
IRBuilder<> IRB(&I);
35013507
Value *S = IRB.CreateOrReduce(getShadow(&I, 0));
35023508
S = CreateShadowCast(IRB, S, getShadowTy(&I));
@@ -3509,6 +3515,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
35093515
// %a1)
35103516
// shadow = shadow[a0] | shadow[a1.0] | shadow[a1.1]
35113517
void handleVectorReduceWithStarterIntrinsic(IntrinsicInst &I) {
3518+
assert(I.arg_size() == 2);
3519+
35123520
IRBuilder<> IRB(&I);
35133521
Value *Shadow0 = getShadow(&I, 0);
35143522
Value *Shadow1 = IRB.CreateOrReduce(getShadow(&I, 1));
@@ -3521,6 +3529,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
35213529
// Valid (non-poisoned) set bits in the operand pull low the
35223530
// corresponding shadow bits.
35233531
void handleVectorReduceOrIntrinsic(IntrinsicInst &I) {
3532+
assert(I.arg_size() == 1);
3533+
35243534
IRBuilder<> IRB(&I);
35253535
Value *OperandShadow = getShadow(&I, 0);
35263536
Value *OperandUnsetBits = IRB.CreateNot(I.getOperand(0));
@@ -3539,6 +3549,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
35393549
// Valid (non-poisoned) unset bits in the operand pull down the
35403550
// corresponding shadow bits.
35413551
void handleVectorReduceAndIntrinsic(IntrinsicInst &I) {
3552+
assert(I.arg_size() == 1);
3553+
35423554
IRBuilder<> IRB(&I);
35433555
Value *OperandShadow = getShadow(&I, 0);
35443556
Value *OperandSetOrPoison = IRB.CreateOr(I.getOperand(0), OperandShadow);
@@ -3801,6 +3813,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
38013813
// (and we) do not reduce AVX/AVX2 masked intrinsics into LLVM masked
38023814
// intrinsics.
38033815
void handleAVXMaskedStore(IntrinsicInst &I) {
3816+
assert(I.arg_size() == 3);
3817+
38043818
IRBuilder<> IRB(&I);
38053819

38063820
Value *Dst = I.getArgOperand(0);
@@ -3865,6 +3879,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
38653879
// because we need to apply getShadowOriginPtr, not getShadow, to the first
38663880
// parameter.
38673881
void handleAVXMaskedLoad(IntrinsicInst &I) {
3882+
assert(I.arg_size() == 2);
3883+
38683884
IRBuilder<> IRB(&I);
38693885

38703886
Value *Src = I.getArgOperand(0);
@@ -4298,7 +4314,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
42984314
}
42994315

43004316
// Approximation only
4317+
//
4318+
// e.g., <16 x i8> @llvm.aarch64.neon.pmull64(i64, i64)
43014319
void handleNEONVectorMultiplyIntrinsic(IntrinsicInst &I) {
4320+
assert(I.arg_size() == 2);
4321+
43024322
handleShadowOr(I);
43034323
}
43044324

0 commit comments

Comments
 (0)