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

@ -246,11 +246,13 @@ static int get_module_type(char *kv_str)
/* Queue callback of App Manager */
static void app_manager_queue_callback(void *message)
static void app_manager_queue_callback(void *message, void *arg)
{
request_t *request = (request_t *) bh_message_payload((bh_message_t)message);
int mid = request->mid, module_type, offset;
(void)arg;
if ((offset = check_url_start(request->url, strlen(request->url), "/applet"))
> 0) {
module_type = get_module_type(request->url + offset);
@ -376,7 +378,7 @@ void app_manager_startup(host_interface *interface)
app_manager_printf("App Manager started.\n");
/* Enter loop run */
bh_queue_enter_loop_run(g_app_mgr_queue, app_manager_queue_callback);
bh_queue_enter_loop_run(g_app_mgr_queue, app_manager_queue_callback, NULL);
fail2: module_data_list_destroy();

View File

@ -65,7 +65,7 @@ void
module_data_list_destroy();
bool
app_manager_is_interrupting_module(uint32 module_type);
app_manager_is_interrupting_module(uint32 module_type, void *module_inst);
void release_module(module_data *m_data);

View File

@ -130,35 +130,35 @@ module_data_list_lookup_id(unsigned int module_id)
}
module_data *
app_manager_get_module_data(uint32 module_type)
app_manager_get_module_data(uint32 module_type, void *module_inst)
{
if (g_module_interfaces[module_type]
&& g_module_interfaces[module_type]->module_get_module_data)
return g_module_interfaces[module_type]->module_get_module_data();
return g_module_interfaces[module_type]->module_get_module_data(module_inst);
return NULL;
}
void*
app_manager_get_module_queue(uint32 module_type)
app_manager_get_module_queue(uint32 module_type, void *module_inst)
{
return app_manager_get_module_data(module_type)->queue;
return app_manager_get_module_data(module_type, module_inst)->queue;
}
const char*
app_manager_get_module_name(uint32 module_type)
app_manager_get_module_name(uint32 module_type, void *module_inst)
{
return app_manager_get_module_data(module_type)->module_name;
return app_manager_get_module_data(module_type, module_inst)->module_name;
}
unsigned int app_manager_get_module_id(uint32 module_type)
unsigned int app_manager_get_module_id(uint32 module_type, void *module_inst)
{
return app_manager_get_module_data(module_type)->id;
return app_manager_get_module_data(module_type, module_inst)->id;
}
void*
app_manager_get_module_heap(uint32 module_type)
app_manager_get_module_heap(uint32 module_type, void *module_inst)
{
return app_manager_get_module_data(module_type)->heap;
return app_manager_get_module_data(module_type, module_inst)->heap;
}
module_data*
@ -179,9 +179,9 @@ void app_manager_del_module_data(module_data *m_data)
release_module(m_data);
}
bool app_manager_is_interrupting_module(uint32 module_type)
bool app_manager_is_interrupting_module(uint32 module_type, void *module_inst)
{
return app_manager_get_module_data(module_type)->wd_timer.is_interrupting;
return app_manager_get_module_data(module_type, module_inst)->wd_timer.is_interrupting;
}
extern void destroy_module_timer_ctx(unsigned int module_id);

View File

@ -81,7 +81,7 @@ static bool wasm_app_module_install(request_t *msg);
static bool wasm_app_module_uninstall(request_t *msg);
static void wasm_app_module_watchdog_kill(module_data *module_data);
static bool wasm_app_module_handle_host_url(void *queue_msg);
static module_data *wasm_app_module_get_module_data(void);
static module_data *wasm_app_module_get_module_data(void *inst);
static bool
wasm_app_module_on_install_request_byte_arrive(uint8 ch, int request_total_size,
int *received_size);
@ -110,14 +110,13 @@ static unsigned align_uint(unsigned v, unsigned b)
return (v + m) & ~m;
}
static void app_instance_queue_callback(void *queue_msg)
static void app_instance_queue_callback(void *queue_msg, void *arg)
{
uint32 argv[2];
wasm_function_inst_t func_onRequest, func_onTimer;
module_data *m_data = app_manager_get_module_data(Module_WASM_App);
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
wasm_module_inst_t inst = (wasm_module_inst_t)arg;
module_data *m_data = app_manager_get_module_data(Module_WASM_App, inst);
int message_type = bh_message_type(queue_msg);
switch (message_type) {
@ -262,17 +261,16 @@ wasm_app_routine(void *arg)
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
korp_tid thread = wasm_app_data->thread_id;
/* attach newly created thread to the VM managed instance */
if (!wasm_runtime_attach_current_thread(inst, m_data)) {
goto fail1;
}
/* Set m_data to the VM managed instance's custom data */
wasm_runtime_set_custom_data(inst, m_data);
app_manager_printf("WASM app '%s' started\n", m_data->module_name);
/* Call app's onInit() method */
func_onInit = wasm_runtime_lookup_function(inst, "_on_init", "()");
if (!func_onInit) {
app_manager_printf("Cannot find function on_init().\n");
goto fail2;
goto fail1;
}
if (!wasm_runtime_call_wasm(inst, NULL, func_onInit, 0, NULL)) {
@ -281,23 +279,21 @@ wasm_app_routine(void *arg)
wasm_runtime_clear_exception(inst);
/* call on_destroy() in case some resources are opened in on_init()
* and then exception thrown */
goto fail3;
goto fail2;
}
/* Enter queue loop run to receive and process applet queue message */
bh_queue_enter_loop_run(m_data->queue, app_instance_queue_callback);
bh_queue_enter_loop_run(m_data->queue, app_instance_queue_callback, inst);
app_manager_printf("App instance main thread exit.\n");
fail3:
fail2:
/* Call WASM app onDestroy() method if there is */
func_onDestroy = wasm_runtime_lookup_function(inst, "_on_destroy", "()");
if (func_onDestroy)
wasm_runtime_call_wasm(inst, NULL, func_onDestroy, 0, NULL);
fail2: wasm_runtime_detach_current_thread(inst);
fail1:
fail1:
vm_thread_detach(thread);
vm_thread_exit(NULL);
@ -548,8 +544,7 @@ static bool wasm_app_module_install(request_t * msg)
goto fail;
}
/* create a thread. This thread may not dedicate for this WASM app.
WASM app instance needs to attach to one thread */
/* Create WASM app thread. */
if (vm_thread_create(&wasm_app_data->thread_id, wasm_app_routine,
(void*) m_data, APP_THREAD_STACK_SIZE_DEFAULT) != 0) {
module_data_list_remove(m_data);
@ -648,9 +643,10 @@ static bool wasm_app_module_handle_host_url(void *queue_msg)
}
static module_data*
wasm_app_module_get_module_data(void)
wasm_app_module_get_module_data(void *inst)
{
return wasm_runtime_get_current_thread_data();
wasm_module_inst_t module_inst = (wasm_module_inst_t)inst;
return (module_data *)wasm_runtime_get_custom_data(module_inst);
}
static void wasm_app_module_watchdog_kill(module_data *m_data)

View File

@ -46,8 +46,9 @@ static bool wasm_lib_module_handle_host_url(void *queue_msg)
}
static module_data*
wasm_lib_module_get_module_data(void)
wasm_lib_module_get_module_data(void *inst)
{
(void) inst;
return NULL;
}

View File

@ -105,7 +105,7 @@ typedef bool (*module_install_func)(request_t *msg);
typedef bool (*module_uninstall_func)(request_t *msg);
typedef void (*module_watchdog_kill_func)(module_data *module_data);
typedef bool (*module_handle_host_url_func)(void *queue_msg);
typedef module_data *(*module_get_module_data_func)(void);
typedef module_data *(*module_get_module_data_func)(void *inst);
/**
* @typedef module_on_install_request_byte_arrive_func
@ -194,24 +194,24 @@ app_manager_startup(host_interface *interface);
/* Get queue of current applet */
void *
app_manager_get_module_queue(uint32 module_type);
app_manager_get_module_queue(uint32 module_type, void *module_inst);
/* Get applet name of current applet */
const char *
app_manager_get_module_name(uint32 module_type);
app_manager_get_module_name(uint32 module_type, void *module_inst);
/* Get heap of current applet */
void *
app_manager_get_module_heap(uint32 module_type);
app_manager_get_module_heap(uint32 module_type, void *module_inst);
void*
get_app_manager_queue();
module_data*
app_manager_get_module_data(uint32 module_type);
app_manager_get_module_data(uint32 module_type, void *module_inst);
unsigned int
app_manager_get_module_id(uint32 module_type);
app_manager_get_module_id(uint32 module_type, void *module_inst);
module_data*
app_manager_lookup_module_data(const char *name);

View File

@ -24,6 +24,7 @@
wgl_obj_t wgl_btn_create(wgl_obj_t par, wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_CREATE);

View File

@ -25,6 +25,7 @@
wgl_obj_t wgl_cb_create(wgl_obj_t par, const wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_CREATE);

View File

@ -26,6 +26,7 @@
wgl_obj_t wgl_label_create(wgl_obj_t par, wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CREATE);

View File

@ -26,8 +26,10 @@
wgl_obj_t wgl_list_create(wgl_obj_t par, const wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_LIST_NATIVE_FUNC(LIST_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
@ -41,9 +43,10 @@ wgl_obj_t wgl_list_create(wgl_obj_t par, const wgl_obj_t copy)
wgl_obj_t wgl_list_add_btn(wgl_obj_t list, const void * img_src, const char * txt)
{
uint32 argv[3] = {0};
(void)img_src; /* doesn't support img src currently */
uint32 argv[3] = {0};
argv[0] = (uint32)list;
argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;

View File

@ -25,6 +25,9 @@
#define validate_app_addr(offset, size) \
wasm_runtime_validate_app_addr(module_inst, offset, size)
#define validate_app_str_addr(offset) \
wasm_runtime_validate_app_str_addr(module_inst, offset)
#define addr_app_to_native(offset) \
wasm_runtime_addr_app_to_native(module_inst, offset)

View File

@ -1,162 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _WASM_EXPORT_API_H
#define _WASM_EXPORT_API_H
#include <inttypes.h>
#include <stdbool.h>
/**
* API exported to WASM application
*/
#ifdef __cplusplus
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
*
* @return current WASM module instance of the current native thread, 0
* if not found
* Note: the return type is uint64_t but not pointer type, because that
* the we only supports WASM-32, in which the pointer type is
* compiled to WASM i32 type, but the pointer type in native can be
* 32-bit and 64-bit. And if the native pointer is 64-bit, data loss
* occurs after converting it to WASM i32 type.
*/
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
* instance's address space, or in its heap space or memory space.
*
* @param module_inst the WASM module instance
* @param app_offset the app address to validate, which is a relative address
* @param size the size bytes of the app address
*
* @return true if success, false otherwise.
*/
static inline bool
wasm_runtime_validate_app_addr(uint64_t module_inst,
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
* instance's address space, or in its heap space or memory space.
*
* @param module_inst the WASM module instance
* @param native_ptr the native address to validate, which is an absolute
* address
* @param size the size bytes of the app address
*
* @return true if success, false otherwise.
*/
static inline bool
wasm_runtime_validate_native_addr(uint64_t module_inst,
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)
*
* @param module_inst the WASM module instance
* @param app_offset the app adress
*
* @return the native address converted
*/
static inline uint64_t
wasm_runtime_addr_app_to_native(uint64_t module_inst,
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)
*
* @param module_inst the WASM module instance
* @param native_ptr the native address
*
* @return the app address converted
*/
static inline int32_t
wasm_runtime_addr_native_to_app(uint64_t module_inst,
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_API_H */

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();

View File

@ -18,6 +18,7 @@
#define CONNECTION_LIB_H_
#include "attr_container.h"
#include "wasm_export.h"
#ifdef __cplusplus
extern "C" {
@ -37,7 +38,8 @@ extern "C" {
*
* @return 0~0xFFFFFFFE means id of the connection, otherwise(-1) means fail
*/
typedef uint32 (*connection_open_f)(const char *name, attr_container_t *args);
typedef uint32 (*connection_open_f)(wasm_module_inst_t module_inst,
const char *name, attr_container_t *args);
/*
* @brief Close a connection.

View File

@ -30,7 +30,7 @@ wasm_open_connection(wasm_module_inst_t module_inst,
attr_container_t *args;
char *name, *args_buf;
if (!validate_app_addr(name_offset, 1) ||
if (!validate_app_str_addr(name_offset) ||
!validate_app_addr(args_offset, len) ||
!(name = addr_app_to_native(name_offset)) ||
!(args_buf = addr_app_to_native(args_offset)))
@ -39,7 +39,7 @@ wasm_open_connection(wasm_module_inst_t module_inst,
args = (attr_container_t *)args_buf;
if (connection_impl._open != NULL)
return connection_impl._open(name, args);
return connection_impl._open(module_inst, name, args);
return -1;
}

View File

@ -86,7 +86,8 @@ static struct epoll_event epoll_events[MAX_EVENTS];
/* Buffer to receive data */
static char io_buf[IO_BUF_SIZE];
static uint32 _conn_open(const char *name, attr_container_t *args);
static uint32 _conn_open(wasm_module_inst_t module_inst,
const char *name, attr_container_t *args);
static void _conn_close(uint32 handle);
static int _conn_send(uint32 handle, const char *data, int len);
static bool _conn_config(uint32 handle, attr_container_t *cfg);
@ -217,12 +218,14 @@ static conn_type_t get_conn_type(const char *name)
}
/* --- connection lib function --- */
static uint32 _conn_open(const char *name, attr_container_t *args)
static uint32 _conn_open(wasm_module_inst_t module_inst,
const char *name, attr_container_t *args)
{
int fd;
sys_connection_t *conn;
struct epoll_event ev;
uint32 module_id = app_manager_get_module_id(Module_WASM_App);
uint32 module_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP)
return -1;

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 ***********/

View File

@ -106,7 +106,8 @@ wasm_sensor_config(wasm_module_inst_t module_inst,
if (s == NULL)
return false;
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);
vm_mutex_lock(&s->lock);
@ -144,7 +145,7 @@ wasm_sensor_open(wasm_module_inst_t module_inst,
{
char *name = NULL;
if (!validate_app_addr(name_offset, 1))
if (!validate_app_str_addr(name_offset))
return -1;
name = addr_app_to_native(name_offset);
@ -155,7 +156,8 @@ wasm_sensor_open(wasm_module_inst_t module_inst,
if (s == NULL)
return -1;
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);
vm_mutex_lock(&s->lock);
@ -222,7 +224,8 @@ wasm_sensor_config_with_attr_container(wasm_module_inst_t module_inst,
bool
wasm_sensor_close(wasm_module_inst_t module_inst, uint32 sensor)
{
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);
unsigned int client_id = mod_id;
sensor_obj_t s = find_sys_sensor_id(sensor);
sensor_client_t *c;

View File

@ -37,6 +37,9 @@ wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
#define validate_app_addr(offset, size) \
wasm_runtime_validate_app_addr(module_inst, offset, size)
#define validate_app_str_addr(offset) \
wasm_runtime_validate_app_str_addr(module_inst, offset)
#define addr_app_to_native(offset) \
wasm_runtime_addr_app_to_native(module_inst, offset)
@ -49,29 +52,6 @@ wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
#define module_free(offset) \
wasm_runtime_module_free(module_inst, offset)
static bool
validate_str_addr(wasm_module_inst_t module_inst, int32 str_offset)
{
int32 app_end_offset;
char *str, *str_end;
if (!wasm_runtime_get_app_addr_range(module_inst, str_offset,
NULL, &app_end_offset))
goto fail;
str = addr_app_to_native(str_offset);
str_end = str + (app_end_offset - str_offset);
while (str < str_end && *str != '\0')
str++;
if (str == str_end)
goto fail;
return true;
fail:
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
return false;
}
typedef int (*out_func_t)(int c, void *ctx);
enum pad_type {
@ -335,7 +315,7 @@ _vprintf_wa(out_func_t out, void *ctx, const char *fmt, _va_list ap,
CHECK_VA_ARG(ap, uint32);
s_offset = _va_arg(ap, uint32);
if (!validate_str_addr(module_inst, s_offset)) {
if (!validate_app_str_addr(s_offset)) {
return false;
}
@ -438,7 +418,7 @@ parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
_va_list v;
} u;
if (!validate_str_addr(module_inst, fmt_offset)
if (!validate_app_str_addr(fmt_offset)
|| !validate_app_addr(va_list_offset, sizeof(int32)))
return false;
@ -539,7 +519,7 @@ _puts_wrapper(wasm_module_inst_t module_inst,
{
const char *str;
if (!validate_str_addr(module_inst, str_offset))
if (!validate_app_str_addr(str_offset))
return 0;
str = addr_app_to_native(str_offset);
@ -561,7 +541,7 @@ _strdup_wrapper(wasm_module_inst_t module_inst,
uint32 len;
int32 str_ret_offset = 0;
if (!validate_str_addr(module_inst, str_offset))
if (!validate_app_str_addr(str_offset))
return 0;
str = addr_app_to_native(str_offset);
@ -650,7 +630,7 @@ _strchr_wrapper(wasm_module_inst_t module_inst,
const char *s;
char *ret;
if (!validate_str_addr(module_inst, s_offset))
if (!validate_app_str_addr(s_offset))
return s_offset;
s = addr_app_to_native(s_offset);
@ -664,8 +644,8 @@ _strcmp_wrapper(wasm_module_inst_t module_inst,
{
void *s1, *s2;
if (!validate_str_addr(module_inst, s1_offset)
|| !validate_str_addr(module_inst, s2_offset))
if (!validate_app_str_addr(s1_offset)
|| !validate_app_str_addr(s2_offset))
return 0;
s1 = addr_app_to_native(s1_offset);
@ -695,7 +675,7 @@ _strcpy_wrapper(wasm_module_inst_t module_inst,
char *dst, *src;
uint32 len;
if (!validate_str_addr(module_inst, src_offset))
if (!validate_app_str_addr(src_offset))
return 0;
src = addr_app_to_native(src_offset);
@ -731,7 +711,7 @@ _strlen_wrapper(wasm_module_inst_t module_inst,
{
char *s;
if (!validate_str_addr(module_inst, s_offset))
if (!validate_app_str_addr(s_offset))
return 0;
s = addr_app_to_native(s_offset);

View File

@ -255,37 +255,23 @@ void
wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
/**
* Attach the current native thread to a WASM module instance.
* A native thread cannot be attached simultaneously to two WASM module
* instances. The WASM module instance will be attached to the native
* thread which it is instantiated in by default.
* Set custom data to WASM module instance.
*
* @param module_inst the WASM module instance to attach
* @param thread_data the thread data that current native thread requires
* the WASM module instance to store
*
* @return true if SUCCESS, false otherwise
*/
bool
wasm_runtime_attach_current_thread(wasm_module_inst_t module_inst,
void *thread_data);
/**
* Detach the current native thread from a WASM module instance.
*
* @param module_inst the WASM module instance to detach
* @param module_inst the WASM module instance
* @param custom_data the custom data to be set
*/
void
wasm_runtime_detach_current_thread(wasm_module_inst_t module_inst);
wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
void *custom_data);
/**
* Get the thread data that the current native thread requires the WASM
* module instance to store when attaching.
* Get the custom data within a WASM module instance.
*
* @return the thread data stored when attaching
* @param module_inst the WASM module instance
*
* @return the custom data (NULL if not set yet)
*/
void*
wasm_runtime_get_current_thread_data();
wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
/**
* Allocate memory from the heap of WASM module instance
@ -342,6 +328,23 @@ bool
wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
int32_t app_offset, uint32_t size);
/**
* Similar to wasm_runtime_validate_app_addr(), except that the size parameter
* is not provided. This function validates the app string address, check whether it
* belongs to WASM module instance's address space, or in its heap space or
* memory space. Moreover, it checks whether it is the offset of a string that
* is end with '\0'.
* @param module_inst the WASM module instance
* @param app_str_offset the app address of the string to validate, which is a
* relative address
*
* @return true if success, false otherwise. If failed, an exception will
* be thrown.
*/
bool
wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
int32_t app_str_offset);
/**
* Validate the native address, check whether it belongs to WASM module
* instance's address space, or in its heap space or memory space.

View File

@ -18,7 +18,8 @@
void invokeNative(void (*native_code)(), uint32 argv[], uint32 argc)
{
WASMThread *self;
wasm_assert(argc >= sizeof(WASMModuleInstance *)/sizeof(uint32));
switch(argc) {
case 0:
native_code();
@ -84,9 +85,11 @@ void invokeNative(void (*native_code)(), uint32 argv[], uint32 argc)
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19]);
break;
default:
{
/* FIXME: If this happen, add more cases. */
self = wasm_runtime_get_self();
wasm_runtime_set_exception(self->module_inst, "the argument number of native function exceeds maximum");
WASMModuleInstance *module_inst = *(WASMModuleInstance**)argv;
wasm_runtime_set_exception(module_inst, "the argument number of native function exceeds maximum");
return;
}
}
}

View File

@ -2178,10 +2178,11 @@ wasm_interp_call_func_bytecode(WASMThread *self,
}
void
wasm_interp_call_wasm(WASMFunctionInstance *function,
wasm_interp_call_wasm(WASMModuleInstance *module_inst,
WASMFunctionInstance *function,
uint32 argc, uint32 argv[])
{
WASMThread *self = wasm_runtime_get_self();
WASMThread *self = &module_inst->main_tlr;
WASMRuntimeFrame *prev_frame = wasm_thread_get_cur_frame(self);
WASMInterpFrame *frame, *outs_area;

View File

@ -23,6 +23,7 @@
extern "C" {
#endif
struct WASMModuleInstance;
struct WASMFunctionInstance;
typedef struct WASMInterpFrame {
@ -69,7 +70,8 @@ wasm_interp_interp_frame_size(unsigned all_cell_num)
}
void
wasm_interp_call_wasm(struct WASMFunctionInstance *function,
wasm_interp_call_wasm(struct WASMModuleInstance *module_inst,
struct WASMFunctionInstance *function,
uint32 argc, uint32 argv[]);
#ifdef __cplusplus

View File

@ -60,14 +60,14 @@ read_leb(const uint8 *buf, const uint8 *buf_end,
*p_offset += 1;
result |= ((byte & 0x7f) << shift);
shift += 7;
bcnt += 1;
if ((byte & 0x80) == 0) {
break;
}
bcnt += 1;
}
if (bcnt > (maxbits + 7 - 1) / 7) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: unsigned LEB overflow.");
"integer representation too long");
return false;
}
if (sign && (shift < maxbits) && (byte & 0x40)) {
@ -625,17 +625,19 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
const uint8 *p = buf, *p_end = buf_end;
const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
uint32 func_count, total_size;
uint32 code_count, code_size, type_index, i, j, k, local_type_index;
uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
uint32 local_count, local_set_count, sub_local_count;
uint8 type;
WASMFunction *func;
read_leb_uint32(p, p_end, func_count);
read_leb_uint32(p_code, buf_code_end, code_count);
if (buf_code)
read_leb_uint32(p_code, buf_code_end, code_count);
if (func_count != code_count) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: invalid function count.");
"function and code section have inconsistent lengths");
return false;
}
@ -677,6 +679,11 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
/* Calculate total local count */
for (j = 0; j < local_set_count; j++) {
read_leb_uint32(p_code, buf_code_end, sub_local_count);
if (sub_local_count > UINT32_MAX - local_count) {
set_error_buf(error_buf, error_buf_size,
"too many locals");
return false;
}
read_leb_uint8(p_code, buf_code_end, type);
local_count += sub_local_count;
}
@ -1084,11 +1091,29 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
}
static bool
load_code_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
load_code_section(const uint8 *buf, const uint8 *buf_end,
const uint8 *buf_func,
const uint8 *buf_func_end,
WASMModule *module,
char *error_buf, uint32 error_buf_size)
{
/* code has been loaded in function section, so pass it here */
/* TODO: should check if there really have section_size code bytes */
const uint8 *p = buf, *p_end = buf_end;
const uint8 *p_func = buf_func;
uint32 func_count = 0, code_count;
/* code has been loaded in function section, so pass it here, just check
* whether function and code section have inconsistent lengths */
read_leb_uint32(p, p_end, code_count);
if (buf_func)
read_leb_uint32(p_func, buf_func_end, func_count);
if (func_count != code_count) {
set_error_buf(error_buf, error_buf_size,
"function and code section have inconsistent lengths");
return false;
}
LOG_VERBOSE("Load code segment section success.\n");
return true;
}
@ -1132,14 +1157,19 @@ load_from_sections(WASMModule *module, WASMSection *sections,
char *error_buf, uint32 error_buf_size)
{
WASMSection *section = sections;
const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL;
const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
*buf_func = NULL, *buf_func_end = NULL;
uint32 i;
/* Find code and function sections if have */
while (section) {
if (section->section_type == SECTION_TYPE_CODE) {
buf_code = section->section_body;
buf_code_end = buf_code + section->section_body_size;
break;
}
else if (section->section_type == SECTION_TYPE_FUNC) {
buf_func = section->section_body;
buf_func_end = buf_func + section->section_body_size;
}
section = section->next;
}
@ -1151,6 +1181,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
switch (section->section_type) {
case SECTION_TYPE_USER:
/* unsupported user section, ignore it. */
/* add a check to pass spec test case */
CHECK_BUF(buf, buf_end, 1);
break;
case SECTION_TYPE_TYPE:
if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size))
@ -1161,11 +1193,6 @@ load_from_sections(WASMModule *module, WASMSection *sections,
return false;
break;
case SECTION_TYPE_FUNC:
if (!buf_code) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: find code section failed.");
return false;
}
if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
module, error_buf, error_buf_size))
return false;
@ -1195,7 +1222,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
return false;
break;
case SECTION_TYPE_CODE:
if (!load_code_section(buf, buf_end, module, error_buf, error_buf_size))
if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
module, error_buf, error_buf_size))
return false;
break;
case SECTION_TYPE_DATA:
@ -1356,7 +1384,7 @@ create_sections(const uint8 *buf, uint32 size,
p += section_size;
}
else {
set_error_buf(error_buf, error_buf_size, "invalid section type");
set_error_buf(error_buf, error_buf_size, "invalid section id");
return false;
}
}
@ -2540,7 +2568,14 @@ handle_op_br:
}
read_leb_uint32(p, p_end, type_idx);
read_leb_uint8(p, p_end, u8); /* 0x00 */
/* reserved byte 0x00 */
if (*p++ != 0x00) {
set_error_buf(error_buf, error_buf_size,
"zero flag expected");
goto fail;
}
POP_I32();
if (type_idx >= module->type_count) {
@ -2752,13 +2787,23 @@ handle_op_br:
case WASM_OP_MEMORY_SIZE:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* 0x00 */
/* reserved byte 0x00 */
if (*p++ != 0x00) {
set_error_buf(error_buf, error_buf_size,
"zero flag expected");
goto fail;
}
PUSH_I32();
break;
case WASM_OP_MEMORY_GROW:
CHECK_MEMORY();
read_leb_uint32(p, p_end, u32); /* 0x00 */
/* reserved byte 0x00 */
if (*p++ != 0x00) {
set_error_buf(error_buf, error_buf_size,
"zero flag expected");
goto fail;
}
POP_I32();
PUSH_I32();
break;

View File

@ -44,8 +44,6 @@ wasm_runtime_init()
if (ws_thread_sys_init() != 0)
return false;
wasm_runtime_set_tlr(NULL);
wasm_native_init();
return true;
}
@ -53,7 +51,6 @@ wasm_runtime_init()
void
wasm_runtime_destroy()
{
wasm_runtime_set_tlr(NULL);
ws_thread_sys_destroy();
}
@ -71,7 +68,7 @@ wasm_runtime_call_wasm(WASMModuleInstance *module_inst,
unsigned argc, uint32 argv[])
{
/* Only init stack when no application is running. */
if (!wasm_runtime_get_self()->cur_frame) {
if (!module_inst->main_tlr.cur_frame) {
if (!exec_env) {
if (!module_inst->wasm_stack) {
if (!(module_inst->wasm_stack =
@ -107,7 +104,7 @@ wasm_runtime_call_wasm(WASMModuleInstance *module_inst,
}
}
wasm_interp_call_wasm(function, argc, argv);
wasm_interp_call_wasm(module_inst, function, argc, argv);
return !wasm_runtime_get_exception(module_inst) ? true : false;
}
@ -959,9 +956,7 @@ wasm_runtime_instantiate(WASMModule *module,
module_inst->wasm_stack_size = stack_size;
module_inst->main_tlr.module_inst = module_inst;
/* Bind thread data with current native thread:
set thread local root to current thread. */
wasm_runtime_set_tlr(&module_inst->main_tlr);
/* The native thread handle may be used in future, e.g multiple threads. */
module_inst->main_tlr.handle = ws_self_thread();
/* Execute __post_instantiate and start function */
@ -1133,34 +1128,17 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *env)
}
}
bool
wasm_runtime_attach_current_thread(WASMModuleInstance *module_inst,
void *thread_data)
{
wasm_runtime_set_tlr(&module_inst->main_tlr);
module_inst->main_tlr.handle = ws_self_thread();
module_inst->thread_data = thread_data;
return true;
}
void
wasm_runtime_detach_current_thread(WASMModuleInstance *module_inst)
wasm_runtime_set_custom_data(WASMModuleInstance *module_inst,
void *custom_data)
{
module_inst->thread_data = NULL;
module_inst->custom_data = custom_data;
}
void*
wasm_runtime_get_current_thread_data()
wasm_runtime_get_custom_data(WASMModuleInstance *module_inst)
{
WASMThread *tlr = wasm_runtime_get_self();
return (tlr && tlr->module_inst) ? tlr->module_inst->thread_data : NULL;
}
WASMModuleInstance *
wasm_runtime_get_current_module_inst()
{
WASMThread *tlr = wasm_runtime_get_self();
return tlr ? tlr->module_inst : NULL;
return module_inst->custom_data;
}
int32
@ -1247,6 +1225,30 @@ fail:
return false;
}
bool
wasm_runtime_validate_app_str_addr(WASMModuleInstance *module_inst,
int32 app_str_offset)
{
int32 app_end_offset;
char *str, *str_end;
if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset,
NULL, &app_end_offset))
goto fail;
str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
str_end = str + (app_end_offset - app_str_offset);
while (str < str_end && *str != '\0')
str++;
if (str == str_end)
goto fail;
return true;
fail:
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
return false;
}
bool
wasm_runtime_validate_native_addr(WASMModuleInstance *module_inst,
void *native_ptr, uint32 size)

View File

@ -164,8 +164,9 @@ typedef struct WASMModuleInstance {
/* The exception buffer of wasm interpreter for current thread. */
char cur_exception[128];
/* The thread data of the attaching thread */
void *thread_data;
/* The custom data that can be set/get by
* wasm_runtime_set_custom_data/wasm_runtime_get_custom_data */
void *custom_data;
/* Main Thread */
WASMThread main_tlr;
@ -283,38 +284,57 @@ wasm_runtime_get_exception(WASMModuleInstance *module);
bool
wasm_runtime_enlarge_memory(WASMModuleInstance *module, int inc_page_count);
/* See wasm-export.h for description */
/* See wasm_export.h for description */
WASMModuleInstance *
wasm_runtime_get_current_module_inst();
/* See wasm-export.h for description */
/* See wasm_export.h for description */
int32
wasm_runtime_module_malloc(WASMModuleInstance *module_inst, uint32 size);
/* See wasm-export.h for description */
/* See wasm_export.h for description */
void
wasm_runtime_module_free(WASMModuleInstance *module_inst, int32 ptr);
/* See wasm-export.h for description */
/* See wasm_export.h for description */
bool
wasm_runtime_validate_app_addr(WASMModuleInstance *module_inst,
int32 app_offset, uint32 size);
/* See wasm-export.h for description */
/* See wasm_export.h for description */
bool
wasm_runtime_validate_app_str_addr(WASMModuleInstance *module_inst,
int32 app_offset);
/* See wasm_export.h for description */
bool
wasm_runtime_validate_native_addr(WASMModuleInstance *module_inst,
void *native_ptr, uint32 size);
/* See wasm-export.h for description */
/* See wasm_export.h for description */
void *
wasm_runtime_addr_app_to_native(WASMModuleInstance *module_inst,
int32 app_offset);
/* See wasm-export.h for description */
/* See wasm_export.h for description */
int32
wasm_runtime_addr_native_to_app(WASMModuleInstance *module_inst,
void *native_ptr);
/* See wasm_export.h for description */
bool
wasm_runtime_get_app_addr_range(WASMModuleInstance *module_inst,
int32_t app_offset,
int32_t *p_app_start_offset,
int32_t *p_app_end_offset);
/* See wasm_export.h for description */
bool
wasm_runtime_get_native_addr_range(WASMModuleInstance *module_inst,
uint8_t *native_ptr,
uint8_t **p_native_start_addr,
uint8_t **p_native_end_addr);
bool
wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
WASMModuleInstance *module_inst,

View File

@ -29,7 +29,7 @@ typedef struct _bh_queue_node * bh_message_t;
struct bh_queue;
typedef struct bh_queue bh_queue;
typedef void (*bh_queue_handle_msg_callback)(void *message);
typedef void (*bh_queue_handle_msg_callback)(void *message, void *arg);
#define bh_queue_malloc bh_malloc
#define bh_queue_free bh_free
@ -82,12 +82,8 @@ bh_queue_get_message_count(bh_queue *queue);
void
bh_queue_enter_loop_run(bh_queue *queue,
bh_queue_handle_msg_callback handle_cb);
void
bh_queue_enter_loop_run1(bh_queue *queue,
bh_queue_handle_msg_callback handle_cb);
bh_queue_handle_msg_callback handle_cb,
void *arg);
void
bh_queue_exit_loop_run(bh_queue *queue);

View File

@ -234,7 +234,8 @@ unsigned bh_queue_get_message_count(bh_queue *queue)
}
void bh_queue_enter_loop_run(bh_queue *queue,
bh_queue_handle_msg_callback handle_cb)
bh_queue_handle_msg_callback handle_cb,
void *arg)
{
if (!queue)
return;
@ -243,7 +244,7 @@ void bh_queue_enter_loop_run(bh_queue *queue,
bh_queue_node * message = bh_get_msg(queue, BH_WAIT_FOREVER);
if (message) {
handle_cb(message);
handle_cb(message, arg);
bh_free_msg(message);
}
}