mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2024-10-16 13:02:32 +00:00
Fix malloc non-thread-safe usage in lib-wasi-threads test (#2022)
In the WASI thread test modified in this PR, malloc was used in multiple threads without a lock. But wasi-libc implementation of malloc is not thread-safe.
This commit is contained in:
parent
ff3887757e
commit
ea6cef7e51
|
@ -11,6 +11,7 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wasi_thread_start.h"
|
||||
|
||||
|
@ -29,6 +30,7 @@ typedef struct {
|
|||
int *pval;
|
||||
} shared_t;
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
int *vals[NUM_THREADS];
|
||||
|
||||
void
|
||||
|
@ -39,7 +41,9 @@ __wasi_thread_start_C(int thread_id, int *start_arg)
|
|||
for (int i = 0; i < NUM_ITER; i++)
|
||||
__atomic_fetch_add(data->count, 1, __ATOMIC_SEQ_CST);
|
||||
|
||||
pthread_mutex_lock(&mutex); /* malloc is not thread-safe in wasi-libc */
|
||||
vals[data->iteration] = malloc(sizeof(int));
|
||||
pthread_mutex_unlock(&mutex);
|
||||
*vals[data->iteration] = data->iteration;
|
||||
|
||||
__atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST);
|
||||
|
@ -53,6 +57,9 @@ main(int argc, char **argv)
|
|||
int thread_ids[NUM_THREADS];
|
||||
int *count = calloc(1, sizeof(int));
|
||||
|
||||
assert(count != NULL && "Failed to call calloc");
|
||||
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)
|
||||
&& "Stack allocation for thread failed");
|
||||
|
@ -82,5 +89,7 @@ main(int argc, char **argv)
|
|||
}
|
||||
|
||||
free(count);
|
||||
assert(pthread_mutex_destroy(&mutex) == 0 && "Failed to destroy mutex");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user