Skip to content

[pthread] Return EAGAIN from pthread_create on allocation failure - #28402

Merged
sbc100 merged 2 commits into
emscripten-core:mainfrom
blackbird-aslough:pthread_create_eagain
Nov 30, 2026
Merged

sbc100 merged 2 commits into
emscripten-core:mainfrom
blackbird-aslough:pthread_create_eagain

Conversation

@blackbird-aslough

@blackbird-aslough blackbird-aslough commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

When an application creates many threads on a heap that cannot grow,
pthread_create corrupts linear memory starting at address 0 instead of
returning an error. The stray writes can overwrite static data, allocator
state, or main-thread TLS and cause later allocations to trap.

In system/lib/pthread/pthread_create.c, __pthread_create allocates the
thread block with emscripten_builtin_malloc(size) and passes the result to
memset without checking for NULL. WebAssembly has no unmapped page at
address 0, so memset and the following writes to struct pthread
overwrite low memory instead of faulting.

The mailbox has the same problem one allocation later. em_task_queue_create
returns NULL when its malloc fails, but _emscripten_thread_mailbox_init
stores the NULL and pthread_create succeeds. The new thread then locks and
writes through the null queue when it receives a proxied call or exits.

Return EAGAIN when either allocation fails, and free the thread block in the
mailbox case. POSIX specifies EAGAIN when the system lacks the resources to
create another thread.

Add other.test_pthread_create_nomem. It makes the block allocation fail with
a stack larger than the heap, and makes the mailbox allocation fail with a
malloc override that leaves emscripten_builtin_malloc alone.

Comment thread ChangeLog.md Outdated
7.0.2 (in development)
----------------------
- `pthread_create()` now returns `EAGAIN` when it cannot allocate the new
thread's block or mailbox, instead of writing to address 0.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Probably not worth adding a changelog entry for this.

@blackbird-aslough blackbird-aslough Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

Comment thread test/other/test_pthread_create_nomem.c Outdated
fail_malloc = 1;
rtn = pthread_create(&t, NULL, thread_main, NULL);
fail_malloc = 0;
printf("mailbox: %d\n", rtn == EAGAIN);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How about printf("mailbox: %s\n", strerror(rtn)); .. which would make the .out file a little more readable.

@blackbird-aslough blackbird-aslough Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, for both block and mailbox.

Comment thread test/other/test_pthread_create_nomem.c Outdated

// The thread block comes from emscripten_builtin_malloc, which this does not
// override, so setting fail_malloc fails only the allocations after it.
static _Atomic int fail_malloc;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Use bool here

@blackbird-aslough blackbird-aslough Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread system/lib/pthread/pthread_create.c Outdated
@@ -218,6 +221,13 @@ int __pthread_create(pthread_t* restrict res,
#endif

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If we move these 3 lines down below the next block maybe we don't need the extra #ifndef NDEBUG block on line 225?

@blackbird-aslough blackbird-aslough Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — moved _emscripten_thread_profiler_init(new) below the mailbox check and dropped the extra #ifndef NDEBUG cleanup.

Comment thread test/other/test_pthread_create_nomem.c Outdated

void free(void* ptr) {
emscripten_builtin_free(ptr);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this function needed?

@blackbird-aslough blackbird-aslough Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, removed — works fine without it.

Comment thread test/other/test_pthread_create_nomem.c Outdated
assert(rtn == 0);
rtn = pthread_join(t, &result);
assert(rtn == 0);
assert(result == (void*)42);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe just drop these 7 lines. I'm not sure there is really a good way to test "The heap and the main thread are intact" ... but when the program exists (in debug mode) it will check the stack cookie and the zero-address cookie which should be enough to catch this.

@blackbird-aslough blackbird-aslough Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dropped (along with the now-unused result variable).

@sbc100
sbc100 enabled auto-merge (squash) November 30, 2026 17:35
@sbc100

sbc100 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Looks like the codesize tests need rebasing one more time.

When an application creates many threads on a heap that cannot grow,
`pthread_create` corrupts linear memory starting at address 0 instead of
returning an error. The stray writes can overwrite static data, allocator
state, or main-thread TLS and cause later allocations to trap.

In `system/lib/pthread/pthread_create.c`, `__pthread_create` allocates the
thread block with `emscripten_builtin_malloc(size)` and passes the result to
`memset` without checking for `NULL`. WebAssembly has no unmapped page at
address 0, so `memset` and the following writes to `struct pthread`
overwrite low memory instead of faulting.

The mailbox has the same problem one allocation later. `em_task_queue_create`
returns `NULL` when its `malloc` fails, but `_emscripten_thread_mailbox_init`
stores the `NULL` and `pthread_create` succeeds. The new thread then locks and
writes through the null queue when it receives a proxied call or exits.

Return `EAGAIN` when either allocation fails, and free the thread block in the
mailbox case. POSIX specifies `EAGAIN` when the system lacks the resources to
create another thread.

Add `other.test_pthread_create_nomem`. It makes the block allocation fail with
a stack larger than the heap. It makes the mailbox allocation fail with a
`malloc` override that leaves `emscripten_builtin_malloc` alone. It then
checks that `malloc` and a new thread still work.
auto-merge was automatically disabled November 30, 2026 20:46

Head branch was pushed to by a user without write access

@sbc100
sbc100 enabled auto-merge (squash) November 30, 2026 21:17
@sbc100
sbc100 disabled auto-merge November 30, 2026 22:05
@sbc100
sbc100 merged commit 4acf5c6 into emscripten-core:main Nov 30, 2026
38 of 39 checks passed
sbc100 added a commit that referenced this pull request Nov 30, 2026
`test_codesize_minimal_pthreads` and
`test_codesize_minimal_pthreads_memgrowth` have been failing by +1 byte
in `a.out.nodebug.wasm.gz` (`8765` -> `8766` and `8766` -> `8767`) since
#28402 landed.

On #28402, Codesize Checks flagged this exact 1-byte mismatch before
merging. The PR was then rebased onto `main` without updating
`test/codesize/test_codesize_minimal_pthreads.json` and
`test/codesize/test_codesize_minimal_pthreads_memgrowth.json`. GitHub
Actions failed to start any jobs on that final force-push (reporting a
workflow file error), so Codesize Checks never ran on the rebased commit
before #28402 was merged.

See: #28402
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.

2 participants