Skip to content

Commit 0c3bc1f

Browse files
committed
[ASan][RISCV] Fix RISC-V memory mapping
Fixes the ASan RISC-V memory mapping (originally introduced by D87580 and D87581). This should be an improvement both in terms of first principles soundness and observed test failures --- test failures would occur non-deterministically depending on the ASLR random offset. On RISC-V Linux (64-bit), `TASK_UNMAPPED_BASE` is currently defined as `PAGE_ALIGN(TASK_SIZE / 3)`. The non-power-of-two divisor makes the result be the not very round number 0x1555556000. That address had to be further rounded to ensure page alignment after the shadow scale shifting is applied. Still, that value explains why the mapping table may look less regular than expected. Further cleanups: - Moved the mapping table comment, to ensure that the two Linux/AArch64 tables stayed together; - Removed mention of Sv48. Neither the original mapping nor this one are compatible with an actual Linux Sv48 address space (mainline Linux still operates Sv48 in Sv39 mode). A future patch can improve this; - Removed the additional comments, for consistency. Differential Revision: https://reviews.llvm.org/D97646
1 parent 61d065e commit 0c3bc1f

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

compiler-rt/lib/asan/asan_mapping.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,20 @@
7272
// || `[0x2000000000, 0x23ffffffff]` || LowShadow ||
7373
// || `[0x0000000000, 0x1fffffffff]` || LowMem ||
7474
//
75+
// Default Linux/RISCV64 Sv39 mapping:
76+
// || `[0x1555550000, 0x3fffffffff]` || HighMem ||
77+
// || `[0x0fffffa000, 0x1555555fff]` || HighShadow ||
78+
// || `[0x0effffa000, 0x0fffff9fff]` || ShadowGap ||
79+
// || `[0x0d55550000, 0x0effff9fff]` || LowShadow ||
80+
// || `[0x0000000000, 0x0d5554ffff]` || LowMem ||
81+
//
7582
// Default Linux/AArch64 (39-bit VMA) mapping:
7683
// || `[0x2000000000, 0x7fffffffff]` || highmem ||
7784
// || `[0x1400000000, 0x1fffffffff]` || highshadow ||
7885
// || `[0x1200000000, 0x13ffffffff]` || shadowgap ||
7986
// || `[0x1000000000, 0x11ffffffff]` || lowshadow ||
8087
// || `[0x0000000000, 0x0fffffffff]` || lowmem ||
8188
//
82-
// RISC-V has only 38 bits for task size
83-
// Low mem size is set with kRiscv64_ShadowOffset64 in
84-
// compiler-rt/lib/asan/asan_allocator.h and in
85-
// llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp with
86-
// kRiscv64_ShadowOffset64, High mem top border is set with
87-
// GetMaxVirtualAddress() in
88-
// compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
89-
// Default Linux/RISCV64 Sv39/Sv48 mapping:
90-
// || `[0x000820000000, 0x003fffffffff]` || HighMem ||
91-
// || `[0x000124000000, 0x00081fffffff]` || HighShadow ||
92-
// || `[0x000024000000, 0x000123ffffff]` || ShadowGap ||
93-
// || `[0x000020000000, 0x000023ffffff]` || LowShadow ||
94-
// || `[0x000000000000, 0x00001fffffff]` || LowMem ||
95-
//
9689
// Default Linux/AArch64 (42-bit VMA) mapping:
9790
// || `[0x10000000000, 0x3ffffffffff]` || highmem ||
9891
// || `[0x0a000000000, 0x0ffffffffff]` || highshadow ||
@@ -175,7 +168,7 @@ static const u64 kDefaultShadowOffset64 = 1ULL << 44;
175168
static const u64 kDefaultShort64bitShadowOffset =
176169
0x7FFFFFFF & (~0xFFFULL << kDefaultShadowScale); // < 2G.
177170
static const u64 kAArch64_ShadowOffset64 = 1ULL << 36;
178-
static const u64 kRiscv64_ShadowOffset64 = 0x20000000;
171+
static const u64 kRiscv64_ShadowOffset64 = 0xd55550000;
179172
static const u64 kMIPS32_ShadowOffset32 = 0x0aaa0000;
180173
static const u64 kMIPS64_ShadowOffset64 = 1ULL << 37;
181174
static const u64 kPPC64_ShadowOffset64 = 1ULL << 44;

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static const uint64_t kSystemZ_ShadowOffset64 = 1ULL << 52;
105105
static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000;
106106
static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37;
107107
static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36;
108-
static const uint64_t kRISCV64_ShadowOffset64 = 0x20000000;
108+
static const uint64_t kRISCV64_ShadowOffset64 = 0xd55550000;
109109
static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
110110
static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
111111
static const uint64_t kNetBSD_ShadowOffset32 = 1ULL << 30;

0 commit comments

Comments
 (0)