WAMR vmcore is a set of runtime libraries for loading and running Wasm modules. This document introduces how to build the WAMR vmcore.
References:
By including the script runtime_lib.cmake under folder build-scripts in CMakeList.txt, it is easy to use vmcore to build host software with cmake.
# add this into your CMakeList.txt
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
The script runtime_lib.cmake defines a number of variables for configuring the WAMR runtime features. You can set these variables in your CMakeList.txt or pass the configurations from cmake command line.
WAMR_BUILD_PLATFORM: set the target platform. It can be set to any platform name (folder name) under folder core/shared/platform.
WAMR_BUILD_TARGET: set the target CPU architecture. Current supported targets are: X86_64, X86_32, AARCH64, ARM, THUMB, XTENSA, ARC, RISCV32, RISCV64 and MIPS.
For RISCV32, the format is <arch>[_abi], where "_abi" is optional, currently the supported formats are RISCV32, RISCV32_ILP32D, RISCV32_ILP32F and RISCV32_ILP32: RISCV32 and RISCV32_ILP32D are identical, using ILP32D as abi (ILP32 with hardware floating-point calling convention for FLEN=64). RISCV32_ILP32F uses ILP32F as abi (ILP32 with hardware floating-point calling convention for FLEN=32). And RISCV32_ILP32 uses ILP32 as abi (Integer calling-convention only, and hardware floating-point calling convention is not used).
cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM
WAMR_BUILD_INTERP=1/0: enable or disable WASM interpreter
WAMR_BUILD_FAST_INTERP=1/0: build fast (default) or classic WASM interpreter.
NOTE: the fast interpreter runs ~2X faster than classic interpreter, but consumes about 2X memory to hold the pre-compiled code.
WAMR_BUILD_LIBC_BUILTIN=1/0, build the built-in libc subset for WASM app, default to enable if not set
WAMR_BUILD_LIBC_WASI=1/0, build the WASI libc subset for WASM app, default to enable if not set
WAMR_BUILD_LIBC_UVWASI=1/0 (Experiment), build the WASI libc subset for WASM app based on uvwasi implementation, default to disable if not set
Note: for platform which doesn't support WAMR_BUILD_LIBC_WASI, e.g. Windows, developer can try using WAMR_BUILD_LIBC_UVWASI.
Note: the mini loader doesn't check the integrity of the WASM binary file, developer must ensure that the WASM file is well-formed.
Note: Currently, the memory64 feature is only supported in classic interpreter running mode and AOT mode.
shared memory and thread manager will be enabled automatically.See WAMR pthread library for more details.
lib-pthread, it will be enabled automatically if this feature is enabled.shared memory and thread manager will be enabled automatically.See wasi-threads and Introduction to WAMR WASI threads for more details.
WAMR_BUILD_WASI_NN_ENABLE_EXTERNAL_DELEGATE=1/0, default to disable if not set
WAMR_BUILD_WASI_NN_EXTERNAL_DELEGATE_PATH=Path to the external delegate shared library (e.g. libedgetpu.so.1.0 for Coral USB)
wasi_ephemeral_nn module supportWAMR_DISABLE_STACK_HW_BOUND_CHECK below isn't set.WAMR_DISABLE_HW_BOUND_CHECK.
> Note: When boundary check with hardware trap is disabled, or WAMR_DISABLE_HW_BOUND_CHECK is set to 1, the native stack boundary check with hardware trap will be disabled too, no matter what value is set to WAMR_DISABLE_STACK_HW_BOUND_CHECK. And when boundary check with hardware trap is enabled, the status of this feature is set according to the value of WAMR_DISABLE_STACK_HW_BOUND_CHECK.Note: Currently, the exception handling feature is only supported in classic interpreter running mode.
--enable-dump-call-stack option to wamrc during compiling AOT module.Note: if it is enabled, the call stack will be dumped when exception occurs.
- For interpreter mode, the function names are firstly extracted from custom name section, if this section doesn't exist or the feature is not enabled, then the name will be extracted from the import/export sections
- For AOT/JIT mode, the function names are extracted from import/export section, please export as many functions as possible (for
wasi-sdkyou can use-Wl,--export-all) when compiling wasm module, and add--enable-dump-call-stack --emit-custom-sections=nameoption to wamrc during compiling AOT module.
void wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env) to dump the memory consumption info.
Currently we only profile the memory consumption of module, module_instance and exec_env, the memory consumed by other components such as wasi-ctx, multi-module and thread-manager are not included.Also refer to Memory usage estimation for a module.
void wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst) to dump the performance consumption info. Currently we only profile the performance consumption of each WASM function.The function name searching sequence is the same with dump call stack feature.
Also refer to Tune the performance of running wasm/aot file.
WAMR_BUILD_GLOBAL_HEAP_POOL is used in the iwasm applications provided in the directory
product-mini. When writing your own host application using WAMR, if you want to use a global heap and allocate memory from it, you must set the initialization argumentmem_alloc_typetoAlloc_With_Pool. The global heap is defined in the documentation Memory model and memory usage tunning.
WAMR_BUILD_GLOBAL_HEAP_SIZE is used in the iwasm applications provided in the directory
product-mini. When writing your own host application using WAMR, if you want to set the amount of memory dedicated to the global heap pool, you must set the initialization argumentmem_alloc_option.poolwith the appropriate values. The global heap is defined in the documentation Memory model and memory usage tunning. Note: ifWAMR_BUILD_GLOBAL_HEAP_SIZEis not set and the flagWAMR_BUILD_SPEC_TESTis set, the global heap size is equal to 300 MB (314572800), or 100 MB (104857600) when compiled for Intel SGX (Linux).
C
> int my_vprintf(const char *format, va_list ap)
> {
> /* output to pre-opened file stream */
> FILE *my_file = ...;
> return vfprintf(my_file, format, ap);
> /* or output to pre-opened file descriptor */
> int my_fd = ...;
> return vdprintf(my_fd, format, ap);
> /* or output to string buffer and print the string */
> char buf[128];
> vsnprintf(buf, sizeof(buf), format, ap);
> return my_printf("%s", buf);
> }
>
>
> and then use cmake -DWAMR_BH_VPRINTF=my_vprintf .. to pass the callback function, or add BH_VPRINTF=my_vprintf macro for the compiler, e.g. add line add_definitions(-DBH_VPRINTF=my_vprintf) in CMakeLists.txt. See basic sample for a usage example.
Note: if the log_callback function is provided by the developer, WAMR logs are redirected to such callback. For example:
> void my_log(uint32 log_level, const char *file, int line, const char *fmt, ...) > { > /* Usage of custom logger */ > } > ``` > See [basic sample](../samples/basic/src/main.c) for a usage example. #### **Enable reference types feature** - **WAMR_BUILD_REF_TYPES**=1/0, default to disable if not set #### **Exclude WAMR application entry functions** - **WAMR_DISABLE_APP_ENTRY**=1/0, default to disable if not set > Note: The WAMR application entry (`core/iwasm/common/wasm_application.c`) encapsulate some common process to instantiate, execute the wasm functions and print the results. Some platform related APIs are used in these functions, so you can enable this flag to exclude this file if your platform doesn't support those APIs. > *Don't enable this flag if you are building `product-mini`* #### **Enable source debugging features** - **WAMR_BUILD_DEBUG_INTERP**=1/0, default to 0 if not set > Note: There are some other setup required by source debugging, please refer to [source_debugging.md](./source_debugging.md) and [WAMR source debugging basic](https://bytecodealliance.github.io/wamr.dev/blog/wamr-source-debugging-basic) for more details. #### **Enable load wasm custom sections** - **WAMR_BUILD_LOAD_CUSTOM_SECTION**=1/0, default to disable if not set > Note: By default, the custom sections are ignored. If the embedder wants to get custom sections from `wasm_module_t`, then `WAMR_BUILD_LOAD_CUSTOM_SECTION` should be enabled, and then `wasm_runtime_get_custom_section` can be used to get a custom section by name. > Note: If `WAMR_BUILD_CUSTOM_NAME_SECTION` is enabled, then the `custom name section` will be treated as a special section and consumed by the runtime, not available to the embedder. > For AoT file, must use `--emit-custom-sections` to specify which sections need to be emitted into AoT file, otherwise all custom sections will be ignored. #### **Stack guard size** - **WAMR_BUILD_STACK_GUARD_SIZE**=n, default to N/A if not set. > Note: By default, the stack guard size is 1K (1024) or 24K (if uvwasi enabled). #### **Disable writing the linear memory base address to x86 GS segment register** - **WAMR_DISABLE_WRITE_GS_BASE**=1/0, default to enable if not set and supported by platform > Note: by default only platform [linux x86-64](https://github.com/bytecodealliance/wasm-micro-runtime/blob/5fb5119239220b0803e7045ca49b0a29fe65e70e/core/shared/platform/linux/platform_internal.h#L67) will enable this feature, for 32-bit platforms it's automatically disabled even when the flag is set to 0. In linux x86-64, writing the linear memory base address to x86 GS segment register may be used to speedup the linear memory access for LLVM AOT/JIT, when `--enable-segue=[<flags>]` option is added for `wamrc` or `iwasm`. > See [Enable segue optimization for wamrc when generating the aot file](./perf_tune.md#3-enable-segue-optimization-for-wamrc-when-generating-the-aot-file) for more details. #### **User defined linear memory allocator** - **WAMR_BUILD_ALLOC_WITH_USAGE**=1/0, default to disable if not set > Notes: by default, the linear memory is allocated by system. when it's set to 1 and Alloc_With_Allocator is selected, it will be allocated by customer. #### **Enable running PGO(Profile-Guided Optimization) instrumented AOT file** - **WAMR_BUILD_STATIC_PGO**=1/0, default to disable if not set > Note: See [Use the AOT static PGO method](./perf_tune.md#5-use-the-aot-static-pgo-method) for more details. #### **Enable linux perf support** - **WAMR_BUILD_LINUX_PERF**=1/0, enable linux perf support to generate the flamegraph to analyze the performance of a wasm application, default to disable if not set > Note: See [Use linux-perf](./perf_tune.md#7-use-linux-perf) for more details. #### **Enable module instance context APIs** - **WAMR_BUILD_MODULE_INST_CONTEXT**=1/0, enable module instance context APIs which can set one or more contexts created by the embedder for a wasm module instance, default to enable if not set:C
wasm_runtime_create_context_key wasm_runtime_destroy_context_key wasm_runtime_set_context wasm_runtime_set_context_spread wasm_runtime_get_context> Note: See [wasm_export.h](../core/iwasm/include/wasm_export.h) for more details. #### **Enable quick AOT/JTI entries** - **WAMR_BUILD_QUICK_AOT_ENTRY**=1/0, enable registering quick call entries to speedup the aot/jit func call process, default to enable if not set > Note: See [Refine callings to AOT/JIT functions from host native](./perf_tune.md#83-refine-callings-to-aotjit-functions-from-host-native) for more details. #### **Enable AOT intrinsics** - **WAMR_BUILD_AOT_INTRINSICS**=1/0, enable the AOT intrinsic functions, default to enable if not set. These functions can be called from the AOT code when `--disable-llvm-intrinsics` flag or `--enable-builtin-intrinsics=<intr1,intr2,...>` flag is used by wamrc to generate the AOT file. > Note: See [Tuning the XIP intrinsic functions](./xip.md#tuning-the-xip-intrinsic-functions) for more details. #### **Configurable memory access boundary check** - **WAMR_CONFIGURABLE_BOUNDS_CHECKS**=1/0, default to disable if not set > Note: If it is enabled, allow to run `iwasm --disable-bounds-checks` to disable the memory access boundary checks for interpreter mode. #### **Module instance context APIs** - **WAMR_BUILD_MODULE_INST_CONTEXT**=1/0, default to disable if not set > Note: If it is enabled, allow to set one or more contexts created by embedder for a module instance, the below APIs are provided:C
wasm_runtime_create_context_key wasm_runtime_destroy_context_key wasm_runtime_set_context wasm_runtime_set_context_spread wasm_runtime_get_context#### **Shared heap among wasm apps and host native** - **WAMR_BUILD_SHARED_HEAP**=1/0, default to disable if not set > Note: If it is enabled, allow to create one or more shared heaps, and attach one to a module instance, the belows APIs ared provided:C wasm_runtime_create_shared_heap wasm_runtime_attach_shared_heap wasm_runtime_detach_shared_heap wasm_runtime_shared_heap_malloc wasm_runtime_shared_heap_free
And the wasm app can calls below APIs to allocate/free memory from/to the shared heap if it is attached to the app's module instance:C void *shared_heap_malloc(); void shared_heap_free(void *ptr);
**Combination of configurations:** We can combine the configurations. For example, if we want to disable interpreter, enable AOT and WASI, we can run command:Bash cmake .. -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_AOT=1 -DWAMR_BUILD_LIBC_WASI=0 -DWAMR_BUILD_PLATFORM=linux
Or if we want to enable interpreter, disable AOT and WASI, and build as X86_32, we can run command:Bash cmake .. -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_AOT=0 -DWAMR_BUILD_LIBC_WASI=0 -DWAMR_BUILD_TARGET=X86_32 ```