wasm-micro-runtime/samples/native-lib
Wenyong Huang 0ee5ffce85
Refactor APIs and data structures as preliminary work for Memory64 (#3209)
# Change the data type representing linear memory address from u32 to u64

## APIs signature changes
- (Export)wasm_runtime_module_malloc
  - wasm_module_malloc
    - wasm_module_malloc_internal
  - aot_module_malloc
    - aot_module_malloc_internal
- wasm_runtime_module_realloc
  - wasm_module_realloc
    - wasm_module_realloc_internal
  - aot_module_realloc
    - aot_module_realloc_internal
- (Export)wasm_runtime_module_free
  - wasm_module_free
    - wasm_module_free_internal
  - aot_module_malloc
    - aot_module_free_internal
- (Export)wasm_runtime_module_dup_data
  - wasm_module_dup_data
  - aot_module_dup_data
- (Export)wasm_runtime_validate_app_addr
- (Export)wasm_runtime_validate_app_str_addr
- (Export)wasm_runtime_validate_native_addr
- (Export)wasm_runtime_addr_app_to_native
- (Export)wasm_runtime_addr_native_to_app
- (Export)wasm_runtime_get_app_addr_range
- aot_set_aux_stack
- aot_get_aux_stack
- wasm_set_aux_stack
- wasm_get_aux_stack
- aot_check_app_addr_and_convert, wasm_check_app_addr_and_convert
  and jit_check_app_addr_and_convert
- wasm_exec_env_set_aux_stack
- wasm_exec_env_get_aux_stack
- wasm_cluster_create_thread
- wasm_cluster_allocate_aux_stack
- wasm_cluster_free_aux_stack

## Data structure changes
- WASMModule and AOTModule
  - field aux_data_end, aux_heap_base and aux_stack_bottom
- WASMExecEnv
  - field aux_stack_boundary and aux_stack_bottom
- AOTCompData
  - field aux_data_end, aux_heap_base and aux_stack_bottom
- WASMMemoryInstance(AOTMemoryInstance)
  - field memory_data_size and change __padding to is_memory64
- WASMModuleInstMemConsumption
  - field total_size and memories_size
- WASMDebugExecutionMemory
  - field start_offset and current_pos
- WASMCluster
  - field stack_tops

## Components that are affected by the APIs and data structure changes
- libc-builtin
- libc-emcc
- libc-uvwasi
- libc-wasi
- Python and Go Language Embedding
- Interpreter Debug engine
- Multi-thread: lib-pthread, wasi-threads and thread manager
2024-03-12 11:38:50 +08:00
..
wasm-app samples/native-lib: Add an example to use wamr API from native lib (#1649) 2022-10-28 19:31:21 +08:00
CMakeLists.txt samples/native-lib: use the same shared lib name as product-mini (#1669) 2022-11-01 21:55:43 +08:00
README.md Fix download link for wasi-sdk (#3077) 2024-01-24 13:42:45 +08:00
test_add.c samples/native-lib: Fix exec_env type (#1557) 2022-10-06 20:21:21 +08:00
test_hello.c samples/native-lib: Add a bit more complicated example (#1643) 2022-10-27 15:06:14 +08:00
test_hello2.c Refactor APIs and data structures as preliminary work for Memory64 (#3209) 2024-03-12 11:38:50 +08:00
test_sqrt.c samples/native-lib: Fix exec_env type (#1557) 2022-10-06 20:21:21 +08:00

"native-lib" sample introduction

This sample demonstrates how to write required interfaces in native library, build it into a shared library and register the shared library to iwasm.

The native library should provide get_native_lib API for iwasm to return the native library info, including the module name, the native symbol list and the native symbol count, so that iwasm can use them to regiter the native library, for example:

static int
foo_wrapper(wasm_exec_env_t exec_env, int x, int y)
{
    return x + y;
}

#define REG_NATIVE_FUNC(func_name, signature) \
    { #func_name, func_name##_wrapper, signature, NULL }

static NativeSymbol native_symbols[] = {
    REG_NATIVE_FUNC(foo, "(ii)i")
};

uint32_t
get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
{
    *p_module_name = "env";
    *p_native_symbols = native_symbols;
    return sizeof(native_symbols) / sizeof(NativeSymbol);
}

Preparation

Please install WASI SDK, download the wasi-sdk release and extract the archive to default path /opt/wasi-sdk.

Build the sample

mkdir build
cd build
cmake ..
make

iwasm, one wasm module test.wasm and two shared libraries libtest_add.so, libtest_sqrt.so will be generated.

Run workload

Linux

cd build
./iwasm --native-lib=./libtest_add.so --native-lib=./libtest_sqrt.so --native-lib=./libtest_hello.so --native-lib=./libtest_hello2.so wasm-app/test.wasm

macOS

cd build
./iwasm --native-lib=libtest_add.dylib --native-lib=libtest_sqrt.dylib --native-lib=libtest_hello.dylib --native-lib=libtest_hello2.dylib wasm-app/test.wasm

The output is:

init_native_lib in test_hello2.c called
Hello World!
10 + 20 = 30
sqrt(10, 20) = 500
test_hello("main", 0x0, 0) = 41
malloc(42) = 0x24e8
test_hello("main", 0x24e8, 42) = 41
Message from test_hello: Hello, main. This is test_hello_wrapper!
test_hello2("main", 0x0, 0) = 85
malloc(86) = 0x24e8
test_hello2("main", 0x24e8, 86) = 85
Message from test_hello2: Hello, main. This is test_hello2_wrapper! Your wasm_module_inst_t is 0x7fe0e6804280.
deinit_native_lib in test_hello2.c called