Skip to content

Commit 5c4877e

Browse files
authored
[msan] Re-fix disjoint OR instrumentation from #145990 (#148760)
When disjoint OR was specified and a bit position contained a 1 in both operands, #145990 would set the corresponding shadow bit to uninitialized. However, the output of the operation is (LLVM) 'poison' for the entire result, hence the entire shadow ought to be uninitialized. This patch corrects the issue.
1 parent 868793f commit 5c4877e

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,9 +2509,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
25092509
//
25102510
// S = (S1 & S2) | (~V1 & S2) | (S1 & ~V2)
25112511
//
2512-
// Addendum if the "Or" is "disjoint":
2513-
// 1|1 => p;
2514-
// S = S | (V1 & V2)
2512+
// If the "disjoint OR" property is violated, the result is poison, and
2513+
// hence the entire shadow is uninitialized:
2514+
// S = S | SignExt(V1 & V2 != 0)
25152515
Value *S1 = getShadow(&I, 0);
25162516
Value *S2 = getShadow(&I, 1);
25172517
Value *V1 = I.getOperand(0);
@@ -2532,7 +2532,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
25322532

25332533
if (ClPreciseDisjointOr && cast<PossiblyDisjointInst>(&I)->isDisjoint()) {
25342534
Value *V1V2 = IRB.CreateAnd(V1, V2);
2535-
S = IRB.CreateOr(S, V1V2, "_ms_disjoint");
2535+
Value *DisjointOrShadow = IRB.CreateSExt(
2536+
IRB.CreateICmpNE(V1V2, getCleanShadow(V1V2)), V1V2->getType());
2537+
S = IRB.CreateOr(S, DisjointOrShadow, "_ms_disjoint");
25362538
}
25372539

25382540
setShadow(&I, S);

llvm/test/Instrumentation/MemorySanitizer/or.ll

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,41 @@ define i8 @test_or(i8 %a, i8 %b) sanitize_memory {
2929
}
3030

3131
define i8 @test_disjoint_or(i8 %a, i8 %b) sanitize_memory {
32-
; CHECK-LABEL: define i8 @test_disjoint_or(
33-
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) #[[ATTR0]] {
34-
; CHECK-NEXT: [[TMP1:%.*]] = load i8, ptr @__msan_param_tls, align 8
35-
; CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_param_tls to i64), i64 8) to ptr), align 8
36-
; CHECK-NEXT: call void @llvm.donothing()
37-
; CHECK-NEXT: [[TMP3:%.*]] = xor i8 [[A]], -1
38-
; CHECK-NEXT: [[TMP4:%.*]] = xor i8 [[B]], -1
39-
; CHECK-NEXT: [[TMP5:%.*]] = and i8 [[TMP1]], [[TMP2]]
40-
; CHECK-NEXT: [[TMP6:%.*]] = and i8 [[TMP3]], [[TMP2]]
41-
; CHECK-NEXT: [[TMP7:%.*]] = and i8 [[TMP1]], [[TMP4]]
42-
; CHECK-NEXT: [[TMP8:%.*]] = or i8 [[TMP5]], [[TMP6]]
43-
; CHECK-NEXT: [[TMP11:%.*]] = or i8 [[TMP8]], [[TMP7]]
32+
; CHECK-IMPRECISE-LABEL: define i8 @test_disjoint_or(
33+
; CHECK-IMPRECISE-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) #[[ATTR0]] {
34+
; CHECK-IMPRECISE-NEXT: [[TMP1:%.*]] = load i8, ptr @__msan_param_tls, align 8
35+
; CHECK-IMPRECISE-NEXT: [[TMP2:%.*]] = load i8, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_param_tls to i64), i64 8) to ptr), align 8
36+
; CHECK-IMPRECISE-NEXT: call void @llvm.donothing()
37+
; CHECK-IMPRECISE-NEXT: [[TMP3:%.*]] = xor i8 [[A]], -1
38+
; CHECK-IMPRECISE-NEXT: [[TMP4:%.*]] = xor i8 [[B]], -1
39+
; CHECK-IMPRECISE-NEXT: [[TMP5:%.*]] = and i8 [[TMP1]], [[TMP2]]
40+
; CHECK-IMPRECISE-NEXT: [[TMP6:%.*]] = and i8 [[TMP3]], [[TMP2]]
41+
; CHECK-IMPRECISE-NEXT: [[TMP7:%.*]] = and i8 [[TMP1]], [[TMP4]]
42+
; CHECK-IMPRECISE-NEXT: [[TMP8:%.*]] = or i8 [[TMP5]], [[TMP6]]
43+
; CHECK-IMPRECISE-NEXT: [[TMP9:%.*]] = or i8 [[TMP8]], [[TMP7]]
44+
; CHECK-IMPRECISE-NEXT: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
45+
; CHECK-IMPRECISE-NEXT: store i8 [[TMP9]], ptr @__msan_retval_tls, align 8
46+
; CHECK-IMPRECISE-NEXT: ret i8 [[C]]
4447
;
45-
; CHECK-IMPRECISE: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
46-
; CHECK-IMPRECISE-NEXT: store i8 [[TMP11]], ptr @__msan_retval_tls, align 8
47-
;
48-
; CHECK-PRECISE: [[TMP10:%.*]] = and i8 [[A]], [[B]]
49-
; CHECK-PRECISE-NEXT: [[TMP12:%.*]] = or i8 [[TMP11]], [[TMP10]]
48+
; CHECK-PRECISE-LABEL: define i8 @test_disjoint_or(
49+
; CHECK-PRECISE-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) #[[ATTR0]] {
50+
; CHECK-PRECISE-NEXT: [[TMP1:%.*]] = load i8, ptr @__msan_param_tls, align 8
51+
; CHECK-PRECISE-NEXT: [[TMP2:%.*]] = load i8, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_param_tls to i64), i64 8) to ptr), align 8
52+
; CHECK-PRECISE-NEXT: call void @llvm.donothing()
53+
; CHECK-PRECISE-NEXT: [[TMP3:%.*]] = xor i8 [[A]], -1
54+
; CHECK-PRECISE-NEXT: [[TMP4:%.*]] = xor i8 [[B]], -1
55+
; CHECK-PRECISE-NEXT: [[TMP5:%.*]] = and i8 [[TMP1]], [[TMP2]]
56+
; CHECK-PRECISE-NEXT: [[TMP6:%.*]] = and i8 [[TMP3]], [[TMP2]]
57+
; CHECK-PRECISE-NEXT: [[TMP7:%.*]] = and i8 [[TMP1]], [[TMP4]]
58+
; CHECK-PRECISE-NEXT: [[TMP8:%.*]] = or i8 [[TMP5]], [[TMP6]]
59+
; CHECK-PRECISE-NEXT: [[TMP9:%.*]] = or i8 [[TMP8]], [[TMP7]]
60+
; CHECK-PRECISE-NEXT: [[TMP10:%.*]] = and i8 [[A]], [[B]]
61+
; CHECK-PRECISE-NEXT: [[TMP11:%.*]] = icmp ne i8 [[TMP10]], 0
62+
; CHECK-PRECISE-NEXT: [[TMP12:%.*]] = sext i1 [[TMP11]] to i8
63+
; CHECK-PRECISE-NEXT: [[_MS_DISJOINT:%.*]] = or i8 [[TMP9]], [[TMP12]]
5064
; CHECK-PRECISE-NEXT: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
51-
; CHECK-PRECISE-NEXT: store i8 [[TMP12]], ptr @__msan_retval_tls, align 8
52-
;
53-
; CHECK-NEXT: ret i8 [[C]]
65+
; CHECK-PRECISE-NEXT: store i8 [[_MS_DISJOINT]], ptr @__msan_retval_tls, align 8
66+
; CHECK-PRECISE-NEXT: ret i8 [[C]]
5467
;
5568
%c = or disjoint i8 %a, %b
5669
ret i8 %c

0 commit comments

Comments
 (0)