Skip to content

Commit d5094b4

Browse files
committed
Control behavior using -msan-precise-disjoint-or and default to legacy
behavior
1 parent 3f8fc04 commit d5094b4

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ static cl::opt<bool> ClPoisonUndefVectors(
282282
"unaffected by this flag (see -msan-poison-undef)."),
283283
cl::Hidden, cl::init(false));
284284

285+
static cl::opt<bool> ClPreciseDisjointOr(
286+
"msan-precise-disjoint-or",
287+
cl::desc("Precisely poison disjoint OR. If false (legacy behavior), "
288+
"disjointedness is ignored (i.e., 1|1 is initialized)."),
289+
cl::Hidden, cl::init(false));
290+
285291
static cl::opt<bool>
286292
ClHandleICmp("msan-handle-icmp",
287293
cl::desc("propagate shadow through ICmpEQ and ICmpNE"),
@@ -2520,10 +2526,12 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
25202526
Value *S1V2 = IRB.CreateAnd(S1, V2);
25212527

25222528
Value *S = IRB.CreateOr({S1S2, V1S2, S1V2});
2523-
auto *MaybeDisjoint = cast<PossiblyDisjointInst>(&I);
2524-
if (MaybeDisjoint->isDisjoint()) {
2525-
Value *V1V2 = IRB.CreateAnd(V1, V2);
2526-
S = IRB.CreateOr({S, V1V2});
2529+
if (ClPreciseDisjointOr) {
2530+
auto *MaybeDisjoint = cast<PossiblyDisjointInst>(&I);
2531+
if (MaybeDisjoint->isDisjoint()) {
2532+
Value *V1V2 = IRB.CreateAnd(V1, V2);
2533+
S = IRB.CreateOr({S, V1V2});
2534+
}
25272535
}
25282536

25292537
setShadow(&I, S);

llvm/test/Instrumentation/MemorySanitizer/or.ll

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt < %s -S -passes=msan 2>&1 | FileCheck %s
2+
; RUN: opt < %s -S -passes=msan -msan-precise-disjoint-or=false 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK-IMPRECISE
3+
; RUN: opt < %s -S -passes=msan -msan-precise-disjoint-or=true 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK-PRECISE
34
;
45
; Test bitwise OR instructions, including "disjoint OR".
56

@@ -40,10 +41,14 @@ define i8 @test_disjoint_or(i8 %a, i8 %b) sanitize_memory {
4041
; CHECK-NEXT: [[TMP7:%.*]] = and i8 [[TMP1]], [[TMP4]]
4142
; CHECK-NEXT: [[TMP8:%.*]] = or i8 [[TMP5]], [[TMP6]]
4243
; CHECK-NEXT: [[TMP11:%.*]] = or i8 [[TMP8]], [[TMP7]]
43-
; CHECK-NEXT: [[TMP10:%.*]] = and i8 [[TMP3]], [[TMP4]]
44-
; CHECK-NEXT: [[TMP12:%.*]] = or i8 [[TMP11]], [[TMP10]]
45-
; CHECK-NEXT: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
46-
; CHECK-NEXT: store i8 [[TMP12]], ptr @__msan_retval_tls, align 8
44+
;
45+
; CHECK-IMPRECISE: store i8 [[TMP11]], ptr @__msan_retval_tls, align 8
46+
;
47+
; CHECK-PRECISE: [[TMP10:%.*]] = and i8 [[TMP3]], [[TMP4]]
48+
; CHECK-PRECISE-NEXT: [[TMP12:%.*]] = or i8 [[TMP11]], [[TMP10]]
49+
; CHECK-PRECISE-NEXT: [[C:%.*]] = or disjoint i8 [[A]], [[B]]
50+
; CHECK-PRECISE-NEXT: store i8 [[TMP12]], ptr @__msan_retval_tls, align 8
51+
;
4752
; CHECK-NEXT: ret i8 [[C]]
4853
;
4954
%c = or disjoint i8 %a, %b

0 commit comments

Comments
 (0)