Implement module instance context APIs (#2436)

Introduce module instance context APIs which can set one or more contexts created
by the embedder for a wasm module instance:
```C
    wasm_runtime_create_context_key
    wasm_runtime_destroy_context_key
    wasm_runtime_set_context
    wasm_runtime_set_context_spread
    wasm_runtime_get_context
```

And make libc-wasi use it and set wasi context as the first context bound to the wasm
module instance.

Also add samples.

Refer to https://github.com/bytecodealliance/wasm-micro-runtime/issues/2460.
This commit is contained in:
YAMAMOTO Takashi
2023-09-07 15:54:11 +09:00
committed by GitHub
parent af2f3c8759
commit 6c846acc59
35 changed files with 1190 additions and 56 deletions

View File

@ -0,0 +1 @@
/out/

View File

@ -0,0 +1,92 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required (VERSION 3.14)
include(CheckPIESupported)
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
project (inst-context)
else()
project (inst-context C ASM)
enable_language (ASM_MASM)
endif()
################ runtime settings ################
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
if (APPLE)
add_definitions(-DBH_PLATFORM_DARWIN)
endif ()
# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# WAMR features switch
# Set WAMR_BUILD_TARGET, currently values supported:
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
if (NOT DEFINED WAMR_BUILD_TARGET)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
set (WAMR_BUILD_TARGET "AARCH64")
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
set (WAMR_BUILD_TARGET "RISCV64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
# Build as X86_64 by default in 64-bit platform
set (WAMR_BUILD_TARGET "X86_64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
# Build as X86_32 by default in 32-bit platform
set (WAMR_BUILD_TARGET "X86_32")
else ()
message(SEND_ERROR "Unsupported build target platform!")
endif ()
endif ()
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Debug)
endif ()
set (WAMR_BUILD_INTERP 1)
set (WAMR_BUILD_AOT 1)
set (WAMR_BUILD_JIT 0)
set (WAMR_BUILD_LIBC_BUILTIN 0)
set (WAMR_BUILD_LIB_WASI_THREADS 1)
if (NOT MSVC)
set (WAMR_BUILD_LIBC_WASI 1)
endif ()
if (NOT MSVC)
# linker flags
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
endif ()
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
endif ()
endif ()
endif ()
# build out vmlib
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
################ application related ################
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
add_executable (inst-context src/main.c src/native_impl.c ${UNCOMMON_SHARED_SOURCE})
check_pie_supported()
set_target_properties (inst-context PROPERTIES POSITION_INDEPENDENT_CODE ON)
if (APPLE)
target_link_libraries (inst-context vmlib -lm -ldl -lpthread)
else ()
target_link_libraries (inst-context vmlib -lm -ldl -lpthread -lrt)
endif ()

View File

@ -0,0 +1,4 @@
The "inst-context" sample project
=================================
This sample demonstrates module instance context API.

View File

@ -0,0 +1,61 @@
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#!/bin/bash
CURR_DIR=$PWD
WAMR_DIR=${PWD}/../..
OUT_DIR=${PWD}/out
WASM_APPS=${PWD}/wasm-apps
rm -rf ${OUT_DIR}
mkdir ${OUT_DIR}
mkdir ${OUT_DIR}/wasm-apps
echo "#####################build inst-context project"
cd ${CURR_DIR}
mkdir -p cmake_build
cd cmake_build
cmake ..
make -j ${nproc}
if [ $? != 0 ];then
echo "BUILD_FAIL inst-context exit as $?\n"
exit 2
fi
cp -a inst-context ${OUT_DIR}
echo -e "\n"
echo "#####################build wasm apps"
cd ${WASM_APPS}
for i in `ls *.c`
do
APP_SRC="$i"
OUT_FILE=${i%.*}.wasm
# use WAMR SDK to build out the .wasm binary
# require wasi-sdk with wasi-threads support. (wasi-sdk-20.0 or later)
/opt/wasi-sdk/bin/clang \
--target=wasm32-wasi-threads \
-pthread \
-Wl,--import-memory \
-Wl,--export-memory \
-Wl,--max-memory=655360 \
-o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
echo "build ${OUT_FILE} success"
else
echo "build ${OUT_FILE} fail"
fi
done
echo "####################build wasm apps done"

View File

@ -0,0 +1,3 @@
#!/bin/bash
out/inst-context -f out/wasm-apps/testapp.wasm

View File

@ -0,0 +1,151 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_export.h"
#include "bh_read_file.h"
#include "bh_getopt.h"
#include "my_context.h"
void
set_context(wasm_exec_env_t exec_env, int32_t n);
int32_t
get_context(wasm_exec_env_t exec_env);
void *my_context_key;
struct my_context my_context;
int my_dtor_called;
wasm_module_inst_t module_inst = NULL;
void
print_usage(void)
{
fprintf(stdout, "Options:\r\n");
fprintf(stdout, " -f [path of wasm file] \n");
}
void
my_context_dtor(wasm_module_inst_t inst, void *ctx)
{
printf("%s called\n", __func__);
my_dtor_called++;
bh_assert(ctx == &my_context);
bh_assert(inst == module_inst);
}
int
main(int argc, char *argv_main[])
{
static char global_heap_buf[512 * 1024];
char *buffer;
char error_buf[128];
int opt;
char *wasm_path = NULL;
int exit_code = 1;
wasm_module_t module = NULL;
uint32 buf_size, stack_size = 8092, heap_size = 8092;
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
switch (opt) {
case 'f':
wasm_path = optarg;
break;
case 'h':
print_usage();
return 0;
case '?':
print_usage();
return 0;
}
}
if (optind == 1) {
print_usage();
return 0;
}
// Define an array of NativeSymbol for the APIs to be exported.
// Note: the array must be static defined since runtime
// will keep it after registration
// For the function signature specifications, goto the link:
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
static NativeSymbol native_symbols[] = {
{ "set_context", set_context, "(i)", NULL },
{ "get_context", get_context, "()i", NULL },
};
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
// Native symbols need below registration phase
init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
init_args.native_module_name = "env";
init_args.native_symbols = native_symbols;
if (!wasm_runtime_full_init(&init_args)) {
printf("Init runtime environment failed.\n");
return -1;
}
my_context_key = wasm_runtime_create_context_key(my_context_dtor);
if (!my_context_key) {
printf("wasm_runtime_create_context_key failed.\n");
return -1;
}
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
if (!buffer) {
printf("Open wasm app file [%s] failed.\n", wasm_path);
goto fail;
}
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
sizeof(error_buf));
if (!module) {
printf("Load wasm module failed. error: %s\n", error_buf);
goto fail;
}
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
error_buf, sizeof(error_buf));
if (!module_inst) {
printf("Instantiate wasm module failed. error: %s\n", error_buf);
goto fail;
}
char *args[] = {
"testapp",
};
wasm_application_execute_main(module_inst, 1, args);
const char *exc = wasm_runtime_get_exception(module_inst);
if (exc != NULL) {
printf("call wasm function calculate failed. error: %s\n", exc);
goto fail;
}
exit_code = 0;
fail:
if (module_inst) {
bh_assert(my_dtor_called == 0);
wasm_runtime_deinstantiate(module_inst);
bh_assert(my_dtor_called == 1);
}
if (module)
wasm_runtime_unload(module);
if (buffer)
BH_FREE(buffer);
if (my_context_key)
wasm_runtime_destroy_context_key(my_context_key);
wasm_runtime_destroy();
return exit_code;
}

View File

@ -0,0 +1,11 @@
/*
* Copyright (C) 2023 Midokura Japan KK. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
struct my_context {
int x;
};
extern void *my_context_key;
extern struct my_context my_context;

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2023 Midokura Japan KK. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stddef.h>
#include <stdio.h>
#include "wasm_export.h"
#include "my_context.h"
void
set_context(wasm_exec_env_t exec_env, int32_t n)
{
wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
printf("%s called on module inst %p\n", __func__, inst);
struct my_context *ctx = &my_context;
ctx->x = n;
wasm_runtime_set_context_spread(inst, my_context_key, ctx);
}
int32_t
get_context(wasm_exec_env_t exec_env)
{
wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
printf("%s called on module inst %p\n", __func__, inst);
struct my_context *ctx = wasm_runtime_get_context(inst, my_context_key);
if (ctx == NULL) {
return -1;
}
return ctx->x;
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2023 Midokura Japan KK. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void
set_context(int32_t n) __attribute__((import_module("env")))
__attribute__((import_name("set_context")));
int32_t
get_context() __attribute__((import_module("env")))
__attribute__((import_name("get_context")));
void *
start(void *vp)
{
int32_t v;
printf("thread started\n");
printf("confirming the initial state on thread\n");
v = get_context();
assert(v == -1);
printf("setting the context on thread\n");
set_context(1234);
printf("confirming the context on thread\n");
v = get_context();
assert(v == 1234);
return NULL;
}
int
main()
{
pthread_t t1;
int32_t v;
int ret;
printf("confirming the initial state on main\n");
v = get_context();
assert(v == -1);
printf("creating a thread\n");
ret = pthread_create(&t1, NULL, start, NULL);
assert(ret == 0);
void *val;
ret = pthread_join(t1, &val);
assert(ret == 0);
printf("joined the thread\n");
printf("confirming the context propagated from the thread on main\n");
v = get_context();
assert(v == 1234);
printf("success\n");
return 0;
}