Skip to content

Commit be8e4de

Browse files
author
Kostya Kortchinsky
committed
[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
1 parent 1bc7bff commit be8e4de

File tree

4 files changed

+74
-63
lines changed

4 files changed

+74
-63
lines changed

compiler-rt/lib/gwp_asan/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(GWP_ASAN_SOURCES
1212
guarded_pool_allocator.cpp
1313
random.cpp
1414
stack_trace_compressor.cpp
15+
utilities.cpp
1516
)
1617

1718
set(GWP_ASAN_HEADERS

compiler-rt/lib/gwp_asan/platform_specific/utilities_posix.cpp

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,72 +19,14 @@ extern "C" GWP_ASAN_WEAK void android_set_abort_message(const char *);
1919
#endif
2020

2121
namespace gwp_asan {
22-
22+
void die(const char *Message) {
2323
#ifdef __BIONIC__
24-
void Check(bool Condition, const char *Message) {
25-
if (Condition)
26-
return;
2724
if (&android_set_abort_message != nullptr)
2825
android_set_abort_message(Message);
2926
abort();
30-
}
3127
#else // __BIONIC__
32-
void Check(bool Condition, const char *Message) {
33-
if (Condition)
34-
return;
3528
fprintf(stderr, "%s", Message);
3629
__builtin_trap();
37-
}
38-
#endif // __BIONIC__
39-
40-
// See `bionic/tests/malloc_test.cpp` in the Android source for documentation
41-
// regarding their alignment guarantees. We always round up to the closest
42-
// 8-byte window. As GWP-ASan's malloc(X) can always get exactly an X-sized
43-
// allocation, an allocation that rounds up to 16-bytes will always be given a
44-
// 16-byte aligned allocation.
45-
static size_t alignBionic(size_t RealAllocationSize) {
46-
if (RealAllocationSize % 8 == 0)
47-
return RealAllocationSize;
48-
return RealAllocationSize + 8 - (RealAllocationSize % 8);
49-
}
50-
51-
static size_t alignPowerOfTwo(size_t RealAllocationSize) {
52-
if (RealAllocationSize <= 2)
53-
return RealAllocationSize;
54-
if (RealAllocationSize <= 4)
55-
return 4;
56-
if (RealAllocationSize <= 8)
57-
return 8;
58-
if (RealAllocationSize % 16 == 0)
59-
return RealAllocationSize;
60-
return RealAllocationSize + 16 - (RealAllocationSize % 16);
61-
}
62-
63-
#ifdef __BIONIC__
64-
static constexpr AlignmentStrategy PlatformDefaultAlignment =
65-
AlignmentStrategy::BIONIC;
66-
#else // __BIONIC__
67-
static constexpr AlignmentStrategy PlatformDefaultAlignment =
68-
AlignmentStrategy::POWER_OF_TWO;
6930
#endif // __BIONIC__
70-
71-
size_t rightAlignedAllocationSize(size_t RealAllocationSize,
72-
AlignmentStrategy Align) {
73-
assert(RealAllocationSize > 0);
74-
if (Align == AlignmentStrategy::DEFAULT)
75-
Align = PlatformDefaultAlignment;
76-
77-
switch (Align) {
78-
case AlignmentStrategy::BIONIC:
79-
return alignBionic(RealAllocationSize);
80-
case AlignmentStrategy::POWER_OF_TWO:
81-
return alignPowerOfTwo(RealAllocationSize);
82-
case AlignmentStrategy::PERFECT:
83-
return RealAllocationSize;
84-
case AlignmentStrategy::DEFAULT:
85-
__builtin_unreachable();
86-
}
87-
__builtin_unreachable();
8831
}
89-
9032
} // namespace gwp_asan
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===-- utilities.cpp -------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "gwp_asan/utilities.h"
10+
11+
#include <assert.h>
12+
13+
namespace gwp_asan {
14+
// See `bionic/tests/malloc_test.cpp` in the Android source for documentation
15+
// regarding their alignment guarantees. We always round up to the closest
16+
// 8-byte window. As GWP-ASan's malloc(X) can always get exactly an X-sized
17+
// allocation, an allocation that rounds up to 16-bytes will always be given a
18+
// 16-byte aligned allocation.
19+
static size_t alignBionic(size_t RealAllocationSize) {
20+
if (RealAllocationSize % 8 == 0)
21+
return RealAllocationSize;
22+
return RealAllocationSize + 8 - (RealAllocationSize % 8);
23+
}
24+
25+
static size_t alignPowerOfTwo(size_t RealAllocationSize) {
26+
if (RealAllocationSize <= 2)
27+
return RealAllocationSize;
28+
if (RealAllocationSize <= 4)
29+
return 4;
30+
if (RealAllocationSize <= 8)
31+
return 8;
32+
if (RealAllocationSize % 16 == 0)
33+
return RealAllocationSize;
34+
return RealAllocationSize + 16 - (RealAllocationSize % 16);
35+
}
36+
37+
#ifdef __BIONIC__
38+
static constexpr AlignmentStrategy PlatformDefaultAlignment =
39+
AlignmentStrategy::BIONIC;
40+
#else // __BIONIC__
41+
static constexpr AlignmentStrategy PlatformDefaultAlignment =
42+
AlignmentStrategy::POWER_OF_TWO;
43+
#endif // __BIONIC__
44+
45+
size_t rightAlignedAllocationSize(size_t RealAllocationSize,
46+
AlignmentStrategy Align) {
47+
assert(RealAllocationSize > 0);
48+
if (Align == AlignmentStrategy::DEFAULT)
49+
Align = PlatformDefaultAlignment;
50+
51+
switch (Align) {
52+
case AlignmentStrategy::BIONIC:
53+
return alignBionic(RealAllocationSize);
54+
case AlignmentStrategy::POWER_OF_TWO:
55+
return alignPowerOfTwo(RealAllocationSize);
56+
case AlignmentStrategy::PERFECT:
57+
return RealAllocationSize;
58+
case AlignmentStrategy::DEFAULT:
59+
__builtin_unreachable();
60+
}
61+
__builtin_unreachable();
62+
}
63+
} // namespace gwp_asan

compiler-rt/lib/gwp_asan/utilities.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
#include "gwp_asan/definitions.h"
1313

1414
#include <stddef.h>
15-
#include <stdint.h>
1615

1716
namespace gwp_asan {
18-
// Checks that `Condition` is true, otherwise fails in a platform-specific way
19-
// with `Message`.
20-
void Check(bool Condition, const char *Message);
17+
// Terminates in a platform-specific way with `Message`.
18+
void die(const char *Message);
19+
20+
// Checks that `Condition` is true, otherwise dies with `Message`.
21+
GWP_ASAN_ALWAYS_INLINE void Check(bool Condition, const char *Message) {
22+
if (Condition)
23+
return;
24+
die(Message);
25+
}
2126

2227
enum class AlignmentStrategy {
2328
// Default => POWER_OF_TWO on most platforms, BIONIC for Android Bionic.

0 commit comments

Comments
 (0)