Skip to content

Conversation

4a6f656c
Copy link
Contributor

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.

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.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@4a6f656c
Copy link
Contributor Author

@mengzhuo @wangpc-pp

@llvmbot
Copy link
Member

llvmbot commented Aug 21, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Joel Sing (4a6f656c)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/154701.diff

3 Files Affected:

  • (modified) compiler-rt/lib/tsan/go/test.c (+7)
  • (modified) compiler-rt/lib/tsan/rtl/tsan_platform.h (+36-3)
  • (modified) compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp (+2-2)
diff --git a/compiler-rt/lib/tsan/go/test.c b/compiler-rt/lib/tsan/go/test.c
index 1b0d828c90447..d328ab1b331d7 100644
--- a/compiler-rt/lib/tsan/go/test.c
+++ b/compiler-rt/lib/tsan/go/test.c
@@ -63,6 +63,13 @@ int main(void) {
   __tsan_init(&thr0, &proc0, symbolize_cb);
   current_proc = proc0;
 
+#if defined(__riscv) && (__riscv_xlen == 64) && defined(__linux__)
+  // Use correct go_heap for riscv64 sv39.
+  if (65 - __builtin_clzl((unsigned long)__builtin_frame_address(0)) == 39) {
+    go_heap = (void *)0x511100000;
+  }
+#endif
+
   // Allocate something resembling a heap in Go.
   buf0 = mmap(go_heap, 16384, PROT_READ | PROT_WRITE,
               MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0);
diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform.h b/compiler-rt/lib/tsan/rtl/tsan_platform.h
index ada594bc11fc7..158ed58809a72 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform.h
@@ -681,6 +681,33 @@ struct MappingGoMips64_47 {
   static const uptr kShadowAdd = 0x200000000000ull;
 };
 
+/* Go on linux/riscv64 (39-bit VMA)
+0000 0000 0000 - 000a 0000 0000: executable and heap (40 GiB)
+000a 0000 0000 - 000c 0000 0000: -
+000c 0000 0000 - 0034 0000 0000: shadow - 160 GiB ( ~ 4 * app)
+0034 0000 0000 - 0035 0000 0000: -
+0035 0000 0000 - 0039 0000 0000: metainfo - 40 GiB ( ~ 1 * app)
+0039 0000 0000 - 0040 0000 0000: -
+*/
+struct MappingGoRiscv64_39 {
+  static const uptr kMetaShadowBeg = 0x003500000000ull;
+  static const uptr kMetaShadowEnd = 0x003900000000ull;
+  static const uptr kShadowBeg = 0x000c00000000ull;
+  static const uptr kShadowEnd = 0x003400000000ull;
+  static const uptr kLoAppMemBeg = 0x000000010000ull;
+  static const uptr kLoAppMemEnd = 0x000a00000000ull;
+  static const uptr kMidAppMemBeg = 0;
+  static const uptr kMidAppMemEnd = 0;
+  static const uptr kHiAppMemBeg = 0;
+  static const uptr kHiAppMemEnd = 0;
+  static const uptr kHeapMemBeg = 0;
+  static const uptr kHeapMemEnd = 0;
+  static const uptr kVdsoBeg = 0;
+  static const uptr kShadowMsk = 0;
+  static const uptr kShadowXor = 0;
+  static const uptr kShadowAdd = 0x000c00000000ull;
+};
+
 /* Go on linux/riscv64 (48-bit VMA)
 0000 0001 0000 - 00e0 0000 0000: executable and heap (896 GiB)
 00e0 0000 0000 - 2000 0000 0000: -
@@ -689,7 +716,7 @@ struct MappingGoMips64_47 {
 3000 0000 0000 - 3100 0000 0000: metainfo - 1 TiB ( ~ 1 * app)
 3100 0000 0000 - 8000 0000 0000: -
 */
-struct MappingGoRiscv64 {
+struct MappingGoRiscv64_48 {
   static const uptr kMetaShadowBeg = 0x300000000000ull;
   static const uptr kMetaShadowEnd = 0x310000000000ull;
   static const uptr kShadowBeg = 0x200000000000ull;
@@ -756,7 +783,12 @@ ALWAYS_INLINE auto SelectMapping(Arg arg) {
 #  elif defined(__loongarch_lp64)
   return Func::template Apply<MappingGoLoongArch64_47>(arg);
 #  elif SANITIZER_RISCV64
-  return Func::template Apply<MappingGoRiscv64>(arg);
+  switch (vmaSize) {
+    case 39:
+      return Func::template Apply<MappingGoRiscv64_39>(arg);
+    case 48:
+      return Func::template Apply<MappingGoRiscv64_48>(arg);
+   }
 #  elif SANITIZER_WINDOWS
   return Func::template Apply<MappingGoWindows>(arg);
 #  else
@@ -827,7 +859,8 @@ void ForEachMapping() {
   Func::template Apply<MappingGoAarch64>();
   Func::template Apply<MappingGoLoongArch64_47>();
   Func::template Apply<MappingGoMips64_47>();
-  Func::template Apply<MappingGoRiscv64>();
+  Func::template Apply<MappingGoRiscv64_39>();
+  Func::template Apply<MappingGoRiscv64_48>();
   Func::template Apply<MappingGoS390x>();
 }
 
diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
index 2c55645a15479..4b55aab49a2b9 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
@@ -393,9 +393,9 @@ void InitializePlatformEarly() {
     Die();
   }
 #    else
-  if (vmaSize != 48) {
+  if (vmaSize != 39 && vmaSize != 48) {
     Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
-    Printf("FATAL: Found %zd - Supported 48\n", vmaSize);
+    Printf("FATAL: Found %zd - Supported 39 and 48\n", vmaSize);
     Die();
   }
 #    endif

Copy link
Contributor

@thurstond thurstond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please enlarge the meta region

@@ -63,6 +63,13 @@ int main(void) {
__tsan_init(&thr0, &proc0, symbolize_cb);
current_proc = proc0;

#if defined(__riscv) && (__riscv_xlen == 64) && defined(__linux__)
// Use correct go_heap for riscv64 sv39.
if (65 - __builtin_clzl((unsigned long)__builtin_frame_address(0)) == 39) {
Copy link
Contributor

@fmayer fmayer Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we know how many bits are in __builtin_frame_address? Is this making assumptions on where the stack is placed by the kernel? Is this guaranteed?

Copy link
Contributor Author

@4a6f656c 4a6f656c Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we know how many bits are in __builtin_frame_address? Is this making assumptions on where the stack is placed by the kernel? Is this guaranteed?

At least on RISC-V, the user region is split such that if the stack is in the top half of user space it will have the top bit set. While I'm not sure that it is strictly guaranteed, it would be a rather unexpected situation.

This is the same approach used by LLVM TSAN:

I've just distilled it to a version that works without additional macros/indirection.

@4a6f656c
Copy link
Contributor Author

Please enlarge the meta region

Thanks for the detailed review - I've reworked the regions, increasing the exec/heap to 60GB and reducing the shadow to 128GB (with 32GB for the meta region). PTAL.

@4a6f656c 4a6f656c requested review from thurstond and fmayer August 23, 2025 14:56
Copy link

github-actions bot commented Aug 24, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@fmayer
Copy link
Contributor

fmayer commented Aug 24, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:

git-clang-format --diff HEAD~1 HEAD --extensions c,h,cpp -- compiler-rt/lib/tsan/go/test.c compiler-rt/lib/tsan/rtl/tsan_platform.h compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp

View the diff from clang-format here.

diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform.h b/compiler-rt/lib/tsan/rtl/tsan_platform.h
index 1057e9d97..6c14537d2 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform.h
@@ -787,7 +787,7 @@ ALWAYS_INLINE auto SelectMapping(Arg arg) {
       return Func::template Apply<MappingGoRiscv64_39>(arg);
     case 48:
       return Func::template Apply<MappingGoRiscv64_48>(arg);
-   }
+  }
 #  elif SANITIZER_WINDOWS
   return Func::template Apply<MappingGoWindows>(arg);
 #  else

Please fix

Copy link
Contributor

@thurstond thurstond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New mapping looks good to me. Thanks for the patch!

Please fix the clang-format, as @fmayer has noted.

@4a6f656c
Copy link
Contributor Author

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️
[snip]
Please fix

Done.

@fmayer fmayer merged commit 9958b1c into llvm:main Aug 25, 2025
9 checks passed
Copy link

@4a6f656c Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants