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

@ -13,11 +13,19 @@
static host_interface host_commu;
/* 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 */
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;
/* IMRTLink Receive Context */
@ -74,7 +82,8 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
}
return 0;
} else if (ctx->phase == Phase_Leading) {
}
else if (ctx->phase == Phase_Leading) {
if (ch == leadings[1]) {
if (enable_log)
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;
return 0;
} else if (ctx->phase == Phase_Type) {
}
else if (ctx->phase == Phase_Type) {
if (ctx->size_in_phase++ == 0) {
if (enable_log)
app_manager_printf("##On byte arrive: got type 0\n");
ctx->message.message_type = ch;
} else {
}
else {
if (enable_log)
app_manager_printf("##On byte arrive: got type 1\n");
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;
} else if (ctx->phase == Phase_Size) {
}
else if (ctx->phase == Phase_Size) {
unsigned char *p = (unsigned char *) &ctx->message.payload_size;
if (enable_log)
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;
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)
app_manager_printf("##On byte arrive: payload_size: %d\n",
ctx->message.payload_size);
ctx->message.payload_size);
if (ctx->message.payload) {
APP_MGR_FREE(ctx->message.payload);
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) {
ctx->phase = Phase_Non_Start;
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;
}
if (ctx->message.payload_size > 1024 * 1024) {
ctx->phase = Phase_Non_Start;
return 0;
}
if (ctx->message.message_type != INSTALL_WASM_APP) {
ctx->message.payload =
(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;
} else if (ctx->phase == Phase_Payload) {
}
else if (ctx->phase == Phase_Payload) {
if (ctx->message.message_type == INSTALL_WASM_APP) {
int received_size;
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;
return 1;
}
} else {
}
else {
/* 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;
} else {
}
else {
ctx->phase = Phase_Non_Start;
ctx->size_in_phase = 0;
return 0;
}
} else {
}
else {
ctx->message.payload[ctx->size_in_phase++] = ch;
if (ctx->size_in_phase == ctx->message.payload_size) {
ctx->phase = Phase_Non_Start;
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);
return 1;
}
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;
}
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;
@ -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);
/* 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 */
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);
return n;
} else {
}
else {
app_manager_printf("no send api provided\n");
}
return 0;