Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
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.


void GetThreadStackAndTls(bool main, uptr *stk_begin, uptr *stk_end,
uptr *tls_begin, uptr *tls_end) {
Expand Down
Loading