This is an archive of the discontinued LLVM Phabricator instance.

[builtins] Add __emutls_unregister_key function
ClosedPublic

Authored by kongyi on Sep 18 2018, 3:22 PM.

Details

Summary

This is called by Bionic on dlclose to delete the emutls pthread key.

The return value of pthread_key_delete is unchecked and behaviour of multiple calls to the method is dependent on the implementation of pthread_key_delete.

Diff Detail

Repository
rL LLVM

Event Timeline

kongyi created this revision.Sep 18 2018, 3:22 PM
Herald added subscribers: Restricted Project, llvm-commits, jfb. · View Herald TranscriptSep 18 2018, 3:22 PM
chh added inline comments.Sep 18 2018, 4:22 PM
lib/builtins/emutls.c
394 ↗(On Diff #166043)

I think this function should be useful to other non-bionic platform too.
Did you get any link error with compiler-rt tests?

397 ↗(On Diff #166043)

Shouldn't this be called only after the pthread key was created?
gcc's version has a flag emutls_key_created set only after emutls_init.

kongyi updated this revision to Diff 166188.Sep 19 2018, 3:00 PM
kongyi marked 2 inline comments as done.
kongyi added inline comments.
lib/builtins/emutls.c
394 ↗(On Diff #166043)

No link error.

Other platforms don't need this for dlclose; it is Bionic specific.

397 ↗(On Diff #166043)

We don't need to check this, since emutls_key_delete is not expensive to call and we can safely ignore failure from it.

srhines accepted this revision.Sep 19 2018, 3:09 PM
srhines added subscribers: enh, danalbert.

+enh and danalbert - in case they have any other concerns about this (Ryan's already on the review). From my perspective, everything here makes sense for resolving this issue.

This revision is now accepted and ready to land.Sep 19 2018, 3:10 PM
rprichard added inline comments.Sep 19 2018, 3:38 PM
lib/builtins/emutls.c
397 ↗(On Diff #166043)

This code assumes that a zeroed pthread_key_t is never valid. I suspect that's true for Bionic? I confirmed it on aosp-master and KitKat. For the NDK's purposes, it needs to be true for android-16 and up.

It's apparently not generally true? http://www.tekkotsu.org/cvslog2web/2009/03/commit-1236808823.html

yeah, we always used the low numbers internally.

chh added inline comments.Sep 19 2018, 4:18 PM
lib/builtins/emutls.c
397 ↗(On Diff #166043)

My concern is run time error or crash in calls to pthread_key_delete(0).
That might not happen with bionic, but i think the behavior is undefined in POSIX and could happen in other platforms.
I know some other platforms already depend on this emutls.c.
Maybe they have not seen the dlclose issue yet, but it would be nice to make this function work for general cases.

kongyi updated this revision to Diff 166195.Sep 19 2018, 4:37 PM
kongyi marked an inline comment as done.
kongyi added inline comments.
lib/builtins/emutls.c
397 ↗(On Diff #166043)

Changed to only call pthread_key_delete if key is created.

rprichard accepted this revision.Sep 19 2018, 4:41 PM
This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.
kongyi marked an inline comment as done.