mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-06 06:55:07 +00:00
Update wasi-libc version in CI and implement custom sync primitives (#2028)
Update wasi-libc version to resolve the hang issue when running wasi-threads cases. Implement custom sync primitives as a counterpart of `pthread_barrier_wait` to attempt to replace pthread sync primitives since they seem to cause data races when running with the thread sanitizer.
This commit is contained in:
parent
0faec7c96c
commit
0f73ce1076
|
@ -368,9 +368,9 @@ jobs:
|
|||
mkdir wasi-libc
|
||||
cd wasi-libc
|
||||
git init
|
||||
# "Rename thread_spawn import" commit on main branch
|
||||
# "Fix a_store operation in atomic.h" commit on main branch
|
||||
git fetch https://github.com/WebAssembly/wasi-libc \
|
||||
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
|
||||
1dfe5c302d1c5ab621f7abf04620fae92700fd22
|
||||
git checkout FETCH_HEAD
|
||||
make -j \
|
||||
AR=/opt/wasi-sdk/bin/llvm-ar \
|
||||
|
@ -532,9 +532,9 @@ jobs:
|
|||
mkdir wasi-libc
|
||||
cd wasi-libc
|
||||
git init
|
||||
# "Rename thread_spawn import" commit on main branch
|
||||
# "Fix a_store operation in atomic.h" commit on main branch
|
||||
git fetch https://github.com/WebAssembly/wasi-libc \
|
||||
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
|
||||
1dfe5c302d1c5ab621f7abf04620fae92700fd22
|
||||
git checkout FETCH_HEAD
|
||||
make -j \
|
||||
AR=/opt/wasi-sdk/bin/llvm-ar \
|
||||
|
|
4
.github/workflows/compilation_on_sgx.yml
vendored
4
.github/workflows/compilation_on_sgx.yml
vendored
|
@ -265,9 +265,9 @@ jobs:
|
|||
mkdir wasi-libc
|
||||
cd wasi-libc
|
||||
git init
|
||||
# "Rename thread_spawn import" commit on main branch
|
||||
# "Fix a_store operation in atomic.h" commit on main branch
|
||||
git fetch https://github.com/WebAssembly/wasi-libc \
|
||||
8f5275796a82f8ecfd0833a4f3f444fa37ed4546
|
||||
1dfe5c302d1c5ab621f7abf04620fae92700fd22
|
||||
git checkout FETCH_HEAD
|
||||
make \
|
||||
AR=/opt/wasi-sdk/bin/llvm-ar \
|
||||
|
|
|
@ -6,11 +6,16 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
|
||||
#if USE_CUSTOM_SYNC_PRIMITIVES != 0
|
||||
#include "sync_primitives.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "wasi_thread_start.h"
|
||||
|
||||
typedef enum {
|
||||
|
|
|
@ -11,7 +11,12 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#if USE_CUSTOM_SYNC_PRIMITIVES != 0
|
||||
#include "sync_primitives.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "wasi_thread_start.h"
|
||||
|
||||
|
|
91
core/iwasm/libraries/lib-wasi-threads/test/sync_primitives.h
Normal file
91
core/iwasm/libraries/lib-wasi-threads/test/sync_primitives.h
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/* Mutex */
|
||||
|
||||
typedef int pthread_mutex_t;
|
||||
|
||||
int
|
||||
pthread_mutex_init(pthread_mutex_t *mutex, void *unused)
|
||||
{
|
||||
*mutex = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
pthread_mutex_destroy(pthread_mutex_t *mutex)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool
|
||||
try_pthread_mutex_lock(pthread_mutex_t *mutex)
|
||||
{
|
||||
int expected = 0;
|
||||
return __atomic_compare_exchange_n(mutex, &expected, 1, false,
|
||||
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
|
||||
int
|
||||
pthread_mutex_lock(pthread_mutex_t *mutex)
|
||||
{
|
||||
while (!try_pthread_mutex_lock(mutex))
|
||||
__builtin_wasm_memory_atomic_wait32(mutex, 1, -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
pthread_mutex_unlock(pthread_mutex_t *mutex)
|
||||
{
|
||||
__atomic_store_n(mutex, 0, __ATOMIC_SEQ_CST);
|
||||
__builtin_wasm_memory_atomic_notify(mutex, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Barrier */
|
||||
|
||||
typedef struct {
|
||||
int count;
|
||||
int num_threads;
|
||||
int mutex;
|
||||
int ready;
|
||||
} pthread_barrier_t;
|
||||
|
||||
int
|
||||
pthread_barrier_init(pthread_barrier_t *barrier, void *unused, int num_threads)
|
||||
{
|
||||
barrier->count = 0;
|
||||
barrier->num_threads = num_threads;
|
||||
barrier->ready = 0;
|
||||
pthread_mutex_init(&barrier->mutex, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
pthread_barrier_wait(pthread_barrier_t *barrier)
|
||||
{
|
||||
bool no_wait = false;
|
||||
int count;
|
||||
|
||||
pthread_mutex_lock(&barrier->mutex);
|
||||
count = barrier->count++;
|
||||
if (barrier->count >= barrier->num_threads) {
|
||||
no_wait = true;
|
||||
barrier->count = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&barrier->mutex);
|
||||
|
||||
if (no_wait) {
|
||||
__atomic_store_n(&barrier->ready, 1, __ATOMIC_SEQ_CST);
|
||||
__builtin_wasm_memory_atomic_notify(&barrier->ready, count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
__builtin_wasm_memory_atomic_wait32(&barrier->ready, 0, -1);
|
||||
return 0;
|
||||
}
|
|
@ -11,8 +11,6 @@ $ cmake ..
|
|||
$ make
|
||||
...
|
||||
$ ./iwasm wasm-apps/no_pthread.wasm
|
||||
...
|
||||
$ ./iwasm wasm-apps/exception_propagation.wasm
|
||||
```
|
||||
|
||||
## Run samples in AOT mode
|
||||
|
|
|
@ -43,5 +43,4 @@ function (compile_sample SOURCE_FILE)
|
|||
)
|
||||
endfunction ()
|
||||
|
||||
compile_sample(no_pthread.c wasi_thread_start.S)
|
||||
compile_sample(thread_termination.c wasi_thread_start.S)
|
||||
compile_sample(no_pthread.c wasi_thread_start.S)
|
|
@ -1,142 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2022 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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "wasi_thread_start.h"
|
||||
|
||||
#define BUSY_WAIT 0
|
||||
#define ATOMIC_WAIT 1
|
||||
#define POLL_ONEOFF 2
|
||||
|
||||
/* Change parameters here to modify the sample behavior */
|
||||
#define TEST_TERMINATION_BY_TRAP 0 /* Otherwise `proc_exit` termination */
|
||||
#define TEST_TERMINATION_IN_MAIN_THREAD 1 /* Otherwise in spawn thread */
|
||||
#define LONG_TASK_IMPL ATOMIC_WAIT
|
||||
|
||||
#define TIMEOUT_SECONDS 10
|
||||
#define NUM_THREADS 3
|
||||
static pthread_barrier_t barrier;
|
||||
|
||||
typedef struct {
|
||||
start_args_t base;
|
||||
bool throw_exception;
|
||||
} shared_t;
|
||||
|
||||
void
|
||||
run_long_task()
|
||||
{
|
||||
#if LONG_TASK_IMPL == BUSY_WAIT
|
||||
for (int i = 0; i < TIMEOUT_SECONDS; i++)
|
||||
sleep(1);
|
||||
#elif LONG_TASK_IMPL == ATOMIC_WAIT
|
||||
__builtin_wasm_memory_atomic_wait32(0, 0, -1);
|
||||
#else
|
||||
sleep(TIMEOUT_SECONDS);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
start_job()
|
||||
{
|
||||
/* 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 */
|
||||
printf("Waiting before terminating\n");
|
||||
pthread_barrier_wait(&barrier);
|
||||
|
||||
printf("Force termination\n");
|
||||
#if TEST_TERMINATION_BY_TRAP == 1
|
||||
__builtin_trap();
|
||||
#else
|
||||
__wasi_proc_exit(33);
|
||||
#endif
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int thread_id = -1, i;
|
||||
shared_t data[NUM_THREADS] = { 0 };
|
||||
|
||||
if (pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) != 0) {
|
||||
printf("Failed to init barrier\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
/* No graceful memory free to simplify the example */
|
||||
if (!start_args_init(&data[i].base)) {
|
||||
printf("Failed to allocate thread's stack\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Create a thread that forces termination through trap or `proc_exit` */
|
||||
#if TEST_TERMINATION_IN_MAIN_THREAD == 1
|
||||
data[0].throw_exception = false;
|
||||
#else
|
||||
data[0].throw_exception = true;
|
||||
#endif
|
||||
thread_id = __wasi_thread_spawn(&data[0]);
|
||||
if (thread_id < 0) {
|
||||
printf("Failed to create thread: %d\n", thread_id);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* Create two additional threads to test exception propagation */
|
||||
data[1].throw_exception = false;
|
||||
thread_id = __wasi_thread_spawn(&data[1]);
|
||||
if (thread_id < 0) {
|
||||
printf("Failed to create thread: %d\n", thread_id);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
data[2].throw_exception = false;
|
||||
thread_id = __wasi_thread_spawn(&data[2]);
|
||||
if (thread_id < 0) {
|
||||
printf("Failed to create thread: %d\n", thread_id);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#if TEST_TERMINATION_IN_MAIN_THREAD == 1
|
||||
printf("Force termination (main thread)\n");
|
||||
terminate_process();
|
||||
#else /* TEST_TERMINATION_IN_MAIN_THREAD */
|
||||
printf("Main thread running\n");
|
||||
|
||||
start_job();
|
||||
#endif /* TEST_TERMINATION_IN_MAIN_THREAD */
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user