Repository navigation
[pthread] Return EAGAIN from pthread_create on allocation failure - #28402
Conversation
| 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. |
There was a problem hiding this comment.
Probably not worth adding a changelog entry for this.
There was a problem hiding this comment.
Removed.
| fail_malloc = 1; | ||
| rtn = pthread_create(&t, NULL, thread_main, NULL); | ||
| fail_malloc = 0; | ||
| printf("mailbox: %d\n", rtn == EAGAIN); |
There was a problem hiding this comment.
How about printf("mailbox: %s\n", strerror(rtn)); .. which would make the .out file a little more readable.
There was a problem hiding this comment.
Done, for both block and mailbox.
|
|
||
| // 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; |
| @@ -218,6 +221,13 @@ int __pthread_create(pthread_t* restrict res, | |||
| #endif | |||
There was a problem hiding this comment.
If we move these 3 lines down below the next block maybe we don't need the extra #ifndef NDEBUG block on line 225?
There was a problem hiding this comment.
Done — moved _emscripten_thread_profiler_init(new) below the mailbox check and dropped the extra #ifndef NDEBUG cleanup.
|
|
||
| void free(void* ptr) { | ||
| emscripten_builtin_free(ptr); | ||
| } |
There was a problem hiding this comment.
No, removed — works fine without it.
| assert(rtn == 0); | ||
| rtn = pthread_join(t, &result); | ||
| assert(rtn == 0); | ||
| assert(result == (void*)42); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Dropped (along with the now-unused result variable).
|
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.
Head branch was pushed to by a user without write access
c04ce4c to
578d6f4
Compare
578d6f4 to
b0fc378
Compare
`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
When an application creates many threads on a heap that cannot grow,
pthread_createcorrupts linear memory starting at address 0 instead ofreturning 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_createallocates thethread block with
emscripten_builtin_malloc(size)and passes the result tomemsetwithout checking forNULL. WebAssembly has no unmapped page ataddress 0, so
memsetand the following writes tostruct pthreadoverwrite low memory instead of faulting.
The mailbox has the same problem one allocation later.
em_task_queue_createreturns
NULLwhen itsmallocfails, but_emscripten_thread_mailbox_initstores the
NULLandpthread_createsucceeds. The new thread then locks andwrites through the null queue when it receives a proxied call or exits.
Return
EAGAINwhen either allocation fails, and free the thread block in themailbox case. POSIX specifies
EAGAINwhen the system lacks the resources tocreate another thread.
Add
other.test_pthread_create_nomem. It makes the block allocation fail witha stack larger than the heap, and makes the mailbox allocation fail with a
mallocoverride that leavesemscripten_builtin_mallocalone.