Skip to content

[compiler-rt] add check-cmp flag for nsan #108707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler-rt/lib/nsan/nsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// on the runtime configuration. The middle part indicates the type of
// the application value, the suffix (f,d,l) indicates the type of the
// shadow, and depends on the instrumentation configuration.
// * __nsan_fcmp_fail_* emits a warning for an fcmp instruction whose
// * __nsan_fcmp_fail_* emits a warning for a fcmp instruction whose
// corresponding shadow fcmp result differs.
//
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -682,7 +682,7 @@ void fCmpFailFT(const FT Lhs, const FT Rhs, ShadowFT LhsShadow,
if (flags().enable_warning_stats)
nsan_stats->AddWarning(CheckTypeT::kFcmp, pc, bp, 0.0);

if (flags().disable_warnings)
if (flags().disable_warnings || !flags().check_cmp)
return;

// FIXME: ideally we would print the shadow value as FP128. Right now because
Expand Down
5 changes: 4 additions & 1 deletion compiler-rt/lib/nsan/nsan_flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ NSAN_FLAG(bool, enable_loadtracking_stats, false,
NSAN_FLAG(bool, poison_in_free, true, "")
NSAN_FLAG(bool, print_stats_on_exit, false, "If true, print stats on exit.")
NSAN_FLAG(bool, check_nan, false,
"If true, check the floating-point number is nan")
"If true, check the floating-point number is nan")
NSAN_FLAG(bool, check_cmp, true,
"If true, emit a warning for a fcmp instruction whose "
"corresponding shadow fcmp result differs.")
28 changes: 28 additions & 0 deletions compiler-rt/test/nsan/fcmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %clangxx_nsan -O2 -g %s -o %t
// RUN: env NSAN_OPTIONS=check_cmp=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s -check-prefix=CMP_ENABLE
// RUN: env NSAN_OPTIONS=check_cmp=false,halt_on_error=0 %run %t 2>&1 | FileCheck %s -check-prefix=CMP_DISABLE

#include <cmath>
#include <cstdio>

// 0.6/0.2 is slightly below 3, so the comparison will fail after a certain
// threshold that depends on the precision of the computation.
__attribute__((noinline)) // To check call stack reporting.
bool DoCmp(double a, double b, double c, double threshold) {
return c - a / b < threshold;
// CMP_ENABLE: WARNING: NumericalStabilitySanitizer: floating-point comparison results depend on precision
// CMP_ENABLE: double {{ *}}precision dec (native): {{.*}}<{{.*}}
// CMP_ENABLE: __float128{{ *}}precision dec (shadow): {{.*}}<{{.*}}
// CMP_ENABLE: {{#0 .*in DoCmp}}
}

int main() {
double threshold = 1.0;
for (int i = 0; i < 60; ++i) {
threshold /= 2;
// CMP_DISABLE: value at threshold {{.*}}
printf("value at threshold %.20f: %i\n", threshold,
DoCmp(0.6, 0.2, 3.0, threshold));
}
return 0;
}
Loading