WebAssembly Micro Runtime first version
This commit is contained in:
48
core/iwasm/lib/native/base/base-lib-export.c
Normal file
48
core/iwasm/lib/native/base/base-lib-export.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "lib-export.h"
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
#include "base-lib-export.h"
|
||||
#endif
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
|
||||
add functions to register. */
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
EXPORT_WASM_API(wasm_register_resource),
|
||||
EXPORT_WASM_API(wasm_response_send),
|
||||
EXPORT_WASM_API(wasm_post_request),
|
||||
EXPORT_WASM_API(wasm_sub_event),
|
||||
EXPORT_WASM_API(wasm_create_timer),
|
||||
EXPORT_WASM_API(wasm_timer_destory),
|
||||
EXPORT_WASM_API(wasm_timer_cancel),
|
||||
EXPORT_WASM_API(wasm_timer_restart),
|
||||
EXPORT_WASM_API(wasm_get_sys_tick_ms),
|
||||
#endif
|
||||
};
|
||||
|
||||
int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
|
||||
{
|
||||
*p_base_lib_apis = extended_native_symbol_defs;
|
||||
return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol);
|
||||
}
|
||||
|
||||
24
core/iwasm/lib/native/base/base-lib-export.h
Normal file
24
core/iwasm/lib/native/base/base-lib-export.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 _BASE_LIB_EXPORT_H_
|
||||
#define _BASE_LIB_EXPORT_H_
|
||||
|
||||
#include "attr-container.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
#endif /* end of _BASE_LIB_EXPORT_H_ */
|
||||
|
||||
111
core/iwasm/lib/native/base/request_response.c
Normal file
111
core/iwasm/lib/native/base/request_response.c
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "native_interface.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "coap_ext.h"
|
||||
#include "wasm-export.h"
|
||||
|
||||
extern void module_request_handler(request_t *request, uint32 register_id);
|
||||
|
||||
bool wasm_response_send(int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return false;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
response_t response[1];
|
||||
|
||||
if (NULL == unpack_response(buffer, size, response))
|
||||
return false;
|
||||
|
||||
am_send_response(response);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void wasm_register_resource(int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_addr(url_offset, 1))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
am_register_resource(url, module_request_handler, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
void wasm_post_request(int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
request_t req[1];
|
||||
|
||||
if (!unpack_request(buffer, size, req))
|
||||
return;
|
||||
|
||||
// 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);
|
||||
req->sender = mod_id;
|
||||
|
||||
if (req->action == COAP_EVENT) {
|
||||
am_publish_event(req);
|
||||
return;
|
||||
}
|
||||
|
||||
am_dispatch_request(req);
|
||||
}
|
||||
}
|
||||
|
||||
void wasm_sub_event(int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_addr(url_offset, 1))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
am_register_event(url, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
29
core/iwasm/lib/native/base/runtime_lib.h
Normal file
29
core/iwasm/lib/native/base/runtime_lib.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 LIB_BASE_RUNTIME_LIB_H_
|
||||
#define LIB_BASE_RUNTIME_LIB_H_
|
||||
|
||||
#include "native_interface.h"
|
||||
|
||||
#include "runtime_timer.h"
|
||||
|
||||
void init_wasm_timer();
|
||||
timer_ctx_t get_wasm_timer_ctx();
|
||||
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num);
|
||||
void destory_module_timer_ctx(unsigned int module_id);
|
||||
|
||||
#endif /* LIB_BASE_RUNTIME_LIB_H_ */
|
||||
178
core/iwasm/lib/native/base/timer_wrapper.c
Normal file
178
core/iwasm/lib/native/base/timer_wrapper.c
Normal file
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "runtime_timer.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "bh_list.h"
|
||||
#include "bh_thread.h"
|
||||
#include "bh_time.h"
|
||||
|
||||
bh_list g_timer_ctx_list;
|
||||
korp_cond g_timer_ctx_list_cond;
|
||||
korp_mutex g_timer_ctx_list_mutex;
|
||||
typedef struct {
|
||||
bh_list_link l;
|
||||
timer_ctx_t timer_ctx;
|
||||
} timer_ctx_node_t;
|
||||
|
||||
void wasm_timer_callback(timer_id_t id, unsigned int mod_id)
|
||||
{
|
||||
module_data* module = module_data_list_lookup_id(mod_id);
|
||||
if (module == NULL)
|
||||
return;
|
||||
|
||||
// !!! the length parameter must be 0, so the receiver will
|
||||
// not free the payload pointer.
|
||||
bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *) id, 0);
|
||||
}
|
||||
|
||||
///
|
||||
/// why we create a separate link for module timer contexts
|
||||
/// rather than traverse the module list?
|
||||
/// It helps to reduce the lock frequency for the module list.
|
||||
/// Also when we lock the module list and then call the callback for
|
||||
/// timer expire, the callback is request the list lock again for lookup
|
||||
/// the module from module id. It is for avoiding that situation.
|
||||
|
||||
void * thread_modulers_timer_check(void * arg)
|
||||
{
|
||||
|
||||
int ms_to_expiry;
|
||||
while (1) {
|
||||
ms_to_expiry = -1;
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
|
||||
&g_timer_ctx_list);
|
||||
while (elem) {
|
||||
int next = check_app_timers(elem->timer_ctx);
|
||||
if (next != -1) {
|
||||
if (ms_to_expiry == -1 || ms_to_expiry > next)
|
||||
ms_to_expiry = next;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
if (ms_to_expiry == -1)
|
||||
ms_to_expiry = 60 * 1000;
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
|
||||
ms_to_expiry);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
void wakeup_modules_timer_thread(timer_ctx_t ctx)
|
||||
{
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
vm_cond_signal(&g_timer_ctx_list_cond);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
void init_wasm_timer()
|
||||
{
|
||||
korp_tid tm_tid;
|
||||
bh_list_init(&g_timer_ctx_list);
|
||||
|
||||
vm_cond_init(&g_timer_ctx_list_cond);
|
||||
/* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
|
||||
vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
|
||||
|
||||
vm_thread_create(&tm_tid, thread_modulers_timer_check,
|
||||
NULL,
|
||||
BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
}
|
||||
|
||||
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
|
||||
{
|
||||
timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
|
||||
wakeup_modules_timer_thread, prealloc_num, module_id);
|
||||
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
timer_ctx_node_t * node = (timer_ctx_node_t*) bh_malloc(
|
||||
sizeof(timer_ctx_node_t));
|
||||
if (node == NULL) {
|
||||
destroy_timer_ctx(ctx);
|
||||
return NULL;
|
||||
}
|
||||
memset(node, 0, sizeof(*node));
|
||||
node->timer_ctx = ctx;
|
||||
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
bh_list_insert(&g_timer_ctx_list, node);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void destory_module_timer_ctx(unsigned int module_id)
|
||||
{
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
|
||||
&g_timer_ctx_list);
|
||||
while (elem) {
|
||||
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
|
||||
bh_list_remove(&g_timer_ctx_list, elem);
|
||||
destroy_timer_ctx(elem->timer_ctx);
|
||||
bh_free(elem);
|
||||
break;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
timer_ctx_t get_wasm_timer_ctx()
|
||||
{
|
||||
module_data * m = app_manager_get_module_data(Module_WASM_App);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
return m->timer_ctx;
|
||||
}
|
||||
|
||||
timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start)
|
||||
{
|
||||
return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
|
||||
auto_start);
|
||||
}
|
||||
|
||||
void wasm_timer_destory(timer_id_t timer_id)
|
||||
{
|
||||
sys_timer_destory(get_wasm_timer_ctx(), timer_id);
|
||||
}
|
||||
|
||||
void wasm_timer_cancel(timer_id_t timer_id)
|
||||
{
|
||||
sys_timer_cancel(get_wasm_timer_ctx(), timer_id);
|
||||
}
|
||||
|
||||
void wasm_timer_restart(timer_id_t timer_id, int interval)
|
||||
{
|
||||
sys_timer_restart(get_wasm_timer_ctx(), timer_id, interval);
|
||||
}
|
||||
|
||||
extern uint32 get_sys_tick_ms();
|
||||
|
||||
uint32 wasm_get_sys_tick_ms(void)
|
||||
{
|
||||
return (uint32) bh_get_tick_ms();
|
||||
}
|
||||
|
||||
23
core/iwasm/lib/native/base/wasm_lib_base.cmake
Normal file
23
core/iwasm/lib/native/base/wasm_lib_base.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
set (WASM_LIB_BASE_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIB_BASE_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIB_BASE_DIR}/*.c)
|
||||
|
||||
set (WASM_LIB_BASE_SOURCE ${source_all})
|
||||
|
||||
425
core/iwasm/lib/native/extension/sensor/runtime_sensor.c
Normal file
425
core/iwasm/lib/native/extension/sensor/runtime_sensor.c
Normal file
@ -0,0 +1,425 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "runtime_sensor.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "bh_thread.h"
|
||||
#include "bh_time.h"
|
||||
|
||||
static sys_sensor_t * g_sys_sensors = NULL;
|
||||
static int g_sensor_id_max = 0;
|
||||
static sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
|
||||
unsigned int client_id, bool remove_if_found);
|
||||
|
||||
void (*rechedule_sensor_callback)() = NULL;
|
||||
|
||||
/*
|
||||
* API for the applications to call - don't call it from the runtime
|
||||
*
|
||||
*/
|
||||
|
||||
static void sensor_event_cleaner(sensor_event_data_t *sensor_event)
|
||||
{
|
||||
if (sensor_event->data != NULL) {
|
||||
if (sensor_event->data_fmt == FMT_ATTR_CONTAINER)
|
||||
attr_container_destroy(sensor_event->data);
|
||||
else
|
||||
bh_free(sensor_event->data);
|
||||
}
|
||||
|
||||
bh_free(sensor_event);
|
||||
}
|
||||
|
||||
static void wasm_sensor_callback(void *client, uint32 sensor_id,
|
||||
void *user_data)
|
||||
{
|
||||
attr_container_t *sensor_data = (attr_container_t *) user_data;
|
||||
attr_container_t *sensor_data_clone;
|
||||
int sensor_data_len;
|
||||
sensor_event_data_t *sensor_event;
|
||||
bh_message_t msg;
|
||||
sensor_client_t *c = (sensor_client_t *) client;
|
||||
|
||||
module_data *module = module_data_list_lookup_id(c->client_id);
|
||||
if (module == NULL)
|
||||
return;
|
||||
|
||||
if (sensor_data == NULL)
|
||||
return;
|
||||
|
||||
sensor_data_len = attr_container_get_serialize_length(sensor_data);
|
||||
sensor_data_clone = (attr_container_t *)bh_malloc(sensor_data_len);
|
||||
if (sensor_data_clone == NULL)
|
||||
return;
|
||||
|
||||
/* multiple sensor clients may use/free the sensor data, so make a copy */
|
||||
memcpy(sensor_data_clone, sensor_data, sensor_data_len);
|
||||
|
||||
sensor_event = (sensor_event_data_t *)bh_malloc(sizeof(*sensor_event));
|
||||
if (sensor_event == NULL) {
|
||||
bh_free(sensor_data_clone);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(sensor_event, 0, sizeof(*sensor_event));
|
||||
sensor_event->sensor_id = sensor_id;
|
||||
sensor_event->data = sensor_data_clone;
|
||||
sensor_event->data_fmt = FMT_ATTR_CONTAINER;
|
||||
|
||||
msg = bh_new_msg(SENSOR_EVENT_WASM,
|
||||
sensor_event,
|
||||
sizeof(*sensor_event),
|
||||
sensor_event_cleaner);
|
||||
if (!msg) {
|
||||
sensor_event_cleaner(sensor_event);
|
||||
return;
|
||||
}
|
||||
|
||||
bh_post_msg2(module->queue, msg);
|
||||
}
|
||||
|
||||
bool wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay)
|
||||
{
|
||||
attr_container_t * attr_cont;
|
||||
sensor_client_t * c;
|
||||
sensor_obj_t s = find_sys_sensor_id(sensor);
|
||||
if (s == NULL)
|
||||
return false;
|
||||
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
vm_mutex_lock(&s->lock);
|
||||
|
||||
c = find_sensor_client(s, mod_id, false);
|
||||
if (c == NULL) {
|
||||
vm_mutex_unlock(&s->lock);
|
||||
return false;
|
||||
}
|
||||
|
||||
c->interval = interval;
|
||||
c->bit_cfg = bit_cfg;
|
||||
c->delay = delay;
|
||||
|
||||
vm_mutex_unlock(&s->lock);
|
||||
|
||||
if (s->config != NULL) {
|
||||
attr_cont = attr_container_create("config sensor");
|
||||
attr_container_set_int(&attr_cont, "interval", interval);
|
||||
attr_container_set_int(&attr_cont, "bit_cfg", bit_cfg);
|
||||
attr_container_set_int(&attr_cont, "delay", delay);
|
||||
s->config(s, attr_cont);
|
||||
attr_container_destroy(attr_cont);
|
||||
}
|
||||
|
||||
refresh_read_interval(s);
|
||||
|
||||
reschedule_sensor_read();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32 wasm_sensor_open(int32 name_offset, int instance)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *name = NULL;
|
||||
|
||||
if (!validate_app_addr(name_offset, 1))
|
||||
return -1;
|
||||
|
||||
name = addr_app_to_native(name_offset);
|
||||
|
||||
if (name != NULL) {
|
||||
sensor_client_t *c;
|
||||
sys_sensor_t *s = find_sys_sensor(name, instance);
|
||||
if (s == NULL)
|
||||
return -1;
|
||||
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
vm_mutex_lock(&s->lock);
|
||||
|
||||
c = find_sensor_client(s, mod_id, false);
|
||||
if (c) {
|
||||
// the app already opened this sensor
|
||||
vm_mutex_unlock(&s->lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sensor_client_t * client = (sensor_client_t*) bh_malloc(
|
||||
sizeof(sensor_client_t));
|
||||
if (client == NULL) {
|
||||
vm_mutex_unlock(&s->lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(client, 0, sizeof(sensor_client_t));
|
||||
client->client_id = mod_id;
|
||||
client->client_callback = wasm_sensor_callback;
|
||||
client->interval = s->default_interval;
|
||||
client->next = s->clients;
|
||||
s->clients = client;
|
||||
|
||||
vm_mutex_unlock(&s->lock);
|
||||
|
||||
refresh_read_interval(s);
|
||||
|
||||
reschedule_sensor_read();
|
||||
|
||||
return s->sensor_id;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
|
||||
int len)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, len))
|
||||
return false;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
attr_container_t * cfg;
|
||||
|
||||
sensor_obj_t s = find_sys_sensor_id(sensor);
|
||||
if (s == NULL)
|
||||
return false;
|
||||
|
||||
if (s->config == NULL)
|
||||
return false;
|
||||
|
||||
return s->config(s, cfg);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wasm_sensor_close(uint32 sensor)
|
||||
{
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
unsigned int client_id = mod_id;
|
||||
sensor_obj_t s = find_sys_sensor_id(sensor);
|
||||
sensor_client_t *c;
|
||||
|
||||
if (s == NULL)
|
||||
return false;
|
||||
|
||||
vm_mutex_lock(&s->lock);
|
||||
if ((c = find_sensor_client(s, client_id, true)) != NULL)
|
||||
bh_free(c);
|
||||
vm_mutex_unlock(&s->lock);
|
||||
|
||||
refresh_read_interval(s);
|
||||
|
||||
reschedule_sensor_read();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* sensor framework API - don't expose to the applications
|
||||
*
|
||||
*/
|
||||
void set_sensor_reshceduler(void (*callback)())
|
||||
{
|
||||
rechedule_sensor_callback = callback;
|
||||
}
|
||||
|
||||
// used for other threads to wakeup the sensor read thread
|
||||
void reschedule_sensor_read()
|
||||
{
|
||||
if (rechedule_sensor_callback)
|
||||
rechedule_sensor_callback();
|
||||
}
|
||||
|
||||
void refresh_read_interval(sensor_obj_t sensor)
|
||||
{
|
||||
sensor_client_t *c;
|
||||
uint32 interval = sensor->default_interval;
|
||||
vm_mutex_lock(&sensor->lock);
|
||||
|
||||
c = sensor->clients;
|
||||
if (c)
|
||||
interval = c->interval;
|
||||
|
||||
while (c) {
|
||||
if (c->interval < interval)
|
||||
interval = c->interval;
|
||||
c = c->next;
|
||||
}
|
||||
|
||||
vm_mutex_unlock(&sensor->lock);
|
||||
|
||||
sensor->read_interval = interval;
|
||||
}
|
||||
|
||||
sensor_obj_t add_sys_sensor(char * name, char * description, int instance,
|
||||
uint32 default_interval, void * read_func, void * config_func)
|
||||
{
|
||||
sys_sensor_t * s = (sys_sensor_t *) bh_malloc(sizeof(sys_sensor_t));
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->name = bh_strdup(name);
|
||||
s->sensor_instance = instance;
|
||||
s->default_interval = default_interval;
|
||||
|
||||
if (!s->name) {
|
||||
bh_free(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (description) {
|
||||
s->description = bh_strdup(description);
|
||||
if (!s->description) {
|
||||
bh_free(s->name);
|
||||
bh_free(s);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
g_sensor_id_max++;
|
||||
if (g_sensor_id_max == -1)
|
||||
g_sensor_id_max++;
|
||||
s->sensor_id = g_sensor_id_max;
|
||||
|
||||
s->read = read_func;
|
||||
s->config = config_func;
|
||||
|
||||
if (g_sys_sensors == NULL) {
|
||||
g_sys_sensors = s;
|
||||
} else {
|
||||
s->next = g_sys_sensors;
|
||||
g_sys_sensors = s;
|
||||
}
|
||||
|
||||
vm_mutex_init(&s->lock);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
sensor_obj_t find_sys_sensor(const char* name, int instance)
|
||||
{
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
while (s) {
|
||||
if (strcmp(s->name, name) == 0 && s->sensor_instance == instance)
|
||||
return s;
|
||||
|
||||
s = s->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sensor_obj_t find_sys_sensor_id(uint32 sensor_id)
|
||||
{
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
while (s) {
|
||||
if (s->sensor_id == sensor_id)
|
||||
return s;
|
||||
|
||||
s = s->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
|
||||
unsigned int client_id, bool remove_if_found)
|
||||
{
|
||||
sensor_client_t *prev = NULL, *c = sensor->clients;
|
||||
|
||||
while (c) {
|
||||
sensor_client_t *next = c->next;
|
||||
if (c->client_id == client_id) {
|
||||
if (remove_if_found) {
|
||||
if (prev)
|
||||
prev->next = next;
|
||||
else
|
||||
sensor->clients = next;
|
||||
}
|
||||
return c;
|
||||
} else {
|
||||
c = c->next;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// return the milliseconds to next check
|
||||
int check_sensor_timers()
|
||||
{
|
||||
int ms_to_next_check = -1;
|
||||
uint32 now = (uint32) bh_get_tick_ms();
|
||||
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
while (s) {
|
||||
uint32 last_read = s->last_read;
|
||||
uint32 elpased_ms = bh_get_elpased_ms(&last_read);
|
||||
|
||||
if (s->read_interval <= 0 || s->clients == NULL) {
|
||||
s = s->next;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (elpased_ms >= s->read_interval) {
|
||||
attr_container_t * data = s->read(s);
|
||||
if (data) {
|
||||
sensor_client_t * client = s->clients;
|
||||
while (client) {
|
||||
client->client_callback(client, s->sensor_id, data);
|
||||
client = client->next;
|
||||
}
|
||||
attr_container_destroy(data);
|
||||
}
|
||||
|
||||
s->last_read = now;
|
||||
|
||||
if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval))
|
||||
ms_to_next_check = s->read_interval;
|
||||
} else {
|
||||
int remaining = s->read_interval - elpased_ms;
|
||||
if (ms_to_next_check == -1 || (ms_to_next_check < remaining))
|
||||
ms_to_next_check = remaining;
|
||||
|
||||
}
|
||||
|
||||
s = s->next;
|
||||
}
|
||||
|
||||
return ms_to_next_check;
|
||||
}
|
||||
|
||||
void sensor_cleanup_callback(uint32 module_id)
|
||||
{
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
|
||||
while (s) {
|
||||
sensor_client_t *c;
|
||||
vm_mutex_lock(&s->lock);
|
||||
if ((c = find_sensor_client(s, module_id, true)) != NULL) {
|
||||
bh_free(c);
|
||||
}
|
||||
vm_mutex_unlock(&s->lock);
|
||||
s = s->next;
|
||||
}
|
||||
}
|
||||
75
core/iwasm/lib/native/extension/sensor/runtime_sensor.h
Normal file
75
core/iwasm/lib/native/extension/sensor/runtime_sensor.h
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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 LIB_EXTENSION_RUNTIME_SENSOR_H_
|
||||
#define LIB_EXTENSION_RUNTIME_SENSOR_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "attr-container.h"
|
||||
struct _sys_sensor;
|
||||
typedef struct _sys_sensor* sensor_obj_t;
|
||||
|
||||
typedef struct _sensor_client {
|
||||
struct _sensor_client * next;
|
||||
unsigned int client_id; // the app id
|
||||
int interval;
|
||||
int bit_cfg;
|
||||
int delay;
|
||||
void (*client_callback)(void * client, uint32, attr_container_t *);
|
||||
} sensor_client_t;
|
||||
|
||||
typedef struct _sys_sensor {
|
||||
struct _sys_sensor * next;
|
||||
char * name;
|
||||
int sensor_instance;
|
||||
char * description;
|
||||
uint32 sensor_id;
|
||||
sensor_client_t * clients;
|
||||
/* app, sensor mgr and app mgr may access the clients at the same time,
|
||||
* so need a lock to protect the clients */
|
||||
korp_mutex lock;
|
||||
uint32 last_read;
|
||||
uint32 read_interval;
|
||||
uint32 default_interval;
|
||||
|
||||
attr_container_t * (*read)(void *); /* TODO: may support other type return value, such as 'cbor' */
|
||||
bool (*config)(void *, void *);
|
||||
|
||||
} sys_sensor_t;
|
||||
|
||||
sensor_obj_t add_sys_sensor(char * name, char * description, int instance,
|
||||
uint32 default_interval, void * read_func, void * config_func);
|
||||
sensor_obj_t find_sys_sensor(const char* name, int instance);
|
||||
sensor_obj_t find_sys_sensor_id(uint32 sensor_id);
|
||||
void refresh_read_interval(sensor_obj_t sensor);
|
||||
void sensor_cleanup_callback(uint32 module_id);
|
||||
int check_sensor_timers();
|
||||
void reschedule_sensor_read();
|
||||
|
||||
uint32
|
||||
wasm_sensor_open(int32 name_offset, int instance);
|
||||
|
||||
bool
|
||||
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
|
||||
|
||||
bool
|
||||
wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
|
||||
int len);
|
||||
|
||||
bool
|
||||
wasm_sensor_close(uint32 sensor);
|
||||
|
||||
#endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */
|
||||
20
core/iwasm/lib/native/extension/sensor/runtime_sensor.inl
Normal file
20
core/iwasm/lib/native/extension/sensor/runtime_sensor.inl
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
EXPORT_WASM_API(wasm_sensor_open),
|
||||
EXPORT_WASM_API(wasm_sensor_config),
|
||||
EXPORT_WASM_API(wasm_sensor_config_with_attr_container),
|
||||
EXPORT_WASM_API(wasm_sensor_close),
|
||||
145
core/iwasm/lib/native/extension/sensor/sensor_mgr_ref.c
Normal file
145
core/iwasm/lib/native/extension/sensor/sensor_mgr_ref.c
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "bh_common.h"
|
||||
#include "bh_queue.h"
|
||||
#include "bh_thread.h"
|
||||
#include "runtime_sensor.h"
|
||||
#include "attr-container.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "wasm-export.h"
|
||||
|
||||
/*
|
||||
*
|
||||
* One reference implementation for sensor manager
|
||||
*
|
||||
*
|
||||
*/
|
||||
static korp_cond cond;
|
||||
static korp_mutex mutex;
|
||||
|
||||
void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
|
||||
{
|
||||
uint32 argv[3];
|
||||
wasm_function_inst_t func_onSensorEvent;
|
||||
|
||||
bh_assert(SENSOR_EVENT_WASM == bh_message_type(msg));
|
||||
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
|
||||
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
|
||||
|
||||
sensor_event_data_t *payload = (sensor_event_data_t*) bh_message_payload(
|
||||
msg);
|
||||
if (payload == NULL)
|
||||
return;
|
||||
|
||||
func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event",
|
||||
"(i32i32i32)");
|
||||
if (!func_onSensorEvent) {
|
||||
printf("Cannot find function onRequest\n");
|
||||
} else {
|
||||
int32 sensor_data_offset;
|
||||
uint32 sensor_data_len;
|
||||
|
||||
if (payload->data_fmt == FMT_ATTR_CONTAINER) {
|
||||
sensor_data_len = attr_container_get_serialize_length(
|
||||
payload->data);
|
||||
} else {
|
||||
printf("Unsupported sensor data format: %d\n", payload->data_fmt);
|
||||
return;
|
||||
}
|
||||
|
||||
sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data,
|
||||
sensor_data_len);
|
||||
if (sensor_data_offset == 0) {
|
||||
printf("Got exception running wasm code: %s\n",
|
||||
wasm_runtime_get_exception(inst));
|
||||
wasm_runtime_clear_exception(inst);
|
||||
return;
|
||||
}
|
||||
|
||||
argv[0] = payload->sensor_id;
|
||||
argv[1] = (uint32) sensor_data_offset;
|
||||
argv[2] = sensor_data_len;
|
||||
|
||||
if (!wasm_runtime_call_wasm(inst, NULL, func_onSensorEvent, 3, argv)) {
|
||||
printf(":Got exception running wasm code: %s\n",
|
||||
wasm_runtime_get_exception(inst));
|
||||
wasm_runtime_clear_exception(inst);
|
||||
wasm_runtime_module_free(inst, sensor_data_offset);
|
||||
return;
|
||||
}
|
||||
|
||||
wasm_runtime_module_free(inst, sensor_data_offset);
|
||||
}
|
||||
}
|
||||
|
||||
static attr_container_t * read_test_sensor(void * sensor)
|
||||
{
|
||||
//luc: for test
|
||||
attr_container_t *attr_obj = attr_container_create("read test sensor data");
|
||||
if (attr_obj) {
|
||||
attr_container_set_string(&attr_obj, "name", "read test sensor");
|
||||
return attr_obj;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool config_test_sensor(void * s, void * config)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void * thread_sensor_check(void * arg)
|
||||
{
|
||||
while (1) {
|
||||
int ms_to_expiry = check_sensor_timers();
|
||||
if (ms_to_expiry == -1)
|
||||
ms_to_expiry = 5000;
|
||||
vm_mutex_lock(&mutex);
|
||||
vm_cond_reltimedwait(&cond, &mutex, ms_to_expiry);
|
||||
vm_mutex_unlock(&mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static void cb_wakeup_thread()
|
||||
{
|
||||
vm_cond_signal(&cond);
|
||||
}
|
||||
|
||||
void init_sensor_framework()
|
||||
{
|
||||
// init the mutext and conditions
|
||||
korp_thread tid;
|
||||
vm_cond_init(&cond);
|
||||
vm_mutex_init(&mutex);
|
||||
|
||||
// add the sys sensor objects
|
||||
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
|
||||
read_test_sensor, config_test_sensor);
|
||||
|
||||
set_sensor_reshceduler(cb_wakeup_thread);
|
||||
|
||||
wasm_register_msg_callback(SENSOR_EVENT_WASM,
|
||||
app_mgr_sensor_event_callback);
|
||||
|
||||
wasm_register_cleanup_callback(sensor_cleanup_callback);
|
||||
|
||||
vm_thread_create(&tid, thread_sensor_check, NULL,
|
||||
BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
|
||||
}
|
||||
|
||||
23
core/iwasm/lib/native/extension/sensor/wasm_lib_sensor.cmake
Normal file
23
core/iwasm/lib/native/extension/sensor/wasm_lib_sensor.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
set (WASM_LIB_SENSOR_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIB_SENSOR_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIB_SENSOR_DIR}/*.c)
|
||||
|
||||
set (WASM_LIB_SENSOR_SOURCE ${source_all})
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "lib-export.h"
|
||||
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to add functions to register. */
|
||||
|
||||
NativeSymbol extended_native_symbol_defs[] = {
|
||||
|
||||
/*EXPORT_WASM_API(publish_event)*/
|
||||
|
||||
};
|
||||
893
core/iwasm/lib/native/libc/libc_wrapper.c
Normal file
893
core/iwasm/lib/native/libc/libc_wrapper.c
Normal file
@ -0,0 +1,893 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "wasm-native.h"
|
||||
#include "wasm-export.h"
|
||||
#include "wasm_log.h"
|
||||
#include "wasm_platform_log.h"
|
||||
|
||||
void
|
||||
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
|
||||
uint32
|
||||
wasm_runtime_get_temp_ret(wasm_module_inst_t module);
|
||||
void
|
||||
wasm_runtime_set_temp_ret(wasm_module_inst_t module, uint32 temp_ret);
|
||||
uint32
|
||||
wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
|
||||
void
|
||||
wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
|
||||
|
||||
#define get_module_inst() \
|
||||
wasm_runtime_get_current_module_inst()
|
||||
|
||||
#define validate_app_addr(offset, size) \
|
||||
wasm_runtime_validate_app_addr(module_inst, offset, size)
|
||||
|
||||
#define addr_app_to_native(offset) \
|
||||
wasm_runtime_addr_app_to_native(module_inst, offset)
|
||||
|
||||
#define addr_native_to_app(ptr) \
|
||||
wasm_runtime_addr_native_to_app(module_inst, ptr)
|
||||
|
||||
#define module_malloc(size) \
|
||||
wasm_runtime_module_malloc(module_inst, size)
|
||||
|
||||
#define module_free(offset) \
|
||||
wasm_runtime_module_free(module_inst, offset)
|
||||
|
||||
typedef int (*out_func_t)(int c, void *ctx);
|
||||
|
||||
enum pad_type {
|
||||
PAD_NONE, PAD_ZERO_BEFORE, PAD_SPACE_BEFORE, PAD_SPACE_AFTER,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Output an unsigned long in hex format
|
||||
*
|
||||
* Output an unsigned long on output installed by platform at init time. Should
|
||||
* be able to handle an unsigned long of any size, 32 or 64 bit.
|
||||
* @param num Number to output
|
||||
*
|
||||
* @return N/A
|
||||
*/
|
||||
static void _printf_hex_ulong(out_func_t out, void *ctx,
|
||||
const unsigned long num, enum pad_type padding, int min_width)
|
||||
{
|
||||
int size = sizeof(num) * 2;
|
||||
int found_largest_digit = 0;
|
||||
int remaining = 8; /* 8 digits max */
|
||||
int digits = 0;
|
||||
|
||||
for (; size; size--) {
|
||||
char nibble = (num >> ((size - 1) << 2) & 0xf);
|
||||
|
||||
if (nibble || found_largest_digit || size == 1) {
|
||||
found_largest_digit = 1;
|
||||
nibble += nibble > 9 ? 87 : 48;
|
||||
out((int) nibble, ctx);
|
||||
digits++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (remaining-- <= min_width) {
|
||||
if (padding == PAD_ZERO_BEFORE) {
|
||||
out('0', ctx);
|
||||
} else if (padding == PAD_SPACE_BEFORE) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (padding == PAD_SPACE_AFTER) {
|
||||
remaining = min_width * 2 - digits;
|
||||
while (remaining-- > 0) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Output an unsigned long (32-bit) in decimal format
|
||||
*
|
||||
* Output an unsigned long on output installed by platform at init time. Only
|
||||
* works with 32-bit values.
|
||||
* @param num Number to output
|
||||
*
|
||||
* @return N/A
|
||||
*/
|
||||
static void _printf_dec_ulong(out_func_t out, void *ctx,
|
||||
const unsigned long num, enum pad_type padding, int min_width)
|
||||
{
|
||||
unsigned long pos = 999999999;
|
||||
unsigned long remainder = num;
|
||||
int found_largest_digit = 0;
|
||||
int remaining = 10; /* 10 digits max */
|
||||
int digits = 1;
|
||||
|
||||
/* make sure we don't skip if value is zero */
|
||||
if (min_width <= 0) {
|
||||
min_width = 1;
|
||||
}
|
||||
|
||||
while (pos >= 9) {
|
||||
if (found_largest_digit || remainder > pos) {
|
||||
found_largest_digit = 1;
|
||||
out((int) ((remainder / (pos + 1)) + 48), ctx);
|
||||
digits++;
|
||||
} else if (remaining <= min_width && padding < PAD_SPACE_AFTER) {
|
||||
out((int) (padding == PAD_ZERO_BEFORE ? '0' : ' '), ctx);
|
||||
digits++;
|
||||
}
|
||||
remaining--;
|
||||
remainder %= (pos + 1);
|
||||
pos /= 10;
|
||||
}
|
||||
out((int) (remainder + 48), ctx);
|
||||
|
||||
if (padding == PAD_SPACE_AFTER) {
|
||||
remaining = min_width - digits;
|
||||
while (remaining-- > 0) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void _vprintf(out_func_t out, void *ctx, const char *fmt, va_list ap,
|
||||
wasm_module_inst_t module_inst)
|
||||
{
|
||||
int might_format = 0; /* 1 if encountered a '%' */
|
||||
enum pad_type padding = PAD_NONE;
|
||||
int min_width = -1;
|
||||
int long_ctr = 0;
|
||||
|
||||
/* fmt has already been adjusted if needed */
|
||||
|
||||
while (*fmt) {
|
||||
if (!might_format) {
|
||||
if (*fmt != '%') {
|
||||
out((int) *fmt, ctx);
|
||||
} else {
|
||||
might_format = 1;
|
||||
min_width = -1;
|
||||
padding = PAD_NONE;
|
||||
long_ctr = 0;
|
||||
}
|
||||
} else {
|
||||
switch (*fmt) {
|
||||
case '-':
|
||||
padding = PAD_SPACE_AFTER;
|
||||
goto still_might_format;
|
||||
|
||||
case '0':
|
||||
if (min_width < 0 && padding == PAD_NONE) {
|
||||
padding = PAD_ZERO_BEFORE;
|
||||
goto still_might_format;
|
||||
}
|
||||
/* Fall through */
|
||||
case '1' ... '9':
|
||||
if (min_width < 0) {
|
||||
min_width = *fmt - '0';
|
||||
} else {
|
||||
min_width = 10 * min_width + *fmt - '0';
|
||||
}
|
||||
|
||||
if (padding == PAD_NONE) {
|
||||
padding = PAD_SPACE_BEFORE;
|
||||
}
|
||||
goto still_might_format;
|
||||
|
||||
case 'l':
|
||||
long_ctr++;
|
||||
/* Fall through */
|
||||
case 'z':
|
||||
case 'h':
|
||||
/* FIXME: do nothing for these modifiers */
|
||||
goto still_might_format;
|
||||
|
||||
case 'd':
|
||||
case 'i': {
|
||||
long d;
|
||||
if (long_ctr < 2) {
|
||||
d = va_arg(ap, long);
|
||||
} else {
|
||||
d = (long)va_arg(ap, long long);
|
||||
}
|
||||
|
||||
if (d < 0) {
|
||||
out((int) '-', ctx);
|
||||
d = -d;
|
||||
min_width--;
|
||||
}
|
||||
_printf_dec_ulong(out, ctx, d, padding, min_width);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'u': {
|
||||
unsigned long u;
|
||||
|
||||
if (long_ctr < 2) {
|
||||
u = va_arg(ap, unsigned long);
|
||||
} else {
|
||||
u = (unsigned long)va_arg(ap,
|
||||
unsigned long long);
|
||||
}
|
||||
_printf_dec_ulong(out, ctx, u, padding, min_width);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'p':
|
||||
out('0', ctx);
|
||||
out('x', ctx);
|
||||
/* left-pad pointers with zeros */
|
||||
padding = PAD_ZERO_BEFORE;
|
||||
min_width = 8;
|
||||
/* Fall through */
|
||||
case 'x':
|
||||
case 'X': {
|
||||
unsigned long x;
|
||||
|
||||
if (long_ctr < 2) {
|
||||
x = va_arg(ap, unsigned long);
|
||||
} else {
|
||||
x = (unsigned long)va_arg(ap, unsigned long long);
|
||||
}
|
||||
|
||||
_printf_hex_ulong(out, ctx, x, padding, min_width);
|
||||
break;
|
||||
}
|
||||
|
||||
case 's': {
|
||||
char *s;
|
||||
char *start;
|
||||
int32 s_offset = va_arg(ap, uint32);
|
||||
|
||||
if (!validate_app_addr(s_offset, 1)) {
|
||||
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
|
||||
return;
|
||||
}
|
||||
|
||||
s = start = addr_app_to_native(s_offset);
|
||||
|
||||
while (*s)
|
||||
out((int) (*s++), ctx);
|
||||
|
||||
if (padding == PAD_SPACE_AFTER) {
|
||||
int remaining = min_width - (s - start);
|
||||
while (remaining-- > 0) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'c': {
|
||||
int c = va_arg(ap, int);
|
||||
out(c, ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
case '%': {
|
||||
out((int) '%', ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
out((int) '%', ctx);
|
||||
out((int) *fmt, ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
might_format = 0;
|
||||
}
|
||||
|
||||
still_might_format: ++fmt;
|
||||
}
|
||||
}
|
||||
|
||||
struct str_context {
|
||||
char *str;
|
||||
int max;
|
||||
int count;
|
||||
};
|
||||
|
||||
static int sprintf_out(int c, struct str_context *ctx)
|
||||
{
|
||||
if (!ctx->str || ctx->count >= ctx->max) {
|
||||
ctx->count++;
|
||||
return c;
|
||||
}
|
||||
|
||||
if (ctx->count == ctx->max - 1) {
|
||||
ctx->str[ctx->count++] = '\0';
|
||||
} else {
|
||||
ctx->str[ctx->count++] = c;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
static int printf_out(int c, struct str_context *ctx)
|
||||
{
|
||||
printf("%c", c);
|
||||
ctx->count++;
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline va_list get_va_list(uint32 *args)
|
||||
{
|
||||
union {
|
||||
uint32 u;
|
||||
va_list v;
|
||||
} u;
|
||||
u.u = args[0];
|
||||
return u.v;
|
||||
}
|
||||
|
||||
static bool parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
|
||||
int32 va_list_offset, const char **p_fmt, va_list *p_va_args)
|
||||
{
|
||||
const char *fmt;
|
||||
union {
|
||||
uintptr_t u;
|
||||
va_list v;
|
||||
} u;
|
||||
|
||||
if (!validate_app_addr(fmt_offset,
|
||||
1) || !validate_app_addr(va_list_offset, sizeof(int32)))
|
||||
return false;
|
||||
|
||||
fmt = (const char*) addr_app_to_native(fmt_offset);
|
||||
u.u = (uintptr_t) addr_app_to_native(va_list_offset);
|
||||
|
||||
*p_fmt = fmt;
|
||||
*p_va_args = u.v;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int _printf_wrapper(int32 fmt_offset, int32 va_list_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
struct str_context ctx = { NULL, 0, 0 };
|
||||
const char *fmt;
|
||||
va_list va_args;
|
||||
|
||||
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
|
||||
return 0;
|
||||
|
||||
_vprintf((out_func_t) printf_out, &ctx, fmt, va_args, module_inst);
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
static int _sprintf_wrapper(int32 str_offset, int32 fmt_offset,
|
||||
int32 va_list_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
struct str_context ctx;
|
||||
char *str;
|
||||
const char *fmt;
|
||||
va_list va_args;
|
||||
|
||||
if (!validate_app_addr(str_offset, 1))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
|
||||
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
|
||||
return 0;
|
||||
|
||||
ctx.str = str;
|
||||
ctx.max = INT_MAX;
|
||||
ctx.count = 0;
|
||||
|
||||
_vprintf((out_func_t) sprintf_out, &ctx, fmt, va_args, module_inst);
|
||||
|
||||
if (ctx.count < ctx.max) {
|
||||
str[ctx.count] = '\0';
|
||||
}
|
||||
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
static int _snprintf_wrapper(int32 str_offset, int32 size, int32 fmt_offset,
|
||||
int32 va_list_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
struct str_context ctx;
|
||||
char *str;
|
||||
const char *fmt;
|
||||
va_list va_args;
|
||||
|
||||
if (!validate_app_addr(str_offset, size))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
|
||||
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
|
||||
return 0;
|
||||
|
||||
ctx.str = str;
|
||||
ctx.max = size;
|
||||
ctx.count = 0;
|
||||
|
||||
_vprintf((out_func_t) sprintf_out, &ctx, fmt, va_args, module_inst);
|
||||
|
||||
if (ctx.count < ctx.max) {
|
||||
str[ctx.count] = '\0';
|
||||
}
|
||||
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
static int _puts_wrapper(int32 str_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
const char *str;
|
||||
|
||||
if (!validate_app_addr(str_offset, 1))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
return printf("%s\n", str);
|
||||
}
|
||||
|
||||
static int _putchar_wrapper(int c)
|
||||
{
|
||||
printf("%c", c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int32 _strdup_wrapper(int32 str_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *str, *str_ret;
|
||||
uint32 len;
|
||||
int32 str_ret_offset = 0;
|
||||
|
||||
if (!validate_app_addr(str_offset, 1))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
|
||||
if (str) {
|
||||
len = strlen(str) + 1;
|
||||
|
||||
str_ret_offset = module_malloc(len);
|
||||
if (str_ret_offset) {
|
||||
str_ret = addr_app_to_native(str_ret_offset);
|
||||
memcpy(str_ret, str, len);
|
||||
}
|
||||
}
|
||||
|
||||
return str_ret_offset;
|
||||
}
|
||||
|
||||
static int32 _memcmp_wrapper(int32 s1_offset, int32 s2_offset, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s1, *s2;
|
||||
|
||||
if (!validate_app_addr(s1_offset, size) || !validate_app_addr(s2_offset, size))
|
||||
return 0;
|
||||
|
||||
s1 = addr_app_to_native(s1_offset);
|
||||
s2 = addr_app_to_native(s2_offset);
|
||||
return memcmp(s1, s2, size);
|
||||
}
|
||||
|
||||
static int32 _memcpy_wrapper(int32 dst_offset, int32 src_offset, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *dst, *src;
|
||||
|
||||
if (size == 0)
|
||||
return dst_offset;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return dst_offset;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
memcpy(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static int32 _memmove_wrapper(int32 dst_offset, int32 src_offset, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return dst_offset;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
memmove(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static int32 _memset_wrapper(int32 s_offset, int32 c, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s;
|
||||
|
||||
if (!validate_app_addr(s_offset, size))
|
||||
return s_offset;
|
||||
|
||||
s = addr_app_to_native(s_offset);
|
||||
memset(s, c, size);
|
||||
return s_offset;
|
||||
}
|
||||
|
||||
static int32 _strchr_wrapper(int32 s_offset, int32 c)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
const char *s;
|
||||
char *ret;
|
||||
|
||||
if (!validate_app_addr(s_offset, 1))
|
||||
return s_offset;
|
||||
|
||||
s = addr_app_to_native(s_offset);
|
||||
ret = strchr(s, c);
|
||||
return ret ? addr_native_to_app(ret) : 0;
|
||||
}
|
||||
|
||||
static int32 _strcmp_wrapper(int32 s1_offset, int32 s2_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s1, *s2;
|
||||
|
||||
if (!validate_app_addr(s1_offset, 1) || !validate_app_addr(s2_offset, 1))
|
||||
return 0;
|
||||
|
||||
s1 = addr_app_to_native(s1_offset);
|
||||
s2 = addr_app_to_native(s2_offset);
|
||||
return strcmp(s1, s2);
|
||||
}
|
||||
|
||||
static int32 _strncmp_wrapper(int32 s1_offset, int32 s2_offset, uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s1, *s2;
|
||||
|
||||
if (!validate_app_addr(s1_offset, size) || !validate_app_addr(s2_offset, size))
|
||||
return 0;
|
||||
|
||||
s1 = addr_app_to_native(s1_offset);
|
||||
s2 = addr_app_to_native(s2_offset);
|
||||
return strncmp(s1, s2, size);
|
||||
}
|
||||
|
||||
static int32 _strcpy_wrapper(int32 dst_offset, int32 src_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, 1) || !validate_app_addr(src_offset, 1))
|
||||
return 0;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
strcpy(dst, src);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static int32 _strncpy_wrapper(int32 dst_offset, int32 src_offset, uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return 0;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
strncpy(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static uint32 _strlen_wrapper(int32 s_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *s;
|
||||
|
||||
if (!validate_app_addr(s_offset, 1))
|
||||
return 0;
|
||||
|
||||
s = addr_app_to_native(s_offset);
|
||||
return strlen(s);
|
||||
}
|
||||
|
||||
static int32 _malloc_wrapper(uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
return module_malloc(size);
|
||||
}
|
||||
|
||||
static int32 _calloc_wrapper(uint32 nmemb, uint32 size)
|
||||
{
|
||||
uint64 total_size = (uint64) nmemb * (uint64) size;
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
uint32 ret_offset = 0;
|
||||
uint8 *ret_ptr;
|
||||
|
||||
if (total_size > UINT32_MAX)
|
||||
total_size = UINT32_MAX;
|
||||
|
||||
ret_offset = module_malloc((uint32 )total_size);
|
||||
if (ret_offset) {
|
||||
ret_ptr = addr_app_to_native(ret_offset);
|
||||
memset(ret_ptr, 0, (uint32) total_size);
|
||||
}
|
||||
|
||||
return ret_offset;
|
||||
}
|
||||
|
||||
static void _free_wrapper(int32 ptr_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
|
||||
if (!validate_app_addr(ptr_offset, 4))
|
||||
return;
|
||||
return module_free(ptr_offset);
|
||||
}
|
||||
|
||||
static void setTempRet0_wrapper(uint32 temp_ret)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
wasm_runtime_set_temp_ret(module_inst, temp_ret);
|
||||
}
|
||||
|
||||
static uint32 getTempRet0_wrapper()
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
return wasm_runtime_get_temp_ret(module_inst);
|
||||
}
|
||||
|
||||
static uint32 _llvm_bswap_i16_wrapper(uint32 data)
|
||||
{
|
||||
return (data & 0xFFFF0000) | ((data & 0xFF) << 8) | ((data & 0xFF00) >> 8);
|
||||
}
|
||||
|
||||
static uint32 _llvm_bswap_i32_wrapper(uint32 data)
|
||||
{
|
||||
return ((data & 0xFF) << 24) | ((data & 0xFF00) << 8) | ((data & 0xFF0000) >> 8)
|
||||
| ((data & 0xFF000000) >> 24);
|
||||
}
|
||||
|
||||
static uint32 _bitshift64Lshr_wrapper(uint32 uint64_part0, uint32 uint64_part1,
|
||||
uint32 bits)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
union {
|
||||
uint64 value;
|
||||
uint32 parts[2];
|
||||
} u;
|
||||
|
||||
u.parts[0] = uint64_part0;
|
||||
u.parts[1] = uint64_part1;
|
||||
|
||||
u.value >>= bits;
|
||||
/* return low 32bit and save high 32bit to temp ret */
|
||||
wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
|
||||
return (uint32) u.value;
|
||||
}
|
||||
|
||||
static uint32 _bitshift64Shl_wrapper(uint32 int64_part0, uint32 int64_part1,
|
||||
uint32 bits)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
union {
|
||||
int64 value;
|
||||
uint32 parts[2];
|
||||
} u;
|
||||
|
||||
u.parts[0] = int64_part0;
|
||||
u.parts[1] = int64_part1;
|
||||
|
||||
u.value <<= bits;
|
||||
/* return low 32bit and save high 32bit to temp ret */
|
||||
wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
|
||||
return (uint32) u.value;
|
||||
}
|
||||
|
||||
static void _llvm_stackrestore_wrapper(uint32 llvm_stack)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
printf("_llvm_stackrestore called!\n");
|
||||
wasm_runtime_set_llvm_stack(module_inst, llvm_stack);
|
||||
}
|
||||
|
||||
static uint32 _llvm_stacksave_wrapper()
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
printf("_llvm_stacksave called!\n");
|
||||
return wasm_runtime_get_llvm_stack(module_inst);
|
||||
}
|
||||
|
||||
static int32 _emscripten_memcpy_big_wrapper(int32 dst_offset, int32 src_offset,
|
||||
uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return dst_offset;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
|
||||
memcpy(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static void abort_wrapper(int32 code)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "env.abort(%i)", code);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
}
|
||||
|
||||
static void abortStackOverflow_wrapper(int32 code)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "env.abortStackOverflow(%i)", code);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
}
|
||||
|
||||
static void nullFunc_X_wrapper(int32 code)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "env.nullFunc_X(%i)", code);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
}
|
||||
|
||||
/* TODO: add function parameter/result types check */
|
||||
#define REG_NATIVE_FUNC(module_name, func_name) \
|
||||
{#module_name, #func_name, func_name##_wrapper}
|
||||
|
||||
typedef struct WASMNativeFuncDef {
|
||||
const char *module_name;
|
||||
const char *func_name;
|
||||
void *func_ptr;
|
||||
} WASMNativeFuncDef;
|
||||
|
||||
static WASMNativeFuncDef native_func_defs[] = {
|
||||
REG_NATIVE_FUNC(env, _printf),
|
||||
REG_NATIVE_FUNC(env, _sprintf),
|
||||
REG_NATIVE_FUNC(env, _snprintf),
|
||||
REG_NATIVE_FUNC(env, _puts),
|
||||
REG_NATIVE_FUNC(env, _putchar),
|
||||
REG_NATIVE_FUNC(env, _memcmp),
|
||||
REG_NATIVE_FUNC(env, _memcpy),
|
||||
REG_NATIVE_FUNC(env, _memmove),
|
||||
REG_NATIVE_FUNC(env, _memset),
|
||||
REG_NATIVE_FUNC(env, _strchr),
|
||||
REG_NATIVE_FUNC(env, _strcmp),
|
||||
REG_NATIVE_FUNC(env, _strcpy),
|
||||
REG_NATIVE_FUNC(env, _strlen),
|
||||
REG_NATIVE_FUNC(env, _strncmp),
|
||||
REG_NATIVE_FUNC(env, _strncpy),
|
||||
REG_NATIVE_FUNC(env, _malloc),
|
||||
REG_NATIVE_FUNC(env, _calloc),
|
||||
REG_NATIVE_FUNC(env, _strdup),
|
||||
REG_NATIVE_FUNC(env, _free),
|
||||
REG_NATIVE_FUNC(env, setTempRet0),
|
||||
REG_NATIVE_FUNC(env, getTempRet0),
|
||||
REG_NATIVE_FUNC(env, _llvm_bswap_i16),
|
||||
REG_NATIVE_FUNC(env, _llvm_bswap_i32),
|
||||
REG_NATIVE_FUNC(env, _bitshift64Lshr),
|
||||
REG_NATIVE_FUNC(env, _bitshift64Shl),
|
||||
REG_NATIVE_FUNC(env, _llvm_stackrestore),
|
||||
REG_NATIVE_FUNC(env, _llvm_stacksave),
|
||||
REG_NATIVE_FUNC(env, _emscripten_memcpy_big),
|
||||
REG_NATIVE_FUNC(env, abort),
|
||||
REG_NATIVE_FUNC(env, abortStackOverflow),
|
||||
REG_NATIVE_FUNC(env, nullFunc_X), };
|
||||
|
||||
void*
|
||||
wasm_native_func_lookup(const char *module_name, const char *func_name)
|
||||
{
|
||||
uint32 size = sizeof(native_func_defs) / sizeof(WASMNativeFuncDef);
|
||||
WASMNativeFuncDef *func_def = native_func_defs;
|
||||
WASMNativeFuncDef *func_def_end = func_def + size;
|
||||
void *ret;
|
||||
|
||||
if (!module_name || !func_name)
|
||||
return NULL;
|
||||
|
||||
while (func_def < func_def_end) {
|
||||
if (!strcmp(func_def->module_name, module_name)
|
||||
&& !strcmp(func_def->func_name, func_name))
|
||||
return (void*) (uintptr_t) func_def->func_ptr;
|
||||
func_def++;
|
||||
}
|
||||
|
||||
if ((ret = wasm_platform_native_func_lookup(module_name, func_name)))
|
||||
return ret;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*************************************
|
||||
* Global Variables *
|
||||
*************************************/
|
||||
|
||||
typedef struct WASMNativeGlobalDef {
|
||||
const char *module_name;
|
||||
const char *global_name;
|
||||
WASMValue global_data;
|
||||
} WASMNativeGlobalDef;
|
||||
|
||||
static WASMNativeGlobalDef native_global_defs[] = { { "env", "STACKTOP",
|
||||
.global_data.u32 = 0 }, { "env", "STACK_MAX", .global_data.u32 = 0 }, { "env",
|
||||
"ABORT", .global_data.u32 = 0 }, { "env", "memoryBase", .global_data.u32 = 0 },
|
||||
{ "env", "__memory_base", .global_data.u32 = 0 }, { "env", "tableBase",
|
||||
.global_data.u32 = 0 }, { "env", "__table_base", .global_data.u32 = 0 }, {
|
||||
"env", "DYNAMICTOP_PTR", .global_data.addr = 0 }, { "env", "tempDoublePtr",
|
||||
.global_data.addr = 0 }, { "global", "NaN", .global_data.u64 =
|
||||
0x7FF8000000000000LL }, { "global", "Infinity", .global_data.u64 =
|
||||
0x7FF0000000000000LL }, };
|
||||
|
||||
bool wasm_native_global_lookup(const char *module_name, const char *global_name,
|
||||
WASMGlobalImport *global)
|
||||
{
|
||||
uint32 size = sizeof(native_global_defs) / sizeof(WASMNativeGlobalDef);
|
||||
WASMNativeGlobalDef *global_def = native_global_defs;
|
||||
WASMNativeGlobalDef *global_def_end = global_def + size;
|
||||
|
||||
if (!module_name || !global_name || !global)
|
||||
return false;
|
||||
|
||||
/* Lookup constant globals which can be defined by table */
|
||||
while (global_def < global_def_end) {
|
||||
if (!strcmp(global_def->module_name, module_name)
|
||||
&& !strcmp(global_def->global_name, global_name)) {
|
||||
global->global_data_linked = global_def->global_data;
|
||||
return true;
|
||||
}
|
||||
global_def++;
|
||||
}
|
||||
|
||||
/* Lookup non-constant globals which cannot be defined by table */
|
||||
if (!strcmp(module_name, "env")) {
|
||||
if (!strcmp(global_name, "_stdin")) {
|
||||
global->global_data_linked.addr = (uintptr_t) stdin;
|
||||
global->is_addr = true;
|
||||
return true;
|
||||
} else if (!strcmp(global_name, "_stdout")) {
|
||||
global->global_data_linked.addr = (uintptr_t) stdout;
|
||||
global->is_addr = true;
|
||||
return true;
|
||||
} else if (!strcmp(global_name, "_stderr")) {
|
||||
global->global_data_linked.addr = (uintptr_t) stderr;
|
||||
global->is_addr = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wasm_native_init()
|
||||
{
|
||||
/* TODO: qsort the function defs and global defs. */
|
||||
return true;
|
||||
}
|
||||
|
||||
23
core/iwasm/lib/native/libc/wasm_libc.cmake
Normal file
23
core/iwasm/lib/native/libc/wasm_libc.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
set (WASM_LIBC_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIBC_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIBC_DIR}/*.c)
|
||||
|
||||
set (WASM_LIBC_SOURCE ${source_all})
|
||||
|
||||
Reference in New Issue
Block a user