[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:45:56 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/4] 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/4] 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

>From adeb6b9616f5ca9a72b965aea2c9b2db00459ff2 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 20 Jun 2024 00:42:45 -0700
Subject: [PATCH 3/4] more

Created using spr 1.3.5-bogner
---
 compiler-rt/lib/nsan/nsan.cpp              | 66 +++++++++++-----------
 compiler-rt/lib/nsan/nsan.h                | 12 ++--
 compiler-rt/lib/nsan/nsan_interceptors.cpp | 26 ++++-----
 3 files changed, 49 insertions(+), 55 deletions(-)

diff --git a/compiler-rt/lib/nsan/nsan.cpp b/compiler-rt/lib/nsan/nsan.cpp
index 0c72f798a2dca..f7b2ce2048290 100644
--- a/compiler-rt/lib/nsan/nsan.cpp
+++ b/compiler-rt/lib/nsan/nsan.cpp
@@ -51,16 +51,16 @@
 using namespace __sanitizer;
 using namespace __nsan;
 
-static constexpr const int kMaxVectorWidth = 8;
+constexpr int kMaxVectorWidth = 8;
 
 // When copying application memory, we also copy its shadow and shadow type.
 // FIXME: We could provide fixed-size versions that would nicely
 // vectorize for known sizes.
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
 __nsan_copy_values(const u8 *daddr, const u8 *saddr, uptr size) {
-  internal_memmove((void *)getShadowTypeAddrFor(daddr),
-                   getShadowTypeAddrFor(saddr), size);
-  internal_memmove((void *)getShadowAddrFor(daddr), getShadowAddrFor(saddr),
+  internal_memmove((void *)GetShadowTypeAddrFor(daddr),
+                   GetShadowTypeAddrFor(saddr), size);
+  internal_memmove((void *)GetShadowAddrFor(daddr), GetShadowAddrFor(saddr),
                    size * kShadowScale);
 }
 
@@ -68,7 +68,7 @@ __nsan_copy_values(const u8 *daddr, const u8 *saddr, uptr size) {
 // vectorize for known sizes.
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
 __nsan_set_value_unknown(const u8 *addr, uptr size) {
-  internal_memset((void *)getShadowTypeAddrFor(addr), 0, size);
+  internal_memset((void *)GetShadowTypeAddrFor(addr), 0, size);
 }
 
 
@@ -83,25 +83,27 @@ const char FTInfo<long double>::kTypePattern[sizeof(long double)];
 
 // Helper for __nsan_dump_shadow_mem: Reads the value at address `ptr`,
 // identified by its type id.
-template <typename ShadowFT> __float128 readShadowInternal(const u8 *ptr) {
+template <typename ShadowFT>
+static __float128 ReadShadowInternal(const u8 *ptr) {
   ShadowFT Shadow;
   __builtin_memcpy(&Shadow, ptr, sizeof(Shadow));
   return Shadow;
 }
 
-__float128 readShadow(const u8 *ptr, const char ShadowTypeId) {
+static __float128 ReadShadow(const u8 *ptr, const char ShadowTypeId) {
   switch (ShadowTypeId) {
   case 'd':
-    return readShadowInternal<double>(ptr);
+    return ReadShadowInternal<double>(ptr);
   case 'l':
-    return readShadowInternal<long double>(ptr);
+    return ReadShadowInternal<long double>(ptr);
   case 'q':
-    return readShadowInternal<__float128>(ptr);
+    return ReadShadowInternal<__float128>(ptr);
   default:
     return 0.0;
   }
 }
 
+namespace {
 class Decorator : public __sanitizer::SanitizerCommonDecorator {
 public:
   Decorator() : SanitizerCommonDecorator() {}
@@ -110,8 +112,6 @@ class Decorator : public __sanitizer::SanitizerCommonDecorator {
   const char *End() { return Default(); }
 };
 
-namespace {
-
 // Workaround for the fact that Printf() does not support floats.
 struct PrintBuffer {
   char Buffer[64];
@@ -202,12 +202,12 @@ static void NsanAtexit() {
 // We have to have 3 versions because we need to know which type we are storing
 // since we are setting the type shadow memory.
 template <typename FT> static u8 *getShadowPtrForStore(u8 *store_addr, uptr n) {
-  unsigned char *shadow_type = getShadowTypeAddrFor(store_addr);
+  unsigned char *shadow_type = GetShadowTypeAddrFor(store_addr);
   for (uptr i = 0; i < n; ++i) {
     __builtin_memcpy(shadow_type + i * sizeof(FT), FTInfo<FT>::kTypePattern,
                      sizeof(FTInfo<FT>::kTypePattern));
   }
-  return getShadowAddrFor(store_addr);
+  return GetShadowAddrFor(store_addr);
 }
 
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u8 *
@@ -244,7 +244,7 @@ template <typename FT> static bool IsUnknownShadowType(const u8 *shadow_type) {
 // They return nullptr if the type of the value is unknown or incomplete.
 template <typename FT>
 static const u8 *getShadowPtrForLoad(const u8 *load_addr, uptr n) {
-  const u8 *const shadow_type = getShadowTypeAddrFor(load_addr);
+  const u8 *const shadow_type = GetShadowTypeAddrFor(load_addr);
   for (uptr i = 0; i < n; ++i) {
     if (!IsValidShadowType<FT>(shadow_type + i * sizeof(FT))) {
       // If loadtracking stats are enabled, log loads with invalid types
@@ -266,7 +266,7 @@ static const u8 *getShadowPtrForLoad(const u8 *load_addr, uptr n) {
       return nullptr;
     }
   }
-  return getShadowAddrFor(load_addr);
+  return GetShadowAddrFor(load_addr);
 }
 
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE const u8 *
@@ -288,14 +288,14 @@ __nsan_get_shadow_ptr_for_longdouble_load(const u8 *load_addr, uptr n) {
 // opaque.
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u8 *
 __nsan_internal_get_raw_shadow_ptr(const u8 *addr) {
-  return getShadowAddrFor(const_cast<u8 *>(addr));
+  return GetShadowAddrFor(const_cast<u8 *>(addr));
 }
 
 // Returns the raw shadow type pointer. The returned pointer should be
 // considered opaque.
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u8 *
 __nsan_internal_get_raw_shadow_type_ptr(const u8 *addr) {
-  return reinterpret_cast<u8 *>(getShadowTypeAddrFor(const_cast<u8 *>(addr)));
+  return reinterpret_cast<u8 *>(GetShadowTypeAddrFor(const_cast<u8 *>(addr)));
 }
 
 static ValueType getValueType(u8 c) { return static_cast<ValueType>(c & 0x3); }
@@ -322,8 +322,8 @@ static bool checkValueConsistency(const u8 *shadow_type) {
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
 __nsan_dump_shadow_mem(const u8 *addr, size_t size_bytes, size_t bytes_per_line,
                        size_t shadow_value_type_ids) {
-  const u8 *const shadow_type = getShadowTypeAddrFor(addr);
-  const u8 *const shadow = getShadowAddrFor(addr);
+  const u8 *const shadow_type = GetShadowTypeAddrFor(addr);
+  const u8 *const shadow = GetShadowAddrFor(addr);
 
   constexpr int kMaxNumDecodedValues = 16;
   __float128 decoded_values[kMaxNumDecodedValues];
@@ -356,7 +356,7 @@ __nsan_dump_shadow_mem(const u8 *addr, size_t size_bytes, size_t bytes_per_line,
         printf("f%x ", pos);
         if (LastPos == sizeof(float) - 1) {
           decoded_values[num_decoded_values] =
-              readShadow(shadow + kShadowScale * (Offset + 1 - sizeof(float)),
+              ReadShadow(shadow + kShadowScale * (Offset + 1 - sizeof(float)),
                          static_cast<char>(shadow_value_type_ids & 0xff));
           ++num_decoded_values;
         }
@@ -364,7 +364,7 @@ __nsan_dump_shadow_mem(const u8 *addr, size_t size_bytes, size_t bytes_per_line,
       case kDoubleValueType:
         printf("d%x ", pos);
         if (LastPos == sizeof(double) - 1) {
-          decoded_values[num_decoded_values] = readShadow(
+          decoded_values[num_decoded_values] = ReadShadow(
               shadow + kShadowScale * (Offset + 1 - sizeof(double)),
               static_cast<char>((shadow_value_type_ids >> 8) & 0xff));
           ++num_decoded_values;
@@ -373,7 +373,7 @@ __nsan_dump_shadow_mem(const u8 *addr, size_t size_bytes, size_t bytes_per_line,
       case kFp80ValueType:
         printf("l%x ", pos);
         if (LastPos == sizeof(long double) - 1) {
-          decoded_values[num_decoded_values] = readShadow(
+          decoded_values[num_decoded_values] = ReadShadow(
               shadow + kShadowScale * (Offset + 1 - sizeof(long double)),
               static_cast<char>((shadow_value_type_ids >> 16) & 0xff));
           ++num_decoded_values;
@@ -470,17 +470,17 @@ int32_t checkFT(const FT value, ShadowFT Shadow, CheckTypeT CheckType,
                static_cast<long double>(100.0 * abs_err / largest),
                log2l(static_cast<long double>(abs_err / largest / Eps)));
     }
-    char UlpErrBuf[128] = "";
-    const double ShadowUlpDiff = GetULPDiff(check_value, check_shadow);
-    if (ShadowUlpDiff != kMaxULPDiff) {
+    char ulp_err_buf[128] = "";
+    const double shadow_ulp_diff = GetULPDiff(check_value, check_shadow);
+    if (shadow_ulp_diff != kMaxULPDiff) {
       // This is the ULP diff in the internal domain. The user actually cares
       // about that in the original domain.
-      const double UlpDiff =
-          ShadowUlpDiff / (u64{1} << (FTInfo<InternalFT>::kMantissaBits -
-                                      FTInfo<FT>::kMantissaBits));
-      snprintf(UlpErrBuf, sizeof(UlpErrBuf) - 1,
-               "(%.0f ULPs == %.1f digits == %.1f bits)", UlpDiff,
-               log10(UlpDiff), log2(UlpDiff));
+      const double ulp_diff =
+          shadow_ulp_diff / (u64{1} << (FTInfo<InternalFT>::kMantissaBits -
+                                        FTInfo<FT>::kMantissaBits));
+      snprintf(ulp_err_buf, sizeof(ulp_err_buf) - 1,
+               "(%.0f ULPs == %.1f digits == %.1f bits)", ulp_diff,
+               log10(ulp_diff), log2(ulp_diff));
     }
     Printf("WARNING: NumericalStabilitySanitizer: inconsistent shadow results");
     switch (CheckType) {
@@ -525,7 +525,7 @@ int32_t checkFT(const FT value, ShadowFT Shadow, CheckTypeT CheckType,
            ShadowPrinter::dec(Shadow).Buffer, ShadowPrinter::hex(Shadow).Buffer,
            FTInfo<FT>::kCppTypeName, ValuePrinter::dec(Shadow).Buffer,
            ValuePrinter::hex(Shadow).Buffer, RelErrBuf,
-           ValuePrinter::hex(abs_err).Buffer, UlpErrBuf);
+           ValuePrinter::hex(abs_err).Buffer, ulp_err_buf);
     stack.Print();
   }
 
diff --git a/compiler-rt/lib/nsan/nsan.h b/compiler-rt/lib/nsan/nsan.h
index 1b57e916700f6..896e5379dfc37 100644
--- a/compiler-rt/lib/nsan/nsan.h
+++ b/compiler-rt/lib/nsan/nsan.h
@@ -58,25 +58,25 @@ void InitializeInterceptors();
 
 // See notes in nsan_platform.
 // printf-free (see comment in nsan_interceptors.cc).
-inline u8 *getShadowAddrFor(u8 *Ptr) {
+inline u8 *GetShadowAddrFor(u8 *Ptr) {
   uptr AppOffset = ((uptr)Ptr) & ShadowMask();
   return (u8 *)(AppOffset * kShadowScale + ShadowAddr());
 }
 
 // printf-free (see comment in nsan_interceptors.cc).
-inline const u8 *getShadowAddrFor(const u8 *Ptr) {
-  return getShadowAddrFor(const_cast<u8 *>(Ptr));
+inline const u8 *GetShadowAddrFor(const u8 *Ptr) {
+  return GetShadowAddrFor(const_cast<u8 *>(Ptr));
 }
 
 // printf-free (see comment in nsan_interceptors.cc).
-inline u8 *getShadowTypeAddrFor(u8 *Ptr) {
+inline u8 *GetShadowTypeAddrFor(u8 *Ptr) {
   uptr AppOffset = ((uptr)Ptr) & ShadowMask();
   return (u8 *)(AppOffset + TypesAddr());
 }
 
 // printf-free (see comment in nsan_interceptors.cc).
-inline const u8 *getShadowTypeAddrFor(const u8 *Ptr) {
-  return getShadowTypeAddrFor(const_cast<u8 *>(Ptr));
+inline const u8 *GetShadowTypeAddrFor(const u8 *Ptr) {
+  return GetShadowTypeAddrFor(const_cast<u8 *>(Ptr));
 }
 
 // Information about value types and their shadow counterparts.
diff --git a/compiler-rt/lib/nsan/nsan_interceptors.cpp b/compiler-rt/lib/nsan/nsan_interceptors.cpp
index fc5cbe39f5413..61ea85e83dbb2 100644
--- a/compiler-rt/lib/nsan/nsan_interceptors.cpp
+++ b/compiler-rt/lib/nsan/nsan_interceptors.cpp
@@ -29,8 +29,8 @@ using namespace __sanitizer;
 using __nsan::nsan_init_is_running;
 using __nsan::nsan_initialized;
 
-static constexpr uptr kEarlyAllocBufSize = 16384;
-static uptr AllocatedBytes;
+constexpr uptr kEarlyAllocBufSize = 16384;
+static uptr allocated_bytes;
 static char early_alloc_buf[kEarlyAllocBufSize];
 
 static bool isInEarlyAllocBuf(const void *ptr) {
@@ -38,20 +38,14 @@ static bool isInEarlyAllocBuf(const void *ptr) {
           ((uptr)ptr - (uptr)early_alloc_buf) < sizeof(early_alloc_buf));
 }
 
-static u8 *toU8Ptr(wchar_t *ptr) { return reinterpret_cast<u8 *>(ptr); }
-
-static const u8 *toU8Ptr(const wchar_t *ptr) {
-  return reinterpret_cast<const u8 *>(ptr);
-}
-
 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 *)&early_alloc_buf[AllocatedBytes];
-  AllocatedBytes += size;
-  CHECK_LT(AllocatedBytes, kEarlyAllocBufSize);
+  void *Mem = (void *)&early_alloc_buf[allocated_bytes];
+  allocated_bytes += size;
+  CHECK_LT(allocated_bytes, kEarlyAllocBufSize);
   return Mem;
 }
 
@@ -68,7 +62,7 @@ INTERCEPTOR(void *, memset, void *dst, int v, uptr 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);
+  __nsan_set_value_unknown((u8 *)dst, sizeof(wchar_t) * size);
   return res;
 }
 
@@ -86,7 +80,7 @@ INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
 
 INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dst, const wchar_t *src, uptr size) {
   wchar_t *res = REAL(wmemmove)(dst, src, size);
-  __nsan_copy_values(toU8Ptr(dst), toU8Ptr(src), sizeof(wchar_t) * size);
+  __nsan_copy_values((u8 *)dst, (const u8 *)src, sizeof(wchar_t) * size);
   return res;
 }
 
@@ -107,7 +101,7 @@ INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
 
 INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dst, const wchar_t *src, uptr size) {
   wchar_t *res = REAL(wmemcpy)(dst, src, size);
-  __nsan_copy_values(toU8Ptr(dst), toU8Ptr(src), sizeof(wchar_t) * size);
+  __nsan_copy_values((u8 *)dst, (const u8 *)src, sizeof(wchar_t) * size);
   return res;
 }
 
@@ -227,8 +221,8 @@ static void nsanCopyZeroTerminated(char *dst, const char *src, uptr n) {
 }
 
 static void nsanWCopyZeroTerminated(wchar_t *dst, const wchar_t *src, uptr n) {
-  __nsan_copy_values(toU8Ptr(dst), toU8Ptr(src), sizeof(wchar_t) * n);
-  __nsan_set_value_unknown(toU8Ptr(dst + n), sizeof(wchar_t));
+  __nsan_copy_values((u8 *)dst, (const u8 *)(src), sizeof(wchar_t) * n);
+  __nsan_set_value_unknown((u8 *)(dst + n), sizeof(wchar_t));
 }
 
 INTERCEPTOR(char *, strdup, const char *S) {

>From 2dea1f78be93d5a03b7b70f6564503bd16008c9b Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 20 Jun 2024 00:45:46 -0700
Subject: [PATCH 4/4] rebase

Created using spr 1.3.5-bogner
---
 compiler-rt/lib/nsan/nsan_interceptors.cpp | 40 +++++++++++-----------
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/compiler-rt/lib/nsan/nsan_interceptors.cpp b/compiler-rt/lib/nsan/nsan_interceptors.cpp
index 61ea85e83dbb2..68127f169ee46 100644
--- a/compiler-rt/lib/nsan/nsan_interceptors.cpp
+++ b/compiler-rt/lib/nsan/nsan_interceptors.cpp
@@ -154,15 +154,15 @@ INTERCEPTOR(void *, valloc, uptr size) {
   return res;
 }
 
-INTERCEPTOR(void *, memalign, uptr Alignment, uptr size) {
-  void *const res = REAL(memalign)(Alignment, size);
+INTERCEPTOR(void *, memalign, uptr align, uptr size) {
+  void *const res = REAL(memalign)(align, size);
   if (res)
     __nsan_set_value_unknown(static_cast<u8 *>(res), size);
   return res;
 }
 
-INTERCEPTOR(void *, __libc_memalign, uptr Alignment, uptr size) {
-  void *const res = REAL(__libc_memalign)(Alignment, size);
+INTERCEPTOR(void *, __libc_memalign, uptr align, uptr size) {
+  void *const res = REAL(__libc_memalign)(align, size);
   if (res)
     __nsan_set_value_unknown(static_cast<u8 *>(res), size);
   return res;
@@ -175,30 +175,30 @@ INTERCEPTOR(void *, pvalloc, uptr size) {
   return res;
 }
 
-INTERCEPTOR(void *, aligned_alloc, uptr Alignment, uptr size) {
-  void *const res = REAL(aligned_alloc)(Alignment, size);
+INTERCEPTOR(void *, aligned_alloc, uptr align, uptr size) {
+  void *const res = REAL(aligned_alloc)(align, size);
   if (res)
     __nsan_set_value_unknown(static_cast<u8 *>(res), size);
   return res;
 }
 
-INTERCEPTOR(int, posix_memalign, void **Memptr, uptr Alignment, uptr size) {
-  int res = REAL(posix_memalign)(Memptr, Alignment, size);
-  if (res == 0 && *Memptr)
-    __nsan_set_value_unknown(static_cast<u8 *>(*Memptr), size);
+INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr size) {
+  int res = REAL(posix_memalign)(memptr, align, size);
+  if (res == 0 && *memptr)
+    __nsan_set_value_unknown(static_cast<u8 *>(*memptr), size);
   return res;
 }
 
-INTERCEPTOR(char *, strfry, char *S) {
-  const auto Len = internal_strlen(S);
-  char *res = REAL(strfry)(S);
+INTERCEPTOR(char *, strfry, char *s) {
+  const auto Len = internal_strlen(s);
+  char *res = REAL(strfry)(s);
   if (res)
-    __nsan_set_value_unknown(reinterpret_cast<u8 *>(S), Len);
+    __nsan_set_value_unknown(reinterpret_cast<u8 *>(s), Len);
   return res;
 }
 
-INTERCEPTOR(char *, strsep, char **Stringp, const char *Delim) {
-  char *OrigStringp = REAL(strsep)(Stringp, Delim);
+INTERCEPTOR(char *, strsep, char **Stringp, const char *delim) {
+  char *OrigStringp = REAL(strsep)(Stringp, delim);
   if (Stringp != nullptr) {
     // The previous character has been overwritten with a '\0' char.
     __nsan_set_value_unknown(reinterpret_cast<u8 *>(*Stringp) - 1, 1);
@@ -206,12 +206,12 @@ INTERCEPTOR(char *, strsep, char **Stringp, const char *Delim) {
   return OrigStringp;
 }
 
-INTERCEPTOR(char *, strtok, char *Str, const char *Delim) {
+INTERCEPTOR(char *, strtok, char *str, const char *delim) {
   // This is overly conservative, but the probability that modern code is using
   // strtok on double data is essentially zero anyway.
-  if (Str)
-    __nsan_set_value_unknown(reinterpret_cast<u8 *>(Str), internal_strlen(Str));
-  return REAL(strtok)(Str, Delim);
+  if (str)
+    __nsan_set_value_unknown(reinterpret_cast<u8 *>(str), internal_strlen(str));
+  return REAL(strtok)(str, delim);
 }
 
 static void nsanCopyZeroTerminated(char *dst, const char *src, uptr n) {



More information about the llvm-branch-commits mailing list