[compiler-rt] [sanitizer][Darwin] Define TlsSize on arm64 (PR #133989)

Julian Lettner via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 29 10:13:42 PDT 2025


================
@@ -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); }
----------------
yln wrote:

```
#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.

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


More information about the llvm-commits mailing list