Просмотр исходного кода

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>
Wenyong Huang 5 лет назад
Родитель
Сommit
54e82ec439

+ 38 - 28
core/app-mgr/app-manager/app_manager.c

@@ -32,8 +32,8 @@ void app_manager_post_applets_update_event()
         return;
         return;
 
 
     if (!(attr_cont = attr_container_create("All Applets"))) {
     if (!(attr_cont = attr_container_create("All Applets"))) {
-        app_manager_printf(
-                "Post applets update event failed: allocate memory failed.");
+        app_manager_printf("Post applets update event failed: "
+                           "allocate memory failed.");
         return;
         return;
     }
     }
 
 
@@ -46,8 +46,8 @@ void app_manager_post_applets_update_event()
     }
     }
 
 
     if (!(attr_container_set_int(&attr_cont, "num", num))) {
     if (!(attr_container_set_int(&attr_cont, "num", num))) {
-        app_manager_printf(
-                "Post applets update event failed: set attr container key failed.");
+        app_manager_printf("Post applets update event failed: "
+                           "set attr container key failed.");
         goto fail;
         goto fail;
     }
     }
 
 
@@ -57,14 +57,14 @@ void app_manager_post_applets_update_event()
         i++;
         i++;
         snprintf(buf, sizeof(buf), "%s%d", "applet", i);
         snprintf(buf, sizeof(buf), "%s%d", "applet", i);
         if (!(attr_container_set_string(&attr_cont, buf, m_data->module_name))) {
         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.");
+            app_manager_printf("Post applets update event failed: "
+                               "set attr applet name key failed.");
             goto fail;
             goto fail;
         }
         }
         snprintf(buf, sizeof(buf), "%s%d", "heap", i);
         snprintf(buf, sizeof(buf), "%s%d", "heap", i);
         if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
         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.");
+            app_manager_printf("Post applets update event failed: "
+                               "set attr heap key failed.");
             goto fail;
             goto fail;
         }
         }
         m_data = m_data->next;
         m_data = m_data->next;
@@ -114,7 +114,7 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
     attr_cont = attr_container_create("Applets Info");
     attr_cont = attr_container_create("Applets Info");
     if (!attr_cont) {
     if (!attr_cont) {
         SEND_ERR_RESPONSE(msg->mid,
         SEND_ERR_RESPONSE(msg->mid,
-                "Query Applets failed: allocate memory failed.");
+                          "Query Applets failed: allocate memory failed.");
         return false;
         return false;
     }
     }
 
 
@@ -128,7 +128,7 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
 
 
     if (name == NULL && !(attr_container_set_int(&attr_cont, "num", num))) {
     if (name == NULL && !(attr_container_set_int(&attr_cont, "num", num))) {
         SEND_ERR_RESPONSE(msg->mid,
         SEND_ERR_RESPONSE(msg->mid,
-                "Query Applets failed: set attr container key failed.");
+                          "Query Applets failed: set attr container key failed.");
         goto fail;
         goto fail;
     }
     }
 
 
@@ -142,26 +142,31 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
             if (!(attr_container_set_string(&attr_cont, buf,
             if (!(attr_container_set_string(&attr_cont, buf,
                     m_data->module_name))) {
                     m_data->module_name))) {
                 SEND_ERR_RESPONSE(msg->mid,
                 SEND_ERR_RESPONSE(msg->mid,
-                        "Query Applets failed: set attr container key failed.");
+                                  "Query Applets failed: "
+                                  "set attr container key failed.");
                 goto fail;
                 goto fail;
             }
             }
             snprintf(buf, sizeof(buf), "%s%d", "heap", i);
             snprintf(buf, sizeof(buf), "%s%d", "heap", i);
             if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
             if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
                 SEND_ERR_RESPONSE(msg->mid,
                 SEND_ERR_RESPONSE(msg->mid,
-                        "Query Applets failed: set attr container heap key failed.");
+                                  "Query Applets failed: "
+                                  "set attr container heap key failed.");
                 goto fail;
                 goto fail;
             }
             }
-        } else if (!strcmp(name, m_data->module_name)) {
+        }
+        else if (!strcmp(name, m_data->module_name)) {
             found = true;
             found = true;
             if (!(attr_container_set_string(&attr_cont, "name",
             if (!(attr_container_set_string(&attr_cont, "name",
                     m_data->module_name))) {
                     m_data->module_name))) {
                 SEND_ERR_RESPONSE(msg->mid,
                 SEND_ERR_RESPONSE(msg->mid,
-                        "Query Applet failed: set attr container key failed.");
+                                  "Query Applet failed: "
+                                  "set attr container key failed.");
                 goto fail;
                 goto fail;
             }
             }
             if (!(attr_container_set_int(&attr_cont, "heap", m_data->heap_size))) {
             if (!(attr_container_set_int(&attr_cont, "heap", m_data->heap_size))) {
                 SEND_ERR_RESPONSE(msg->mid,
                 SEND_ERR_RESPONSE(msg->mid,
-                        "Query Applet failed: set attr container heap key failed.");
+                                  "Query Applet failed: "
+                                  "set attr container heap key failed.");
                 goto fail;
                 goto fail;
             }
             }
         }
         }
@@ -266,7 +271,7 @@ static void app_manager_queue_callback(void *message, void *arg)
                 goto fail;
                 goto fail;
             }
             }
             if (g_module_interfaces[module_type]
             if (g_module_interfaces[module_type]
-                    && g_module_interfaces[module_type]->module_install) {
+                && g_module_interfaces[module_type]->module_install) {
                 if (!g_module_interfaces[module_type]->module_install(request))
                 if (!g_module_interfaces[module_type]->module_install(request))
                     goto fail;
                     goto fail;
             }
             }
@@ -276,14 +281,13 @@ static void app_manager_queue_callback(void *message, void *arg)
             module_type = get_module_type(request->url + offset);
             module_type = get_module_type(request->url + offset);
             if (module_type == -1) {
             if (module_type == -1) {
                 SEND_ERR_RESPONSE(mid,
                 SEND_ERR_RESPONSE(mid,
-                        "Uninstall Applet failed: invalid module type.");
+                                  "Uninstall Applet failed: invalid module type.");
                 goto fail;
                 goto fail;
             }
             }
 
 
             if (g_module_interfaces[module_type]
             if (g_module_interfaces[module_type]
                     && g_module_interfaces[module_type]->module_uninstall) {
                     && g_module_interfaces[module_type]->module_uninstall) {
-                if (!g_module_interfaces[module_type]->module_uninstall(
-                        request))
+                if (!g_module_interfaces[module_type]->module_uninstall(request))
                     goto fail;
                     goto fail;
             }
             }
         }
         }
@@ -292,18 +296,19 @@ static void app_manager_queue_callback(void *message, void *arg)
             char name[APP_NAME_MAX_LEN] = { 0 };
             char name[APP_NAME_MAX_LEN] = { 0 };
             char *properties = request->url + offset;
             char *properties = request->url + offset;
             find_key_value(properties, strlen(properties), "name", name,
             find_key_value(properties, strlen(properties), "name", name,
-                    sizeof(name) - 1, '&');
+                           sizeof(name) - 1, '&');
             if (strlen(name) > 0)
             if (strlen(name) > 0)
                 app_manager_query_applets(request, name);
                 app_manager_query_applets(request, name);
             else
             else
                 app_manager_query_applets(request, NULL);
                 app_manager_query_applets(request, NULL);
-        } else {
+        }
+        else {
             SEND_ERR_RESPONSE(mid, "Invalid request of applet: invalid action");
             SEND_ERR_RESPONSE(mid, "Invalid request of applet: invalid action");
         }
         }
     }
     }
     /* Event Register/Unregister */
     /* Event Register/Unregister */
     else if ((offset = check_url_start(request->url, strlen(request->url),
     else if ((offset = check_url_start(request->url, strlen(request->url),
-            "/event/")) > 0) {
+                                       "/event/")) > 0) {
         char url_buf[256] = { 0 };
         char url_buf[256] = { 0 };
 
 
         strncpy(url_buf, request->url + offset, sizeof(url_buf) - 1);
         strncpy(url_buf, request->url + offset, sizeof(url_buf) - 1);
@@ -313,11 +318,12 @@ static void app_manager_queue_callback(void *message, void *arg)
             goto fail;
             goto fail;
         }
         }
         send_error_response_to_host(mid, CONTENT_2_05, NULL); /* OK */
         send_error_response_to_host(mid, CONTENT_2_05, NULL); /* OK */
-    } else {
+    }
+    else {
         int i;
         int i;
         for (i = 0; i < Module_Max; i++) {
         for (i = 0; i < Module_Max; i++) {
             if (g_module_interfaces[i]
             if (g_module_interfaces[i]
-                    && g_module_interfaces[i]->module_handle_host_url) {
+                && g_module_interfaces[i]->module_handle_host_url) {
                 if (g_module_interfaces[i]->module_handle_host_url(request))
                 if (g_module_interfaces[i]->module_handle_host_url(request))
                     break;
                     break;
             }
             }
@@ -325,10 +331,8 @@ static void app_manager_queue_callback(void *message, void *arg)
 
 
     }
     }
 
 
-    fail:
-
+fail:
     return;
     return;
-
 }
 }
 
 
 static void module_interfaces_init()
 static void module_interfaces_init()
@@ -360,7 +364,7 @@ void app_manager_startup(host_interface *interface)
 
 
     am_register_resource("/app/", targeted_app_request_handler, ID_APP_MGR);
     am_register_resource("/app/", targeted_app_request_handler, ID_APP_MGR);
 
 
-    /*/app/ and /event/ are both processed by applet_mgt_reqeust_handler*/
+    /* /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("/applet", applet_mgt_reqeust_handler, ID_APP_MGR);
     am_register_resource("/event/", applet_mgt_reqeust_handler, ID_APP_MGR);
     am_register_resource("/event/", applet_mgt_reqeust_handler, ID_APP_MGR);
 
 
@@ -369,6 +373,12 @@ void app_manager_startup(host_interface *interface)
     /* Enter loop run */
     /* Enter loop run */
     bh_queue_enter_loop_run(g_app_mgr_queue, app_manager_queue_callback, NULL);
     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:
 fail2:
     module_data_list_destroy();
     module_data_list_destroy();
 
 

+ 53 - 27
core/app-mgr/app-manager/app_manager_host.c

@@ -13,11 +13,19 @@
 static host_interface host_commu;
 static host_interface host_commu;
 
 
 /* IMRTLink Two leading bytes */
 /* IMRTLink Two leading bytes */
-static unsigned char leadings[] = { (unsigned char) 0x12, (unsigned char) 0x34 };
+static unsigned char leadings[] = {
+    (unsigned char)0x12,
+    (unsigned char)0x34
+};
 
 
 /* IMRTLink Receiving Phase */
 /* IMRTLink Receiving Phase */
 typedef enum recv_phase_t {
 typedef enum recv_phase_t {
-    Phase_Non_Start, Phase_Leading, Phase_Type, Phase_Size, Phase_Payload
+    Phase_Non_Start,
+    Phase_Leading,
+    Phase_Type,
+    Phase_Size,
+    Phase_Payload,
+    Phase_Ignoring
 } recv_phase_t;
 } recv_phase_t;
 
 
 /* IMRTLink Receive Context */
 /* IMRTLink Receive Context */
@@ -74,7 +82,8 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
         }
         }
 
 
         return 0;
         return 0;
-    } else if (ctx->phase == Phase_Leading) {
+    }
+    else if (ctx->phase == Phase_Leading) {
         if (ch == leadings[1]) {
         if (ch == leadings[1]) {
             if (enable_log)
             if (enable_log)
                 app_manager_printf("##On byte arrive: got leading 1\n");
                 app_manager_printf("##On byte arrive: got leading 1\n");
@@ -83,12 +92,14 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
             ctx->phase = Phase_Non_Start;
             ctx->phase = Phase_Non_Start;
 
 
         return 0;
         return 0;
-    } else if (ctx->phase == Phase_Type) {
+    }
+    else if (ctx->phase == Phase_Type) {
         if (ctx->size_in_phase++ == 0) {
         if (ctx->size_in_phase++ == 0) {
             if (enable_log)
             if (enable_log)
                 app_manager_printf("##On byte arrive: got type 0\n");
                 app_manager_printf("##On byte arrive: got type 0\n");
             ctx->message.message_type = ch;
             ctx->message.message_type = ch;
-        } else {
+        }
+        else {
             if (enable_log)
             if (enable_log)
                 app_manager_printf("##On byte arrive: got type 1\n");
                 app_manager_printf("##On byte arrive: got type 1\n");
             ctx->message.message_type |= (ch << 8);
             ctx->message.message_type |= (ch << 8);
@@ -98,12 +109,13 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
         }
         }
 
 
         return 0;
         return 0;
-    } else if (ctx->phase == Phase_Size) {
+    }
+    else if (ctx->phase == Phase_Size) {
         unsigned char *p = (unsigned char *) &ctx->message.payload_size;
         unsigned char *p = (unsigned char *) &ctx->message.payload_size;
 
 
         if (enable_log)
         if (enable_log)
             app_manager_printf("##On byte arrive: got payload_size, byte %d\n",
             app_manager_printf("##On byte arrive: got payload_size, byte %d\n",
-                    ctx->size_in_phase);
+                               ctx->size_in_phase);
         p[ctx->size_in_phase++] = ch;
         p[ctx->size_in_phase++] = ch;
 
 
         if (ctx->size_in_phase == sizeof(ctx->message.payload_size)) {
         if (ctx->size_in_phase == sizeof(ctx->message.payload_size)) {
@@ -112,7 +124,7 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
 
 
             if (enable_log)
             if (enable_log)
                 app_manager_printf("##On byte arrive: payload_size: %d\n",
                 app_manager_printf("##On byte arrive: payload_size: %d\n",
-                        ctx->message.payload_size);
+                                   ctx->message.payload_size);
             if (ctx->message.payload) {
             if (ctx->message.payload) {
                 APP_MGR_FREE(ctx->message.payload);
                 APP_MGR_FREE(ctx->message.payload);
                 ctx->message.payload = NULL;
                 ctx->message.payload = NULL;
@@ -122,16 +134,11 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
             if (ctx->message.payload_size == 0) {
             if (ctx->message.payload_size == 0) {
                 ctx->phase = Phase_Non_Start;
                 ctx->phase = Phase_Non_Start;
                 if (enable_log)
                 if (enable_log)
-                    app_manager_printf(
-                            "##On byte arrive: receive end, payload_size is 0.\n");
+                    app_manager_printf("##On byte arrive: receive end, "
+                                       "payload_size is 0.\n");
                 return 1;
                 return 1;
             }
             }
 
 
-            if (ctx->message.payload_size > 1024 * 1024) {
-                ctx->phase = Phase_Non_Start;
-                return 0;
-            }
-
             if (ctx->message.message_type != INSTALL_WASM_APP) {
             if (ctx->message.message_type != INSTALL_WASM_APP) {
                 ctx->message.payload =
                 ctx->message.payload =
                     (char *) APP_MGR_MALLOC(ctx->message.payload_size);
                     (char *) APP_MGR_MALLOC(ctx->message.payload_size);
@@ -146,7 +153,8 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
         }
         }
 
 
         return 0;
         return 0;
-    } else if (ctx->phase == Phase_Payload) {
+    }
+    else if (ctx->phase == Phase_Payload) {
         if (ctx->message.message_type == INSTALL_WASM_APP) {
         if (ctx->message.message_type == INSTALL_WASM_APP) {
             int received_size;
             int received_size;
             module_on_install_request_byte_arrive_func module_on_install =
             module_on_install_request_byte_arrive_func module_on_install =
@@ -162,36 +170,53 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
                         ctx->phase = Phase_Non_Start;
                         ctx->phase = Phase_Non_Start;
                         return 1;
                         return 1;
                     }
                     }
-                } else {
+                }
+                else {
                     /* receive or handle fail */
                     /* receive or handle fail */
-                    ctx->phase = Phase_Non_Start;
-                    ctx->size_in_phase = 0;
+                    if (ctx->size_in_phase < ctx->message.payload_size) {
+                        ctx->phase = Phase_Ignoring;
+                    }
+                    else {
+                        ctx->phase = Phase_Non_Start;
+                        ctx->size_in_phase = 0;
+                    }
                     return 0;
                     return 0;
                 }
                 }
-                return 0;
-            } else {
+            }
+            else {
                 ctx->phase = Phase_Non_Start;
                 ctx->phase = Phase_Non_Start;
                 ctx->size_in_phase = 0;
                 ctx->size_in_phase = 0;
                 return 0;
                 return 0;
             }
             }
-        } else {
+        }
+        else {
             ctx->message.payload[ctx->size_in_phase++] = ch;
             ctx->message.payload[ctx->size_in_phase++] = ch;
 
 
             if (ctx->size_in_phase == ctx->message.payload_size) {
             if (ctx->size_in_phase == ctx->message.payload_size) {
                 ctx->phase = Phase_Non_Start;
                 ctx->phase = Phase_Non_Start;
                 if (enable_log)
                 if (enable_log)
-                    app_manager_printf("##On byte arrive: receive end, payload_size is %d.\n",
+                    app_manager_printf("##On byte arrive: receive end, "
+                                       "payload_size is %d.\n",
                                        ctx->message.payload_size);
                                        ctx->message.payload_size);
                 return 1;
                 return 1;
             }
             }
             return 0;
             return 0;
         }
         }
     }
     }
+    else if (ctx->phase == Phase_Ignoring) {
+        ctx->size_in_phase++;
+        if (ctx->size_in_phase == ctx->message.payload_size) {
+            if (ctx->message.payload)
+                APP_MGR_FREE(ctx->message.payload);
+            memset(ctx, 0, sizeof(*ctx));
+            return 0;
+        }
+    }
 
 
     return 0;
     return 0;
 }
 }
 
 
-int aee_host_msg_callback(void *msg, uint16_t msg_len)
+int aee_host_msg_callback(void *msg, uint32_t msg_len)
 {
 {
     unsigned char *p = msg, *p_end = p + msg_len;
     unsigned char *p = msg, *p_end = p + msg_len;
 
 
@@ -259,8 +284,8 @@ int app_manager_host_send_msg(int msg_type, const char *buf, int size)
         bh_memcpy_s(header, 2, leadings, 2);
         bh_memcpy_s(header, 2, leadings, 2);
 
 
         /* message type */
         /* message type */
-        // todo: check if use network byte order!!!
-        *((uint16*) (header + 2)) = htons(msg_type);
+        /* TODO: check if use network byte order!!! */
+        *((uint16*)(header + 2)) = htons(msg_type);
 
 
         /* payload length */
         /* payload length */
         if (is_little_endian())
         if (is_little_endian())
@@ -279,7 +304,8 @@ int app_manager_host_send_msg(int msg_type, const char *buf, int size)
 
 
         app_manager_printf("sent %d bytes to host\n", n);
         app_manager_printf("sent %d bytes to host\n", n);
         return n;
         return n;
-    } else {
+    }
+    else {
         app_manager_printf("no send api provided\n");
         app_manager_printf("no send api provided\n");
     }
     }
     return 0;
     return 0;

+ 101 - 52
core/app-mgr/app-manager/module_wasm_app.c

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

+ 3 - 2
core/app-mgr/app-manager/resource_reg.c

@@ -136,7 +136,8 @@ void * am_dispatch_request(request_t *request)
 }
 }
 
 
 bool am_register_resource(const char *url,
 bool am_register_resource(const char *url,
-        void (*request_handler)(request_t *, void *), uint32 register_id)
+                          void (*request_handler)(request_t *, void *),
+                          uint32 register_id)
 {
 {
     app_res_register_t * r = g_resources;
     app_res_register_t * r = g_resources;
     int register_num = 0;
     int register_num = 0;
@@ -158,7 +159,7 @@ bool am_register_resource(const char *url,
     if (register_num >= RESOURCE_REGISTRATION_NUM_MAX)
     if (register_num >= RESOURCE_REGISTRATION_NUM_MAX)
         return false;
         return false;
 
 
-    r = (app_res_register_t *) APP_MGR_MALLOC(sizeof(app_res_register_t));
+    r = (app_res_register_t *)APP_MGR_MALLOC(sizeof(app_res_register_t));
     if (r == NULL)
     if (r == NULL)
         return false;
         return false;
 
 

+ 6 - 0
core/app-mgr/app-manager/watchdog.c

@@ -124,3 +124,9 @@ bool watchdog_startup()
 #endif
 #endif
     return true;
     return true;
 }
 }
+
+bool watchdog_destroy()
+{
+    bh_queue_exit_loop_run(watchdog_queue);
+    bh_queue_destroy(watchdog_queue);
+}

+ 3 - 0
core/app-mgr/app-manager/watchdog.h

@@ -31,6 +31,9 @@ app_manager_get_watchdog_timer(void *timer);
 bool
 bool
 watchdog_startup();
 watchdog_startup();
 
 
+bool
+watchdog_destroy();
+
 #ifdef __cplusplus
 #ifdef __cplusplus
 } /* end of extern "C" */
 } /* end of extern "C" */
 #endif
 #endif

+ 1 - 0
core/shared/mem-alloc/ems/ems_alloc.c

@@ -570,6 +570,7 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
         }
         }
     }
     }
 
 
+
     hmu = alloc_hmu_ex(heap, tot_size);
     hmu = alloc_hmu_ex(heap, tot_size);
     if (!hmu)
     if (!hmu)
         goto finish;
         goto finish;

+ 1 - 1
samples/gui/wasm-runtime-wgl/src/platform/linux/iwasm_main.c

@@ -48,7 +48,7 @@ static int baudrate = B115200;
 extern void init_sensor_framework();
 extern void init_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_connection_framework();
 extern void exit_connection_framework();
-extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
+extern int aee_host_msg_callback(void *msg, uint32_t msg_len);
 extern bool init_connection_framework();
 extern bool init_connection_framework();
 
 
 #ifndef CONNECTION_UART
 #ifndef CONNECTION_UART

+ 1 - 1
samples/gui/wasm-runtime-wgl/src/platform/zephyr/iwasm_main.c

@@ -18,7 +18,7 @@
 
 
 extern void init_sensor_framework();
 extern void init_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_sensor_framework();
-extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
+extern int aee_host_msg_callback(void *msg, uint32_t msg_len);
 extern bool touchscreen_read(lv_indev_data_t * data);
 extern bool touchscreen_read(lv_indev_data_t * data);
 extern int ili9340_init();
 extern int ili9340_init();
 extern void xpt2046_init(void);
 extern void xpt2046_init(void);

+ 1 - 1
samples/littlevgl/vgl-wasm-runtime/src/platform/linux/iwasm_main.c

@@ -46,7 +46,7 @@ static int baudrate = B115200;
 extern void init_sensor_framework();
 extern void init_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_connection_framework();
 extern void exit_connection_framework();
-extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
+extern int aee_host_msg_callback(void *msg, uint32_t msg_len);
 extern bool init_connection_framework();
 extern bool init_connection_framework();
 
 
 #ifndef CONNECTION_UART
 #ifndef CONNECTION_UART

+ 1 - 1
samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c

@@ -23,7 +23,7 @@
 
 
 extern void init_sensor_framework();
 extern void init_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_sensor_framework();
-extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
+extern int aee_host_msg_callback(void *msg, uint32_t msg_len);
 
 
 int uart_char_cnt = 0;
 int uart_char_cnt = 0;
 
 

+ 1 - 1
samples/simple/src/iwasm_main.c

@@ -44,7 +44,7 @@ static int baudrate = B115200;
 extern void init_sensor_framework();
 extern void init_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_sensor_framework();
 extern void exit_connection_framework();
 extern void exit_connection_framework();
-extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
+extern int aee_host_msg_callback(void *msg, uint32_t msg_len);
 extern bool init_connection_framework();
 extern bool init_connection_framework();
 
 
 #ifndef CONNECTION_UART
 #ifndef CONNECTION_UART

+ 69 - 84
test-tools/host-tool/src/main.c

@@ -30,7 +30,12 @@
 #define CONNECTION_MODE_UART 2
 #define CONNECTION_MODE_UART 2
 
 
 typedef enum {
 typedef enum {
-    INSTALL, UNINSTALL, QUERY, REQUEST, REGISTER, UNREGISTER
+    INSTALL,
+    UNINSTALL,
+    QUERY,
+    REQUEST,
+    REGISTER,
+    UNREGISTER
 } op_type;
 } op_type;
 
 
 typedef struct {
 typedef struct {
@@ -81,7 +86,8 @@ typedef struct {
 } operation;
 } operation;
 
 
 typedef enum REPLY_PACKET_TYPE {
 typedef enum REPLY_PACKET_TYPE {
-    REPLY_TYPE_EVENT = 0, REPLY_TYPE_RESPONSE = 1
+    REPLY_TYPE_EVENT = 0,
+    REPLY_TYPE_RESPONSE = 1
 } REPLY_PACKET_TYPE;
 } REPLY_PACKET_TYPE;
 
 
 static uint32_t g_timeout_ms = DEFAULT_TIMEOUT_MS;
 static uint32_t g_timeout_ms = DEFAULT_TIMEOUT_MS;
@@ -119,7 +125,7 @@ static int send_request(request_t *request, uint16_t msg_type)
     /* payload length */
     /* payload length */
     req_size_n = htonl(req_size);
     req_size_n = htonl(req_size);
     if (!host_tool_send_data(g_conn_fd, (char *) &req_size_n,
     if (!host_tool_send_data(g_conn_fd, (char *) &req_size_n,
-            sizeof(req_size_n)))
+                             sizeof(req_size_n)))
         goto ret;
         goto ret;
 
 
     /* payload */
     /* payload */
@@ -128,29 +134,16 @@ static int send_request(request_t *request, uint16_t msg_type)
 
 
     ret = 0;
     ret = 0;
 
 
-    ret: free_req_resp_packet(req_p);
-
+ret:
+    free_req_resp_packet(req_p);
     return ret;
     return ret;
 }
 }
 
 
-/*
-static package_type_t get_app_package_type(const char *buf, int size)
-{
-    if (buf && size > 4) {
-        if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
-            return Wasm_Module_Bytecode;
-        if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
-            return Wasm_Module_AoT;
-    }
-    return Package_Type_Unknown;
-}
-*/
-
 #define url_remain_space (sizeof(url) - strlen(url))
 #define url_remain_space (sizeof(url) - strlen(url))
 
 
-/*return:
- 0: success
- others: fail*/
+/**
+ * return: 0: success, others: fail
+ */
 static int install(inst_info *info)
 static int install(inst_info *info)
 {
 {
     request_t request[1] = { 0 };
     request_t request[1] = { 0 };
@@ -176,13 +169,11 @@ static int install(inst_info *info)
         snprintf(url + strlen(url), url_remain_space, "&wd=%d",
         snprintf(url + strlen(url), url_remain_space, "&wd=%d",
                 info->watchdog_interval);
                 info->watchdog_interval);
 
 
-    /*TODO: permissions to access JLF resource: AUDIO LOCATION SENSOR VISION platform.SERVICE */
-
     if ((app_file_buf = read_file_to_buffer(info->file, &app_size)) == NULL)
     if ((app_file_buf = read_file_to_buffer(info->file, &app_size)) == NULL)
         return -1;
         return -1;
 
 
-    init_request(request, url, COAP_PUT,
-    FMT_APP_RAW_BINARY, app_file_buf, app_size);
+    init_request(request, url, COAP_PUT, FMT_APP_RAW_BINARY,
+                 app_file_buf, app_size);
     request->mid = gen_random_id();
     request->mid = gen_random_id();
 
 
     if (info->module_type == NULL || strcmp(info->module_type, "wasm") == 0)
     if (info->module_type == NULL || strcmp(info->module_type, "wasm") == 0)
@@ -204,11 +195,10 @@ static int uninstall(uninst_info *info)
 
 
     if (info->module_type != NULL && url_remain_space > 0)
     if (info->module_type != NULL && url_remain_space > 0)
         snprintf(url + strlen(url), url_remain_space, "&type=%s",
         snprintf(url + strlen(url), url_remain_space, "&type=%s",
-                info->module_type);
+                 info->module_type);
 
 
-    init_request(request, url, COAP_DELETE,
-    FMT_ATTR_CONTAINER,
-    NULL, 0);
+    init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER,
+                 NULL, 0);
     request->mid = gen_random_id();
     request->mid = gen_random_id();
 
 
     return send_request(request, REQUEST_PACKET);
     return send_request(request, REQUEST_PACKET);
@@ -217,7 +207,6 @@ static int uninstall(uninst_info *info)
 static int query(query_info *info)
 static int query(query_info *info)
 {
 {
     request_t request[1] = { 0 };
     request_t request[1] = { 0 };
-    int ret = -1;
     char url[URL_MAX_LEN] = { 0 };
     char url[URL_MAX_LEN] = { 0 };
 
 
     if (info->name != NULL)
     if (info->name != NULL)
@@ -225,14 +214,10 @@ static int query(query_info *info)
     else
     else
         snprintf(url, sizeof(url) - 1, "/applet");
         snprintf(url, sizeof(url) - 1, "/applet");
 
 
-    init_request(request, url, COAP_GET,
-    FMT_ATTR_CONTAINER,
-    NULL, 0);
+    init_request(request, url, COAP_GET, FMT_ATTR_CONTAINER, NULL, 0);
     request->mid = gen_random_id();
     request->mid = gen_random_id();
 
 
-    ret = send_request(request, REQUEST_PACKET);
-
-    return ret;
+    return send_request(request, REQUEST_PACKET);
 }
 }
 
 
 static int request(req_info *info)
 static int request(req_info *info)
@@ -247,7 +232,7 @@ static int request(req_info *info)
         int payload_file_size;
         int payload_file_size;
 
 
         if ((payload_file = read_file_to_buffer(info->json_payload_file,
         if ((payload_file = read_file_to_buffer(info->json_payload_file,
-                &payload_file_size)) == NULL)
+                                                &payload_file_size)) == NULL)
             return -1;
             return -1;
 
 
         if (NULL == (json = cJSON_Parse(payload_file))) {
         if (NULL == (json = cJSON_Parse(payload_file))) {
@@ -267,7 +252,7 @@ static int request(req_info *info)
     }
     }
 
 
     init_request(request, (char *)info->url, info->action,
     init_request(request, (char *)info->url, info->action,
-    FMT_ATTR_CONTAINER, payload, payload_len);
+                 FMT_ATTR_CONTAINER, payload, payload_len);
     request->mid = gen_random_id();
     request->mid = gen_random_id();
 
 
     ret = send_request(request, REQUEST_PACKET);
     ret = send_request(request, REQUEST_PACKET);
@@ -275,12 +260,13 @@ static int request(req_info *info)
     if (info->json_payload_file != NULL && payload != NULL)
     if (info->json_payload_file != NULL && payload != NULL)
         attr_container_destroy(payload);
         attr_container_destroy(payload);
 
 
-    fail: return ret;
+fail:
+    return ret;
 }
 }
 
 
-/*
- TODO: currently only support 1 url.
- how to handle multiple responses and set process's exit code?
+/**
+ * TODO: currently only support 1 url.
+ * how to handle multiple responses and set process's exit code?
  */
  */
 static int subscribe(reg_info *info)
 static int subscribe(reg_info *info)
 {
 {
@@ -307,9 +293,8 @@ static int subscribe(reg_info *info)
     char url[URL_MAX_LEN] = { 0 };
     char url[URL_MAX_LEN] = { 0 };
     char *prefix = info->urls[0] == '/' ? "/event" : "/event/";
     char *prefix = info->urls[0] == '/' ? "/event" : "/event/";
     snprintf(url, URL_MAX_LEN, "%s%s", prefix, info->urls);
     snprintf(url, URL_MAX_LEN, "%s%s", prefix, info->urls);
-    init_request(request, url, COAP_PUT,
-    FMT_ATTR_CONTAINER,
-    NULL, 0);
+    init_request(request, url, COAP_PUT, FMT_ATTR_CONTAINER,
+                 NULL, 0);
     request->mid = gen_random_id();
     request->mid = gen_random_id();
     ret = send_request(request, REQUEST_PACKET);
     ret = send_request(request, REQUEST_PACKET);
 #endif
 #endif
@@ -340,9 +325,8 @@ static int unsubscribe(unreg_info *info)
 #else
 #else
     char url[URL_MAX_LEN] = { 0 };
     char url[URL_MAX_LEN] = { 0 };
     snprintf(url, URL_MAX_LEN, "%s%s", "/event/", info->urls);
     snprintf(url, URL_MAX_LEN, "%s%s", "/event/", info->urls);
-    init_request(request, url, COAP_DELETE,
-    FMT_ATTR_CONTAINER,
-    NULL, 0);
+    init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER,
+                 NULL, 0);
     request->mid = gen_random_id();
     request->mid = gen_random_id();
     ret = send_request(request, REQUEST_PACKET);
     ret = send_request(request, REQUEST_PACKET);
 #endif
 #endif
@@ -357,7 +341,8 @@ static int init()
             return -1;
             return -1;
         g_conn_fd = fd;
         g_conn_fd = fd;
         return 0;
         return 0;
-    } else if (g_connection_mode == CONNECTION_MODE_UART) {
+    }
+    else if (g_connection_mode == CONNECTION_MODE_UART) {
         int fd;
         int fd;
         if (!uart_init(g_uart_dev, g_baudrate, &fd))
         if (!uart_init(g_uart_dev, g_baudrate, &fd))
             return -1;
             return -1;
@@ -389,7 +374,6 @@ static int parse_action(const char *str)
 static void showUsage()
 static void showUsage()
 {
 {
     printf("\n");
     printf("\n");
-    /*printf("Usage: host_tool [-i|--install]|[-u|--uninstall]|[-q|--query]|[-r|--request]|[-s|--register]|[-d|--deregister] ...\n");*/
     printf("Usage:\n\thost_tool -i|-u|-q|-r|-s|-d ...\n\n");
     printf("Usage:\n\thost_tool -i|-u|-q|-r|-s|-d ...\n\n");
 
 
     printf("\thost_tool -i <App Name> -f <App File>\n"
     printf("\thost_tool -i <App Name> -f <App File>\n"
@@ -453,26 +437,24 @@ static void showUsage()
     printf("\t<Watchdog Interval>=Watchdog interval in ms.\n");
     printf("\t<Watchdog Interval>=Watchdog interval in ms.\n");
 }
 }
 
 
-#define CHECK_DUPLICATE_OPERATION do{ \
-  if (operation_parsed)               \
-  {                                   \
-    showUsage();                      \
-    return false;                     \
-  }                                   \
-}while(0)
-
-#define ERROR_RETURN do{   \
-  showUsage();             \
-  return false;            \
-}while(0)
-
-#define CHECK_ARGS_UNMATCH_OPERATION(op_type) do{ \
-  if (!operation_parsed || op->type != op_type)   \
-  {                                    \
-    showUsage();                       \
-    return false;                      \
-  }                                    \
-}while(0)
+#define CHECK_DUPLICATE_OPERATION do {  \
+  if (operation_parsed) {               \
+    showUsage();                        \
+    return false;                       \
+  }                                     \
+} while(0)
+
+#define ERROR_RETURN do {               \
+  showUsage();                          \
+  return false;                         \
+} while(0)
+
+#define CHECK_ARGS_UNMATCH_OPERATION(op_type) do {  \
+  if (!operation_parsed || op->type != op_type) {   \
+    showUsage();                                    \
+    return false;                                   \
+  }                                                 \
+} while(0)
 
 
 static bool parse_args(int argc, char *argv[], operation *op)
 static bool parse_args(int argc, char *argv[], operation *op)
 {
 {
@@ -509,7 +491,7 @@ static bool parse_args(int argc, char *argv[], operation *op)
         };
         };
 
 
         c = getopt_long(argc, argv, "i:u:q::r:s:d:t:a:o:U:A:f:p:S:P:D:B:h",
         c = getopt_long(argc, argv, "i:u:q::r:s:d:t:a:o:U:A:f:p:S:P:D:B:h",
-                longOpts, &optIndex);
+                        longOpts, &optIndex);
         if (c == -1)
         if (c == -1)
             break;
             break;
 
 
@@ -661,11 +643,10 @@ static bool parse_args(int argc, char *argv[], operation *op)
     return true;
     return true;
 }
 }
 
 
-/*
- return value:
- < 0: not complete message
- REPLY_TYPE_EVENT: event(request)
- REPLY_TYPE_RESPONSE: response
+/**
+ * return value: < 0: not complete message
+ *               REPLY_TYPE_EVENT: event(request)
+ *               REPLY_TYPE_RESPONSE: response
  */
  */
 static int preocess_reply_data(const char *buf, int len,
 static int preocess_reply_data(const char *buf, int len,
         imrt_link_recv_context_t *ctx)
         imrt_link_recv_context_t *ctx)
@@ -696,6 +677,7 @@ static int preocess_reply_data(const char *buf, int len,
             break;
             break;
         }
         }
     }
     }
+
     return -1;
     return -1;
 }
 }
 
 
@@ -717,8 +699,8 @@ parse_event_from_imrtlink(imrt_link_message_t *message, request_t *request)
     return request;
     return request;
 }
 }
 
 
-static void output(const char *header, attr_container_t *payload, int foramt,
-        int payload_len)
+static void output(const char *header, attr_container_t *payload,
+                   int foramt, int payload_len)
 {
 {
     cJSON *json = NULL;
     cJSON *json = NULL;
     char *json_str = NULL;
     char *json_str = NULL;
@@ -780,7 +762,7 @@ int main(int argc, char *argv[])
     if (!parse_args(argc, argv, &op))
     if (!parse_args(argc, argv, &op))
         return -1;
         return -1;
 
 
-    //TODO: reconnect 3 times
+    /* TODO: reconnect 3 times */
     if (init() != 0)
     if (init() != 0)
         return -1;
         return -1;
 
 
@@ -853,8 +835,10 @@ int main(int argc, char *argv[])
                 ret = -1;
                 ret = -1;
                 goto ret;
                 goto ret;
             }
             }
-        } else if (result == 0) { /* select timeout */
-        } else if (result > 0) {
+        }
+        else if (result == 0) { /* select timeout */
+        }
+        else if (result > 0) {
             int n;
             int n;
             if (FD_ISSET(g_conn_fd, &readfds)) {
             if (FD_ISSET(g_conn_fd, &readfds)) {
                 int reply_type = -1;
                 int reply_type = -1;
@@ -886,7 +870,8 @@ int main(int argc, char *argv[])
                         total_elpased_ms = 0;
                         total_elpased_ms = 0;
                         bh_get_elpased_ms(&last_check);
                         bh_get_elpased_ms(&last_check);
                     }
                     }
-                } else if (reply_type == REPLY_TYPE_EVENT) {
+                }
+                else if (reply_type == REPLY_TYPE_EVENT) {
                     request_t event[1] = { 0 };
                     request_t event[1] = { 0 };
 
 
                     parse_event_from_imrtlink(&recv_ctx.message, event);
                     parse_event_from_imrtlink(&recv_ctx.message, event);
@@ -899,10 +884,10 @@ int main(int argc, char *argv[])
         }
         }
     } /* end of while(1) */
     } /* end of while(1) */
 
 
-    ret: if (recv_ctx.message.payload != NULL)
+ret:
+    if (recv_ctx.message.payload != NULL)
         free(recv_ctx.message.payload);
         free(recv_ctx.message.payload);
 
 
     deinit();
     deinit();
-
     return ret;
     return ret;
 }
 }

+ 16 - 11
test-tools/host-tool/src/transport.c

@@ -115,7 +115,6 @@ bool uart_init(const char *device, int baudrate, int *fd)
     }
     }
 
 
     *fd = uart_fd;
     *fd = uart_fd;
-
     return true;
     return true;
 }
 }
 
 
@@ -133,11 +132,10 @@ bool udp_send(const char *address, int port, const char *buf, int len)
     servaddr.sin_port = htons(port);
     servaddr.sin_port = htons(port);
     servaddr.sin_addr.s_addr = INADDR_ANY;
     servaddr.sin_addr.s_addr = INADDR_ANY;
 
 
-    sendto(sockfd, buf, len, MSG_CONFIRM, (const struct sockaddr *) &servaddr,
-        sizeof(servaddr));
+    sendto(sockfd, buf, len, MSG_CONFIRM,
+           (const struct sockaddr *)&servaddr, sizeof(servaddr));
 
 
     close(sockfd);
     close(sockfd);
-
     return true;
     return true;
 }
 }
 
 
@@ -150,7 +148,8 @@ bool host_tool_send_data(int fd, const char *buf, unsigned int len)
         return false;
         return false;
     }
     }
 
 
-    resend: ret = write(fd, buf, len);
+resend:
+    ret = write(fd, buf, len);
 
 
     if (ret == -1) {
     if (ret == -1) {
         if (errno == ECONNRESET) {
         if (errno == ECONNRESET) {
@@ -192,17 +191,21 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
 
 
         if (leading[0] == ch) {
         if (leading[0] == ch) {
             ctx->phase = Phase_Leading;
             ctx->phase = Phase_Leading;
-        } else {
+        }
+        else {
             return -1;
             return -1;
         }
         }
-    } else if (ctx->phase == Phase_Leading) {
+    }
+    else if (ctx->phase == Phase_Leading) {
         if (leading[1] == ch) {
         if (leading[1] == ch) {
             SET_RECV_PHASE(ctx, Phase_Type);
             SET_RECV_PHASE(ctx, Phase_Type);
-        } else {
+        }
+        else {
             ctx->phase = Phase_Non_Start;
             ctx->phase = Phase_Non_Start;
             return -1;
             return -1;
         }
         }
-    } else if (ctx->phase == Phase_Type) {
+    }
+    else if (ctx->phase == Phase_Type) {
         unsigned char *p = (unsigned char *) &ctx->message.message_type;
         unsigned char *p = (unsigned char *) &ctx->message.message_type;
         p[ctx->size_in_phase++] = ch;
         p[ctx->size_in_phase++] = ch;
 
 
@@ -210,7 +213,8 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
             ctx->message.message_type = ntohs(ctx->message.message_type);
             ctx->message.message_type = ntohs(ctx->message.message_type);
             SET_RECV_PHASE(ctx, Phase_Size);
             SET_RECV_PHASE(ctx, Phase_Size);
         }
         }
-    } else if (ctx->phase == Phase_Size) {
+    }
+    else if (ctx->phase == Phase_Size) {
         unsigned char * p = (unsigned char *) &ctx->message.payload_size;
         unsigned char * p = (unsigned char *) &ctx->message.payload_size;
         p[ctx->size_in_phase++] = ch;
         p[ctx->size_in_phase++] = ch;
 
 
@@ -237,7 +241,8 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
             ctx->message.payload = (char *) malloc(ctx->message.payload_size);
             ctx->message.payload = (char *) malloc(ctx->message.payload_size);
             SET_RECV_PHASE(ctx, Phase_Payload);
             SET_RECV_PHASE(ctx, Phase_Payload);
         }
         }
-    } else if (ctx->phase == Phase_Payload) {
+    }
+    else if (ctx->phase == Phase_Payload) {
         ctx->message.payload[ctx->size_in_phase++] = ch;
         ctx->message.payload[ctx->size_in_phase++] = ch;
 
 
         if (ctx->size_in_phase == ctx->message.payload_size) {
         if (ctx->size_in_phase == ctx->message.payload_size) {