This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
wasm-micro-runtime/core/app-mgr/app-manager/app_manager.c

411 lines
12 KiB
C
Raw Normal View History

/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "app_manager.h"
#include "app_manager_host.h"
#include "bh_platform.h"
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
2020-01-21 13:26:14 +08:00
#include "bi-inc/attr_container.h"
#include "event.h"
#include "watchdog.h"
#include "coap_ext.h"
/* Queue of app manager */
static bh_queue *g_app_mgr_queue;
void*
get_app_manager_queue()
{
return g_app_mgr_queue;
}
void app_manager_post_applets_update_event()
{
module_data *m_data;
attr_container_t *attr_cont;
request_t msg;
int num = 0, i = 0;
char *url = "/applets";
if (!event_is_registered(url))
return;
if (!(attr_cont = attr_container_create("All Applets"))) {
app_manager_printf("Post applets update event failed: "
"allocate memory failed.");
return;
}
os_mutex_lock(&module_data_list_lock);
m_data = module_data_list;
while (m_data) {
num++;
m_data = m_data->next;
}
if (!(attr_container_set_int(&attr_cont, "num", num))) {
app_manager_printf("Post applets update event failed: "
"set attr container key failed.");
goto fail;
}
m_data = module_data_list;
while (m_data) {
char buf[32];
i++;
snprintf(buf, sizeof(buf), "%s%d", "applet", i);
if (!(attr_container_set_string(&attr_cont, buf, m_data->module_name))) {
app_manager_printf("Post applets update event failed: "
"set attr applet name key failed.");
goto fail;
}
snprintf(buf, sizeof(buf), "%s%d", "heap", i);
if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
app_manager_printf("Post applets update event failed: "
"set attr heap key failed.");
goto fail;
}
m_data = m_data->next;
}
memset(&msg, 0, sizeof(msg));
msg.url = url;
msg.action = COAP_EVENT;
msg.payload = (char*) attr_cont;
send_request_to_host(&msg);
app_manager_printf("Post applets update event success!\n");
attr_container_dump(attr_cont);
fail:
os_mutex_unlock(&module_data_list_lock);
attr_container_destroy(attr_cont);
}
static int get_applets_count()
{
module_data *m_data;
int num = 0;
os_mutex_lock(&module_data_list_lock);
m_data = module_data_list;
while (m_data) {
num++;
m_data = m_data->next;
}
os_mutex_unlock(&module_data_list_lock);
return num;
}
/* Query fw apps info if name = NULL, otherwise query specify app */
static bool app_manager_query_applets(request_t *msg, const char *name)
{
module_data *m_data;
attr_container_t *attr_cont;
int num = 0, i = 0, len;
bool ret = false, found = false;
response_t response[1] = { 0 };
attr_cont = attr_container_create("Applets Info");
if (!attr_cont) {
SEND_ERR_RESPONSE(msg->mid,
"Query Applets failed: allocate memory failed.");
return false;
}
os_mutex_lock(&module_data_list_lock);
m_data = module_data_list;
while (m_data) {
num++;
m_data = m_data->next;
}
if (name == NULL && !(attr_container_set_int(&attr_cont, "num", num))) {
SEND_ERR_RESPONSE(msg->mid,
"Query Applets failed: set attr container key failed.");
goto fail;
}
m_data = module_data_list;
while (m_data) {
char buf[32];
if (name == NULL) {
i++;
snprintf(buf, sizeof(buf), "%s%d", "applet", i);
if (!(attr_container_set_string(&attr_cont, buf,
m_data->module_name))) {
SEND_ERR_RESPONSE(msg->mid,
"Query Applets failed: "
"set attr container key failed.");
goto fail;
}
snprintf(buf, sizeof(buf), "%s%d", "heap", i);
if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
SEND_ERR_RESPONSE(msg->mid,
"Query Applets failed: "
"set attr container heap key failed.");
goto fail;
}
}
else if (!strcmp(name, m_data->module_name)) {
found = true;
if (!(attr_container_set_string(&attr_cont, "name",
m_data->module_name))) {
SEND_ERR_RESPONSE(msg->mid,
"Query Applet failed: "
"set attr container key failed.");
goto fail;
}
if (!(attr_container_set_int(&attr_cont, "heap", m_data->heap_size))) {
SEND_ERR_RESPONSE(msg->mid,
"Query Applet failed: "
"set attr container heap key failed.");
goto fail;
}
}
m_data = m_data->next;
}
if (name != NULL && !found) {
SEND_ERR_RESPONSE(msg->mid,
"Query Applet failed: the app is not found.");
goto fail;
}
len = attr_container_get_serialize_length(attr_cont);
make_response_for_request(msg, response);
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, (char*) attr_cont, len);
send_response_to_host(response);
ret = true;
app_manager_printf("Query Applets success!\n");
attr_container_dump(attr_cont);
fail:
os_mutex_unlock(&module_data_list_lock);
attr_container_destroy(attr_cont);
return ret;
}
void applet_mgt_reqeust_handler(request_t *request, void *unused)
{
bh_message_t msg;
/* deep copy, but not use app self heap, but use global heap */
request_t *req = clone_request(request);
if (!req)
return;
msg = bh_new_msg(RESTFUL_REQUEST, req, sizeof(*req), request_cleaner);
if (!msg) {
request_cleaner(req);
return;
}
bh_post_msg2(get_app_manager_queue(), msg);
}
/* return -1 for error */
static int get_module_type(char *kv_str)
{
int module_type = -1;
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
2020-01-21 13:26:14 +08:00
char type_str[16] = { 0 };
find_key_value(kv_str, strlen(kv_str), "type", type_str,
sizeof(type_str) - 1, '&');
if (strlen(type_str) == 0)
module_type = Module_WASM_App;
else if (strcmp(type_str, "jeff") == 0)
module_type = Module_Jeff;
else if (strcmp(type_str, "wasm") == 0)
module_type = Module_WASM_App;
else if (strcmp(type_str, "wasmlib") == 0)
module_type = Module_WASM_Lib;
return module_type;
}
#define APP_NAME_MAX_LEN 128
/* Queue callback of App Manager */
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);
if (module_type == -1) {
SEND_ERR_RESPONSE(mid,
"Applet Management failed: invalid module type.");
goto fail;
}
/* Install Applet */
if (request->action == COAP_PUT) {
if (get_applets_count() >= MAX_APP_INSTALLATIONS) {
SEND_ERR_RESPONSE(mid,
"Install Applet failed: exceed max app installations.");
goto fail;
}
if (!request->payload) {
SEND_ERR_RESPONSE(mid,
"Install Applet failed: invalid payload.");
goto fail;
}
if (g_module_interfaces[module_type]
&& g_module_interfaces[module_type]->module_install) {
if (!g_module_interfaces[module_type]->module_install(request))
goto fail;
}
}
/* Uninstall Applet */
else if (request->action == COAP_DELETE) {
module_type = get_module_type(request->url + offset);
if (module_type == -1) {
SEND_ERR_RESPONSE(mid,
"Uninstall Applet failed: invalid module type.");
goto fail;
}
if (g_module_interfaces[module_type]
&& g_module_interfaces[module_type]->module_uninstall) {
if (!g_module_interfaces[module_type]->module_uninstall(request))
goto fail;
}
}
/* Query Applets installed */
else if (request->action == COAP_GET) {
char name[APP_NAME_MAX_LEN] = { 0 };
char *properties = request->url + offset;
find_key_value(properties, strlen(properties), "name", name,
sizeof(name) - 1, '&');
if (strlen(name) > 0)
app_manager_query_applets(request, name);
else
app_manager_query_applets(request, NULL);
}
else {
SEND_ERR_RESPONSE(mid, "Invalid request of applet: invalid action");
}
}
/* Event Register/Unregister */
else if ((offset = check_url_start(request->url, strlen(request->url),
"/event/")) > 0) {
char url_buf[256] = { 0 };
strncpy(url_buf, request->url + offset, sizeof(url_buf) - 1);
if (!event_handle_event_request(request->action, url_buf, ID_HOST)) {
SEND_ERR_RESPONSE(mid, "Handle event request failed.");
goto fail;
}
send_error_response_to_host(mid, CONTENT_2_05, NULL); /* OK */
}
else {
int i;
for (i = 0; i < Module_Max; i++) {
if (g_module_interfaces[i]
&& g_module_interfaces[i]->module_handle_host_url) {
if (g_module_interfaces[i]->module_handle_host_url(request))
break;
}
}
}
fail:
return;
}
static void module_interfaces_init()
{
int i;
for (i = 0; i < Module_Max; i++) {
if (g_module_interfaces[i] && g_module_interfaces[i]->module_init)
g_module_interfaces[i]->module_init();
}
}
void app_manager_startup(host_interface *interface)
{
module_interfaces_init();
/* Create queue of App Manager */
g_app_mgr_queue = bh_queue_create();
if (!g_app_mgr_queue)
return;
if (!module_data_list_init())
goto fail1;
if (!watchdog_startup())
goto fail2;
/* Initialize Host */
app_manager_host_init(interface);
am_register_resource("/app/", targeted_app_request_handler, ID_APP_MGR);
/* /app/ and /event/ are both processed by applet_mgt_reqeust_handler */
am_register_resource("/applet", applet_mgt_reqeust_handler, ID_APP_MGR);
am_register_resource("/event/", applet_mgt_reqeust_handler, ID_APP_MGR);
app_manager_printf("App Manager started.\n");
/* Enter loop run */
bh_queue_enter_loop_run(g_app_mgr_queue, app_manager_queue_callback, NULL);
/* Destroy registered resources */
am_cleanup_registeration(ID_APP_MGR);
/* Destroy watchdog */
watchdog_destroy();
fail2:
module_data_list_destroy();
fail1:
bh_queue_destroy(g_app_mgr_queue);
}
#include "module_config.h"
module_interface *g_module_interfaces[Module_Max] = {
#if ENABLE_MODULE_JEFF != 0
&jeff_module_interface,
#else
NULL,
#endif
#if ENABLE_MODULE_WASM_APP != 0
&wasm_app_module_interface,
#else
NULL,
#endif
#if ENABLE_MODULE_WASM_LIB != 0
&wasm_lib_module_interface
#else
NULL
#endif
};