This is an archive of the discontinued LLVM Phabricator instance.

Don't call exit() from atexit handlers on Darwin
ClosedPublic

Authored by fjricci on Jul 17 2017, 3:09 PM.

Details

Summary

Calling exit() from an atexit handler is undefined behavior.
On Linux, it's unavoidable, since we cannot intercept exit (_exit isn't called
if a user program uses return instead of exit()), and I haven't
seen it cause issues regardless.

However, on Darwin, I have a fairly complex internal test that hangs roughly
once in every 300 runs after leak reporting finishes, which is resolved with
this patch, and is presumably due to the undefined behavior (since the Die() is
the only thing that happens after the end of leak reporting).

In addition, this is the way TSan works as well, where an atexit handler+Die()
is used on Linux, and an _exit() interceptor is used on Darwin. I'm not sure if it's
intentionally structured that way in TSan, since TSan sets up the atexit handler and the
_exit() interceptor on both platforms, but I have observed that on Darwin, only the
_exit() interceptor is used, and on Linux the atexit handler is used.

There is some additional related discussion here: https://reviews.llvm.org/D35085

Diff Detail

Repository
rL LLVM

Event Timeline

fjricci created this revision.Jul 17 2017, 3:09 PM
vsk added a comment.Jul 17 2017, 3:13 PM

I haven't reviewed an lsan patch before, so I can't say whether there's anything missing (e.g I'm not sure how lsan is tested), but the approach and rationale here sgtm.

alekseyshl added inline comments.Jul 17 2017, 6:13 PM
lib/lsan/lsan_common.cc
579 ↗(On Diff #106964)

has_reported_leaks?

589 ↗(On Diff #106964)

Why not SetHasReportedLeaks() here, for all platforms? Linux does not care. In this case we can remove SetHasReportedLeaks() altogether and the logic is a bit easier to reason about.

lib/lsan/lsan_common.h
232 ↗(On Diff #106964)

HandleLeaks(), maybe?

Please separate it from other two and add a comment to it.

fjricci updated this revision to Diff 107106.Jul 18 2017, 8:16 AM

Address comments

alekseyshl accepted this revision.Jul 18 2017, 12:47 PM
This revision is now accepted and ready to land.Jul 18 2017, 12:47 PM
This revision was automatically updated to reflect the committed changes.