Fix function type not set issue of aot_call_indirect (#229)

Add registration of libc-wasi to 'wasi_snapshot_preview1' to support cargo-wasi
change zephyr build method from cmake to west
fix problem when preserve space for local vars
fix wasi authority problem
This commit is contained in:
Xu Jun
2020-04-07 11:04:46 +08:00
committed by GitHub
parent 374e687938
commit 5e196253f6
26 changed files with 512 additions and 143 deletions

View File

@ -675,9 +675,11 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
p = p_old;
/* insert "env" and "wasi_unstable" to const str list */
/* insert "env", "wasi_unstable" and "wasi_snapshot_preview1" to const str list */
if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size)
|| !const_str_list_insert((uint8*)"wasi_unstable", 13, module,
error_buf, error_buf_size)
|| !const_str_list_insert((uint8*)"wasi_snapshot_preview1", 22, module,
error_buf, error_buf_size)) {
return false;
}
@ -797,7 +799,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
#if WASM_ENABLE_LIBC_WASI != 0
import = module->import_functions;
for (i = 0; i < module->import_function_count; i++, import++) {
if (!strcmp(import->u.names.module_name, "wasi_unstable")) {
if (!strcmp(import->u.names.module_name, "wasi_unstable")
|| !strcmp(import->u.names.module_name, "wasi_snapshot_preview1")) {
module->is_wasi_module = true;
break;
}
@ -2377,6 +2380,9 @@ typedef struct WASMLoaderContext {
int16 start_dynamic_offset;
int16 max_dynamic_offset;
/* preserved local offset */
int16 preserved_local_offset;
/* const buffer */
uint8 *const_buf;
uint16 num_const;
@ -2873,6 +2879,9 @@ wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
ctx->frame_offset = ctx->frame_offset_bottom;
ctx->dynamic_offset = ctx->start_dynamic_offset;
/* init preserved local offsets */
ctx->preserved_local_offset = ctx->max_dynamic_offset;
/* const buf is reserved */
return true;
}
@ -2950,19 +2959,21 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
skip_label();
if (local_type == VALUE_TYPE_I32
|| local_type == VALUE_TYPE_F32) {
preserved_offset = loader_ctx->dynamic_offset++;
preserved_offset = loader_ctx->preserved_local_offset;
/* Only increase preserve offset in the second traversal */
if (loader_ctx->p_code_compiled)
loader_ctx->preserved_local_offset++;
emit_label(EXT_OP_COPY_STACK_TOP);
}
else {
preserved_offset = loader_ctx->dynamic_offset;
loader_ctx->dynamic_offset += 2;
preserved_offset = loader_ctx->preserved_local_offset;
if (loader_ctx->p_code_compiled)
loader_ctx->preserved_local_offset += 2;
emit_label(EXT_OP_COPY_STACK_TOP_I64);
}
emit_operand(loader_ctx, local_index);
emit_operand(loader_ctx, preserved_offset);
emit_label(opcode);
if (loader_ctx->dynamic_offset > loader_ctx->max_dynamic_offset)
loader_ctx->max_dynamic_offset = loader_ctx->dynamic_offset;
}
loader_ctx->frame_offset_bottom[i] = preserved_offset;
}
@ -3116,12 +3127,14 @@ wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) {
ctx->frame_offset -= 1;
if (*(ctx->frame_offset) > ctx->start_dynamic_offset)
if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
&& (*(ctx->frame_offset) < ctx->max_dynamic_offset))
ctx->dynamic_offset -= 1;
}
else {
ctx->frame_offset -= 2;
if (*(ctx->frame_offset) > ctx->start_dynamic_offset)
if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
&& (*(ctx->frame_offset) < ctx->max_dynamic_offset))
ctx->dynamic_offset -= 2;
}
emit_operand(ctx, *(ctx->frame_offset));
@ -4682,7 +4695,7 @@ handle_next_reachable_block:
}
}
func->max_stack_cell_num = loader_ctx->max_dynamic_offset -
func->max_stack_cell_num = loader_ctx->preserved_local_offset -
loader_ctx->start_dynamic_offset + 1;
#else
func->max_stack_cell_num = loader_ctx->max_stack_cell_num;

View File

@ -1166,3 +1166,42 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
#endif /* end of WASM_ENABLE_MEMORY_GROW */
}
bool
wasm_call_indirect(WASMExecEnv *exec_env,
uint32_t element_indices,
uint32_t argc, uint32_t argv[])
{
WASMModuleInstance *module_inst = NULL;
WASMTableInstance *table_inst = NULL;
uint32_t function_indices = 0;
WASMFunctionInstance *function_inst = NULL;
module_inst =
(WASMModuleInstance*)exec_env->module_inst;
bh_assert(module_inst);
table_inst = module_inst->default_table;
if (!table_inst) {
wasm_set_exception(module_inst, "there is no table");
goto got_exception;
}
if (element_indices >= table_inst->cur_size) {
wasm_set_exception(module_inst, "undefined element");
goto got_exception;
}
function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
if (function_indices == 0xFFFFFFFF) {
wasm_set_exception(module_inst, "uninitialized element");
goto got_exception;
}
function_inst = module_inst->functions + function_indices;
wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
return !wasm_get_exception(module_inst) ? true : false;
got_exception:
return false;
}

View File

@ -280,6 +280,11 @@ wasm_get_native_addr_range(WASMModuleInstance *module_inst,
bool
wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count);
bool
wasm_call_indirect(WASMExecEnv *exec_env,
uint32_t element_indices,
uint32_t argc, uint32_t argv[]);
#ifdef __cplusplus
}
#endif