From 4b5543cc1a9284b18a66afd54731dd22ae2147e4 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Mon, 10 Jan 2022 15:59:58 +0800 Subject: [PATCH] Enable Windows XIP (#944) Enable running XIP file on Windows platform. And add more strict checks for wamrc to report error when the input file is same with output file, or the input file is AOT file but not wasm file. --- core/iwasm/aot/aot_loader.c | 24 ++++++++++++++++++------ core/iwasm/aot/aot_runtime.c | 5 +++++ product-mini/platforms/windows/main.c | 27 ++++++++++++++++++++++++++- wamr-compiler/main.c | 10 ++++++++++ 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index f8766ddee..90ff8fcd4 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -1575,8 +1575,10 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) if (module->func_count > 0) { + uint32 plt_table_size = + module->is_indirect_mode ? 0 : get_plt_table_size(); rtl_func_table[module->func_count - 1].EndAddress = - (DWORD)(module->code_size - get_plt_table_size()); + (DWORD)(module->code_size - plt_table_size); if (!RtlAddFunctionTable(rtl_func_table, module->func_count, (DWORD64)(uintptr_t)module->code)) { @@ -2113,19 +2115,29 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, memcpy(group_name_buf, group_name, group_name_len); memcpy(symbol_name_buf, symbol_name, symbol_name_len); - if (group_name_len == strlen(".text") + if ((group_name_len == strlen(".text") + || (module->is_indirect_mode + && group_name_len == strlen(".text") + 1)) && !strncmp(group_name, ".text", strlen(".text"))) { - if (symbol_name_len == strlen(XMM_PLT_PREFIX) + 32 + if ((symbol_name_len == strlen(XMM_PLT_PREFIX) + 32 + || (module->is_indirect_mode + && symbol_name_len == strlen(XMM_PLT_PREFIX) + 32 + 1)) && !strncmp(symbol_name, XMM_PLT_PREFIX, strlen(XMM_PLT_PREFIX))) { module->xmm_plt_count++; } - else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 16 + else if ((symbol_name_len == strlen(REAL_PLT_PREFIX) + 16 + || (module->is_indirect_mode + && symbol_name_len + == strlen(REAL_PLT_PREFIX) + 16 + 1)) && !strncmp(symbol_name, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))) { module->real_plt_count++; } - else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 8 + else if ((symbol_name_len >= strlen(REAL_PLT_PREFIX) + 8 + || (module->is_indirect_mode + && symbol_name_len + == strlen(REAL_PLT_PREFIX) + 8 + 1)) && !strncmp(symbol_name, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))) { module->float_plt_count++; @@ -2230,7 +2242,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, #endif ) { #if !defined(BH_PLATFORM_LINUX) && !defined(BH_PLATFORM_LINUX_SGX) \ - && !defined(BH_PLATFORM_DARWIN) + && !defined(BH_PLATFORM_DARWIN) && !defined(BH_PLATFORM_WINDOWS) if (module->is_indirect_mode) { set_error_buf(error_buf, error_buf_size, "cannot apply relocation to text section " diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 7cad5d3be..4ff63d465 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1260,6 +1260,11 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info) return EXCEPTION_CONTINUE_EXECUTION; } } + + os_printf("Unhandled exception thrown: exception code: 0x%lx, " + "exception address: %p, exception information: %p\n", + ExceptionRecord->ExceptionCode, ExceptionRecord->ExceptionAddress, + sig_addr); return EXCEPTION_CONTINUE_SEARCH; } #endif /* end of BH_PLATFORM_WINDOWS */ diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index dbe9acd59..6e25c4359 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -234,6 +234,7 @@ main(int argc, char *argv[]) int log_verbose_level = 2; #endif bool is_repl_mode = false; + bool is_xip_file = false; #if WASM_ENABLE_LIBC_WASI != 0 const char *dir_list[8] = { NULL }; uint32 dir_list_size = 0; @@ -382,6 +383,27 @@ main(int argc, char *argv[]) (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) goto fail1; +#if WASM_ENABLE_AOT != 0 + if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) { + uint8 *wasm_file_mapped; + int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; + int map_flags = MMAP_MAP_32BIT; + + if (!(wasm_file_mapped = + os_mmap(NULL, (uint32)wasm_file_size, map_prot, map_flags))) { + printf("mmap memory failed\n"); + wasm_runtime_free(wasm_file_buf); + goto fail1; + } + + bh_memcpy_s(wasm_file_mapped, wasm_file_size, wasm_file_buf, + wasm_file_size); + wasm_runtime_free(wasm_file_buf); + wasm_file_buf = wasm_file_mapped; + is_xip_file = true; + } +#endif + #if WASM_ENABLE_MULTI_MODULE != 0 wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer); #endif @@ -422,7 +444,10 @@ fail3: fail2: /* free the file buffer */ - wasm_runtime_free(wasm_file_buf); + if (!is_xip_file) + wasm_runtime_free(wasm_file_buf); + else + os_munmap(wasm_file_buf, wasm_file_size); fail1: /* destroy runtime environment */ diff --git a/wamr-compiler/main.c b/wamr-compiler/main.c index 885a1382f..1587d2ae1 100644 --- a/wamr-compiler/main.c +++ b/wamr-compiler/main.c @@ -235,6 +235,11 @@ main(int argc, char *argv[]) wasm_file_name = argv[0]; + if (!strcmp(wasm_file_name, out_file_name)) { + printf("Error: input file and output file are the same"); + return -1; + } + memset(&init_args, 0, sizeof(RuntimeInitArgs)); init_args.mem_alloc_type = Alloc_With_Allocator; @@ -257,6 +262,11 @@ main(int argc, char *argv[]) (uint8 *)bh_read_file_to_buffer(wasm_file_name, &wasm_file_size))) goto fail1; + if (get_package_type(wasm_file, wasm_file_size) != Wasm_Module_Bytecode) { + printf("Invalid file type: expected wasm file but got other\n"); + goto fail2; + } + /* load WASM module */ if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf, sizeof(error_buf)))) {