Add test for validating linear memory size updates (#2078)

This commit is contained in:
Marcin Kolny 2023-04-08 12:07:20 +01:00 committed by GitHub
parent 4ca4f7913b
commit a2d4744a2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 1 deletions

8
core/iwasm/libraries/lib-wasi-threads/test/build.sh Normal file → Executable file
View File

@ -12,6 +12,12 @@ WAMR_DIR=../../../../..
for test_c in *.c; do
test_wasm="$(basename $test_c .c).wasm"
if [ $test_wasm = "linear_memory_size_update.wasm" ]; then
thread_start_file=""
else
thread_start_file=$WAMR_DIR/samples/wasi-threads/wasm-apps/wasi_thread_start.S
fi
echo "Compiling $test_c to $test_wasm"
$CC \
-target wasm32-wasi-threads \
@ -24,6 +30,6 @@ for test_c in *.c; do
-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 \
$thread_start_file \
$test_c -o $test_wasm
done

View File

@ -0,0 +1,94 @@
/*
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdlib.h>
#include <pthread.h>
typedef enum {
APP_STARTED,
THREAD_STARTED,
MEMORY_ALLOCATED,
} app_state_t;
typedef struct {
pthread_cond_t cond;
pthread_mutex_t mutex;
app_state_t state;
char *data;
} context_t;
void
context_init(context_t *ctx)
{
pthread_cond_init(&ctx->cond, NULL);
pthread_mutex_init(&ctx->mutex, NULL);
ctx->state = APP_STARTED;
ctx->data = NULL;
}
void
context_destroy(context_t *ctx)
{
pthread_cond_destroy(&ctx->cond);
pthread_mutex_destroy(&ctx->mutex);
if (ctx->data) {
free(ctx->data);
}
}
void
context_set_state(context_t *ctx, app_state_t state)
{
pthread_mutex_lock(&ctx->mutex);
ctx->state = state;
pthread_mutex_unlock(&ctx->mutex);
pthread_cond_signal(&ctx->cond);
}
void
context_wait_for_state(context_t *ctx, app_state_t state)
{
pthread_mutex_lock(&ctx->mutex);
while (ctx->state != state) {
pthread_cond_wait(&ctx->cond, &ctx->mutex);
}
pthread_mutex_unlock(&ctx->mutex);
}
void *
fnc(void *p)
{
context_t *ctx = (context_t *)p;
context_set_state(ctx, THREAD_STARTED);
context_wait_for_state(ctx, MEMORY_ALLOCATED);
// trigger memory.copy
__builtin_memcpy(ctx->data + 512 * 1024, ctx->data + 1024, 1024);
return NULL;
}
int
main()
{
context_t ctx;
context_init(&ctx);
pthread_t th;
pthread_create(&th, NULL, fnc, &ctx);
context_wait_for_state(&ctx, THREAD_STARTED);
// trigger memory.grow
ctx.data = calloc(1024 * 1024, 1);
context_set_state(&ctx, MEMORY_ALLOCATED);
pthread_join(th, NULL);
context_destroy(&ctx);
return 0;
}