Skip to content

Commit 4b5543c

Browse files
authored
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.
1 parent ae18a03 commit 4b5543c

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,8 +1575,10 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
15751575

15761576
#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
15771577
if (module->func_count > 0) {
1578+
uint32 plt_table_size =
1579+
module->is_indirect_mode ? 0 : get_plt_table_size();
15781580
rtl_func_table[module->func_count - 1].EndAddress =
1579-
(DWORD)(module->code_size - get_plt_table_size());
1581+
(DWORD)(module->code_size - plt_table_size);
15801582

15811583
if (!RtlAddFunctionTable(rtl_func_table, module->func_count,
15821584
(DWORD64)(uintptr_t)module->code)) {
@@ -2113,19 +2115,29 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
21132115
memcpy(group_name_buf, group_name, group_name_len);
21142116
memcpy(symbol_name_buf, symbol_name, symbol_name_len);
21152117

2116-
if (group_name_len == strlen(".text")
2118+
if ((group_name_len == strlen(".text")
2119+
|| (module->is_indirect_mode
2120+
&& group_name_len == strlen(".text") + 1))
21172121
&& !strncmp(group_name, ".text", strlen(".text"))) {
2118-
if (symbol_name_len == strlen(XMM_PLT_PREFIX) + 32
2122+
if ((symbol_name_len == strlen(XMM_PLT_PREFIX) + 32
2123+
|| (module->is_indirect_mode
2124+
&& symbol_name_len == strlen(XMM_PLT_PREFIX) + 32 + 1))
21192125
&& !strncmp(symbol_name, XMM_PLT_PREFIX,
21202126
strlen(XMM_PLT_PREFIX))) {
21212127
module->xmm_plt_count++;
21222128
}
2123-
else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 16
2129+
else if ((symbol_name_len == strlen(REAL_PLT_PREFIX) + 16
2130+
|| (module->is_indirect_mode
2131+
&& symbol_name_len
2132+
== strlen(REAL_PLT_PREFIX) + 16 + 1))
21242133
&& !strncmp(symbol_name, REAL_PLT_PREFIX,
21252134
strlen(REAL_PLT_PREFIX))) {
21262135
module->real_plt_count++;
21272136
}
2128-
else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 8
2137+
else if ((symbol_name_len >= strlen(REAL_PLT_PREFIX) + 8
2138+
|| (module->is_indirect_mode
2139+
&& symbol_name_len
2140+
== strlen(REAL_PLT_PREFIX) + 8 + 1))
21292141
&& !strncmp(symbol_name, REAL_PLT_PREFIX,
21302142
strlen(REAL_PLT_PREFIX))) {
21312143
module->float_plt_count++;
@@ -2230,7 +2242,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
22302242
#endif
22312243
) {
22322244
#if !defined(BH_PLATFORM_LINUX) && !defined(BH_PLATFORM_LINUX_SGX) \
2233-
&& !defined(BH_PLATFORM_DARWIN)
2245+
&& !defined(BH_PLATFORM_DARWIN) && !defined(BH_PLATFORM_WINDOWS)
22342246
if (module->is_indirect_mode) {
22352247
set_error_buf(error_buf, error_buf_size,
22362248
"cannot apply relocation to text section "

core/iwasm/aot/aot_runtime.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,11 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info)
12601260
return EXCEPTION_CONTINUE_EXECUTION;
12611261
}
12621262
}
1263+
1264+
os_printf("Unhandled exception thrown: exception code: 0x%lx, "
1265+
"exception address: %p, exception information: %p\n",
1266+
ExceptionRecord->ExceptionCode, ExceptionRecord->ExceptionAddress,
1267+
sig_addr);
12631268
return EXCEPTION_CONTINUE_SEARCH;
12641269
}
12651270
#endif /* end of BH_PLATFORM_WINDOWS */

product-mini/platforms/windows/main.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ main(int argc, char *argv[])
234234
int log_verbose_level = 2;
235235
#endif
236236
bool is_repl_mode = false;
237+
bool is_xip_file = false;
237238
#if WASM_ENABLE_LIBC_WASI != 0
238239
const char *dir_list[8] = { NULL };
239240
uint32 dir_list_size = 0;
@@ -382,6 +383,27 @@ main(int argc, char *argv[])
382383
(uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
383384
goto fail1;
384385

386+
#if WASM_ENABLE_AOT != 0
387+
if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) {
388+
uint8 *wasm_file_mapped;
389+
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
390+
int map_flags = MMAP_MAP_32BIT;
391+
392+
if (!(wasm_file_mapped =
393+
os_mmap(NULL, (uint32)wasm_file_size, map_prot, map_flags))) {
394+
printf("mmap memory failed\n");
395+
wasm_runtime_free(wasm_file_buf);
396+
goto fail1;
397+
}
398+
399+
bh_memcpy_s(wasm_file_mapped, wasm_file_size, wasm_file_buf,
400+
wasm_file_size);
401+
wasm_runtime_free(wasm_file_buf);
402+
wasm_file_buf = wasm_file_mapped;
403+
is_xip_file = true;
404+
}
405+
#endif
406+
385407
#if WASM_ENABLE_MULTI_MODULE != 0
386408
wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer);
387409
#endif
@@ -422,7 +444,10 @@ main(int argc, char *argv[])
422444

423445
fail2:
424446
/* free the file buffer */
425-
wasm_runtime_free(wasm_file_buf);
447+
if (!is_xip_file)
448+
wasm_runtime_free(wasm_file_buf);
449+
else
450+
os_munmap(wasm_file_buf, wasm_file_size);
426451

427452
fail1:
428453
/* destroy runtime environment */

wamr-compiler/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ main(int argc, char *argv[])
235235

236236
wasm_file_name = argv[0];
237237

238+
if (!strcmp(wasm_file_name, out_file_name)) {
239+
printf("Error: input file and output file are the same");
240+
return -1;
241+
}
242+
238243
memset(&init_args, 0, sizeof(RuntimeInitArgs));
239244

240245
init_args.mem_alloc_type = Alloc_With_Allocator;
@@ -257,6 +262,11 @@ main(int argc, char *argv[])
257262
(uint8 *)bh_read_file_to_buffer(wasm_file_name, &wasm_file_size)))
258263
goto fail1;
259264

265+
if (get_package_type(wasm_file, wasm_file_size) != Wasm_Module_Bytecode) {
266+
printf("Invalid file type: expected wasm file but got other\n");
267+
goto fail2;
268+
}
269+
260270
/* load WASM module */
261271
if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
262272
sizeof(error_buf)))) {

0 commit comments

Comments
 (0)