[compiler-rt] 3ccb770 - [compiler-rt] Mark some performance critical buffers uninitialized

Marco Elver via llvm-commits llvm-commits at lists.llvm.org
Tue May 30 03:02:28 PDT 2023


Author: Marco Elver
Date: 2023-05-30T11:59:09+02:00
New Revision: 3ccb7702425a965836ca69fe75184698a59ee8f9

URL: https://github.com/llvm/llvm-project/commit/3ccb7702425a965836ca69fe75184698a59ee8f9
DIFF: https://github.com/llvm/llvm-project/commit/3ccb7702425a965836ca69fe75184698a59ee8f9.diff

LOG: [compiler-rt] Mark some performance critical buffers uninitialized

With -ftrivial-auto-var-init, do not emit memset() calls for performance
critical stack variables.

Reviewed By: vitalybuka, dvyukov, MaskRay

Differential Revision: https://reviews.llvm.org/D151551

Added: 
    

Modified: 
    compiler-rt/lib/asan/asan_stack.h
    compiler-rt/lib/msan/msan.h
    compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h
    compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_stack.h b/compiler-rt/lib/asan/asan_stack.h
index b9575d2f427ee..02a76af847ae6 100644
--- a/compiler-rt/lib/asan/asan_stack.h
+++ b/compiler-rt/lib/asan/asan_stack.h
@@ -32,24 +32,24 @@ u32 GetMallocContextSize();
 // as early as possible (in functions exposed to the user), as we generally
 // don't want stack trace to contain functions from ASan internals.
 
-#define GET_STACK_TRACE(max_size, fast)                          \
-  BufferedStackTrace stack;                                      \
-  if (max_size <= 2) {                                           \
-    stack.size = max_size;                                       \
-    if (max_size > 0) {                                          \
-      stack.top_frame_bp = GET_CURRENT_FRAME();                  \
-      stack.trace_buffer[0] = StackTrace::GetCurrentPc();        \
-      if (max_size > 1) stack.trace_buffer[1] = GET_CALLER_PC(); \
-    }                                                            \
-  } else {                                                       \
-    stack.Unwind(StackTrace::GetCurrentPc(),                     \
-                 GET_CURRENT_FRAME(), nullptr, fast, max_size);  \
+#define GET_STACK_TRACE(max_size, fast)                                    \
+  UNINITIALIZED BufferedStackTrace stack;                                  \
+  if (max_size <= 2) {                                                     \
+    stack.size = max_size;                                                 \
+    if (max_size > 0) {                                                    \
+      stack.top_frame_bp = GET_CURRENT_FRAME();                            \
+      stack.trace_buffer[0] = StackTrace::GetCurrentPc();                  \
+      if (max_size > 1)                                                    \
+        stack.trace_buffer[1] = GET_CALLER_PC();                           \
+    }                                                                      \
+  } else {                                                                 \
+    stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, \
+                 fast, max_size);                                          \
   }
 
-#define GET_STACK_TRACE_FATAL(pc, bp)              \
-  BufferedStackTrace stack;                        \
-  stack.Unwind(pc, bp, nullptr,                    \
-               common_flags()->fast_unwind_on_fatal)
+#define GET_STACK_TRACE_FATAL(pc, bp)     \
+  UNINITIALIZED BufferedStackTrace stack; \
+  stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal)
 
 #define GET_STACK_TRACE_FATAL_HERE                                \
   GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)

diff  --git a/compiler-rt/lib/msan/msan.h b/compiler-rt/lib/msan/msan.h
index 5d8ea52668abe..50cbc5fe44d37 100644
--- a/compiler-rt/lib/msan/msan.h
+++ b/compiler-rt/lib/msan/msan.h
@@ -269,31 +269,33 @@ const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
 const int STACK_TRACE_TAG_FIELDS = STACK_TRACE_TAG_POISON + 1;
 const int STACK_TRACE_TAG_VPTR = STACK_TRACE_TAG_FIELDS + 1;
 
-#define GET_MALLOC_STACK_TRACE                                            \
-  BufferedStackTrace stack;                                               \
-  if (__msan_get_track_origins() && msan_inited)                          \
-    stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(),         \
-                 nullptr, common_flags()->fast_unwind_on_malloc,          \
-                 common_flags()->malloc_context_size)
+#define GET_MALLOC_STACK_TRACE                                             \
+  UNINITIALIZED BufferedStackTrace stack;                                  \
+  if (__msan_get_track_origins() && msan_inited) {                         \
+    stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, \
+                 common_flags()->fast_unwind_on_malloc,                    \
+                 common_flags()->malloc_context_size);                     \
+  }
 
 // For platforms which support slow unwinder only, we restrict the store context
 // size to 1, basically only storing the current pc. We do this because the slow
 // unwinder which is based on libunwind is not async signal safe and causes
 // random freezes in forking applications as well as in signal handlers.
-#define GET_STORE_STACK_TRACE_PC_BP(pc, bp)                                    \
-  BufferedStackTrace stack;                                                    \
-  if (__msan_get_track_origins() > 1 && msan_inited) {                         \
-    int size = flags()->store_context_size;                                    \
-    if (!SANITIZER_CAN_FAST_UNWIND)                                            \
-      size = Min(size, 1);                                                     \
-    stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_malloc, size);\
+#define GET_STORE_STACK_TRACE_PC_BP(pc, bp)                              \
+  UNINITIALIZED BufferedStackTrace stack;                                \
+  if (__msan_get_track_origins() > 1 && msan_inited) {                   \
+    int size = flags()->store_context_size;                              \
+    if (!SANITIZER_CAN_FAST_UNWIND)                                      \
+      size = Min(size, 1);                                               \
+    stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_malloc, \
+                 size);                                                  \
   }
 
 #define GET_STORE_STACK_TRACE \
   GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
 
 #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp)                              \
-  BufferedStackTrace stack;                                              \
+  UNINITIALIZED BufferedStackTrace stack;                                \
   if (msan_inited) {                                                     \
     stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal); \
   }

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h
index f2471efced613..52fe3fe3d15bd 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h
@@ -353,7 +353,7 @@ class SizeClassAllocator32 {
     DCHECK_GT(max_count, 0);
     TransferBatch *b = nullptr;
     constexpr uptr kShuffleArraySize = 48;
-    uptr shuffle_array[kShuffleArraySize];
+    UNINITIALIZED uptr shuffle_array[kShuffleArraySize];
     uptr count = 0;
     for (uptr i = region; i < region + n_chunks * size; i += size) {
       shuffle_array[count++] = i;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
index ee1b3156c779e..95f4760cffd74 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
@@ -215,6 +215,7 @@ typedef u64 tid_t;
 # define UNLIKELY(x) (x)
 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
 # define WARN_UNUSED_RESULT
+# define UNINITIALIZED
 #else  // _MSC_VER
 # define ALWAYS_INLINE inline __attribute__((always_inline))
 # define ALIAS(x) __attribute__((alias(SANITIZER_STRINGIFY(x))))
@@ -234,6 +235,11 @@ typedef u64 tid_t;
 #  define PREFETCH(x) __builtin_prefetch(x)
 # endif
 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+# if __has_attribute(uninitialized)
+#  define UNINITIALIZED __attribute__((uninitialized))
+# else  // __has_attribute(uninitialized)
+#  define UNINITIALIZED
+# endif  // __has_attribute(uninitialized)
 #endif  // _MSC_VER
 
 #if !defined(_MSC_VER) || defined(__clang__)


        


More information about the llvm-commits mailing list