Refine binary size and fix several minor issues (#104)
* 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
This commit is contained in:
@ -293,10 +293,15 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
|
||||
break;
|
||||
case VALUE_TYPE_I64:
|
||||
{
|
||||
char buf[16];
|
||||
union { uint64 val; uint32 parts[2]; } u;
|
||||
u.parts[0] = argv1[0];
|
||||
u.parts[1] = argv1[1];
|
||||
wasm_printf("0x%llx:i64", u.val);
|
||||
if (sizeof(long) == 4)
|
||||
snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
|
||||
else
|
||||
snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
|
||||
wasm_printf(buf, u.val);
|
||||
break;
|
||||
}
|
||||
case VALUE_TYPE_F32:
|
||||
|
||||
@ -753,7 +753,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
|
||||
uint32 i, depth, cond, count, fidx, tidx, frame_size = 0, all_cell_num = 0;
|
||||
int32 didx, val;
|
||||
uint8 *else_addr, *end_addr;
|
||||
uint8 *maddr;
|
||||
uint8 *maddr = NULL;
|
||||
|
||||
#if WASM_ENABLE_LABELS_AS_VALUES != 0
|
||||
#define HANDLE_OPCODE(op) &&HANDLE_##op
|
||||
@ -1148,70 +1148,98 @@ wasm_interp_call_func_bytecode(WASMThread *self,
|
||||
|
||||
/* memory load instructions */
|
||||
HANDLE_OP (WASM_OP_I32_LOAD):
|
||||
DEF_OP_LOAD(PUSH_I32(*(int32*)maddr));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_LOAD):
|
||||
DEF_OP_LOAD(PUSH_I64(GET_I64_FROM_ADDR((uint32*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_F32_LOAD):
|
||||
DEF_OP_LOAD(PUSH_F32(*(float32*)maddr));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_F64_LOAD):
|
||||
DEF_OP_LOAD(PUSH_F64(GET_F64_FROM_ADDR((uint32*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I32_LOAD8_S):
|
||||
DEF_OP_LOAD(PUSH_I32(sign_ext_8_32(*(int8*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I32_LOAD8_U):
|
||||
DEF_OP_LOAD(PUSH_I32((uint32)(*(uint8*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I32_LOAD16_S):
|
||||
DEF_OP_LOAD(PUSH_I32(sign_ext_16_32(*(int16*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I32_LOAD16_U):
|
||||
DEF_OP_LOAD(PUSH_I32((uint32)(*(uint16*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_LOAD8_S):
|
||||
DEF_OP_LOAD(PUSH_I64(sign_ext_8_64(*(int8*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_LOAD8_U):
|
||||
DEF_OP_LOAD(PUSH_I64((uint64)(*(uint8*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_LOAD16_S):
|
||||
DEF_OP_LOAD(PUSH_I64(sign_ext_16_64(*(int16*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_LOAD16_U):
|
||||
DEF_OP_LOAD(PUSH_I64((uint64)(*(uint16*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_LOAD32_S):
|
||||
DEF_OP_LOAD(PUSH_I64(sign_ext_32_64(*(int32*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_LOAD32_U):
|
||||
DEF_OP_LOAD(PUSH_I64((uint64)(*(uint32*)maddr)));
|
||||
HANDLE_OP_END ();
|
||||
{
|
||||
uint32 offset, flags, addr;
|
||||
GET_OPCODE();
|
||||
read_leb_uint32(frame_ip, frame_ip_end, flags);
|
||||
read_leb_uint32(frame_ip, frame_ip_end, offset);
|
||||
addr = POP_I32();
|
||||
CHECK_MEMORY_OVERFLOW();
|
||||
#if WASM_ENABLE_LABELS_AS_VALUES != 0
|
||||
static const void *handle_load_table[] = {
|
||||
&&HANDLE_LOAD_WASM_OP_I32_LOAD,
|
||||
&&HANDLE_LOAD_WASM_OP_I64_LOAD,
|
||||
&&HANDLE_LOAD_WASM_OP_F32_LOAD,
|
||||
&&HANDLE_LOAD_WASM_OP_F64_LOAD,
|
||||
&&HANDLE_LOAD_WASM_OP_I32_LOAD8_S,
|
||||
&&HANDLE_LOAD_WASM_OP_I32_LOAD8_U,
|
||||
&&HANDLE_LOAD_WASM_OP_I32_LOAD16_S,
|
||||
&&HANDLE_LOAD_WASM_OP_I32_LOAD16_U,
|
||||
&&HANDLE_LOAD_WASM_OP_I64_LOAD8_S,
|
||||
&&HANDLE_LOAD_WASM_OP_I64_LOAD8_U,
|
||||
&&HANDLE_LOAD_WASM_OP_I64_LOAD16_S,
|
||||
&&HANDLE_LOAD_WASM_OP_I64_LOAD16_U,
|
||||
&&HANDLE_LOAD_WASM_OP_I64_LOAD32_S,
|
||||
&&HANDLE_LOAD_WASM_OP_I64_LOAD32_U
|
||||
};
|
||||
#define HANDLE_OP_LOAD(opcode) HANDLE_LOAD_##opcode
|
||||
goto *handle_load_table[opcode - WASM_OP_I32_LOAD];
|
||||
#else
|
||||
#define HANDLE_OP_LOAD(opcode) case opcode
|
||||
switch (opcode)
|
||||
#endif
|
||||
{
|
||||
HANDLE_OP_LOAD(WASM_OP_I32_LOAD):
|
||||
PUSH_I32(*(int32*)maddr);
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I64_LOAD):
|
||||
PUSH_I64(GET_I64_FROM_ADDR((uint32*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_F32_LOAD):
|
||||
PUSH_F32(*(float32*)maddr);
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_F64_LOAD):
|
||||
PUSH_F64(GET_F64_FROM_ADDR((uint32*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_S):
|
||||
PUSH_I32(sign_ext_8_32(*(int8*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_U):
|
||||
PUSH_I32((uint32)(*(uint8*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_S):
|
||||
PUSH_I32(sign_ext_16_32(*(int16*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_U):
|
||||
PUSH_I32((uint32)(*(uint16*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_S):
|
||||
PUSH_I64(sign_ext_8_64(*(int8*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_U):
|
||||
PUSH_I64((uint64)(*(uint8*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_S):
|
||||
PUSH_I64(sign_ext_16_64(*(int16*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_U):
|
||||
PUSH_I64((uint64)(*(uint16*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_S):
|
||||
PUSH_I64(sign_ext_32_64(*(int32*)maddr));
|
||||
HANDLE_OP_END();
|
||||
HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_U):
|
||||
PUSH_I64((uint64)(*(uint32*)maddr));
|
||||
HANDLE_OP_END();
|
||||
}
|
||||
(void)flags;
|
||||
HANDLE_OP_END ();
|
||||
}
|
||||
|
||||
/* memory store instructions */
|
||||
HANDLE_OP (WASM_OP_I32_STORE):
|
||||
DEF_OP_STORE(uint32, I32, *(int32*)maddr = sval);
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_STORE):
|
||||
DEF_OP_STORE(uint64, I64, PUT_I64_TO_ADDR((uint32*)maddr, sval));
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_F32_STORE):
|
||||
{
|
||||
uint32 offset, flags, addr;
|
||||
@ -1241,25 +1269,63 @@ wasm_interp_call_func_bytecode(WASMThread *self,
|
||||
HANDLE_OP_END ();
|
||||
}
|
||||
|
||||
HANDLE_OP (WASM_OP_I32_STORE):
|
||||
HANDLE_OP (WASM_OP_I32_STORE8):
|
||||
DEF_OP_STORE(uint32, I32, *(uint8*)maddr = (uint8)sval);
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I32_STORE16):
|
||||
DEF_OP_STORE(uint32, I32, *(uint16*)maddr = (uint16)sval);
|
||||
HANDLE_OP_END ();
|
||||
{
|
||||
uint32 offset, flags, addr;
|
||||
uint32 sval;
|
||||
GET_OPCODE();
|
||||
read_leb_uint32(frame_ip, frame_ip_end, flags);
|
||||
read_leb_uint32(frame_ip, frame_ip_end, offset);
|
||||
sval = POP_I32();
|
||||
addr = POP_I32();
|
||||
CHECK_MEMORY_OVERFLOW();
|
||||
switch (opcode) {
|
||||
case WASM_OP_I32_STORE:
|
||||
*(int32*)maddr = sval;
|
||||
break;
|
||||
case WASM_OP_I32_STORE8:
|
||||
*(uint8*)maddr = (uint8)sval;
|
||||
break;
|
||||
case WASM_OP_I32_STORE16:
|
||||
*(uint16*)maddr = (uint16)sval;
|
||||
break;
|
||||
}
|
||||
(void)flags;
|
||||
HANDLE_OP_END ();
|
||||
}
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_STORE):
|
||||
HANDLE_OP (WASM_OP_I64_STORE8):
|
||||
DEF_OP_STORE(uint64, I64, *(uint8*)maddr = (uint8)sval);
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_STORE16):
|
||||
DEF_OP_STORE(uint64, I64, *(uint16*)maddr = (uint16)sval);
|
||||
HANDLE_OP_END ();
|
||||
|
||||
HANDLE_OP (WASM_OP_I64_STORE32):
|
||||
DEF_OP_STORE(uint64, I64, *(uint32*)maddr = (uint32)sval);
|
||||
HANDLE_OP_END ();
|
||||
{
|
||||
uint32 offset, flags, addr;
|
||||
uint64 sval;
|
||||
GET_OPCODE();
|
||||
read_leb_uint32(frame_ip, frame_ip_end, flags);
|
||||
read_leb_uint32(frame_ip, frame_ip_end, offset);
|
||||
sval = POP_I64();
|
||||
addr = POP_I32();
|
||||
CHECK_MEMORY_OVERFLOW();
|
||||
switch (opcode) {
|
||||
case WASM_OP_I64_STORE:
|
||||
PUT_I64_TO_ADDR((uint32*)maddr, sval);
|
||||
break;
|
||||
case WASM_OP_I64_STORE8:
|
||||
*(uint8*)maddr = (uint8)sval;
|
||||
break;
|
||||
case WASM_OP_I64_STORE16:
|
||||
*(uint16*)maddr = (uint16)sval;
|
||||
break;
|
||||
case WASM_OP_I64_STORE32:
|
||||
*(uint32*)maddr = (uint32)sval;
|
||||
break;
|
||||
}
|
||||
(void)flags;
|
||||
HANDLE_OP_END ();
|
||||
}
|
||||
|
||||
/* memory size and memory grow instructions */
|
||||
HANDLE_OP (WASM_OP_MEMORY_SIZE):
|
||||
|
||||
@ -947,18 +947,11 @@ wasm_runtime_instantiate(WASMModule *module,
|
||||
wasm_runtime_set_tlr(&module_inst->main_tlr);
|
||||
module_inst->main_tlr.handle = ws_self_thread();
|
||||
|
||||
/* Execute __post_instantiate function */
|
||||
if (!execute_post_inst_function(module_inst)) {
|
||||
const char *exception = wasm_runtime_get_exception(module_inst);
|
||||
wasm_printf("%s\n", exception);
|
||||
wasm_runtime_deinstantiate(module_inst);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Execute start function */
|
||||
if (!execute_start_function(module_inst)) {
|
||||
const char *exception = wasm_runtime_get_exception(module_inst);
|
||||
wasm_printf("%s\n", exception);
|
||||
/* Execute __post_instantiate and start function */
|
||||
if (!execute_post_inst_function(module_inst)
|
||||
|| !execute_start_function(module_inst)) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
module_inst->cur_exception);
|
||||
wasm_runtime_deinstantiate(module_inst);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user