Fix app manager fail to install large app file issue (#555)

Remove the limit of app file size no larger than 1 MB, fix possible memory leak issues when fail to install app file, change message size of aee_host_msg_callback() from uint16 type to uint32 type to fix possible integer overflow issue, and fix some coding style issues of app manager.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-03-03 06:19:24 -06:00
committed by GitHub
parent fe76ce3b68
commit 54e82ec439
14 changed files with 293 additions and 207 deletions

View File

@ -504,18 +504,20 @@ cleanup_app_resource(module_data *m_data)
/* Destroy remain sections (i.e. data segment section for bytecode file
* or text section of aot file) from app file's section list. */
if (is_bytecode)
if (is_bytecode) {
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
destroy_all_wasm_sections((wasm_section_list_t)(wasm_app_data->sections));
#else
bh_assert(0);
#endif
else
}
else {
#if WASM_ENABLE_AOT != 0
destroy_all_aot_sections((aot_section_list_t)(wasm_app_data->sections));
#else
bh_assert(0);
#endif
}
if (wasm_app_data->wasm_module)
wasm_runtime_unload(wasm_app_data->wasm_module);
@ -568,7 +570,7 @@ wasm_app_module_install(request_t * msg)
wasm_app_file_t *wasm_app_file;
wasm_data *wasm_app_data;
package_type_t package_type;
module_data *m_data;
module_data *m_data = NULL;
wasm_module_t module = NULL;
wasm_module_inst_t inst = NULL;
wasm_exec_env_t exec_env = NULL;
@ -589,23 +591,30 @@ wasm_app_module_install(request_t * msg)
return false;
}
/* Judge the app type is AOTed or not */
package_type = get_package_type((uint8 *)msg->payload, msg->payload_len);
wasm_app_file = (wasm_app_file_t *)msg->payload;
/* Check app name */
properties_offset = check_url_start(msg->url, strlen(msg->url), "/applet");
bh_assert(properties_offset > 0);
if (properties_offset <= 0)
return false;
if (properties_offset <= 0) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: invalid app name.");
goto fail;
}
properties = msg->url + properties_offset;
find_key_value(properties, strlen(properties), "name", m_name,
sizeof(m_name) - 1, '&');
if (strlen(m_name) == 0) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: invalid app name.");
return false;
goto fail;
}
if (app_manager_lookup_module_data(m_name)) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: app already installed.");
return false;
goto fail;
}
/* Parse heap size */
@ -620,9 +629,6 @@ wasm_app_module_install(request_t * msg)
heap_size = APP_HEAP_SIZE_MAX;
}
/* Judge the app type is AOTed or not */
package_type = get_package_type((uint8 *) msg->payload, msg->payload_len);
/* Load WASM file and instantiate*/
switch (package_type) {
#if WASM_ENABLE_AOT != 0
@ -639,8 +645,6 @@ wasm_app_module_install(request_t * msg)
AOT_SECTION_TYPE_SIGANATURE
};
wasm_app_file = (wasm_app_file_t *) msg->payload;
bh_assert(wasm_app_file);
aot_file = &wasm_app_file->u.aot;
/* Load AOT module from sections */
@ -649,9 +653,7 @@ wasm_app_module_install(request_t * msg)
if (!module) {
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: load WASM file failed.");
app_manager_printf("error: %s\n", err);
destroy_all_aot_sections(aot_file->sections);
return false;
goto fail;
}
/* Destroy useless sections from list after load */
destroy_part_aot_sections(&aot_file->sections,
@ -663,9 +665,7 @@ wasm_app_module_install(request_t * msg)
wasi_dir_buf, sizeof(wasi_dir_buf))) {
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: prepare wasi env failed.");
wasm_runtime_unload(module);
destroy_all_aot_sections(aot_file->sections);
return false;
goto fail;
}
wasm_runtime_set_wasi_args(module,
wasi_dir_list, 1,
@ -679,10 +679,7 @@ wasm_app_module_install(request_t * msg)
if (!inst) {
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: instantiate wasm runtime failed.");
app_manager_printf("error: %s\n", err);
wasm_runtime_unload(module);
destroy_all_aot_sections(aot_file->sections);
return false;
goto fail;
}
break;
}
@ -712,8 +709,6 @@ wasm_app_module_install(request_t * msg)
/* Sections to be released after instantiating */
uint8 sections2[] = { SECTION_TYPE_DATA };
wasm_app_file = (wasm_app_file_t *) msg->payload;
bh_assert(wasm_app_file);
bytecode_file = &wasm_app_file->u.bytecode;
/* Load wasm module from sections */
@ -722,9 +717,7 @@ wasm_app_module_install(request_t * msg)
if (!module) {
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: load WASM file failed.");
app_manager_printf("error: %s\n", err);
destroy_all_wasm_sections(bytecode_file->sections);
return false;
goto fail;
}
/* Destroy useless sections from list after load */
@ -737,9 +730,7 @@ wasm_app_module_install(request_t * msg)
wasi_dir_buf, sizeof(wasi_dir_buf))) {
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: prepare wasi env failed.");
wasm_runtime_unload(module);
destroy_all_wasm_sections(bytecode_file->sections);
return false;
goto fail;
}
wasm_runtime_set_wasi_args(module,
wasi_dir_list, 1,
@ -753,10 +744,7 @@ wasm_app_module_install(request_t * msg)
if (!inst) {
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: instantiate wasm runtime failed.");
app_manager_printf("error: %s\n", err);
wasm_runtime_unload(module);
destroy_all_wasm_sections(bytecode_file->sections);
return false;
goto fail;
}
/* Destroy useless sections from list after instantiate */
@ -769,7 +757,7 @@ wasm_app_module_install(request_t * msg)
default:
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: invalid wasm package type.");
return false;
goto fail;
}
/* Create module data including the wasm_app_data as its internal_data*/
@ -875,8 +863,13 @@ wasm_app_module_install(request_t * msg)
fail:
if (m_data)
release_module(m_data);
wasm_runtime_deinstantiate(inst);
wasm_runtime_unload(module);
if (inst)
wasm_runtime_deinstantiate(inst);
if (module)
wasm_runtime_unload(module);
if (exec_env)
wasm_runtime_destroy_exec_env(exec_env);
@ -969,7 +962,7 @@ wasm_app_module_uninstall(request_t *msg)
static bool
wasm_app_module_handle_host_url(void *queue_msg)
{
//todo: implement in future
/* TODO: implement in future */
app_manager_printf("App handles host url address %d\n",
(int)(uintptr_t)queue_msg);
return false;
@ -985,7 +978,7 @@ wasm_app_module_get_module_data(void *inst)
static void
wasm_app_module_watchdog_kill(module_data *m_data)
{
//todo: implement in future
/* TODO: implement in future */
app_manager_printf("Watchdog kills app: %s\n", m_data->module_name);
return;
}
@ -997,7 +990,7 @@ wasm_register_msg_callback(int message_type,
int i;
int freeslot = -1;
for (i = 0; i < Max_Msg_Callback; i++) {
// replace handler for the same event registered
/* replace handler for the same event registered */
if (g_msg_type[i] == message_type)
break;
@ -1055,6 +1048,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
int *received_size)
{
uint8 *p;
int magic;
package_type_t package_type = Package_Type_Unknown;
if (recv_ctx.phase == Phase_Req_Ver) {
@ -1102,6 +1096,9 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
APP_MGR_MALLOC(recv_ctx.message.request_url_len + 1);
if (NULL == recv_ctx.message.request_url) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"allocate memory failed.");
goto fail;
}
memset(recv_ctx.message.request_url, 0,
@ -1131,7 +1128,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
if (recv_ctx.size_in_phase ==
sizeof(recv_ctx.message.app_file_magic)) {
int magic = recv_ctx.message.app_file_magic;
magic = recv_ctx.message.app_file_magic;
package_type = get_package_type((uint8 *)&magic, sizeof(magic) + 1);
switch (package_type) {
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
@ -1152,7 +1149,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
#endif
default:
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: invalid file format.");
"Install WASM app failed: "
"invalid file format.");
goto fail;
}
}
@ -1166,6 +1164,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
p[recv_ctx.size_in_phase++] = ch;
else {
app_manager_printf("Invalid WASM version!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: invalid WASM version.");
goto fail;
}
@ -1185,8 +1185,12 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
#endif
if (section_type <= section_type_max) {
wasm_section_t *new_section;
if (!(new_section = (wasm_section_t *) APP_MGR_MALLOC(sizeof(wasm_section_t)))) {
if (!(new_section = (wasm_section_t *)
APP_MGR_MALLOC(sizeof(wasm_section_t)))) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"allocate memory failed.");
goto fail;
}
memset(new_section, 0, sizeof(wasm_section_t));
@ -1209,7 +1213,13 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
return true;
}
else {
char error_buf[128];
app_manager_printf("Invalid wasm section type: %d\n", section_type);
snprintf(error_buf, sizeof(error_buf),
"Install WASM app failed: invalid wasm section type %d",
section_type);
SEND_ERR_RESPONSE(recv_ctx.message.request_mid, error_buf);
goto fail;
}
}
@ -1228,7 +1238,10 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
/* check leab128 overflow for uint32 value */
if (recv_ctx.size_in_phase >
(sizeof(section->section_body_size) * 8 + 7 - 1) / 7) {
app_manager_printf(" LEB overflow when parsing section size\n");
app_manager_printf("LEB overflow when parsing section size\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"LEB overflow when parsing section size");
goto fail;
}
@ -1236,6 +1249,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
/* leb128 encoded section size parsed done */
if (!(section->section_body = APP_MGR_MALLOC(section->section_body_size))) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: allocate memory failed");
goto fail;
}
recv_ctx.phase = Phase_Wasm_Section_Content;
@ -1263,7 +1278,15 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
}
else {
app_manager_printf("Handle install message failed!\n");
goto fail;
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"handle install message failed");
/**
* The sections were destroyed inside
* module_wasm_app_handle_install_msg(),
* no need to destroy again.
*/
return false;
}
}
else {
@ -1283,7 +1306,9 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
if (ch == wasm_aot_version[recv_ctx.size_in_phase])
p[recv_ctx.size_in_phase++] = ch;
else {
app_manager_printf("Invalid WASM AOT version!\n");
app_manager_printf("Invalid AOT version!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: invalid AOT version");
goto fail;
}
@ -1305,8 +1330,12 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
if (aot_file_cur_offset % 4)
return true;
if (!(cur_section = (aot_section_t *) APP_MGR_MALLOC(sizeof(aot_section_t)))) {
if (!(cur_section = (aot_section_t *)
APP_MGR_MALLOC(sizeof(aot_section_t)))) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"allocate memory failed");
goto fail;
}
memset(cur_section, 0, sizeof(aot_section_t));
@ -1336,8 +1365,14 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
recv_ctx.size_in_phase = 0;
}
else {
char error_buf[128];
app_manager_printf("Invalid AOT section id: %d\n",
cur_section->section_type);
snprintf(error_buf, sizeof(error_buf),
"Install WASM app failed: invalid AOT section id %d",
cur_section->section_type);
SEND_ERR_RESPONSE(recv_ctx.message.request_mid, error_buf);
goto fail;
}
}
@ -1375,6 +1410,9 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
os_mmap(NULL, (uint32)total_size,
map_prot, map_flags))) {
app_manager_printf("Allocate executable memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"allocate memory failed");
goto fail;
}
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
@ -1387,6 +1425,9 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
if (!(section->section_body =
APP_MGR_MALLOC(section->section_body_size))) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"allocate memory failed");
goto fail;
}
}
@ -1426,7 +1467,15 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
}
else {
app_manager_printf("Handle install message failed!\n");
goto fail;
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"handle install message failed");
/**
* The sections were destroyed inside
* module_wasm_app_handle_install_msg(),
* no need to destroy again.
*/
return false;
}
}
else {
@ -1441,6 +1490,9 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
#endif /* end of WASM_ENABLE_AOT != 0 */
fail:
/* Restore the package type */
magic = recv_ctx.message.app_file_magic;
package_type = get_package_type((uint8 *)&magic, sizeof(magic) + 1);
switch (package_type) {
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
case Wasm_Module_Bytecode:
@ -1461,10 +1513,7 @@ fail:
recv_ctx.message.request_url = NULL;
}
recv_ctx.phase = Phase_Req_Ver;
recv_ctx.size_in_phase = 0;
recv_ctx.total_received_size = 0;
memset(&recv_ctx, 0, sizeof(recv_ctx));
return false;
}