This is an archive of the discontinued LLVM Phabricator instance.

Implement tls scanning for darwin LSan
ClosedPublic

Authored by fjricci on May 15 2017, 2:52 PM.

Details

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.

Diff Detail

Repository
rL LLVM

Event Timeline

fjricci created this revision.May 15 2017, 2:52 PM
alekseyshl accepted this revision.May 16 2017, 1:06 PM
alekseyshl added inline comments.
test/lsan/TestCases/many_tls_keys.cc
82 ↗(On Diff #99067)

Check the result

This revision is now accepted and ready to land.May 16 2017, 1:06 PM
fjricci updated this revision to Diff 99195.May 16 2017, 2:10 PM

Check pthread_create result

This revision was automatically updated to reflect the committed changes.