Skip to content

Commit

Permalink
When using WASM_WORKERS: make sure sbrk ptr is aligned properly for t…
Browse files Browse the repository at this point in the history
…ls space, and malloc needs to be thread safe (emscripten-core#18157)

* emscripten_wasm_worker_main_thread_initialize: make sure sbrk ptr is aligned properly when account for tls space

* dlmalloc.c: define USE_LOCKS when __EMSCRIPTEN_WASM_WORKERS__ is true, so that memory allocation is thread safe

* emscripten_wasm_worker_main_thread_initialize: make sure sbrk ptr is aligned properly when account for tls space

* emscripten_wasm_worker_main_thread_initialize: make sure sbrk ptr is aligned properly when account for tls space

Co-authored-by: kme <none>
  • Loading branch information
mannk123 authored Nov 9, 2022
1 parent b1a4545 commit ed897a5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions system/lib/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
/* XXX Emscripten Tracing API. This defines away the code if tracing is disabled. */
#include <emscripten/trace.h>

#ifdef __EMSCRIPTEN_WASM_WORKERS__
#define USE_LOCKS 1
#endif

/* Make malloc() and free() threadsafe by securing the memory allocations with pthread mutexes. */
#if __EMSCRIPTEN_PTHREADS__
#define USE_LOCKS 1
Expand Down
4 changes: 3 additions & 1 deletion system/lib/wasm_worker/library_wasm_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ void __wasm_init_tls(void *memory);
__attribute__((constructor(48)))
static void emscripten_wasm_worker_main_thread_initialize() {
uintptr_t* sbrk_ptr = emscripten_get_sbrk_ptr();
assert((*sbrk_ptr & 15) == 0);
assert(__builtin_wasm_tls_align() <= 16);
__wasm_init_tls((void*)*sbrk_ptr);
*sbrk_ptr += __builtin_wasm_tls_size();
*sbrk_ptr += (__builtin_wasm_tls_size() + 15) & -16;
}

emscripten_wasm_worker_t emscripten_create_wasm_worker(void *stackLowestAddress, uint32_t stackSize)
Expand Down

0 comments on commit ed897a5

Please sign in to comment.