Refine wgl lib and refine wasm function index check (#122)

Refine wgl lib: remove module_inst parameter from widget functions
Refine wasm function check: move function index check from interpreter call_indirect to runtime instantiate
This commit is contained in:
wenyongh 2019-09-16 14:49:17 +08:00 committed by GitHub
parent ff0267b7e6
commit 6e99a37bf2
9 changed files with 65 additions and 41 deletions

View File

@ -51,7 +51,8 @@ wasm_btn_native_call(wasm_module_inst_t module_inst,
{ {
uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(btn_native_func_defs, wgl_native_func_call(module_inst,
btn_native_func_defs,
size, size,
func_id, func_id,
argv_offset, argv_offset,

View File

@ -23,12 +23,14 @@
/* ------------------------------------------------------------------------- /* -------------------------------------------------------------------------
* Label widget native function wrappers * Label widget native function wrappers
* -------------------------------------------------------------------------*/ * -------------------------------------------------------------------------*/
static int32 _cb_create(lv_obj_t *par, lv_obj_t *copy) static int32
_cb_create(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);
} }
static int32 _cb_get_text_length(lv_obj_t *cb) static int32
_cb_get_text_length(lv_obj_t *cb)
{ {
const char *text = lv_cb_get_text(cb); const char *text = lv_cb_get_text(cb);
@ -38,9 +40,9 @@ static int32 _cb_get_text_length(lv_obj_t *cb)
return strlen(text); return strlen(text);
} }
static int32 _cb_get_text(lv_obj_t *cb, char *buffer, int buffer_len) static char *
_cb_get_text(lv_obj_t *cb, char *buffer, int buffer_len)
{ {
wasm_module_inst_t module_inst = get_module_inst();
const char *text = lv_cb_get_text(cb); const char *text = lv_cb_get_text(cb);
if (text == NULL) if (text == NULL)
@ -49,7 +51,7 @@ static int32 _cb_get_text(lv_obj_t *cb, char *buffer, int buffer_len)
strncpy(buffer, text, buffer_len - 1); strncpy(buffer, text, buffer_len - 1);
buffer[buffer_len - 1] = '\0'; buffer[buffer_len - 1] = '\0';
return addr_native_to_app(buffer); return buffer;
} }
static WGLNativeFuncDef cb_native_func_defs[] = { static WGLNativeFuncDef cb_native_func_defs[] = {
@ -57,7 +59,7 @@ static WGLNativeFuncDef cb_native_func_defs[] = {
{ CB_FUNC_ID_SET_TEXT, lv_cb_set_text, NO_RET, 2, {0, -1}, {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_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_LENGTH, _cb_get_text_length, HAS_RET, 1, {0, -1}, {-1} },
{ CB_FUNC_ID_GET_TEXT, _cb_get_text, HAS_RET, 3, {0, -1}, {1, -1} }, { CB_FUNC_ID_GET_TEXT, _cb_get_text, RET_PTR, 3, {0, -1}, {1, -1} },
}; };
/*************** Native Interface to Wasm App ***********/ /*************** Native Interface to Wasm App ***********/
@ -67,7 +69,8 @@ wasm_cb_native_call(wasm_module_inst_t module_inst,
{ {
uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(cb_native_func_defs, wgl_native_func_call(module_inst,
cb_native_func_defs,
size, size,
func_id, func_id,
argv_offset, argv_offset,

View File

@ -23,12 +23,14 @@
/* ------------------------------------------------------------------------- /* -------------------------------------------------------------------------
* Label widget native function wrappers * Label widget native function wrappers
* -------------------------------------------------------------------------*/ * -------------------------------------------------------------------------*/
static int32 _label_create(lv_obj_t *par, lv_obj_t *copy) static int32
_label_create(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);
} }
static int32 _label_get_text_length(lv_obj_t *label) static int32
_label_get_text_length(lv_obj_t *label)
{ {
char *text = lv_label_get_text(label); char *text = lv_label_get_text(label);
@ -38,9 +40,9 @@ static int32 _label_get_text_length(lv_obj_t *label)
return strlen(text); return strlen(text);
} }
static int32 _label_get_text(lv_obj_t *label, char *buffer, int buffer_len) static char *
_label_get_text(lv_obj_t *label, char *buffer, int buffer_len)
{ {
wasm_module_inst_t module_inst = get_module_inst();
char *text = lv_label_get_text(label); char *text = lv_label_get_text(label);
if (text == NULL) if (text == NULL)
@ -49,14 +51,14 @@ static int32 _label_get_text(lv_obj_t *label, char *buffer, int buffer_len)
strncpy(buffer, text, buffer_len - 1); strncpy(buffer, text, buffer_len - 1);
buffer[buffer_len - 1] = '\0'; buffer[buffer_len - 1] = '\0';
return addr_native_to_app(buffer); return buffer;
} }
static WGLNativeFuncDef label_native_func_defs[] = { 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_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_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_LENGTH, _label_get_text_length, HAS_RET, 1, {0, -1}, {-1} },
{ LABEL_FUNC_ID_GET_TEXT, _label_get_text, HAS_RET, 3, {0, -1}, {1, -1} }, { LABEL_FUNC_ID_GET_TEXT, _label_get_text, RET_PTR, 3, {0, -1}, {1, -1} },
}; };
/*************** Native Interface to Wasm App ***********/ /*************** Native Interface to Wasm App ***********/
@ -66,7 +68,8 @@ wasm_label_native_call(wasm_module_inst_t module_inst,
{ {
uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(label_native_func_defs, wgl_native_func_call(module_inst,
label_native_func_defs,
size, size,
func_id, func_id,
argv_offset, argv_offset,

View File

@ -57,7 +57,8 @@ wasm_list_native_call(wasm_module_inst_t module_inst,
{ {
uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(list_native_func_defs, wgl_native_func_call(module_inst,
list_native_func_defs,
size, size,
func_id, func_id,
argv_offset, argv_offset,

View File

@ -7,7 +7,7 @@
#include <stdint.h> #include <stdint.h>
#define THROW_EXC(msg) wasm_runtime_set_exception(get_module_inst(), msg); #define THROW_EXC(msg) wasm_runtime_set_exception(module_inst, msg);
void void
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
@ -44,7 +44,8 @@ uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy)
return 0; return 0;
} }
static void invokeNative(intptr_t argv[], uint32 argc, void (*native_code)()) static void invokeNative(wasm_module_inst_t module_inst,
intptr_t argv[], uint32 argc, void (*native_code)())
{ {
switch(argc) { switch(argc) {
case 0: case 0:
@ -87,20 +88,20 @@ static void invokeNative(intptr_t argv[], uint32 argc, void (*native_code)())
default: default:
/* FIXME: If this happen, add more cases. */ /* FIXME: If this happen, add more cases. */
wasm_runtime_set_exception(get_module_inst(), THROW_EXC("the argument number of native function exceeds maximum");
"the argument number of native function exceeds maximum");
return; return;
} }
} }
typedef void (*GenericFunctionPointer)(); typedef void (*GenericFunctionPointer)();
typedef int32 (*Int32FuncPtr)(intptr_t *, uint32, GenericFunctionPointer); typedef int32 (*Int32FuncPtr)(wasm_module_inst_t, intptr_t *, uint32, GenericFunctionPointer);
typedef void (*VoidFuncPtr)(intptr_t *, uint32, GenericFunctionPointer); typedef void (*VoidFuncPtr)(wasm_module_inst_t, intptr_t *, uint32, GenericFunctionPointer);
static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative; static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative; static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
void wgl_native_func_call(WGLNativeFuncDef *funcs, void wgl_native_func_call(wasm_module_inst_t module_inst,
WGLNativeFuncDef *funcs,
uint32 size, uint32 size,
int32 func_id, int32 func_id,
uint32 argv_offset, uint32 argv_offset,
@ -109,7 +110,6 @@ void wgl_native_func_call(WGLNativeFuncDef *funcs,
WGLNativeFuncDef *func_def = funcs; WGLNativeFuncDef *func_def = funcs;
WGLNativeFuncDef *func_def_end = func_def + size; WGLNativeFuncDef *func_def_end = func_def + size;
uint32 *argv; uint32 *argv;
wasm_module_inst_t module_inst = get_module_inst();
if (!validate_app_addr(argv_offset, argc * sizeof(uint32))) if (!validate_app_addr(argv_offset, argc * sizeof(uint32)))
return; return;
@ -173,13 +173,20 @@ void wgl_native_func_call(WGLNativeFuncDef *funcs,
} }
if (func_def->has_ret == NO_RET) if (func_def->has_ret == NO_RET)
invokeNative_Void(argv_copy, invokeNative_Void(module_inst,
argv_copy,
func_def->arg_num, func_def->arg_num,
func_def->func_ptr); func_def->func_ptr);
else else {
argv[0] = invokeNative_Int32(argv_copy, argv[0] = invokeNative_Int32(module_inst,
argv_copy,
func_def->arg_num, func_def->arg_num,
func_def->func_ptr); func_def->func_ptr);
/* Convert to app memory offset if return value is a
* native address pointer */
if (func_def->has_ret == RET_PTR)
argv[0] = addr_native_to_app((char *)(intptr_t)argv[0]);
}
if (argv_copy != argv_copy_buf) if (argv_copy != argv_copy_buf)
bh_free(argv_copy); bh_free(argv_copy);

View File

@ -17,8 +17,12 @@ extern "C" {
#define NULL_OK 0x80 #define NULL_OK 0x80
enum { enum {
/* The function has a normal return value (not a pointer) */
HAS_RET, HAS_RET,
NO_RET /* The function doesn't have return value */
NO_RET,
/* The function's return value is a native address pointer */
RET_PTR
}; };
enum { enum {
@ -61,15 +65,13 @@ uint32 wgl_native_wigdet_create(int8 widget_type,
lv_obj_t *par, lv_obj_t *par,
lv_obj_t *copy); lv_obj_t *copy);
void wgl_native_func_call(WGLNativeFuncDef *funcs, void wgl_native_func_call(wasm_module_inst_t module_inst,
WGLNativeFuncDef *funcs,
uint32 size, uint32 size,
int32 func_id, int32 func_id,
uint32 argv_offset, uint32 argv_offset,
uint32 argc); uint32 argc);
wasm_module_inst_t wasm_runtime_get_current_module_inst();
#define get_module_inst() wasm_runtime_get_current_module_inst()
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -347,7 +347,8 @@ wasm_obj_native_call(wasm_module_inst_t module_inst,
{ {
uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(obj_native_func_defs, wgl_native_func_call(module_inst,
obj_native_func_defs,
size, size,
func_id, func_id,
argv_offset, argv_offset,

View File

@ -880,11 +880,8 @@ wasm_interp_call_func_bytecode(WASMThread *self,
} }
fidx = ((uint32*)table->base_addr)[val]; fidx = ((uint32*)table->base_addr)[val];
if (fidx >= module->function_count) { /* Skip function index check, it has been checked
wasm_runtime_set_exception(module, "function index is overflow"); in wasm module instantiate */
goto got_exception;
}
cur_func = module->functions + fidx; cur_func = module->functions + fidx;
if (cur_func->is_import_func) if (cur_func->is_import_func)

View File

@ -741,7 +741,7 @@ wasm_runtime_instantiate(WASMModule *module,
WASMTableSeg *table_seg; WASMTableSeg *table_seg;
WASMDataSeg *data_seg; WASMDataSeg *data_seg;
WASMGlobalInstance *globals = NULL, *global; WASMGlobalInstance *globals = NULL, *global;
uint32 global_count, addr_data_size = 0, global_data_size = 0, i; uint32 global_count, addr_data_size = 0, global_data_size = 0, i, j;
uint32 base_offset, length, memory_size; uint32 base_offset, length, memory_size;
uint8 *global_data, *global_data_end, *addr_data, *addr_data_end; uint8 *global_data, *global_data_end, *addr_data, *addr_data_end;
uint8 *memory_data; uint8 *memory_data;
@ -927,6 +927,15 @@ wasm_runtime_instantiate(WASMModule *module,
module_inst->default_table->cur_size) module_inst->default_table->cur_size)
length = module_inst->default_table->cur_size length = module_inst->default_table->cur_size
- table_seg->base_offset.u.i32; - table_seg->base_offset.u.i32;
/* Check function index */
for (j = 0; j < length; j++) {
if (table_seg->func_indexes[j] >= module_inst->function_count) {
set_error_buf(error_buf, error_buf_size,
"function index is overflow");
wasm_runtime_deinstantiate(module_inst);
return NULL;
}
}
memcpy(table_data + table_seg->base_offset.u.i32, memcpy(table_data + table_seg->base_offset.u.i32,
table_seg->func_indexes, length * sizeof(uint32)); table_seg->func_indexes, length * sizeof(uint32));
} }
@ -1618,9 +1627,9 @@ wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
break; break;
case VALUE_TYPE_F32: case VALUE_TYPE_F32:
if (n_fps < MAX_REG_FLOATS) if (n_fps < MAX_REG_FLOATS)
*(float64*)&fps[n_fps++] = *(float32*)argv_src++; *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
else else
*(float64*)&stacks[n_stacks++] = *(float32*)argv_src++; *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
break; break;
case VALUE_TYPE_F64: case VALUE_TYPE_F64:
if (n_fps < MAX_REG_FLOATS) if (n_fps < MAX_REG_FLOATS)