2021-07-29 06:38:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
2022-11-28 02:45:32 +00:00
|
|
|
#include <stdio.h>
|
2021-07-29 06:38:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "bh_platform.h"
|
|
|
|
#include "bh_read_file.h"
|
|
|
|
#include "wasm_export.h"
|
|
|
|
|
2023-08-18 02:37:41 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
|
|
|
#include "../common/libc_wasi.c"
|
|
|
|
#endif
|
|
|
|
|
2025-02-05 07:28:26 +00:00
|
|
|
#include "../common/wasm_proposal.c"
|
|
|
|
|
2022-04-28 04:31:28 +00:00
|
|
|
#if BH_HAS_DLFCN
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
static int app_argc;
|
|
|
|
static char **app_argv;
|
|
|
|
|
2021-10-14 01:12:07 +00:00
|
|
|
/* clang-format off */
|
2021-07-29 06:38:25 +00:00
|
|
|
static int
|
2024-09-10 01:42:23 +00:00
|
|
|
print_help(void)
|
2021-07-29 06:38:25 +00:00
|
|
|
{
|
|
|
|
printf("Usage: iwasm [-options] wasm_file [args...]\n");
|
|
|
|
printf("options:\n");
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" -f|--function name Specify a function name of the module to run rather\n"
|
|
|
|
" than main\n");
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_LOG != 0
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n"
|
|
|
|
" level with more log\n");
|
2023-02-02 10:16:01 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
printf(" --interp Run the wasm app with interpreter mode\n");
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
printf(" --fast-jit Run the wasm app with fast jit mode\n");
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
printf(" --llvm-jit Run the wasm app with llvm jit mode\n");
|
|
|
|
#endif
|
|
|
|
#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");
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
2022-11-24 07:50:05 +00:00
|
|
|
printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n");
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
|
2022-08-02 08:03:50 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
2022-09-22 13:46:14 +00:00
|
|
|
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);
|
2023-02-02 10:16:01 +00:00
|
|
|
#endif
|
2024-02-06 12:47:11 +00:00
|
|
|
#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
|
2023-02-02 10:16:01 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
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");
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
#if defined(os_writegsbase)
|
|
|
|
printf(" --enable-segue[=<flags>] Enable using segment register GS as the base address of\n");
|
|
|
|
printf(" linear memory, which may improve performance, flags can be:\n");
|
|
|
|
printf(" i32.load, i64.load, f32.load, f64.load, v128.load,\n");
|
|
|
|
printf(" i32.store, i64.store, f32.store, f64.store, v128.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");
|
|
|
|
#endif
|
2024-01-02 07:58:17 +00:00
|
|
|
#endif /* WASM_ENABLE_JIT != 0*/
|
|
|
|
#if WASM_ENABLE_LINUX_PERF != 0
|
|
|
|
printf(" --enable-linux-perf Enable linux perf support. It works in aot and llvm-jit.\n");
|
2022-08-02 08:03:50 +00:00
|
|
|
#endif
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
|
|
|
|
" that runs commands in the form of \"FUNC ARG...\"\n");
|
2023-11-21 09:32:45 +00:00
|
|
|
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
|
2023-07-04 08:21:30 +00:00
|
|
|
printf(" --disable-bounds-checks Disable bounds checks for memory accesses\n");
|
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
2023-08-18 02:37:41 +00:00
|
|
|
libc_wasi_print_help();
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
2022-04-28 04:31:28 +00:00
|
|
|
#if BH_HAS_DLFCN
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" --native-lib=<lib> Register native libraries to the WASM module, which\n");
|
|
|
|
printf(" are shared object (.so) files, for example:\n");
|
|
|
|
printf(" --native-lib=test1.so --native-lib=test2.so\n");
|
2022-04-27 03:12:50 +00:00
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" --module-path=<path> Indicate a module search path. default is current\n"
|
|
|
|
" directory('./')\n");
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
2023-02-07 05:46:41 +00:00
|
|
|
#if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2023-09-13 09:09:31 +00:00
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
|
|
|
printf(" --timeout=ms Set the maximum execution time in ms.\n");
|
|
|
|
printf(" If it expires, the runtime aborts the execution\n");
|
|
|
|
printf(" with a trap.\n");
|
|
|
|
#endif
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2022-09-22 13:46:14 +00:00
|
|
|
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");
|
2023-06-05 01:17:39 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_STATIC_PGO != 0
|
|
|
|
printf(" --gen-prof-file=<path> Generate LLVM PGO (Profile-Guided Optimization) profile file\n");
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
2022-09-22 13:46:14 +00:00
|
|
|
printf(" --version Show version information\n");
|
2021-07-29 06:38:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-10-14 01:12:07 +00:00
|
|
|
/* clang-format on */
|
2021-07-29 06:38:25 +00:00
|
|
|
|
2022-11-28 01:44:33 +00:00
|
|
|
static const void *
|
2021-07-29 06:38:25 +00:00
|
|
|
app_instance_main(wasm_module_inst_t module_inst)
|
|
|
|
{
|
|
|
|
const char *exception;
|
|
|
|
|
|
|
|
wasm_application_execute_main(module_inst, app_argc, app_argv);
|
2023-09-29 02:52:54 +00:00
|
|
|
exception = wasm_runtime_get_exception(module_inst);
|
2022-11-24 12:26:18 +00:00
|
|
|
return exception;
|
2021-07-29 06:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 01:44:33 +00:00
|
|
|
static const void *
|
2021-07-29 06:38:25 +00:00
|
|
|
app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
|
|
|
|
{
|
|
|
|
wasm_application_execute_func(module_inst, func_name, app_argc - 1,
|
|
|
|
app_argv + 1);
|
|
|
|
/* The result of wasm function or exception info was output inside
|
|
|
|
wasm_application_execute_func(), here we don't output them again. */
|
2022-11-24 12:26:18 +00:00
|
|
|
return wasm_runtime_get_exception(module_inst);
|
2021-07-29 06:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
* Split a string into an array of strings
|
2021-07-29 06:38:25 +00:00
|
|
|
* Returns NULL on failure
|
|
|
|
* Memory must be freed by caller
|
|
|
|
* Based on: http://stackoverflow.com/a/11198630/471795
|
|
|
|
*/
|
|
|
|
static char **
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
split_string(char *str, int *count, const char *delimer)
|
2021-07-29 06:38:25 +00:00
|
|
|
{
|
2022-06-10 13:51:13 +00:00
|
|
|
char **res = NULL, **res1;
|
2021-07-29 06:38:25 +00:00
|
|
|
char *p;
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
/* split string and append tokens to 'res' */
|
|
|
|
do {
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
p = strtok(str, delimer);
|
2021-07-29 06:38:25 +00:00
|
|
|
str = NULL;
|
2022-06-10 13:51:13 +00:00
|
|
|
res1 = res;
|
|
|
|
res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
|
2021-07-29 06:38:25 +00:00
|
|
|
if (res == NULL) {
|
2022-06-10 13:51:13 +00:00
|
|
|
free(res1);
|
2021-07-29 06:38:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res[idx++] = p;
|
|
|
|
} while (p);
|
|
|
|
|
|
|
|
/**
|
2022-06-10 13:51:13 +00:00
|
|
|
* Due to the function name,
|
|
|
|
* res[0] might contain a '\' to indicate a space
|
2021-07-29 06:38:25 +00:00
|
|
|
* func\name -> func name
|
|
|
|
*/
|
|
|
|
p = strchr(res[0], '\\');
|
|
|
|
while (p) {
|
|
|
|
*p = ' ';
|
|
|
|
p = strchr(p, '\\');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
*count = idx - 1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
app_instance_repl(wasm_module_inst_t module_inst)
|
|
|
|
{
|
|
|
|
char *cmd = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t n;
|
|
|
|
|
2022-11-28 02:45:32 +00:00
|
|
|
while ((printf("webassembly> "), fflush(stdout),
|
|
|
|
n = getline(&cmd, &len, stdin))
|
|
|
|
!= -1) {
|
2021-07-29 06:38:25 +00:00
|
|
|
bh_assert(n > 0);
|
|
|
|
if (cmd[n - 1] == '\n') {
|
|
|
|
if (n == 1)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
cmd[n - 1] = '\0';
|
|
|
|
}
|
|
|
|
if (!strcmp(cmd, "__exit__")) {
|
|
|
|
printf("exit repl mode\n");
|
|
|
|
break;
|
|
|
|
}
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
app_argv = split_string(cmd, &app_argc, " ");
|
2021-07-29 06:38:25 +00:00
|
|
|
if (app_argv == NULL) {
|
|
|
|
LOG_ERROR("Wasm prepare param failed: split string failed.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (app_argc != 0) {
|
2023-11-09 00:42:05 +00:00
|
|
|
const char *exception;
|
2021-07-29 06:38:25 +00:00
|
|
|
wasm_application_execute_func(module_inst, app_argv[0],
|
|
|
|
app_argc - 1, app_argv + 1);
|
2023-11-09 00:42:05 +00:00
|
|
|
if ((exception = wasm_runtime_get_exception(module_inst)))
|
|
|
|
printf("%s\n", exception);
|
2021-07-29 06:38:25 +00:00
|
|
|
}
|
|
|
|
free(app_argv);
|
|
|
|
}
|
|
|
|
free(cmd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
static uint32
|
|
|
|
resolve_segue_flags(char *str_flags)
|
|
|
|
{
|
|
|
|
uint32 segue_flags = 0;
|
|
|
|
int32 flag_count, i;
|
|
|
|
char **flag_list;
|
|
|
|
|
|
|
|
flag_list = split_string(str_flags, &flag_count, ",");
|
|
|
|
if (flag_list) {
|
|
|
|
for (i = 0; i < flag_count; i++) {
|
|
|
|
if (!strcmp(flag_list[i], "i32.load")) {
|
|
|
|
segue_flags |= 1 << 0;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "i64.load")) {
|
|
|
|
segue_flags |= 1 << 1;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "f32.load")) {
|
|
|
|
segue_flags |= 1 << 2;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "f64.load")) {
|
|
|
|
segue_flags |= 1 << 3;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "v128.load")) {
|
|
|
|
segue_flags |= 1 << 4;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "i32.store")) {
|
|
|
|
segue_flags |= 1 << 8;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "i64.store")) {
|
|
|
|
segue_flags |= 1 << 9;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "f32.store")) {
|
|
|
|
segue_flags |= 1 << 10;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "f64.store")) {
|
|
|
|
segue_flags |= 1 << 11;
|
|
|
|
}
|
|
|
|
else if (!strcmp(flag_list[i], "v128.store")) {
|
|
|
|
segue_flags |= 1 << 12;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* invalid flag */
|
|
|
|
segue_flags = (uint32)-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(flag_list);
|
|
|
|
}
|
|
|
|
return segue_flags;
|
|
|
|
}
|
|
|
|
#endif /* end of WASM_ENABLE_JIT != 0 */
|
|
|
|
|
2022-04-28 04:31:28 +00:00
|
|
|
#if BH_HAS_DLFCN
|
2023-08-14 06:05:59 +00:00
|
|
|
struct native_lib {
|
|
|
|
void *handle;
|
|
|
|
|
|
|
|
uint32 (*get_native_lib)(char **p_module_name,
|
|
|
|
NativeSymbol **p_native_symbols);
|
|
|
|
int (*init_native_lib)(void);
|
|
|
|
void (*deinit_native_lib)(void);
|
2022-04-27 03:12:50 +00:00
|
|
|
|
|
|
|
char *module_name;
|
2023-08-14 06:05:59 +00:00
|
|
|
NativeSymbol *native_symbols;
|
|
|
|
uint32 n_native_symbols;
|
|
|
|
};
|
2022-04-27 03:12:50 +00:00
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
struct native_lib *
|
|
|
|
load_native_lib(const char *name)
|
|
|
|
{
|
|
|
|
struct native_lib *lib = wasm_runtime_malloc(sizeof(*lib));
|
|
|
|
if (lib == NULL) {
|
|
|
|
LOG_WARNING("warning: failed to load native library %s because of "
|
|
|
|
"allocation failure",
|
|
|
|
name);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
memset(lib, 0, sizeof(*lib));
|
2022-04-27 03:12:50 +00:00
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
/* open the native library */
|
|
|
|
if (!(lib->handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL))
|
|
|
|
&& !(lib->handle = dlopen(name, RTLD_LAZY))) {
|
2024-06-14 04:06:56 +00:00
|
|
|
LOG_WARNING("warning: failed to load native library %s. %s", name,
|
|
|
|
dlerror());
|
2023-08-14 06:05:59 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib->init_native_lib = dlsym(lib->handle, "init_native_lib");
|
|
|
|
lib->get_native_lib = dlsym(lib->handle, "get_native_lib");
|
|
|
|
lib->deinit_native_lib = dlsym(lib->handle, "deinit_native_lib");
|
|
|
|
|
|
|
|
if (!lib->get_native_lib) {
|
|
|
|
LOG_WARNING("warning: failed to lookup `get_native_lib` function "
|
|
|
|
"from native lib %s",
|
|
|
|
name);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lib->init_native_lib) {
|
|
|
|
int ret = lib->init_native_lib();
|
|
|
|
if (ret != 0) {
|
|
|
|
LOG_WARNING("warning: `init_native_lib` function from native "
|
|
|
|
"lib %s failed with %d",
|
2023-08-17 02:54:02 +00:00
|
|
|
name, ret);
|
2023-08-14 06:05:59 +00:00
|
|
|
goto fail;
|
2022-04-27 03:12:50 +00:00
|
|
|
}
|
2023-08-14 06:05:59 +00:00
|
|
|
}
|
2022-04-27 03:12:50 +00:00
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
lib->n_native_symbols =
|
|
|
|
lib->get_native_lib(&lib->module_name, &lib->native_symbols);
|
|
|
|
|
|
|
|
/* register native symbols */
|
|
|
|
if (!(lib->n_native_symbols > 0 && lib->module_name && lib->native_symbols
|
|
|
|
&& wasm_runtime_register_natives(
|
|
|
|
lib->module_name, lib->native_symbols, lib->n_native_symbols))) {
|
|
|
|
LOG_WARNING("warning: failed to register native lib %s", name);
|
|
|
|
if (lib->deinit_native_lib) {
|
|
|
|
lib->deinit_native_lib();
|
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return lib;
|
|
|
|
fail:
|
|
|
|
if (lib != NULL) {
|
|
|
|
if (lib->handle != NULL) {
|
|
|
|
dlclose(lib->handle);
|
2022-04-27 03:12:50 +00:00
|
|
|
}
|
2023-08-14 06:05:59 +00:00
|
|
|
wasm_runtime_free(lib);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-04-27 03:12:50 +00:00
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
static uint32
|
|
|
|
load_and_register_native_libs(const char **native_lib_list,
|
|
|
|
uint32 native_lib_count,
|
|
|
|
struct native_lib **native_lib_loaded_list)
|
|
|
|
{
|
|
|
|
uint32 i, native_lib_loaded_count = 0;
|
2022-04-27 03:12:50 +00:00
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
for (i = 0; i < native_lib_count; i++) {
|
|
|
|
struct native_lib *lib = load_native_lib(native_lib_list[i]);
|
|
|
|
if (lib == NULL) {
|
2022-04-27 03:12:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-14 06:05:59 +00:00
|
|
|
native_lib_loaded_list[native_lib_loaded_count++] = lib;
|
2022-04-27 03:12:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
return native_lib_loaded_count;
|
2022-04-27 03:12:50 +00:00
|
|
|
}
|
2022-10-28 03:03:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
unregister_and_unload_native_libs(uint32 native_lib_count,
|
2023-08-14 06:05:59 +00:00
|
|
|
struct native_lib **native_lib_loaded_list)
|
2022-10-28 03:03:39 +00:00
|
|
|
{
|
2023-08-14 06:05:59 +00:00
|
|
|
uint32 i;
|
2022-10-28 03:03:39 +00:00
|
|
|
|
|
|
|
for (i = 0; i < native_lib_count; i++) {
|
2023-08-14 06:05:59 +00:00
|
|
|
struct native_lib *lib = native_lib_loaded_list[i];
|
2022-10-28 03:03:39 +00:00
|
|
|
|
|
|
|
/* unregister native symbols */
|
2023-08-14 06:05:59 +00:00
|
|
|
if (!wasm_runtime_unregister_natives(lib->module_name,
|
|
|
|
lib->native_symbols)) {
|
|
|
|
LOG_WARNING("warning: failed to unregister native lib %p",
|
|
|
|
lib->handle);
|
2022-10-28 03:03:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
if (lib->deinit_native_lib) {
|
|
|
|
lib->deinit_native_lib();
|
2022-10-28 03:03:39 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:05:59 +00:00
|
|
|
dlclose(lib->handle);
|
|
|
|
wasm_runtime_free(lib);
|
2022-10-28 03:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 04:31:28 +00:00
|
|
|
#endif /* BH_HAS_DLFCN */
|
2021-07-29 06:38:25 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
static char *
|
|
|
|
handle_module_path(const char *module_path)
|
|
|
|
{
|
|
|
|
/* next character after = */
|
|
|
|
return (strchr(module_path, '=')) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *module_search_path = ".";
|
|
|
|
|
|
|
|
static bool
|
2023-09-28 00:56:11 +00:00
|
|
|
module_reader_callback(package_type_t module_type, const char *module_name,
|
|
|
|
uint8 **p_buffer, uint32 *p_size)
|
2021-07-29 06:38:25 +00:00
|
|
|
{
|
2023-10-08 00:36:49 +00:00
|
|
|
char *file_format = NULL;
|
2023-09-28 00:56:11 +00:00
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
if (module_type == Wasm_Module_Bytecode)
|
|
|
|
file_format = ".wasm";
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_AOT != 0
|
|
|
|
if (module_type == Wasm_Module_AoT)
|
|
|
|
file_format = ".aot";
|
|
|
|
#endif
|
2023-10-08 00:36:49 +00:00
|
|
|
bh_assert(file_format);
|
2023-09-28 00:56:11 +00:00
|
|
|
const char *format = "%s/%s%s";
|
2021-10-14 01:12:07 +00:00
|
|
|
int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
|
2023-09-28 00:56:11 +00:00
|
|
|
+ strlen(file_format) + 1;
|
2023-10-31 02:48:51 +00:00
|
|
|
char *wasm_file_name = wasm_runtime_malloc(sz);
|
2021-07-29 06:38:25 +00:00
|
|
|
if (!wasm_file_name) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-28 00:56:11 +00:00
|
|
|
snprintf(wasm_file_name, sz, format, module_search_path, module_name,
|
|
|
|
file_format);
|
2021-07-29 06:38:25 +00:00
|
|
|
*p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_name, p_size);
|
|
|
|
|
|
|
|
wasm_runtime_free(wasm_file_name);
|
|
|
|
return *p_buffer != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-05-05 23:56:48 +00:00
|
|
|
module_destroyer_callback(uint8 *buffer, uint32 size)
|
2021-07-29 06:38:25 +00:00
|
|
|
{
|
|
|
|
if (!buffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wasm_runtime_free(buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
#endif /* WASM_ENABLE_MULTI_MODULE */
|
|
|
|
|
2022-04-27 03:12:50 +00:00
|
|
|
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
|
2022-09-01 08:15:00 +00:00
|
|
|
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
|
2023-11-03 07:23:31 +00:00
|
|
|
#else
|
|
|
|
static void *
|
|
|
|
malloc_func(
|
2024-04-18 11:40:57 +00:00
|
|
|
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
|
|
|
mem_alloc_usage_t usage,
|
|
|
|
#endif
|
2023-11-03 07:23:31 +00:00
|
|
|
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
|
|
|
void *user_data,
|
2022-04-27 03:12:50 +00:00
|
|
|
#endif
|
2023-11-03 07:23:31 +00:00
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
realloc_func(
|
2024-04-18 11:40:57 +00:00
|
|
|
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
|
|
|
mem_alloc_usage_t usage, bool full_size_mmaped,
|
|
|
|
#endif
|
2023-11-03 07:23:31 +00:00
|
|
|
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
|
|
|
void *user_data,
|
|
|
|
#endif
|
|
|
|
void *ptr, unsigned int size)
|
|
|
|
{
|
|
|
|
return realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_func(
|
2024-04-18 11:40:57 +00:00
|
|
|
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
|
|
|
mem_alloc_usage_t usage,
|
|
|
|
#endif
|
2023-11-03 07:23:31 +00:00
|
|
|
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
|
|
|
void *user_data,
|
|
|
|
#endif
|
|
|
|
void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
#endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */
|
2022-04-27 03:12:50 +00:00
|
|
|
|
2023-06-05 01:17:39 +00:00
|
|
|
#if WASM_ENABLE_STATIC_PGO != 0
|
|
|
|
static void
|
|
|
|
dump_pgo_prof_data(wasm_module_inst_t module_inst, const char *path)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
uint32 len;
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
if (!(len = wasm_runtime_get_pgo_prof_data_size(module_inst))) {
|
|
|
|
printf("failed to get LLVM PGO profile data size\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(buf = wasm_runtime_malloc(len))) {
|
|
|
|
printf("allocate memory failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != wasm_runtime_dump_pgo_prof_data_to_buf(module_inst, buf, len)) {
|
|
|
|
printf("failed to dump LLVM PGO profile data\n");
|
|
|
|
wasm_runtime_free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(file = fopen(path, "wb"))) {
|
|
|
|
printf("failed to create file %s", path);
|
|
|
|
wasm_runtime_free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fwrite(buf, len, 1, file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
wasm_runtime_free(buf);
|
|
|
|
|
|
|
|
printf("LLVM raw profile file %s was generated.\n", path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-13 09:09:31 +00:00
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
|
|
|
struct timeout_arg {
|
|
|
|
uint32 timeout_ms;
|
|
|
|
wasm_module_inst_t inst;
|
2023-09-15 08:24:12 +00:00
|
|
|
#if defined(BH_HAS_STD_ATOMIC)
|
|
|
|
_Atomic
|
|
|
|
#endif
|
|
|
|
bool cancel;
|
2023-09-13 09:09:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void *
|
|
|
|
timeout_thread(void *vp)
|
|
|
|
{
|
|
|
|
const struct timeout_arg *arg = vp;
|
2024-05-30 07:55:24 +00:00
|
|
|
const uint64 end_time =
|
|
|
|
os_time_get_boot_us() + (uint64)arg->timeout_ms * 1000;
|
2023-09-13 09:09:31 +00:00
|
|
|
while (!arg->cancel) {
|
2024-05-30 07:55:24 +00:00
|
|
|
const uint64 now = os_time_get_boot_us();
|
|
|
|
if ((int64)(now - end_time) > 0) {
|
2023-09-13 09:09:31 +00:00
|
|
|
wasm_runtime_terminate(arg->inst);
|
|
|
|
break;
|
|
|
|
}
|
2024-05-30 07:55:24 +00:00
|
|
|
const uint64 left_us = end_time - now;
|
|
|
|
uint32 us;
|
|
|
|
if (left_us >= 100 * 1000) {
|
|
|
|
us = 100 * 1000;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
us = left_us;
|
|
|
|
}
|
|
|
|
os_usleep(us);
|
2023-09-13 09:09:31 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2022-08-12 10:03:14 +00:00
|
|
|
int32 ret = -1;
|
2021-07-29 06:38:25 +00:00
|
|
|
char *wasm_file = NULL;
|
|
|
|
const char *func_name = NULL;
|
|
|
|
uint8 *wasm_file_buf = NULL;
|
|
|
|
uint32 wasm_file_size;
|
2023-07-07 08:09:15 +00:00
|
|
|
uint32 stack_size = 64 * 1024;
|
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
|
|
|
uint32 heap_size = 0;
|
|
|
|
#else
|
|
|
|
uint32 heap_size = 16 * 1024;
|
|
|
|
#endif
|
2022-08-02 08:03:50 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE;
|
2023-02-02 10:16:01 +00:00
|
|
|
#endif
|
2024-02-06 12:47:11 +00:00
|
|
|
#if WASM_ENABLE_GC != 0
|
|
|
|
uint32 gc_heap_size = GC_HEAP_SIZE_DEFAULT;
|
|
|
|
#endif
|
2023-02-02 10:16:01 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
uint32 llvm_jit_size_level = 3;
|
|
|
|
uint32 llvm_jit_opt_level = 3;
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
uint32 segue_flags = 0;
|
2024-01-02 07:58:17 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_LINUX_PERF != 0
|
|
|
|
bool enable_linux_perf = false;
|
2022-08-02 08:03:50 +00:00
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
wasm_module_t wasm_module = NULL;
|
|
|
|
wasm_module_inst_t wasm_module_inst = NULL;
|
2023-02-02 10:16:01 +00:00
|
|
|
RunningMode running_mode = 0;
|
2021-07-29 06:38:25 +00:00
|
|
|
RuntimeInitArgs init_args;
|
|
|
|
char error_buf[128] = { 0 };
|
|
|
|
#if WASM_ENABLE_LOG != 0
|
|
|
|
int log_verbose_level = 2;
|
|
|
|
#endif
|
|
|
|
bool is_repl_mode = false;
|
2021-12-06 09:25:10 +00:00
|
|
|
bool is_xip_file = false;
|
2023-11-21 09:32:45 +00:00
|
|
|
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
|
2023-07-04 08:21:30 +00:00
|
|
|
bool disable_bounds_checks = false;
|
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
2023-08-18 02:37:41 +00:00
|
|
|
libc_wasi_parse_context_t wasi_parse_ctx;
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
2022-04-28 04:31:28 +00:00
|
|
|
#if BH_HAS_DLFCN
|
2022-04-27 03:12:50 +00:00
|
|
|
const char *native_lib_list[8] = { NULL };
|
|
|
|
uint32 native_lib_count = 0;
|
2023-08-14 06:05:59 +00:00
|
|
|
struct native_lib *native_lib_loaded_list[8];
|
|
|
|
uint32 native_lib_loaded_count = 0;
|
2022-04-27 03:12:50 +00:00
|
|
|
#endif
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2021-10-14 01:12:07 +00:00
|
|
|
char *ip_addr = NULL;
|
2021-09-29 05:36:46 +00:00
|
|
|
int instance_port = 0;
|
|
|
|
#endif
|
2023-06-05 01:17:39 +00:00
|
|
|
#if WASM_ENABLE_STATIC_PGO != 0
|
|
|
|
const char *gen_prof_file = NULL;
|
|
|
|
#endif
|
2023-09-13 09:09:31 +00:00
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
|
|
|
int timeout_ms = -1;
|
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
|
2023-08-18 02:37:41 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
|
|
|
memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
|
|
|
|
#endif
|
|
|
|
|
2021-10-14 01:12:07 +00:00
|
|
|
/* Process options. */
|
2021-07-29 06:38:25 +00:00
|
|
|
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
|
|
|
|
if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
|
|
|
|
argc--, argv++;
|
|
|
|
if (argc < 2) {
|
2022-08-12 10:03:14 +00:00
|
|
|
return print_help();
|
2021-07-29 06:38:25 +00:00
|
|
|
}
|
|
|
|
func_name = argv[0];
|
|
|
|
}
|
2023-02-02 10:16:01 +00:00
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
else if (!strcmp(argv[0], "--interp")) {
|
|
|
|
running_mode = Mode_Interp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
else if (!strcmp(argv[0], "--fast-jit")) {
|
|
|
|
running_mode = Mode_Fast_JIT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
else if (!strcmp(argv[0], "--llvm-jit")) {
|
|
|
|
running_mode = Mode_LLVM_JIT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
|
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
else if (!strcmp(argv[0], "--multi-tier-jit")) {
|
|
|
|
running_mode = Mode_Multi_Tier_JIT;
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_LOG != 0
|
|
|
|
else if (!strncmp(argv[0], "-v=", 3)) {
|
|
|
|
log_verbose_level = atoi(argv[0] + 3);
|
|
|
|
if (log_verbose_level < 0 || log_verbose_level > 5)
|
|
|
|
return print_help();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else if (!strcmp(argv[0], "--repl")) {
|
|
|
|
is_repl_mode = true;
|
|
|
|
}
|
2023-11-21 09:32:45 +00:00
|
|
|
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
|
2023-07-04 08:21:30 +00:00
|
|
|
else if (!strcmp(argv[0], "--disable-bounds-checks")) {
|
|
|
|
disable_bounds_checks = true;
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
else if (!strncmp(argv[0], "--stack-size=", 13)) {
|
|
|
|
if (argv[0][13] == '\0')
|
|
|
|
return print_help();
|
|
|
|
stack_size = atoi(argv[0] + 13);
|
|
|
|
}
|
|
|
|
else if (!strncmp(argv[0], "--heap-size=", 12)) {
|
|
|
|
if (argv[0][12] == '\0')
|
|
|
|
return print_help();
|
|
|
|
heap_size = atoi(argv[0] + 12);
|
|
|
|
}
|
2022-08-02 08:03:50 +00:00
|
|
|
#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
|
2024-02-06 12:47:11 +00:00
|
|
|
#if WASM_ENABLE_GC != 0
|
|
|
|
else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
|
2024-04-08 01:06:49 +00:00
|
|
|
if (argv[0][15] == '\0')
|
2024-02-06 12:47:11 +00:00
|
|
|
return print_help();
|
|
|
|
gc_heap_size = atoi(argv[0] + 15);
|
|
|
|
}
|
|
|
|
#endif
|
2023-02-02 10:16:01 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) {
|
|
|
|
if (argv[0][22] == '\0')
|
|
|
|
return print_help();
|
|
|
|
llvm_jit_size_level = atoi(argv[0] + 22);
|
|
|
|
if (llvm_jit_size_level < 1) {
|
|
|
|
printf("LLVM JIT size level shouldn't be smaller than 1, "
|
|
|
|
"setting it to 1\n");
|
|
|
|
llvm_jit_size_level = 1;
|
|
|
|
}
|
|
|
|
else if (llvm_jit_size_level > 3) {
|
|
|
|
printf("LLVM JIT size level shouldn't be greater than 3, "
|
|
|
|
"setting it to 3\n");
|
|
|
|
llvm_jit_size_level = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strncmp(argv[0], "--llvm-jit-opt-level=", 21)) {
|
|
|
|
if (argv[0][21] == '\0')
|
|
|
|
return print_help();
|
|
|
|
llvm_jit_opt_level = atoi(argv[0] + 21);
|
|
|
|
if (llvm_jit_opt_level < 1) {
|
|
|
|
printf("LLVM JIT opt level shouldn't be smaller than 1, "
|
|
|
|
"setting it to 1\n");
|
|
|
|
llvm_jit_opt_level = 1;
|
|
|
|
}
|
|
|
|
else if (llvm_jit_opt_level > 3) {
|
|
|
|
printf("LLVM JIT opt level shouldn't be greater than 3, "
|
|
|
|
"setting it to 3\n");
|
|
|
|
llvm_jit_opt_level = 3;
|
|
|
|
}
|
|
|
|
}
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
else if (!strcmp(argv[0], "--enable-segue")) {
|
|
|
|
/* all flags are enabled */
|
|
|
|
segue_flags = 0x1F1F;
|
|
|
|
}
|
|
|
|
else if (!strncmp(argv[0], "--enable-segue=", 15)) {
|
|
|
|
segue_flags = resolve_segue_flags(argv[0] + 15);
|
|
|
|
if (segue_flags == (uint32)-1)
|
|
|
|
return print_help();
|
|
|
|
}
|
|
|
|
#endif /* end of WASM_ENABLE_JIT != 0 */
|
2022-04-28 04:31:28 +00:00
|
|
|
#if BH_HAS_DLFCN
|
2022-04-27 03:12:50 +00:00
|
|
|
else if (!strncmp(argv[0], "--native-lib=", 13)) {
|
|
|
|
if (argv[0][13] == '\0')
|
|
|
|
return print_help();
|
|
|
|
if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
|
|
|
|
printf("Only allow max native lib number %d\n",
|
|
|
|
(int)(sizeof(native_lib_list) / sizeof(char *)));
|
2022-08-12 10:03:14 +00:00
|
|
|
return 1;
|
2022-04-27 03:12:50 +00:00
|
|
|
}
|
|
|
|
native_lib_list[native_lib_count++] = argv[0] + 13;
|
|
|
|
}
|
|
|
|
#endif
|
2024-01-02 07:58:17 +00:00
|
|
|
#if WASM_ENABLE_LINUX_PERF != 0
|
|
|
|
else if (!strncmp(argv[0], "--enable-linux-perf", 19)) {
|
|
|
|
enable_linux_perf = true;
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2022-03-10 07:13:38 +00:00
|
|
|
else if (!strncmp(argv[0],
|
|
|
|
"--module-path=", strlen("--module-path="))) {
|
2021-07-29 06:38:25 +00:00
|
|
|
module_search_path = handle_module_path(argv[0]);
|
|
|
|
if (!strlen(module_search_path)) {
|
|
|
|
return print_help();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2023-02-07 05:46:41 +00:00
|
|
|
#if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
|
2021-07-29 06:38:25 +00:00
|
|
|
else if (!strncmp(argv[0], "--max-threads=", 14)) {
|
|
|
|
if (argv[0][14] == '\0')
|
|
|
|
return print_help();
|
|
|
|
wasm_runtime_set_max_thread_num(atoi(argv[0] + 14));
|
|
|
|
}
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2023-09-13 09:09:31 +00:00
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
|
|
|
else if (!strncmp(argv[0], "--timeout=", 10)) {
|
|
|
|
if (argv[0][10] == '\0')
|
|
|
|
return print_help();
|
|
|
|
timeout_ms = atoi(argv[0] + 10);
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
|
else if (!strncmp(argv[0], "-g=", 3)) {
|
2021-10-14 01:12:07 +00:00
|
|
|
char *port_str = strchr(argv[0] + 3, ':');
|
2021-09-29 05:36:46 +00:00
|
|
|
char *port_end;
|
|
|
|
if (port_str == NULL)
|
|
|
|
return print_help();
|
|
|
|
*port_str = '\0';
|
|
|
|
instance_port = strtoul(port_str + 1, &port_end, 10);
|
|
|
|
if (port_str[1] == '\0' || *port_end != '\0')
|
|
|
|
return print_help();
|
|
|
|
ip_addr = argv[0] + 3;
|
|
|
|
}
|
2023-06-05 01:17:39 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_STATIC_PGO != 0
|
|
|
|
else if (!strncmp(argv[0], "--gen-prof-file=", 16)) {
|
|
|
|
if (argv[0][16] == '\0')
|
|
|
|
return print_help();
|
|
|
|
gen_prof_file = argv[0] + 16;
|
|
|
|
}
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
2024-01-10 10:37:02 +00:00
|
|
|
else if (!strcmp(argv[0], "--version")) {
|
Enable the semantic version mechanism for WAMR (#1374)
Use the semantic versioning (https://semver.org) to replace the current date
versioning system, which is more general and is requested by some developers,
e.g. issue #1357.
There are three parts in the new version string:
- major. Any incompatible modification on ABIs and APIs will lead to an increment
in the value of major, which mainly includes: AOT calling conventions, AOT file
format, wasm_export.h, wasm_c_api.h, and so on.
- minor. It represents new features, including MVP/POST-MVP features, libraries,
WAMR private ones, and so one.
- patch. It represents patches.
The new version will start from 1.0.0. Update the help info and version showing for
iwasm and wamrc.
2022-08-18 11:01:05 +00:00
|
|
|
uint32 major, minor, patch;
|
|
|
|
wasm_runtime_get_version(&major, &minor, &patch);
|
2022-09-01 06:11:00 +00:00
|
|
|
printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
|
|
|
|
patch);
|
2025-02-05 07:28:26 +00:00
|
|
|
printf("\n");
|
|
|
|
wasm_proposal_print_status();
|
Enable the semantic version mechanism for WAMR (#1374)
Use the semantic versioning (https://semver.org) to replace the current date
versioning system, which is more general and is requested by some developers,
e.g. issue #1357.
There are three parts in the new version string:
- major. Any incompatible modification on ABIs and APIs will lead to an increment
in the value of major, which mainly includes: AOT calling conventions, AOT file
format, wasm_export.h, wasm_c_api.h, and so on.
- minor. It represents new features, including MVP/POST-MVP features, libraries,
WAMR private ones, and so one.
- patch. It represents patches.
The new version will start from 1.0.0. Update the help info and version showing for
iwasm and wamrc.
2022-08-18 11:01:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2023-08-18 02:37:41 +00:00
|
|
|
else {
|
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
|
|
|
libc_wasi_parse_result_t result =
|
|
|
|
libc_wasi_parse(argv[0], &wasi_parse_ctx);
|
|
|
|
switch (result) {
|
|
|
|
case LIBC_WASI_PARSE_RESULT_OK:
|
|
|
|
continue;
|
|
|
|
case LIBC_WASI_PARSE_RESULT_NEED_HELP:
|
|
|
|
return print_help();
|
|
|
|
case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
2021-07-29 06:38:25 +00:00
|
|
|
return print_help();
|
2023-08-18 02:37:41 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-07-29 06:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return print_help();
|
|
|
|
|
|
|
|
wasm_file = argv[0];
|
|
|
|
app_argc = argc;
|
|
|
|
app_argv = argv;
|
|
|
|
|
|
|
|
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
|
|
|
|
2023-02-02 10:16:01 +00:00
|
|
|
init_args.running_mode = running_mode;
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
|
|
|
|
init_args.mem_alloc_type = Alloc_With_Pool;
|
|
|
|
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
|
|
|
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
|
|
|
#else
|
|
|
|
init_args.mem_alloc_type = Alloc_With_Allocator;
|
2023-11-03 07:23:31 +00:00
|
|
|
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
|
|
|
/* Set user data for the allocator is needed */
|
|
|
|
/* init_args.mem_alloc_option.allocator.user_data = user_data; */
|
|
|
|
#endif
|
|
|
|
init_args.mem_alloc_option.allocator.malloc_func = malloc_func;
|
|
|
|
init_args.mem_alloc_option.allocator.realloc_func = realloc_func;
|
|
|
|
init_args.mem_alloc_option.allocator.free_func = free_func;
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-02 08:03:50 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
init_args.fast_jit_code_cache_size = jit_code_cache_size;
|
|
|
|
#endif
|
|
|
|
|
2024-02-06 12:47:11 +00:00
|
|
|
#if WASM_ENABLE_GC != 0
|
|
|
|
init_args.gc_heap_size = gc_heap_size;
|
|
|
|
#endif
|
|
|
|
|
2023-02-02 10:16:01 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
init_args.llvm_jit_size_level = llvm_jit_size_level;
|
|
|
|
init_args.llvm_jit_opt_level = llvm_jit_opt_level;
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
init_args.segue_flags = segue_flags;
|
2024-01-02 07:58:17 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_LINUX_PERF != 0
|
|
|
|
init_args.enable_linux_perf = enable_linux_perf;
|
2023-02-02 10:16:01 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
|
init_args.instance_port = instance_port;
|
|
|
|
if (ip_addr)
|
2024-04-07 03:57:31 +00:00
|
|
|
/* ensure that init_args.ip_addr is null terminated */
|
|
|
|
strncpy(init_args.ip_addr, ip_addr, sizeof(init_args.ip_addr) - 1);
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
/* initialize runtime environment */
|
|
|
|
if (!wasm_runtime_full_init(&init_args)) {
|
|
|
|
printf("Init runtime environment failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_LOG != 0
|
|
|
|
bh_log_set_verbose_level(log_verbose_level);
|
|
|
|
#endif
|
|
|
|
|
2022-04-28 04:31:28 +00:00
|
|
|
#if BH_HAS_DLFCN
|
2023-08-14 06:05:59 +00:00
|
|
|
native_lib_loaded_count = load_and_register_native_libs(
|
|
|
|
native_lib_list, native_lib_count, native_lib_loaded_list);
|
2022-04-27 03:12:50 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
/* load WASM byte buffer from WASM bin file */
|
|
|
|
if (!(wasm_file_buf =
|
2021-10-14 01:12:07 +00:00
|
|
|
(uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
|
2021-07-29 06:38:25 +00:00
|
|
|
goto fail1;
|
|
|
|
|
2021-12-06 09:25:10 +00:00
|
|
|
#if WASM_ENABLE_AOT != 0
|
|
|
|
if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) {
|
2021-08-12 09:44:39 +00:00
|
|
|
uint8 *wasm_file_mapped;
|
2024-05-14 04:16:19 +00:00
|
|
|
uint8 *daddr;
|
2021-08-12 09:44:39 +00:00
|
|
|
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
|
2021-12-06 09:25:10 +00:00
|
|
|
int map_flags = MMAP_MAP_32BIT;
|
2021-08-12 09:44:39 +00:00
|
|
|
|
2023-11-16 00:28:54 +00:00
|
|
|
if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot,
|
|
|
|
map_flags, os_get_invalid_handle()))) {
|
2021-08-12 09:44:39 +00:00
|
|
|
printf("mmap memory failed\n");
|
|
|
|
wasm_runtime_free(wasm_file_buf);
|
|
|
|
goto fail1;
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:16:19 +00:00
|
|
|
#if (WASM_MEM_DUAL_BUS_MIRROR != 0)
|
|
|
|
daddr = os_get_dbus_mirror(wasm_file_mapped);
|
|
|
|
#else
|
|
|
|
daddr = wasm_file_mapped;
|
|
|
|
#endif
|
|
|
|
bh_memcpy_s(daddr, wasm_file_size, wasm_file_buf, wasm_file_size);
|
|
|
|
#if (WASM_MEM_DUAL_BUS_MIRROR != 0)
|
|
|
|
os_dcache_flush();
|
|
|
|
#endif
|
2021-08-12 09:44:39 +00:00
|
|
|
wasm_runtime_free(wasm_file_buf);
|
|
|
|
wasm_file_buf = wasm_file_mapped;
|
2021-12-06 09:25:10 +00:00
|
|
|
is_xip_file = true;
|
2021-08-12 09:44:39 +00:00
|
|
|
}
|
2021-12-06 09:25:10 +00:00
|
|
|
#endif
|
2021-08-12 09:44:39 +00:00
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2024-05-05 23:56:48 +00:00
|
|
|
wasm_runtime_set_module_reader(module_reader_callback,
|
|
|
|
module_destroyer_callback);
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* load WASM module */
|
|
|
|
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
|
|
|
error_buf, sizeof(error_buf)))) {
|
|
|
|
printf("%s\n", error_buf);
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
2024-09-18 03:02:10 +00:00
|
|
|
#if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
|
|
|
|
if (!wasm_runtime_set_module_name(wasm_module, wasm_file, error_buf,
|
|
|
|
sizeof(error_buf))) {
|
|
|
|
printf("set aot module name failed in dynamic aot debug mode, %s\n",
|
|
|
|
error_buf);
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
2023-08-18 02:37:41 +00:00
|
|
|
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
|
2021-07-29 06:38:25 +00:00
|
|
|
#endif
|
|
|
|
|
2025-02-10 06:15:47 +00:00
|
|
|
InstantiationArgs args = {
|
|
|
|
.default_stack_size = stack_size,
|
|
|
|
.host_managed_heap_size = heap_size,
|
|
|
|
.max_memory_pages = 0,
|
|
|
|
.run_start_function = false,
|
|
|
|
};
|
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
/* instantiate the module */
|
2025-02-10 06:15:47 +00:00
|
|
|
if (!(wasm_module_inst = wasm_runtime_instantiate_ex(
|
|
|
|
wasm_module, &args, error_buf, sizeof(error_buf)))) {
|
|
|
|
printf("%s\n", error_buf);
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
|
|
|
struct timeout_arg timeout_arg;
|
|
|
|
korp_tid timeout_tid;
|
|
|
|
if (timeout_ms >= 0) {
|
|
|
|
timeout_arg.timeout_ms = timeout_ms;
|
|
|
|
timeout_arg.inst = wasm_module_inst;
|
|
|
|
timeout_arg.cancel = false;
|
|
|
|
ret = os_thread_create(&timeout_tid, timeout_thread, &timeout_arg,
|
|
|
|
APP_THREAD_STACK_SIZE_DEFAULT);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf("Failed to start timeout\n");
|
|
|
|
goto fail4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!wasm_runtime_instantiate_run_start_func(
|
|
|
|
wasm_module_inst, NULL, NULL, error_buf, sizeof(error_buf))) {
|
2021-07-29 06:38:25 +00:00
|
|
|
printf("%s\n", error_buf);
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
2023-11-21 09:32:45 +00:00
|
|
|
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
|
2023-07-04 08:21:30 +00:00
|
|
|
if (disable_bounds_checks) {
|
|
|
|
wasm_runtime_set_bounds_checks(wasm_module_inst, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-08-24 12:50:26 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
|
if (ip_addr != NULL) {
|
|
|
|
wasm_exec_env_t exec_env =
|
|
|
|
wasm_runtime_get_exec_env_singleton(wasm_module_inst);
|
|
|
|
uint32_t debug_port;
|
|
|
|
if (exec_env == NULL) {
|
|
|
|
printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
|
2025-02-10 06:15:47 +00:00
|
|
|
goto fail5;
|
2022-08-24 12:50:26 +00:00
|
|
|
}
|
|
|
|
debug_port = wasm_runtime_start_debug_instance(exec_env);
|
|
|
|
if (debug_port == 0) {
|
|
|
|
printf("Failed to start debug instance\n");
|
2023-09-13 09:09:31 +00:00
|
|
|
goto fail5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-24 12:26:18 +00:00
|
|
|
ret = 0;
|
2023-09-29 02:52:54 +00:00
|
|
|
const char *exception = NULL;
|
2022-11-24 12:26:18 +00:00
|
|
|
if (is_repl_mode) {
|
2021-07-29 06:38:25 +00:00
|
|
|
app_instance_repl(wasm_module_inst);
|
2022-11-24 12:26:18 +00:00
|
|
|
}
|
|
|
|
else if (func_name) {
|
2023-09-29 02:52:54 +00:00
|
|
|
exception = app_instance_func(wasm_module_inst, func_name);
|
|
|
|
if (exception) {
|
2022-11-24 12:26:18 +00:00
|
|
|
/* got an exception */
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-09-29 02:52:54 +00:00
|
|
|
exception = app_instance_main(wasm_module_inst);
|
|
|
|
if (exception) {
|
2022-11-24 12:26:18 +00:00
|
|
|
/* got an exception */
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 06:38:25 +00:00
|
|
|
|
2022-11-24 12:26:18 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
|
|
|
if (ret == 0) {
|
2023-09-04 10:01:09 +00:00
|
|
|
/* propagate wasi exit code. */
|
2022-11-24 12:26:18 +00:00
|
|
|
ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst);
|
|
|
|
}
|
|
|
|
#endif
|
2022-08-12 10:03:14 +00:00
|
|
|
|
2023-09-29 02:52:54 +00:00
|
|
|
if (exception)
|
|
|
|
printf("%s\n", exception);
|
|
|
|
|
2023-06-05 01:17:39 +00:00
|
|
|
#if WASM_ENABLE_STATIC_PGO != 0 && WASM_ENABLE_AOT != 0
|
|
|
|
if (get_package_type(wasm_file_buf, wasm_file_size) == Wasm_Module_AoT
|
|
|
|
&& gen_prof_file)
|
|
|
|
dump_pgo_prof_data(wasm_module_inst, gen_prof_file);
|
|
|
|
#endif
|
|
|
|
|
2023-09-13 09:09:31 +00:00
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
|
|
|
if (timeout_ms >= 0) {
|
|
|
|
timeout_arg.cancel = true;
|
|
|
|
os_thread_join(timeout_tid, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2025-02-10 06:15:47 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2023-09-13 09:09:31 +00:00
|
|
|
fail5:
|
|
|
|
#endif
|
2025-02-10 06:15:47 +00:00
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
2022-08-24 12:50:26 +00:00
|
|
|
fail4:
|
|
|
|
#endif
|
2021-07-29 06:38:25 +00:00
|
|
|
/* destroy the module instance */
|
|
|
|
wasm_runtime_deinstantiate(wasm_module_inst);
|
|
|
|
|
|
|
|
fail3:
|
|
|
|
/* unload the module */
|
|
|
|
wasm_runtime_unload(wasm_module);
|
|
|
|
|
|
|
|
fail2:
|
|
|
|
/* free the file buffer */
|
2021-12-06 09:25:10 +00:00
|
|
|
if (!is_xip_file)
|
2021-08-12 09:44:39 +00:00
|
|
|
wasm_runtime_free(wasm_file_buf);
|
|
|
|
else
|
|
|
|
os_munmap(wasm_file_buf, wasm_file_size);
|
2021-07-29 06:38:25 +00:00
|
|
|
|
|
|
|
fail1:
|
2022-04-28 04:31:28 +00:00
|
|
|
#if BH_HAS_DLFCN
|
2022-04-27 03:12:50 +00:00
|
|
|
/* unload the native libraries */
|
2023-08-14 06:05:59 +00:00
|
|
|
unregister_and_unload_native_libs(native_lib_loaded_count,
|
|
|
|
native_lib_loaded_list);
|
2022-04-27 03:12:50 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-29 06:38:25 +00:00
|
|
|
/* destroy runtime environment */
|
|
|
|
wasm_runtime_destroy();
|
2022-08-12 10:03:14 +00:00
|
|
|
|
|
|
|
return ret;
|
2021-07-29 06:38:25 +00:00
|
|
|
}
|