[compiler-rt] be8e4de - [GWP-ASan] Rework utilities (NFC)

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 20 16:04:55 PDT 2020


Author: Kostya Kortchinsky
Date: 2020-10-20T16:04:21-07:00
New Revision: be8e4de7240eb0f7dc2f7fd6d07898184c04c72b

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

LOG: [GWP-ASan] Rework utilities (NFC)

Few changes wrt utilities:
- split `Check` into a platform agnostic condition test and a platform
  specific termination, for which we introduce the function `die`.
- add a platform agnostic `utilities.cpp` that gets the allocation
  alignment functions original in the platform specific file, as they
  are reusable by all platforms.

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

Added: 
    compiler-rt/lib/gwp_asan/utilities.cpp

Modified: 
    compiler-rt/lib/gwp_asan/CMakeLists.txt
    compiler-rt/lib/gwp_asan/platform_specific/utilities_posix.cpp
    compiler-rt/lib/gwp_asan/utilities.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/gwp_asan/CMakeLists.txt b/compiler-rt/lib/gwp_asan/CMakeLists.txt
index 2fc789c96b13..60efc8a39e62 100644
--- a/compiler-rt/lib/gwp_asan/CMakeLists.txt
+++ b/compiler-rt/lib/gwp_asan/CMakeLists.txt
@@ -12,6 +12,7 @@ set(GWP_ASAN_SOURCES
   guarded_pool_allocator.cpp
   random.cpp
   stack_trace_compressor.cpp
+  utilities.cpp
 )
 
 set(GWP_ASAN_HEADERS

diff  --git a/compiler-rt/lib/gwp_asan/platform_specific/utilities_posix.cpp b/compiler-rt/lib/gwp_asan/platform_specific/utilities_posix.cpp
index 98f74e0f8b95..477a7ffc459e 100644
--- a/compiler-rt/lib/gwp_asan/platform_specific/utilities_posix.cpp
+++ b/compiler-rt/lib/gwp_asan/platform_specific/utilities_posix.cpp
@@ -19,72 +19,14 @@ extern "C" GWP_ASAN_WEAK void android_set_abort_message(const char *);
 #endif
 
 namespace gwp_asan {
-
+void die(const char *Message) {
 #ifdef __BIONIC__
-void Check(bool Condition, const char *Message) {
-  if (Condition)
-    return;
   if (&android_set_abort_message != nullptr)
     android_set_abort_message(Message);
   abort();
-}
 #else // __BIONIC__
-void Check(bool Condition, const char *Message) {
-  if (Condition)
-    return;
   fprintf(stderr, "%s", Message);
   __builtin_trap();
-}
-#endif // __BIONIC__
-
-// See `bionic/tests/malloc_test.cpp` in the Android source for documentation
-// regarding their alignment guarantees. We always round up to the closest
-// 8-byte window. As GWP-ASan's malloc(X) can always get exactly an X-sized
-// allocation, an allocation that rounds up to 16-bytes will always be given a
-// 16-byte aligned allocation.
-static size_t alignBionic(size_t RealAllocationSize) {
-  if (RealAllocationSize % 8 == 0)
-    return RealAllocationSize;
-  return RealAllocationSize + 8 - (RealAllocationSize % 8);
-}
-
-static size_t alignPowerOfTwo(size_t RealAllocationSize) {
-  if (RealAllocationSize <= 2)
-    return RealAllocationSize;
-  if (RealAllocationSize <= 4)
-    return 4;
-  if (RealAllocationSize <= 8)
-    return 8;
-  if (RealAllocationSize % 16 == 0)
-    return RealAllocationSize;
-  return RealAllocationSize + 16 - (RealAllocationSize % 16);
-}
-
-#ifdef __BIONIC__
-static constexpr AlignmentStrategy PlatformDefaultAlignment =
-    AlignmentStrategy::BIONIC;
-#else // __BIONIC__
-static constexpr AlignmentStrategy PlatformDefaultAlignment =
-    AlignmentStrategy::POWER_OF_TWO;
 #endif // __BIONIC__
-
-size_t rightAlignedAllocationSize(size_t RealAllocationSize,
-                                  AlignmentStrategy Align) {
-  assert(RealAllocationSize > 0);
-  if (Align == AlignmentStrategy::DEFAULT)
-    Align = PlatformDefaultAlignment;
-
-  switch (Align) {
-  case AlignmentStrategy::BIONIC:
-    return alignBionic(RealAllocationSize);
-  case AlignmentStrategy::POWER_OF_TWO:
-    return alignPowerOfTwo(RealAllocationSize);
-  case AlignmentStrategy::PERFECT:
-    return RealAllocationSize;
-  case AlignmentStrategy::DEFAULT:
-    __builtin_unreachable();
-  }
-  __builtin_unreachable();
 }
-
 } // namespace gwp_asan

diff  --git a/compiler-rt/lib/gwp_asan/utilities.cpp b/compiler-rt/lib/gwp_asan/utilities.cpp
new file mode 100644
index 000000000000..902e1450c491
--- /dev/null
+++ b/compiler-rt/lib/gwp_asan/utilities.cpp
@@ -0,0 +1,63 @@
+//===-- utilities.cpp -------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "gwp_asan/utilities.h"
+
+#include <assert.h>
+
+namespace gwp_asan {
+// See `bionic/tests/malloc_test.cpp` in the Android source for documentation
+// regarding their alignment guarantees. We always round up to the closest
+// 8-byte window. As GWP-ASan's malloc(X) can always get exactly an X-sized
+// allocation, an allocation that rounds up to 16-bytes will always be given a
+// 16-byte aligned allocation.
+static size_t alignBionic(size_t RealAllocationSize) {
+  if (RealAllocationSize % 8 == 0)
+    return RealAllocationSize;
+  return RealAllocationSize + 8 - (RealAllocationSize % 8);
+}
+
+static size_t alignPowerOfTwo(size_t RealAllocationSize) {
+  if (RealAllocationSize <= 2)
+    return RealAllocationSize;
+  if (RealAllocationSize <= 4)
+    return 4;
+  if (RealAllocationSize <= 8)
+    return 8;
+  if (RealAllocationSize % 16 == 0)
+    return RealAllocationSize;
+  return RealAllocationSize + 16 - (RealAllocationSize % 16);
+}
+
+#ifdef __BIONIC__
+static constexpr AlignmentStrategy PlatformDefaultAlignment =
+    AlignmentStrategy::BIONIC;
+#else // __BIONIC__
+static constexpr AlignmentStrategy PlatformDefaultAlignment =
+    AlignmentStrategy::POWER_OF_TWO;
+#endif // __BIONIC__
+
+size_t rightAlignedAllocationSize(size_t RealAllocationSize,
+                                  AlignmentStrategy Align) {
+  assert(RealAllocationSize > 0);
+  if (Align == AlignmentStrategy::DEFAULT)
+    Align = PlatformDefaultAlignment;
+
+  switch (Align) {
+  case AlignmentStrategy::BIONIC:
+    return alignBionic(RealAllocationSize);
+  case AlignmentStrategy::POWER_OF_TWO:
+    return alignPowerOfTwo(RealAllocationSize);
+  case AlignmentStrategy::PERFECT:
+    return RealAllocationSize;
+  case AlignmentStrategy::DEFAULT:
+    __builtin_unreachable();
+  }
+  __builtin_unreachable();
+}
+} // namespace gwp_asan

diff  --git a/compiler-rt/lib/gwp_asan/utilities.h b/compiler-rt/lib/gwp_asan/utilities.h
index 8aeefe188235..cee5672b491d 100644
--- a/compiler-rt/lib/gwp_asan/utilities.h
+++ b/compiler-rt/lib/gwp_asan/utilities.h
@@ -12,12 +12,17 @@
 #include "gwp_asan/definitions.h"
 
 #include <stddef.h>
-#include <stdint.h>
 
 namespace gwp_asan {
-// Checks that `Condition` is true, otherwise fails in a platform-specific way
-// with `Message`.
-void Check(bool Condition, const char *Message);
+// Terminates in a platform-specific way with `Message`.
+void die(const char *Message);
+
+// Checks that `Condition` is true, otherwise dies with `Message`.
+GWP_ASAN_ALWAYS_INLINE void Check(bool Condition, const char *Message) {
+  if (Condition)
+    return;
+  die(Message);
+}
 
 enum class AlignmentStrategy {
   // Default => POWER_OF_TWO on most platforms, BIONIC for Android Bionic.


        


More information about the llvm-commits mailing list