mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2024-11-26 15:32:05 +00:00
Remove get_module_inst() and fix issue of call native (#120)
This commit is contained in:
parent
473f96525b
commit
ff0267b7e6
|
@ -22,9 +22,6 @@
|
|||
#include "bh_platform.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
#define get_module_inst() \
|
||||
wasm_runtime_get_current_module_inst()
|
||||
|
||||
#define validate_app_addr(offset, size) \
|
||||
wasm_runtime_validate_app_addr(module_inst, offset, size)
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _WASM_EXPORT_H
|
||||
#define _WASM_EXPORT_H
|
||||
#ifndef _WASM_EXPORT_API_H
|
||||
#define _WASM_EXPORT_API_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -28,6 +28,31 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
void
|
||||
wasm_runtime_get_current_module_inst1(uint64_t *p_module_inst);
|
||||
|
||||
bool
|
||||
wasm_runtime_validate_app_addr1(uint32_t module_inst_part0,
|
||||
uint32_t module_inst_part1,
|
||||
int32_t app_offset, uint32_t size);
|
||||
bool
|
||||
wasm_runtime_validate_native_addr1(uint32_t module_inst_part0,
|
||||
uint32_t module_inst_part1,
|
||||
uint32_t native_ptr_part0,
|
||||
uint32_t native_ptr_part1,
|
||||
uint32_t size);
|
||||
bool
|
||||
wasm_runtime_addr_app_to_native1(uint32_t module_inst_part0,
|
||||
uint32_t module_inst_part1,
|
||||
int32_t app_offset,
|
||||
uint64_t *p_native_ptr);
|
||||
|
||||
int32_t
|
||||
wasm_runtime_addr_native_to_app1(uint32_t module_inst_part0,
|
||||
uint32_t module_inst_part1,
|
||||
uint32_t native_ptr_part0,
|
||||
uint32_t native_ptr_part1);
|
||||
|
||||
/**
|
||||
* Get current WASM module instance of the current native thread
|
||||
*
|
||||
|
@ -39,8 +64,13 @@ extern "C" {
|
|||
* 32-bit and 64-bit. And if the native pointer is 64-bit, data loss
|
||||
* occurs after converting it to WASM i32 type.
|
||||
*/
|
||||
uint64_t
|
||||
wasm_runtime_get_current_module_inst();
|
||||
static inline uint64_t
|
||||
wasm_runtime_get_current_module_inst()
|
||||
{
|
||||
uint64_t module_inst;
|
||||
wasm_runtime_get_current_module_inst1(&module_inst);
|
||||
return module_inst;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the app address, check whether it belongs to WASM module
|
||||
|
@ -52,9 +82,15 @@ wasm_runtime_get_current_module_inst();
|
|||
*
|
||||
* @return true if success, false otherwise.
|
||||
*/
|
||||
bool
|
||||
static inline bool
|
||||
wasm_runtime_validate_app_addr(uint64_t module_inst,
|
||||
int32_t app_offset, uint32_t size);
|
||||
int32_t app_offset, uint32_t size)
|
||||
{
|
||||
union { uint64_t val; uint32_t parts[2]; } u;
|
||||
u.val = module_inst;
|
||||
return wasm_runtime_validate_app_addr1(u.parts[0], u.parts[1],
|
||||
app_offset, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the native address, check whether it belongs to WASM module
|
||||
|
@ -67,9 +103,17 @@ wasm_runtime_validate_app_addr(uint64_t module_inst,
|
|||
*
|
||||
* @return true if success, false otherwise.
|
||||
*/
|
||||
bool
|
||||
static inline bool
|
||||
wasm_runtime_validate_native_addr(uint64_t module_inst,
|
||||
uint64_t native_ptr, uint32_t size);
|
||||
uint64_t native_ptr, uint32_t size)
|
||||
{
|
||||
union { uint64_t val; uint32_t parts[2]; } u1, u2;
|
||||
u1.val = module_inst;
|
||||
u2.val = native_ptr;
|
||||
return wasm_runtime_validate_native_addr1(u1.parts[0], u1.parts[1],
|
||||
u2.parts[0], u2.parts[1],
|
||||
size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert app address(relative address) to native address(absolute address)
|
||||
|
@ -79,9 +123,18 @@ wasm_runtime_validate_native_addr(uint64_t module_inst,
|
|||
*
|
||||
* @return the native address converted
|
||||
*/
|
||||
uint64_t
|
||||
static inline uint64_t
|
||||
wasm_runtime_addr_app_to_native(uint64_t module_inst,
|
||||
int32_t app_offset);
|
||||
int32_t app_offset)
|
||||
{
|
||||
union { uint64_t val; uint32_t parts[2]; } u;
|
||||
uint64_t native_ptr;
|
||||
u.val = module_inst;
|
||||
if (!wasm_runtime_addr_app_to_native1(u.parts[0], u.parts[1],
|
||||
app_offset, &native_ptr))
|
||||
return 0;
|
||||
return native_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert native address(absolute address) to app address(relative address)
|
||||
|
@ -91,12 +144,19 @@ wasm_runtime_addr_app_to_native(uint64_t module_inst,
|
|||
*
|
||||
* @return the app address converted
|
||||
*/
|
||||
int32_t
|
||||
static inline int32_t
|
||||
wasm_runtime_addr_native_to_app(uint64_t module_inst,
|
||||
uint64_t native_ptr);
|
||||
uint64_t native_ptr)
|
||||
{
|
||||
union { uint64_t val; uint32_t parts[2]; } u1, u2;
|
||||
u1.val = module_inst;
|
||||
u2.val = native_ptr;
|
||||
return wasm_runtime_addr_native_to_app1(u1.parts[0], u1.parts[1],
|
||||
u2.parts[0], u2.parts[1]);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _WASM_EXPORT_H */
|
||||
#endif /* end of _WASM_EXPORT_API_H */
|
||||
|
|
|
@ -25,16 +25,25 @@
|
|||
#include "base_lib_export.h"
|
||||
#endif
|
||||
|
||||
static uint64
|
||||
wasm_runtime_get_current_module_inst_wrapper(wasm_module_inst_t module_inst)
|
||||
static void
|
||||
wasm_runtime_get_current_module_inst1(wasm_module_inst_t module_inst,
|
||||
int32 inst_offset)
|
||||
{
|
||||
return (uint64)(uintptr_t)module_inst;
|
||||
uint64 *p_module_inst;
|
||||
|
||||
if (!wasm_runtime_validate_app_addr(module_inst, inst_offset, 8))
|
||||
return;
|
||||
|
||||
p_module_inst =
|
||||
wasm_runtime_addr_app_to_native(module_inst, inst_offset);
|
||||
*p_module_inst = (uint64)(uintptr_t)module_inst;
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
wasm_runtime_validate_app_addr_wrapper(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
int32 app_offset, uint32 size)
|
||||
wasm_runtime_validate_app_addr1(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
int32 app_offset, uint32 size)
|
||||
{
|
||||
bool ret;
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
|
@ -43,7 +52,7 @@ wasm_runtime_validate_app_addr_wrapper(wasm_module_inst_t module_inst,
|
|||
inst.parts[1] = inst_part1;
|
||||
|
||||
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
|
||||
printf("Invalid module instance\n");
|
||||
bh_printf("Invalid module instance\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -54,11 +63,11 @@ wasm_runtime_validate_app_addr_wrapper(wasm_module_inst_t module_inst,
|
|||
}
|
||||
|
||||
static bool
|
||||
wasm_runtime_validate_native_addr_wrapper(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
uint32 native_ptr_part0,
|
||||
uint32 native_ptr_part1,
|
||||
uint32 size)
|
||||
wasm_runtime_validate_native_addr1(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
uint32 native_ptr_part0,
|
||||
uint32 native_ptr_part1,
|
||||
uint32 size)
|
||||
{
|
||||
bool ret;
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
|
@ -74,37 +83,49 @@ wasm_runtime_validate_native_addr_wrapper(wasm_module_inst_t module_inst,
|
|||
|
||||
native_ptr.parts[0] = native_ptr_part0;
|
||||
native_ptr.parts[1] = native_ptr_part1;
|
||||
ret = wasm_runtime_validate_native_addr(module_inst,
|
||||
(void*)(uintptr_t)native_ptr.u64,
|
||||
size);
|
||||
ret = wasm_runtime_validate_native_addr(module_inst,
|
||||
(void*)(uintptr_t)native_ptr.u64,
|
||||
size);
|
||||
if (!ret)
|
||||
wasm_runtime_clear_exception(module_inst);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint64
|
||||
wasm_runtime_addr_app_to_native_wrapper(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
int32 app_offset)
|
||||
static bool
|
||||
wasm_runtime_addr_app_to_native1(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
int32 app_offset,
|
||||
int32 native_ptr_offset)
|
||||
|
||||
{
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
uint64 *p_native_ptr;
|
||||
|
||||
inst.parts[0] = inst_part0;
|
||||
inst.parts[1] = inst_part1;
|
||||
|
||||
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
|
||||
printf("Invalid module instance\n");
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
return (uint64)(uintptr_t)
|
||||
wasm_runtime_addr_app_to_native(module_inst, app_offset);
|
||||
|
||||
if (!wasm_runtime_validate_app_addr(module_inst, native_ptr_offset, 8)) {
|
||||
wasm_runtime_clear_exception(module_inst);
|
||||
return false;
|
||||
}
|
||||
|
||||
p_native_ptr =
|
||||
wasm_runtime_addr_app_to_native(module_inst, native_ptr_offset);
|
||||
*p_native_ptr = (uint64)(uintptr_t)
|
||||
wasm_runtime_addr_app_to_native(module_inst, app_offset);
|
||||
return true;
|
||||
}
|
||||
|
||||
static int32
|
||||
wasm_runtime_addr_native_to_app_wrapper(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
uint32 native_ptr_part0,
|
||||
uint32 native_ptr_part1)
|
||||
wasm_runtime_addr_native_to_app1(wasm_module_inst_t module_inst,
|
||||
uint32 inst_part0, uint32 inst_part1,
|
||||
uint32 native_ptr_part0,
|
||||
uint32 native_ptr_part1)
|
||||
{
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
union { uint64 u64; uint32 parts[2]; } native_ptr;
|
||||
|
@ -124,26 +145,26 @@ wasm_runtime_addr_native_to_app_wrapper(wasm_module_inst_t module_inst,
|
|||
}
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
|
||||
add functions to register. */
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
|
||||
add functions to register. */
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
EXPORT_WASM_API(wasm_register_resource),
|
||||
EXPORT_WASM_API(wasm_response_send),
|
||||
EXPORT_WASM_API(wasm_post_request),
|
||||
EXPORT_WASM_API(wasm_sub_event),
|
||||
EXPORT_WASM_API(wasm_create_timer),
|
||||
EXPORT_WASM_API(wasm_timer_destroy),
|
||||
EXPORT_WASM_API(wasm_timer_cancel),
|
||||
EXPORT_WASM_API(wasm_timer_restart),
|
||||
EXPORT_WASM_API(wasm_get_sys_tick_ms),
|
||||
EXPORT_WASM_API(wasm_register_resource),
|
||||
EXPORT_WASM_API(wasm_response_send),
|
||||
EXPORT_WASM_API(wasm_post_request),
|
||||
EXPORT_WASM_API(wasm_sub_event),
|
||||
EXPORT_WASM_API(wasm_create_timer),
|
||||
EXPORT_WASM_API(wasm_timer_destroy),
|
||||
EXPORT_WASM_API(wasm_timer_cancel),
|
||||
EXPORT_WASM_API(wasm_timer_restart),
|
||||
EXPORT_WASM_API(wasm_get_sys_tick_ms),
|
||||
#endif
|
||||
EXPORT_WASM_API2(wasm_runtime_get_current_module_inst),
|
||||
EXPORT_WASM_API2(wasm_runtime_validate_app_addr),
|
||||
EXPORT_WASM_API2(wasm_runtime_validate_native_addr),
|
||||
EXPORT_WASM_API2(wasm_runtime_addr_app_to_native),
|
||||
EXPORT_WASM_API2(wasm_runtime_addr_native_to_app),
|
||||
};
|
||||
EXPORT_WASM_API(wasm_runtime_get_current_module_inst1),
|
||||
EXPORT_WASM_API(wasm_runtime_validate_app_addr1),
|
||||
EXPORT_WASM_API(wasm_runtime_validate_native_addr1),
|
||||
EXPORT_WASM_API(wasm_runtime_addr_app_to_native1),
|
||||
EXPORT_WASM_API(wasm_runtime_addr_native_to_app1),
|
||||
};
|
||||
|
||||
int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
|
||||
{
|
||||
|
|
|
@ -9,6 +9,7 @@ extern "C" {
|
|||
|
||||
#include "bh_platform.h"
|
||||
#include "lvgl.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
#define OBJ_ARG_NUM_MAX 4
|
||||
#define PTR_ARG_NUM_MAX 4
|
||||
|
@ -66,6 +67,9 @@ void wgl_native_func_call(WGLNativeFuncDef *funcs,
|
|||
uint32 argv_offset,
|
||||
uint32 argc);
|
||||
|
||||
wasm_module_inst_t wasm_runtime_get_current_module_inst();
|
||||
#define get_module_inst() wasm_runtime_get_current_module_inst()
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -287,15 +287,6 @@ wasm_runtime_detach_current_thread(wasm_module_inst_t module_inst);
|
|||
void*
|
||||
wasm_runtime_get_current_thread_data();
|
||||
|
||||
/**
|
||||
* Get current WASM module instance of the current native thread
|
||||
*
|
||||
* @return current WASM module instance of the current native thread, NULL
|
||||
* if not found
|
||||
*/
|
||||
wasm_module_inst_t
|
||||
wasm_runtime_get_current_module_inst();
|
||||
|
||||
/**
|
||||
* Allocate memory from the heap of WASM module instance
|
||||
*
|
||||
|
|
|
@ -1387,7 +1387,6 @@ wasm_runtime_get_native_addr_range(WASMModuleInstance *module_inst,
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint32
|
||||
wasm_runtime_get_temp_ret(WASMModuleInstance *module_inst)
|
||||
{
|
||||
|
@ -1465,28 +1464,19 @@ static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
|
|||
static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
|
||||
static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
|
||||
|
||||
/* As JavaScript can't represent int64s, emcc compiles C int64 argument
|
||||
into two WASM i32 arguments, see:
|
||||
https://github.com/emscripten-core/emscripten/issues/7199
|
||||
And also JavaScript float point is always 64-bit, emcc compiles
|
||||
float32 argument into WASM f64 argument.
|
||||
But clang compiles C int64 argument into WASM i64 argument, and
|
||||
compiles C float32 argument into WASM f32 argument.
|
||||
So for the compatability of emcc and clang, we treat i64 as two i32s,
|
||||
treat f32 as f64 while passing arguments to the native function, and
|
||||
require the native function uses two i32 arguments instead one i64
|
||||
argument, and uses double argument instead of float argment. */
|
||||
|
||||
bool
|
||||
wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
|
||||
WASMModuleInstance *module_inst,
|
||||
uint32 *argv, uint32 argc, uint32 *ret)
|
||||
{
|
||||
union { float64 val; int32 parts[2]; } u;
|
||||
uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
|
||||
uint64 size;
|
||||
|
||||
#if !defined(__arm__) && !defined(__mips__)
|
||||
argc1 = argc + 2;
|
||||
#else
|
||||
argc1 = func_type->param_count * 2 + 2;
|
||||
#endif
|
||||
|
||||
if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
|
||||
size = ((uint64)sizeof(uint32)) * argc1;
|
||||
|
@ -1500,6 +1490,10 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
|
|||
for (i = 0; i < sizeof(WASMModuleInstance*) / sizeof(uint32); i++)
|
||||
argv1[j++] = ((uint32*)&module_inst)[i];
|
||||
|
||||
#if !defined(__arm__) && !defined(__mips__)
|
||||
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:
|
||||
|
@ -1507,25 +1501,23 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
|
|||
break;
|
||||
case VALUE_TYPE_I64:
|
||||
case VALUE_TYPE_F64:
|
||||
/* 64-bit data must be 8 bytes alined in arm and mips */
|
||||
if (j & 1)
|
||||
j++;
|
||||
argv1[j++] = *argv++;
|
||||
argv1[j++] = *argv++;
|
||||
break;
|
||||
case VALUE_TYPE_F32:
|
||||
u.val = *(float32*)argv++;
|
||||
#if defined(__arm__) || defined(__mips__)
|
||||
/* 64-bit data must be 8 bytes alined in arm and mips */
|
||||
if (j & 1)
|
||||
j++;
|
||||
#endif
|
||||
argv1[j++] = u.parts[0];
|
||||
argv1[j++] = u.parts[1];
|
||||
argv1[j++] = *argv++;
|
||||
break;
|
||||
default:
|
||||
wasm_assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
argc1 = j;
|
||||
if (func_type->result_count == 0) {
|
||||
invokeNative_Void(argv1, argc1, func_ptr);
|
||||
}
|
||||
|
@ -1543,6 +1535,9 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
|
|||
case VALUE_TYPE_F64:
|
||||
PUT_F64_TO_ADDR(ret, invokeNative_Float64(argv1, argc1, func_ptr));
|
||||
break;
|
||||
default:
|
||||
wasm_assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1582,7 +1577,7 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
|
|||
uint32 *argv, uint32 argc, uint32 *ret)
|
||||
{
|
||||
uint64 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size;
|
||||
uint32 *argv_src = argv, i, j, argc1, n_ints = 0, n_stacks = 0;
|
||||
uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
|
||||
#if defined(_WIN32) || defined(_WIN32_)
|
||||
/* important difference in calling conventions */
|
||||
#define n_fps n_ints
|
||||
|
@ -1615,12 +1610,11 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
|
|||
stacks[n_stacks++] = *argv_src++;
|
||||
break;
|
||||
case VALUE_TYPE_I64:
|
||||
for (j = 0; j < 2; j++) {
|
||||
if (n_ints < MAX_REG_INTS)
|
||||
ints[n_ints++] = *argv_src++;
|
||||
else
|
||||
stacks[n_stacks++] = *argv_src++;
|
||||
}
|
||||
if (n_ints < MAX_REG_INTS)
|
||||
ints[n_ints++] = *(uint64*)argv_src;
|
||||
else
|
||||
stacks[n_stacks++] = *(uint64*)argv_src;
|
||||
argv_src += 2;
|
||||
break;
|
||||
case VALUE_TYPE_F32:
|
||||
if (n_fps < MAX_REG_FLOATS)
|
||||
|
|
Loading…
Reference in New Issue
Block a user