Skip to content

Commit f234f5c

Browse files
eugeniszmodem
authored andcommitted
[msan] Instrument x86.pclmulqdq* intrinsics.
Summary: These instructions ignore parts of the input vectors which makes the default MSan handling too strict and causes false positive reports. Reviewers: vitalybuka, RKSimon, thakis Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D73374 (cherry picked from commit 1df8549)
1 parent 23d9392 commit f234f5c

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,43 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
30053005
setOriginForNaryOp(I);
30063006
}
30073007

3008+
Constant *getPclmulMask(IRBuilder<> &IRB, unsigned Width, bool OddElements) {
3009+
SmallVector<Constant *, 8> Mask;
3010+
for (unsigned X = OddElements ? 1 : 0; X < Width; X += 2) {
3011+
Constant *C = ConstantInt::get(IRB.getInt32Ty(), X);
3012+
Mask.push_back(C);
3013+
Mask.push_back(C);
3014+
}
3015+
return ConstantVector::get(Mask);
3016+
}
3017+
3018+
// Instrument pclmul intrinsics.
3019+
// These intrinsics operate either on odd or on even elements of the input
3020+
// vectors, depending on the constant in the 3rd argument, ignoring the rest.
3021+
// Replace the unused elements with copies of the used ones, ex:
3022+
// (0, 1, 2, 3) -> (0, 0, 2, 2) (even case)
3023+
// or
3024+
// (0, 1, 2, 3) -> (1, 1, 3, 3) (odd case)
3025+
// and then apply the usual shadow combining logic.
3026+
void handlePclmulIntrinsic(IntrinsicInst &I) {
3027+
IRBuilder<> IRB(&I);
3028+
Type *ShadowTy = getShadowTy(&I);
3029+
unsigned Width = I.getArgOperand(0)->getType()->getVectorNumElements();
3030+
assert(isa<ConstantInt>(I.getArgOperand(2)) &&
3031+
"pclmul 3rd operand must be a constant");
3032+
unsigned Imm = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
3033+
Value *Shuf0 =
3034+
IRB.CreateShuffleVector(getShadow(&I, 0), UndefValue::get(ShadowTy),
3035+
getPclmulMask(IRB, Width, Imm & 0x01));
3036+
Value *Shuf1 =
3037+
IRB.CreateShuffleVector(getShadow(&I, 1), UndefValue::get(ShadowTy),
3038+
getPclmulMask(IRB, Width, Imm & 0x10));
3039+
ShadowAndOriginCombiner SOC(this, IRB);
3040+
SOC.Add(Shuf0, getOrigin(&I, 0));
3041+
SOC.Add(Shuf1, getOrigin(&I, 1));
3042+
SOC.Done(&I);
3043+
}
3044+
30083045
void visitIntrinsicInst(IntrinsicInst &I) {
30093046
switch (I.getIntrinsicID()) {
30103047
case Intrinsic::lifetime_start:
@@ -3238,6 +3275,12 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
32383275
handleBmiIntrinsic(I);
32393276
break;
32403277

3278+
case Intrinsic::x86_pclmulqdq:
3279+
case Intrinsic::x86_pclmulqdq_256:
3280+
case Intrinsic::x86_pclmulqdq_512:
3281+
handlePclmulIntrinsic(I);
3282+
break;
3283+
32413284
case Intrinsic::is_constant:
32423285
// The result of llvm.is.constant() is always defined.
32433286
setShadow(&I, getCleanShadow(&I));
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
; RUN: opt < %s -msan-check-access-address=0 -S -passes=msan 2>&1 | FileCheck \
2+
; RUN: %s
3+
; RUN: opt < %s -msan -msan-check-access-address=0 -S | FileCheck %s
4+
; RUN: opt < %s -msan -msan-check-access-address=0 -msan-track-origins=1 -S | FileCheck %s --check-prefixes=CHECK,ORIGIN
5+
; REQUIRES: x86-registered-target
6+
7+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
declare <2 x i64> @llvm.x86.pclmulqdq(<2 x i64>, <2 x i64>, i8 immarg) nounwind readnone
11+
declare <4 x i64> @llvm.x86.pclmulqdq.256(<4 x i64>, <4 x i64>, i8 immarg) nounwind readnone
12+
declare <8 x i64> @llvm.x86.pclmulqdq.512(<8 x i64>, <8 x i64>, i8 immarg) nounwind readnone
13+
14+
define <2 x i64> @clmul00(<2 x i64> %a, <2 x i64> %b) sanitize_memory {
15+
entry:
16+
%0 = tail call <2 x i64> @llvm.x86.pclmulqdq(<2 x i64> %a, <2 x i64> %b, i8 0)
17+
ret <2 x i64> %0
18+
}
19+
20+
; CHECK-LABEL: @clmul00
21+
; CHECK: %[[S0:.*]] = load <2 x i64>, <2 x i64>* {{.*}}@__msan_param_tls
22+
; CHECK: %[[S1:.*]] = load <2 x i64>, <2 x i64>* {{.*}}@__msan_param_tls
23+
; CHECK: %[[SHUF1:.*]] = shufflevector <2 x i64> %[[S1]], <2 x i64> undef, <2 x i32> zeroinitializer
24+
; CHECK: %[[SHUF0:.*]] = shufflevector <2 x i64> %[[S0]], <2 x i64> undef, <2 x i32> zeroinitializer
25+
; CHECK: %[[SRET:.*]] = or <2 x i64> %[[SHUF1]], %[[SHUF0]]
26+
; CHECK: store <2 x i64> %[[SRET]], <2 x i64>* {{.*}}@__msan_retval_tls
27+
28+
define <2 x i64> @clmul10(<2 x i64> %a, <2 x i64> %b) sanitize_memory {
29+
entry:
30+
%0 = tail call <2 x i64> @llvm.x86.pclmulqdq(<2 x i64> %a, <2 x i64> %b, i8 16)
31+
ret <2 x i64> %0
32+
}
33+
34+
; CHECK-LABEL: @clmul10
35+
; CHECK: %[[S0:.*]] = load <2 x i64>, <2 x i64>* {{.*}}@__msan_param_tls
36+
; CHECK: %[[S1:.*]] = load <2 x i64>, <2 x i64>* {{.*}}@__msan_param_tls
37+
; CHECK: %[[SHUF1:.*]] = shufflevector <2 x i64> %[[S1]], <2 x i64> undef, <2 x i32> zeroinitializer
38+
; CHECK: %[[SHUF0:.*]] = shufflevector <2 x i64> %[[S0]], <2 x i64> undef, <2 x i32> <i32 1, i32 1>
39+
; CHECK: %[[SRET:.*]] = or <2 x i64> %[[SHUF1]], %[[SHUF0]]
40+
; CHECK: store <2 x i64> %[[SRET]], <2 x i64>* {{.*}}@__msan_retval_tls
41+
42+
define <4 x i64> @clmul11_256(<4 x i64> %a, <4 x i64> %b) sanitize_memory {
43+
entry:
44+
%0 = tail call <4 x i64> @llvm.x86.pclmulqdq.256(<4 x i64> %a, <4 x i64> %b, i8 17)
45+
ret <4 x i64> %0
46+
}
47+
48+
; CHECK-LABEL: @clmul11_256
49+
; CHECK: %[[S0:.*]] = load <4 x i64>, <4 x i64>* {{.*}}@__msan_param_tls
50+
; CHECK: %[[S1:.*]] = load <4 x i64>, <4 x i64>* {{.*}}@__msan_param_tls
51+
; CHECK: %[[SHUF1:.*]] = shufflevector <4 x i64> %[[S1]], <4 x i64> undef, <4 x i32> <i32 1, i32 1, i32 3, i32 3>
52+
; CHECK: %[[SHUF0:.*]] = shufflevector <4 x i64> %[[S0]], <4 x i64> undef, <4 x i32> <i32 1, i32 1, i32 3, i32 3>
53+
; CHECK: %[[SRET:.*]] = or <4 x i64> %[[SHUF1]], %[[SHUF0]]
54+
; CHECK: store <4 x i64> %[[SRET]], <4 x i64>* {{.*}}@__msan_retval_tls
55+
56+
define <8 x i64> @clmul01_512(<8 x i64> %a, <8 x i64> %b) sanitize_memory {
57+
entry:
58+
%0 = tail call <8 x i64> @llvm.x86.pclmulqdq.512(<8 x i64> %a, <8 x i64> %b, i8 16)
59+
ret <8 x i64> %0
60+
}
61+
62+
; CHECK-LABEL: @clmul01_512
63+
; CHECK: %[[S0:.*]] = load <8 x i64>, <8 x i64>* {{.*}}@__msan_param_tls
64+
; CHECK: %[[S1:.*]] = load <8 x i64>, <8 x i64>* {{.*}}@__msan_param_tls
65+
; CHECK: %[[SHUF1:.*]] = shufflevector <8 x i64> %[[S1]], <8 x i64> undef, <8 x i32> <i32 0, i32 0, i32 2, i32 2, i32 4, i32 4, i32 6, i32 6>
66+
; CHECK: %[[SHUF0:.*]] = shufflevector <8 x i64> %[[S0]], <8 x i64> undef, <8 x i32> <i32 1, i32 1, i32 3, i32 3, i32 5, i32 5, i32 7, i32 7>
67+
; CHECK: %[[SRET:.*]] = or <8 x i64> %[[SHUF1]], %[[SHUF0]]
68+
; ORIGIN: %[[FLAT:.*]] = bitcast <8 x i64> %[[SHUF0]] to i512
69+
; ORIGIN: %[[I:.*]] = icmp ne i512 %[[FLAT]], 0
70+
; ORIGIN: %[[O:.*]] = select i1 %[[I]],
71+
; CHECK: store <8 x i64> %[[SRET]], <8 x i64>* {{.*}}@__msan_retval_tls
72+
; ORIGIN: store i32 %[[O]], i32* @__msan_retval_origin_tls

0 commit comments

Comments
 (0)