Skip to content

Commit da0c8b2

Browse files
hau-hsukito-cheng
andauthored
[RISCV][sanitizer] Fix sanitizer support for different virtual memory layout (llvm#66743)
This PR combines the following reviews from Phabricator: * https://reviews.llvm.org/D139823 * https://reviews.llvm.org/D139827 Other related (and merged) reviews are: * https://reviews.llvm.org/D152895 * https://reviews.llvm.org/D152991 * https://reviews.llvm.org/D152990 --------- Co-authored-by: Kito Cheng <[email protected]>
1 parent c68c289 commit da0c8b2

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

compiler-rt/lib/asan/asan_mapping.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272
// || `[0x2000000000, 0x23ffffffff]` || LowShadow ||
7373
// || `[0x0000000000, 0x1fffffffff]` || LowMem ||
7474
//
75-
// Default Linux/RISCV64 Sv39 mapping:
75+
// Default Linux/RISCV64 Sv39 mapping with SHADOW_OFFSET == 0xd55550000;
76+
// (the exact location of SHADOW_OFFSET may vary depending the dynamic probing
77+
// by FindDynamicShadowStart).
78+
//
7679
// || `[0x1555550000, 0x3fffffffff]` || HighMem ||
7780
// || `[0x0fffffa000, 0x1555555fff]` || HighShadow ||
7881
// || `[0x0effffa000, 0x0fffff9fff]` || ShadowGap ||
@@ -186,7 +189,7 @@
186189
# elif SANITIZER_FREEBSD && defined(__aarch64__)
187190
# define ASAN_SHADOW_OFFSET_CONST 0x0000800000000000
188191
# elif SANITIZER_RISCV64
189-
# define ASAN_SHADOW_OFFSET_CONST 0x0000000d55550000
192+
# define ASAN_SHADOW_OFFSET_DYNAMIC
190193
# elif defined(__aarch64__)
191194
# define ASAN_SHADOW_OFFSET_CONST 0x0000001000000000
192195
# elif defined(__powerpc64__)

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,8 @@ uptr GetMaxVirtualAddress() {
11091109
# if SANITIZER_NETBSD && defined(__x86_64__)
11101110
return 0x7f7ffffff000ULL; // (0x00007f8000000000 - PAGE_SIZE)
11111111
# elif SANITIZER_WORDSIZE == 64
1112-
# if defined(__powerpc64__) || defined(__aarch64__) || defined(__loongarch__)
1112+
# if defined(__powerpc64__) || defined(__aarch64__) || \
1113+
defined(__loongarch__) || SANITIZER_RISCV64
11131114
// On PowerPC64 we have two different address space layouts: 44- and 46-bit.
11141115
// We somehow need to figure out which one we are using now and choose
11151116
// one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
@@ -1118,9 +1119,8 @@ uptr GetMaxVirtualAddress() {
11181119
// This should (does) work for both PowerPC64 Endian modes.
11191120
// Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
11201121
// loongarch64 also has multiple address space layouts: default is 47-bit.
1122+
// RISC-V 64 also has multiple address space layouts: 39, 48 and 57-bit.
11211123
return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1122-
# elif SANITIZER_RISCV64
1123-
return (1ULL << 38) - 1;
11241124
# elif SANITIZER_MIPS64
11251125
return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
11261126
# elif defined(__s390x__)

compiler-rt/lib/sanitizer_common/sanitizer_platform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@
295295
// For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or
296296
// change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here.
297297
#ifndef SANITIZER_CAN_USE_ALLOCATOR64
298-
# if (SANITIZER_RISCV64 && !SANITIZER_FUCHSIA) || SANITIZER_IOS || \
299-
SANITIZER_DRIVERKIT
298+
# if (SANITIZER_RISCV64 && !SANITIZER_FUCHSIA && !SANITIZER_LINUX) || \
299+
SANITIZER_IOS || SANITIZER_DRIVERKIT
300300
# define SANITIZER_CAN_USE_ALLOCATOR64 0
301301
# elif defined(__mips64) || defined(__hexagon__)
302302
# define SANITIZER_CAN_USE_ALLOCATOR64 0
@@ -322,7 +322,7 @@
322322
# if SANITIZER_FUCHSIA
323323
# define SANITIZER_MMAP_RANGE_SIZE (1ULL << 38)
324324
# else
325-
# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
325+
# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 56)
326326
# endif
327327
#elif defined(__aarch64__)
328328
# if SANITIZER_APPLE

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000;
108108
static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37;
109109
static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36;
110110
static const uint64_t kLoongArch64_ShadowOffset64 = 1ULL << 46;
111-
static const uint64_t kRISCV64_ShadowOffset64 = 0xd55550000;
111+
static const uint64_t kRISCV64_ShadowOffset64 = kDynamicShadowSentinel;
112112
static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
113113
static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
114114
static const uint64_t kFreeBSDAArch64_ShadowOffset64 = 1ULL << 47;

0 commit comments

Comments
 (0)