Skip to content

Commit c01004b

Browse files
authored
[WASM_WORKERS] Don't define pthread function in WASM_WORKERS builds (#26336)
Using the pthread API with WASM_WORKERS simply will not work. This also means function-local-static C++ initializes will now fail to link with WASM_WORKERS, at least until we fix #26277. Note that prior to this change this was already broken but would fail at runtime. See #26314
1 parent 83a2d8e commit c01004b

File tree

7 files changed

+85
-18
lines changed

7 files changed

+85
-18
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ See docs/process.md for more on how version tagging works.
2020

2121
5.0.3 (in development)
2222
----------------------
23+
- When building with `-sWASM_WORKERS` emscripten will no longer include pthread
24+
API stub functions. These stub functions where never designed to work under
25+
Wasm Workers, so its safer to error at link time if pthread APIs are used
26+
in Wasm Worker-based programs. (#26336)
2327

2428
5.0.2 - 02/25/26
2529
----------------
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2026 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <features.h>
9+
10+
static void dummy(double now) {
11+
}
12+
13+
weak_alias(dummy, _emscripten_check_timers);
14+
15+
void _emscripten_yield(double now) {
16+
_emscripten_check_timers(now);
17+
}

system/lib/pthread/library_pthread_stub.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
#include <emscripten/threading.h>
1818
#include <emscripten/emscripten.h>
1919

20+
#ifdef __EMSCRIPTEN_WASM_WORKERS__
21+
#error "This file contains stubs that should not be included in wasm workers builds."
22+
#endif
23+
2024
bool emscripten_has_threading_support() { return false; }
2125

2226
int emscripten_num_logical_cores() { return 1; }
2327

24-
#ifndef __EMSCRIPTEN_WASM_WORKERS__
2528
// These low level primites are defined in both pthreads and wasm workers
2629
// builds.
2730

@@ -55,7 +58,6 @@ void __unlock(void* ptr) {}
5558
void __acquire_ptc() {}
5659

5760
void __release_ptc() {}
58-
#endif
5961

6062
void emscripten_main_thread_process_queued_calls() {
6163
// nop
@@ -65,16 +67,6 @@ void emscripten_current_thread_process_queued_calls() {
6567
// nop
6668
}
6769

68-
static void dummy(double now)
69-
{
70-
}
71-
72-
weak_alias(dummy, _emscripten_check_timers);
73-
74-
void _emscripten_yield(double now) {
75-
_emscripten_check_timers(now);
76-
}
77-
7870
int pthread_mutex_init(
7971
pthread_mutex_t* __restrict mutex, const pthread_mutexattr_t* __restrict attr) {
8072
return 0;

test/test_other.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13519,6 +13519,16 @@ def test_shared_memory_preprocessor_flags(self):
1351913519
def test_wasm_worker_preprocessor_flags(self):
1352013520
self.run_process([EMCC, '-c', test_file('wasm_worker/wasm_worker_preprocessor_flags.c'), '-sWASM_WORKERS'])
1352113521

13522+
@also_with_minimal_runtime
13523+
def test_wasm_worker_pthread_api_usage(self):
13524+
self.assert_fail([EMCC, test_file('wasm_worker/wasm_worker_pthread_api_usage.c'), '-sWASM_WORKERS'], 'undefined symbol: pthread_mutex_lock')
13525+
13526+
@also_with_minimal_runtime
13527+
def test_wasm_worker_cxx_init(self):
13528+
# For now, C++ init guards do not work with Wasm Workers.
13529+
# See https://github.com/emscripten-core/emscripten/issues/26277
13530+
self.assert_fail([EMCC, test_file('wasm_worker/wasm_worker_cxx_init.cpp'), '-sWASM_WORKERS'], 'undefined symbol: pthread_cond_wait')
13531+
1352213532
@parameterized({
1352313533
# we will warn here since -O2 runs the optimizer and -g enables DWARF
1352413534
'O2_g': (True, ['-O2', '-g']),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
3+
struct Foo {
4+
Foo() {
5+
printf("create Foo\n");
6+
}
7+
8+
~Foo() {
9+
printf("destroy Foo\n");
10+
}
11+
12+
void baz() {
13+
printf("method called\n");
14+
}
15+
};
16+
17+
18+
void bar() {
19+
static Foo foo;
20+
foo.baz();
21+
}
22+
23+
int main() {
24+
printf("main\n");
25+
bar();
26+
printf("done\n");
27+
return 0;
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <pthread.h>
2+
3+
int main() {
4+
// This program should fail to link with WASM_WORKERS.
5+
pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
6+
pthread_mutex_lock(&my_mutex);
7+
}

tools/system_libs.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,13 +1256,10 @@ def get_files(self):
12561256
'thrd_sleep.c',
12571257
'thrd_yield.c',
12581258
])
1259+
12591260
libc_files += files_in_path(
1260-
path='system/lib/pthread',
1261-
filenames=[
1262-
'library_pthread_stub.c',
1263-
'pthread_self_stub.c',
1264-
'proxying_stub.c',
1265-
])
1261+
path='system/lib/libc',
1262+
filenames=['emscripten_yield_stub.c'])
12661263

12671264
if self.is_ww:
12681265
libc_files += files_in_path(
@@ -1272,6 +1269,18 @@ def get_files(self):
12721269
'__wait.c',
12731270
'lock_ptc.c',
12741271
])
1272+
else:
1273+
# Include stub version of thread functions when building
1274+
# in single theaded mode.
1275+
# Note: We do *not* include these stubs in the Wasm Workers build since it would
1276+
# never be safe to call these from a Wasm Worker.
1277+
libc_files += files_in_path(
1278+
path='system/lib/pthread',
1279+
filenames=[
1280+
'library_pthread_stub.c',
1281+
'pthread_self_stub.c',
1282+
'proxying_stub.c',
1283+
])
12751284

12761285
if self.is_mt or self.is_ww:
12771286
# Low level thread primitives available in both pthreads and wasm workers builds.

0 commit comments

Comments
 (0)