Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[sanitizer][Darwin] Define TlsSize on arm64 #133989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

speednoisemovement
Copy link
Contributor

No description provided.

Copy link

github-actions bot commented Apr 1, 2025

βœ… With the latest revision this PR passed the C/C++ code formatter.

@vitalybuka vitalybuka requested review from delcypher and yln April 24, 2025 04:16
@llvmbot
Copy link
Member

llvmbot commented Apr 24, 2025

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

Author: Leonard Grey (speednoisemovement)

Changes

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

1 Files Affected:

  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp (+6-10)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index 0b8a75391136d..70d8cddc6e573 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -558,16 +558,12 @@ uptr TlsBaseAddr() {
   return segbase;
 }
 
-// The size of the tls on darwin does not appear to be well documented,
-// however the vm memory map suggests that it is 1024 uptrs in size,
-// with a size of 0x2000 bytes on x86_64 and 0x1000 bytes on i386.
-uptr TlsSize() {
-#if defined(__x86_64__) || defined(__i386__)
-  return 1024 * sizeof(uptr);
-#else
-  return 0;
-#endif
-}
+// The size of the tls on darwin does not appear to be well documented.
+// but `pthread_s`'s `tsd` member (see libpthread/src/types_internal.h) is
+// defined as `_INTERNAL_POSIX_THREAD_KEYS_MAX +
+// `_INTERNAL_POSIX_THREAD_KEYS_END` (512 pointers on iPhone and 768 elsewhere).
+// Keep at 1024 for backwards compatibility.
+uptr TlsSize() { return 1024 * sizeof(uptr); }
 
 void GetThreadStackAndTls(bool main, uptr *stk_begin, uptr *stk_end,
                           uptr *tls_begin, uptr *tls_end) {

@delcypher
Copy link
Contributor

@speednoisemovement I’m pinging my colleagues to see if they have any concerns about this.

// defined as `_INTERNAL_POSIX_THREAD_KEYS_MAX +
// `_INTERNAL_POSIX_THREAD_KEYS_END` (512 pointers on iPhone and 768 elsewhere).
// Keep at 1024 for backwards compatibility.
uptr TlsSize() { return 1024 * sizeof(uptr); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
#define _EXTERNAL_POSIX_THREAD_KEYS_MAX 256
#define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
#define _INTERNAL_POSIX_THREAD_KEYS_END 512
#else
#define _EXTERNAL_POSIX_THREAD_KEYS_MAX 512
#define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
#define _INTERNAL_POSIX_THREAD_KEYS_END 768
#endif

void *tsd[_EXTERNAL_POSIX_THREAD_KEYS_MAX + _INTERNAL_POSIX_THREAD_KEYS_MAX];

https://github.com/apple-oss-distributions/libpthread/blob/libpthread-535/src/types_internal.h#L418

512 pointers on iPhone and 768 elsewhere

πŸ‘

Currently we are returning 1024 on macOS/Intel or 0 elsewhere, both wrong! :/

@speednoisemovement, can you comment on the "why?", which problem is this solving?

TlsSize() is used only in GetThreadStackAndTls(). What is the nature of this function? Is the conservative answer to return a lower ("only touch this much") or upper ("at least poison this much") bound? Depending on that we should return 512 or 768.

We could also sidestep this question and return the exact answer. Not too much worse since we are already hardcoding a value derived from an internal header in any case.

// Derived from:
// https://github.com/apple-oss-distributions/libpthread/blob/libpthread-535/src/types_internal.h#L418
uptr TlsSize() {
#if SANITIZER_IOS && !SANITIZER_IOSSIM
  return 512 * sizeof(uptr);
#else
  return 768 * sizeof(uptr);
#endif
}

⬆️ Voting for this if others don't have concerns.

@wrotki
Copy link
Contributor

wrotki commented Apr 29, 2025

This change has some consequences, it will cause a call to DontNeedShadowFor(thr->tls_addr, thr->tls_size) (than_rtl.cpp:222) to happen where it used to not happen, on arm64 . Perhaps those are harmless, but - why do you need this change to happen, is it a part of something bigger?

Also, sanitizer_mac.cpp:244 :

uptr GetTlsSize() {
return 0;
}

, which seems to return same thing and will be inconsistent with TlsSize(), perhaps it'll need some refactoring to decrease confusion potential.

@speednoisemovement
Copy link
Contributor Author

Thanks for reviewing!

The motivation is to bring some parity for LSAN since anything that gets stashed in TSD is a false positive on arm64 right now.

I'll look into DontNeedShadowFor/GetTls and get back to you.

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.

6 participants