Enable native/app address validation and conversion for wasm app (#102)
Enable setting external memory space for wasm app, the feature is disabled by default; Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused.
This commit is contained in:
@ -18,11 +18,116 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "lib_export.h"
|
||||
#include "bh_platform.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
#include "base_lib_export.h"
|
||||
#endif
|
||||
|
||||
static uint64
|
||||
wasm_runtime_get_current_module_inst_wrapper()
|
||||
{
|
||||
return (uint64)(uintptr_t)
|
||||
wasm_runtime_get_current_module_inst();
|
||||
}
|
||||
|
||||
static bool
|
||||
wasm_runtime_validate_app_addr_wrapper(uint32 inst_part0, uint32 inst_part1,
|
||||
int32 app_offset, uint32 size)
|
||||
{
|
||||
bool ret;
|
||||
wasm_module_inst_t module_inst =
|
||||
wasm_runtime_get_current_module_inst();
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
|
||||
inst.parts[0] = inst_part0;
|
||||
inst.parts[1] = inst_part1;
|
||||
|
||||
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
|
||||
printf("Invalid module instance\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = wasm_runtime_validate_app_addr(module_inst, app_offset, size);
|
||||
if (!ret)
|
||||
wasm_runtime_clear_exception(module_inst);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool
|
||||
wasm_runtime_validate_native_addr_wrapper(uint32 inst_part0, uint32 inst_part1,
|
||||
uint32 native_ptr_part0,
|
||||
uint32 native_ptr_part1,
|
||||
uint32 size)
|
||||
{
|
||||
bool ret;
|
||||
wasm_module_inst_t module_inst =
|
||||
wasm_runtime_get_current_module_inst();
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
union { uint64 u64; uint32 parts[2]; } 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 false;
|
||||
}
|
||||
|
||||
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);
|
||||
if (!ret)
|
||||
wasm_runtime_clear_exception(module_inst);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint64
|
||||
wasm_runtime_addr_app_to_native_wrapper(uint32 inst_part0, uint32 inst_part1,
|
||||
int32 app_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst =
|
||||
wasm_runtime_get_current_module_inst();
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
|
||||
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 (uint64)(uintptr_t)
|
||||
wasm_runtime_addr_app_to_native(module_inst, app_offset);
|
||||
}
|
||||
|
||||
static int32
|
||||
wasm_runtime_addr_native_to_app_wrapper(uint32 inst_part0, uint32 inst_part1,
|
||||
uint32 native_ptr_part0,
|
||||
uint32 native_ptr_part1)
|
||||
{
|
||||
wasm_module_inst_t module_inst =
|
||||
wasm_runtime_get_current_module_inst();
|
||||
union { uint64 u64; uint32 parts[2]; } inst;
|
||||
union { uint64 u64; uint32 parts[2]; } 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;
|
||||
}
|
||||
|
||||
native_ptr.parts[0] = native_ptr_part0;
|
||||
native_ptr.parts[1] = native_ptr_part1;
|
||||
return wasm_runtime_addr_native_to_app(module_inst,
|
||||
(void*)(uintptr_t)native_ptr.u64);
|
||||
}
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
|
||||
add functions to register. */
|
||||
@ -38,6 +143,11 @@ static NativeSymbol extended_native_symbol_defs[] = {
|
||||
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),
|
||||
};
|
||||
|
||||
int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
|
||||
|
||||
Reference in New Issue
Block a user