Update wasm app build scripts for wasi-sdk-12 and refine interpreter (#481)

Update wasm app build scripts for wasi-sdk-12.0: add --export=__main_argc_argv, remove --no-threads
Lookup function with name "__main_argc_argv" as main function besides "main"
Change module_malloc to runtime_malloc in wasi native lib
Refine classic interpreter op_block and op_br_table
Refine faster interpreter op_br_table

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang 2020-12-27 20:46:31 -06:00 committed by GitHub
parent 13f0b2485b
commit 724858c731
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 144 additions and 192 deletions

View File

@ -2039,12 +2039,9 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
} }
#endif /* end of WASM_ENABLE_LIBC_WASI */ #endif /* end of WASM_ENABLE_LIBC_WASI */
func = resolve_function(module_inst, "_main"); if (!(func = resolve_function(module_inst, "main"))
if (!func) { && !(func = resolve_function(module_inst, "__main_argc_argv"))
func = resolve_function(module_inst, "main"); && !(func = resolve_function(module_inst, "_main"))) {
}
if (!func) {
wasm_runtime_set_exception(module_inst, wasm_runtime_set_exception(module_inst,
"lookup main function failed"); "lookup main function failed");
return false; return false;

View File

@ -383,7 +383,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
/* Get block info */ /* Get block info */
if (!(wasm_loader_find_block_addr((BlockAddr*)block_addr_cache, if (!(wasm_loader_find_block_addr((BlockAddr*)block_addr_cache,
*p_frame_ip, frame_ip_end, (uint8)label_type, *p_frame_ip, frame_ip_end, (uint8)label_type,
&else_addr, &end_addr, NULL, 0))) { &else_addr, &end_addr))) {
aot_set_last_error("find block end addr failed."); aot_set_last_error("find block end addr failed.");
return false; return false;
} }

View File

@ -405,10 +405,10 @@ typedef struct BlockType {
} BlockType; } BlockType;
typedef struct WASMBranchBlock { typedef struct WASMBranchBlock {
uint8 label_type; uint8 *begin_addr;
uint32 cell_num;
uint8 *target_addr; uint8 *target_addr;
uint32 *frame_sp; uint32 *frame_sp;
uint32 cell_num;
} WASMBranchBlock; } WASMBranchBlock;
/* Execution environment, e.g. stack info */ /* Execution environment, e.g. stack info */

View File

@ -381,28 +381,28 @@ popcount64(uint64 u)
static uint64 static uint64
read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign) read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
{ {
uint64 result = 0; uint64 result = 0, byte;
uint32 offset = *p_offset;
uint32 shift = 0; uint32 shift = 0;
uint32 bcnt = 0;
uint64 byte;
while (true) { while (true) {
byte = buf[*p_offset]; byte = buf[offset++];
*p_offset += 1;
result |= ((byte & 0x7f) << shift); result |= ((byte & 0x7f) << shift);
shift += 7; shift += 7;
if ((byte & 0x80) == 0) { if ((byte & 0x80) == 0) {
break; break;
} }
bcnt += 1;
} }
if (sign && (shift < maxbits) && (byte & 0x40)) { if (sign && (shift < maxbits) && (byte & 0x40)) {
/* Sign extend */ /* Sign extend */
result |= - ((uint64)1 << shift); result |= - ((uint64)1 << shift);
} }
*p_offset = offset;
return result; return result;
} }
#define skip_leb(p) while (*p++ & 0x80)
#define PUSH_I32(value) do { \ #define PUSH_I32(value) do { \
*(int32*)frame_sp++ = (int32)(value); \ *(int32*)frame_sp++ = (int32)(value); \
} while (0) } while (0)
@ -423,8 +423,9 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
#define PUSH_CSP(_label_type, cell_num, _target_addr) do { \ #define PUSH_CSP(_label_type, cell_num, _target_addr) do { \
bh_assert(frame_csp < frame->csp_boundary); \ bh_assert(frame_csp < frame->csp_boundary); \
frame_csp->label_type = _label_type; \ /* frame_csp->label_type = _label_type; */ \
frame_csp->cell_num = cell_num; \ frame_csp->cell_num = cell_num; \
frame_csp->begin_addr = frame_ip; \
frame_csp->target_addr = _target_addr; \ frame_csp->target_addr = _target_addr; \
frame_csp->frame_sp = frame_sp; \ frame_csp->frame_sp = frame_sp; \
frame_csp++; \ frame_csp++; \
@ -1078,11 +1079,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
BlockAddr *cache_items; BlockAddr *cache_items;
uint8 *frame_ip_end = frame_ip + 1; uint8 *frame_ip_end = frame_ip + 1;
uint8 opcode; uint8 opcode;
uint32 *depths = NULL; uint32 i, depth, cond, count, fidx, tidx, lidx, frame_size = 0;
uint32 depth_buf[BR_TABLE_TMP_BUF_LEN];
uint32 i, depth, cond, count, fidx, tidx, frame_size = 0;
uint64 all_cell_num = 0; uint64 all_cell_num = 0;
int32 didx, val; int32 val;
uint8 *else_addr, *end_addr, *maddr = NULL; uint8 *else_addr, *end_addr, *maddr = NULL;
uint32 local_idx, local_offset, global_idx; uint32 local_idx, local_offset, global_idx;
uint8 local_type, *global_addr; uint8 local_type, *global_addr;
@ -1127,15 +1126,9 @@ handle_op_block:
else if (cache_items[1].start_addr == frame_ip) { else if (cache_items[1].start_addr == frame_ip) {
end_addr = cache_items[1].end_addr; end_addr = cache_items[1].end_addr;
} }
else if (!wasm_loader_find_block_addr((BlockAddr*)exec_env->block_addr_cache, else {
frame_ip, (uint8*)-1, end_addr = NULL;
LABEL_TYPE_BLOCK,
&else_addr, &end_addr,
NULL, 0)) {
wasm_set_exception(module, "find block address failed");
goto got_exception;
} }
PUSH_CSP(LABEL_TYPE_BLOCK, cell_num, end_addr); PUSH_CSP(LABEL_TYPE_BLOCK, cell_num, end_addr);
HANDLE_OP_END (); HANDLE_OP_END ();
@ -1173,26 +1166,26 @@ handle_op_if:
else if (!wasm_loader_find_block_addr((BlockAddr*)exec_env->block_addr_cache, else if (!wasm_loader_find_block_addr((BlockAddr*)exec_env->block_addr_cache,
frame_ip, (uint8*)-1, frame_ip, (uint8*)-1,
LABEL_TYPE_IF, LABEL_TYPE_IF,
&else_addr, &end_addr, &else_addr, &end_addr)) {
NULL, 0)) {
wasm_set_exception(module, "find block address failed"); wasm_set_exception(module, "find block address failed");
goto got_exception; goto got_exception;
} }
cond = (uint32)POP_I32(); cond = (uint32)POP_I32();
PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr); if (cond) { /* if branch is met */
PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr);
/* condition of the if branch is false, else condition is met */ }
if (cond == 0) { else { /* if branch is not met */
/* if there is no else branch, go to the end addr */ /* if there is no else branch, go to the end addr */
if (else_addr == NULL) { if (else_addr == NULL) {
POP_CSP();
frame_ip = end_addr + 1; frame_ip = end_addr + 1;
} }
/* if there is an else branch, go to the else addr */ /* if there is an else branch, go to the else addr */
else else {
PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr);
frame_ip = else_addr + 1; frame_ip = else_addr + 1;
}
} }
HANDLE_OP_END (); HANDLE_OP_END ();
@ -1221,6 +1214,16 @@ handle_op_if:
read_leb_uint32(frame_ip, frame_ip_end, depth); read_leb_uint32(frame_ip, frame_ip_end, depth);
label_pop_csp_n: label_pop_csp_n:
POP_CSP_N(depth); POP_CSP_N(depth);
if (!frame_ip) { /* must be label pushed by WASM_OP_BLOCK */
if (!wasm_loader_find_block_addr((BlockAddr*)exec_env->block_addr_cache,
(frame_csp - 1)->begin_addr, (uint8*)-1,
LABEL_TYPE_BLOCK,
&else_addr, &end_addr)) {
wasm_set_exception(module, "find block address failed");
goto got_exception;
}
frame_ip = end_addr;
}
HANDLE_OP_END (); HANDLE_OP_END ();
HANDLE_OP (WASM_OP_BR_IF): HANDLE_OP (WASM_OP_BR_IF):
@ -1238,30 +1241,13 @@ label_pop_csp_n:
CHECK_SUSPEND_FLAGS(); CHECK_SUSPEND_FLAGS();
#endif #endif
read_leb_uint32(frame_ip, frame_ip_end, count); read_leb_uint32(frame_ip, frame_ip_end, count);
if (count <= BR_TABLE_TMP_BUF_LEN) lidx = POP_I32();
depths = depth_buf; if (lidx > count)
else { lidx = count;
uint64 total_size = sizeof(uint32) * (uint64)count; for (i = 0; i < lidx; i++)
if (total_size >= UINT32_MAX skip_leb(frame_ip);
|| !(depths = wasm_runtime_malloc((uint32)total_size))) {
wasm_set_exception(module, "allocate memory failed");
goto got_exception;
}
}
for (i = 0; i < count; i++) {
read_leb_uint32(frame_ip, frame_ip_end, depths[i]);
}
read_leb_uint32(frame_ip, frame_ip_end, depth); read_leb_uint32(frame_ip, frame_ip_end, depth);
didx = POP_I32();
if (didx >= 0 && (uint32)didx < count) {
depth = depths[didx];
}
if (depths != depth_buf) {
wasm_runtime_free(depths);
depths = NULL;
}
goto label_pop_csp_n; goto label_pop_csp_n;
HANDLE_OP_END ();
HANDLE_OP (WASM_OP_RETURN): HANDLE_OP (WASM_OP_RETURN):
frame_sp -= cur_func->ret_cell_num; frame_sp -= cur_func->ret_cell_num;

View File

@ -1249,20 +1249,36 @@ recover_br_info:
HANDLE_OP_END (); HANDLE_OP_END ();
HANDLE_OP (WASM_OP_BR_TABLE): HANDLE_OP (WASM_OP_BR_TABLE):
#if WASM_ENABLE_THREAD_MGR != 0 {
CHECK_SUSPEND_FLAGS(); uint32 arity, br_item_size;
#endif
count = read_uint32(frame_ip);
didx = GET_OPERAND(uint32, 0);
frame_ip += 2;
if (!(didx >= 0 && (uint32)didx < count)) #if WASM_ENABLE_THREAD_MGR != 0
CHECK_SUSPEND_FLAGS();
#endif
count = read_uint32(frame_ip);
didx = GET_OPERAND(uint32, 0);
frame_ip += 2;
if (!(didx >= 0 && (uint32)didx < count))
didx = count; didx = count;
while (didx--) /* all br items must have the same arity and item size,
SKIP_BR_INFO(); so we only calculate the first item size */
arity = *(uint32*)frame_ip;
br_item_size = sizeof(uint32); /* arity */
if (arity) {
/* total cell num */
br_item_size += sizeof(uint32);
/* cells, src offsets and dst offsets */
br_item_size += (sizeof(uint8) + sizeof(int16) + sizeof(uint16))
* arity;
}
/* target address */
br_item_size += sizeof(uint8*);
goto recover_br_info; frame_ip += br_item_size * didx;
goto recover_br_info;
}
HANDLE_OP (WASM_OP_RETURN): HANDLE_OP (WASM_OP_RETURN):
{ {

View File

@ -79,53 +79,10 @@ check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
} \ } \
} while (0) } while (0)
static bool #define skip_leb(p) while (*p++ & 0x80)
skip_leb(const uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, #define skip_leb_int64(p, p_end) skip_leb(p)
char* error_buf, uint32 error_buf_size) #define skip_leb_uint32(p, p_end) skip_leb(p)
{ #define skip_leb_int32(p, p_end) skip_leb(p)
const uint8 *buf = *p_buf;
uint32 offset = 0, bcnt = 0;
uint64 byte;
while (true) {
if (bcnt + 1 > (maxbits + 6) / 7) {
set_error_buf(error_buf, error_buf_size,
"integer representation too long");
return false;
}
CHECK_BUF(buf, buf_end, offset + 1);
byte = buf[offset];
offset += 1;
bcnt += 1;
if ((byte & 0x80) == 0) {
break;
}
}
*p_buf += offset;
return true;
fail:
return false;
}
#define skip_leb_int64(p, p_end) do { \
if (!skip_leb(&p, p_end, 64, \
error_buf, error_buf_size)) \
return false; \
} while (0)
#define skip_leb_uint32(p, p_end) do { \
if (!skip_leb(&p, p_end, 32, \
error_buf, error_buf_size)) \
return false; \
} while (0)
#define skip_leb_int32(p, p_end) do { \
if (!skip_leb(&p, p_end, 32, \
error_buf, error_buf_size)) \
return false; \
} while (0)
static bool static bool
read_leb(uint8 **p_buf, const uint8 *buf_end, read_leb(uint8 **p_buf, const uint8 *buf_end,
@ -1134,13 +1091,15 @@ load_global_import(const WASMModule *parent_module,
} }
if (wasm_runtime_is_host_module(sub_module_name)) { if (wasm_runtime_is_host_module(sub_module_name)) {
/* do nothing, let host injects the symbol */ /* do nothing, let host inject the symbol */
} }
#if WASM_ENABLE_LIBC_BUILTIN != 0
else if (wasm_runtime_is_built_in_module(sub_module_name)) { else if (wasm_runtime_is_built_in_module(sub_module_name)) {
/* check built-in modules */ /* check built-in modules */
global->is_linked = wasm_native_lookup_libc_builtin_global( global->is_linked = wasm_native_lookup_libc_builtin_global(
sub_module_name, global_name, global); sub_module_name, global_name, global);
} }
#endif
#if WASM_ENABLE_MULTI_MODULE != 0 #if WASM_ENABLE_MULTI_MODULE != 0
else { else {
/* check sub modules */ /* check sub modules */
@ -3267,17 +3226,17 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
const uint8 *code_end_addr, const uint8 *code_end_addr,
uint8 label_type, uint8 label_type,
uint8 **p_else_addr, uint8 **p_else_addr,
uint8 **p_end_addr, uint8 **p_end_addr)
char *error_buf,
uint32 error_buf_size)
{ {
const uint8 *p = start_addr, *p_end = code_end_addr; const uint8 *p = start_addr, *p_end = code_end_addr;
uint8 *else_addr = NULL; uint8 *else_addr = NULL;
char error_buf[128];
uint32 block_nested_depth = 1, count, i, j, t; uint32 block_nested_depth = 1, count, i, j, t;
uint32 error_buf_size = sizeof(error_buf);
uint8 opcode, u8; uint8 opcode, u8;
BlockAddr block_stack[16] = { 0 }, *block; BlockAddr block_stack[16] = { 0 }, *block;
i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE; i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) { for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
@ -3303,7 +3262,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
case WASM_OP_BLOCK: case WASM_OP_BLOCK:
case WASM_OP_LOOP: case WASM_OP_LOOP:
case WASM_OP_IF: case WASM_OP_IF:
CHECK_BUF(p, p_end, 1);
/* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */ /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
u8 = read_uint8(p); u8 = read_uint8(p);
if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) { if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
@ -3342,7 +3300,8 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) { for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
start_addr = block_stack[t].start_addr; start_addr = block_stack[t].start_addr;
if (start_addr) { if (start_addr) {
i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE; i = ((uintptr_t)start_addr)
& (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
if (!block[j].start_addr) if (!block[j].start_addr)
@ -3633,9 +3592,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
break; break;
#endif #endif
default: default:
set_error_buf_v(error_buf, error_buf_size,
"%s %02x %02x",
"unsupported opcode", 0xfc, opcode);
return false; return false;
} }
break; break;
@ -3704,10 +3660,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
default: default:
LOG_WARNING("WASM loader find block addr failed: " LOG_WARNING("WASM loader find block addr failed: "
"invalid opcode fd 0x%02x.", opcode); "invalid opcode fd 0x%02x.", opcode);
if (error_buf)
snprintf(error_buf, error_buf_size,
"WASM loader find block addr failed: "
"invalid opcode fd %02x.", opcode);
return false; return false;
} }
break; break;
@ -3733,9 +3685,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
#endif #endif
default: default:
set_error_buf_v(error_buf, error_buf_size,
"%s %02x",
"unsupported opcode", opcode);
return false; return false;
} }
} }
@ -6126,7 +6075,6 @@ handle_op_block_and_loop:
#endif #endif
POP_I32(); POP_I32();
/* TODO: check the const */
for (i = 0; i <= count; i++) { for (i = 0; i <= count; i++) {
if (!(frame_csp_tmp = if (!(frame_csp_tmp =
check_branch_block(loader_ctx, &p, p_end, check_branch_block(loader_ctx, &p, p_end,
@ -6136,8 +6084,8 @@ handle_op_block_and_loop:
if (i == 0) { if (i == 0) {
if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP) if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
ret_count = ret_count =
block_type_get_result_types(&frame_csp_tmp->block_type, block_type_get_result_types(&frame_csp_tmp->block_type,
&ret_types); &ret_types);
} }
else { else {
uint8 *tmp_ret_types = NULL; uint8 *tmp_ret_types = NULL;
@ -6146,8 +6094,8 @@ handle_op_block_and_loop:
/* Check whether all table items have the same return type */ /* Check whether all table items have the same return type */
if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP) if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
tmp_ret_count = tmp_ret_count =
block_type_get_result_types(&frame_csp_tmp->block_type, block_type_get_result_types(&frame_csp_tmp->block_type,
&tmp_ret_types); &tmp_ret_types);
if (ret_count != tmp_ret_count if (ret_count != tmp_ret_count
|| (ret_count || (ret_count

View File

@ -68,9 +68,7 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
const uint8 *code_end_addr, const uint8 *code_end_addr,
uint8 block_type, uint8 block_type,
uint8 **p_else_addr, uint8 **p_else_addr,
uint8 **p_end_addr, uint8 **p_end_addr);
char *error_buf,
uint32 error_buf_size);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -340,7 +340,6 @@ wasi_fd_pread(wasm_exec_env_t exec_env,
wasi_iovec_t *iovec, *iovec_begin; wasi_iovec_t *iovec, *iovec_begin;
uint64 total_size; uint64 total_size;
size_t nread; size_t nread;
uint32 mem;
uint32 i; uint32 i;
wasi_errno_t err; wasi_errno_t err;
@ -355,7 +354,7 @@ wasi_fd_pread(wasm_exec_env_t exec_env,
total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len; total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(mem = module_malloc((uint32)total_size, (void**)&iovec_begin))) || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1; return (wasi_errno_t)-1;
iovec = iovec_begin; iovec = iovec_begin;
@ -380,7 +379,7 @@ wasi_fd_pread(wasm_exec_env_t exec_env,
err = 0; err = 0;
fail: fail:
module_free(mem); wasm_runtime_free(iovec_begin);
return err; return err;
} }
@ -395,7 +394,6 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env,
wasi_ciovec_t *ciovec, *ciovec_begin; wasi_ciovec_t *ciovec, *ciovec_begin;
uint64 total_size; uint64 total_size;
size_t nwritten; size_t nwritten;
uint32 mem;
uint32 i; uint32 i;
wasi_errno_t err; wasi_errno_t err;
@ -410,7 +408,7 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env,
total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len; total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(mem = module_malloc((uint32)total_size, (void**)&ciovec_begin))) || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1; return (wasi_errno_t)-1;
ciovec = ciovec_begin; ciovec = ciovec_begin;
@ -435,7 +433,7 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env,
err = 0; err = 0;
fail: fail:
module_free(mem); wasm_runtime_free(ciovec_begin);
return err; return err;
} }
@ -451,7 +449,6 @@ wasi_fd_read(wasm_exec_env_t exec_env,
uint64 total_size; uint64 total_size;
size_t nread; size_t nread;
uint32 i; uint32 i;
uint32 mem;
wasi_errno_t err; wasi_errno_t err;
if (!wasi_ctx) if (!wasi_ctx)
@ -465,7 +462,7 @@ wasi_fd_read(wasm_exec_env_t exec_env,
total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len; total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(mem = module_malloc((uint32)total_size, (void**)&iovec_begin))) || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1; return (wasi_errno_t)-1;
iovec = iovec_begin; iovec = iovec_begin;
@ -490,7 +487,7 @@ wasi_fd_read(wasm_exec_env_t exec_env,
err = 0; err = 0;
fail: fail:
module_free(mem); wasm_runtime_free(iovec_begin);
return err; return err;
} }
@ -622,7 +619,6 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_ciovec_t *ciovec, *ciovec_begin; wasi_ciovec_t *ciovec, *ciovec_begin;
uint64 total_size; uint64 total_size;
size_t nwritten; size_t nwritten;
uint32 mem;
uint32 i; uint32 i;
wasi_errno_t err; wasi_errno_t err;
@ -637,7 +633,7 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len; total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(mem = module_malloc((uint32)total_size, (void**)&ciovec_begin))) || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1; return (wasi_errno_t)-1;
ciovec = ciovec_begin; ciovec = ciovec_begin;
@ -662,7 +658,7 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
err = 0; err = 0;
fail: fail:
module_free(mem); wasm_runtime_free(ciovec_begin);
return err; return err;
} }
@ -1052,7 +1048,6 @@ wasi_sock_recv(wasm_exec_env_t exec_env,
wasi_iovec_t *iovec, *iovec_begin; wasi_iovec_t *iovec, *iovec_begin;
uint64 total_size; uint64 total_size;
size_t ro_datalen; size_t ro_datalen;
uint32 mem;
uint32 i; uint32 i;
wasi_errno_t err; wasi_errno_t err;
@ -1068,7 +1063,7 @@ wasi_sock_recv(wasm_exec_env_t exec_env,
total_size = sizeof(wasi_iovec_t) * (uint64)ri_data_len; total_size = sizeof(wasi_iovec_t) * (uint64)ri_data_len;
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(mem = module_malloc((uint32)total_size, (void**)&iovec_begin))) || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1; return (wasi_errno_t)-1;
iovec = iovec_begin; iovec = iovec_begin;
@ -1095,7 +1090,7 @@ wasi_sock_recv(wasm_exec_env_t exec_env,
err = 0; err = 0;
fail: fail:
module_free(mem); wasm_runtime_free(iovec_begin);
return err; return err;
} }
@ -1112,7 +1107,6 @@ wasi_sock_send(wasm_exec_env_t exec_env,
wasi_ciovec_t *ciovec, *ciovec_begin; wasi_ciovec_t *ciovec, *ciovec_begin;
uint64 total_size; uint64 total_size;
size_t so_datalen; size_t so_datalen;
uint32 mem;
uint32 i; uint32 i;
wasi_errno_t err; wasi_errno_t err;
@ -1127,7 +1121,7 @@ wasi_sock_send(wasm_exec_env_t exec_env,
total_size = sizeof(wasi_ciovec_t) * (uint64)si_data_len; total_size = sizeof(wasi_ciovec_t) * (uint64)si_data_len;
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(mem = module_malloc((uint32)total_size, (void**)&ciovec_begin))) || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1; return (wasi_errno_t)-1;
ciovec = ciovec_begin; ciovec = ciovec_begin;
@ -1153,7 +1147,7 @@ wasi_sock_send(wasm_exec_env_t exec_env,
err = 0; err = 0;
fail: fail:
module_free(mem); wasm_runtime_free(ciovec_begin);
return err; return err;
} }

View File

@ -64,18 +64,19 @@ There are some useful options which can be specified to build the source code:
- **-Wl,--shared-memory** Use shared linear memory - **-Wl,--shared-memory** Use shared linear memory
- **-Wl,--threads** or **-Wl,--no-threads** Run or do not run the linker multi-threaded
- **-Wl,--allow-undefined** Allow undefined symbols in linked binary - **-Wl,--allow-undefined** Allow undefined symbols in linked binary
- **-Wl,--allow-undefined-file=<value>** Allow symbols listed in <file> to be undefined in linked binary - **-Wl,--allow-undefined-file=<value>** Allow symbols listed in <file> to be undefined in linked binary
- **-pthread** Support POSIX threads in generated code
For example, we can build the wasm app with command: For example, we can build the wasm app with command:
``` Bash ``` Bash
/opt/wasi-sdk/bin/clang -O3 -nostdlib \ /opt/wasi-sdk/bin/clang -O3 -nostdlib \
-z stack-size=8192 -Wl,--initial-memory=65536 \ -z stack-size=8192 -Wl,--initial-memory=65536 \
-Wl,--export=main -o test.wasm test.c \ -o test.wasm test.c \
-Wl,--export=__heap_base,--export=__data_end \ -Wl,--export=main -Wl,--export=__main_argc_argv \
-Wl,--export=__heap_base -Wl,--export=__data_end \
-Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined -Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined
``` ```
to generate a wasm binary with small footprint. to generate a wasm binary with small footprint.

View File

@ -3,13 +3,23 @@
WAMR_DIR=${PWD}/../../.. WAMR_DIR=${PWD}/../../..
/opt/wasi-sdk/bin/clang \ echo "Build wasm app .."
--target=wasm32 -O3 \ /opt/wasi-sdk/bin/clang -O3 \
-z stack-size=4096 -Wl,--initial-memory=65536 \ -z stack-size=4096 -Wl,--initial-memory=65536 \
--sysroot=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot \ -o test.wasm main.c \
-Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt \ -Wl,--export=main -Wl,--export=__main_argc_argv \
-Wl,--export=main, \ -Wl,--export=__data_end -Wl,--export=__heap_base \
-Wl,--export=__data_end, -Wl,--export=__heap_base \ -Wl,--strip-all,--no-entry \
-Wl,--no-threads,--strip-all,--no-entry \ -Wl,--allow-undefined \
-nostdlib -o test.wasm *.c -nostdlib \
#./jeffdump -o test_wasm.h -n wasm_test_file test.wasm
echo "Build binarydump tool .."
rm -fr build && mkdir build && cd build
cmake ../../../../test-tools/binarydump-tool
make
cd ..
echo "Generate test_wasm.h .."
./build/binarydump -o test_wasm.h -n wasm_test_file test.wasm
echo "Done"

View File

@ -46,7 +46,7 @@ OUT_FILE=${i%.*}.wasm
--target=wasm32 -O0 -z stack-size=4096 -Wl,--initial-memory=65536 \ --target=wasm32 -O0 -z stack-size=4096 -Wl,--initial-memory=65536 \
--sysroot=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot \ --sysroot=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot \
-Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt \ -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt \
-Wl,--no-threads,--strip-all,--no-entry -nostdlib \ -Wl,--strip-all,--no-entry -nostdlib \
-Wl,--export=generate_float \ -Wl,--export=generate_float \
-Wl,--export=float_to_string \ -Wl,--export=float_to_string \
-Wl,--export=calculate\ -Wl,--export=calculate\

View File

@ -21,8 +21,8 @@ all:
--target=wasm32 -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \ --target=wasm32 -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
--sysroot=$(SDK_DIR)/libc-builtin-sysroot \ --sysroot=$(SDK_DIR)/libc-builtin-sysroot \
-L$(APP_FRAMEWORK_DIR)/lib -lapp_framework \ -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework \
-Wl,--allow-undefined-file=$(SDK_DIR)/libc-builtin-sysroot/share/defined-symbols.txt \ -Wl,--allow-undefined-file=$(SDK_DIR)/libc-builtin-sysroot/share/defined-symbols.txt \
-Wl,--no-threads,--strip-all,--no-entry -nostdlib \ -Wl,--strip-all,--no-entry -nostdlib \
-Wl,--export=on_init -Wl,--export=on_timer_callback \ -Wl,--export=on_init -Wl,--export=on_timer_callback \
-Wl,--export=on_widget_event \ -Wl,--export=on_widget_event \
-Wl,--export=__heap_base,--export=__data_end \ -Wl,--export=__heap_base,--export=__data_end \

View File

@ -27,7 +27,7 @@ all:
@$(CC) $(CFLAGS) $(SRCS) \ @$(CC) $(CFLAGS) $(SRCS) \
--target=wasm32 -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \ --target=wasm32 -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
-Wl,--allow-undefined \ -Wl,--allow-undefined \
-Wl,--no-threads,--strip-all,--no-entry -nostdlib \ -Wl,--strip-all,--no-entry -nostdlib \
-Wl,--export=on_init -Wl,--export=on_timer_callback \ -Wl,--export=on_init -Wl,--export=on_timer_callback \
-Wl,--export=on_widget_event \ -Wl,--export=on_widget_event \
-Wl,--export=__heap_base,--export=__data_end \ -Wl,--export=__heap_base,--export=__data_end \

View File

@ -49,9 +49,9 @@ all:
@$(CC) $(CFLAGS) $(SRCS) \ @$(CC) $(CFLAGS) $(SRCS) \
-O3 -z stack-size=2048 -Wl,--initial-memory=65536 \ -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
-DLV_CONF_INCLUDE_SIMPLE \ -DLV_CONF_INCLUDE_SIMPLE \
-L$(APP_FRAMEWORK_DIR)/lib -lapp_framework \ -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework \
-Wl,--allow-undefined \ -Wl,--allow-undefined \
-Wl,--no-threads,--strip-all,--no-entry \ -Wl,--strip-all,--no-entry \
-Wl,--export=on_init -Wl,--export=on_timer_callback \ -Wl,--export=on_init -Wl,--export=on_timer_callback \
-Wl,--export=__heap_base,--export=__data_end \ -Wl,--export=__heap_base,--export=__data_end \
-o ui_app_wasi.wasm -o ui_app_wasi.wasm

View File

@ -52,8 +52,8 @@ all:
--sysroot=$(WAMR_DIR)/wamr-sdk/app/libc-builtin-sysroot \ --sysroot=$(WAMR_DIR)/wamr-sdk/app/libc-builtin-sysroot \
-O3 -z stack-size=2048 -Wl,--initial-memory=65536 \ -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
-DLV_CONF_INCLUDE_SIMPLE \ -DLV_CONF_INCLUDE_SIMPLE \
-L$(APP_FRAMEWORK_DIR)/lib -lapp_framework \ -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework \
-Wl,--allow-undefined \ -Wl,--allow-undefined \
-Wl,--no-threads,--strip-all,--no-entry -nostdlib \ -Wl,--strip-all,--no-entry -nostdlib \
-Wl,--export=on_init -Wl,--export=on_timer_callback \ -Wl,--export=on_init -Wl,--export=on_timer_callback \
-o ui_app_builtin_libc.wasm -o ui_app_builtin_libc.wasm

View File

@ -27,11 +27,13 @@ set (DEFINED_SYMBOLS
"${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt") "${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
set (CMAKE_EXE_LINKER_FLAGS set (CMAKE_EXE_LINKER_FLAGS
"-Wl,--shared-memory,--max-memory=131072, \ "-Wl,--shared-memory,--max-memory=131072, \
-Wl,--no-entry,--strip-all,--export=main, \ -Wl,--no-entry,--strip-all, \
-Wl,--export=__heap_base,--export=__data_end \ -Wl,--export=__heap_base,--export=__data_end \
-Wl,--export=__wasm_call_ctors \ -Wl,--export=__wasm_call_ctors \
-Wl,--allow-undefined-file=${DEFINED_SYMBOLS}" -Wl,--export=main -Wl,--export=__main_argc_argv \
-Wl,--allow-undefined"
#-Wl,--allow-undefined-file=${DEFINED_SYMBOLS}"
) )
add_executable(test.wasm main.c) add_executable(test.wasm main.c)

View File

@ -153,7 +153,7 @@ OUT_FILE=${i%.*}.wasm
--target=wasm32 -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \ --target=wasm32 -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \
--sysroot=${WAMR_DIR}/wamr-sdk/out/$PROFILE/app-sdk/libc-builtin-sysroot \ --sysroot=${WAMR_DIR}/wamr-sdk/out/$PROFILE/app-sdk/libc-builtin-sysroot \
-Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/out/$PROFILE/app-sdk/libc-builtin-sysroot/share/defined-symbols.txt \ -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/out/$PROFILE/app-sdk/libc-builtin-sysroot/share/defined-symbols.txt \
-Wl,--no-threads,--strip-all,--no-entry -nostdlib \ -Wl,--strip-all,--no-entry -nostdlib \
-Wl,--export=on_init -Wl,--export=on_destroy \ -Wl,--export=on_init -Wl,--export=on_destroy \
-Wl,--export=on_request -Wl,--export=on_response \ -Wl,--export=on_request -Wl,--export=on_response \
-Wl,--export=on_sensor_event -Wl,--export=on_timer_callback \ -Wl,--export=on_sensor_event -Wl,--export=on_timer_callback \

View File

@ -20,11 +20,11 @@ OUT_FILE=${i%.*}.wasm
-Wno-int-conversion \ -Wno-int-conversion \
-I${APP_FRAMEWORK_DIR}/include \ -I${APP_FRAMEWORK_DIR}/include \
-I${DEPS_DIR} \ -I${DEPS_DIR} \
--target=wasm32 -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \ -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \
--sysroot=${SDK_DIR}/app-sdk/libc-builtin-sysroot \ --sysroot=${SDK_DIR}/app-sdk/libc-builtin-sysroot \
-L${APP_FRAMEWORK_DIR}/lib -lapp_framework \ -L${APP_FRAMEWORK_DIR}/lib -lapp_framework \
-Wl,--allow-undefined-file=${SDK_DIR}/app-sdk/libc-builtin-sysroot/share/defined-symbols.txt \ -Wl,--allow-undefined-file=${SDK_DIR}/app-sdk/libc-builtin-sysroot/share/defined-symbols.txt \
-Wl,--no-threads,--strip-all,--no-entry -nostdlib \ -Wl,--strip-all,--no-entry -nostdlib \
-Wl,--export=on_init -Wl,--export=on_destroy \ -Wl,--export=on_init -Wl,--export=on_destroy \
-Wl,--export=on_request -Wl,--export=on_response \ -Wl,--export=on_request -Wl,--export=on_response \
-Wl,--export=on_sensor_event -Wl,--export=on_timer_callback \ -Wl,--export=on_sensor_event -Wl,--export=on_timer_callback \
@ -36,4 +36,4 @@ if [ -f ${OUT_FILE} ]; then
else else
echo "build ${OUT_FILE} fail" echo "build ${OUT_FILE} fail"
fi fi
done done

View File

@ -18,7 +18,7 @@ SET (CMAKE_CXX_COMPILER_TARGET "wasm32")
SET (CMAKE_CXX_COMPILER "${WASI_SDK_DIR}/bin/clang++") SET (CMAKE_CXX_COMPILER "${WASI_SDK_DIR}/bin/clang++")
SET (CMAKE_EXE_LINKER_FLAGS SET (CMAKE_EXE_LINKER_FLAGS
"-Wl,--initial-memory=65536,--no-entry,--no-threads,--strip-all" CACHE INTERNAL "") "-Wl,--initial-memory=65536,--no-entry,--strip-all" CACHE INTERNAL "")
SET (CMAKE_LINKER "${WASI_SDK_DIR}/bin/wasm-ld" CACHE INTERNAL "") SET (CMAKE_LINKER "${WASI_SDK_DIR}/bin/wasm-ld" CACHE INTERNAL "")
SET (CMAKE_AR "${WASI_SDK_DIR}/bin/llvm-ar" CACHE INTERNAL "") SET (CMAKE_AR "${WASI_SDK_DIR}/bin/llvm-ar" CACHE INTERNAL "")