[llvm-branch-commits] [compiler-rt] [nsan] Fix style issue (PR #96142)

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jun 20 00:29:38 PDT 2024


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/96142

>From 54357a77702361abf1044d1ee5d0dee3e4fa407c Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 20 Jun 2024 00:23:01 -0700
Subject: [PATCH 1/2] more

Created using spr 1.3.5-bogner
---
 compiler-rt/lib/nsan/nsan.cpp              |  59 ++++++------
 compiler-rt/lib/nsan/nsan_flags.cpp        |   2 -
 compiler-rt/lib/nsan/nsan_stats.cpp        | 107 ++++++++++-----------
 compiler-rt/lib/nsan/nsan_stats.h          |  43 +++++----
 compiler-rt/lib/nsan/nsan_suppressions.cpp |  48 +++++----
 5 files changed, 124 insertions(+), 135 deletions(-)

diff --git a/compiler-rt/lib/nsan/nsan.cpp b/compiler-rt/lib/nsan/nsan.cpp
index ece1130f73d14..fd5390e20a029 100644
--- a/compiler-rt/lib/nsan/nsan.cpp
+++ b/compiler-rt/lib/nsan/nsan.cpp
@@ -71,7 +71,6 @@ __nsan_set_value_unknown(const u8 *addr, uptr size) {
   internal_memset((void *)getShadowTypeAddrFor(addr), 0, size);
 }
 
-namespace __nsan {
 
 const char *FTInfo<float>::kCppTypeName = "float";
 const char *FTInfo<double>::kCppTypeName = "double";
@@ -177,8 +176,6 @@ template <typename T> T max(T a, T b) { return a < b ? b : a; }
 
 } // end anonymous namespace
 
-} // end namespace __nsan
-
 void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
                                                  void *context,
                                                  bool request_fast,
@@ -189,7 +186,7 @@ void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
 
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_print_accumulated_stats() {
   if (nsan_stats)
-    nsan_stats->print();
+    nsan_stats->Print();
 }
 
 static void NsanAtexit() {
@@ -228,18 +225,18 @@ __nsan_get_shadow_ptr_for_longdouble_store(u8 *store_addr, uptr n) {
   return getShadowPtrForStore<long double>(store_addr, n);
 }
 
-template <typename FT> static bool isValidShadowType(const u8 *shadow_type) {
+template <typename FT> static bool IsValidShadowType(const u8 *shadow_type) {
   return __builtin_memcmp(shadow_type, FTInfo<FT>::kTypePattern, sizeof(FT)) ==
          0;
 }
 
-template <int kSize, typename T> static bool isZero(const T *ptr) {
+template <int kSize, typename T> static bool IsZero(const T *ptr) {
   constexpr const char kZeros[kSize] = {}; // Zero initialized.
   return __builtin_memcmp(ptr, kZeros, kSize) == 0;
 }
 
-template <typename FT> static bool isUnknownShadowType(const u8 *shadow_type) {
-  return isZero<sizeof(FTInfo<FT>::kTypePattern)>(shadow_type);
+template <typename FT> static bool IsUnknownShadowType(const u8 *shadow_type) {
+  return IsZero<sizeof(FTInfo<FT>::kTypePattern)>(shadow_type);
 }
 
 // The three folowing functions check that the address stores a complete
@@ -249,21 +246,21 @@ template <typename FT>
 static const u8 *getShadowPtrForLoad(const u8 *load_addr, uptr n) {
   const u8 *const shadow_type = getShadowTypeAddrFor(load_addr);
   for (uptr i = 0; i < n; ++i) {
-    if (!isValidShadowType<FT>(shadow_type + i * sizeof(FT))) {
+    if (!IsValidShadowType<FT>(shadow_type + i * sizeof(FT))) {
       // If loadtracking stats are enabled, log loads with invalid types
       // (tampered with through type punning).
       if (flags().enable_loadtracking_stats) {
-        if (isUnknownShadowType<FT>(shadow_type + i * sizeof(FT))) {
+        if (IsUnknownShadowType<FT>(shadow_type + i * sizeof(FT))) {
           // Warn only if the value is non-zero. Zero is special because
           // applications typically initialize large buffers to zero in an
           // untyped way.
-          if (!isZero<sizeof(FT)>(load_addr)) {
+          if (!IsZero<sizeof(FT)>(load_addr)) {
             GET_CALLER_PC_BP;
-            nsan_stats->addUnknownLoadTrackingEvent(pc, bp);
+            nsan_stats->AddUnknownLoadTrackingEvent(pc, bp);
           }
         } else {
           GET_CALLER_PC_BP;
-          nsan_stats->addInvalidLoadTrackingEvent(pc, bp);
+          nsan_stats->AddInvalidLoadTrackingEvent(pc, bp);
         }
       }
       return nullptr;
@@ -442,7 +439,7 @@ int32_t checkFT(const FT value, ShadowFT Shadow, CheckTypeT CheckType,
     // want to avoid having to move the computation of `largest` before the
     // absolute value check when this branch is not taken.
     const InternalFT largest = max(ftAbs(check_value), ftAbs(check_shadow));
-    nsan_stats->addCheck(CheckType, pc, bp, abs_err / largest);
+    nsan_stats->AddCheck(CheckType, pc, bp, abs_err / largest);
   }
 
   // Note: writing the comparison that way ensures that when `abs_err` is Nan
@@ -534,7 +531,7 @@ int32_t checkFT(const FT value, ShadowFT Shadow, CheckTypeT CheckType,
 
   if (flags().enable_warning_stats) {
     GET_CALLER_PC_BP;
-    nsan_stats->addWarning(CheckType, pc, bp, abs_err / largest);
+    nsan_stats->AddWarning(CheckType, pc, bp, abs_err / largest);
   }
 
   if (flags().halt_on_error) {
@@ -565,10 +562,10 @@ __nsan_internal_check_longdouble_q(long double value, __float128 shadow,
   return checkFT(value, shadow, static_cast<CheckTypeT>(check_type), check_arg);
 }
 
-static const char *getTruthValueName(bool v) { return v ? "true" : "false"; }
+static const char *GetTruthValueName(bool v) { return v ? "true" : "false"; }
 
 // This uses the same values as CmpInst::Predicate.
-static const char *getPredicateName(int v) {
+static const char *GetPredicateName(int v) {
   switch (v) {
   case 0:
     return "(false)";
@@ -618,21 +615,19 @@ void fCmpFailFT(const FT Lhs, const FT Rhs, ShadowFT LhsShadow,
   }
 
   GET_CALLER_PC_BP;
-  BufferedStackTrace Stack;
-  Stack.Unwind(pc, bp, nullptr, false);
+  BufferedStackTrace stack;
+  stack.Unwind(pc, bp, nullptr, false);
 
-  if (GetSuppressionForStack(&Stack, CheckKind::Fcmp)) {
+  if (GetSuppressionForStack(&stack, CheckKind::Fcmp)) {
     // FIXME: optionally print.
     return;
   }
 
-  if (flags().enable_warning_stats) {
-    nsan_stats->addWarning(CheckTypeT::kFcmp, pc, bp, 0.0);
-  }
+  if (flags().enable_warning_stats)
+    nsan_stats->AddWarning(CheckTypeT::kFcmp, pc, bp, 0.0);
 
-  if (flags().disable_warnings) {
+  if (flags().disable_warnings)
     return;
-  }
 
   // FIXME: ideally we would print the shadow value as FP128. Right now because
   // we truncate to long double we can sometimes see stuff like:
@@ -640,7 +635,7 @@ void fCmpFailFT(const FT Lhs, const FT Rhs, ShadowFT LhsShadow,
   using ValuePrinter = FTPrinter<FT>;
   using ShadowPrinter = FTPrinter<ShadowFT>;
   Decorator D;
-  const char *const PredicateName = getPredicateName(Predicate);
+  const char *const PredicateName = GetPredicateName(Predicate);
   Printf("%s", D.Warning());
   Printf("WARNING: NumericalStabilitySanitizer: floating-point comparison "
          "results depend on precision\n"
@@ -651,20 +646,20 @@ void fCmpFailFT(const FT Lhs, const FT Rhs, ShadowFT LhsShadow,
          "%s",
          // Native, decimal.
          FTInfo<FT>::kCppTypeName, ValuePrinter::dec(Lhs).Buffer, PredicateName,
-         ValuePrinter::dec(Rhs).Buffer, getTruthValueName(result),
+         ValuePrinter::dec(Rhs).Buffer, GetTruthValueName(result),
          // Shadow, decimal
          FTInfo<ShadowFT>::kCppTypeName, ShadowPrinter::dec(LhsShadow).Buffer,
          PredicateName, ShadowPrinter::dec(RhsShadow).Buffer,
-         getTruthValueName(ShadowResult),
+         GetTruthValueName(ShadowResult),
          // Native, hex.
          FTInfo<FT>::kCppTypeName, ValuePrinter::hex(Lhs).Buffer, PredicateName,
-         ValuePrinter::hex(Rhs).Buffer, getTruthValueName(result),
+         ValuePrinter::hex(Rhs).Buffer, GetTruthValueName(result),
          // Shadow, hex
          FTInfo<ShadowFT>::kCppTypeName, ShadowPrinter::hex(LhsShadow).Buffer,
          PredicateName, ShadowPrinter::hex(RhsShadow).Buffer,
-         getTruthValueName(ShadowResult), D.End());
+         GetTruthValueName(ShadowResult), D.End());
   Printf("%s", D.Default());
-  Stack.Print();
+  stack.Print();
   if (flags().halt_on_error) {
     Printf("Exiting\n");
     Die();
@@ -799,7 +794,7 @@ extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_init() {
 
   InitializeInterceptors();
 
-  initializeStats();
+  InitializeStats();
   if (flags().print_stats_on_exit)
     Atexit(NsanAtexit);
 
diff --git a/compiler-rt/lib/nsan/nsan_flags.cpp b/compiler-rt/lib/nsan/nsan_flags.cpp
index 2c7764653a22d..94e3a187c8e6c 100644
--- a/compiler-rt/lib/nsan/nsan_flags.cpp
+++ b/compiler-rt/lib/nsan/nsan_flags.cpp
@@ -18,11 +18,9 @@
 using namespace __sanitizer;
 using namespace __nsan;
 
-namespace __nsan {
 SANITIZER_INTERFACE_WEAK_DEF(const char *, __nsan_default_options, void) {
   return "";
 }
-} // namespace __nsan
 
 Flags __nsan::flags_data;
 
diff --git a/compiler-rt/lib/nsan/nsan_stats.cpp b/compiler-rt/lib/nsan/nsan_stats.cpp
index f23a72e3ba2a8..d188df1b1f70b 100644
--- a/compiler-rt/lib/nsan/nsan_stats.cpp
+++ b/compiler-rt/lib/nsan/nsan_stats.cpp
@@ -22,72 +22,73 @@
 #include <assert.h>
 #include <stdio.h>
 
-namespace __nsan {
-
 using namespace __sanitizer;
+using namespace __nsan;
 
 Stats::Stats() {
-  CheckAndWarnings.Initialize(0);
+  check_and_warnings.Initialize(0);
   TrackedLoads.Initialize(0);
 }
 
 Stats::~Stats() { Printf("deleting nsan stats\n"); }
 
-static uptr key(CheckTypeT CheckType, u32 StackId) {
+static uptr Key(CheckTypeT CheckType, u32 StackId) {
   return static_cast<uptr>(CheckType) +
          StackId * static_cast<uptr>(CheckTypeT::kMaxCheckType);
 }
 
 template <typename MapT, typename VectorT, typename Fn>
-void UpdateEntry(CheckTypeT CheckTy, uptr PC, uptr BP, MapT *Map,
-                 VectorT *Vector, Mutex *Mutex, Fn F) {
+static void UpdateEntry(CheckTypeT check_ty, uptr pc, uptr bp, MapT *map,
+                        VectorT *vector, Mutex *mutex, Fn F) {
   BufferedStackTrace Stack;
-  Stack.Unwind(PC, BP, nullptr, false);
-  u32 StackId = StackDepotPut(Stack);
-  typename MapT::Handle Handle(Map, key(CheckTy, StackId));
-  Lock L(Mutex);
+  Stack.Unwind(pc, bp, nullptr, false);
+  u32 stack_id = StackDepotPut(Stack);
+  typename MapT::Handle Handle(map, Key(check_ty, stack_id));
+  Lock L(mutex);
   if (Handle.created()) {
-    typename VectorT::value_type Entry;
-    Entry.StackId = StackId;
-    Entry.CheckTy = CheckTy;
-    F(Entry);
-    Vector->push_back(Entry);
+    typename VectorT::value_type entry;
+    entry.stack_id = stack_id;
+    entry.check_ty = check_ty;
+    F(entry);
+    vector->push_back(entry);
   } else {
-    auto &Entry = (*Vector)[*Handle];
-    F(Entry);
+    auto &entry = (*vector)[*Handle];
+    F(entry);
   }
 }
 
-void Stats::addCheck(CheckTypeT CheckTy, uptr PC, uptr BP, double RelErr) {
-  UpdateEntry(CheckTy, PC, BP, &CheckAndWarningsMap, &CheckAndWarnings,
-              &CheckAndWarningsMutex, [RelErr](CheckAndWarningsValue &Entry) {
-                ++Entry.NumChecks;
-                if (RelErr > Entry.MaxRelativeError) {
-                  Entry.MaxRelativeError = RelErr;
+void Stats::AddCheck(CheckTypeT check_ty, uptr pc, uptr bp, double rel_err) {
+  UpdateEntry(check_ty, pc, bp, &CheckAndWarningsMap, &check_and_warnings,
+              &check_and_warning_mutex,
+              [rel_err](CheckAndWarningsValue &entry) {
+                ++entry.num_checks;
+                if (rel_err > entry.max_relative_err) {
+                  entry.max_relative_err = rel_err;
                 }
               });
 }
 
-void Stats::addWarning(CheckTypeT CheckTy, uptr PC, uptr BP, double RelErr) {
-  UpdateEntry(CheckTy, PC, BP, &CheckAndWarningsMap, &CheckAndWarnings,
-              &CheckAndWarningsMutex, [RelErr](CheckAndWarningsValue &Entry) {
-                ++Entry.NumWarnings;
-                if (RelErr > Entry.MaxRelativeError) {
-                  Entry.MaxRelativeError = RelErr;
+void Stats::AddWarning(CheckTypeT check_ty, uptr pc, uptr bp, double rel_err) {
+  UpdateEntry(check_ty, pc, bp, &CheckAndWarningsMap, &check_and_warnings,
+              &check_and_warning_mutex,
+              [rel_err](CheckAndWarningsValue &entry) {
+                ++entry.num_warnings;
+                if (rel_err > entry.max_relative_err) {
+                  entry.max_relative_err = rel_err;
                 }
               });
 }
 
-void Stats::addInvalidLoadTrackingEvent(uptr PC, uptr BP) {
-  UpdateEntry(CheckTypeT::kLoad, PC, BP, &LoadTrackingMap, &TrackedLoads,
+void Stats::AddInvalidLoadTrackingEvent(uptr pc, uptr bp) {
+  UpdateEntry(CheckTypeT::kLoad, pc, bp, &LoadTrackingMap, &TrackedLoads,
               &TrackedLoadsMutex,
-              [](LoadTrackingValue &Entry) { ++Entry.NumInvalid; });
+              [](LoadTrackingValue &entry) { ++entry.num_invalid; });
 }
 
-void Stats::addUnknownLoadTrackingEvent(uptr PC, uptr BP) {
-  UpdateEntry(CheckTypeT::kLoad, PC, BP, &LoadTrackingMap, &TrackedLoads,
+void Stats::AddUnknownLoadTrackingEvent(uptr pc, uptr bp) {
+  UpdateEntry(CheckTypeT::kLoad, pc, bp, &LoadTrackingMap, &TrackedLoads,
               &TrackedLoadsMutex,
-              [](LoadTrackingValue &Entry) { ++Entry.NumUnknown; });
+              [](LoadTrackingValue &entry) { ++entry.num_unknown; });
 }
 
 static const char *CheckTypeDisplay(CheckTypeT CheckType) {
@@ -115,20 +116,20 @@ static const char *CheckTypeDisplay(CheckTypeT CheckType) {
   return "";
 }
 
-void Stats::print() const {
+void Stats::Print() const {
   {
-    Lock L(&CheckAndWarningsMutex);
-    for (const auto &Entry : CheckAndWarnings) {
-      Printf("warned %llu times out of %llu %s checks ", Entry.NumWarnings,
-             Entry.NumChecks, CheckTypeDisplay(Entry.CheckTy));
-      if (Entry.NumWarnings > 0) {
+    Lock L(&check_and_warning_mutex);
+    for (const auto &entry : check_and_warnings) {
+      Printf("warned %llu times out of %llu %s checks ", entry.num_warnings,
+             entry.num_checks, CheckTypeDisplay(entry.check_ty));
+      if (entry.num_warnings > 0) {
         char RelErrBuf[64];
         snprintf(RelErrBuf, sizeof(RelErrBuf) - 1, "%f",
-                 Entry.MaxRelativeError * 100.0);
+                 entry.max_relative_err * 100.0);
         Printf("(max relative error: %s%%) ", RelErrBuf);
       }
       Printf("at:\n");
-      StackDepotGet(Entry.StackId).Print();
+      StackDepotGet(entry.stack_id).Print();
     }
   }
 
@@ -136,12 +137,12 @@ void Stats::print() const {
     Lock L(&TrackedLoadsMutex);
     u64 TotalInvalidLoadTracking = 0;
     u64 TotalUnknownLoadTracking = 0;
-    for (const auto &Entry : TrackedLoads) {
-      TotalInvalidLoadTracking += Entry.NumInvalid;
-      TotalUnknownLoadTracking += Entry.NumUnknown;
-      Printf("invalid/unknown type for %llu/%llu loads at:\n", Entry.NumInvalid,
-             Entry.NumUnknown);
-      StackDepotGet(Entry.StackId).Print();
+    for (const auto &entry : TrackedLoads) {
+      TotalInvalidLoadTracking += entry.num_invalid;
+      TotalUnknownLoadTracking += entry.num_unknown;
+      Printf("invalid/unknown type for %llu/%llu loads at:\n",
+             entry.num_invalid, entry.num_unknown);
+      StackDepotGet(entry.stack_id).Print();
     }
     Printf(
         "There were %llu/%llu floating-point loads where the shadow type was "
@@ -150,9 +151,7 @@ void Stats::print() const {
   }
 }
 
-alignas(64) static char StatsPlaceholder[sizeof(Stats)];
-Stats *nsan_stats = nullptr;
-
-void initializeStats() { nsan_stats = new (StatsPlaceholder) Stats(); }
+alignas(64) static char stats_placeholder[sizeof(Stats)];
+Stats *__nsan::nsan_stats = nullptr;
 
-} // namespace __nsan
+void __nsan::InitializeStats() { nsan_stats = new (stats_placeholder) Stats(); }
diff --git a/compiler-rt/lib/nsan/nsan_stats.h b/compiler-rt/lib/nsan/nsan_stats.h
index 7e8c7bb7bf12d..d1c7e0115e7ae 100644
--- a/compiler-rt/lib/nsan/nsan_stats.h
+++ b/compiler-rt/lib/nsan/nsan_stats.h
@@ -40,52 +40,53 @@ class Stats {
   ~Stats();
 
   // Signal that we checked the instruction at the given address.
-  void addCheck(CheckTypeT CheckType, __sanitizer::uptr PC,
-                __sanitizer::uptr BP, double RelErr);
+  void AddCheck(CheckTypeT check_ty, __sanitizer::uptr pc, __sanitizer::uptr bp,
+                double rel_err);
   // Signal that we warned for the instruction at the given address.
-  void addWarning(CheckTypeT CheckType, __sanitizer::uptr PC,
-                  __sanitizer::uptr BP, double RelErr);
+  void AddWarning(CheckTypeT check_ty, __sanitizer::uptr pc,
+                  __sanitizer::uptr bp, double rel_err);
 
   // Signal that we detected a floating-point load where the shadow type was
   // invalid.
-  void addInvalidLoadTrackingEvent(__sanitizer::uptr PC, __sanitizer::uptr BP);
+  void AddInvalidLoadTrackingEvent(__sanitizer::uptr pc, __sanitizer::uptr bp);
   // Signal that we detected a floating-point load where the shadow type was
   // unknown but the value was nonzero.
-  void addUnknownLoadTrackingEvent(__sanitizer::uptr PC, __sanitizer::uptr BP);
+  void AddUnknownLoadTrackingEvent(__sanitizer::uptr pc, __sanitizer::uptr bp);
 
-  void print() const;
+  void Print() const;
 
 private:
   using IndexMap = __sanitizer::AddrHashMap<__sanitizer::uptr, 11>;
 
   struct CheckAndWarningsValue {
-    CheckTypeT CheckTy;
-    __sanitizer::u32 StackId = 0;
-    __sanitizer::u64 NumChecks = 0;
-    __sanitizer::u64 NumWarnings = 0;
+    CheckTypeT check_ty;
+    __sanitizer::u32 stack_id = 0;
+    __sanitizer::u64 num_checks = 0;
+    __sanitizer::u64 num_warnings = 0;
     // This is a bitcasted double. Doubles have the nice idea to be ordered as
     // ints.
-    double MaxRelativeError = 0;
+    double max_relative_err = 0;
   };
-  // Maps key(CheckType, StackId) to indices in CheckAndWarnings.
+  // Map Key(check_ty, StackId) to indices in CheckAndWarnings.
   IndexMap CheckAndWarningsMap;
-  __sanitizer::InternalMmapVectorNoCtor<CheckAndWarningsValue> CheckAndWarnings;
-  mutable __sanitizer::Mutex CheckAndWarningsMutex;
+  __sanitizer::InternalMmapVectorNoCtor<CheckAndWarningsValue>
+      check_and_warnings;
+  mutable __sanitizer::Mutex check_and_warning_mutex;
 
   struct LoadTrackingValue {
-    CheckTypeT CheckTy;
-    __sanitizer::u32 StackId = 0;
-    __sanitizer::u64 NumInvalid = 0;
-    __sanitizer::u64 NumUnknown = 0;
+    CheckTypeT check_ty;
+    __sanitizer::u32 stack_id = 0;
+    __sanitizer::u64 num_invalid = 0;
+    __sanitizer::u64 num_unknown = 0;
   };
-  // Maps key(CheckTypeT::kLoad, StackId) to indices in TrackedLoads.
+  // Map Key(CheckTypeT::kLoad, StackId) to indices in TrackedLoads.
   IndexMap LoadTrackingMap;
   __sanitizer::InternalMmapVectorNoCtor<LoadTrackingValue> TrackedLoads;
   mutable __sanitizer::Mutex TrackedLoadsMutex;
 };
 
 extern Stats *nsan_stats;
-void initializeStats();
+void InitializeStats();
 
 } // namespace __nsan
 
diff --git a/compiler-rt/lib/nsan/nsan_suppressions.cpp b/compiler-rt/lib/nsan/nsan_suppressions.cpp
index 12dd8708c37e7..c4d438e090e05 100644
--- a/compiler-rt/lib/nsan/nsan_suppressions.cpp
+++ b/compiler-rt/lib/nsan/nsan_suppressions.cpp
@@ -7,55 +7,52 @@
 //===----------------------------------------------------------------------===//
 
 #include "nsan_suppressions.h"
-
+#include "nsan_flags.h"
 #include "sanitizer_common/sanitizer_placement_new.h"
 #include "sanitizer_common/sanitizer_stacktrace.h"
 #include "sanitizer_common/sanitizer_symbolizer.h"
 
-#include "nsan_flags.h"
-
-// Can be overriden in frontend.
-SANITIZER_WEAK_DEFAULT_IMPL
-const char *__nsan_default_suppressions() { return 0; }
-
-namespace __nsan {
+using namespace __sanitizer;
+using namespace __nsan;
 
-const char *const kSuppressionFcmp = "fcmp";
-const char *const kSuppressionConsistency = "consistency";
+SANITIZER_INTERFACE_WEAK_DEF(const char *, __nsan_default_suppressions, void) {
+  return 0;
+}
 
-using namespace __sanitizer;
+const char kSuppressionFcmp[] = "fcmp";
+const char kSuppressionConsistency[] = "consistency";
 
-alignas(64) static char suppressionPlaceholder[sizeof(SuppressionContext)];
-static SuppressionContext *suppressionCtx = nullptr;
+alignas(64) static char suppression_placeholder[sizeof(SuppressionContext)];
+static SuppressionContext *suppression_ctx;
 
 // The order should match the enum CheckKind.
 static const char *kSuppressionTypes[] = {kSuppressionFcmp,
                                           kSuppressionConsistency};
 
-void InitializeSuppressions() {
-  CHECK_EQ(nullptr, suppressionCtx);
-  suppressionCtx = new (suppressionPlaceholder)
+void __nsan::InitializeSuppressions() {
+  CHECK_EQ(nullptr, suppression_ctx);
+  suppression_ctx = new (suppression_placeholder)
       SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
-  suppressionCtx->ParseFromFile(flags().suppressions);
-  suppressionCtx->Parse(__nsan_default_suppressions());
+  suppression_ctx->ParseFromFile(flags().suppressions);
+  suppression_ctx->Parse(__nsan_default_suppressions());
 }
 
-static Suppression *GetSuppressionForAddr(uptr addr, const char *supprType) {
+static Suppression *GetSuppressionForAddr(uptr addr, const char *suppr_type) {
   Suppression *s = nullptr;
 
   // Suppress by module name.
-  SuppressionContext *suppressions = suppressionCtx;
+  SuppressionContext *suppressions = suppression_ctx;
   if (const char *moduleName =
           Symbolizer::GetOrInit()->GetModuleNameForPc(addr)) {
-    if (suppressions->Match(moduleName, supprType, &s))
+    if (suppressions->Match(moduleName, suppr_type, &s))
       return s;
   }
 
   // Suppress by file or function name.
   SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(addr);
   for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
-    if (suppressions->Match(cur->info.function, supprType, &s) ||
-        suppressions->Match(cur->info.file, supprType, &s)) {
+    if (suppressions->Match(cur->info.function, suppr_type, &s) ||
+        suppressions->Match(cur->info.file, suppr_type, &s)) {
       break;
     }
   }
@@ -63,7 +60,8 @@ static Suppression *GetSuppressionForAddr(uptr addr, const char *supprType) {
   return s;
 }
 
-Suppression *GetSuppressionForStack(const StackTrace *stack, CheckKind k) {
+Suppression *__nsan::GetSuppressionForStack(const StackTrace *stack,
+                                            CheckKind k) {
   for (uptr i = 0, e = stack->size; i < e; i++) {
     Suppression *s = GetSuppressionForAddr(
         StackTrace::GetPreviousInstructionPc(stack->trace[i]),
@@ -73,5 +71,3 @@ Suppression *GetSuppressionForStack(const StackTrace *stack, CheckKind k) {
   }
   return nullptr;
 }
-
-} // end namespace __nsan

>From 45e30df51a89e5c17e1b0ff6175bba336899e9ef Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 20 Jun 2024 00:29:30 -0700
Subject: [PATCH 2/2] more

Created using spr 1.3.5-bogner
---
 compiler-rt/lib/nsan/nsan.cpp               |  2 +-
 compiler-rt/lib/nsan/nsan.h                 | 50 ++++++++++-----------
 compiler-rt/lib/nsan/nsan_interceptors.cpp  | 24 +++++-----
 compiler-rt/lib/nsan/tests/NSanUnitTest.cpp | 48 ++++++++++----------
 4 files changed, 62 insertions(+), 62 deletions(-)

diff --git a/compiler-rt/lib/nsan/nsan.cpp b/compiler-rt/lib/nsan/nsan.cpp
index fd5390e20a029..0c72f798a2dca 100644
--- a/compiler-rt/lib/nsan/nsan.cpp
+++ b/compiler-rt/lib/nsan/nsan.cpp
@@ -471,7 +471,7 @@ int32_t checkFT(const FT value, ShadowFT Shadow, CheckTypeT CheckType,
                log2l(static_cast<long double>(abs_err / largest / Eps)));
     }
     char UlpErrBuf[128] = "";
-    const double ShadowUlpDiff = getULPDiff(check_value, check_shadow);
+    const double ShadowUlpDiff = GetULPDiff(check_value, check_shadow);
     if (ShadowUlpDiff != kMaxULPDiff) {
       // This is the ULP diff in the internal domain. The user actually cares
       // about that in the original domain.
diff --git a/compiler-rt/lib/nsan/nsan.h b/compiler-rt/lib/nsan/nsan.h
index 510d68b9fef02..1b57e916700f6 100644
--- a/compiler-rt/lib/nsan/nsan.h
+++ b/compiler-rt/lib/nsan/nsan.h
@@ -164,60 +164,60 @@ template <> struct FTInfo<__float128> {
 constexpr double kMaxULPDiff = INFINITY;
 
 // Helper for getULPDiff that works on bit representations.
-template <typename BT> double getULPDiffBits(BT V1Bits, BT V2Bits) {
+template <typename BT> double GetULPDiffBits(BT v1_bits, BT v2_bits) {
   // If the integer representations of two same-sign floats are subtracted then
   // the absolute value of the result is equal to one plus the number of
   // representable floats between them.
-  return V1Bits >= V2Bits ? V1Bits - V2Bits : V2Bits - V1Bits;
+  return v1_bits >= v2_bits ? v1_bits - v2_bits : v2_bits - v1_bits;
 }
 
-// Returns the the number of floating point values between V1 and V2, capped to
+// Returns the the number of floating point values between v1 and v2, capped to
 // u64max. Return 0 for (-0.0,0.0).
-template <typename FT> double getULPDiff(FT V1, FT V2) {
-  if (V1 == V2) {
+template <typename FT> double GetULPDiff(FT v1, FT v2) {
+  if (v1 == v2) {
     return 0; // Typically, -0.0 and 0.0
   }
   using BT = typename FTInfo<FT>::orig_bits_type;
   static_assert(sizeof(FT) == sizeof(BT), "not implemented");
   static_assert(sizeof(BT) <= 64, "not implemented");
-  BT V1Bits;
-  __builtin_memcpy(&V1Bits, &V1, sizeof(BT));
-  BT V2Bits;
-  __builtin_memcpy(&V2Bits, &V2, sizeof(BT));
+  BT v1_bits;
+  __builtin_memcpy(&v1_bits, &v1, sizeof(BT));
+  BT v2_bits;
+  __builtin_memcpy(&v2_bits, &v2, sizeof(BT));
   // Check whether the signs differ. IEEE-754 float types always store the sign
   // in the most significant bit. NaNs and infinities are handled by the calling
   // code.
   constexpr BT kSignMask = BT{1} << (CHAR_BIT * sizeof(BT) - 1);
-  if ((V1Bits ^ V2Bits) & kSignMask) {
+  if ((v1_bits ^ v2_bits) & kSignMask) {
     // Signs differ. We can get the ULPs as `getULPDiff(negative_number, -0.0)
     // + getULPDiff(0.0, positive_number)`.
-    if (V1Bits & kSignMask) {
-      return getULPDiffBits<BT>(V1Bits, kSignMask) +
-             getULPDiffBits<BT>(0, V2Bits);
+    if (v1_bits & kSignMask) {
+      return GetULPDiffBits<BT>(v1_bits, kSignMask) +
+             GetULPDiffBits<BT>(0, v2_bits);
     } else {
-      return getULPDiffBits<BT>(V2Bits, kSignMask) +
-             getULPDiffBits<BT>(0, V1Bits);
+      return GetULPDiffBits<BT>(v2_bits, kSignMask) +
+             GetULPDiffBits<BT>(0, v1_bits);
     }
   }
-  return getULPDiffBits(V1Bits, V2Bits);
+  return GetULPDiffBits(v1_bits, v2_bits);
 }
 
 // FIXME: This needs mor work: Because there is no 80-bit integer type, we have
 // to go through __uint128_t. Therefore the assumptions about the sign bit do
 // not hold.
-template <> inline double getULPDiff(long double V1, long double V2) {
+template <> inline double GetULPDiff(long double v1, long double v2) {
   using BT = __uint128_t;
-  BT V1Bits = 0;
-  __builtin_memcpy(&V1Bits, &V1, sizeof(long double));
-  BT V2Bits = 0;
-  __builtin_memcpy(&V2Bits, &V2, sizeof(long double));
-  if ((V1Bits ^ V2Bits) & (BT{1} << (CHAR_BIT * sizeof(BT) - 1)))
-    return (V1 == V2) ? __sanitizer::u64{0} : kMaxULPDiff; // Signs differ.
+  BT v1_bits = 0;
+  __builtin_memcpy(&v1_bits, &v1, sizeof(long double));
+  BT v2_bits = 0;
+  __builtin_memcpy(&v2_bits, &v2, sizeof(long double));
+  if ((v1_bits ^ v2_bits) & (BT{1} << (CHAR_BIT * sizeof(BT) - 1)))
+    return v1 == v2 ? __sanitizer::u64{0} : kMaxULPDiff; // Signs differ.
   // If the integer representations of two same-sign floats are subtracted then
   // the absolute value of the result is equal to one plus the number of
   // representable floats between them.
-  BT Diff = V1Bits >= V2Bits ? V1Bits - V2Bits : V2Bits - V1Bits;
-  return Diff >= kMaxULPDiff ? kMaxULPDiff : Diff;
+  BT diff = v1_bits >= v2_bits ? v1_bits - v2_bits : v2_bits - v1_bits;
+  return diff >= kMaxULPDiff ? kMaxULPDiff : diff;
 }
 
 } // end namespace __nsan
diff --git a/compiler-rt/lib/nsan/nsan_interceptors.cpp b/compiler-rt/lib/nsan/nsan_interceptors.cpp
index 77ae3e8ac352a..fc5cbe39f5413 100644
--- a/compiler-rt/lib/nsan/nsan_interceptors.cpp
+++ b/compiler-rt/lib/nsan/nsan_interceptors.cpp
@@ -31,11 +31,11 @@ using __nsan::nsan_initialized;
 
 static constexpr uptr kEarlyAllocBufSize = 16384;
 static uptr AllocatedBytes;
-static char EarlyAllocBuf[kEarlyAllocBufSize];
+static char early_alloc_buf[kEarlyAllocBufSize];
 
-static bool isInEarlyAllocBuf(const void *Ptr) {
-  return ((uptr)Ptr >= (uptr)EarlyAllocBuf &&
-          ((uptr)Ptr - (uptr)EarlyAllocBuf) < sizeof(EarlyAllocBuf));
+static bool isInEarlyAllocBuf(const void *ptr) {
+  return ((uptr)ptr >= (uptr)early_alloc_buf &&
+          ((uptr)ptr - (uptr)early_alloc_buf) < sizeof(early_alloc_buf));
 }
 
 static u8 *toU8Ptr(wchar_t *ptr) { return reinterpret_cast<u8 *>(ptr); }
@@ -49,25 +49,25 @@ template <typename T> T min(T a, T b) { return a < b ? a : b; }
 // Handle allocation requests early (before all interceptors are setup). dlsym,
 // for example, calls calloc.
 static void *HandleEarlyAlloc(uptr size) {
-  void *Mem = (void *)&EarlyAllocBuf[AllocatedBytes];
+  void *Mem = (void *)&early_alloc_buf[AllocatedBytes];
   AllocatedBytes += size;
   CHECK_LT(AllocatedBytes, kEarlyAllocBufSize);
   return Mem;
 }
 
-INTERCEPTOR(void *, memset, void *dst, int V, uptr size) {
+INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
   // NOTE: This guard is needed because nsan's initialization code might call
   // memset.
   if (!nsan_initialized && REAL(memset) == nullptr)
-    return internal_memset(dst, V, size);
+    return internal_memset(dst, v, size);
 
-  void *res = REAL(memset)(dst, V, size);
+  void *res = REAL(memset)(dst, v, size);
   __nsan_set_value_unknown(static_cast<u8 *>(dst), size);
   return res;
 }
 
-INTERCEPTOR(wchar_t *, wmemset, wchar_t *dst, wchar_t V, uptr size) {
-  wchar_t *res = REAL(wmemset)(dst, V, size);
+INTERCEPTOR(wchar_t *, wmemset, wchar_t *dst, wchar_t v, uptr size) {
+  wchar_t *res = REAL(wmemset)(dst, v, size);
   __nsan_set_value_unknown(toU8Ptr(dst), sizeof(wchar_t) * size);
   return res;
 }
@@ -123,8 +123,8 @@ INTERCEPTOR(void *, malloc, uptr size) {
   return res;
 }
 
-INTERCEPTOR(void *, realloc, void *Ptr, uptr size) {
-  void *res = REAL(realloc)(Ptr, size);
+INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
+  void *res = REAL(realloc)(ptr, size);
   // FIXME: We might want to copy the types from the original allocation
   // (although that would require that we know its size).
   if (res)
diff --git a/compiler-rt/lib/nsan/tests/NSanUnitTest.cpp b/compiler-rt/lib/nsan/tests/NSanUnitTest.cpp
index 3c6b505aaf7a1..d121292c36682 100644
--- a/compiler-rt/lib/nsan/tests/NSanUnitTest.cpp
+++ b/compiler-rt/lib/nsan/tests/NSanUnitTest.cpp
@@ -14,41 +14,41 @@ namespace __nsan {
 
 template <typename FT, auto next> void TestFT() {
   // Basic local tests anchored at 0.0.
-  ASSERT_EQ(getULPDiff<FT>(0.0, 0.0), 0);
-  ASSERT_EQ(getULPDiff<FT>(-0.0, 0.0), 0);
-  ASSERT_EQ(getULPDiff<FT>(next(-0.0, -1.0), 0.0), 1);
-  ASSERT_EQ(getULPDiff<FT>(next(0.0, 1.0), -0.0), 1);
-  ASSERT_EQ(getULPDiff<FT>(next(-0.0, -1.0), next(0.0, 1.0)), 2);
+  ASSERT_EQ(GetULPDiff<FT>(0.0, 0.0), 0);
+  ASSERT_EQ(GetULPDiff<FT>(-0.0, 0.0), 0);
+  ASSERT_EQ(GetULPDiff<FT>(next(-0.0, -1.0), 0.0), 1);
+  ASSERT_EQ(GetULPDiff<FT>(next(0.0, 1.0), -0.0), 1);
+  ASSERT_EQ(GetULPDiff<FT>(next(-0.0, -1.0), next(0.0, 1.0)), 2);
   // Basic local tests anchored at 2.0.
-  ASSERT_EQ(getULPDiff<FT>(next(2.0, 1.0), 2.0), 1);
-  ASSERT_EQ(getULPDiff<FT>(next(2.0, 3.0), 2.0), 1);
-  ASSERT_EQ(getULPDiff<FT>(next(2.0, 1.0), next(2.0, 3.0)), 2);
+  ASSERT_EQ(GetULPDiff<FT>(next(2.0, 1.0), 2.0), 1);
+  ASSERT_EQ(GetULPDiff<FT>(next(2.0, 3.0), 2.0), 1);
+  ASSERT_EQ(GetULPDiff<FT>(next(2.0, 1.0), next(2.0, 3.0)), 2);
 
-  ASSERT_NE(getULPDiff<FT>(-0.01, 0.01), kMaxULPDiff);
+  ASSERT_NE(GetULPDiff<FT>(-0.01, 0.01), kMaxULPDiff);
 
   // Basic local tests anchored at a random number.
   const FT X = 4863.5123;
   const FT To = 2 * X;
   FT Y = X;
-  ASSERT_EQ(getULPDiff<FT>(X, Y), 0);
-  ASSERT_EQ(getULPDiff<FT>(-X, -Y), 0);
+  ASSERT_EQ(GetULPDiff<FT>(X, Y), 0);
+  ASSERT_EQ(GetULPDiff<FT>(-X, -Y), 0);
   Y = next(Y, To);
-  ASSERT_EQ(getULPDiff<FT>(X, Y), 1);
-  ASSERT_EQ(getULPDiff<FT>(-X, -Y), 1);
+  ASSERT_EQ(GetULPDiff<FT>(X, Y), 1);
+  ASSERT_EQ(GetULPDiff<FT>(-X, -Y), 1);
   Y = next(Y, To);
-  ASSERT_EQ(getULPDiff<FT>(X, Y), 2);
-  ASSERT_EQ(getULPDiff<FT>(-X, -Y), 2);
+  ASSERT_EQ(GetULPDiff<FT>(X, Y), 2);
+  ASSERT_EQ(GetULPDiff<FT>(-X, -Y), 2);
   Y = next(Y, To);
-  ASSERT_EQ(getULPDiff<FT>(X, Y), 3);
-  ASSERT_EQ(getULPDiff<FT>(-X, -Y), 3);
+  ASSERT_EQ(GetULPDiff<FT>(X, Y), 3);
+  ASSERT_EQ(GetULPDiff<FT>(-X, -Y), 3);
 
   // Values with larger differences.
   static constexpr const __sanitizer::u64 MantissaSize =
       __sanitizer::u64{1} << FTInfo<FT>::kMantissaBits;
-  ASSERT_EQ(getULPDiff<FT>(1.0, next(2.0, 1.0)), MantissaSize - 1);
-  ASSERT_EQ(getULPDiff<FT>(1.0, 2.0), MantissaSize);
-  ASSERT_EQ(getULPDiff<FT>(1.0, next(2.0, 3.0)), MantissaSize + 1);
-  ASSERT_EQ(getULPDiff<FT>(1.0, 3.0), (3 * MantissaSize) / 2);
+  ASSERT_EQ(GetULPDiff<FT>(1.0, next(2.0, 1.0)), MantissaSize - 1);
+  ASSERT_EQ(GetULPDiff<FT>(1.0, 2.0), MantissaSize);
+  ASSERT_EQ(GetULPDiff<FT>(1.0, next(2.0, 3.0)), MantissaSize + 1);
+  ASSERT_EQ(GetULPDiff<FT>(1.0, 3.0), (3 * MantissaSize) / 2);
 }
 
 TEST(NSanTest, Float) { TestFT<float, nextafterf>(); }
@@ -59,9 +59,9 @@ TEST(NSanTest, Double) {
 
 TEST(NSanTest, Float128) {
   // Very basic tests. FIXME: improve when we have nextafter<__float128>.
-  ASSERT_EQ(getULPDiff<__float128>(0.0, 0.0), 0);
-  ASSERT_EQ(getULPDiff<__float128>(-0.0, 0.0), 0);
-  ASSERT_NE(getULPDiff<__float128>(-0.01, 0.01), kMaxULPDiff);
+  ASSERT_EQ(GetULPDiff<__float128>(0.0, 0.0), 0);
+  ASSERT_EQ(GetULPDiff<__float128>(-0.0, 0.0), 0);
+  ASSERT_NE(GetULPDiff<__float128>(-0.01, 0.01), kMaxULPDiff);
 }
 
 } // end namespace __nsan



More information about the llvm-branch-commits mailing list