Skip to content

Commit 1603127

Browse files
kito-chenghau-hsu
authored andcommitted
[RISCV][Asan] Use dynamic shadow offset to make it work on different width of virtual-memory system.
The original asan port was support Sv39 only, however Sv48 support has landed to linux kernel[1] for a while, so we are trying to make it support both Sv39 and Sv48, the key point is we need to use a dynamic offset for the shadow region, this bring extra runtime overhead, but compare to other overhead in ASan, this should be acceptable. [1] https://www.phoronix.com/news/Linux-5.17-RISC-V-sv48
1 parent 42a6d04 commit 1603127

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
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
@@ -1096,7 +1096,8 @@ uptr GetMaxVirtualAddress() {
10961096
# if SANITIZER_NETBSD && defined(__x86_64__)
10971097
return 0x7f7ffffff000ULL; // (0x00007f8000000000 - PAGE_SIZE)
10981098
# elif SANITIZER_WORDSIZE == 64
1099-
# if defined(__powerpc64__) || defined(__aarch64__) || defined(__loongarch__)
1099+
# if defined(__powerpc64__) || defined(__aarch64__) || \
1100+
defined(__loongarch__) || SANITIZER_RISCV64
11001101
// On PowerPC64 we have two different address space layouts: 44- and 46-bit.
11011102
// We somehow need to figure out which one we are using now and choose
11021103
// one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
@@ -1105,9 +1106,8 @@ uptr GetMaxVirtualAddress() {
11051106
// This should (does) work for both PowerPC64 Endian modes.
11061107
// Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
11071108
// loongarch64 also has multiple address space layouts: default is 47-bit.
1109+
// RISC-V 64 also has multiple address space layouts: 39, 48 and 57-bit.
11081110
return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1109-
# elif SANITIZER_RISCV64
1110-
return (1ULL << 38) - 1;
11111111
# elif SANITIZER_MIPS64
11121112
return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
11131113
# elif defined(__s390x__)

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

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

0 commit comments

Comments
 (0)