Skip to content

Commit 9958b1c

Browse files
authored
[tsan][riscv] add Go race detector support for RISC-V sv39 VMA (#154701)
The majority of readily available RISC-V hardware provides sv39, rather than sv48. Add a memory mapping for sv39, which will allow the Go race detector to be used on more hardware.
1 parent 9801a0f commit 9958b1c

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

compiler-rt/lib/tsan/go/test.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ int main(void) {
6363
__tsan_init(&thr0, &proc0, symbolize_cb);
6464
current_proc = proc0;
6565

66+
#if defined(__riscv) && (__riscv_xlen == 64) && defined(__linux__)
67+
// Use correct go_heap for riscv64 sv39.
68+
if (65 - __builtin_clzl((unsigned long)__builtin_frame_address(0)) == 39) {
69+
go_heap = (void *)0x511100000;
70+
}
71+
#endif
72+
6673
// Allocate something resembling a heap in Go.
6774
buf0 = mmap(go_heap, 16384, PROT_READ | PROT_WRITE,
6875
MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0);

compiler-rt/lib/tsan/rtl/tsan_platform.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,32 @@ struct MappingGoMips64_47 {
681681
static const uptr kShadowAdd = 0x200000000000ull;
682682
};
683683

684+
/* Go on linux/riscv64 (39-bit VMA)
685+
0000 0001 0000 - 000f 0000 0000: executable and heap (60 GiB)
686+
000f 0000 0000 - 0010 0000 0000: -
687+
0010 0000 0000 - 0030 0000 0000: shadow - 128 GiB ( ~ 2 * app)
688+
0030 0000 0000 - 0038 0000 0000: metainfo - 32 GiB ( ~ 0.5 * app)
689+
0038 0000 0000 - 0040 0000 0000: -
690+
*/
691+
struct MappingGoRiscv64_39 {
692+
static const uptr kMetaShadowBeg = 0x003000000000ull;
693+
static const uptr kMetaShadowEnd = 0x003800000000ull;
694+
static const uptr kShadowBeg = 0x001000000000ull;
695+
static const uptr kShadowEnd = 0x003000000000ull;
696+
static const uptr kLoAppMemBeg = 0x000000010000ull;
697+
static const uptr kLoAppMemEnd = 0x000f00000000ull;
698+
static const uptr kMidAppMemBeg = 0;
699+
static const uptr kMidAppMemEnd = 0;
700+
static const uptr kHiAppMemBeg = 0;
701+
static const uptr kHiAppMemEnd = 0;
702+
static const uptr kHeapMemBeg = 0;
703+
static const uptr kHeapMemEnd = 0;
704+
static const uptr kVdsoBeg = 0;
705+
static const uptr kShadowMsk = 0;
706+
static const uptr kShadowXor = 0;
707+
static const uptr kShadowAdd = 0x001000000000ull;
708+
};
709+
684710
/* Go on linux/riscv64 (48-bit VMA)
685711
0000 0001 0000 - 00e0 0000 0000: executable and heap (896 GiB)
686712
00e0 0000 0000 - 2000 0000 0000: -
@@ -689,7 +715,7 @@ struct MappingGoMips64_47 {
689715
3000 0000 0000 - 3100 0000 0000: metainfo - 1 TiB ( ~ 1 * app)
690716
3100 0000 0000 - 8000 0000 0000: -
691717
*/
692-
struct MappingGoRiscv64 {
718+
struct MappingGoRiscv64_48 {
693719
static const uptr kMetaShadowBeg = 0x300000000000ull;
694720
static const uptr kMetaShadowEnd = 0x310000000000ull;
695721
static const uptr kShadowBeg = 0x200000000000ull;
@@ -756,7 +782,12 @@ ALWAYS_INLINE auto SelectMapping(Arg arg) {
756782
# elif defined(__loongarch_lp64)
757783
return Func::template Apply<MappingGoLoongArch64_47>(arg);
758784
# elif SANITIZER_RISCV64
759-
return Func::template Apply<MappingGoRiscv64>(arg);
785+
switch (vmaSize) {
786+
case 39:
787+
return Func::template Apply<MappingGoRiscv64_39>(arg);
788+
case 48:
789+
return Func::template Apply<MappingGoRiscv64_48>(arg);
790+
}
760791
# elif SANITIZER_WINDOWS
761792
return Func::template Apply<MappingGoWindows>(arg);
762793
# else
@@ -827,7 +858,8 @@ void ForEachMapping() {
827858
Func::template Apply<MappingGoAarch64>();
828859
Func::template Apply<MappingGoLoongArch64_47>();
829860
Func::template Apply<MappingGoMips64_47>();
830-
Func::template Apply<MappingGoRiscv64>();
861+
Func::template Apply<MappingGoRiscv64_39>();
862+
Func::template Apply<MappingGoRiscv64_48>();
831863
Func::template Apply<MappingGoS390x>();
832864
}
833865

compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ void InitializePlatformEarly() {
393393
Die();
394394
}
395395
# else
396-
if (vmaSize != 48) {
396+
if (vmaSize != 39 && vmaSize != 48) {
397397
Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
398-
Printf("FATAL: Found %zd - Supported 48\n", vmaSize);
398+
Printf("FATAL: Found %zd - Supported 39 and 48\n", vmaSize);
399399
Die();
400400
}
401401
# endif

0 commit comments

Comments
 (0)