Enhance XIP and add XIP document (#863)

Auto detect whether file is XIP file before loading module in posix like and
linux-sgx platforms, and if yes, mmap executable memory automatically to
run the XIP file.
Add document about XIP feature.
Enable test spec cases with XIP feature.
This commit is contained in:
Wenyong Huang
2021-12-06 17:25:10 +08:00
committed by GitHub
parent 3f808d4596
commit b490a229f6
11 changed files with 184 additions and 24 deletions

View File

@ -52,6 +52,8 @@ typedef struct EnclaveModule {
uint32 wasi_env_list_size;
char **wasi_argv;
uint32 wasi_argc;
bool is_xip_file;
uint32 total_size_mapped;
} EnclaveModule;
#if WASM_ENABLE_SPEC_TEST == 0
@ -127,28 +129,57 @@ handle_cmd_load_module(uint64 *args, uint32 argc)
uint32 error_buf_size = *(uint32 *)args++;
uint64 total_size = sizeof(EnclaveModule) + (uint64)wasm_file_size;
EnclaveModule *enclave_module;
bool is_xip_file = false;
bh_assert(argc == 4);
if (total_size >= UINT32_MAX
|| !(enclave_module =
(EnclaveModule *)wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: "
"allocate memory failed.");
*(void **)args_org = NULL;
return;
#if WASM_ENABLE_AOT != 0
is_xip_file = wasm_runtime_is_xip_file((uint8 *)wasm_file, wasm_file_size);
#endif
if (!is_xip_file) {
if (total_size >= UINT32_MAX
|| !(enclave_module = (EnclaveModule *)wasm_runtime_malloc(
(uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: "
"allocate memory failed.");
*(void **)args_org = NULL;
return;
}
memset(enclave_module, 0, (uint32)total_size);
}
else {
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
int map_flags = MMAP_MAP_NONE;
if (total_size >= UINT32_MAX
|| !(enclave_module = (EnclaveModule *)os_mmap(
NULL, (uint32)total_size, map_prot, map_flags))) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: mmap memory failed.");
*(void **)args_org = NULL;
return;
}
memset(enclave_module, 0, (uint32)total_size);
enclave_module->is_xip_file = true;
enclave_module->total_size_mapped = (uint32)total_size;
}
memset(enclave_module, 0, (uint32)total_size);
enclave_module->wasm_file = (uint8 *)enclave_module + sizeof(EnclaveModule);
bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file,
wasm_file_size);
if (is_xip_file) {
enclave_module->is_xip_file = true;
}
if (!(enclave_module->module =
wasm_runtime_load(enclave_module->wasm_file, wasm_file_size,
error_buf, error_buf_size))) {
wasm_runtime_free(enclave_module);
if (!is_xip_file)
wasm_runtime_free(enclave_module);
else
os_munmap(enclave_module, (uint32)total_size);
*(void **)args_org = NULL;
return;
}
@ -170,7 +201,10 @@ handle_cmd_unload_module(uint64 *args, uint32 argc)
wasm_runtime_free(enclave_module->wasi_arg_buf);
wasm_runtime_unload(enclave_module->module);
wasm_runtime_free(enclave_module);
if (!enclave_module->is_xip_file)
wasm_runtime_free(enclave_module);
else
os_munmap(enclave_module, enclave_module->total_size_mapped);
LOG_VERBOSE("Unload module success.\n");
}

View File

@ -34,8 +34,6 @@ print_help()
printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
" that runs commands in the form of \"FUNC ARG...\"\n");
printf(" --xip Enable XIP (Execution In Place) mode to run AOT file\n"
" generated with \"--enable-indirect-mode\" flag\n");
#if WASM_ENABLE_LIBC_WASI != 0
printf(" --env=<env> Pass wasi environment variables with \"key=value\"\n");
printf(" to the program, for example:\n");
@ -240,7 +238,7 @@ main(int argc, char *argv[])
int log_verbose_level = 2;
#endif
bool is_repl_mode = false;
bool is_xip_mode = false;
bool is_xip_file = false;
#if WASM_ENABLE_LIBC_WASI != 0
const char *dir_list[8] = { NULL };
uint32 dir_list_size = 0;
@ -273,9 +271,6 @@ main(int argc, char *argv[])
else if (!strcmp(argv[0], "--repl")) {
is_repl_mode = true;
}
else if (!strcmp(argv[0], "--xip")) {
is_xip_mode = true;
}
else if (!strncmp(argv[0], "--stack-size=", 13)) {
if (argv[0][13] == '\0')
return print_help();
@ -392,10 +387,11 @@ main(int argc, char *argv[])
(uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
goto fail1;
if (is_xip_mode) {
#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_NONE;
int map_flags = MMAP_MAP_32BIT;
if (!(wasm_file_mapped =
os_mmap(NULL, (uint32)wasm_file_size, map_prot, map_flags))) {
@ -408,7 +404,9 @@ main(int argc, char *argv[])
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);
@ -450,7 +448,7 @@ fail3:
fail2:
/* free the file buffer */
if (!is_xip_mode)
if (!is_xip_file)
wasm_runtime_free(wasm_file_buf);
else
os_munmap(wasm_file_buf, wasm_file_size);