This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt/profile] register __llvm_profile_write_file to run during quick_exit.
AbandonedPublic

Authored by pirama on May 19 2021, 11:15 AM.

Details

Reviewers
phosek
davidxl
Summary

This change registers __llvm_profile_write_file with at_quick_exit
when it's available.

Some processes don't exit normally (via return from main or
exit()) to avoid the cost of calling destructors of static globals.
They call quick_exit or _exit instead. Registering with
at_quick_exit allows coverage data to be written when quick_exit is
called.

Diff Detail

Event Timeline

pirama created this revision.May 19 2021, 11:15 AM
pirama requested review of this revision.May 19 2021, 11:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 19 2021, 11:15 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
davidxl added inline comments.May 19 2021, 2:23 PM
compiler-rt/test/profile/instrprof-write-file-at-quick-exit-explicitly.c
13

Is this explicit call needed?

pirama added inline comments.May 19 2021, 2:55 PM
compiler-rt/test/profile/instrprof-write-file-at-quick-exit-explicitly.c
13

I based this test on compiler-rt/test/profile/instrprof-write-file-atexit-explicitly.c, which tests this for atexit. That test also has a similar structure.

We cannot set LLVM_PROFILE_FILE in this test (like RUN: %run LLVM_PROFILE_FILE=%t.profraw %t %t.profraw) because it may not work on all platforms. Instead, we want to set the file name via __llvm_profile_set_filename. But that call still creates an empty default.profraw when the runtime gets initialized. So we set __llvm_profile_runtime = 0 to skip initializing the runtime but explicitly have a call __llvm_profile_register_write_file_atexit.

davidxl accepted this revision.May 19 2021, 2:56 PM

lgtm

This revision is now accepted and ready to land.May 19 2021, 2:56 PM
pirama abandoned this revision.May 21 2021, 10:43 AM
pirama added a subscriber: enh.

Looks like quick_exit + dlclose interaction is not well-defined and this change may cause calls into dlclose-d libraries.

As @enh pointed out in an internal thread about An at_quick_exit() registered function is not called when the object defining the function is unloaded. Rather, it is simply removed from the at_quick_exit() registration list.:

it seems like glibc does this. no-one else (the BSDs, Android [which just reuses a BSD implementation], iOS/macOS [which haven't even implemented quick_exit yet], or musl [which has a trivial implementation like ours, but with more locking]) seems to do so.

"POSIX hasn't said anything about this yet, and the C standard doesn't have dlopen()/dlclose(), so C11 doesn't say anything"

I'm going to abandon this incarnation of the patch. Once Android has a well-defined behavior of this interaction, we can call at_quick_exit here under a #ifdef.

enh added a comment.May 21 2021, 11:48 AM

I'm going to abandon this incarnation of the patch. Once Android has a well-defined behavior of this interaction, we can call at_quick_exit here under a #ifdef.

yeah, it should probably check the API level too, since it's no longer just whether the target _has_ at_quick_exit(), it's whether it's one that understands dlclose()...