wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

Is `wasm_runtime_module_malloc` thread-safe?

Open vdimir opened this issue 2 years ago • 2 comments

I get strange failures when calling wasm_runtime_module_malloc with different module instances from multiple threads, does it mean that this function is not thread safe? I use Alloc_With_Allocator option in RuntimeInitArgs. It's expected that in may be not thread safe with Alloc_With_Pool, but if if calls malloc it should be safe or not?

Also, can Alloc_With_Pool be used with multiple module instance running at the same time, if allocating will be performed under global mutex? What also should be in critical section?

vdimir avatar Jul 31 '21 17:07 vdimir

Hi, how do you build your wasm application ?

If your wasm application are built as LIBC-WASI mode, you need to hack the wasi-libc's dlmalloc.o as the dlmalloc function implemented by wasi-libc is not thread-safe, please refer to Build with libc-WASI section in pthread_library.md for more details.

If you are using LIBC-BUILTIN mode, then wasm_runtime_module_malloc is thread-safe.

xujuntwt95329 avatar Aug 01 '21 07:08 xujuntwt95329

Hi, how do you build your wasm application ?

I compile it with Emscripten emcc testapp.c -s STANDALONE_WASM -s ERROR_ON_UNDEFINED_SYMBOLS=0 -O2 -s TOTAL_MEMORY=6MB -o testapp.wasm --no-entry, but actually no explicit allocations are performed in wasm app.

Ok, I'll try different emcc options or another compiler.

vdimir avatar Aug 02 '21 07:08 vdimir