Implement tls scanning for darwin LSan
authorFrancis Ricci <francisjricci@gmail.com>
Thu, 25 May 2017 17:41:13 +0000 (17:41 +0000)
committerFrancis Ricci <francisjricci@gmail.com>
Thu, 25 May 2017 17:41:13 +0000 (17:41 +0000)
commit86e070f7e9b0c173269c3827665b8d8141165d2d
treee5b4407964aba71b7bb344e97c635ab6e7adb415
parent75ca300f2b6a8d32b924a52a7d5640af9e2d64c3
Implement tls scanning for darwin LSan

Summary:
This required for any users who call exit() after creating
thread-specific data, as tls destructors are only called when
pthread_exit() or pthread_cancel() are used. This should also
match tls behavior on linux.

Getting the base address of the tls section is straightforward,
as it's stored as a section offset in %gs. The size is a bit trickier
to work out, as there doesn't appear to be any official documentation
or source code referring to it. The size used in this patch was determined
by taking the difference between the base address and the address of the
subsequent memory region returned by vm_region_recurse_64, which was
1024 * sizeof(uptr) on all threads except the main thread, where it was
larger. Since the section must be the same size on all of the threads,
1024 * sizeof(uptr) seemed to be a reasonable size to use, barring
a more programtic way to get the size.

1024 seems like a reasonable number, given that PTHREAD_KEYS_MAX
is 512 on darwin, so pthread keys will fit inside the region while
leaving space for other tls data. A larger size would overflow the
memory region returned by vm_region_recurse_64, and a smaller size
wouldn't leave room for all the pthread keys. In addition, the
stress test added here passes, which means that we are scanning at
least the full set of possible pthread keys, and probably
the full tls section.

Reviewers: alekseyshl, kubamracek

Subscribers: krytarowski, llvm-commits

Differential Revision: https://reviews.llvm.org/D33215

llvm-svn: 303887
compiler-rt/lib/lsan/lsan_common.cc
compiler-rt/lib/lsan/lsan_common_mac.cc
compiler-rt/lib/lsan/lsan_flags.inc
compiler-rt/lib/sanitizer_common/sanitizer_mac.cc
compiler-rt/test/lsan/TestCases/many_tls_keys.cc [new file with mode: 0644]