Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)

* Implement memory profiler, optimize memory usage, modify code indent

* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default

* Add a new extension library: connection

* Fix bug of reading magic number and version in big endian platform

* Re-org platform APIs: move most platform APIs from iwasm to shared-lib

* Enhance wasm loader to fix some security issues

* Fix issue about illegal load of EXC_RETURN into PC on stm32 board

* Updates that let a restricted version of the interpreter run in SGX

* Enable native/app address validation and conversion for wasm app

* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused

* Refine binary size and fix several minor issues

Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c

* Add more strict security checks for libc wrapper API's

* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files

* Enhance security of libc strcpy/sprintf wrapper function

* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions

* Remove get_module_inst() and fix issue of call native

* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate

* Refine interpreter call native process, refine memory boudary check

* Fix issues of invokeNative function of arm/mips/general version

* Add a switch to build simple sample without gui support

* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code

* Re-org shared lib header files, remove unused info; fix compile issues of vxworks

* Add build target general

* Remove unused files

* Update license header

* test push

* Restore file

* Sync up with internal/feature

* Sync up with internal/feature

* Rename build_wamr_app to build_wasm_app

* Fix small issues of README

* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc

* Sync up with internal/feature

* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue

* Sync up with internal/feature

* Fix bug of config.h and rename wasi config.h to ssp_config.h

* Sync up with internal/feature

* Import wamr aot

* update document

* update document

* Update document, disable WASI in 32bit

* update document

* remove files

* update document

* Update document

* update document

* update document

* update samples

* Sync up with internal repo
This commit is contained in:
wenyongh
2020-01-21 13:26:14 +08:00
committed by Wang Xin
parent 2a4528c749
commit 46b93b9d22
464 changed files with 25137 additions and 7911 deletions

View File

@ -0,0 +1,437 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "runtime_sensor.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "bh_thread.h"
#include "bh_time.h"
#include "bh_common.h"
#include "bh_assert.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 */
bh_memcpy_s(sensor_data_clone, sensor_data_len,
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(wasm_exec_env_t exec_env,
uint32 sensor, int interval,
int bit_cfg, int delay)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
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,
module_inst);
bh_assert(mod_id != ID_NONE);
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(wasm_exec_env_t exec_env,
int32 name_offset, int instance)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
char *name = NULL;
if (!validate_app_str_addr(name_offset))
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,
module_inst);
bh_assert(mod_id != ID_NONE);
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 = (void *)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(wasm_exec_env_t exec_env,
uint32 sensor, int32 buffer_offset,
int len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
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 = (attr_container_t *)buffer;
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(wasm_exec_env_t exec_env, uint32 sensor)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
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;
bh_assert(mod_id != ID_NONE);
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;
}
}

View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef LIB_EXTENSION_RUNTIME_SENSOR_H_
#define LIB_EXTENSION_RUNTIME_SENSOR_H_
#include "bh_platform.h"
#include "bi-inc/attr_container.h"
#include "wasm_export.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(wasm_exec_env_t exec_env,
int32 name_offset, int instance);
bool
wasm_sensor_config(wasm_exec_env_t exec_env,
uint32 sensor, int interval, int bit_cfg, int delay);
bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env,
uint32 sensor, int32 buffer_offset,
int len);
bool
wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor);
#endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */

View File

@ -0,0 +1,9 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
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),

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _SENSOR_API_H_
#define _SENSOR_API_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
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);
#ifdef __cplusplus
}
#endif
#endif /* end of _SENSOR_API_H_ */

View File

@ -0,0 +1,148 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_common.h"
#include "bh_queue.h"
#include "bh_thread.h"
#include "runtime_sensor.h"
#include "bi-inc/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;
static bool sensor_check_thread_run = true;
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)
func_onSensorEvent = wasm_runtime_lookup_function(inst, "on_sensor_event",
"(i32i32i32)");
if (!func_onSensorEvent) {
printf("Cannot find function on_sensor_event\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) {
const char *exception = wasm_runtime_get_exception(inst);
if (exception) {
printf("Got exception running wasm code: %s\n",
exception);
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(wasm_app_data->exec_env, func_onSensorEvent,
3, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
printf(":Got exception running wasm code: %s\n",
exception);
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 (sensor_check_thread_run) {
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 set_sensor_reshceduler(void (*callback)());
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, (void *)thread_sensor_check, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
}
void exit_sensor_framework()
{
sensor_check_thread_run = false;
}

View File

@ -0,0 +1,12 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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_APP_LIB_CURRENT_SOURCE ${source_all})