Skip to content

[pthreads] Initialize TLS on workers in MINIMAL_RUNTIME mode. #24646

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 2 commits into from
Jul 2, 2025

Conversation

brendandahl
Copy link
Collaborator

TLS was not initialized on workers causing the tls_base address to be 0 and write to the wrong memory location.

Fixes #21528 and is also needed for PR #24565

TLS was not initialized on workers causing the tls_base address
to be 0 and write to the wrong memory location.

Fixes emscripten-core#21528 and is also needed for PR emscripten-core#24565
@brendandahl brendandahl requested review from kripken and sbc100 July 2, 2025 17:55
@@ -66,10 +69,6 @@ function initRuntime(wasmExports) {
writeStackCookie();
#endif

#if PTHREADS
PThread.tlsInitFunctions.push(wasmExports['_emscripten_tls_init']);
Copy link
Member

Choose a reason for hiding this comment

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

Does this not remove it from the main thread as well? (And the new code only re-adds it for pthreads)

Or is this not needed on the main thread? But from libpthread.js it looks like it is run there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does remove it from the main thread, which was not my intention. I'm amazed that worked. I guess there's not much testing for minimal+pthread.

Ideally this code would be in some shared spot that both minimal runtime + regular runtime would share, but I think that's going to be a bigger refactor.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds like a hole in testing, yeah... we do have pthreads tests for minimal runtime, but without TLS, I guess, until this PR.

@brendandahl brendandahl merged commit b0c461c into emscripten-core:main Jul 2, 2025
25 of 30 checks passed
#include <assert.h>
#include <stdio.h>

static _Thread_local int thread_local = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it perhaps make sense to do = 3 here and assert that thread_local starts with this value in moth main and in `thread_start?

def test_pthread_c_thread_local(self):
if self.get_setting('MINIMAL_RUNTIME') and is_sanitizing(self.cflags):
self.skipTest('MINIMAL_RUNTIME + threads + asan does not work')
self.do_run_in_out_file_test('pthread/test_pthread_c_thread_local.c')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Surely we already have tests that cover this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like test/pthread/test_pthread_tls.cpp is probably what we want. It happens to use C++ thread_local instead of the C's _Thread_local but I think it should the same thing from the POV of this PR.

sbc100 added a commit to sbc100/emscripten that referenced this pull request Jul 7, 2025
- Convert to C
- Run in core tests under node.
- Remove redundant test added in emscripten-core#24646
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jul 8, 2025
- Convert to C
- Run in core tests under node.
- Remove redundant test added in emscripten-core#24646
sbc100 added a commit that referenced this pull request Jul 8, 2025
- Convert to C
- Run in core tests under node.
- Remove redundant test added in #24646
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.

Memory corruption/segfault with MINIMAL_RUNTIME + std::thread + thread_local variables
3 participants