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

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 16 19:21:33 PDT 2024


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/108707

>From debd2826d523bdaf4fdf771c0f23765596e9da0e Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 14 Sep 2024 23:48:08 +0800
Subject: [PATCH 1/2] check cmp flag

---
 compiler-rt/lib/nsan/nsan.cpp       | 4 ++--
 compiler-rt/lib/nsan/nsan_flags.inc | 5 ++++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/nsan/nsan.cpp b/compiler-rt/lib/nsan/nsan.cpp
index 4679bcd589eb45..1c06340549acae 100644
--- a/compiler-rt/lib/nsan/nsan.cpp
+++ b/compiler-rt/lib/nsan/nsan.cpp
@@ -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.
 //
 //===----------------------------------------------------------------------===//
@@ -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
diff --git a/compiler-rt/lib/nsan/nsan_flags.inc b/compiler-rt/lib/nsan/nsan_flags.inc
index 7c9e579d91fc33..760973295a8a50 100644
--- a/compiler-rt/lib/nsan/nsan_flags.inc
+++ b/compiler-rt/lib/nsan/nsan_flags.inc
@@ -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")
\ No newline at end of file
+          "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.")

>From f32b93941ec6f22999a393067bb88e9efa0508ef Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Tue, 17 Sep 2024 02:21:21 +0000
Subject: [PATCH 2/2] add test

---
 compiler-rt/test/nsan/fcmp.cpp | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 compiler-rt/test/nsan/fcmp.cpp

diff --git a/compiler-rt/test/nsan/fcmp.cpp b/compiler-rt/test/nsan/fcmp.cpp
new file mode 100644
index 00000000000000..df9516b9ba2549
--- /dev/null
+++ b/compiler-rt/test/nsan/fcmp.cpp
@@ -0,0 +1,27 @@
+// 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
+// RUN: env NSAN_OPTIONS=check_cmp=false,halt_on_error=0 %run %t 2>&1 | FileCheck %s
+
+#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: WARNING: NumericalStabilitySanitizer: floating-point comparison results depend on precision
+  // CMP: double    {{ *}}precision dec (native): {{.*}}<{{.*}}
+  // CMP: __float128{{ *}}precision dec (shadow): {{.*}}<{{.*}}
+  // CMP: {{#0 .*in DoCmp}}
+}
+
+int main() {
+  double threshold = 1.0;
+  for (int i = 0; i < 60; ++i) {
+    threshold /= 2;
+    printf("value at threshold %.20f: %i\n", threshold,
+           DoCmp(0.6, 0.2, 3.0, threshold));
+  }
+  return 0;
+}
\ No newline at end of file



More information about the llvm-commits mailing list