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.
This commit is contained in:
Wenyong Huang 2022-01-10 15:59:58 +08:00 committed by GitHub
parent ae18a03f60
commit 4b5543cc1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 7 deletions

View File

@ -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 defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
if (module->func_count > 0) { 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 = 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, if (!RtlAddFunctionTable(rtl_func_table, module->func_count,
(DWORD64)(uintptr_t)module->code)) { (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(group_name_buf, group_name, group_name_len);
memcpy(symbol_name_buf, symbol_name, symbol_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"))) { && !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, && !strncmp(symbol_name, XMM_PLT_PREFIX,
strlen(XMM_PLT_PREFIX))) { strlen(XMM_PLT_PREFIX))) {
module->xmm_plt_count++; 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, && !strncmp(symbol_name, REAL_PLT_PREFIX,
strlen(REAL_PLT_PREFIX))) { strlen(REAL_PLT_PREFIX))) {
module->real_plt_count++; 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, && !strncmp(symbol_name, REAL_PLT_PREFIX,
strlen(REAL_PLT_PREFIX))) { strlen(REAL_PLT_PREFIX))) {
module->float_plt_count++; module->float_plt_count++;
@ -2230,7 +2242,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
#endif #endif
) { ) {
#if !defined(BH_PLATFORM_LINUX) && !defined(BH_PLATFORM_LINUX_SGX) \ #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) { if (module->is_indirect_mode) {
set_error_buf(error_buf, error_buf_size, set_error_buf(error_buf, error_buf_size,
"cannot apply relocation to text section " "cannot apply relocation to text section "

View File

@ -1260,6 +1260,11 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info)
return EXCEPTION_CONTINUE_EXECUTION; 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; return EXCEPTION_CONTINUE_SEARCH;
} }
#endif /* end of BH_PLATFORM_WINDOWS */ #endif /* end of BH_PLATFORM_WINDOWS */

View File

@ -234,6 +234,7 @@ main(int argc, char *argv[])
int log_verbose_level = 2; int log_verbose_level = 2;
#endif #endif
bool is_repl_mode = false; bool is_repl_mode = false;
bool is_xip_file = false;
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
const char *dir_list[8] = { NULL }; const char *dir_list[8] = { NULL };
uint32 dir_list_size = 0; 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))) (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
goto fail1; 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 #if WASM_ENABLE_MULTI_MODULE != 0
wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer); wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer);
#endif #endif
@ -422,7 +444,10 @@ fail3:
fail2: fail2:
/* free the file buffer */ /* 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: fail1:
/* destroy runtime environment */ /* destroy runtime environment */

View File

@ -235,6 +235,11 @@ main(int argc, char *argv[])
wasm_file_name = argv[0]; 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)); memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Allocator; 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))) (uint8 *)bh_read_file_to_buffer(wasm_file_name, &wasm_file_size)))
goto fail1; 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 */ /* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf, if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
sizeof(error_buf)))) { sizeof(error_buf)))) {