sync iwasm between windows and posix a bit (#4593)

this commit includes:

* update windows for
  * WASM_MEM_ALLOC_WITH_USAGE
  * InstantiationArgs2
  * --jit-codecache-size
  * --disable-bounds-checks
  * wasm_proposal_print_status

* fix WASM_ENABLE_SHARED_HEAP for windows
  (https://github.com/bytecodealliance/wasm-micro-runtime/issues/4592)

* cosmetic changes to reduce the diff
This commit is contained in:
YAMAMOTO Takashi 2025-09-14 15:07:54 +09:00 committed by GitHub
parent 5e779b3d7b
commit 92b065a3ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 126 additions and 56 deletions

View File

@ -80,7 +80,7 @@ print_help(void)
printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n"); printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n");
printf(" and --enable-segue means all flags are added.\n"); printf(" and --enable-segue means all flags are added.\n");
#endif #endif
#endif /* WASM_ENABLE_JIT != 0*/ #endif /* WASM_ENABLE_JIT != 0 */
#if WASM_ENABLE_LINUX_PERF != 0 #if WASM_ENABLE_LINUX_PERF != 0
printf(" --enable-linux-perf Enable linux perf support. It works in aot and llvm-jit.\n"); printf(" --enable-linux-perf Enable linux perf support. It works in aot and llvm-jit.\n");
#endif #endif
@ -404,7 +404,7 @@ unregister_and_unload_native_libs(uint32 native_lib_count,
static char * static char *
handle_module_path(const char *module_path) handle_module_path(const char *module_path)
{ {
/* next character after = */ /* next character after '=' */
return (strchr(module_path, '=')) + 1; return (strchr(module_path, '=')) + 1;
} }
@ -583,7 +583,7 @@ main(int argc, char *argv[])
uint32 heap_size = 16 * 1024; uint32 heap_size = 16 * 1024;
#endif #endif
#if WASM_ENABLE_SHARED_HEAP != 0 #if WASM_ENABLE_SHARED_HEAP != 0
SharedHeapInitArgs heap_init_args; SharedHeapInitArgs shared_heap_init_args;
uint32 shared_heap_size = 0; uint32 shared_heap_size = 0;
void *shared_heap = NULL; void *shared_heap = NULL;
#endif #endif
@ -1025,15 +1025,16 @@ main(int argc, char *argv[])
#if WASM_ENABLE_SHARED_HEAP != 0 #if WASM_ENABLE_SHARED_HEAP != 0
if (shared_heap_size > 0) { if (shared_heap_size > 0) {
memset(&heap_init_args, 0, sizeof(heap_init_args)); memset(&shared_heap_init_args, 0, sizeof(shared_heap_init_args));
heap_init_args.size = shared_heap_size; shared_heap_init_args.size = shared_heap_size;
shared_heap = wasm_runtime_create_shared_heap(&heap_init_args); shared_heap = wasm_runtime_create_shared_heap(&shared_heap_init_args);
if (!shared_heap) { if (!shared_heap) {
printf("Create preallocated shared heap failed\n"); printf("Create preallocated shared heap failed\n");
goto fail6; goto fail6;
} }
/* attach module instance 2 to the shared heap */ /* attach module instance to the shared heap */
if (!wasm_runtime_attach_shared_heap(wasm_module_inst, shared_heap)) { if (!wasm_runtime_attach_shared_heap(wasm_module_inst, shared_heap)) {
printf("Attach shared heap failed.\n"); printf("Attach shared heap failed.\n");
goto fail6; goto fail6;

View File

@ -14,70 +14,77 @@
#include "../common/libc_wasi.c" #include "../common/libc_wasi.c"
#endif #endif
#include "../common/wasm_proposal.c"
static int app_argc; static int app_argc;
static char **app_argv; static char **app_argv;
#define MODULE_PATH ("--module-path=")
/* clang-format off */ /* clang-format off */
static int static int
print_help() print_help(void)
{ {
printf("Usage: iwasm [-options] wasm_file [args...]\n"); printf("Usage: iwasm [-options] wasm_file [args...]\n");
printf("options:\n"); printf("options:\n");
printf(" -f|--function name Specify a function name of the module to run rather\n" printf(" -f|--function name Specify a function name of the module to run rather\n"
" than main\n"); " than main\n");
#if WASM_ENABLE_LOG != 0 #if WASM_ENABLE_LOG != 0
printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n" printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n"
" level with more log\n"); " level with more log\n");
#endif #endif
#if WASM_ENABLE_INTERP != 0 #if WASM_ENABLE_INTERP != 0
printf(" --interp Run the wasm app with interpreter mode\n"); printf(" --interp Run the wasm app with interpreter mode\n");
#endif #endif
#if WASM_ENABLE_FAST_JIT != 0 #if WASM_ENABLE_FAST_JIT != 0
printf(" --fast-jit Run the wasm app with fast jit mode\n"); printf(" --fast-jit Run the wasm app with fast jit mode\n");
#endif #endif
#if WASM_ENABLE_JIT != 0 #if WASM_ENABLE_JIT != 0
printf(" --llvm-jit Run the wasm app with llvm jit mode\n"); printf(" --llvm-jit Run the wasm app with llvm jit mode\n");
#endif #endif
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0 #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
printf(" --multi-tier-jit Run the wasm app with multi-tier jit mode\n"); printf(" --multi-tier-jit Run the wasm app with multi-tier jit mode\n");
#endif #endif
printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n"); printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n");
#if WASM_ENABLE_LIBC_WASI !=0 #if WASM_ENABLE_LIBC_WASI !=0
printf(" --heap-size=n Set maximum heap size in bytes, default is 0 KB when libc wasi is enabled\n"); printf(" --heap-size=n Set maximum heap size in bytes, default is 0 KB when libc wasi is enabled\n");
#else #else
printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB when libc wasi is diabled\n"); printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB when libc wasi is diabled\n");
#endif #endif
#if WASM_ENABLE_GC != 0
printf(" --gc-heap-size=n Set maximum gc heap size in bytes,\n");
printf(" default is %u KB\n", GC_HEAP_SIZE_DEFAULT / 1024);
#endif
#if WASM_ENABLE_SHARED_HEAP != 0 #if WASM_ENABLE_SHARED_HEAP != 0
printf(" --shared-heap-size=n Create shared heap of n bytes and attach to the wasm app.\n"); printf(" --shared-heap-size=n Create shared heap of n bytes and attach to the wasm app.\n");
printf(" The size n will be adjusted to a minumum number aligned to page size\n"); printf(" The size n will be adjusted to a minumum number aligned to page size\n");
#endif #endif
#if WASM_ENABLE_FAST_JIT != 0
printf(" --jit-codecache-size=n Set fast jit maximum code cache size in bytes,\n");
printf(" default is %u KB\n", FAST_JIT_DEFAULT_CODE_CACHE_SIZE / 1024);
#endif
#if WASM_ENABLE_GC != 0
printf(" --gc-heap-size=n Set maximum gc heap size in bytes,\n");
printf(" default is %u KB\n", GC_HEAP_SIZE_DEFAULT / 1024);
#endif
#if WASM_ENABLE_JIT != 0 #if WASM_ENABLE_JIT != 0
printf(" --llvm-jit-size-level=n Set LLVM JIT size level, default is 3\n"); printf(" --llvm-jit-size-level=n Set LLVM JIT size level, default is 3\n");
printf(" --llvm-jit-opt-level=n Set LLVM JIT optimization level, default is 3\n"); printf(" --llvm-jit-opt-level=n Set LLVM JIT optimization level, default is 3\n");
#endif /* WASM_ENABLE_JIT != 0 */
printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
" that runs commands in the form of `FUNC ARG...`\n");
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
printf(" --disable-bounds-checks Disable bounds checks for memory accesses\n");
#endif #endif
printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
" that runs commands in the form of `FUNC ARG...`\n");
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
libc_wasi_print_help(); libc_wasi_print_help();
#endif #endif
#if WASM_ENABLE_MULTI_MODULE != 0 #if WASM_ENABLE_MULTI_MODULE != 0
printf(" --module-path=<path> Indicate a module search path. default is current\n" printf(" --module-path=<path> Indicate a module search path. default is current\n"
" directory('./')\n"); " directory('./')\n");
#endif #endif
#if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 #if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n"); printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
#endif #endif
#if WASM_ENABLE_DEBUG_INTERP != 0 #if WASM_ENABLE_DEBUG_INTERP != 0
printf(" -g=ip:port Set the debug sever address, default is debug disabled\n"); printf(" -g=ip:port Set the debug sever address, default is debug disabled\n");
printf(" if port is 0, then a random port will be used\n"); printf(" if port is 0, then a random port will be used\n");
#endif #endif
printf(" --version Show version information\n"); printf(" --version Show version information\n");
return 1; return 1;
} }
/* clang-format on */ /* clang-format on */
@ -190,6 +197,9 @@ static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#else #else
static void * static void *
malloc_func( malloc_func(
#if WASM_MEM_ALLOC_WITH_USAGE != 0
mem_alloc_usage_t usage,
#endif
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data, void *user_data,
#endif #endif
@ -200,6 +210,9 @@ malloc_func(
static void * static void *
realloc_func( realloc_func(
#if WASM_MEM_ALLOC_WITH_USAGE != 0
mem_alloc_usage_t usage, bool full_size_mmaped,
#endif
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data, void *user_data,
#endif #endif
@ -210,6 +223,9 @@ realloc_func(
static void static void
free_func( free_func(
#if WASM_MEM_ALLOC_WITH_USAGE != 0
mem_alloc_usage_t usage,
#endif
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0 #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
void *user_data, void *user_data,
#endif #endif
@ -228,6 +244,7 @@ handle_module_path(const char *module_path)
} }
static char *module_search_path = "."; static char *module_search_path = ".";
static bool static bool
module_reader_callback(package_type_t module_type, const char *module_name, module_reader_callback(package_type_t module_type, const char *module_name,
uint8 **p_buffer, uint32 *p_size) uint8 **p_buffer, uint32 *p_size)
@ -283,6 +300,14 @@ main(int argc, char *argv[])
#else #else
uint32 heap_size = 16 * 1024; uint32 heap_size = 16 * 1024;
#endif #endif
#if WASM_ENABLE_SHARED_HEAP != 0
SharedHeapInitArgs shared_heap_init_args;
uint32 shared_heap_size = 0;
void *shared_heap = NULL;
#endif
#if WASM_ENABLE_FAST_JIT != 0
uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE;
#endif
#if WASM_ENABLE_GC != 0 #if WASM_ENABLE_GC != 0
uint32 gc_heap_size = GC_HEAP_SIZE_DEFAULT; uint32 gc_heap_size = GC_HEAP_SIZE_DEFAULT;
#endif #endif
@ -294,12 +319,16 @@ main(int argc, char *argv[])
wasm_module_inst_t wasm_module_inst = NULL; wasm_module_inst_t wasm_module_inst = NULL;
RunningMode running_mode = 0; RunningMode running_mode = 0;
RuntimeInitArgs init_args; RuntimeInitArgs init_args;
struct InstantiationArgs2 *inst_args;
char error_buf[128] = { 0 }; char error_buf[128] = { 0 };
#if WASM_ENABLE_LOG != 0 #if WASM_ENABLE_LOG != 0
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; bool is_xip_file = false;
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
bool disable_bounds_checks = false;
#endif
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
libc_wasi_parse_context_t wasi_parse_ctx; libc_wasi_parse_context_t wasi_parse_ctx;
#endif #endif
@ -351,6 +380,11 @@ main(int argc, char *argv[])
else if (!strcmp(argv[0], "--repl")) { else if (!strcmp(argv[0], "--repl")) {
is_repl_mode = true; is_repl_mode = true;
} }
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
else if (!strcmp(argv[0], "--disable-bounds-checks")) {
disable_bounds_checks = true;
}
#endif
else if (!strncmp(argv[0], "--stack-size=", 13)) { else if (!strncmp(argv[0], "--stack-size=", 13)) {
if (argv[0][13] == '\0') if (argv[0][13] == '\0')
return print_help(); return print_help();
@ -361,13 +395,6 @@ main(int argc, char *argv[])
return print_help(); return print_help();
heap_size = atoi(argv[0] + 12); heap_size = atoi(argv[0] + 12);
} }
#if WASM_ENABLE_GC != 0
else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
if (argv[0][15] == '\0')
return print_help();
gc_heap_size = atoi(argv[0] + 15);
}
#endif
#if WASM_ENABLE_SHARED_HEAP != 0 #if WASM_ENABLE_SHARED_HEAP != 0
else if (!strncmp(argv[0], "--shared-heap-size=", 19)) { else if (!strncmp(argv[0], "--shared-heap-size=", 19)) {
if (argv[0][19] == '\0') if (argv[0][19] == '\0')
@ -375,6 +402,20 @@ main(int argc, char *argv[])
shared_heap_size = atoi(argv[0] + 19); shared_heap_size = atoi(argv[0] + 19);
} }
#endif #endif
#if WASM_ENABLE_FAST_JIT != 0
else if (!strncmp(argv[0], "--jit-codecache-size=", 21)) {
if (argv[0][21] == '\0')
return print_help();
jit_code_cache_size = atoi(argv[0] + 21);
}
#endif
#if WASM_ENABLE_GC != 0
else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
if (argv[0][15] == '\0')
return print_help();
gc_heap_size = atoi(argv[0] + 15);
}
#endif
#if WASM_ENABLE_JIT != 0 #if WASM_ENABLE_JIT != 0
else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) { else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) {
if (argv[0][22] == '\0') if (argv[0][22] == '\0')
@ -408,7 +449,8 @@ main(int argc, char *argv[])
} }
#endif #endif
#if WASM_ENABLE_MULTI_MODULE != 0 #if WASM_ENABLE_MULTI_MODULE != 0
else if (!strncmp(argv[0], MODULE_PATH, strlen(MODULE_PATH))) { else if (!strncmp(argv[0],
"--module-path=", strlen("--module-path="))) {
module_search_path = handle_module_path(argv[0]); module_search_path = handle_module_path(argv[0]);
if (!strlen(module_search_path)) { if (!strlen(module_search_path)) {
return print_help(); return print_help();
@ -440,6 +482,8 @@ main(int argc, char *argv[])
wasm_runtime_get_version(&major, &minor, &patch); wasm_runtime_get_version(&major, &minor, &patch);
printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor, printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
patch); patch);
printf("\n");
wasm_proposal_print_status();
return 0; return 0;
} }
else { else {
@ -485,6 +529,10 @@ main(int argc, char *argv[])
init_args.mem_alloc_option.allocator.free_func = free_func; init_args.mem_alloc_option.allocator.free_func = free_func;
#endif #endif
#if WASM_ENABLE_FAST_JIT != 0
init_args.fast_jit_code_cache_size = jit_code_cache_size;
#endif
#if WASM_ENABLE_GC != 0 #if WASM_ENABLE_GC != 0
init_args.gc_heap_size = gc_heap_size; init_args.gc_heap_size = gc_heap_size;
#endif #endif
@ -554,28 +602,27 @@ main(int argc, char *argv[])
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx); libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
#endif #endif
if (!wasm_runtime_instantiation_args_create(&inst_args)) {
printf("failed to create instantiate args\n");
goto fail3;
}
wasm_runtime_instantiation_args_set_default_stack_size(inst_args,
stack_size);
wasm_runtime_instantiation_args_set_host_managed_heap_size(inst_args,
heap_size);
/* instantiate the module */ /* instantiate the module */
if (!(wasm_module_inst = wasm_module_inst = wasm_runtime_instantiate_ex2(
wasm_runtime_instantiate(wasm_module, stack_size, heap_size, wasm_module, inst_args, error_buf, sizeof(error_buf));
error_buf, sizeof(error_buf)))) { wasm_runtime_instantiation_args_destroy(inst_args);
if (!wasm_module_inst) {
printf("%s\n", error_buf); printf("%s\n", error_buf);
goto fail3; goto fail3;
} }
#if WASM_ENABLE_SHARED_HEAP != 0 #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
if (shared_heap_size > 0) { if (disable_bounds_checks) {
memset(&shared_heap_init_args, 0, sizeof(shared_heap_init_args)); wasm_runtime_set_bounds_checks(wasm_module_inst, false);
shared_heap_init_args.size = shared_heap_size;
shared_heap = wasm_runtime_create_shared_heap(&shared_heap_init_args);
if (!shared_heap) {
printf("Create shared heap failed.\n");
goto fail5;
}
if (!wasm_runtime_attach_shared_heap(wasm_module_inst, shared_heap)) {
printf("Attach shared heap failed.\n");
goto fail5;
}
} }
#endif #endif
@ -596,6 +643,25 @@ main(int argc, char *argv[])
} }
#endif #endif
#if WASM_ENABLE_SHARED_HEAP != 0
if (shared_heap_size > 0) {
memset(&shared_heap_init_args, 0, sizeof(shared_heap_init_args));
shared_heap_init_args.size = shared_heap_size;
shared_heap = wasm_runtime_create_shared_heap(&shared_heap_init_args);
if (!shared_heap) {
printf("Create preallocated shared heap failed\n");
goto fail6;
}
/* attach module instance to the shared heap */
if (!wasm_runtime_attach_shared_heap(wasm_module_inst, shared_heap)) {
printf("Attach shared heap failed.\n");
goto fail6;
}
}
#endif
ret = 0; ret = 0;
const char *exception = NULL; const char *exception = NULL;
if (is_repl_mode) { if (is_repl_mode) {
@ -627,8 +693,11 @@ main(int argc, char *argv[])
printf("%s\n", exception); printf("%s\n", exception);
#if WASM_ENABLE_SHARED_HEAP != 0 #if WASM_ENABLE_SHARED_HEAP != 0
fail5: fail6:
#endif #endif
/* fail5: label is used by posix/main.c */
#if WASM_ENABLE_DEBUG_INTERP != 0 #if WASM_ENABLE_DEBUG_INTERP != 0
fail4: fail4:
#endif #endif