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

@ -22,25 +22,115 @@
/* -------------------------------------------------------------------------
* Button widget native function wrappers
* -------------------------------------------------------------------------*/
static int32 _btn_create(lv_obj_t *par, lv_obj_t *copy)
static int32
lv_btn_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_BTN, par, copy);
return wgl_native_wigdet_create(WIDGET_TYPE_BTN, par, copy, module_inst);
}
static void
lv_btn_set_toggle_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, bool tgl)
{
(void)module_inst;
lv_btn_set_toggle(btn, tgl);
}
static void
lv_btn_set_state_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, lv_btn_state_t state)
{
(void)module_inst;
lv_btn_set_state(btn, state);
}
static void
lv_btn_set_ink_in_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, uint16_t time)
{
(void)module_inst;
lv_btn_set_ink_in_time(btn, time);
}
static void
lv_btn_set_ink_out_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, uint16_t time)
{
(void)module_inst;
lv_btn_set_ink_out_time(btn, time);
}
static void
lv_btn_set_ink_wait_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, uint16_t time)
{
(void)module_inst;
lv_btn_set_ink_wait_time(btn, time);
}
static uint16_t
lv_btn_get_ink_in_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_ink_in_time(btn);
}
static uint16_t
lv_btn_get_ink_out_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_ink_out_time(btn);
}
static uint16_t
lv_btn_get_ink_wait_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_ink_wait_time(btn);
}
static lv_btn_state_t
lv_btn_get_state_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_state(btn);
}
static bool
lv_btn_get_toggle_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_toggle(btn);
}
static void
lv_btn_toggle_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
lv_btn_toggle(btn);
}
static WGLNativeFuncDef btn_native_func_defs[] = {
{ BTN_FUNC_ID_CREATE, _btn_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_STATE, lv_btn_set_state, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_CREATE, lv_btn_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_SET_STATE, lv_btn_set_state_wrapper, NO_RET, 3, {1, -1}, {-1} },
// { BTN_FUNC_ID_SET_STYLE, _btn_set_style, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_STATE, lv_btn_get_state, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle, HAS_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_TOGGLE, lv_btn_toggle, NO_RET, 1, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_STATE, lv_btn_get_state_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, NO_RET, 2, {1, -1}, {-1} },
};

View File

@ -24,13 +24,31 @@
* Label widget native function wrappers
* -------------------------------------------------------------------------*/
static int32
_cb_create(lv_obj_t *par, lv_obj_t *copy)
lv_cb_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_CB, par, copy);
return wgl_native_wigdet_create(WIDGET_TYPE_CB, par, copy, module_inst);
}
static void
lv_cb_set_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * cb, const char * txt)
{
(void)module_inst;
lv_cb_set_text(cb, txt);
}
static void
lv_cb_set_static_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * cb, const char * txt)
{
(void)module_inst;
lv_cb_set_static_text(cb, txt);
}
static int32
_cb_get_text_length(lv_obj_t *cb)
lv_cb_get_text_length_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *cb)
{
const char *text = lv_cb_get_text(cb);
@ -41,7 +59,8 @@ _cb_get_text_length(lv_obj_t *cb)
}
static char *
_cb_get_text(lv_obj_t *cb, char *buffer, int buffer_len)
lv_cb_get_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *cb, char *buffer, int buffer_len)
{
const char *text = lv_cb_get_text(cb);
@ -55,11 +74,11 @@ _cb_get_text(lv_obj_t *cb, char *buffer, int buffer_len)
}
static WGLNativeFuncDef cb_native_func_defs[] = {
{ CB_FUNC_ID_CREATE, _cb_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ CB_FUNC_ID_SET_TEXT, lv_cb_set_text, NO_RET, 2, {0, -1}, {1, -1} },
{ CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text, NO_RET, 2, {0, -1}, {1, -1} },
{ CB_FUNC_ID_GET_TEXT_LENGTH, _cb_get_text_length, HAS_RET, 1, {0, -1}, {-1} },
{ CB_FUNC_ID_GET_TEXT, _cb_get_text, RET_PTR, 3, {0, -1}, {1, -1} },
{ CB_FUNC_ID_CREATE, lv_cb_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ CB_FUNC_ID_SET_TEXT, lv_cb_set_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
{ CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
{ CB_FUNC_ID_GET_TEXT_LENGTH, lv_cb_get_text_length_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ CB_FUNC_ID_GET_TEXT, lv_cb_get_text_wrapper, RET_PTR, 4, {1, -1}, {2, -1} },
};
/*************** Native Interface to Wasm App ***********/

View File

@ -24,13 +24,23 @@
* Label widget native function wrappers
* -------------------------------------------------------------------------*/
static int32
_label_create(lv_obj_t *par, lv_obj_t *copy)
lv_label_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par, copy);
return wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par, copy, module_inst);
}
static void
lv_label_set_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * label, const char * text)
{
(void)module_inst;
lv_label_set_text(label, text);
}
static int32
_label_get_text_length(lv_obj_t *label)
lv_label_get_text_length_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *label)
{
char *text = lv_label_get_text(label);
@ -41,7 +51,8 @@ _label_get_text_length(lv_obj_t *label)
}
static char *
_label_get_text(lv_obj_t *label, char *buffer, int buffer_len)
lv_label_get_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *label, char *buffer, int buffer_len)
{
char *text = lv_label_get_text(label);
@ -55,10 +66,10 @@ _label_get_text(lv_obj_t *label, char *buffer, int buffer_len)
}
static WGLNativeFuncDef label_native_func_defs[] = {
{ LABEL_FUNC_ID_CREATE, _label_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ LABEL_FUNC_ID_SET_TEXT, lv_label_set_text, NO_RET, 2, {0, -1}, {1, -1} },
{ LABEL_FUNC_ID_GET_TEXT_LENGTH, _label_get_text_length, HAS_RET, 1, {0, -1}, {-1} },
{ LABEL_FUNC_ID_GET_TEXT, _label_get_text, RET_PTR, 3, {0, -1}, {1, -1} },
{ LABEL_FUNC_ID_CREATE, lv_label_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ LABEL_FUNC_ID_SET_TEXT, lv_label_set_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
{ LABEL_FUNC_ID_GET_TEXT_LENGTH, lv_label_get_text_length_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ LABEL_FUNC_ID_GET_TEXT, lv_label_get_text_wrapper, RET_PTR, 4, {1, -1}, {2, -1} },
};
/*************** Native Interface to Wasm App ***********/

View File

@ -22,12 +22,16 @@
/* -------------------------------------------------------------------------
* List widget native function wrappers
* -------------------------------------------------------------------------*/
static int32 _list_create(lv_obj_t *par, lv_obj_t *copy)
static int32
lv_list_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_LIST, par, copy);
return wgl_native_wigdet_create(WIDGET_TYPE_LIST, par, copy, module_inst);
}
static int32 _list_add_btn(lv_obj_t *list, const char *text)
static int32
lv_list_add_btn_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *list, const char *text)
{
uint32 btn_obj_id;
lv_obj_t *btn;
@ -38,7 +42,8 @@ static int32 _list_add_btn(lv_obj_t *list, const char *text)
return 0;
if (wgl_native_add_object(btn,
app_manager_get_module_id(Module_WASM_App),
app_manager_get_module_id(Module_WASM_App,
module_inst),
&btn_obj_id))
return btn_obj_id; /* success return */
@ -46,8 +51,8 @@ static int32 _list_add_btn(lv_obj_t *list, const char *text)
}
static WGLNativeFuncDef list_native_func_defs[] = {
{ LIST_FUNC_ID_CREATE, _list_create, HAS_RET, 2, {0 | NULL_OK, 1 | NULL_OK, -1}, {-1} },
{ LIST_FUNC_ID_ADD_BTN, _list_add_btn, HAS_RET, 2, {0, -1}, {1, -1} },
{ LIST_FUNC_ID_CREATE, lv_list_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, HAS_RET, 3, {1, -1}, {2, -1} },
};
/*************** Native Interface to Wasm App ***********/

View File

@ -4,6 +4,7 @@
#include "lvgl.h"
#include "module_wasm_app.h"
#include "wasm_export.h"
#include "wasm_assert.h"
#include <stdint.h>
@ -12,7 +13,8 @@
void
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy)
uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy,
wasm_module_inst_t module_inst)
{
uint32 obj_id;
lv_obj_t *wigdet;
@ -37,20 +39,19 @@ uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy)
return 0;
if (wgl_native_add_object(wigdet,
app_manager_get_module_id(Module_WASM_App),
app_manager_get_module_id(Module_WASM_App,
module_inst),
&obj_id))
return obj_id; /* success return */
return 0;
}
static void invokeNative(wasm_module_inst_t module_inst,
intptr_t argv[], uint32 argc, void (*native_code)())
static void invokeNative(intptr_t argv[], uint32 argc, void (*native_code)())
{
wasm_assert(argc >= 1);
switch(argc) {
case 0:
native_code();
break;
case 1:
native_code(argv[0]);
break;
@ -87,15 +88,18 @@ static void invokeNative(wasm_module_inst_t module_inst,
break;
default:
{
/* FIXME: If this happen, add more cases. */
wasm_module_inst_t module_inst = (wasm_module_inst_t)argv[0];
THROW_EXC("the argument number of native function exceeds maximum");
return;
}
}
}
typedef void (*GenericFunctionPointer)();
typedef int32 (*Int32FuncPtr)(wasm_module_inst_t, intptr_t *, uint32, GenericFunctionPointer);
typedef void (*VoidFuncPtr)(wasm_module_inst_t, intptr_t *, uint32, GenericFunctionPointer);
typedef int32 (*Int32FuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
typedef void (*VoidFuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
@ -118,11 +122,13 @@ void wgl_native_func_call(wasm_module_inst_t module_inst,
while (func_def < func_def_end) {
if (func_def->func_id == func_id) {
int i, obj_arg_num = 0, ptr_arg_num = 0;
int i, obj_arg_num = 0, ptr_arg_num = 0, argc1 = 0;
intptr_t argv_copy_buf[16];
intptr_t *argv_copy = argv_copy_buf;
if (func_def->arg_num > 16) {
argc1++; /* module_inst */
argc1 += func_def->arg_num;
if (argc1 > 16) {
argv_copy = (intptr_t *)bh_malloc(func_def->arg_num *
sizeof(intptr_t));
if (argv_copy == NULL)
@ -130,8 +136,9 @@ void wgl_native_func_call(wasm_module_inst_t module_inst,
}
/* Init argv_copy */
argv_copy[0] = (intptr_t)module_inst;
for (i = 0; i < func_def->arg_num; i++)
argv_copy[i] = (intptr_t)argv[i];
argv_copy[i + 1] = (intptr_t)argv[i];
/* Validate object arguments */
i = 0;
@ -143,7 +150,7 @@ void wgl_native_func_call(wasm_module_inst_t module_inst,
index = index & (~NULL_OK);
/* Some API's allow to pass NULL obj, such as xxx_create() */
if (argv[index] == 0) {
if (argv_copy[index] == 0) {
if (!null_ok) {
THROW_EXC("the object id is 0 and invalid");
goto fail;
@ -152,7 +159,8 @@ void wgl_native_func_call(wasm_module_inst_t module_inst,
continue;
}
if (!wgl_native_validate_object(argv[index], (lv_obj_t **)&argv_copy[index])) {
if (!wgl_native_validate_object(argv_copy[index],
(lv_obj_t **)&argv_copy[index])) {
THROW_EXC("the object is invalid");
goto fail;
}
@ -165,22 +173,20 @@ void wgl_native_func_call(wasm_module_inst_t module_inst,
uint8 index = func_def->ptr_arg_indexes[i];
/* The index+1 arg is the data size to be validated */
if (!validate_app_addr(argv[index], argv[index + 1]))
if (!validate_app_addr(argv_copy[index], argv_copy[index + 1]))
goto fail;
/* Convert to native address before call lvgl function */
argv_copy[index] = (intptr_t)addr_app_to_native(argv[index]);
argv_copy[index] = (intptr_t)addr_app_to_native(argv_copy[index]);
}
if (func_def->has_ret == NO_RET)
invokeNative_Void(module_inst,
argv_copy,
func_def->arg_num,
invokeNative_Void(argv_copy,
argc1,
func_def->func_ptr);
else {
argv[0] = invokeNative_Int32(module_inst,
argv_copy,
func_def->arg_num,
argv[0] = invokeNative_Int32(argv_copy,
argc1,
func_def->func_ptr);
/* Convert to app memory offset if return value is a
* native address pointer */

View File

@ -63,7 +63,8 @@ bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id);
uint32 wgl_native_wigdet_create(int8 widget_type,
lv_obj_t *par,
lv_obj_t *copy);
lv_obj_t *copy,
wasm_module_inst_t module_inst);
void wgl_native_func_call(wasm_module_inst_t module_inst,
WGLNativeFuncDef *funcs,

View File

@ -306,8 +306,11 @@ void wgl_init(void)
/* -------------------------------------------------------------------------
* Obj native function wrappers
* -------------------------------------------------------------------------*/
static lv_res_t _obj_del(lv_obj_t *obj)
static lv_res_t
lv_obj_del_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
{
(void)module_inst;
/* Recursively delete object node in the list belong to this
* parent object including itself */
_obj_del_recursive(obj);
@ -315,8 +318,18 @@ static lv_res_t _obj_del(lv_obj_t *obj)
return lv_obj_del(obj);
}
static void _obj_clean(lv_obj_t *obj)
static void
lv_obj_del_async_wrapper(wasm_module_inst_t module_inst, lv_obj_t * obj)
{
(void)module_inst;
lv_obj_del_async(obj);
}
static void
lv_obj_clean_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
{
(void)module_inst;
/* Recursively delete child object node in the list belong to this
* parent object */
_obj_clean_recursive(obj);
@ -325,19 +338,33 @@ static void _obj_clean(lv_obj_t *obj)
lv_obj_clean(obj);
}
static void _obj_set_event_cb(lv_obj_t *obj)
static void
lv_obj_align_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * obj,
const lv_obj_t * base,
lv_align_t align,
lv_coord_t x_mod,
lv_coord_t y_mod)
{
(void)module_inst;
lv_obj_align(obj, base, align, x_mod, y_mod);
}
static void
lv_obj_set_event_cb_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
{
(void)module_inst;
lv_obj_set_event_cb(obj, internal_lv_obj_event_cb);
}
/* ------------------------------------------------------------------------- */
static WGLNativeFuncDef obj_native_func_defs[] = {
{ OBJ_FUNC_ID_DEL, _obj_del, HAS_RET, 1, {0, -1}, {-1} },
{ OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async, NO_RET, 1, {0, -1}, {-1} },
{ OBJ_FUNC_ID_CLEAN, _obj_clean, NO_RET, 1, {0, -1}, {-1} },
{ OBJ_FUNC_ID_ALIGN, lv_obj_align, NO_RET, 5, {0, 1 | NULL_OK, -1}, {-1} },
{ OBJ_FUNC_ID_SET_EVT_CB, _obj_set_event_cb, NO_RET, 1, {0, -1}, {-1} },
{ OBJ_FUNC_ID_DEL, lv_obj_del_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async_wrapper, NO_RET, 2, {1, -1}, {-1} },
{ OBJ_FUNC_ID_CLEAN, lv_obj_clean_wrapper, NO_RET, 2, {1, -1}, {-1} },
{ OBJ_FUNC_ID_ALIGN, lv_obj_align_wrapper, NO_RET, 6, {1, 2 | NULL_OK, -1}, {-1} },
{ OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, NO_RET, 2, {1, -1}, {-1} },
};
/*************** Native Interface to Wasm App ***********/