Skip to content

Ensure static TLS region is free'd last #15086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2021
Merged

Ensure static TLS region is free'd last #15086

merged 1 commit into from
Sep 22, 2021

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Sep 21, 2021

We were using __cxa_thread_atexit to register the funtion for free'ing
TLS data. And because were were running the exit handlers
__pthread_exit_run_handlers before __pthread_tsd_run_dtors it meant
that any pthread_key descructor functions would not be able to access
TLS variables.

Instead we really need to delay free'ing of TLS data until all exit
handlers and pthread_key descructors have been run.

This was causing failure of lsan.test_stdio_locking due to the fact
that lsan uses TLS during pthread key destruction. Specifically
thread_finalize (a pthread_key desctructor function) calls
ThreadFinish which in turn calls GetCurrentThread which uses a
thread local variable to store the current thread ID.

This change should allow #14489 to be dramatically simplified because
it will allow the use of TLS variables to implement the atexit queue.

@sbc100 sbc100 requested review from kripken and kleisauke September 21, 2021 21:49
@sbc100 sbc100 requested a review from tlively September 21, 2021 22:11
sbc100 added a commit that referenced this pull request Sep 22, 2021
This is needed by a followup PR (#15086) which improves our LSan
support.  LSan wants to be able to call pthread_key_create during its
own startup and doesn't expect allocation to occur during this time:

https://github.com/emscripten-core/emscripten/blob/6b53dedf3d2ace15bfd780e44f4a8ba34407fc27/system/lib/compiler-rt/lib/lsan/lsan.cpp#L113

This method of using a static array of keys is also the one used
by musl so this seems like a reasonable implementation:

https://github.com/emscripten-core/emscripten/blob/6b53dedf3d2ace15bfd780e44f4a8ba34407fc27/system/lib/libc/musl/src/thread/pthread_key_create.c#L8
@sbc100 sbc100 changed the base branch from main to stub_tls_refactor September 22, 2021 00:29
Copy link
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sbc100
Copy link
Collaborator Author

sbc100 commented Sep 22, 2021

This change depends on #15087.. any chance you could take a look at that too?

sbc100 added a commit that referenced this pull request Sep 22, 2021
This is needed by a followup PR (#15086) which improves our LSan
support.  LSan wants to be able to call pthread_key_create during its
own startup and doesn't expect allocation to occur during this time:

https://github.com/emscripten-core/emscripten/blob/6b53dedf3d2ace15bfd780e44f4a8ba34407fc27/system/lib/compiler-rt/lib/lsan/lsan.cpp#L113

This method of using a static array of keys is also the one used
by musl so this seems like a reasonable implementation:

https://github.com/emscripten-core/emscripten/blob/6b53dedf3d2ace15bfd780e44f4a8ba34407fc27/system/lib/libc/musl/src/thread/pthread_key_create.c#L8
Base automatically changed from stub_tls_refactor to main September 22, 2021 04:01
sbc100 added a commit that referenced this pull request Sep 22, 2021
…15087)

This is needed by a followup PR (#15086) which improves our LSan
support.  LSan wants to be able to call pthread_key_create during its
own startup and doesn't expect allocation to occur during this time:

https://github.com/emscripten-core/emscripten/blob/6b53dedf3d2ace15bfd780e44f4a8ba34407fc27/system/lib/compiler-rt/lib/lsan/lsan.cpp#L113

This method of using a static array of keys is also the one used
by musl so this seems like a reasonable implementation:

https://github.com/emscripten-core/emscripten/blob/6b53dedf3d2ace15bfd780e44f4a8ba34407fc27/system/lib/libc/musl/src/thread/pthread_key_create.c#L8
We were using `__cxa_thread_atexit` to register the funtion for free'ing
TLS data.  And because were were running the exit handlers
`__pthread_exit_run_handlers` before `__pthread_tsd_run_dtors` it meant
that any pthread_key descructor functions would not be able to access
TLS variables.

Instead we really need to delay free'ing of TLS data until all exit
handlers and pthread_key descructors have been run.

This was causing failure of `lsan.test_stdio_locking` due to the fact
that lsan uses TLS during pthread key destruction.  Specifically
`thread_finalize` (a pthread_key desctructor function) calls
`ThreadFinish` which in turn calls `GetCurrentThread` which uses a
thread local variable to store the current thread ID.

This change should allow #14489 to be dramatically simplified because
it will allow the use of TLS variables to implement the atexit queue.
@sbc100 sbc100 enabled auto-merge (squash) September 22, 2021 04:02
@sbc100 sbc100 merged commit fd738b2 into main Sep 22, 2021
@sbc100 sbc100 deleted the tls_free_last branch September 22, 2021 05:16
@@ -562,15 +562,14 @@ void InitializeInterceptors() {
LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK;

LSAN_MAYBE_INTERCEPT_STRERROR;
#endif // !SANITIZER_FUCHSIA && !SANITIZER_EMSCRIPTEN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the movement of the fuscia ifdef here a divergence from upstream?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(otherwise lgtm)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, I hadn't realized I had effect fuchsia here. Will follow up.

sbc100 added a commit that referenced this pull request Sep 22, 2021
…dling

As well use being less code to maintain and more shared code this also
enables thread exit handling which means ASan now tracks thread exit
where it didn't before.  With the old/existing code noone was ever
calling the ASan thread destructor (passed as arg0 to AsanTSDInit).

I imagine this approach might not have been possible in the past since
there were some issues with using pthread_keys with destructors (e.g. #15086).

Testing by running entire `asan` test suite.
sbc100 added a commit that referenced this pull request Sep 22, 2021
In #15086 the intent was to enable `g_thread_finalize_key` under
emscripten but I unwittingly modified the ifdef region used by
fuchsia is well.
sbc100 added a commit that referenced this pull request Sep 22, 2021
In #15086 the intent was to enable `g_thread_finalize_key` under
emscripten but I unwittingly modified the ifdef region used by
fuchsia is well.
sbc100 added a commit that referenced this pull request Sep 22, 2021
…dling (#15095)

As well use being less code to maintain and more shared code this also
enables thread exit handling which means ASan now tracks thread exit
where it didn't before.  With the old/existing code noone was ever
calling the ASan thread destructor (passed as arg0 to AsanTSDInit).

I imagine this approach might not have been possible in the past since
there were some issues with using pthread_keys with destructors (e.g. #15086).

Testing by running entire `asan` test suite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants