From 12b2d845aa06c1232df2c918a0d087277cc0cafd Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 15 Feb 2023 23:17:42 +0000 Subject: [PATCH 1/7] test(wasi-threads): add tests for wasi threads --- .../libraries/wasi-threads/test/build.sh | 25 ++++ .../test/create_threads_until_limit.c | 128 ++++++++++++++++++ .../wasi-threads/test/global_atomic.c | 70 ++++++++++ .../libraries/wasi-threads/test/global_lock.c | 85 ++++++++++++ .../wasi-threads/test/spawn_multiple_times.c | 75 ++++++++++ .../test/update_shared_data_and_alloc_heap.c | 86 ++++++++++++ samples/wasi-threads/wasm-apps/no_pthread.c | 2 +- .../wasi-test-script/run_wasi_tests.sh | 4 +- 8 files changed, 473 insertions(+), 2 deletions(-) create mode 100644 core/iwasm/libraries/wasi-threads/test/build.sh create mode 100644 core/iwasm/libraries/wasi-threads/test/create_threads_until_limit.c create mode 100644 core/iwasm/libraries/wasi-threads/test/global_atomic.c create mode 100644 core/iwasm/libraries/wasi-threads/test/global_lock.c create mode 100644 core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c create mode 100644 core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c diff --git a/core/iwasm/libraries/wasi-threads/test/build.sh b/core/iwasm/libraries/wasi-threads/test/build.sh new file mode 100644 index 0000000000..17ebeb1706 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/build.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +CC=${CC:=/opt/wasi-sdk/bin/clang} +WASI_SYSROOT=${WASI_SYSROOT:=~/dev/wasi-libc/sysroot} +WAMR_DIR=../../../../.. + +for test_c in *.c; do + test_wasm="$(basename $test_c .c).wasm" + + echo "Compiling $test_c to $test_wasm" + $CC \ + --sysroot $WASI_SYSROOT \ + -target wasm32-wasi-threads \ + -pthread -ftls-model=local-exec \ + -z stack-size=32768 \ + -Wl,--export=__heap_base \ + -Wl,--export=__data_end \ + -Wl,--shared-memory,--max-memory=1966080 \ + -Wl,--export=wasi_thread_start \ + -Wl,--export=malloc \ + -Wl,--export=free \ + -I $WAMR_DIR/samples/wasi-threads/wasm-apps \ + $WAMR_DIR/samples/wasi-threads/wasm-apps/wasi_thread_start.S \ + $test_c -o $test_wasm +done \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/create_threads_until_limit.c b/core/iwasm/libraries/wasi-threads/test/create_threads_until_limit.c new file mode 100644 index 0000000000..23ba5f6277 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/create_threads_until_limit.c @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include +#include +#include +#include + +#include "wasi_thread_start.h" + +enum CONSTANTS { + MAX_NUM_THREADS = 4, /* Should be the same as "--max-threads" */ + NUM_RETRY = 5, + SECOND = 1000 * 1000 * 1000, /* 1 second */ + TIMEOUT = 10LL * SECOND +}; + +int g_count = 0; + +typedef struct { + start_args_t base; + int th_ready; + int th_continue; + int th_done; + bool no_ops; +} shared_t; + +void +__wasi_thread_start_C(int thread_id, int *start_arg) +{ + shared_t *data = (shared_t *)start_arg; + + if (data->no_ops) { + __builtin_wasm_memory_atomic_wait32(NULL, 0, 2 * SECOND); + return; + } + + __atomic_store_n(&data->th_ready, 1, __ATOMIC_SEQ_CST); + __builtin_wasm_memory_atomic_notify(&data->th_ready, 1); + + if (__builtin_wasm_memory_atomic_wait32(&data->th_continue, 0, TIMEOUT) + == 2) { + assert(false && "Wait should not time out"); + } + + __atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST); + + __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); + __builtin_wasm_memory_atomic_notify(&data->th_done, 1); +} + +int +main(int argc, char **argv) +{ + shared_t data[MAX_NUM_THREADS] = { 0 }; + int thread_ids[MAX_NUM_THREADS]; + + for (int i = 0; i < MAX_NUM_THREADS; i++) { + assert(start_args_init(&data[i].base)); + thread_ids[i] = __wasi_thread_spawn(&data[i]); + printf("Thread created with id=%d\n", thread_ids[i]); + assert(thread_ids[i] > 0 && "Thread creation failed"); + + for (int j = 0; j < i; j++) { + assert(thread_ids[i] != thread_ids[j] && "Duplicated TIDs"); + } + + if (__builtin_wasm_memory_atomic_wait32(&data[i].th_ready, 0, TIMEOUT) + == 2) { + assert(false && "Wait should not time out"); + } + } + + printf("Attempt to create thread when not possible\n"); + shared_t data_fail = { 0 }; + assert(start_args_init(&data_fail.base)); + int thread_id = __wasi_thread_spawn(&data_fail); + start_args_deinit(&data_fail.base); + assert(thread_id < 0 && "Thread creation should fail"); + + printf("Unlock created threads\n"); + for (int i = 0; i < MAX_NUM_THREADS; i++) { + __atomic_store_n(&data[i].th_continue, 1, __ATOMIC_SEQ_CST); + __builtin_wasm_memory_atomic_notify(&data[i].th_continue, 1); + } + + printf("Wait for threads to finish\n"); + for (int i = 0; i < MAX_NUM_THREADS; i++) { + if (__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, TIMEOUT) + == 2) { + assert(false && "Wait should not time out"); + } + + start_args_deinit(&data[i].base); + } + + printf("Value of count after update: %d\n", g_count); + assert(g_count == (MAX_NUM_THREADS) + && "Global count not updated correctly"); + + /* --------------------------------------------------- */ + + printf("Create new threads without waiting from them to finish\n"); + shared_t data_no_join[MAX_NUM_THREADS] = { 0 }; + for (int i = 0; i < MAX_NUM_THREADS; i++) { + /* No graceful memory free to simplify the test */ + assert(start_args_init(&data_no_join[i].base)); + data_no_join[i].no_ops = true; + + int thread_id = -1; + for (int j = 0; j < NUM_RETRY && thread_id < 0; j++) { + thread_id = __wasi_thread_spawn(&data_no_join[i]); + if (thread_id < 0) + __builtin_wasm_memory_atomic_wait32(NULL, 0, SECOND); + } + + printf("Thread created with id=%d\n", thread_id); + assert(thread_id > 0 && "Thread creation should succeed"); + } + + return EXIT_SUCCESS; +} diff --git a/core/iwasm/libraries/wasi-threads/test/global_atomic.c b/core/iwasm/libraries/wasi-threads/test/global_atomic.c new file mode 100644 index 0000000000..9a38b7974a --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/global_atomic.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include +#include +#include +#include + +#include "wasi_thread_start.h" + +enum CONSTANTS { + NUM_THREADS = 5, + NUM_ITER = 1000, + SECOND = 1000 * 1000 * 1000, /* 1 second */ + TIMEOUT = 10LL * SECOND +}; + +int g_count = 0; + +typedef struct { + start_args_t base; + int th_done; +} shared_t; + +void +__wasi_thread_start_C(int thread_id, int *start_arg) +{ + shared_t *data = (shared_t *)start_arg; + + for (int i = 0; i < NUM_ITER; i++) + __atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST); + + __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); + __builtin_wasm_memory_atomic_notify(&data->th_done, 1); +} + +int +main(int argc, char **argv) +{ + shared_t data[NUM_THREADS] = { 0 }; + int thread_ids[NUM_THREADS]; + + for (int i = 0; i < NUM_THREADS; i++) { + assert(start_args_init(&data[i].base)); + thread_ids[i] = __wasi_thread_spawn(&data[i]); + assert(thread_ids[i] > 0 && "Thread creation failed"); + } + + printf("Wait for threads to finish\n"); + for (int i = 0; i < NUM_THREADS; i++) { + if (__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, TIMEOUT) + == 2) { + assert(false && "Wait should not time out"); + } + + start_args_deinit(&data[i].base); + } + + printf("Value of count after update: %d\n", g_count); + assert(g_count == (NUM_THREADS * NUM_ITER) + && "Global count not updated correctly"); + + return EXIT_SUCCESS; +} diff --git a/core/iwasm/libraries/wasi-threads/test/global_lock.c b/core/iwasm/libraries/wasi-threads/test/global_lock.c new file mode 100644 index 0000000000..90d8b6f07b --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/global_lock.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include +#include +#include +#include +#include + +#include "wasi_thread_start.h" + +enum CONSTANTS { + NUM_THREADS = 4, + NUM_ITER = 200, + SECOND = 1000 * 1000 * 1000, /* 1 second */ + TIMEOUT = 10LL * SECOND +}; + +pthread_mutex_t mutex; +int g_count = 0; + +typedef struct { + start_args_t base; + int th_done; +} shared_t; + +void +__wasi_thread_start_C(int thread_id, int *start_arg) +{ + shared_t *data = (shared_t *)start_arg; + + for (int i = 0; i < NUM_ITER; i++) { + pthread_mutex_lock(&mutex); + g_count++; + pthread_mutex_unlock(&mutex); + } + + __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); + __builtin_wasm_memory_atomic_notify(&data->th_done, 1); +} + +int +main(int argc, char **argv) +{ + shared_t data[NUM_THREADS] = { 0 }; + int thread_ids[NUM_THREADS]; + + if (pthread_mutex_init(&mutex, NULL) != 0) { + printf("Failed to init mutex.\n"); + return EXIT_FAILURE; + } + + for (int i = 0; i < NUM_THREADS; i++) { + assert(start_args_init(&data[i].base)); + thread_ids[i] = __wasi_thread_spawn(&data[i]); + assert(thread_ids[i] > 0 && "Thread creation failed"); + } + + printf("Wait for threads to finish\n"); + for (int i = 0; i < NUM_THREADS; i++) { + if (__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, TIMEOUT) + == 2) { + assert(false && "Wait should not time out"); + } + + start_args_deinit(&data[i].base); + } + + printf("Value of count after update: %d\n", g_count); + assert(g_count == (NUM_THREADS * NUM_ITER) + && "Global count not updated correctly"); + + if (pthread_mutex_destroy(&mutex) != 0) { + printf("Failed to init mutex.\n"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c b/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c new file mode 100644 index 0000000000..adaa18276b --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include +#include +#include +#include + +#include "wasi_thread_start.h" + +enum CONSTANTS { + NUM_ITER = 50, + NUM_RETRY = 5, + SECOND = 1000 * 1000 * 1000, /* 1 second */ + TIMEOUT = 5LL * SECOND +}; + +typedef struct { + start_args_t base; + int th_done; +} shared_t; + +int g_count = 0; + +void +__wasi_thread_start_C(int thread_id, int *start_arg) +{ + shared_t *data = (shared_t *)start_arg; + + g_count++; + + __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); + __builtin_wasm_memory_atomic_notify(&data->th_done, 1); +} + +int +main(int argc, char **argv) +{ + shared_t data = { 0 }; + if (!start_args_init(&data.base)) { + printf("Stack allocation for thread failed\n"); + return EXIT_FAILURE; + } + + for (int i = 0; i < NUM_ITER; i++) { + data.th_done = 0; + + printf("Creating thread\n"); + int thread_id = -1; + for (int j = 0; j < NUM_RETRY && thread_id < 0; j++) { + thread_id = __wasi_thread_spawn(&data); + if (thread_id < 0) + __builtin_wasm_memory_atomic_wait32(NULL, 0, SECOND); + } + assert(thread_id > 0 && "Thread creation should succeed"); + + printf("Waiting for thread to finish\n"); + if (__builtin_wasm_memory_atomic_wait32(&data.th_done, 0, TIMEOUT) + == 2) { + assert(false && "Wait should not time out"); + } + printf("Thread has finished\n"); + } + + assert(g_count == NUM_ITER && "Count has not been updated correctly"); + + start_args_deinit(&data.base); + return EXIT_SUCCESS; +} diff --git a/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c b/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c new file mode 100644 index 0000000000..ee17c20082 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include +#include +#include +#include + +#include "wasi_thread_start.h" + +enum CONSTANTS { + NUM_THREADS = 5, + NUM_ITER = 30, + SECOND = 1000 * 1000 * 1000, /* 1 second */ + TIMEOUT = 10LL * SECOND +}; + +typedef struct { + start_args_t base; + int th_done; + int *count; + int iteration; + int *pval; +} shared_t; + +int *vals[NUM_THREADS]; + +void +__wasi_thread_start_C(int thread_id, int *start_arg) +{ + shared_t *data = (shared_t *)start_arg; + + for (int i = 0; i < NUM_ITER; i++) + __atomic_fetch_add(data->count, 1, __ATOMIC_SEQ_CST); + + vals[data->iteration] = malloc(sizeof(int)); + *vals[data->iteration] = data->iteration; + + __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); + __builtin_wasm_memory_atomic_notify(&data->th_done, 1); +} + +int +main(int argc, char **argv) +{ + shared_t data[NUM_THREADS] = { 0 }; + int thread_ids[NUM_THREADS]; + int *count = calloc(1, sizeof(int)); + + for (int i = 0; i < NUM_THREADS; i++) { + assert(start_args_init(&data[i].base) + && "Stack allocation for thread failed"); + data[i].count = count; + data[i].iteration = i; + + thread_ids[i] = __wasi_thread_spawn(&data[i]); + assert(thread_ids[i] > 0 && "Thread creation failed"); + } + + printf("Wait for threads to finish\n"); + for (int i = 0; i < NUM_THREADS; i++) { + if (__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, TIMEOUT) + == 2) { + assert(false && "Wait should not time out"); + } + + start_args_deinit(&data[i].base); + } + + assert(*count == (NUM_THREADS * NUM_ITER) && "Count not updated correctly"); + + for (int i = 0; i < NUM_THREADS; i++) { + printf("val=%d\n", *vals[i]); + assert(*vals[i] == i && "Value not updated correctly"); + free(vals[i]); + } + + free(count); + return EXIT_SUCCESS; +} diff --git a/samples/wasi-threads/wasm-apps/no_pthread.c b/samples/wasi-threads/wasm-apps/no_pthread.c index fcf64a7b76..dc3c955302 100644 --- a/samples/wasi-threads/wasm-apps/no_pthread.c +++ b/samples/wasi-threads/wasm-apps/no_pthread.c @@ -33,7 +33,7 @@ __wasi_thread_start_C(int thread_id, int *start_arg) data->value += 8; printf("Updated value: %d\n", data->value); - data->th_ready = 1; + __atomic_store_n(&data->th_ready, 1, __ATOMIC_SEQ_CST); __builtin_wasm_memory_atomic_notify(&data->th_ready, 1); } diff --git a/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh b/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh index cd3fe18752..b4f89c7fcf 100755 --- a/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh +++ b/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh @@ -10,6 +10,7 @@ readonly TARGET=$2 readonly WORK_DIR=$PWD readonly PLATFORM=$(uname -s | tr A-Z a-z) +readonly WAMR_DIR="${WORK_DIR}/../../../.." readonly IWASM_CMD="${WORK_DIR}/../../../../product-mini/platforms/${PLATFORM}/build/iwasm" readonly WAMRC_CMD="${WORK_DIR}/../../../../wamr-compiler/build/wamrc" @@ -21,7 +22,8 @@ if [[ $MODE != "aot" ]];then -t \ tests/c/testsuite/ \ tests/assemblyscript/testsuite/ \ - tests/proposals/wasi-threads/ + tests/proposals/wasi-threads/ \ + ${WAMR_DIR}/core/iwasm/libraries/wasi-threads/test/ exit_code=${PIPESTATUS[0]} deactivate else From 4b2ae05c372763d641f545376b508441e2349907 Mon Sep 17 00:00:00 2001 From: eloparco Date: Tue, 21 Feb 2023 00:45:25 +0000 Subject: [PATCH 2/7] feat(wasi-threads): add test execution in ci --- .../compilation_on_android_ubuntu.yml | 34 +++++++++++++++++++ .gitignore | 1 + .../wasi-threads/test/global_atomic.c | 2 +- .../test/update_shared_data_and_alloc_heap.c | 2 +- tests/wamr-test-suites/test_wamr.sh | 3 ++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/.github/workflows/compilation_on_android_ubuntu.yml b/.github/workflows/compilation_on_android_ubuntu.yml index 81245307f7..245ac2a029 100644 --- a/.github/workflows/compilation_on_android_ubuntu.yml +++ b/.github/workflows/compilation_on_android_ubuntu.yml @@ -457,6 +457,10 @@ jobs: $THREADS_TEST_OPTIONS, $WASI_TEST_OPTIONS, ] + wasi_sdk_release: + [ + "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-19/wasi-sdk-19.0-linux.tar.gz", + ] llvm_cache_key: ["${{ needs.build_llvm_libraries_on_ubuntu_2004.outputs.cache_key }}"] exclude: @@ -493,6 +497,31 @@ jobs: - name: checkout uses: actions/checkout@v3 + - name: download and install wasi-sdk + if: matrix.test_option == '$WASI_TEST_OPTIONS' + run: | + cd /opt + sudo wget ${{ matrix.wasi_sdk_release }} + sudo tar -xzf wasi-sdk-*.tar.gz + sudo mv wasi-sdk-19.0 wasi-sdk + + - name: build wasi-libc (needed for wasi-threads) + if: matrix.test_option == '$WASI_TEST_OPTIONS' + run: | + mkdir wasi-libc + cd wasi-libc + git init + # "Rename thread_spawn import" commit on main branch + git fetch https://github.com/WebAssembly/wasi-libc \ + 8f5275796a82f8ecfd0833a4f3f444fa37ed4546 + git checkout FETCH_HEAD + make \ + AR=/opt/wasi-sdk/bin/llvm-ar \ + NM=/opt/wasi-sdk/bin/llvm-nm \ + CC=/opt/wasi-sdk/bin/clang \ + THREAD_MODEL=posix + working-directory: core/deps + - name: set env variable(if llvm are used) if: matrix.running_mode == 'aot' || matrix.running_mode == 'jit' || matrix.running_mode == 'multi-tier-jit' run: echo "USE_LLVM=true" >> $GITHUB_ENV @@ -526,6 +555,11 @@ jobs: if: matrix.running_mode == 'aot' && matrix.test_option == '$WASI_TEST_OPTIONS' run: sudo apt-get update && sudo apt install -y jq + - name: Build WASI thread tests + if: matrix.test_option == '$WASI_TEST_OPTIONS' + run: WASI_SYSROOT=../../../../../core/deps/wasi-libc/sysroot bash build.sh + working-directory: ./core/iwasm/libraries/wasi-threads/test/ + - name: run tests timeout-minutes: 10 run: ./test_wamr.sh ${{ matrix.test_option }} -t ${{ matrix.running_mode }} diff --git a/.gitignore b/.gitignore index 8b4dce9b02..4013677abc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ core/deps/** core/shared/mem-alloc/tlsf core/app-framework/wgl +core/iwasm/libraries/wasi-threads/test/*.wasm wamr-sdk/out/ wamr-sdk/runtime/build_runtime_sdk/ diff --git a/core/iwasm/libraries/wasi-threads/test/global_atomic.c b/core/iwasm/libraries/wasi-threads/test/global_atomic.c index 9a38b7974a..a38e753640 100644 --- a/core/iwasm/libraries/wasi-threads/test/global_atomic.c +++ b/core/iwasm/libraries/wasi-threads/test/global_atomic.c @@ -15,7 +15,7 @@ #include "wasi_thread_start.h" enum CONSTANTS { - NUM_THREADS = 5, + NUM_THREADS = 4, NUM_ITER = 1000, SECOND = 1000 * 1000 * 1000, /* 1 second */ TIMEOUT = 10LL * SECOND diff --git a/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c b/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c index ee17c20082..75acd1ec74 100644 --- a/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c +++ b/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c @@ -15,7 +15,7 @@ #include "wasi_thread_start.h" enum CONSTANTS { - NUM_THREADS = 5, + NUM_THREADS = 4, NUM_ITER = 30, SECOND = 1000 * 1000 * 1000, /* 1 second */ TIMEOUT = 10LL * SECOND diff --git a/tests/wamr-test-suites/test_wamr.sh b/tests/wamr-test-suites/test_wamr.sh index cf0bc970b2..5ef58b71f5 100755 --- a/tests/wamr-test-suites/test_wamr.sh +++ b/tests/wamr-test-suites/test_wamr.sh @@ -201,6 +201,7 @@ readonly CLASSIC_INTERP_COMPILE_FLAGS="\ -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0 \ -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=0 \ -DWAMR_BUILD_SPEC_TEST=1 \ + -DWAMR_BUILD_LIB_WASI_THREADS=1 \ -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE}" readonly FAST_INTERP_COMPILE_FLAGS="\ @@ -208,6 +209,7 @@ readonly FAST_INTERP_COMPILE_FLAGS="\ -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1 \ -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=0 \ -DWAMR_BUILD_SPEC_TEST=1 \ + -DWAMR_BUILD_LIB_WASI_THREADS=1 \ -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE}" # jit: report linking error if set COLLECT_CODE_COVERAGE, @@ -233,6 +235,7 @@ readonly AOT_COMPILE_FLAGS="\ -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_FAST_INTERP=0 \ -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=1 \ -DWAMR_BUILD_SPEC_TEST=1 \ + -DWAMR_BUILD_LIB_WASI_THREADS=1 \ -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE}" readonly FAST_JIT_COMPILE_FLAGS="\ From f8121f8d41a3e433d6fbf66cf1a60b998d5589fb Mon Sep 17 00:00:00 2001 From: eloparco Date: Sun, 26 Feb 2023 17:33:12 +0000 Subject: [PATCH 3/7] feat: add tests for thread termination --- .../libraries/wasi-threads/test/common.h | 126 ++++++++++++++++++ .../libraries/wasi-threads/test/global_lock.c | 11 +- .../wasi-threads/test/main_proc_exit_busy.c | 16 +++ .../test/main_proc_exit_busy.json | 3 + .../wasi-threads/test/main_proc_exit_sleep.c | 16 +++ .../test/main_proc_exit_sleep.json | 3 + .../wasi-threads/test/main_proc_exit_wait.c | 16 +++ .../test/main_proc_exit_wait.json | 3 + .../wasi-threads/test/main_trap_busy.c | 16 +++ .../wasi-threads/test/main_trap_busy.json | 3 + .../wasi-threads/test/main_trap_sleep.c | 16 +++ .../wasi-threads/test/main_trap_sleep.json | 3 + .../wasi-threads/test/main_trap_wait.c | 16 +++ .../wasi-threads/test/main_trap_wait.json | 3 + .../test/nonmain_proc_exit_busy.c | 16 +++ .../test/nonmain_proc_exit_busy.json | 3 + .../test/nonmain_proc_exit_sleep.c | 16 +++ .../test/nonmain_proc_exit_sleep.json | 3 + .../test/nonmain_proc_exit_wait.c | 16 +++ .../test/nonmain_proc_exit_wait.json | 3 + .../wasi-threads/test/nonmain_trap_busy.c | 16 +++ .../wasi-threads/test/nonmain_trap_busy.json | 3 + .../wasi-threads/test/nonmain_trap_sleep.c | 16 +++ .../wasi-threads/test/nonmain_trap_sleep.json | 3 + .../wasi-threads/test/nonmain_trap_wait.c | 16 +++ .../wasi-threads/test/nonmain_trap_wait.json | 3 + .../wasi-threads/test/spawn_multiple_times.c | 5 +- tests/wamr-test-suites/test_wamr.sh | 3 - 28 files changed, 357 insertions(+), 16 deletions(-) create mode 100644 core/iwasm/libraries/wasi-threads/test/common.h create mode 100644 core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.c create mode 100644 core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.json create mode 100644 core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.c create mode 100644 core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.json create mode 100644 core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.c create mode 100644 core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.json create mode 100644 core/iwasm/libraries/wasi-threads/test/main_trap_busy.c create mode 100644 core/iwasm/libraries/wasi-threads/test/main_trap_busy.json create mode 100644 core/iwasm/libraries/wasi-threads/test/main_trap_sleep.c create mode 100644 core/iwasm/libraries/wasi-threads/test/main_trap_sleep.json create mode 100644 core/iwasm/libraries/wasi-threads/test/main_trap_wait.c create mode 100644 core/iwasm/libraries/wasi-threads/test/main_trap_wait.json create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.c create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.json create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.c create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.json create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.c create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.json create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.c create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.json create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.c create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.json create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.c create mode 100644 core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.json diff --git a/core/iwasm/libraries/wasi-threads/test/common.h b/core/iwasm/libraries/wasi-threads/test/common.h new file mode 100644 index 0000000000..d8db9d81a4 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/common.h @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2022 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include + +#include "wasi_thread_start.h" + +typedef enum { + BLOCKING_TASK_BUSY_WAIT, + BLOCKING_TASK_ATOMIC_WAIT, + BLOCKING_TASK_POLL_ONEOFF +} blocking_task_type_t; + +/* Parameter to change test behavior */ +static bool termination_by_trap; +static bool termination_in_main_thread; +static blocking_task_type_t blocking_task_type; + +#define TIMEOUT_SECONDS 10ll +#define NUM_THREADS 3 +static sem_t sem; + +typedef struct { + start_args_t base; + bool throw_exception; +} shared_t; + +void +run_long_task() +{ + if (blocking_task_type == BLOCKING_TASK_BUSY_WAIT) { + for (int i = 0; i < TIMEOUT_SECONDS; i++) + sleep(1); + } + else if (blocking_task_type == BLOCKING_TASK_ATOMIC_WAIT) { + __builtin_wasm_memory_atomic_wait32( + 0, 0, TIMEOUT_SECONDS * 1000 * 1000 * 1000); + } + else { + sleep(TIMEOUT_SECONDS); + } +} + +void +start_job() +{ + sem_post(&sem); + run_long_task(); /* Wait to be interrupted */ + assert(false && "Unreachable"); +} + +void +terminate_process() +{ + /* Wait for all other threads (including main thread) to be ready */ + printf("Waiting before terminating\n"); + for (int i = 0; i < NUM_THREADS; i++) + sem_wait(&sem); + + printf("Force termination\n"); + if (termination_by_trap) + __builtin_trap(); + else + __wasi_proc_exit(33); +} + +void +__wasi_thread_start_C(int thread_id, int *start_arg) +{ + shared_t *data = (shared_t *)start_arg; + + if (data->throw_exception) { + terminate_process(); + } + else { + printf("Thread running\n"); + start_job(); + } +} + +void +test_termination(bool trap, bool main, blocking_task_type_t task_type) +{ + termination_by_trap = trap; + termination_in_main_thread = main; + blocking_task_type = task_type; + + int thread_id = -1, i; + shared_t data[NUM_THREADS] = { 0 }; + assert(sem_init(&sem, 0, 0) == 0 && "Failed to init semaphore"); + + for (i = 0; i < NUM_THREADS; i++) { + /* No graceful memory free to simplify the test */ + assert(start_args_init(&data[i].base) + && "Failed to allocate thread's stack"); + } + + /* Create a thread that forces termination through trap or `proc_exit` */ + data[0].throw_exception = !termination_in_main_thread; + thread_id = __wasi_thread_spawn(&data[0]); + assert(thread_id > 0 && "Failed to create thread"); + + /* Create two additional threads to test exception propagation */ + data[1].throw_exception = false; + thread_id = __wasi_thread_spawn(&data[1]); + assert(thread_id > 0 && "Failed to create thread"); + data[2].throw_exception = false; + thread_id = __wasi_thread_spawn(&data[2]); + assert(thread_id > 0 && "Failed to create thread"); + + if (termination_in_main_thread) { + printf("Force termination (main thread)\n"); + terminate_process(); + } + else { + printf("Main thread running\n"); + start_job(); + } +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/global_lock.c b/core/iwasm/libraries/wasi-threads/test/global_lock.c index 90d8b6f07b..64140c1348 100644 --- a/core/iwasm/libraries/wasi-threads/test/global_lock.c +++ b/core/iwasm/libraries/wasi-threads/test/global_lock.c @@ -51,10 +51,7 @@ main(int argc, char **argv) shared_t data[NUM_THREADS] = { 0 }; int thread_ids[NUM_THREADS]; - if (pthread_mutex_init(&mutex, NULL) != 0) { - printf("Failed to init mutex.\n"); - return EXIT_FAILURE; - } + assert(pthread_mutex_init(&mutex, NULL) == 0 && "Failed to init mutex"); for (int i = 0; i < NUM_THREADS; i++) { assert(start_args_init(&data[i].base)); @@ -76,10 +73,6 @@ main(int argc, char **argv) assert(g_count == (NUM_THREADS * NUM_ITER) && "Global count not updated correctly"); - if (pthread_mutex_destroy(&mutex) != 0) { - printf("Failed to init mutex.\n"); - return EXIT_FAILURE; - } - + assert(pthread_mutex_destroy(&mutex) == 0 && "Failed to init mutex"); return EXIT_SUCCESS; } diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.c b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.c new file mode 100644 index 0000000000..19d3ec2561 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(false, true, BLOCKING_TASK_BUSY_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.json b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.json new file mode 100644 index 0000000000..5370f66701 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.json @@ -0,0 +1,3 @@ +{ + "exit_code": 33 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.c b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.c new file mode 100644 index 0000000000..a667e91227 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(false, true, BLOCKING_TASK_POLL_ONEOFF); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.json b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.json new file mode 100644 index 0000000000..5370f66701 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.json @@ -0,0 +1,3 @@ +{ + "exit_code": 33 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.c b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.c new file mode 100644 index 0000000000..dc8615adb0 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(false, true, BLOCKING_TASK_ATOMIC_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.json b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.json new file mode 100644 index 0000000000..5370f66701 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.json @@ -0,0 +1,3 @@ +{ + "exit_code": 33 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_busy.c b/core/iwasm/libraries/wasi-threads/test/main_trap_busy.c new file mode 100644 index 0000000000..bb0ac8fa00 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_trap_busy.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(true, true, BLOCKING_TASK_BUSY_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_busy.json b/core/iwasm/libraries/wasi-threads/test/main_trap_busy.json new file mode 100644 index 0000000000..07689a1055 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_trap_busy.json @@ -0,0 +1,3 @@ +{ + "exit_code": 1 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.c b/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.c new file mode 100644 index 0000000000..a2c248882f --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(true, true, BLOCKING_TASK_POLL_ONEOFF); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.json b/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.json new file mode 100644 index 0000000000..07689a1055 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.json @@ -0,0 +1,3 @@ +{ + "exit_code": 1 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_wait.c b/core/iwasm/libraries/wasi-threads/test/main_trap_wait.c new file mode 100644 index 0000000000..0904f34bb0 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_trap_wait.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(true, true, BLOCKING_TASK_ATOMIC_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_wait.json b/core/iwasm/libraries/wasi-threads/test/main_trap_wait.json new file mode 100644 index 0000000000..07689a1055 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/main_trap_wait.json @@ -0,0 +1,3 @@ +{ + "exit_code": 1 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.c b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.c new file mode 100644 index 0000000000..71fdcb817c --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(false, false, BLOCKING_TASK_BUSY_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.json b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.json new file mode 100644 index 0000000000..5370f66701 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.json @@ -0,0 +1,3 @@ +{ + "exit_code": 33 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.c b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.c new file mode 100644 index 0000000000..14352cf417 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(false, false, BLOCKING_TASK_POLL_ONEOFF); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.json b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.json new file mode 100644 index 0000000000..5370f66701 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.json @@ -0,0 +1,3 @@ +{ + "exit_code": 33 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.c b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.c new file mode 100644 index 0000000000..0963aa02fa --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(false, false, BLOCKING_TASK_ATOMIC_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.json b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.json new file mode 100644 index 0000000000..5370f66701 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.json @@ -0,0 +1,3 @@ +{ + "exit_code": 33 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.c b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.c new file mode 100644 index 0000000000..b3e3af7dc0 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(true, false, BLOCKING_TASK_BUSY_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.json b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.json new file mode 100644 index 0000000000..07689a1055 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.json @@ -0,0 +1,3 @@ +{ + "exit_code": 1 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.c b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.c new file mode 100644 index 0000000000..a68ae8be50 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(true, false, BLOCKING_TASK_POLL_ONEOFF); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.json b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.json new file mode 100644 index 0000000000..07689a1055 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.json @@ -0,0 +1,3 @@ +{ + "exit_code": 1 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.c b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.c new file mode 100644 index 0000000000..52c684a518 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.c @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef __wasi__ +#error This example only compiles to WASM/WASI target +#endif + +#include "common.h" + +int +main(int argc, char **argv) +{ + test_termination(true, false, BLOCKING_TASK_ATOMIC_WAIT); +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.json b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.json new file mode 100644 index 0000000000..07689a1055 --- /dev/null +++ b/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.json @@ -0,0 +1,3 @@ +{ + "exit_code": 1 +} \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c b/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c index adaa18276b..24664c4705 100644 --- a/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c +++ b/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c @@ -43,10 +43,7 @@ int main(int argc, char **argv) { shared_t data = { 0 }; - if (!start_args_init(&data.base)) { - printf("Stack allocation for thread failed\n"); - return EXIT_FAILURE; - } + assert(start_args_init(&data.base) && "Stack allocation for thread failed"); for (int i = 0; i < NUM_ITER; i++) { data.th_done = 0; diff --git a/tests/wamr-test-suites/test_wamr.sh b/tests/wamr-test-suites/test_wamr.sh index 5ef58b71f5..cf0bc970b2 100755 --- a/tests/wamr-test-suites/test_wamr.sh +++ b/tests/wamr-test-suites/test_wamr.sh @@ -201,7 +201,6 @@ readonly CLASSIC_INTERP_COMPILE_FLAGS="\ -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0 \ -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=0 \ -DWAMR_BUILD_SPEC_TEST=1 \ - -DWAMR_BUILD_LIB_WASI_THREADS=1 \ -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE}" readonly FAST_INTERP_COMPILE_FLAGS="\ @@ -209,7 +208,6 @@ readonly FAST_INTERP_COMPILE_FLAGS="\ -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1 \ -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=0 \ -DWAMR_BUILD_SPEC_TEST=1 \ - -DWAMR_BUILD_LIB_WASI_THREADS=1 \ -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE}" # jit: report linking error if set COLLECT_CODE_COVERAGE, @@ -235,7 +233,6 @@ readonly AOT_COMPILE_FLAGS="\ -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_FAST_INTERP=0 \ -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_AOT=1 \ -DWAMR_BUILD_SPEC_TEST=1 \ - -DWAMR_BUILD_LIB_WASI_THREADS=1 \ -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE}" readonly FAST_JIT_COMPILE_FLAGS="\ From 1ccc5c5e8e00aa501817211d652e969286dd78ee Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 1 Mar 2023 01:18:39 +0000 Subject: [PATCH 4/7] fix: remove prints from different threads causing data races --- core/iwasm/libraries/wasi-threads/test/common.h | 5 ----- core/iwasm/libraries/wasi-threads/test/global_lock.c | 2 +- .../wasi-threads/test/update_shared_data_and_alloc_heap.c | 2 +- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/core/iwasm/libraries/wasi-threads/test/common.h b/core/iwasm/libraries/wasi-threads/test/common.h index d8db9d81a4..86188cc3b2 100644 --- a/core/iwasm/libraries/wasi-threads/test/common.h +++ b/core/iwasm/libraries/wasi-threads/test/common.h @@ -60,11 +60,9 @@ void terminate_process() { /* Wait for all other threads (including main thread) to be ready */ - printf("Waiting before terminating\n"); for (int i = 0; i < NUM_THREADS; i++) sem_wait(&sem); - printf("Force termination\n"); if (termination_by_trap) __builtin_trap(); else @@ -80,7 +78,6 @@ __wasi_thread_start_C(int thread_id, int *start_arg) terminate_process(); } else { - printf("Thread running\n"); start_job(); } } @@ -116,11 +113,9 @@ test_termination(bool trap, bool main, blocking_task_type_t task_type) assert(thread_id > 0 && "Failed to create thread"); if (termination_in_main_thread) { - printf("Force termination (main thread)\n"); terminate_process(); } else { - printf("Main thread running\n"); start_job(); } } \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/global_lock.c b/core/iwasm/libraries/wasi-threads/test/global_lock.c index 64140c1348..282bec71c2 100644 --- a/core/iwasm/libraries/wasi-threads/test/global_lock.c +++ b/core/iwasm/libraries/wasi-threads/test/global_lock.c @@ -73,6 +73,6 @@ main(int argc, char **argv) assert(g_count == (NUM_THREADS * NUM_ITER) && "Global count not updated correctly"); - assert(pthread_mutex_destroy(&mutex) == 0 && "Failed to init mutex"); + assert(pthread_mutex_destroy(&mutex) == 0 && "Failed to destroy mutex"); return EXIT_SUCCESS; } diff --git a/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c b/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c index 75acd1ec74..b8cf564958 100644 --- a/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c +++ b/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c @@ -56,7 +56,7 @@ main(int argc, char **argv) for (int i = 0; i < NUM_THREADS; i++) { assert(start_args_init(&data[i].base) && "Stack allocation for thread failed"); - data[i].count = count; + __atomic_store_n(&data[i].count, count, __ATOMIC_SEQ_CST); data[i].iteration = i; thread_ids[i] = __wasi_thread_spawn(&data[i]); From e6ddf59aa18b39a1ba1326b7494ad4698d192475 Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 1 Mar 2023 23:06:09 +0000 Subject: [PATCH 5/7] chore: move wasi thread test folder --- .github/workflows/compilation_on_android_ubuntu.yml | 2 +- .gitignore | 4 +++- .../{wasi-threads => lib-wasi-threads}/test/build.sh | 0 .../{wasi-threads => lib-wasi-threads}/test/common.h | 0 .../test/create_threads_until_limit.c | 0 .../{wasi-threads => lib-wasi-threads}/test/global_atomic.c | 0 .../{wasi-threads => lib-wasi-threads}/test/global_lock.c | 0 .../test/main_proc_exit_busy.c | 0 .../test/main_proc_exit_busy.json | 0 .../test/main_proc_exit_sleep.c | 0 .../test/main_proc_exit_sleep.json | 0 .../test/main_proc_exit_wait.c | 0 .../test/main_proc_exit_wait.json | 0 .../{wasi-threads => lib-wasi-threads}/test/main_trap_busy.c | 0 .../test/main_trap_busy.json | 0 .../{wasi-threads => lib-wasi-threads}/test/main_trap_sleep.c | 0 .../test/main_trap_sleep.json | 0 .../{wasi-threads => lib-wasi-threads}/test/main_trap_wait.c | 0 .../test/main_trap_wait.json | 0 .../test/nonmain_proc_exit_busy.c | 0 .../test/nonmain_proc_exit_busy.json | 0 .../test/nonmain_proc_exit_sleep.c | 0 .../test/nonmain_proc_exit_sleep.json | 0 .../test/nonmain_proc_exit_wait.c | 0 .../test/nonmain_proc_exit_wait.json | 0 .../test/nonmain_trap_busy.c | 0 .../test/nonmain_trap_busy.json | 0 .../test/nonmain_trap_sleep.c | 0 .../test/nonmain_trap_sleep.json | 0 .../test/nonmain_trap_wait.c | 0 .../test/nonmain_trap_wait.json | 0 .../test/spawn_multiple_times.c | 0 .../test/update_shared_data_and_alloc_heap.c | 0 tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh | 2 +- 34 files changed, 5 insertions(+), 3 deletions(-) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/build.sh (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/common.h (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/create_threads_until_limit.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/global_atomic.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/global_lock.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_proc_exit_busy.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_proc_exit_busy.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_proc_exit_sleep.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_proc_exit_sleep.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_proc_exit_wait.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_proc_exit_wait.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_trap_busy.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_trap_busy.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_trap_sleep.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_trap_sleep.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_trap_wait.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/main_trap_wait.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_proc_exit_busy.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_proc_exit_busy.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_proc_exit_sleep.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_proc_exit_sleep.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_proc_exit_wait.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_proc_exit_wait.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_trap_busy.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_trap_busy.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_trap_sleep.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_trap_sleep.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_trap_wait.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/nonmain_trap_wait.json (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/spawn_multiple_times.c (100%) rename core/iwasm/libraries/{wasi-threads => lib-wasi-threads}/test/update_shared_data_and_alloc_heap.c (100%) diff --git a/.github/workflows/compilation_on_android_ubuntu.yml b/.github/workflows/compilation_on_android_ubuntu.yml index 245ac2a029..9d52cac4f0 100644 --- a/.github/workflows/compilation_on_android_ubuntu.yml +++ b/.github/workflows/compilation_on_android_ubuntu.yml @@ -558,7 +558,7 @@ jobs: - name: Build WASI thread tests if: matrix.test_option == '$WASI_TEST_OPTIONS' run: WASI_SYSROOT=../../../../../core/deps/wasi-libc/sysroot bash build.sh - working-directory: ./core/iwasm/libraries/wasi-threads/test/ + working-directory: ./core/iwasm/libraries/lib-wasi-threads/test/ - name: run tests timeout-minutes: 10 diff --git a/.gitignore b/.gitignore index 4013677abc..719972ef8b 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,7 @@ core/deps/** core/shared/mem-alloc/tlsf core/app-framework/wgl -core/iwasm/libraries/wasi-threads/test/*.wasm +core/iwasm/libraries/lib-wasi-threads/test/*.wasm wamr-sdk/out/ wamr-sdk/runtime/build_runtime_sdk/ @@ -36,3 +36,5 @@ tests/benchmarks/coremark/coremark* samples/workload/include/** !samples/workload/include/.gitkeep + +# core/iwasm/libraries/wasi-threads \ No newline at end of file diff --git a/core/iwasm/libraries/wasi-threads/test/build.sh b/core/iwasm/libraries/lib-wasi-threads/test/build.sh similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/build.sh rename to core/iwasm/libraries/lib-wasi-threads/test/build.sh diff --git a/core/iwasm/libraries/wasi-threads/test/common.h b/core/iwasm/libraries/lib-wasi-threads/test/common.h similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/common.h rename to core/iwasm/libraries/lib-wasi-threads/test/common.h diff --git a/core/iwasm/libraries/wasi-threads/test/create_threads_until_limit.c b/core/iwasm/libraries/lib-wasi-threads/test/create_threads_until_limit.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/create_threads_until_limit.c rename to core/iwasm/libraries/lib-wasi-threads/test/create_threads_until_limit.c diff --git a/core/iwasm/libraries/wasi-threads/test/global_atomic.c b/core/iwasm/libraries/lib-wasi-threads/test/global_atomic.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/global_atomic.c rename to core/iwasm/libraries/lib-wasi-threads/test/global_atomic.c diff --git a/core/iwasm/libraries/wasi-threads/test/global_lock.c b/core/iwasm/libraries/lib-wasi-threads/test/global_lock.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/global_lock.c rename to core/iwasm/libraries/lib-wasi-threads/test/global_lock.c diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.c b/core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_busy.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.c rename to core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_busy.c diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.json b/core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_busy.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_proc_exit_busy.json rename to core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_busy.json diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.c b/core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_sleep.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.c rename to core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_sleep.c diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.json b/core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_sleep.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_proc_exit_sleep.json rename to core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_sleep.json diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.c b/core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_wait.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.c rename to core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_wait.c diff --git a/core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.json b/core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_wait.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_proc_exit_wait.json rename to core/iwasm/libraries/lib-wasi-threads/test/main_proc_exit_wait.json diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_busy.c b/core/iwasm/libraries/lib-wasi-threads/test/main_trap_busy.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_trap_busy.c rename to core/iwasm/libraries/lib-wasi-threads/test/main_trap_busy.c diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_busy.json b/core/iwasm/libraries/lib-wasi-threads/test/main_trap_busy.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_trap_busy.json rename to core/iwasm/libraries/lib-wasi-threads/test/main_trap_busy.json diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.c b/core/iwasm/libraries/lib-wasi-threads/test/main_trap_sleep.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_trap_sleep.c rename to core/iwasm/libraries/lib-wasi-threads/test/main_trap_sleep.c diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_sleep.json b/core/iwasm/libraries/lib-wasi-threads/test/main_trap_sleep.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_trap_sleep.json rename to core/iwasm/libraries/lib-wasi-threads/test/main_trap_sleep.json diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_wait.c b/core/iwasm/libraries/lib-wasi-threads/test/main_trap_wait.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_trap_wait.c rename to core/iwasm/libraries/lib-wasi-threads/test/main_trap_wait.c diff --git a/core/iwasm/libraries/wasi-threads/test/main_trap_wait.json b/core/iwasm/libraries/lib-wasi-threads/test/main_trap_wait.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/main_trap_wait.json rename to core/iwasm/libraries/lib-wasi-threads/test/main_trap_wait.json diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.c b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_busy.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.c rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_busy.c diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.json b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_busy.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_busy.json rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_busy.json diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.c b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_sleep.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.c rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_sleep.c diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.json b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_sleep.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_sleep.json rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_sleep.json diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.c b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_wait.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.c rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_wait.c diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.json b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_wait.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_proc_exit_wait.json rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_proc_exit_wait.json diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.c b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_busy.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.c rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_busy.c diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.json b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_busy.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_trap_busy.json rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_busy.json diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.c b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_sleep.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.c rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_sleep.c diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.json b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_sleep.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_trap_sleep.json rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_sleep.json diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.c b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_wait.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.c rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_wait.c diff --git a/core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.json b/core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_wait.json similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/nonmain_trap_wait.json rename to core/iwasm/libraries/lib-wasi-threads/test/nonmain_trap_wait.json diff --git a/core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c b/core/iwasm/libraries/lib-wasi-threads/test/spawn_multiple_times.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/spawn_multiple_times.c rename to core/iwasm/libraries/lib-wasi-threads/test/spawn_multiple_times.c diff --git a/core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c b/core/iwasm/libraries/lib-wasi-threads/test/update_shared_data_and_alloc_heap.c similarity index 100% rename from core/iwasm/libraries/wasi-threads/test/update_shared_data_and_alloc_heap.c rename to core/iwasm/libraries/lib-wasi-threads/test/update_shared_data_and_alloc_heap.c diff --git a/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh b/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh index b4f89c7fcf..17c282435e 100755 --- a/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh +++ b/tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh @@ -23,7 +23,7 @@ if [[ $MODE != "aot" ]];then tests/c/testsuite/ \ tests/assemblyscript/testsuite/ \ tests/proposals/wasi-threads/ \ - ${WAMR_DIR}/core/iwasm/libraries/wasi-threads/test/ + ${WAMR_DIR}/core/iwasm/libraries/lib-wasi-threads/test/ exit_code=${PIPESTATUS[0]} deactivate else From 3dc9a6f64a46cd9ec51d97f8b089830251dff024 Mon Sep 17 00:00:00 2001 From: eloparco Date: Fri, 3 Mar 2023 01:45:37 +0000 Subject: [PATCH 6/7] fix: use barrier instead of semaphore in thread termination tests --- .../libraries/lib-wasi-threads/test/common.h | 19 ++++++++++--------- .../wasm-apps/thread_termination.c | 18 +++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/core/iwasm/libraries/lib-wasi-threads/test/common.h b/core/iwasm/libraries/lib-wasi-threads/test/common.h index 86188cc3b2..a531e39dc4 100644 --- a/core/iwasm/libraries/lib-wasi-threads/test/common.h +++ b/core/iwasm/libraries/lib-wasi-threads/test/common.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -25,7 +25,7 @@ static blocking_task_type_t blocking_task_type; #define TIMEOUT_SECONDS 10ll #define NUM_THREADS 3 -static sem_t sem; +static pthread_barrier_t barrier; typedef struct { start_args_t base; @@ -51,17 +51,17 @@ run_long_task() void start_job() { - sem_post(&sem); - run_long_task(); /* Wait to be interrupted */ - assert(false && "Unreachable"); + /* Wait for all threads (including the main thread) to be ready */ + pthread_barrier_wait(&barrier); + run_long_task(); /* Task to be interrupted */ + assert(false && "Thread termination test failed"); } void terminate_process() { - /* Wait for all other threads (including main thread) to be ready */ - for (int i = 0; i < NUM_THREADS; i++) - sem_wait(&sem); + /* Wait for all threads (including the main thread) to be ready */ + pthread_barrier_wait(&barrier); if (termination_by_trap) __builtin_trap(); @@ -91,7 +91,8 @@ test_termination(bool trap, bool main, blocking_task_type_t task_type) int thread_id = -1, i; shared_t data[NUM_THREADS] = { 0 }; - assert(sem_init(&sem, 0, 0) == 0 && "Failed to init semaphore"); + assert(pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) == 0 + && "Failed to init barrier"); for (i = 0; i < NUM_THREADS; i++) { /* No graceful memory free to simplify the test */ diff --git a/samples/wasi-threads/wasm-apps/thread_termination.c b/samples/wasi-threads/wasm-apps/thread_termination.c index 355bb3f453..9f5cf1fe89 100644 --- a/samples/wasi-threads/wasm-apps/thread_termination.c +++ b/samples/wasi-threads/wasm-apps/thread_termination.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include @@ -26,7 +26,7 @@ #define TIMEOUT_SECONDS 10 #define NUM_THREADS 3 -static sem_t sem; +static pthread_barrier_t barrier; typedef struct { start_args_t base; @@ -49,9 +49,10 @@ run_long_task() void start_job() { - sem_post(&sem); - run_long_task(); /* Wait to be interrupted */ - assert(false && "Unreachable"); + /* Wait for all threads (including the main thread) to be ready */ + pthread_barrier_wait(&barrier); + run_long_task(); /* Task to be interrupted */ + assert(false && "Thread termination test failed"); } void @@ -59,8 +60,7 @@ terminate_process() { /* Wait for all other threads (including main thread) to be ready */ printf("Waiting before terminating\n"); - for (int i = 0; i < NUM_THREADS; i++) - sem_wait(&sem); + pthread_barrier_wait(&barrier); printf("Force termination\n"); #if TEST_TERMINATION_BY_TRAP == 1 @@ -91,8 +91,8 @@ main(int argc, char **argv) int thread_id = -1, i; shared_t data[NUM_THREADS] = { 0 }; - if (sem_init(&sem, 0, 0) != 0) { - printf("Failed to init semaphore\n"); + if (pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) != 0) { + printf("Failed to init barrier\n"); return EXIT_FAILURE; } From 7aafc8d574f8f7562b927950b8df2f2341d02574 Mon Sep 17 00:00:00 2001 From: eloparco Date: Fri, 3 Mar 2023 13:43:45 +0000 Subject: [PATCH 7/7] chore: add copyright --- core/iwasm/libraries/lib-wasi-threads/test/build.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/iwasm/libraries/lib-wasi-threads/test/build.sh b/core/iwasm/libraries/lib-wasi-threads/test/build.sh index 17ebeb1706..58ff59481b 100644 --- a/core/iwasm/libraries/lib-wasi-threads/test/build.sh +++ b/core/iwasm/libraries/lib-wasi-threads/test/build.sh @@ -1,5 +1,10 @@ #!/bin/bash +# +# Copyright (C) 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + CC=${CC:=/opt/wasi-sdk/bin/clang} WASI_SYSROOT=${WASI_SYSROOT:=~/dev/wasi-libc/sysroot} WAMR_DIR=../../../../..