Skip to content

Commit 739acfc

Browse files
authored
Merge pull request bytecodealliance#1966 from bytecodealliance/dev/wasi_threads
Merge wasi_threads to main
2 parents ef3a683 + e170c35 commit 739acfc

28 files changed

+985
-43
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,22 @@ jobs:
358358
sudo tar -xzf wabt-1.0.31-*.tar.gz
359359
sudo mv wabt-1.0.31 wabt
360360
361+
- name: build wasi-libc (needed for wasi-threads)
362+
run: |
363+
mkdir wasi-libc
364+
cd wasi-libc
365+
git init
366+
# "Rename thread_spawn import" commit on main branch
367+
git fetch https://github.com/WebAssembly/wasi-libc \
368+
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
369+
git checkout FETCH_HEAD
370+
make \
371+
AR=/opt/wasi-sdk/bin/llvm-ar \
372+
NM=/opt/wasi-sdk/bin/llvm-nm \
373+
CC=/opt/wasi-sdk/bin/clang \
374+
THREAD_MODEL=posix
375+
working-directory: core/deps
376+
361377
- name: Build Sample [basic]
362378
run: |
363379
cd samples/basic
@@ -411,6 +427,14 @@ jobs:
411427
exit $?
412428
working-directory: ./samples/simple
413429

430+
- name: Build Sample [wasi-threads]
431+
run: |
432+
cd samples/wasi-threads
433+
mkdir build && cd build
434+
cmake -DWASI_SYSROOT=`pwd`/../../../core/deps/wasi-libc/sysroot ..
435+
cmake --build . --config Release --parallel 4
436+
./iwasm wasm-apps/no_pthread.wasm
437+
414438
test:
415439
needs: [build_iwasm, build_llvm_libraries_on_ubuntu_2004, build_wamrc]
416440
runs-on: ubuntu-20.04

.github/workflows/compilation_on_macos.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,22 @@ jobs:
273273
sudo tar -xzf wabt-1.0.31-*.tar.gz
274274
sudo mv wabt-1.0.31 wabt
275275
276+
- name: build wasi-libc (needed for wasi-threads)
277+
run: |
278+
mkdir wasi-libc
279+
cd wasi-libc
280+
git init
281+
# "Rename thread_spawn import" commit on main branch
282+
git fetch https://github.com/WebAssembly/wasi-libc \
283+
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
284+
git checkout FETCH_HEAD
285+
make \
286+
AR=/opt/wasi-sdk/bin/llvm-ar \
287+
NM=/opt/wasi-sdk/bin/llvm-nm \
288+
CC=/opt/wasi-sdk/bin/clang \
289+
THREAD_MODEL=posix
290+
working-directory: core/deps
291+
276292
- name: Build Sample [basic]
277293
run: |
278294
cd samples/basic
@@ -318,3 +334,11 @@ jobs:
318334
cmake ..
319335
cmake --build . --config Release --parallel 4
320336
./hello
337+
338+
- name: Build Sample [wasi-threads]
339+
run: |
340+
cd samples/wasi-threads
341+
mkdir build && cd build
342+
cmake -DWASI_SYSROOT=`pwd`/../../../core/deps/wasi-libc/sysroot ..
343+
cmake --build . --config Release --parallel 4
344+
./iwasm wasm-apps/no_pthread.wasm

.github/workflows/compilation_on_sgx.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,22 @@ jobs:
260260
sudo tar -xzf wabt-1.0.31-*.tar.gz
261261
sudo mv wabt-1.0.31 wabt
262262
263+
- name: build wasi-libc (needed for wasi-threads)
264+
run: |
265+
mkdir wasi-libc
266+
cd wasi-libc
267+
git init
268+
# "Rename thread_spawn import" commit on main branch
269+
git fetch https://github.com/WebAssembly/wasi-libc \
270+
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
271+
git checkout FETCH_HEAD
272+
make \
273+
AR=/opt/wasi-sdk/bin/llvm-ar \
274+
NM=/opt/wasi-sdk/bin/llvm-nm \
275+
CC=/opt/wasi-sdk/bin/clang \
276+
THREAD_MODEL=posix
277+
working-directory: core/deps
278+
263279
- name: install SGX SDK and necessary libraries
264280
run: |
265281
mkdir -p /opt/intel
@@ -319,6 +335,14 @@ jobs:
319335
cmake --build . --config Release --parallel 4
320336
./hello
321337
338+
- name: Build Sample [wasi-threads]
339+
run: |
340+
cd samples/wasi-threads
341+
mkdir build && cd build
342+
cmake -DWASI_SYSROOT=`pwd`/../../../core/deps/wasi-libc/sysroot ..
343+
cmake --build . --config Release --parallel 4
344+
./iwasm wasm-apps/no_pthread.wasm
345+
322346
spec_test_default:
323347
needs: [build_iwasm, build_llvm_libraries, build_wamrc]
324348
runs-on: ubuntu-20.04

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
8383
set (WAMR_BUILD_LIB_PTHREAD 0)
8484
endif ()
8585

86+
if (NOT DEFINED WAMR_BUILD_LIB_WASI_THREADS)
87+
# Disable wasi threads library by default
88+
set (WAMR_BUILD_LIB_WASI_THREADS 0)
89+
endif ()
90+
8691
if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
8792
# Disable wasm mini loader by default
8893
set (WAMR_BUILD_MINI_LOADER 0)

build-scripts/runtime_lib.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ if (WAMR_BUILD_LIB_PTHREAD EQUAL 1)
125125
set (WAMR_BUILD_SHARED_MEMORY 1)
126126
endif ()
127127

128+
if (WAMR_BUILD_LIB_WASI_THREADS EQUAL 1)
129+
include (${IWASM_DIR}/libraries/lib-wasi-threads/lib_wasi_threads.cmake)
130+
# Enable the dependent feature if lib wasi threads is enabled
131+
set (WAMR_BUILD_THREAD_MGR 1)
132+
set (WAMR_BUILD_BULK_MEMORY 1)
133+
set (WAMR_BUILD_SHARED_MEMORY 1)
134+
endif ()
135+
128136
if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
129137
set (WAMR_BUILD_THREAD_MGR 1)
130138
include (${IWASM_DIR}/libraries/debug-engine/debug_engine.cmake)
@@ -191,6 +199,7 @@ set (source_all
191199
${WASM_APP_LIB_SOURCE_ALL}
192200
${NATIVE_INTERFACE_SOURCE}
193201
${APP_MGR_SOURCE}
202+
${LIB_WASI_THREADS_SOURCE}
194203
${LIB_PTHREAD_SOURCE}
195204
${THREAD_MGR_SOURCE}
196205
${LIBC_EMCC_SOURCE}

core/config.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@
161161
#define WASM_ENABLE_LIB_PTHREAD_SEMAPHORE 0
162162
#endif
163163

164+
#ifndef WASM_ENABLE_LIB_WASI_THREADS
165+
#define WASM_ENABLE_LIB_WASI_THREADS 0
166+
#endif
167+
168+
#ifndef WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION
169+
#define WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION WASM_ENABLE_LIB_WASI_THREADS
170+
#elif WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0 \
171+
&& WASM_ENABLE_LIB_WASI_THREADS == 1
172+
#error "Heap aux stack allocation must be enabled for WASI threads"
173+
#endif
174+
164175
#ifndef WASM_ENABLE_BASE_LIB
165176
#define WASM_ENABLE_BASE_LIB 0
166177
#endif

core/iwasm/common/wasm_exec_env.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
196196
void
197197
wasm_exec_env_destroy(WASMExecEnv *exec_env);
198198

199+
static inline bool
200+
wasm_exec_env_is_aux_stack_managed_by_runtime(WASMExecEnv *exec_env)
201+
{
202+
return exec_env->aux_stack_boundary.boundary != 0
203+
|| exec_env->aux_stack_bottom.bottom != 0;
204+
}
205+
199206
/**
200207
* Allocate a WASM frame from the WASM stack.
201208
*

core/iwasm/common/wasm_native.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ uint32
5353
get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
5454
#endif
5555

56+
#if WASM_ENABLE_LIB_WASI_THREADS != 0
57+
bool
58+
lib_wasi_threads_init(void);
59+
60+
void
61+
lib_wasi_threads_destroy(void);
62+
63+
uint32
64+
get_lib_wasi_threads_export_apis(NativeSymbol **p_lib_wasi_threads_apis);
65+
#endif
66+
5667
uint32
5768
get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
5869

@@ -390,7 +401,7 @@ wasm_native_init()
390401
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
391402
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
392403
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
393-
|| WASM_ENABLE_LIB_PTHREAD != 0
404+
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
394405
NativeSymbol *native_symbols;
395406
uint32 n_native_symbols;
396407
#endif
@@ -445,6 +456,17 @@ wasm_native_init()
445456
goto fail;
446457
#endif
447458

459+
#if WASM_ENABLE_LIB_WASI_THREADS != 0
460+
if (!lib_wasi_threads_init())
461+
goto fail;
462+
463+
n_native_symbols = get_lib_wasi_threads_export_apis(&native_symbols);
464+
if (n_native_symbols > 0
465+
&& !wasm_native_register_natives("wasi", native_symbols,
466+
n_native_symbols))
467+
goto fail;
468+
#endif
469+
448470
#if WASM_ENABLE_LIBC_EMCC != 0
449471
n_native_symbols = get_libc_emcc_export_apis(&native_symbols);
450472
if (n_native_symbols > 0
@@ -465,15 +487,15 @@ wasm_native_init()
465487
n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
466488
if (!wasm_native_register_natives("wasi_nn", native_symbols,
467489
n_native_symbols))
468-
return false;
490+
goto fail;
469491
#endif
470492

471493
return true;
472494
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
473495
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
474496
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
475497
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
476-
|| WASM_ENABLE_LIB_PTHREAD != 0
498+
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
477499
fail:
478500
wasm_native_destroy();
479501
return false;
@@ -489,6 +511,10 @@ wasm_native_destroy()
489511
lib_pthread_destroy();
490512
#endif
491513

514+
#if WASM_ENABLE_LIB_WASI_THREADS != 0
515+
lib_wasi_threads_destroy();
516+
#endif
517+
492518
node = g_native_symbols_list;
493519
while (node) {
494520
node_next = node->next;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4195,7 +4195,20 @@ check_wasi_abi_compatibility(const WASMModule *module,
41954195

41964196
memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
41974197
error_buf, error_buf_size);
4198-
if (!memory) {
4198+
if (!memory
4199+
#if WASM_ENABLE_LIB_WASI_THREADS != 0
4200+
/*
4201+
* with wasi-threads, it's still an open question if a memory
4202+
* should be exported.
4203+
*
4204+
* https://github.com/WebAssembly/wasi-threads/issues/22
4205+
* https://github.com/WebAssembly/WASI/issues/502
4206+
*
4207+
* Note: this code assumes the number of memories is at most 1.
4208+
*/
4209+
&& module->import_memory_count == 0
4210+
#endif
4211+
) {
41994212
set_error_buf(error_buf, error_buf_size,
42004213
"a module with WASI apis must export memory by default");
42014214
return false;

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,14 +2640,16 @@ wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
26402640
WASMModuleInstance *module_inst =
26412641
(WASMModuleInstance *)exec_env->module_inst;
26422642
uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
2643+
2644+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
2645+
/* Check the aux stack space */
26432646
uint32 data_end = module_inst->module->aux_data_end;
26442647
uint32 stack_bottom = module_inst->module->aux_stack_bottom;
26452648
bool is_stack_before_data = stack_bottom < data_end ? true : false;
2646-
2647-
/* Check the aux stack space, currently we don't allocate space in heap */
26482649
if ((is_stack_before_data && (size > start_offset))
26492650
|| ((!is_stack_before_data) && (start_offset - data_end < size)))
26502651
return false;
2652+
#endif
26512653

26522654
if (stack_top_idx != (uint32)-1) {
26532655
/* The aux stack top is a wasm global,

0 commit comments

Comments
 (0)