Skip to content

Commit 86c6403

Browse files
authored
[compiler-rt][nsan] Add check-cmp flag (#108707)
Add check-cmp flag. Closes #108435.
1 parent 6bed79b commit 86c6403

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

compiler-rt/lib/nsan/nsan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// on the runtime configuration. The middle part indicates the type of
2626
// the application value, the suffix (f,d,l) indicates the type of the
2727
// shadow, and depends on the instrumentation configuration.
28-
// * __nsan_fcmp_fail_* emits a warning for an fcmp instruction whose
28+
// * __nsan_fcmp_fail_* emits a warning for a fcmp instruction whose
2929
// corresponding shadow fcmp result differs.
3030
//
3131
//===----------------------------------------------------------------------===//
@@ -682,7 +682,7 @@ void fCmpFailFT(const FT Lhs, const FT Rhs, ShadowFT LhsShadow,
682682
if (flags().enable_warning_stats)
683683
nsan_stats->AddWarning(CheckTypeT::kFcmp, pc, bp, 0.0);
684684

685-
if (flags().disable_warnings)
685+
if (flags().disable_warnings || !flags().check_cmp)
686686
return;
687687

688688
// FIXME: ideally we would print the shadow value as FP128. Right now because

compiler-rt/lib/nsan/nsan_flags.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ NSAN_FLAG(bool, enable_loadtracking_stats, false,
4949
NSAN_FLAG(bool, poison_in_free, true, "")
5050
NSAN_FLAG(bool, print_stats_on_exit, false, "If true, print stats on exit.")
5151
NSAN_FLAG(bool, check_nan, false,
52-
"If true, check the floating-point number is nan")
52+
"If true, check the floating-point number is nan")
53+
NSAN_FLAG(bool, check_cmp, true,
54+
"If true, emit a warning for a fcmp instruction whose "
55+
"corresponding shadow fcmp result differs.")

compiler-rt/test/nsan/fcmp.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clangxx_nsan -O2 -g %s -o %t
2+
// RUN: env NSAN_OPTIONS=check_cmp=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s -check-prefix=CMP_ENABLE
3+
// RUN: env NSAN_OPTIONS=check_cmp=false,halt_on_error=0 %run %t 2>&1 | FileCheck %s -check-prefix=CMP_DISABLE
4+
5+
#include <cmath>
6+
#include <cstdio>
7+
8+
// 0.6/0.2 is slightly below 3, so the comparison will fail after a certain
9+
// threshold that depends on the precision of the computation.
10+
__attribute__((noinline)) // To check call stack reporting.
11+
bool DoCmp(double a, double b, double c, double threshold) {
12+
return c - a / b < threshold;
13+
// CMP_ENABLE: WARNING: NumericalStabilitySanitizer: floating-point comparison results depend on precision
14+
// CMP_ENABLE: double {{ *}}precision dec (native): {{.*}}<{{.*}}
15+
// CMP_ENABLE: __float128{{ *}}precision dec (shadow): {{.*}}<{{.*}}
16+
// CMP_ENABLE: {{#0 .*in DoCmp}}
17+
}
18+
19+
int main() {
20+
double threshold = 1.0;
21+
for (int i = 0; i < 60; ++i) {
22+
threshold /= 2;
23+
// CMP_DISABLE: value at threshold {{.*}}
24+
printf("value at threshold %.20f: %i\n", threshold,
25+
DoCmp(0.6, 0.2, 3.0, threshold));
26+
}
27+
return 0;
28+
}

0 commit comments

Comments
 (0)