[compiler-rt] Synchronizing ASAN init on Windows (PR #71833)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 9 09:57:31 PST 2023


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 1df5ea29b43690b6622db2cad7b745607ca4de6a 9a50ce75d6d2d3a9e56c7763bfa0a9c287f880bf -- compiler-rt/lib/asan/asan_allocator.cpp compiler-rt/lib/asan/asan_globals.cpp compiler-rt/lib/asan/asan_interceptors.cpp compiler-rt/lib/asan/asan_interceptors.h compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp compiler-rt/lib/asan/asan_internal.h compiler-rt/lib/asan/asan_malloc_linux.cpp compiler-rt/lib/asan/asan_malloc_mac.cpp compiler-rt/lib/asan/asan_malloc_win.cpp compiler-rt/lib/asan/asan_rtl.cpp compiler-rt/lib/asan/asan_stack.cpp compiler-rt/lib/asan/asan_thread.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index 80d00a728100..234b18bd83aa 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -96,15 +96,15 @@ DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
   ASAN_WRITE_RANGE(ctx, ptr, size)
 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
   ASAN_READ_RANGE(ctx, ptr, size)
-#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)                               \
-  ASAN_INTERCEPTOR_ENTER(ctx, func);                                           \
-  do {                                                                         \
-    if (AsanInitIsRunning())                                                   \
-      return REAL(func)(__VA_ARGS__);                                          \
-    if (SANITIZER_APPLE && UNLIKELY(!AsanInited()))                            \
-      return REAL(func)(__VA_ARGS__);                                          \
-    ENSURE_ASAN_INITED();                                                      \
-  } while (false)
+#  define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)    \
+    ASAN_INTERCEPTOR_ENTER(ctx, func);                \
+    do {                                              \
+      if (AsanInitIsRunning())                        \
+        return REAL(func)(__VA_ARGS__);               \
+      if (SANITIZER_APPLE && UNLIKELY(!AsanInited())) \
+        return REAL(func)(__VA_ARGS__);               \
+      ENSURE_ASAN_INITED();                           \
+    } while (false)
 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
   do {                                            \
   } while (false)
@@ -556,7 +556,8 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
 INTERCEPTOR(char*, strdup, const char *s) {
   void *ctx;
   ASAN_INTERCEPTOR_ENTER(ctx, strdup);
-  if (UNLIKELY(!AsanInited())) return internal_strdup(s);
+  if (UNLIKELY(!AsanInited()))
+    return internal_strdup(s);
   ENSURE_ASAN_INITED();
   uptr length = internal_strlen(s);
   if (flags()->replace_str) {
@@ -574,7 +575,8 @@ INTERCEPTOR(char*, strdup, const char *s) {
 INTERCEPTOR(char*, __strdup, const char *s) {
   void *ctx;
   ASAN_INTERCEPTOR_ENTER(ctx, strdup);
-  if (UNLIKELY(!AsanInited())) return internal_strdup(s);
+  if (UNLIKELY(!AsanInited()))
+    return internal_strdup(s);
   ENSURE_ASAN_INITED();
   uptr length = internal_strlen(s);
   if (flags()->replace_str) {
@@ -634,8 +636,9 @@ INTERCEPTOR(int, atoi, const char *nptr) {
   void *ctx;
   ASAN_INTERCEPTOR_ENTER(ctx, atoi);
 #if SANITIZER_APPLE
-  if (UNLIKELY(!AsanInited())) return REAL(atoi)(nptr);
-#endif
+  if (UNLIKELY(!AsanInited()))
+    return REAL(atoi)(nptr);
+#  endif
   ENSURE_ASAN_INITED();
   if (!flags()->replace_str) {
     return REAL(atoi)(nptr);
@@ -655,8 +658,9 @@ INTERCEPTOR(long, atol, const char *nptr) {
   void *ctx;
   ASAN_INTERCEPTOR_ENTER(ctx, atol);
 #if SANITIZER_APPLE
-  if (UNLIKELY(!AsanInited())) return REAL(atol)(nptr);
-#endif
+  if (UNLIKELY(!AsanInited()))
+    return REAL(atol)(nptr);
+#  endif
   ENSURE_ASAN_INITED();
   if (!flags()->replace_str) {
     return REAL(atol)(nptr);
@@ -693,8 +697,9 @@ static void AtCxaAtexit(void *unused) {
 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
             void *dso_handle) {
 #if SANITIZER_APPLE
-  if (UNLIKELY(!AsanInited())) return REAL(__cxa_atexit)(func, arg, dso_handle);
-#endif
+  if (UNLIKELY(!AsanInited()))
+    return REAL(__cxa_atexit)(func, arg, dso_handle);
+#    endif
   ENSURE_ASAN_INITED();
 #if CAN_SANITIZE_LEAKS
   __lsan::ScopedInterceptorDisabler disabler;
diff --git a/compiler-rt/lib/asan/asan_interceptors.h b/compiler-rt/lib/asan/asan_interceptors.h
index 19a40f18c43f..e355c1258a9f 100644
--- a/compiler-rt/lib/asan/asan_interceptors.h
+++ b/compiler-rt/lib/asan/asan_interceptors.h
@@ -24,12 +24,12 @@ namespace __asan {
 void InitializeAsanInterceptors();
 void InitializePlatformInterceptors();
 
-#define ENSURE_ASAN_INITED()      \
-  do {                            \
-    CHECK(!AsanInitIsRunning());  \
-    if (UNLIKELY(!AsanInited())) {\
-      AsanInitFromRtl();          \
-    }                             \
+#define ENSURE_ASAN_INITED()       \
+  do {                             \
+    CHECK(!AsanInitIsRunning());   \
+    if (UNLIKELY(!AsanInited())) { \
+      AsanInitFromRtl();           \
+    }                              \
   } while (0)
 
 }  // namespace __asan
diff --git a/compiler-rt/lib/asan/asan_internal.h b/compiler-rt/lib/asan/asan_internal.h
index b8b8b44d0dcb..cb5dbfc7d22c 100644
--- a/compiler-rt/lib/asan/asan_internal.h
+++ b/compiler-rt/lib/asan/asan_internal.h
@@ -135,10 +135,10 @@ void InstallAtExitCheckLeaks();
 // failures.
 //
 // From a multithreaded managed environment, if an ASAN instrumented dll
-// is loading on a spawned thread, an intercepted function may be called on multiple
-// threads while ASAN is still in the process of initialization. This can also
-// cause the ASAN thread registry to create the "main" thread after another
-// thread, resulting in a TID != 0.
+// is loading on a spawned thread, an intercepted function may be called on
+// multiple threads while ASAN is still in the process of initialization. This
+// can also cause the ASAN thread registry to create the "main" thread after
+// another thread, resulting in a TID != 0.
 //
 // Two threads can also race to initialize ASAN, resulting in either incorrect
 // state or internal check failures for init already running.
diff --git a/compiler-rt/lib/asan/asan_malloc_mac.cpp b/compiler-rt/lib/asan/asan_malloc_mac.cpp
index 242f030b000a..d2380ee62bf3 100644
--- a/compiler-rt/lib/asan/asan_malloc_mac.cpp
+++ b/compiler-rt/lib/asan/asan_malloc_mac.cpp
@@ -23,45 +23,44 @@
 using namespace __asan;
 #define COMMON_MALLOC_ZONE_NAME "asan"
 #define COMMON_MALLOC_ENTER() ENSURE_ASAN_INITED()
-#define COMMON_MALLOC_SANITIZER_INITIALIZED AsanInited()
-#define COMMON_MALLOC_FORCE_LOCK() asan_mz_force_lock()
-#define COMMON_MALLOC_FORCE_UNLOCK() asan_mz_force_unlock()
-#define COMMON_MALLOC_MEMALIGN(alignment, size) \
-  GET_STACK_TRACE_MALLOC; \
-  void *p = asan_memalign(alignment, size, &stack, FROM_MALLOC)
-#define COMMON_MALLOC_MALLOC(size) \
-  GET_STACK_TRACE_MALLOC; \
-  void *p = asan_malloc(size, &stack)
-#define COMMON_MALLOC_REALLOC(ptr, size) \
-  GET_STACK_TRACE_MALLOC; \
-  void *p = asan_realloc(ptr, size, &stack);
-#define COMMON_MALLOC_CALLOC(count, size) \
-  GET_STACK_TRACE_MALLOC; \
-  void *p = asan_calloc(count, size, &stack);
-#define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
-  GET_STACK_TRACE_MALLOC; \
-  int res = asan_posix_memalign(memptr, alignment, size, &stack);
-#define COMMON_MALLOC_VALLOC(size) \
-  GET_STACK_TRACE_MALLOC; \
-  void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
-#define COMMON_MALLOC_FREE(ptr) \
-  GET_STACK_TRACE_FREE; \
-  asan_free(ptr, &stack, FROM_MALLOC);
-#define COMMON_MALLOC_SIZE(ptr) \
-  uptr size = asan_mz_size(ptr);
-#define COMMON_MALLOC_FILL_STATS(zone, stats) \
-  AsanMallocStats malloc_stats; \
-  FillMallocStatistics(&malloc_stats); \
-  CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \
-  internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
-#define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
-  GET_STACK_TRACE_FREE; \
-  ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
-#define COMMON_MALLOC_NAMESPACE __asan
-#define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
-#define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 1
+#  define COMMON_MALLOC_SANITIZER_INITIALIZED AsanInited()
+#  define COMMON_MALLOC_FORCE_LOCK() asan_mz_force_lock()
+#  define COMMON_MALLOC_FORCE_UNLOCK() asan_mz_force_unlock()
+#  define COMMON_MALLOC_MEMALIGN(alignment, size) \
+    GET_STACK_TRACE_MALLOC;                       \
+    void *p = asan_memalign(alignment, size, &stack, FROM_MALLOC)
+#  define COMMON_MALLOC_MALLOC(size) \
+    GET_STACK_TRACE_MALLOC;          \
+    void *p = asan_malloc(size, &stack)
+#  define COMMON_MALLOC_REALLOC(ptr, size) \
+    GET_STACK_TRACE_MALLOC;                \
+    void *p = asan_realloc(ptr, size, &stack);
+#  define COMMON_MALLOC_CALLOC(count, size) \
+    GET_STACK_TRACE_MALLOC;                 \
+    void *p = asan_calloc(count, size, &stack);
+#  define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
+    GET_STACK_TRACE_MALLOC;                                     \
+    int res = asan_posix_memalign(memptr, alignment, size, &stack);
+#  define COMMON_MALLOC_VALLOC(size) \
+    GET_STACK_TRACE_MALLOC;          \
+    void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
+#  define COMMON_MALLOC_FREE(ptr) \
+    GET_STACK_TRACE_FREE;         \
+    asan_free(ptr, &stack, FROM_MALLOC);
+#  define COMMON_MALLOC_SIZE(ptr) uptr size = asan_mz_size(ptr);
+#  define COMMON_MALLOC_FILL_STATS(zone, stats)                    \
+    AsanMallocStats malloc_stats;                                  \
+    FillMallocStatistics(&malloc_stats);                           \
+    CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats)); \
+    internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
+#  define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
+    GET_STACK_TRACE_FREE;                                                \
+    ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
+#  define COMMON_MALLOC_NAMESPACE __asan
+#  define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
+#  define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 1
 
-#include "sanitizer_common/sanitizer_malloc_mac.inc"
+#  include "sanitizer_common/sanitizer_malloc_mac.inc"
 
 namespace COMMON_MALLOC_NAMESPACE {
 
diff --git a/compiler-rt/lib/asan/asan_rtl.cpp b/compiler-rt/lib/asan/asan_rtl.cpp
index 2cdaf8827845..ade77d68f36d 100644
--- a/compiler-rt/lib/asan/asan_rtl.cpp
+++ b/compiler-rt/lib/asan/asan_rtl.cpp
@@ -430,7 +430,8 @@ void PrintAddressSpaceLayout() {
 
 static void AsanInitInternal() {
   CheckAsanInitRunning();
-  if (LIKELY(AsanInited())) return;
+  if (LIKELY(AsanInited()))
+    return;
   SanitizerToolName = "AddressSanitizer";
   CHECK(!AsanInitIsRunning() && "ASan init calls itself!");
   SetAsanInitIsRunning(1);
diff --git a/compiler-rt/lib/asan/asan_thread.cpp b/compiler-rt/lib/asan/asan_thread.cpp
index 4f70731a329c..d0efc163b255 100644
--- a/compiler-rt/lib/asan/asan_thread.cpp
+++ b/compiler-rt/lib/asan/asan_thread.cpp
@@ -300,7 +300,7 @@ AsanThread *CreateMainThread() {
 // Depending on the loading thread, specifically in managed scenarios, the main
 // thread can be created after other threads on Windows. This ensures we start
 // the main thread before those threads.
-#if SANITIZER_WINDOWS
+#  if SANITIZER_WINDOWS
   uptr PageSize = GetPageSizeCached();
   uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
   AsanThread *main_thread = (AsanThread *)MmapOrDie(size, __func__);
@@ -309,13 +309,13 @@ AsanThread *CreateMainThread() {
   SetCurrentThread(main_thread);
   main_thread->ThreadStart(internal_getpid());
   atomic_store(&mainThreadCreated, 1, memory_order_release);
-#else
+#  else
   AsanThread *main_thread = AsanThread::Create(
       /* parent_tid */ kMainTid,
       /* stack */ nullptr, /* detached */ true);
   SetCurrentThread(main_thread);
   main_thread->ThreadStart(internal_getpid());
-#endif
+#  endif
   return main_thread;
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/71833


More information about the llvm-commits mailing list