2019-05-17 09:15:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
2019-11-11 23:45:21 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-05-17 09:15:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "native_interface.h"
|
|
|
|
#include "app_manager.h"
|
|
|
|
#include "app_manager_export.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 05:26:14 +00:00
|
|
|
#include "bi-inc/shared_utils.h"
|
|
|
|
#include "bi-inc/attr_container.h"
|
2019-05-17 09:15:25 +00:00
|
|
|
#include "coap_ext.h"
|
|
|
|
|
|
|
|
typedef struct _app_res_register {
|
|
|
|
struct _app_res_register *next;
|
|
|
|
char * url;
|
|
|
|
void (*request_handler)(request_t *, void *);
|
|
|
|
uint32 register_id;
|
|
|
|
} app_res_register_t;
|
|
|
|
|
|
|
|
static app_res_register_t * g_resources = NULL;
|
|
|
|
|
|
|
|
void module_request_handler(request_t *request, void *user_data)
|
|
|
|
{
|
2019-09-10 02:23:46 +00:00
|
|
|
unsigned int mod_id = (unsigned int)(uintptr_t)user_data;
|
2019-05-17 09:15:25 +00:00
|
|
|
bh_message_t msg;
|
|
|
|
module_data *m_data;
|
|
|
|
request_t *req;
|
|
|
|
|
|
|
|
/* Check module name */
|
|
|
|
m_data = module_data_list_lookup_id(mod_id);
|
|
|
|
if (!m_data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_data->wd_timer.is_interrupting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = clone_request(request);
|
|
|
|
if (!req) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set queue message and send to applet's queue */
|
|
|
|
msg = bh_new_msg(RESTFUL_REQUEST, req, sizeof(*req), request_cleaner);
|
|
|
|
if (!msg) {
|
|
|
|
request_cleaner(req);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bh_post_msg2(m_data->queue, msg)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app_manager_printf("Send request to app %s success.\n",
|
|
|
|
m_data->module_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void targeted_app_request_handler(request_t *request, void *unused)
|
|
|
|
{
|
|
|
|
char applet_name[128] = { 0 };
|
|
|
|
int offset;
|
|
|
|
char *url = request->url;
|
|
|
|
module_data *m_data;
|
|
|
|
|
|
|
|
offset = check_url_start(request->url, strlen(request->url), "/app/");
|
|
|
|
|
|
|
|
if (offset <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(applet_name, request->url + offset, sizeof(applet_name) - 1);
|
2020-06-11 06:19:55 +00:00
|
|
|
char *p = strchr(applet_name, '/');
|
2019-05-17 09:15:25 +00:00
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
app_manager_printf("Send request to applet: %s\n", applet_name);
|
|
|
|
|
|
|
|
request->url = p + 1;
|
|
|
|
|
|
|
|
/* Check module name */
|
|
|
|
m_data = module_data_list_lookup(applet_name);
|
|
|
|
if (!m_data) {
|
|
|
|
SEND_ERR_RESPONSE(request->mid,
|
|
|
|
"Send request to applet failed: invalid applet name");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2019-09-10 02:23:46 +00:00
|
|
|
module_request_handler(request, (void *)(uintptr_t)m_data->id);
|
2019-05-17 09:15:25 +00:00
|
|
|
end: request->url = url;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void am_send_response(response_t *response)
|
|
|
|
{
|
|
|
|
module_data *m_data;
|
|
|
|
|
|
|
|
// if the receiver is not any of modules, just forward it to the host
|
|
|
|
m_data = module_data_list_lookup_id(response->reciever);
|
|
|
|
if (!m_data) {
|
|
|
|
send_response_to_host(response);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
response_t * resp_for_send = clone_response(response);
|
|
|
|
if (!resp_for_send) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bh_message_t msg = bh_new_msg(RESTFUL_RESPONSE, resp_for_send,
|
|
|
|
sizeof(*resp_for_send), response_cleaner);
|
|
|
|
if (!msg) {
|
|
|
|
response_cleaner(resp_for_send);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bh_post_msg2(m_data->queue, msg)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void * am_dispatch_request(request_t *request)
|
|
|
|
{
|
|
|
|
app_res_register_t *r = g_resources;
|
|
|
|
|
|
|
|
while (r) {
|
|
|
|
if (check_url_start(request->url, strlen(request->url), r->url) > 0) {
|
2019-09-10 02:23:46 +00:00
|
|
|
r->request_handler(request, (void *)(uintptr_t)r->register_id);
|
2019-05-17 09:15:25 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
r = r->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool am_register_resource(const char *url,
|
|
|
|
void (*request_handler)(request_t *, void *), uint32 register_id)
|
|
|
|
{
|
|
|
|
app_res_register_t * r = g_resources;
|
|
|
|
int register_num = 0;
|
|
|
|
|
|
|
|
while (r) {
|
|
|
|
if (strcmp(r->url, url) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->register_id == register_id)
|
|
|
|
register_num++;
|
|
|
|
|
|
|
|
r = r->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(url) > RESOUCE_EVENT_URL_LEN_MAX)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (register_num >= RESOURCE_REGISTRATION_NUM_MAX)
|
|
|
|
return false;
|
|
|
|
|
2020-03-10 11:54:44 +00:00
|
|
|
r = (app_res_register_t *) APP_MGR_MALLOC(sizeof(app_res_register_t));
|
2019-05-17 09:15:25 +00:00
|
|
|
if (r == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memset(r, 0, sizeof(*r));
|
|
|
|
r->url = bh_strdup(url);
|
|
|
|
if (r->url == NULL) {
|
2020-03-10 11:54:44 +00:00
|
|
|
APP_MGR_FREE(r);
|
2019-05-17 09:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->request_handler = request_handler;
|
|
|
|
r->next = g_resources;
|
|
|
|
r->register_id = register_id;
|
|
|
|
g_resources = r;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void am_cleanup_registeration(uint32 register_id)
|
|
|
|
{
|
|
|
|
app_res_register_t * r = g_resources;
|
|
|
|
app_res_register_t * prev = NULL;
|
|
|
|
|
|
|
|
while (r) {
|
|
|
|
app_res_register_t *next = r->next;
|
|
|
|
|
|
|
|
if (register_id == r->register_id) {
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
else
|
|
|
|
g_resources = next;
|
|
|
|
|
2020-03-10 11:54:44 +00:00
|
|
|
APP_MGR_FREE(r->url);
|
|
|
|
APP_MGR_FREE(r);
|
2019-05-17 09:15:25 +00:00
|
|
|
} else
|
|
|
|
/* if r is freed, should not change prev. Only set prev to r
|
|
|
|
when r isn't freed. */
|
|
|
|
prev = r;
|
|
|
|
|
|
|
|
r = next;
|
|
|
|
}
|
|
|
|
}
|