Skip to content

Commit 7a7aeb0

Browse files
committed
[msan] Fix "Add optional flag to improve instrumentation of disjoint OR (#145990)"
The "V1" and "V2" values were already NOT'ed, hence the calculation of disjoint OR was incorrect. This patch fixes the issue by using the instruction operands directly.
1 parent 6550f28 commit 7a7aeb0

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2512,6 +2512,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
25122512
// S = S | (V1 & V2)
25132513
Value *S1 = getShadow(&I, 0);
25142514
Value *S2 = getShadow(&I, 1);
2515+
// Gotcha: V1 and V2 are NOT'ed here
25152516
Value *V1 = IRB.CreateNot(I.getOperand(0));
25162517
Value *V2 = IRB.CreateNot(I.getOperand(1));
25172518
if (V1->getType() != S1->getType()) {
@@ -2524,7 +2525,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
25242525

25252526
Value *S = IRB.CreateOr({S1S2, V1S2, S1V2});
25262527
if (ClPreciseDisjointOr && cast<PossiblyDisjointInst>(&I)->isDisjoint()) {
2527-
Value *V1V2 = IRB.CreateAnd(V1, V2);
2528+
// "V1" and "V2" were NOT'ed above, but we still want to reuse them
2529+
// because they were IntCast'ed to the same type as the shadows.
2530+
//
2531+
// (V1 & V2) == ~(~V1 | ~V2) (de Morgan)
2532+
Value *V1V2 = IRB.CreateNot(IRB.CreateOr(V1, V2));
25282533
S = IRB.CreateOr({S, V1V2});
25292534
}
25302535

llvm/test/Instrumentation/MemorySanitizer/or.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ define i8 @test_disjoint_or(i8 %a, i8 %b) sanitize_memory {
4545
; CHECK-IMPRECISE: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
4646
; CHECK-IMPRECISE-NEXT: store i8 [[TMP11]], ptr @__msan_retval_tls, align 8
4747
;
48-
; CHECK-PRECISE: [[TMP10:%.*]] = and i8 [[TMP3]], [[TMP4]]
49-
; CHECK-PRECISE-NEXT: [[TMP12:%.*]] = or i8 [[TMP11]], [[TMP10]]
48+
; CHECK-PRECISE: [[TMP10:%.*]] = or i8 [[TMP3]], [[TMP4]]
49+
; CHECK-PRECISE-NEXT: [[TMP11:%.*]] = xor i8 [[TMP10]], -1
50+
; CHECK-PRECISE-NEXT: [[TMP12:%.*]] = or i8 [[TMP9]], [[TMP11]]
5051
; CHECK-PRECISE-NEXT: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
5152
; CHECK-PRECISE-NEXT: store i8 [[TMP12]], ptr @__msan_retval_tls, align 8
5253
;

0 commit comments

Comments
 (0)