Remove the binding between current thread and module instance and bugs fix (#131)

Remove wasm_export_api.h that may confuse
Implement wasm_runtime_validate_app_str_addr()
Fix bugs of loader and pass more spec cases

Signed-off-by: Weining Lu <weining.x.lu@intel.com>
This commit is contained in:
Weining
2019-10-11 15:25:23 +08:00
committed by wenyongh
parent bbae4426a0
commit 2a8b1ef454
37 changed files with 496 additions and 552 deletions

View File

@ -18,132 +18,11 @@
#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 void
wasm_runtime_get_current_module_inst1(wasm_module_inst_t module_inst,
int32 inst_offset)
{
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_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;
inst.parts[0] = inst_part0;
inst.parts[1] = inst_part1;
if (inst.u64 != (uint64)(uintptr_t)module_inst) {
bh_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_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;
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 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 false;
}
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_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;
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. */
@ -159,11 +38,6 @@ static NativeSymbol extended_native_symbol_defs[] = {
EXPORT_WASM_API(wasm_timer_restart),
EXPORT_WASM_API(wasm_get_sys_tick_ms),
#endif
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)

View File

@ -50,13 +50,14 @@ wasm_register_resource(wasm_module_inst_t module_inst, int32 url_offset)
{
char *url = NULL;
if (!validate_app_addr(url_offset, 1))
if (!validate_app_str_addr(url_offset))
return;
url = addr_app_to_native(url_offset);
if (url != NULL) {
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
am_register_resource(url, module_request_handler, mod_id);
}
}
@ -81,7 +82,8 @@ wasm_post_request(wasm_module_inst_t module_inst,
// TODO: add permission check, ensure app can't do harm
// set sender to help dispatch the response to the sender ap
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
req->sender = mod_id;
if (req->action == COAP_EVENT) {
@ -98,13 +100,14 @@ wasm_sub_event(wasm_module_inst_t module_inst, int32 url_offset)
{
char *url = NULL;
if (!validate_app_addr(url_offset, 1))
if (!validate_app_str_addr(url_offset))
return;
url = addr_app_to_native(url_offset);
if (url != NULL) {
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
am_register_event(url, mod_id);
}

View File

@ -141,9 +141,10 @@ void destroy_module_timer_ctx(unsigned int module_id)
vm_mutex_unlock(&g_timer_ctx_list_mutex);
}
timer_ctx_t get_wasm_timer_ctx()
timer_ctx_t get_wasm_timer_ctx(wasm_module_inst_t module_inst)
{
module_data * m = app_manager_get_module_data(Module_WASM_App);
module_data * m = app_manager_get_module_data(Module_WASM_App,
module_inst);
if (m == NULL)
return NULL;
return m->timer_ctx;
@ -153,27 +154,27 @@ timer_id_t
wasm_create_timer(wasm_module_inst_t module_inst,
int interval, bool is_period, bool auto_start)
{
return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
return sys_create_timer(get_wasm_timer_ctx(module_inst), interval, is_period,
auto_start);
}
void
wasm_timer_destroy(wasm_module_inst_t module_inst, timer_id_t timer_id)
{
sys_timer_destroy(get_wasm_timer_ctx(), timer_id);
sys_timer_destroy(get_wasm_timer_ctx(module_inst), timer_id);
}
void
wasm_timer_cancel(wasm_module_inst_t module_inst, timer_id_t timer_id)
{
sys_timer_cancel(get_wasm_timer_ctx(), timer_id);
sys_timer_cancel(get_wasm_timer_ctx(module_inst), timer_id);
}
void
wasm_timer_restart(wasm_module_inst_t module_inst,
timer_id_t timer_id, int interval)
{
sys_timer_restart(get_wasm_timer_ctx(), timer_id, interval);
sys_timer_restart(get_wasm_timer_ctx(module_inst), timer_id, interval);
}
extern uint32 get_sys_tick_ms();