Skip to content

Commit 1a9d1b5

Browse files
committed
[msan] Re-fix disjoint OR instrumentation from llvm#145990
When disjoint OR was specified and a bit position contained a 1 in both operands, llvm#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 a76dfde commit 1a9d1b5

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
1+
; NOTE: Assertions have mostly been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt < %s -S -passes=msan -msan-precise-disjoint-or=false 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK-IMPRECISE
33
; RUN: opt < %s -S -passes=msan -msan-precise-disjoint-or=true 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK-PRECISE
44
;
@@ -40,15 +40,17 @@ define i8 @test_disjoint_or(i8 %a, i8 %b) sanitize_memory {
4040
; CHECK-NEXT: [[TMP6:%.*]] = and i8 [[TMP3]], [[TMP2]]
4141
; CHECK-NEXT: [[TMP7:%.*]] = and i8 [[TMP1]], [[TMP4]]
4242
; CHECK-NEXT: [[TMP8:%.*]] = or i8 [[TMP5]], [[TMP6]]
43-
; CHECK-NEXT: [[TMP11:%.*]] = or i8 [[TMP8]], [[TMP7]]
43+
; CHECK-NEXT: [[TMP9:%.*]] = or i8 [[TMP8]], [[TMP7]]
4444
;
4545
; CHECK-IMPRECISE: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
46-
; CHECK-IMPRECISE-NEXT: store i8 [[TMP11]], ptr @__msan_retval_tls, align 8
46+
; CHECK-IMPRECISE-NEXT: store i8 [[TMP9]], ptr @__msan_retval_tls, align 8
4747
;
48-
; CHECK-PRECISE: [[TMP10:%.*]] = and i8 [[A]], [[B]]
49-
; CHECK-PRECISE-NEXT: [[TMP12:%.*]] = or i8 [[TMP11]], [[TMP10]]
48+
; CHECK-PRECISE-NEXT: [[TMP10:%.*]] = and i8 [[A]], [[B]]
49+
; CHECK-PRECISE-NEXT: [[TMP11:%.*]] = icmp ne i8 [[TMP10]], 0
50+
; CHECK-PRECISE-NEXT: [[TMP12:%.*]] = sext i1 [[TMP11]] to i8
51+
; CHECK-PRECISE-NEXT: [[_MS_DISJOINT:%.*]] = or i8 [[TMP9]], [[TMP12]]
5052
; CHECK-PRECISE-NEXT: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
51-
; CHECK-PRECISE-NEXT: store i8 [[TMP12]], ptr @__msan_retval_tls, align 8
53+
; CHECK-PRECISE-NEXT: store i8 [[_MS_DISJOINT]], ptr @__msan_retval_tls, align 8
5254
;
5355
; CHECK-NEXT: ret i8 [[C]]
5456
;

0 commit comments

Comments
 (0)