Implement native function pointer check, addr conversion and register, update documents (#185)

Modified WASM runtime API:
- wasm_runtime_module_malloc()
- wasm_runtime_lookup_function()
Introduced runtime API
- wasm_runtime_register_natives()
This commit is contained in:
wenyongh
2020-03-04 20:12:38 +08:00
committed by GitHub
parent 2e36149e32
commit 0d3f304191
96 changed files with 2293 additions and 2317 deletions

View File

@ -20,6 +20,14 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
return NULL;
memset(exec_env, 0, (uint32)total_size);
#if WASM_ENABLE_AOT != 0
if (!(exec_env->argv_buf = wasm_malloc(sizeof(uint32) * 64))) {
wasm_free(exec_env);
return NULL;
}
#endif
exec_env->module_inst = module_inst;
exec_env->wasm_stack_size = stack_size;
exec_env->wasm_stack.s.top_boundary =
@ -31,6 +39,9 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
void
wasm_exec_env_destroy(WASMExecEnv *exec_env)
{
#if WASM_ENABLE_AOT != 0
wasm_free(exec_env->argv_buf);
#endif
wasm_free(exec_env);
}

View File

@ -30,6 +30,10 @@ typedef struct WASMExecEnv {
/* The WASM module instance of current thread */
struct WASMModuleInstanceCommon *module_inst;
#if WASM_ENABLE_AOT != 0
uint32 *argv_buf;
#endif
/* Current interpreter frame of current thread */
struct WASMInterpFrame *cur_frame;

View File

@ -4,44 +4,124 @@
*/
#include "wasm_native.h"
#include "wasm_runtime_common.h"
#include "bh_log.h"
typedef struct NativeSymbol {
const char *symbol;
void *func_ptr;
} NativeSymbol;
static NativeSymbolsList g_native_symbols_list = NULL;
static NativeSymbolsList g_native_symbols_list_end = NULL;
uint32
get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis);
uint32
get_spectest_export_apis(NativeSymbol **p_libc_builtin_apis);
uint32
get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis);
uint32
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
uint32
get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
static bool
sort_symbol_ptr(NativeSymbol *ptr, int len)
check_symbol_signature(const WASMType *type, const char *signature)
{
int i, j;
NativeSymbol temp;
const char *p = signature, *p_end;
char sig_map[] = { 'F', 'f', 'I', 'i' }, sig;
uint32 i = 0;
for (i = 0; i < len - 1; ++i) {
for (j = i + 1; j < len; ++j) {
if (strcmp((ptr+i)->symbol, (ptr+j)->symbol) > 0) {
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
if (!p || strlen(p) < 2)
return false;
p_end = p + strlen(signature);
if (*p++ != '(')
return false;
if ((uint32)(p_end - p) < type->param_count + 1)
/* signatures of parameters, and ')' */
return false;
for (i = 0; i < type->param_count; i++) {
sig = *p++;
if (sig == sig_map[type->types[i] - VALUE_TYPE_F64])
/* normal parameter */
continue;
if (type->types[i] != VALUE_TYPE_I32)
/* pointer and string must be i32 type */
return false;
if (sig == '*') {
/* it is a pointer */
if (i + 1 < type->param_count
&& type->types[i + 1] == VALUE_TYPE_I32
&& *p == '~') {
/* pointer length followed */
i++;
p++;
}
}
else if (sig == '$') {
/* it is a string */
}
else {
/* invalid signature */
return false;
}
}
if (*p++ != ')')
return false;
if (type->result_count) {
if (p >= p_end)
return false;
if (*p++ != sig_map[type->types[i] - VALUE_TYPE_F64])
return false;
}
if (*p != '\0')
return false;
return true;
}
static void
sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
{
uint32 i, j;
NativeSymbol temp;
for (i = 0; i < n_native_symbols - 1; i++) {
for (j = i + 1; j < n_native_symbols; j++) {
if (strcmp(native_symbols[i].symbol,
native_symbols[j].symbol) > 0) {
temp = native_symbols[i];
native_symbols[i] = native_symbols[j];
native_symbols[j] = temp;
}
}
}
}
static void *
lookup_symbol(NativeSymbol *ptr, int len, const char *symbol)
lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
const char *symbol, const char **p_signature)
{
int low = 0, mid, ret;
int high = len - 1;
int high = n_native_symbols - 1;
while (low <= high) {
mid = (low + high) / 2;
ret = strcmp(symbol, ptr[mid].symbol);
if (ret == 0)
return ptr[mid].func_ptr;
ret = strcmp(symbol, native_symbols[mid].symbol);
if (ret == 0) {
*p_signature = native_symbols[mid].signature;
return native_symbols[mid].func_ptr;
}
else if (ret < 0)
high = mid - 1;
else
@ -51,75 +131,135 @@ lookup_symbol(NativeSymbol *ptr, int len, const char *symbol)
return NULL;
}
void*
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
const WASMType *func_type, const char **p_signature)
{
NativeSymbolsNode *node, *node_next;
const char *signature = NULL;
void *func_ptr = NULL;
node = g_native_symbols_list;
while (node) {
node_next = node->next;
if (!strcmp(node->module_name, module_name)) {
if ((func_ptr = lookup_symbol(node->native_symbols,
node->n_native_symbols,
field_name, &signature))
|| (field_name[0] == '_'
&& (func_ptr = lookup_symbol(node->native_symbols,
node->n_native_symbols,
field_name + 1, &signature))))
break;
}
node = node_next;
}
if (func_ptr) {
if (signature && signature[0] != '\0') {
/* signature is not empty, check its format */
if (!check_symbol_signature(func_type, signature)) {
LOG_WARNING("failed to check signature '%s' and resolve "
"pointer params for import function (%s %s)\n",
signature, module_name, field_name);
return NULL;
}
else
/* Save signature for runtime to do pointer check and
address conversion */
*p_signature = signature;
}
else
/* signature is empty */
*p_signature = NULL;
}
return func_ptr;
}
bool
wasm_native_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
NativeSymbolsNode *node;
if (!(node = bh_malloc(sizeof(NativeSymbolsNode))))
return false;
node->module_name = module_name;
node->native_symbols = native_symbols;
node->n_native_symbols = n_native_symbols;
node->next = NULL;
if (g_native_symbols_list_end) {
g_native_symbols_list_end->next = node;
g_native_symbols_list_end = node;
}
else {
g_native_symbols_list = g_native_symbols_list_end = node;
}
sort_symbol_ptr(native_symbols, n_native_symbols);
return true;
}
bool
wasm_native_init()
{
NativeSymbol *native_symbols;
uint32 n_native_symbols;
#if WASM_ENABLE_LIBC_BUILTIN != 0
n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
if (!wasm_native_register_natives("env",
native_symbols, n_native_symbols))
return false;
n_native_symbols = get_spectest_export_apis(&native_symbols);
if (!wasm_native_register_natives("spectest",
native_symbols, n_native_symbols))
return false;
#endif
#if WASM_ENABLE_LIBC_WASI != 0
n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
if (!wasm_native_register_natives("wasi_unstable",
native_symbols, n_native_symbols))
return false;
#endif
#if WASM_ENABLE_BASE_LIB != 0
static bool is_base_lib_sorted = false;
static NativeSymbol *base_native_symbol_defs;
static int base_native_symbol_len;
n_native_symbols = get_base_lib_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env",
native_symbols, n_native_symbols))
return false;
#endif
int
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
#if WASM_ENABLE_APP_FRAMEWORK != 0
n_native_symbols = get_ext_lib_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env",
native_symbols, n_native_symbols))
return false;
#endif
void *
wasm_native_lookup_base_lib_func(const char *module_name,
const char *func_name)
{
void *ret;
if (strcmp(module_name, "env"))
return NULL;
if (!is_base_lib_sorted) {
base_native_symbol_len = get_base_lib_export_apis(&base_native_symbol_defs);
if (base_native_symbol_len > 0)
sort_symbol_ptr(base_native_symbol_defs, base_native_symbol_len);
is_base_lib_sorted = true;
}
if ((ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
func_name))
|| (func_name[0] == '_'
&& (ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
func_name + 1))))
return ret;
return NULL;
}
#endif /* end of WASM_ENABLE_BASE_LIB */
static bool is_ext_lib_sorted = false;
static NativeSymbol *ext_native_symbol_defs;
static int ext_native_symbol_len;
int
get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
void *
wasm_native_lookup_extension_lib_func(const char *module_name,
const char *func_name)
{
void *ret;
if (strcmp(module_name, "env"))
return NULL;
if (!is_ext_lib_sorted) {
ext_native_symbol_len = get_ext_lib_export_apis(&ext_native_symbol_defs);
if (ext_native_symbol_len > 0)
sort_symbol_ptr(ext_native_symbol_defs, ext_native_symbol_len);
is_ext_lib_sorted = true;
}
if ((ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
func_name))
|| (func_name[0] == '_'
&& (ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
func_name + 1))))
return ret;
return NULL;
return true;
}
void
wasm_native_destroy()
{
NativeSymbolsNode *node, *node_next;
node = g_native_symbols_list;
while (node) {
node_next = node->next;
bh_free(node);
node = node_next;
}
g_native_symbols_list = g_native_symbols_list_end = NULL;
}

View File

@ -7,82 +7,66 @@
#define _WASM_NATIVE_H
#include "bh_common.h"
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
#include "../interpreter/wasm.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* Lookup native function implementation of a given import function
* in libc builtin API's
*
* @param module_name the module name of the import function
* @param func_name the function name of the import function
*
* @return return the native function pointer if success, NULL otherwise
*/
void *
wasm_native_lookup_libc_builtin_func(const char *module_name,
const char *func_name);
typedef struct NativeSymbol {
const char *symbol;
void *func_ptr;
const char *signature;
} NativeSymbol;
typedef struct NativeSymbolsNode {
struct NativeSymbolsNode *next;
const char *module_name;
NativeSymbol *native_symbols;
uint32 n_native_symbols;
} NativeSymbolsNode, *NativeSymbolsList;
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
/**
* Lookup global variable of a given import global
* in libc builtin globals
* from libc builtin globals
*
* @param module_name the module name of the import global
* @param global_name the global name of the import global
* @param global return the global data
*
* @param return true if success, false otherwise
* @param true if success, false otherwise
*/
bool
wasm_native_lookup_libc_builtin_global(const char *module_name,
const char *global_name,
WASMGlobalImport *global);
#endif
/**
* Lookup native function implementation of a given import function
* in libc wasi API's
* Resolve native symbol in all libraries, including libc-builtin, libc-wasi,
* base lib and extension lib, and user registered natives
* function, which can be auto checked by vm before calling native function
*
* @param module_name the module name of the import function
* @param func_name the function name of the import function
* @param func_type the function prototype of the import function
* @param p_signature output the signature if resolve success
*
* @return return the native function pointer if success, NULL otherwise
* @return the native function pointer if success, NULL otherwise
*/
void *
wasm_native_lookup_libc_wasi_func(const char *module_name,
const char *func_name);
void*
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
const WASMType *func_type, const char **p_signature);
/**
* Lookup native function implementation of a given import function
* in base lib API's
*
* @param module_name the module name of the import function
* @param func_name the function name of the import function
*
* @return return the native function pointer if success, NULL otherwise
*/
void *
wasm_native_lookup_base_lib_func(const char *module_name,
const char *func_name);
bool
wasm_native_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols);
/**
* Lookup native function implementation of a given import function
* in extension lib API's
*
* @param module_name the module name of the import function
* @param func_name the function name of the import function
*
* @return return the native function pointer if success, NULL otherwise
*/
void *
wasm_native_lookup_extension_lib_func(const char *module_name,
const char *func_name);
bool
wasm_native_init();
void
wasm_native_destroy();
#ifdef __cplusplus
}

View File

@ -35,12 +35,18 @@ wasm_runtime_init()
if (vm_thread_sys_init() != 0)
return false;
if (wasm_native_init() == false) {
wasm_runtime_destroy();
return false;
}
return true;
}
void
wasm_runtime_destroy()
{
wasm_native_destroy();
vm_thread_sys_destroy();
}
@ -342,15 +348,18 @@ wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
}
int32
wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size)
wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
void **p_native_addr)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode)
return wasm_module_malloc((WASMModuleInstance*)module_inst, size);
return wasm_module_malloc((WASMModuleInstance*)module_inst, size,
p_native_addr);
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT)
return aot_module_malloc((AOTModuleInstance*)module_inst, size);
return aot_module_malloc((AOTModuleInstance*)module_inst, size,
p_native_addr);
#endif
return 0;
}
@ -691,20 +700,17 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
total_size = sizeof(size_t) * (uint64)argc;
if (total_size >= UINT32_MAX
|| !(offset_argv_offsets = wasm_runtime_module_malloc
(module_inst, (uint32)total_size))
(module_inst, (uint32)total_size,
(void**)&argv_offsets))
|| argv_buf_len >= UINT32_MAX
|| !(offset_argv_buf = wasm_runtime_module_malloc
(module_inst, (uint32)argv_buf_len))) {
(module_inst, (uint32)argv_buf_len,
(void**)&argv_buf))) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: allocate memory failed.");
goto fail;
}
argv_offsets = (size_t*)
wasm_runtime_addr_app_to_native(module_inst, offset_argv_offsets);
argv_buf = (char*)
wasm_runtime_addr_app_to_native(module_inst, offset_argv_buf);
for (i = 0; i < argc; i++) {
argv_offsets[i] = argv_buf_offset;
bh_strcpy_s(argv_buf + argv_buf_offset,
@ -718,20 +724,17 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
total_size = sizeof(size_t) * (uint64)argc;
if (total_size >= UINT32_MAX
|| !(offset_env_offsets = wasm_runtime_module_malloc
(module_inst, (uint32)total_size))
(module_inst, (uint32)total_size,
(void**)&env_offsets))
|| env_buf_len >= UINT32_MAX
|| !(offset_env_buf = wasm_runtime_module_malloc
(module_inst, (uint32)env_buf_len))) {
(module_inst, (uint32)env_buf_len,
(void**)&env_buf))) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: allocate memory failed.");
goto fail;
}
env_offsets = (size_t*)
wasm_runtime_addr_app_to_native(module_inst, offset_env_offsets);
env_buf = (char*)
wasm_runtime_addr_app_to_native(module_inst, offset_env_buf);
for (i = 0; i < env_count; i++) {
env_offsets[i] = env_buf_offset;
bh_strcpy_s(env_buf + env_buf_offset,
@ -740,23 +743,20 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
}
if (!(offset_curfds = wasm_runtime_module_malloc
(module_inst, sizeof(struct fd_table)))
(module_inst, sizeof(struct fd_table), (void**)&curfds))
|| !(offset_prestats = wasm_runtime_module_malloc
(module_inst, sizeof(struct fd_prestats)))
(module_inst, sizeof(struct fd_prestats), (void**)&prestats))
|| !(offset_argv_environ = wasm_runtime_module_malloc
(module_inst, sizeof(struct argv_environ_values)))) {
(module_inst, sizeof(struct argv_environ_values),
(void**)&argv_environ))) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: allocate memory failed.");
goto fail;
}
curfds = wasi_ctx->curfds = (struct fd_table*)
wasm_runtime_addr_app_to_native(module_inst, offset_curfds);
prestats = wasi_ctx->prestats = (struct fd_prestats*)
wasm_runtime_addr_app_to_native(module_inst, offset_prestats);
argv_environ = wasi_ctx->argv_environ =
(struct argv_environ_values*)wasm_runtime_addr_app_to_native
(module_inst, offset_argv_environ);
wasi_ctx->curfds = curfds;
wasi_ctx->prestats = prestats;
wasi_ctx->argv_environ = argv_environ;
fd_table_init(curfds);
fd_prestats_init(prestats);
@ -1064,13 +1064,14 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
if (total_size >= UINT32_MAX
|| !(argv_buf_offset =
wasm_runtime_module_malloc(module_inst, (uint32)total_size))) {
wasm_runtime_module_malloc(module_inst, (uint32)total_size,
(void**)&argv_buf))) {
wasm_runtime_set_exception(module_inst,
"allocate memory failed.");
return false;
}
argv_buf = p = wasm_runtime_addr_app_to_native(module_inst, argv_buf_offset);
p = argv_buf;
argv_offsets = (int32*)(p + total_argv_size);
p_end = p + total_size;
@ -1379,6 +1380,15 @@ fail:
return false;
}
bool
wasm_runtime_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
return wasm_native_register_natives(module_name,
native_symbols, n_native_symbols);
}
/**
* Implementation of wasm_runtime_invoke_native()
*/
@ -1425,13 +1435,16 @@ static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
#define MAX_REG_FLOATS 16
bool
wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
WASMExecEnv *exec_env,
uint32 *argv, uint32 argc, uint32 *ret)
wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
uint32 *argv, uint32 argc, uint32 *argv_ret)
{
WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
/* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
uint32 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size;
uint32 *argv_src = argv, i, argc1, n_ints = 0, n_fps = 0, n_stacks = 0;
uint32 arg_i32, ptr_len;
bool ret = false;
n_ints++; /* exec env */
@ -1508,11 +1521,41 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
for (i = 0; i < func_type->param_count; i++) {
switch (func_type->types[i]) {
case VALUE_TYPE_I32:
{
arg_i32 = *argv_src++;
if (signature) {
if (signature[i + 1] == '*') {
/* param is a pointer */
if (signature[i + 2] == '~')
/* pointer with length followed */
ptr_len = *argv_src;
else
/* pointer without length followed */
ptr_len = 1;
if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
goto fail;
arg_i32 = (uintptr_t)
wasm_runtime_addr_app_to_native(module, arg_i32);
}
else if (signature[i + 1] == '$') {
/* param is a string */
if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
goto fail;
arg_i32 = (uintptr_t)
wasm_runtime_addr_app_to_native(module, arg_i32);
}
}
if (n_ints < MAX_REG_INTS)
ints[n_ints++] = *argv_src++;
ints[n_ints++] = arg_i32;
else
stacks[n_stacks++] = *argv_src++;
stacks[n_stacks++] = arg_i32;
break;
}
case VALUE_TYPE_I64:
if (n_ints < MAX_REG_INTS - 1) {
/* 64-bit data must be 8 bytes aligned in arm */
@ -1565,26 +1608,29 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
else {
switch (func_type->types[func_type->param_count]) {
case VALUE_TYPE_I32:
ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
break;
case VALUE_TYPE_I64:
PUT_I64_TO_ADDR(ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
break;
case VALUE_TYPE_F32:
*(float32*)ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
*(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
break;
case VALUE_TYPE_F64:
PUT_F64_TO_ADDR(ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
break;
default:
bh_assert(0);
break;
}
}
ret = true;
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
return true;
return ret;
}
#endif /* end of defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) */
@ -1609,12 +1655,15 @@ static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
bool
wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
WASMExecEnv *exec_env,
uint32 *argv, uint32 argc, uint32 *ret)
wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
uint32 *argv, uint32 argc, uint32 *argv_ret)
{
WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
uint32 arg_i32, ptr_len;
uint64 size;
bool ret = false;
#if defined(BUILD_TARGET_X86_32)
argc1 = argc + 2;
@ -1637,20 +1686,49 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
for (i = 0; i < sizeof(WASMExecEnv*) / sizeof(uint32); i++)
argv1[j++] = ((uint32*)&exec_env)[i];
#if defined(BUILD_TARGET_X86_32)
word_copy(argv1 + j, argv, argc);
j += argc;
#else
for (i = 0; i < func_type->param_count; i++) {
switch (func_type->types[i]) {
case VALUE_TYPE_I32:
argv1[j++] = *argv++;
{
arg_i32 = *argv++;
if (signature) {
if (signature[i + 1] == '*') {
/* param is a pointer */
if (signature[i + 2] == '~')
/* pointer with length followed */
ptr_len = *argv;
else
/* pointer without length followed */
ptr_len = 1;
if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
goto fail;
arg_i32 = (uintptr_t)
wasm_runtime_addr_app_to_native(module, arg_i32);
}
else if (signature[i + 1] == '$') {
/* param is a string */
if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
goto fail;
arg_i32 = (uintptr_t)
wasm_runtime_addr_app_to_native(module, arg_i32);
}
}
argv1[j++] = arg_i32;
break;
}
case VALUE_TYPE_I64:
case VALUE_TYPE_F64:
/* 64-bit data must be 8 bytes aligned in arm and mips */
#if !defined(BUILD_TARGET_X86_32)
/* 64-bit data must be 8 bytes aligned in arm, thumb, mips
and xtensa */
if (j & 1)
j++;
#endif
argv1[j++] = *argv++;
argv1[j++] = *argv++;
break;
@ -1662,7 +1740,6 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
break;
}
}
#endif /* end of defined(BUILD_TARGET_X86_32) */
argc1 = j;
if (func_type->result_count == 0) {
@ -1671,16 +1748,16 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
else {
switch (func_type->types[func_type->param_count]) {
case VALUE_TYPE_I32:
ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
break;
case VALUE_TYPE_I64:
PUT_I64_TO_ADDR(ret, invokeNative_Int64(func_ptr, argv1, argc1));
PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, argc1));
break;
case VALUE_TYPE_F32:
*(float32*)ret = invokeNative_Float32(func_ptr, argv1, argc1);
*(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, argc1);
break;
case VALUE_TYPE_F64:
PUT_F64_TO_ADDR(ret, invokeNative_Float64(func_ptr, argv1, argc1));
PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, argc1));
break;
default:
bh_assert(0);
@ -1688,9 +1765,12 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
}
}
ret = true;
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
return true;
return ret;
}
#endif /* end of defined(BUILD_TARGET_X86_32) \
@ -1724,12 +1804,15 @@ static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
#endif
bool
wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
WASMExecEnv *exec_env,
uint32 *argv, uint32 argc, uint32 *ret)
wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
uint32 *argv, uint32 argc, uint32 *argv_ret)
{
uint64 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size;
WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
uint64 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size, arg_i64;
uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
uint32 arg_i32, ptr_len;
bool ret = false;
#if defined(_WIN32) || defined(_WIN32_)
/* important difference in calling conventions */
#define n_fps n_ints
@ -1757,11 +1840,40 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
for (i = 0; i < func_type->param_count; i++) {
switch (func_type->types[i]) {
case VALUE_TYPE_I32:
{
arg_i32 = *argv_src++;
arg_i64 = arg_i32;
if (signature) {
if (signature[i + 1] == '*') {
/* param is a pointer */
if (signature[i + 2] == '~')
/* pointer with length followed */
ptr_len = *argv_src;
else
/* pointer without length followed */
ptr_len = 1;
if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
goto fail;
arg_i64 = (uintptr_t)
wasm_runtime_addr_app_to_native(module, arg_i32);
}
else if (signature[i + 1] == '$') {
/* param is a string */
if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
goto fail;
arg_i64 = (uintptr_t)
wasm_runtime_addr_app_to_native(module, arg_i32);
}
}
if (n_ints < MAX_REG_INTS)
ints[n_ints++] = *argv_src++;
ints[n_ints++] = arg_i64;
else
stacks[n_stacks++] = *argv_src++;
stacks[n_stacks++] = arg_i64;
break;
}
case VALUE_TYPE_I64:
if (n_ints < MAX_REG_INTS)
ints[n_ints++] = *(uint64*)argv_src;
@ -1794,16 +1906,16 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
else {
switch (func_type->types[func_type->param_count]) {
case VALUE_TYPE_I32:
ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
break;
case VALUE_TYPE_I64:
PUT_I64_TO_ADDR(ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
break;
case VALUE_TYPE_F32:
*(float32*)ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
*(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
break;
case VALUE_TYPE_F64:
PUT_F64_TO_ADDR(ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
break;
default:
bh_assert(0);
@ -1811,10 +1923,12 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
}
}
ret = true;
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
return true;
return ret;
}
#endif /* end of defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) */

View File

@ -10,6 +10,7 @@
#include "bh_common.h"
#include "bh_thread.h"
#include "wasm_exec_env.h"
#include "wasm_native.h"
#include "../interpreter/wasm.h"
#if WASM_ENABLE_LIBC_WASI != 0
#include "wasmtime_ssp.h"
@ -172,7 +173,8 @@ wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
/* See wasm_export.h for description */
int32
wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size);
wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
void **p_native_addr);
/* See wasm_export.h for description */
void
@ -282,9 +284,15 @@ wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
bool
wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count);
/* See wasm_export.h for description */
bool
wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
WASMExecEnv *exec_env,
wasm_runtime_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols);
bool
wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
const WASMType *func_type, const char *signature,
uint32 *argv, uint32 argc, uint32 *ret);