Enable multi-module support for wasm-c-api (#426)

it is allowed that all imported functions and globals can be
linked by multi-module feature automatically or by wasm-c-api manually
This commit is contained in:
lum1n0us
2020-10-16 17:43:57 +08:00
committed by GitHub
parent f1fe5d7872
commit 4787b150b8
26 changed files with 550 additions and 185 deletions

View File

@ -40,6 +40,7 @@ endif()
set(WAMR_BUILD_LIBC_BUILTIN 1)
set(WAMR_BUILD_LIBC_WASI 0)
set(WAMR_BUILD_MULTI_MODULE 1)
if(NOT DEFINED WAMR_BUILD_FAST_INTERP)
set(WAMR_BUILD_FAST_INTERP 0)
@ -70,47 +71,44 @@ endif()
################################################
################ application related ################
file(GLOB SOURCES src/*.c)
add_library(c-api ${SOURCES})
target_include_directories(c-api
PRIVATE ${C_API_PATH}/include
## locate wat2wasm
find_program(WAT2WASM
wat2wasm
PATHS /opt/wabt/bin /opt/wabt-1.0.18/bin
REQUIRED
)
target_link_libraries(c-api PRIVATE vmlib -lpthread -lm)
if (MSVC)
target_compile_definitions(c-api PRIVATE WASM_API_EXTERN=)
if(NOT WAT2WASM)
message(SEND_ERROR "can not find wat2wasm")
endif()
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
set(MM_UTIL src/utils/multi_module_utils.c)
# build executable for each .c
file(GLOB SOURCES src/*.c)
foreach(SRC ${SOURCES})
get_filename_component(APPNAME ${SRC} NAME_WE)
# build executable for each .c
add_executable(${APPNAME} ${SRC})
message("create executable about ${APPNAME}")
target_link_libraries(${APPNAME} c-api)
add_executable(${APPNAME} ${SRC} ${UNCOMMON_SHARED_SOURCE} ${MM_UTIL})
target_include_directories(${APPNAME} PRIVATE ${UNCOMMON_SHARED_DIR})
target_link_libraries(${APPNAME} vmlib -lpthread -lm)
if (MSVC)
target_compile_definitions(${APPNAME} PRIVATE WASM_API_EXTERN=)
endif()
endforeach()
# copy .wasm
add_custom_command(TARGET ${APPNAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/src/${APPNAME}.wasm
${PROJECT_BINARY_DIR}/
BYPRODUCTS ${APPNAME}.wasm
COMMENT "Copy ${SRC} to the output directory"
# wat to wasm
file(GLOB WAT_FILES src/*.wat)
foreach(WAT_FILE ${WAT_FILES})
get_filename_component(WATNAME ${WAT_FILE} NAME_WE)
add_custom_target(${WATNAME}_WASM ALL
COMMAND ${WAT2WASM} ${WAT_FILE} -o ${PROJECT_BINARY_DIR}/${WATNAME}.wasm
DEPENDS ${WAT_FILE}
BYPRODUCTS ${PROJECT_BINARY_DIR}/${WATNAME}.wasm
VERBATIM
SOURCES ${WAT_FILE}
)
# generate .aot file
if(${WAMR_BUILD_AOT} EQUAL 1)
if(EXISTS ${WAMRC})
add_custom_command(TARGET ${APPNAME} POST_BUILD
COMMAND ${WAMRC} -o ${APPNAME}.aot
${CMAKE_CURRENT_SOURCE_DIR}/src/${APPNAME}.wasm
BYPRODUCTS ${APPNAME}.aot
COMMENT "generate a aot file ${APPNAME}.aot"
)
endif()
endif()
endforeach(SRC ${SOURCES})
endforeach()
################################################

View File

@ -1,5 +1,16 @@
WAMR supports *wasm-c-api* in both *interpreter* mode and *aot* mode. By default,
all samples are compiled and run in "interpreter" mode.
WAMR supports *wasm-c-api* in both *interpreter* mode and *aot* mode.
Before staring, we need to download and intall [WABT](https://github.com/WebAssembly/wabt/releases/latest).
``` shell
$ cd /opt
$ wget https://github.com/WebAssembly/wabt/releases/download/1.0.19/wabt-1.0.19-ubuntu.tar.gz
$ tar -xzf wabt-1.0.19-ubuntu.tar.gz
$ mv wabt-1.0.19 wabt
```
By default, all samples are compiled and run in "interpreter" mode.
``` shell
$ mkdir build

View File

@ -4,6 +4,14 @@
#include <inttypes.h>
#include "wasm_c_api.h"
#include "wasm_export.h"
#include "bh_platform.h"
extern bool
reader(const char *module_name, uint8 **p_buffer, uint32 *p_size);
extern void
destroyer(uint8 *buffer, uint32 size);
#define own
@ -61,6 +69,8 @@ own wasm_trap_t* closure_callback(
int main(int argc, const char* argv[]) {
wasm_runtime_set_module_reader(reader, destroyer);
// Initialize.
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();

Binary file not shown.

View File

@ -4,6 +4,14 @@
#include <inttypes.h>
#include "wasm_c_api.h"
#include "wasm_export.h"
#include "bh_platform.h"
extern bool
reader(const char *module_name, uint8 **p_buffer, uint32 *p_size);
extern void
destroyer(uint8 *buffer, uint32 size);
#define own
@ -46,6 +54,8 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
int main(int argc, const char* argv[]) {
wasm_runtime_set_module_reader(reader, destroyer);
// Initialize.
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();

Binary file not shown.

View File

@ -0,0 +1,5 @@
(module
(global $mut_f32_export (export "var f32") (mut f32) (f32.const 7))
(func (export "get var f32 export") (result f32) (global.get $mut_f32_export))
(func (export "set var f32 export") (param f32) (global.set $mut_f32_export (local.get 0)))
)

View File

@ -0,0 +1,7 @@
(module
(global $mut_f32_import (export "var f32") (import "globalexportimport-0" "var f32") (mut f32))
(func (export "get var f32 export") (import "globalexportimport-0" "get var f32 export") (result f32))
(func (export "set var f32 export") (import "globalexportimport-0" "set var f32 export") (param f32))
(func (export "get var f32 import") (result f32) (global.get $mut_f32_import))
(func (export "set var f32 import") (param f32) (global.set $mut_f32_import (local.get 0)))
)

View File

@ -0,0 +1,166 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "wasm_c_api.h"
#include "wasm_export.h"
#include "bh_platform.h"
extern bool
reader(const char *module_name, uint8 **p_buffer, uint32 *p_size);
extern void
destroyer(uint8 *buffer, uint32 size);
#define own
wasm_global_t* get_export_global(const wasm_extern_vec_t* exports, size_t i) {
if (exports->size <= i || !wasm_extern_as_global(exports->data[i])) {
printf("> Error accessing global export %zu!\n", i);
exit(1);
}
return wasm_extern_as_global(exports->data[i]);
}
wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
printf("> Error accessing function export %zu!\n", i);
exit(1);
}
return wasm_extern_as_func(exports->data[i]);
}
#define check(val, type, expected) \
if (val.of.type != expected) { \
printf("> Expected reading value %f or %d \n", expected, expected); \
printf("> Error reading value %f or %d\n", val.of.type, val.of.type); \
}
#define check_global(global, type, expected) \
{ \
wasm_val_t val; \
wasm_global_get(global, &val); \
check(val, type, expected); \
}
#define check_call(func, type, expected) \
{ \
wasm_val_t results[1]; \
wasm_func_call(func, NULL, results); \
check(results[0], type, expected); \
}
wasm_module_t * create_module_from_file(wasm_store_t* store, const char * filename)
{
FILE* file = fopen(filename, "rb");
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0L, SEEK_SET);
wasm_byte_vec_t binary;
wasm_byte_vec_new_uninitialized(&binary, file_size);
if (fread(binary.data, file_size, 1, file) != 1) {
printf("> Error loading module!\n");
return 1;
}
// Compile.
printf("Compiling module...\n");
own wasm_module_t* module = wasm_module_new(store, &binary);
if (!module) {
printf("> Error compiling module!\n");
return 1;
}
wasm_byte_vec_delete(&binary);
fclose(file);
return module;
}
int main(int argc, const char* argv[]) {
wasm_runtime_set_module_reader(reader, destroyer);
// Initialize.
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
// Load binary.
printf("Loading binary...\n");
#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
wasm_module_t* moduleimport = create_module_from_file(store, "globalimport.aot");
#else
wasm_module_t* moduleimport = create_module_from_file(store, "globalexportimport-1.wasm");
#endif
// Instantiate.
printf("Instantiating Import module...\n");
own wasm_instance_t* instance_import =
wasm_instance_new(store, moduleimport, NULL, NULL); //after this var_f32_export->inst_comm_rt is module_import
if (!instance_import) {
printf("> Error instantiating Import module!\n");
return 1;
}
wasm_module_delete(moduleimport);
// Extract export.
printf("Extracting exports from Import module...\n");
own wasm_extern_vec_t exports_of_import;
wasm_instance_exports(instance_import, &exports_of_import);
int i = 0;
wasm_global_t *var_f32_export = get_export_global(&exports_of_import, i++);
wasm_func_t *get_var_f32_export = get_export_func(&exports_of_import, i++);
wasm_func_t* set_var_f32_export = get_export_func(&exports_of_import, i++);
wasm_func_t* get_var_f32_import = get_export_func(&exports_of_import, i++);
wasm_func_t* set_var_f32_import = get_export_func(&exports_of_import, i++);
// Interact.
// Check initial values.
printf("Check initial values...\n");
check_global(var_f32_export, f32, 7.0);
check_call(get_var_f32_export, f32, 7.0); //Call to module export
check_call(get_var_f32_import, f32, 7.0); //Call to module import
// Modify variables through API and check again.
printf("Modify the variable to 37.0...\n");
wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 37.0}};
wasm_global_set(var_f32_export, &val37); // var_f32_export->inst_comm_rt is module_import now
check_global(var_f32_export, f32, 37.0);
check_call(get_var_f32_export, f32, 37.0); //Call to module export Failed here, still 7
check_call(get_var_f32_import, f32, 37.0); //Call to module import
// Modify variables through calls and check again.
printf("Modify the variable to 77.0...\n");
wasm_val_t args77[] = { {.kind = WASM_F32, .of = {.f32 = 77.}} };
wasm_func_call(set_var_f32_export, args77, NULL); //Call to module export
check_call(get_var_f32_export, f32, 77.0); //Call to module export
check_global(var_f32_export, f32, 77.0); //Failed here, still 37
check_call(get_var_f32_import, f32, 77.0); //Call to module import Failed here, still 37
printf("Modify the variable to 78.0...\n");
wasm_val_t args78[] = { {.kind = WASM_F32, .of = {.f32 = 78.0}} };
wasm_func_call(set_var_f32_import, args78, NULL);
check_global(var_f32_export, f32, 78.0);
check_call(get_var_f32_export, f32, 78.0); //Call to module export Failed here, still 77
check_call(get_var_f32_import, f32, 78.0); //Call to module import
// wasm_extern_vec_delete(&exports_of_export);
//wasm_instance_delete(instance_export);
wasm_extern_vec_delete(&exports_of_import);
//wasm_instance_delete(instance_import);
// Shut down.
printf("Shutting down...\n");
wasm_store_delete(store);
wasm_engine_delete(engine);
// All done.
printf("Done.\n");
return 0;
}

View File

@ -4,6 +4,14 @@
#include <inttypes.h>
#include "wasm_c_api.h"
#include "wasm_export.h"
#include "bh_platform.h"
extern bool
reader(const char *module_name, uint8 **p_buffer, uint32 *p_size);
extern void
destroyer(uint8 *buffer, uint32 size);
#define own
@ -18,6 +26,8 @@ own wasm_trap_t* hello_callback(
int main(int argc, const char* argv[]) {
wasm_runtime_set_module_reader(reader, destroyer);
// Initialize.
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();

Binary file not shown.

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_read_file.h"
#include "wasm_export.h"
static char *
build_module_path(const char *module_name)
{
const char *module_search_path = ".";
const char *format = "%s/%s.wasm";
int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
+ strlen(".wasm") + 1;
char *wasm_file_name = BH_MALLOC(sz);
if (!wasm_file_name) {
return NULL;
}
snprintf(wasm_file_name, sz, format, module_search_path, module_name);
return wasm_file_name;
}
bool
reader(const char *module_name, uint8 **p_buffer, uint32 *p_size)
{
char *wasm_file_path = build_module_path(module_name);
if (!wasm_file_path) {
return false;
}
*p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_path, p_size);
BH_FREE(wasm_file_path);
return *p_buffer != NULL;
}
void
destroyer(uint8 *buffer, uint32 size)
{
if (!buffer) {
return;
}
BH_FREE(buffer);
buffer = NULL;
}