summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:19:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:19:22 +0000
commitc21c3b0befeb46a51b6bf3758ffa30813bea0ff0 (patch)
tree9754ff1ca740f6346cf8483ec915d4054bc5da2d /fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api
parentAdding upstream version 1.43.2. (diff)
downloadnetdata-c21c3b0befeb46a51b6bf3758ffa30813bea0ff0.tar.xz
netdata-c21c3b0befeb46a51b6bf3758ffa30813bea0ff0.zip
Adding upstream version 1.44.3.upstream/1.44.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/.gitignore2
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/CMakeLists.txt174
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/README.md174
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/CMakeLists.txt12
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/example1.c204
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/CMakeLists.txt47
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/inc/.gitkeep0
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/send_recv.c244
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/CMakeLists.txt210
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/README.md50
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/LICENSE202
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.c193
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.wat10
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.c294
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.wat32
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.c535
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.wat15
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.c122
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.wat5
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.c278
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.wat27
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-0.wat8
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-1.wat10
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport.c192
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.c138
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.wat4
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.c308
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.wat24
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.c235
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.wat11
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.c163
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.wat7
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.c207
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.wat6
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.c131
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.wat4
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.c218
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.wat12
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.c187
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.wat5
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.c184
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.wat5
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/utils/multi_module_utils.c47
43 files changed, 4936 insertions, 0 deletions
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/.gitignore b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/.gitignore
new file mode 100644
index 000000000..ab998a6eb
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/.gitignore
@@ -0,0 +1,2 @@
+/wasm/inc/**
+!/wasm/inc/.*
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/CMakeLists.txt
new file mode 100644
index 000000000..1325e110c
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/CMakeLists.txt
@@ -0,0 +1,174 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required(VERSION 3.14)
+project(how-to-deal-with-import)
+
+include(CMakePrintHelpers)
+include(CTest)
+include(ExternalProject)
+include(FetchContent)
+
+#
+# dependencies
+#
+set(WAMR_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../)
+# wasm required headers
+execute_process(
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${WARM_ROOT}/${WAMR_ROOT}/wamr-sdk/app/libc-builtin-sysroot/include/pthread.h
+ ${CMAKE_CURRENT_LIST_DIR}/wasm/inc
+)
+
+# vmlib
+################ runtime settings ################
+string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
+if (APPLE)
+ add_definitions(-DBH_PLATFORM_DARWIN)
+endif ()
+
+# Resetdefault linker flags
+set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
+
+# WAMR features switch
+
+# Set WAMR_BUILD_TARGET, currently values supported:
+# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
+# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
+if (NOT DEFINED WAMR_BUILD_TARGET)
+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
+ set (WAMR_BUILD_TARGET "AARCH64")
+ elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
+ set (WAMR_BUILD_TARGET "RISCV64")
+ elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
+ # Build as X86_64 by default in 64-bit platform
+ set (WAMR_BUILD_TARGET "X86_64")
+ elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
+ # Build as X86_32 by default in 32-bit platform
+ set (WAMR_BUILD_TARGET "X86_32")
+ else ()
+ message(SEND_ERROR "Unsupported build target platform!")
+ endif ()
+endif ()
+
+if (NOT CMAKE_BUILD_TYPE)
+ set (CMAKE_BUILD_TYPE Release)
+endif ()
+
+if (NOT DEFINED WAMR_BUILD_AOT)
+ # Enable AOT by default.
+ set (WAMR_BUILD_AOT 1)
+endif ()
+if (NOT DEFINED WAMR_BUILD_INTERP)
+ # Disable Interpreter by default
+ set (WAMR_BUILD_INTERP 0)
+endif ()
+set(WAMR_BUILD_JIT 0)
+set(WAMR_BUILD_FAST_INTERP 1)
+set(WAMR_BUILD_LIB_PTHREAD 1)
+set(WAMR_BUILD_LIBC_BUILTIN 1)
+set(WAMR_BUILD_LIBC_WASI 1)
+set(WAMR_BUILD_SIMD 0)
+
+# compiling and linking flags
+if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+ set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+endif ()
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
+
+# build out vmlib
+set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
+
+add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
+target_link_libraries(vmlib INTERFACE dl m pthread)
+if(WAMR_BUILD_AOT EQUAL 1)
+ target_compile_definitions(vmlib INTERFACE -DWASM_ENABLE_AOT=1)
+else()
+ target_compile_definitions(vmlib INTERFACE -DWASM_ENABLE_AOT=0)
+endif()
+
+if(WAMR_BUILD_INTERP EQUAL 1)
+ target_compile_definitions(vmlib INTERFACE -DWASM_ENABLE_INTERP=1)
+else()
+ target_compile_definitions(vmlib INTERFACE -DWASM_ENABLE_INTERP=0)
+endif()
+
+if(CMAKE_BUILD_TYPE STREQUAL "Debug")
+ # ASAN + UBSAN
+ target_compile_options(vmlib INTERFACE -fsanitize=address,undefined)
+ target_link_options(vmlib INTERFACE -fsanitize=address,undefined)
+endif()
+
+# # MSAN
+# target_compile_options(vmlib INTERFACE -fsanitize=memory -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer)
+# target_link_options(vmlib INTERFACE -fsanitize=memory)
+
+# wamrc
+if(WAMR_BUILD_AOT EQUAL 1 AND WAMR_BUILD_INTERP EQUAL 0)
+ ExternalProject_Add(wamrc
+ PREFIX wamrc-build
+ SOURCE_DIR ${WAMR_ROOT}/wamr-compiler
+ CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${WAMR_ROOT}/wamr-compiler -B build
+ BUILD_COMMAND ${CMAKE_COMMAND} --build build --target wamrc
+ INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different build/wamrc ${CMAKE_CURRENT_BINARY_DIR}/wamrc
+ )
+endif()
+
+#
+# host
+add_subdirectory(host)
+add_custom_target(
+ install_host ALL
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different ./host/example1 .
+ DEPENDS example1
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+)
+
+# TODO: replace it with a find_package()
+set(WASI_SDK_DIR /opt/wasi-sdk-19.0/)
+set(WASI_TOOLCHAIN_FILE ${WASI_SDK_DIR}/share/cmake/wasi-sdk.cmake)
+set(WASI_SYS_ROOT ${WASI_SDK_DIR}/share/wasi-sysroot)
+
+#
+# wasm
+if(WAMR_BUILD_AOT EQUAL 1 AND WAMR_BUILD_INTERP EQUAL 0)
+ ExternalProject_Add(wasm
+ PREFIX wasm-build
+ DEPENDS wamrc
+ BUILD_ALWAYS TRUE
+ SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/wasm
+ CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_LIST_DIR}/wasm -B build
+ -DWASI_SDK_PREFIX=${WASI_SDK_DIR}
+ -DCMAKE_TOOLCHAIN_FILE=${WASI_TOOLCHAIN_FILE}
+ -DCMAKE_SYSROOT=${WASI_SYS_ROOT}
+ -DWASM_TO_AOT=ON
+ -DWAMRC_PATH=${CMAKE_CURRENT_BINARY_DIR}/wamrc
+ -DSOCKET_WASI_CMAKE=${WAMR_ROOT}/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake
+ BUILD_COMMAND ${CMAKE_COMMAND} --build build
+ INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}
+ )
+else()
+ ExternalProject_Add(wasm
+ PREFIX wasm-build
+ BUILD_ALWAYS TRUE
+ SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/wasm
+ CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_LIST_DIR}/wasm -B build
+ -DWASI_SDK_PREFIX=${WASI_SDK_DIR}
+ -DCMAKE_TOOLCHAIN_FILE=${WASI_TOOLCHAIN_FILE}
+ -DCMAKE_SYSROOT=${WASI_SYS_ROOT}
+ -DSOCKET_WASI_CMAKE=${WAMR_ROOT}/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake
+ BUILD_COMMAND ${CMAKE_COMMAND} --build build
+ INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}
+ )
+endif()
+
+#
+# Test
+#
+add_test(
+ NAME run_example1
+ COMMAND ./example1
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/README.md b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/README.md
new file mode 100644
index 000000000..9b61a6e74
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/README.md
@@ -0,0 +1,174 @@
+# How to create `imports` for wasm_instance_new() properly
+
+It's always been asked how to create `wasm_extern_vec_t *imports` for
+`wasm_instance_new()`?
+
+```c
+WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
+ wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
+ own wasm_trap_t** trap
+);
+```
+
+`wasm_extern_vec_t *imports` is required to match the requirement of _the
+import section_ of a .wasm.
+
+```bash
+$ /opt/wabt-1.0.31/bin/wasm-objdump -j Import -x <some_example>.wasm
+
+Section Details:
+
+Import[27]:
+ - func[0] sig=2 <pthread_mutex_lock> <- env.pthread_mutex_lock
+ - func[1] sig=2 <pthread_mutex_unlock> <- env.pthread_mutex_unlock
+ - func[2] sig=2 <pthread_cond_signal> <- env.pthread_cond_signal
+ - func[3] sig=3 <host_log> <- env.log
+ ...
+ - func[11] sig=4 <__imported_wasi_snapshot_preview1_sock_bind> <- wasi_snapshot_preview1.sock_bind
+ - func[12] sig=4 <__imported_wasi_snapshot_preview1_sock_connect> <- wasi_snapshot_preview1.sock_connect
+ - func[13] sig=4 <__imported_wasi_snapshot_preview1_sock_listen> <- wasi_snapshot_preview1.sock_listen
+ - func[14] sig=5 <__imported_wasi_snapshot_preview1_sock_open> <- wasi_snapshot_preview1.sock_open
+ - func[15] sig=4 <__imported_wasi_snapshot_preview1_sock_addr_remote> <- wasi_snapshot_preview1.sock_addr_remote
+ - func[16] sig=4 <__imported_wasi_snapshot_preview1_args_get> <- wasi_snapshot_preview1.args_get
+ - func[17] sig=4 <__imported_wasi_snapshot_preview1_args_sizes_get> <- wasi_snapshot_preview1.args_sizes_get
+ ...
+```
+
+Developers should fill in _imports_ with enough host functions and make sure
+there are no linking problems during instantiation.
+
+```bash
+TODO: linking warnings
+```
+
+## A natural way
+
+One natural answer is "to create a list which matches every item in _the import
+section_" of the .wasm. Since developers can see the section details of
+a .wasm by tools like _wasm-objdump_, the answer is doable. Most of the time,
+if they also prepare Wasm modules, developers have full control over import
+requirements, and they only need to take a look at the order of _the import
+section_.
+
+Yes, _the order_. A proper `wasm_extern_vec_t *imports` includes two things:
+
+1. how many `wasm_extern_t`
+2. and order of those
+
+Because there is no "name information" in a `wasm_extern_t`. The only way is let
+`wasm_instance_new()` to tell which item in _the import section_ of a .wasm
+should match any item in `wasm_extern_vec_t *imports` is based on **_index_**.
+
+The algorithm is quite straightforward. The first one of _the import section_ matches
+`wasm_extern_vec_t *imports->data[0] `. The second one matches `wasm_extern_vec_t *imports->data[1]`.
+And so on.
+
+So the order of `wasm_extern_vec_t *imports` becomes quite a burden. It requires
+developers always checking _the import section_ visually.
+
+Until here, the natural way is still workable although involving some handy work.
+Right?
+
+## A blocker
+
+Sorry, the situation changes a lot when driving wasm32-wasi Wasm modules with
+wasm-c-api.
+
+As you know, WASI provides _a set of crossing-platform standard libraries_ for
+Wasm modules, and leaves some _interfaces_ for native platform-dependent supports.
+Those _interfaces_ are those import items with the module name `wasi_snapshot_preview1`
+in a Wasm module.
+
+It seems not economical to let developers provide their version of host
+implementations of the `wasi_snapshot_preview1.XXX` functions. All those support
+should be packed into a common library and shared in different Wasm modules.
+Like a [cargo WASI](https://github.com/bytecodealliance/cargo-wasi).
+
+WAMR chooses to integrate the WASI support library in the runtime to reduce
+developers' compilation work. It brings developers a new thing of a proper
+`wasm_extern_vec_t *imports` that developers should avoid overwriting those items
+of _the import section_ of a Wasm module that will be provided by the runtime. It
+also not economical to code for those functions.
+
+Using module names as a filter seems to be a simple way. But some private
+additional c/c++ libraries are supported in WAMR. Those supporting will bring
+more import items that don't use `wasi_snapshot_preview1` as module names but are still
+covered by the WASM runtime. Like `env.pthread_`. Plus, [the native lib registeration](https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md)
+provides another possible way to fill in the requirement of _the import section_.
+
+Let's take summarize. A proper `wasm_extern_vec_t *imports` should include:
+
+1. provides all necessary host implementations for items in _the import section_
+2. should not override runtime provided implementation or covered by native
+ registrations. functinal or econmical.
+3. keep them in a right order
+
+## A recommendation
+
+The recommendation is:
+
+- use `wasm_module_imports()` to build the order
+- use `wasm_importtype_is_linked()` to avoid overwriting
+
+[wasm-c-api-imports](.) is a simple showcase of how to do that.
+
+First, let's take a look at the Wasm module. [send_recv](./wasm/send_recv.c)
+uses both standard WASI and WAMR_BUILD_LIB_PTHREAD supporting. Plus a private
+native function `host_log`.
+
+So, `wasm_extern_vec_t *imports` should only include the host implementation of
+`host_log` and avoid WASI related(`wasm-c-api-imports.XXX`) and pthread related(`env.pthread_XXX`).
+
+[Here is how to do](./host/example1.c):
+
+- get import types with `wasm_module_imports(0)`. it contains name information
+
+```c
+ wasm_importtype_vec_t importtypes = { 0 };
+ wasm_module_imports(module, &importtypes);
+```
+
+- traversal import types. The final `wasm_importvec_t *imports` should have the
+ same order with `wasm_importtype_vec_t`
+
+```c
+ for (unsigned i = 0; i < importtypes.num_elems; i++)
+```
+
+- use `wasm_importtype_is_linked()` to avoid those covered by the runtime and
+ registered natives. A little tip is use "wasm_extern_new_empty()" to create
+ a placeholder.
+
+```c
+ /* use wasm_extern_new_empty() to create a placeholder */
+ if (wasm_importtype_is_linked(importtype)) {
+ externs[i] = wasm_extern_new_empty(
+ store, wasm_externtype_kind(wasm_importtype_type(importtype)));
+ continue;
+ }
+```
+
+- use `wasm_importtype_module()` to get the module name, use `wasm_importtype_name()`
+ to get the field name.
+
+```c
+ const wasm_name_t *module_name =
+ wasm_importtype_module(importtypes.data[i]);
+ const wasm_name_t *field_name =
+ wasm_importtype_name(importtypes.data[i]);
+```
+
+- fill in `wasm_externvec_t *imports` dynamically and programmatically.
+
+```c
+ if (strncmp(module_name->data, "env", strlen("env")) == 0
+ && strncmp(field_name->data, "log", strlen("log")) == 0) {
+ wasm_functype_t *log_type = wasm_functype_new_2_0(
+ wasm_valtype_new_i64(), wasm_valtype_new_i32());
+ wasm_func_t *log_func = wasm_func_new(store, log_type, host_logs);
+ wasm_functype_delete(log_type);
+
+ externs[i] = wasm_func_as_extern(log_func);
+ }
+ }
+```
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/CMakeLists.txt
new file mode 100644
index 000000000..e2636f09e
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/CMakeLists.txt
@@ -0,0 +1,12 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required(VERSION 3.14)
+project(host)
+
+set(CMAKE_BUILD_TYPE Debug)
+
+#
+# host
+add_executable(example1 ./example1.c)
+target_link_libraries(example1 vmlib)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/example1.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/example1.c
new file mode 100644
index 000000000..ccf574d79
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/host/example1.c
@@ -0,0 +1,204 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "wasm_c_api.h"
+#include "wasm_export.h"
+
+static wasm_trap_t *
+host_logs(const wasm_val_vec_t *args, wasm_val_vec_t *results)
+{
+ return NULL;
+}
+
+static bool
+build_imports(wasm_store_t *store, const wasm_module_t *module,
+ wasm_extern_vec_t *out)
+{
+ wasm_importtype_vec_t importtypes = { 0 };
+ wasm_module_imports(module, &importtypes);
+
+ wasm_extern_t *externs[32] = { 0 };
+
+ for (unsigned i = 0; i < importtypes.num_elems; i++) {
+ wasm_importtype_t *importtype = importtypes.data[i];
+
+ /* use wasm_extern_new_empty() to create a placeholder */
+ if (wasm_importtype_is_linked(importtype)) {
+ externs[i] = wasm_extern_new_empty(
+ store, wasm_externtype_kind(wasm_importtype_type(importtype)));
+ continue;
+ }
+
+ const wasm_name_t *module_name =
+ wasm_importtype_module(importtypes.data[i]);
+ const wasm_name_t *field_name =
+ wasm_importtype_name(importtypes.data[i]);
+
+ if (strncmp(module_name->data, "env", strlen("env")) == 0
+ && strncmp(field_name->data, "log", strlen("log")) == 0) {
+ wasm_functype_t *log_type = wasm_functype_new_2_0(
+ wasm_valtype_new_i64(), wasm_valtype_new_i32());
+ wasm_func_t *log_func = wasm_func_new(store, log_type, host_logs);
+ wasm_functype_delete(log_type);
+
+ externs[i] = wasm_func_as_extern(log_func);
+ }
+ }
+
+ wasm_extern_vec_new(out, importtypes.num_elems, externs);
+ wasm_importtype_vec_delete(&importtypes);
+ return true;
+}
+
+int
+main()
+{
+ int main_ret = EXIT_FAILURE;
+
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t *engine = wasm_engine_new();
+ if (!engine)
+ goto quit;
+
+ wasm_store_t *store = wasm_store_new(engine);
+ if (!store)
+ goto delete_engine;
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE *file = fopen("send_recv.aot", "rb");
+ printf("> Load .aot\n");
+#else
+ FILE *file = fopen("send_recv.wasm", "rb");
+ printf("> Load .wasm\n");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ goto delete_store;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ goto close_file;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ goto close_file;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ goto close_file;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ goto delete_binary;
+ }
+
+ // Compile.
+ printf("Compiling module...\n");
+ wasm_module_t *module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ goto delete_binary;
+ }
+
+ // Set Wasi Context
+ const char *addr_pool[1] = { "127.0.0.1" };
+ wasm_runtime_set_wasi_addr_pool(*module, addr_pool, 1);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_vec_t imports = { 0 };
+ ret = build_imports(store, module, &imports);
+ if (!ret) {
+ printf("> Error building imports!\n");
+ goto delete_module;
+ }
+
+ wasm_instance_t *instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ goto delete_imports;
+ }
+
+ // Extract export.
+ printf("Extracting export...\n");
+ wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size == 0) {
+ printf("> Error accessing exports!\n");
+ goto delete_instance;
+ }
+
+ /**
+ * should use information from wasm_module_exports to avoid hard coding "1"
+ */
+ const wasm_func_t *start_func = wasm_extern_as_func(exports.data[1]);
+ if (start_func == NULL) {
+ printf("> Error accessing export!\n");
+ goto delete_exports;
+ }
+
+ // Call. "_start(nil) -> i32"
+ printf("Calling _start ...\n");
+ wasm_val_t rs[1] = { WASM_I32_VAL(0) };
+ wasm_val_vec_t args = WASM_EMPTY_VEC;
+ wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
+ wasm_trap_t *trap = wasm_func_call(start_func, &args, &results);
+ if (trap) {
+ wasm_name_t message = { 0 };
+ wasm_trap_message(trap, &message);
+
+ printf("> Error calling function! %s\n", message.data);
+
+ wasm_name_delete(&message);
+ wasm_trap_delete(trap);
+ goto delete_exports;
+ }
+
+ // Print result.
+ printf("Printing result...\n");
+ printf("> %u\n", rs[0].of.i32);
+
+ // Shut down.
+ printf("Shutting down...\n");
+
+ // All done.
+ printf("Done.\n");
+ main_ret = EXIT_SUCCESS;
+
+delete_exports:
+ wasm_extern_vec_delete(&exports);
+delete_instance:
+ wasm_instance_delete(instance);
+delete_imports:
+ wasm_extern_vec_delete(&imports);
+delete_module:
+ wasm_module_delete(module);
+delete_binary:
+ wasm_byte_vec_delete(&binary);
+close_file:
+ fclose(file);
+delete_store:
+ wasm_store_delete(store);
+delete_engine:
+ wasm_engine_delete(engine);
+quit:
+ return main_ret;
+} \ No newline at end of file
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/CMakeLists.txt
new file mode 100644
index 000000000..6b2743cb5
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/CMakeLists.txt
@@ -0,0 +1,47 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required (VERSION 3.14)
+project(wasm_modules)
+
+if(NOT SOCKET_WASI_CMAKE)
+ message(FATAL_ERROR "Require SOCKET_WASI_CMAKE")
+endif()
+
+option(WASM_TO_AOT "transfer wasm to aot" OFF)
+if(WASM_TO_AOT AND NOT WAMRC_PATH)
+ message(FATAL_ERROR "Require WAMRC_PATH when WASM_TO_AOT is ON")
+endif()
+
+#
+# c -> wasm
+include(${SOCKET_WASI_CMAKE})
+add_executable(send_recv ${CMAKE_CURRENT_LIST_DIR}/send_recv.c)
+set_target_properties(send_recv PROPERTIES SUFFIX .wasm)
+target_include_directories(send_recv PUBLIC ${CMAKE_CURRENT_LIST_DIR}/inc)
+target_link_libraries(send_recv socket_wasi_ext)
+target_link_options(send_recv PRIVATE
+ LINKER:--export=__heap_base
+ LINKER:--export=__data_end
+ LINKER:--shared-memory,--max-memory=196608
+ LINKER:--no-check-features
+ LINKER:--allow-undefined
+)
+
+if(WASM_TO_AOT)
+ # wasm -> aot
+ add_custom_target(send_recv_aot ALL
+ COMMAND pwd && ${WAMRC_PATH} --enable-multi-thread -o ./send_recv.aot ./send_recv.wasm
+ DEPENDS send_recv
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ )
+endif()
+
+#
+# install
+if(WASM_TO_AOT)
+ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/send_recv.aot DESTINATION . )
+else()
+ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/send_recv.wasm DESTINATION . )
+endif()
+
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/inc/.gitkeep b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/inc/.gitkeep
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/inc/.gitkeep
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/send_recv.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/send_recv.c
new file mode 100644
index 000000000..d9f95573a
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api-imports/wasm/send_recv.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <arpa/inet.h>
+#include <assert.h>
+#include <netinet/in.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#ifdef __wasi__
+#include <wasi_socket_ext.h>
+#include "pthread.h"
+#else
+#include <pthread.h>
+#endif
+
+static pthread_mutex_t lock = { 0 };
+static pthread_cond_t cond = { 0 };
+static bool server_create_failed = false;
+static bool server_is_ready = false;
+
+#ifdef __wasi__
+__attribute__((import_name("log"))) extern void
+host_log(uint64_t message, uint32_t length);
+#endif
+
+static void
+local_printf(const char *formatter, ...)
+{
+ char buffer[128] = { 0 };
+ va_list args;
+
+ va_start(args, formatter);
+ vsnprintf(buffer, 128, formatter, args);
+ va_end(args);
+
+#ifdef __wasi__
+ host_log((uint64_t)(void *)buffer, strlen(buffer));
+#endif
+ printf("--> %s", buffer);
+}
+
+void *
+run_as_server(void *arg)
+{
+ int sock = -1, on = 1;
+ struct sockaddr_in addr = { 0 };
+ int addrlen = 0;
+ int new_sock = -1;
+ char *buf[] = {
+ "The stars shine down", "It brings us light", "Light comes down",
+ "To make us paths", "It watches us", "And mourns for us",
+ };
+ struct iovec iov[] = {
+ { .iov_base = buf[0], .iov_len = strlen(buf[0]) + 1 },
+ { .iov_base = buf[1], .iov_len = strlen(buf[1]) + 1 },
+ { .iov_base = buf[2], .iov_len = strlen(buf[2]) + 1 },
+ { .iov_base = buf[3], .iov_len = strlen(buf[3]) + 1 },
+ { .iov_base = buf[4], .iov_len = strlen(buf[4]) + 1 },
+ { .iov_base = buf[5], .iov_len = strlen(buf[5]) + 1 },
+ };
+ struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 6 };
+ ssize_t send_len = 0;
+
+ pthread_mutex_lock(&lock);
+ sock = socket(AF_INET, SOCK_STREAM, 0);
+ if (sock < 0) {
+ server_create_failed = true;
+ pthread_cond_signal(&cond);
+ pthread_mutex_unlock(&lock);
+ perror("Create a socket failed");
+ return NULL;
+ }
+
+#ifndef __wasi__
+ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
+ server_create_failed = true;
+ pthread_cond_signal(&cond);
+ pthread_mutex_unlock(&lock);
+ perror("Setsockopt failed");
+ goto fail1;
+ }
+#endif
+
+ /* 0.0.0.0:1234 */
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(1234);
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+ addrlen = sizeof(addr);
+ if (bind(sock, (struct sockaddr *)&addr, addrlen) < 0) {
+ server_create_failed = true;
+ pthread_cond_signal(&cond);
+ pthread_mutex_unlock(&lock);
+ perror("Bind failed");
+ goto fail1;
+ }
+
+ if (listen(sock, 0) < 0) {
+ server_create_failed = true;
+ pthread_cond_signal(&cond);
+ pthread_mutex_unlock(&lock);
+ perror("Listen failed");
+ goto fail1;
+ }
+
+ server_is_ready = true;
+ pthread_cond_signal(&cond);
+ pthread_mutex_unlock(&lock);
+
+ local_printf("Server is online ... \n");
+
+ new_sock = accept(sock, (struct sockaddr *)&addr, (socklen_t *)&addrlen);
+ if (new_sock < 0) {
+ perror("Accept failed");
+ goto fail1;
+ }
+
+ local_printf("Start sending. \n");
+ send_len = sendmsg(new_sock, &msg, 0);
+ if (send_len < 0) {
+ perror("Sendmsg failed");
+ goto fail2;
+ }
+ local_printf("Send %ld bytes successfully!\n", send_len);
+
+fail2:
+ close(new_sock);
+fail1:
+ shutdown(sock, SHUT_RD);
+ close(sock);
+ return NULL;
+}
+
+void *
+run_as_client(void *arg)
+{
+ int sock = -1;
+ struct sockaddr_in addr = { 0 };
+ /* buf of server is 106 bytes */
+ char buf[110] = { 0 };
+ struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
+ struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
+ ssize_t recv_len = 0;
+
+ pthread_mutex_lock(&lock);
+ while (!server_create_failed && !server_is_ready) {
+ pthread_cond_wait(&cond, &lock);
+ }
+ pthread_mutex_unlock(&lock);
+
+ if (server_create_failed) {
+ return NULL;
+ }
+
+ local_printf("Client is running...\n");
+ sock = socket(AF_INET, SOCK_STREAM, 0);
+ if (sock < 0) {
+ perror("Create a socket failed");
+ return NULL;
+ }
+
+ /* 127.0.0.1:1234 */
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(1234);
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+ if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ perror("Connect failed");
+ goto fail;
+ }
+
+ local_printf("Start receiving. \n");
+ recv_len = recvmsg(sock, &msg, 0);
+ if (recv_len < 0) {
+ perror("Recvmsg failed");
+ goto fail;
+ }
+
+ local_printf("Receive %ld bytes successlly!\n", recv_len);
+ assert(recv_len == 106);
+
+ local_printf("Data:\n");
+ char *s = msg.msg_iov->iov_base;
+ while (strlen(s) > 0) {
+ local_printf(" %s\n", s);
+ s += strlen(s) + 1;
+ }
+
+fail:
+ shutdown(sock, SHUT_RD);
+ close(sock);
+ return NULL;
+}
+
+int
+main(int argc, char *argv[])
+{
+ pthread_t cs[2] = { 0 };
+ uint8_t i = 0;
+ int ret = EXIT_SUCCESS;
+
+ if (pthread_mutex_init(&lock, NULL)) {
+ perror("Initialize mutex failed");
+ ret = EXIT_FAILURE;
+ goto RETURN;
+ }
+
+ if (pthread_cond_init(&cond, NULL)) {
+ perror("Initialize condition failed");
+ ret = EXIT_FAILURE;
+ goto DESTROY_MUTEX;
+ }
+
+ if (pthread_create(&cs[0], NULL, run_as_server, NULL)) {
+ perror("Create a server thread failed");
+ ret = EXIT_FAILURE;
+ goto DESTROY_COND;
+ }
+
+ if (pthread_create(&cs[1], NULL, run_as_client, NULL)) {
+ perror("Create a client thread failed");
+ ret = EXIT_FAILURE;
+ goto DESTROY_COND;
+ }
+
+ for (i = 0; i < 2; i++) {
+ pthread_join(cs[i], NULL);
+ }
+
+DESTROY_COND:
+ pthread_cond_destroy(&cond);
+DESTROY_MUTEX:
+ pthread_mutex_destroy(&lock);
+RETURN:
+ return ret;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/CMakeLists.txt
new file mode 100644
index 000000000..c528fe16d
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/CMakeLists.txt
@@ -0,0 +1,210 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required (VERSION 3.14)
+
+include(CheckPIESupported)
+
+if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
+ project(c-api)
+else()
+ project (c-api C ASM)
+ enable_language (ASM_MASM)
+endif()
+
+if(NOT CMAKE_BUILD_TYPE)
+ set(CMAKE_BUILD_TYPE Release)
+endif()
+
+set(CMAKE_CXX_STANDARD 14)
+################ runtime settings ################
+
+string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
+if (APPLE)
+ add_definitions(-DBH_PLATFORM_DARWIN)
+endif ()
+
+# Reset default linker flags
+set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
+
+# WAMR features switch
+
+# Set WAMR_BUILD_TARGET, currently values supported:
+# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
+# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
+if (NOT DEFINED WAMR_BUILD_TARGET)
+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
+ set (WAMR_BUILD_TARGET "AARCH64")
+ elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
+ set (WAMR_BUILD_TARGET "RISCV64")
+ elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
+ # Build as X86_64 by default in 64-bit platform
+ set (WAMR_BUILD_TARGET "X86_64")
+ elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
+ # Build as X86_32 by default in 32-bit platform
+ set (WAMR_BUILD_TARGET "X86_32")
+ else ()
+ message(SEND_ERROR "Unsupported build target platform!")
+ endif ()
+endif ()
+
+if(NOT DEFINED WAMR_BUILD_INTERP)
+ set(WAMR_BUILD_INTERP 1)
+endif()
+
+if(NOT DEFINED WAMR_BUILD_AOT)
+ set(WAMR_BUILD_AOT 0)
+endif()
+
+if(NOT DEFINED WAMR_BUILD_JIT)
+ set(WAMR_BUILD_JIT 0)
+endif()
+
+set(WAMR_BUILD_LIBC_BUILTIN 1)
+set(WAMR_BUILD_LIBC_WASI 0)
+set(WAMR_BUILD_MULTI_MODULE 1)
+set(WAMR_BUILD_DUMP_CALL_STACK 1)
+set(WAMR_BUILD_REF_TYPES 1)
+
+if(NOT DEFINED WAMR_BUILD_FAST_INTERP)
+ set(WAMR_BUILD_FAST_INTERP 1)
+endif()
+
+if (NOT MSVC)
+ # compiling and linking flags
+ if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+ set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+ endif ()
+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
+ if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
+ if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
+ endif ()
+ endif ()
+endif()
+# build out vmlib
+set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
+
+if (NOT DEFINED SANITIZER)
+ set(SANITIZER "")
+elseif (SANITIZER STREQUAL "ubsan")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=alignment" )
+ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
+elseif (NOT (SANITIZER STREQUAL "") )
+ message(SEND_ERROR "Unsupported sanitizer: ${SANITIZER}")
+endif()
+
+add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE})
+if (MSVC)
+ target_compile_definitions(vmlib PRIVATE WASM_API_EXTERN=)
+endif()
+target_link_libraries (vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
+
+if (WAMR_BUILD_WASM_CACHE EQUAL 1)
+ target_link_libraries(vmlib boringssl_crypto)
+endif ()
+################################################
+
+################ application related ################
+## locate wat2wasm
+find_program(WAT2WASM
+ wat2wasm
+ PATHS /opt/wabt/bin
+ REQUIRED
+)
+
+if(NOT WAT2WASM)
+ message(SEND_ERROR "can not find wat2wasm")
+else ()
+ execute_process(COMMAND ${WAT2WASM} --version
+ OUTPUT_VARIABLE WAT2WASM_VERSION_OUTPUT)
+ string(STRIP ${WAT2WASM_VERSION_OUTPUT} WAT2WASM_VERSION)
+ message("-- Found wat2wasm ${WAT2WASM_VERSION}")
+endif()
+
+if (${WAT2WASM_VERSION} VERSION_LESS 1.0.26)
+ set(WAT2WASM_FLAGS "--enable-reference-types")
+endif ()
+
+if(${WAMR_BUILD_AOT} EQUAL 1)
+ ## locate wamrc
+ find_program(WAMRC
+ wamrc
+ PATHS ${WAMR_ROOT_DIR}/wamr-compiler/build/
+ )
+
+ if(NOT WAMRC)
+ message(SEND_ERROR "can not find wamrc. refer to \
+ https://github.com/bytecodealliance/wasm-micro-runtime#build-wamrc-aot-compiler"
+ )
+ endif()
+endif()
+include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
+
+set(MM_UTIL src/utils/multi_module_utils.c)
+# build executable for each .c
+list(APPEND EXAMPLES callback callback_chain empty_imports global hello hostref memory reflect table trap)
+# FIXME enable both in the future
+#list(APPEND EXAMPLES clone threads)
+# FIXME
+# if(WAMR_BUILD_JIT EQUAL 1 AND WAMR_BUILD_LAZY_JIT EQUAL 0)
+# list(APPEND EXAMPLES serialize)
+# endif()
+
+check_pie_supported()
+
+include(CTest)
+enable_testing()
+
+foreach(EX ${EXAMPLES})
+ set(SRC ${CMAKE_CURRENT_LIST_DIR}/src/${EX}.c)
+
+ add_executable(${EX} ${SRC} ${UNCOMMON_SHARED_SOURCE} ${MM_UTIL})
+ set_target_properties (${EX} PROPERTIES POSITION_INDEPENDENT_CODE ON)
+ target_include_directories(${EX} PRIVATE ${UNCOMMON_SHARED_DIR})
+ target_link_libraries(${EX} vmlib)
+ if (MSVC)
+ target_compile_definitions(${EX} PRIVATE WASM_API_EXTERN=)
+ endif()
+
+ # wat to wasm
+ set(WAT ${CMAKE_CURRENT_LIST_DIR}/src/${EX}.wat)
+
+ add_custom_target(${EX}_WASM
+ COMMAND ${WAT2WASM} ${WAT} ${WAT2WASM_FLAGS} -o ${PROJECT_BINARY_DIR}/${EX}.wasm
+ DEPENDS ${WAT}
+ BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.wasm
+ VERBATIM
+ )
+ add_dependencies(${EX} ${EX}_WASM)
+
+ # generate .aot file
+ if(${WAMR_BUILD_AOT} EQUAL 1)
+ add_custom_target(${EX}_AOT
+ COMMAND ${WAMRC} -o ${PROJECT_BINARY_DIR}/${EX}.aot
+ ${PROJECT_BINARY_DIR}/${EX}.wasm
+ DEPENDS ${EX}_WASM
+ BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.aot
+ VERBATIM
+ COMMENT "generate a aot file ${PROJECT_BINARY_DIR}/${EX}.aot"
+ )
+ add_dependencies(${EX} ${EX}_AOT)
+ endif()
+
+ # run `ctest --test-dir build`
+ add_test(NAME Test_${EX}
+ COMMAND ./${EX}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ )
+endforeach()
+
+if (CMAKE_BUILD_TYPE STREQUAL "Debug")
+ find_program(VALGRIND
+ valgrind
+ REQUIRED
+ )
+
+ # run `ctest -T memcheck -V --test-dir build`
+endif()
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/README.md b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/README.md
new file mode 100644
index 000000000..b23276706
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/README.md
@@ -0,0 +1,50 @@
+WAMR supports *wasm-c-api* in both *interpreter* mode and *aot* mode.
+
+Before staring, we need to download and intall [WABT](https://github.com/WebAssembly/wabt/releases/latest).
+
+``` shell
+$ cd /opt
+$ wget https://github.com/WebAssembly/wabt/releases/download/1.0.31/wabt-1.0.31-ubuntu.tar.gz
+$ tar -xzf wabt-1.0.31-ubuntu.tar.gz
+$ mv wabt-1.0.31 wabt
+```
+
+By default, all samples are compiled and run in "interpreter" mode.
+
+
+``` shell
+$ mkdir build
+$ cd build
+$ cmake ..
+$ make
+$ # it will build a library with c-api supporting.
+$ # Also copy *.wasm from ../src/
+$ # and generate executable files
+$ # now, it is ok to run samples
+$ ./hello
+$ ...
+$ ./global
+$ ...
+$ ./callback
+$ ...
+```
+
+They can be compiled and run in *aot* mode when some compiling flags are given.
+
+``` shell
+$ mkdir build
+$ cd build
+$ cmake -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_AOT=1 ..
+$ make
+$ # it will build a library with c-api supporting.
+$ # Also copy *.wasm from ../src/
+$ # and transform *.wasm to *.aot
+$ # and generate executable files
+$ # now, it is ok to run samples
+$ ./hello
+$ ...
+$ ./global
+$ ...
+$ ./callback
+$ ...
+```
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/LICENSE b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/LICENSE
new file mode 100644
index 000000000..8f71f43fe
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/LICENSE
@@ -0,0 +1,202 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright {yyyy} {name of copyright owner}
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.c
new file mode 100644
index 000000000..ccb9ec067
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.c
@@ -0,0 +1,193 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+// Print a Wasm value
+void wasm_val_print(wasm_val_t val) {
+ switch (val.kind) {
+ case WASM_I32: {
+ printf("%" PRIu32, val.of.i32);
+ } break;
+ case WASM_I64: {
+ printf("%" PRIu64, val.of.i64);
+ } break;
+ case WASM_F32: {
+ printf("%f", val.of.f32);
+ } break;
+ case WASM_F64: {
+ printf("%g", val.of.f64);
+ } break;
+ case WASM_ANYREF:
+ case WASM_FUNCREF: {
+ if (val.of.ref == NULL) {
+ printf("null");
+ } else {
+ printf("ref(%p)", val.of.ref);
+ }
+ } break;
+ }
+}
+
+// A function to be called from Wasm code.
+own wasm_trap_t* print_callback(
+ const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ printf("Calling back...\n> ");
+ wasm_val_print(args->data[0]);
+ printf("\n");
+
+ wasm_val_copy(&results->data[0], &args->data[0]);
+ return NULL;
+}
+
+
+// A function closure.
+own wasm_trap_t* closure_callback(
+ void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ int i = *(int*)env;
+ printf("Calling back closure...\n");
+ printf("> %d\n", i);
+
+ results->data[0].kind = WASM_I32;
+ results->data[0].of.i32 = (int32_t)i;
+ return NULL;
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("callback.aot", "rb");
+#else
+ FILE* file = fopen("callback.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Create external print functions.
+ printf("Creating callback...\n");
+ own wasm_functype_t* print_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
+ own wasm_func_t* print_func = wasm_func_new(store, print_type, print_callback);
+
+ int i = 42;
+ own wasm_functype_t* closure_type = wasm_functype_new_0_1(wasm_valtype_new_i32());
+ own wasm_func_t* closure_func = wasm_func_new_with_env(store, closure_type, closure_callback, &i, NULL);
+
+ wasm_functype_delete(print_type);
+ wasm_functype_delete(closure_type);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_t* externs[] = {
+ wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func)
+ };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ wasm_func_delete(print_func);
+ wasm_func_delete(closure_func);
+
+ // Extract export.
+ printf("Extracting export...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size == 0) {
+ printf("> Error accessing exports!\n");
+ return 1;
+ }
+ const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
+ if (run_func == NULL) {
+ printf("> Error accessing export!\n");
+ return 1;
+ }
+
+ wasm_module_delete(module);
+ wasm_instance_delete(instance);
+
+ // Call.
+ printf("Calling export...\n");
+ wasm_val_t as[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
+ wasm_val_t rs[1] = { WASM_INIT_VAL };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(as);
+ wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
+ wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ return 1;
+ }
+
+ wasm_extern_vec_delete(&exports);
+
+ // Print result.
+ printf("Printing result...\n");
+ printf("> %u\n", rs[0].of.i32);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.wat
new file mode 100644
index 000000000..d86195f51
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback.wat
@@ -0,0 +1,10 @@
+(module
+ (func $print (import "" "print") (param i32) (result i32))
+ (func $closure (import "" "closure") (result i32))
+ (func (export "run") (param $x i32) (param $y i32) (result i32)
+ (i32.add
+ (call $print (i32.add (local.get $x) (local.get $y)))
+ (call $closure)
+ )
+ )
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.c
new file mode 100644
index 000000000..e4f5801dc
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.c
@@ -0,0 +1,294 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+static const byte_t *
+get_memory_data(uint32_t offset, uint32_t length);
+
+static bool
+call_wasm_function(uint32_t export_id, const wasm_val_vec_t *args,
+ wasm_val_vec_t *results, const char *name);
+
+/************************ IMPORTED FUNCTIONS **************************/
+
+// (nil) -> i32
+#define FUNCTION_TYPE_NIL_I32 wasm_functype_new_0_1(wasm_valtype_new_i32())
+// (i32, i32) -> nil
+#define FUNCTION_TYPE_I32X2_NIL \
+ wasm_functype_new_2_0(wasm_valtype_new_i32(), wasm_valtype_new_i32())
+
+/* IMPORT FUNCTION LIST */
+#define IMPORT_FUNCTION_LIST(V) \
+ V(get_pairs, 0, FUNCTION_TYPE_NIL_I32) \
+ V(log, 1, FUNCTION_TYPE_I32X2_NIL)
+
+/* EXPORT FUNCTION LIST */
+#define EXPORT_FUNCTION_LIST(V) \
+ V(on_start) \
+ V(on_stop) \
+ V(malloc) \
+ V(free)
+
+enum EXPORT_ITEM_NAME {
+#define DEFINE_ENUM(name) e_##name,
+ EXPORT_FUNCTION_LIST(DEFINE_ENUM)
+#undef DEFINE_ENUM
+ e_MEMORY,
+};
+
+#define DEFINE_FUNCTION(name) \
+ wasm_trap_t *STUB_##name(const wasm_val_vec_t *args, \
+ wasm_val_vec_t *results)
+
+#define DEFINE_EMPTY_FUNCTION(name) \
+ DEFINE_FUNCTION(name) \
+ { \
+ printf("[WASM -> NATIVE] calling back %s\n", __FUNCTION__); \
+ return NULL; \
+ }
+#undef DEFINE_EMPTY_FUNCTION
+
+DEFINE_FUNCTION(get_pairs)
+{
+ wasm_val_vec_t as = { 0 };
+ wasm_val_t data[1] = { WASM_I32_VAL(10) };
+ wasm_val_vec_new(&as, 1, data);
+ if (as.data == NULL) {
+ printf("ERROR: create parameters failed\n");
+ return NULL;
+ }
+
+ call_wasm_function(e_malloc, &as, results, "malloc");
+
+ wasm_val_vec_delete(&as);
+ return NULL;
+}
+
+DEFINE_FUNCTION(log)
+{
+ wasm_val_t offset = args->data[0];
+ wasm_val_t length = args->data[1];
+ const byte_t *data = NULL;
+
+ printf("[WASM -> NATIVE] calling back %s\n", __FUNCTION__);
+
+ if (offset.kind != WASM_I32 || length.kind != WASM_I32) {
+ printf("> Error value type!\n");
+ }
+
+ if (!(data = get_memory_data(offset.of.i32, length.of.i32))) {
+ return NULL;
+ }
+
+ if (data[length.of.i32 - 1]) {
+ printf("> Error terminated character\n");
+ return NULL;
+ }
+
+ printf("[WASM_LOG] %s\n", data);
+ return NULL;
+}
+
+/**********************************************************************/
+// all exportted wasm functions. check with "/opt/wabt/bin/wasm-objdump -x -j
+// Export X.wasm" -1: memory 0-32: functions
+static own wasm_extern_vec_t exports = { 0 };
+
+static const byte_t *
+get_memory_data(uint32_t offset, uint32_t length)
+{
+ wasm_memory_t *memory;
+
+ if (!(memory = wasm_extern_as_memory(exports.data[e_MEMORY]))) {
+ return NULL;
+ }
+
+ byte_t *base = wasm_memory_data(memory);
+ size_t size = wasm_memory_data_size(memory);
+ if (!base || offset + length > size) {
+ return NULL;
+ }
+
+ printf("[NATIVE -> WASM] accessing the memory...\n");
+
+ return base + offset;
+}
+
+static bool
+call_wasm_function(uint32_t export_id, const wasm_val_vec_t *args,
+ wasm_val_vec_t *results, const char *name)
+{
+ const wasm_func_t *function;
+ wasm_trap_t *trap;
+
+ printf("[NATIVE -> WASM] calling func %s...\n", name);
+
+ if (!(function = wasm_extern_as_func(exports.data[export_id]))) {
+ printf("> Error get export function %u\n", export_id);
+ return false;
+ }
+
+ if ((trap = wasm_func_call(function, args, results))) {
+ own wasm_message_t message = { 0 };
+ wasm_trap_message(trap, &message);
+
+ if (message.data) {
+ printf("> Error calling function %s\n", message.data);
+ }
+ else {
+ printf("> Error calling function");
+ }
+
+ wasm_name_delete(&message);
+ wasm_trap_delete(trap);
+ return false;
+ }
+ return true;
+}
+
+int
+main(int argc, const char *argv[])
+{
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t *engine = wasm_engine_new();
+ wasm_store_t *store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE *file = fopen("callback_chain.aot", "rb");
+#else
+ FILE *file = fopen("callback_chain.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t *module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+
+ // Create external functions.
+ printf("Creating callback...\n");
+#define IMPORT_FUNCTION_VARIABLE_NAME(name, ...) \
+ own wasm_func_t *function_##name = NULL;
+ IMPORT_FUNCTION_LIST(IMPORT_FUNCTION_VARIABLE_NAME)
+#undef IMPORT_FUNCTION_VARIABLE_NAME
+
+#define CREATE_WASM_FUNCTION(name, index, CREATE_FUNC_TYPE) \
+ { \
+ own wasm_functype_t *type = CREATE_FUNC_TYPE; \
+ if (!(function_##name = wasm_func_new(store, type, STUB_##name))) { \
+ printf("> Error creating new function\n"); \
+ return 1; \
+ } \
+ wasm_functype_delete(type); \
+ }
+ IMPORT_FUNCTION_LIST(CREATE_WASM_FUNCTION)
+#undef CREATE_WASM_FUNCTION
+
+ wasm_extern_t *fs[2] = { 0 };
+#define ADD_TO_FUNCTION_LIST(name, index, ...) \
+ fs[index] = wasm_func_as_extern(function_##name);
+ IMPORT_FUNCTION_LIST(ADD_TO_FUNCTION_LIST)
+#undef ADD_TO_FUNCTION_LIST
+
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(fs);
+ own wasm_instance_t *instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+#define DESTROY_WASM_FUNCITON(name, index, ...) \
+ wasm_func_delete(function_##name);
+ IMPORT_FUNCTION_LIST(DESTROY_WASM_FUNCITON)
+#undef DESTROY_WASM_FUNCITON
+
+ // Extract export.
+ printf("Extracting export...\n");
+ wasm_instance_exports(instance, &exports);
+ if (!exports.size) {
+ printf("> Error accessing exports!\n");
+ return 1;
+ }
+
+ wasm_module_delete(module);
+ wasm_instance_delete(instance);
+
+ // Call.
+ printf("Calling export...\n");
+
+ if (!call_wasm_function(e_on_start, NULL, NULL, "on_start")) {
+ printf("> Error calling on_start\n");
+ return 1;
+ }
+
+ if (!call_wasm_function(e_on_stop, NULL, NULL, "on_stop")) {
+ printf("> Error calling on_stop\n");
+ return 1;
+ }
+
+ wasm_extern_vec_delete(&exports);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.wat
new file mode 100644
index 000000000..80bbf4f74
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.wat
@@ -0,0 +1,32 @@
+;; Copyright (C) 2019 Intel Corporation. All rights reserved.
+;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+(module
+ (func $get_pairs (import "" "get_pairs") (result i32))
+ (func $log (import "" "log") (param i32 i32))
+
+ (func $on_start (export "on_start")
+ (call $log (i32.const 0) (i32.const 9))
+ (call $get_pairs)
+ (drop)
+ )
+
+ (func $on_stop (export "on_stop")
+ (call $log (i32.const 9) (i32.const 8))
+ )
+
+ (func $malloc (export "malloc") (param i32) (result i32)
+ (call $log (i32.const 17) (i32.const 7))
+ (i32.const 64)
+ )
+
+ (func $free(export "free") (param i32)
+ (call $log (i32.const 24) (i32.const 5))
+ )
+
+ (memory (export "memory") 1)
+ (data (i32.const 0) "on_start")
+ (data (i32.const 9) "on_stop")
+ (data (i32.const 17) "malloc")
+ (data (i32.const 24) "free")
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.c
new file mode 100644
index 000000000..da83b3790
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.c
@@ -0,0 +1,535 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "wasm_c_api.h"
+
+#define WORKER_NUMBER 10
+
+/******************************* VM *******************************/
+/* Use wasm_vm_t and vm_xxx to simulate a minimal Wasm VM in Envoy */
+
+typedef struct _vm {
+ wasm_engine_t *engine;
+ wasm_store_t *store;
+ wasm_module_t *module;
+ wasm_shared_module_t *shared_module;
+ wasm_instance_t *instance;
+ wasm_func_t **function_list;
+ wasm_memory_t *memory;
+ wasm_table_t *table;
+ wasm_extern_vec_t *exports;
+} wasm_vm_t;
+
+typedef enum _clone_level {
+ not_cloneable = 0,
+ compiled_bytecode,
+ instantiated_module
+} clone_level;
+
+typedef struct _thread_arg_t {
+ char name[32];
+ bool *ready_go_flag;
+ pthread_mutex_t *ready_go_lock;
+ pthread_cond_t *ready_go_cond;
+ const wasm_vm_t *base_vm;
+} thread_arg_t;
+
+wasm_vm_t *
+vm_new()
+{
+ wasm_vm_t *vm = NULL;
+
+ vm = malloc(sizeof(struct _vm));
+ if (!vm)
+ goto fail;
+
+ memset(vm, 0, sizeof(wasm_vm_t));
+
+ vm->engine = wasm_engine_new();
+ if (!vm->engine)
+ goto fail;
+
+ vm->store = wasm_store_new(vm->engine);
+ if (!vm->store)
+ goto fail;
+
+ return vm;
+
+fail:
+ if (vm) {
+ if (vm->engine)
+ wasm_engine_delete(vm->engine);
+
+ free(vm);
+ }
+ return NULL;
+}
+
+wasm_vm_t *
+vm_release(wasm_vm_t *vm)
+{
+ if (!vm)
+ return NULL;
+
+ if (vm->function_list) {
+ free(vm->function_list);
+ vm->function_list = NULL;
+ }
+
+ vm->memory = NULL;
+
+ if (vm->exports) {
+ wasm_extern_vec_delete(vm->exports);
+ free(vm->exports);
+ vm->exports = NULL;
+ }
+
+ wasm_instance_delete(vm->instance);
+ vm->instance = NULL;
+
+ wasm_shared_module_delete(vm->shared_module);
+ vm->shared_module = NULL;
+
+ wasm_module_delete(vm->module);
+ vm->module = NULL;
+
+ wasm_store_delete(vm->store);
+ vm->store = NULL;
+
+ wasm_engine_delete(vm->engine);
+ vm->engine = NULL;
+
+ free(vm);
+ return NULL;
+}
+
+bool
+vm_load(wasm_vm_t *vm, const wasm_byte_vec_t *binary)
+{
+ vm->module = wasm_module_new(vm->store, binary);
+ vm->shared_module = wasm_module_share(vm->module);
+ return vm->module != NULL;
+}
+
+bool
+vm_link(wasm_vm_t *vm, wasm_extern_vec_t *imports)
+{
+ vm->instance = wasm_instance_new(vm->store, vm->module, imports, NULL);
+ if (!vm->instance)
+ goto fail;
+
+ vm->exports = malloc(sizeof(wasm_extern_vec_t));
+ if (!vm->exports)
+ goto fail;
+
+ memset(vm->exports, 0, sizeof(wasm_extern_vec_t));
+ wasm_instance_exports(vm->instance, vm->exports);
+ /* an exported memory, and two exported functions */
+ assert(vm->exports->size == 3);
+
+ /* bind memory */
+ assert(wasm_extern_kind(vm->exports->data[0]) == WASM_EXTERN_MEMORY);
+ vm->memory = wasm_extern_as_memory(vm->exports->data[0]);
+
+ vm->function_list = malloc(2 * sizeof(wasm_func_t *));
+ if (!vm->function_list)
+ goto fail;
+
+ memset(vm->function_list, 0, 2 * sizeof(wasm_func_t *));
+
+ /* bind wasm_set_byte(...) */
+ assert(wasm_extern_kind(vm->exports->data[1]) == WASM_EXTERN_FUNC);
+ vm->function_list[0] = wasm_extern_as_func(vm->exports->data[1]);
+
+ /* bind wasm_get_byte(...) */
+ assert(wasm_extern_kind(vm->exports->data[2]) == WASM_EXTERN_FUNC);
+ vm->function_list[1] = wasm_extern_as_func(vm->exports->data[2]);
+
+ return true;
+fail:
+ return false;
+}
+
+wasm_vm_t *
+vm_clone_from_module(const wasm_vm_t *base)
+{
+ printf("Initializing...\n");
+ wasm_vm_t *secondary = NULL;
+
+ secondary = vm_new();
+ if (secondary) {
+ printf("Reuse module and bypass vm_load()...");
+ secondary->module =
+ wasm_module_obtain(base->store, base->shared_module);
+ if (!secondary->module)
+ secondary = vm_release(secondary);
+ }
+
+ return secondary;
+}
+
+wasm_vm_t *
+vm_clone_from_instance(const wasm_vm_t *base)
+{
+ /**
+ * if do a clone of the level instantiated_module, need to malloc and
+ * initialie
+ * - global. WASMGlobalIntance and global data
+ * - memory. WAAMMemoryInstance, memory_data and heap
+ * - table. WASMTableInstance, table_data
+ * - exports. all global, memory and table
+ *
+ * it is almost everything in wasm_instantiate() except funciton.
+ */
+ (void)base;
+ printf("Unsupported\n");
+ return NULL;
+}
+
+wasm_vm_t *
+vm_clone(const wasm_vm_t *base, clone_level level)
+{
+ if (level == not_cloneable)
+ return NULL;
+
+ if (level == compiled_bytecode)
+ return vm_clone_from_module(base);
+ else
+ return vm_clone_from_instance(base);
+}
+
+bool
+vm_memory_set_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t byte)
+{
+ byte_t *data = wasm_memory_data(vm->memory);
+ assert(data);
+ *(data + offset) = byte;
+ return true;
+}
+
+bool
+vm_memory_get_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t *byte)
+{
+ byte_t *data = wasm_memory_data(vm->memory);
+ assert(data);
+ *byte = *(data + offset);
+ return true;
+}
+
+bool
+vm_function_set_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t byte)
+{
+ wasm_val_t a_v[2] = { WASM_I32_VAL(offset), WASM_I32_VAL(byte) };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(a_v);
+ wasm_val_vec_t results = WASM_EMPTY_VEC;
+ wasm_trap_t *trap = wasm_func_call(vm->function_list[0], &args, &results);
+ if (trap) {
+ printf("call wasm_set_byte failed");
+ wasm_trap_delete(trap);
+ return false;
+ }
+
+ return true;
+}
+
+bool
+vm_function_get_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t *byte)
+{
+ wasm_val_t a_v[1] = { WASM_I32_VAL(offset) };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(a_v);
+ wasm_val_t r_v[1] = { WASM_INIT_VAL };
+ wasm_val_vec_t results = WASM_ARRAY_VEC(r_v);
+ wasm_trap_t *trap = wasm_func_call(vm->function_list[1], &args, &results);
+ if (trap) {
+ printf("call wasm_get_byte failed");
+ wasm_trap_delete(trap);
+ return false;
+ }
+
+ assert(results.data->kind == WASM_I32);
+ *byte = results.data->of.i32;
+ return true;
+}
+
+static bool
+load_wasm_file_content(const char *file_name, wasm_byte_vec_t *out)
+{
+ bool ret = false;
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE *file = fopen(file_name, "rb");
+#else
+ FILE *file = fopen(file_name, "rb");
+#endif
+ if (!file) {
+ printf("> Error loading .wasm!\n");
+ goto quit;
+ }
+
+ int offset = fseek(file, 0L, SEEK_END);
+ if (offset == -1) {
+ printf("> Error loading .wasm!\n");
+ goto close_file;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading .wasm!\n");
+ goto close_file;
+ }
+
+ offset = fseek(file, 0L, SEEK_SET);
+ if (offset == -1) {
+ printf("> Error loading .wasm!\n");
+ goto close_file;
+ }
+
+ wasm_byte_vec_new_uninitialized(out, file_size);
+ if (fread(out->data, file_size, 1, file) != 1) {
+ printf("> Error loading content!\n");
+ goto close_file;
+ }
+
+ ret = true;
+close_file:
+ fclose(file);
+quit:
+ return ret;
+}
+
+static pthread_key_t name_key;
+
+wasm_trap_t *
+report_cb(const wasm_val_vec_t *args, wasm_val_vec_t *results)
+{
+ (void)results;
+
+ assert(args->data[0].kind == WASM_I32);
+ uint32_t chk_pnt_no = args->data[0].of.i32;
+
+ char *name = pthread_getspecific(name_key);
+ printf("[%s] Pass CHK POINT #%u\n", name, chk_pnt_no);
+
+ return NULL;
+}
+
+bool
+run_code_start(wasm_vm_t **out)
+{
+ bool ret = false;
+
+ printf("Initializing...\n");
+ wasm_vm_t *vm = vm_new();
+ if (!vm)
+ goto fail;
+
+ printf("Loading binary...\n");
+ wasm_byte_vec_t binary = { 0 };
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ const char *file_name = "clone.aot";
+#else
+ const char *file_name = "clone.wasm";
+#endif
+ if (!load_wasm_file_content(file_name, &binary))
+ goto release_vm;
+
+ printf("Compiling module...\n");
+ ret = vm_load(vm, &binary);
+ wasm_byte_vec_delete(&binary);
+ if (!ret)
+ goto release_vm;
+
+ printf("Creating callback...\n");
+ wasm_functype_t *callback_type =
+ wasm_functype_new_1_0(wasm_valtype_new_i32());
+ if (!callback_type)
+ goto release_vm;
+
+ wasm_func_t *callback = wasm_func_new(vm->store, callback_type, report_cb);
+ wasm_functype_delete(callback_type);
+ if (!callback)
+ goto release_vm;
+
+ printf("Instantiating module...\n");
+ wasm_extern_t *externs[] = { wasm_func_as_extern(callback) };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ ret = vm_link(vm, &imports);
+ wasm_func_delete(callback);
+ if (!ret)
+ goto release_vm;
+
+ *out = vm;
+ return true;
+
+release_vm:
+ vm_release(vm);
+fail:
+ return false;
+}
+
+bool
+run_warm_start_w_compiled_bytecode(const wasm_vm_t *first, wasm_vm_t **out)
+{
+ bool ret;
+ wasm_vm_t *secondary = vm_clone(first, compiled_bytecode);
+ if (!secondary)
+ goto fail;
+
+ printf("Creating callback...\n");
+ wasm_functype_t *callback_type =
+ wasm_functype_new_1_0(wasm_valtype_new_i32());
+ if (!callback_type)
+ goto release_vm;
+
+ wasm_func_t *callback =
+ wasm_func_new(secondary->store, callback_type, report_cb);
+ wasm_functype_delete(callback_type);
+ if (!callback)
+ goto release_vm;
+
+ printf("Instantiating module...\n");
+ wasm_extern_t *externs[] = { wasm_func_as_extern(callback) };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ ret = vm_link(secondary, &imports);
+ wasm_func_delete(callback);
+ if (!ret)
+ goto release_vm;
+
+ *out = secondary;
+ return true;
+
+release_vm:
+ vm_release(secondary);
+fail:
+ return false;
+}
+
+bool
+run_warm_start_w_instantiated_module(const wasm_vm_t *first, wasm_vm_t **out)
+{
+ wasm_vm_t *secondary = vm_clone(first, instantiated_module);
+ if (!secondary)
+ return false;
+
+ *out = secondary;
+ return true;
+}
+
+void
+run_test(const wasm_vm_t *vm)
+{
+ uint8_t byte = 0xFF;
+
+ /* read initialization */
+ vm_function_get_byte(vm, 10, &byte);
+ assert(byte == 0x0);
+ vm_memory_get_byte(vm, 10, &byte);
+ assert(byte == 0x0);
+
+ /* read after writing */
+ vm_function_set_byte(vm, 16, 0xab);
+ vm_function_get_byte(vm, 16, &byte);
+ assert(byte == 0xab);
+
+ vm_memory_set_byte(vm, 16, 0xcd);
+ vm_memory_get_byte(vm, 16, &byte);
+ assert(byte == 0xcd);
+
+ /* reading and writing across */
+ vm_function_set_byte(vm, 16, 0xef);
+ vm_memory_get_byte(vm, 16, &byte);
+ assert(byte == 0xef);
+
+ vm_memory_set_byte(vm, 16, 0x67);
+ vm_function_get_byte(vm, 16, &byte);
+ assert(byte == 0x67);
+
+ printf("All Passed ...\n");
+}
+
+static void *
+thrd_func(void *arg)
+{
+ thread_arg_t *thrd_arg = (thread_arg_t *)arg;
+
+ sleep(rand() % 5);
+ printf("Running warm start at %s...\n", thrd_arg->name);
+
+ pthread_setspecific(name_key, thrd_arg->name);
+
+ wasm_vm_t *vm;
+ if (!run_warm_start_w_compiled_bytecode(thrd_arg->base_vm, &vm))
+ return NULL;
+
+ pthread_mutex_trylock(thrd_arg->ready_go_lock);
+ while (!(*thrd_arg->ready_go_flag)) {
+ pthread_cond_wait(thrd_arg->ready_go_cond, thrd_arg->ready_go_lock);
+ }
+ pthread_mutex_unlock(thrd_arg->ready_go_lock);
+
+ printf("Running test at %s...\n", thrd_arg->name);
+ run_test(vm);
+
+ vm_release(vm);
+ pthread_exit(NULL);
+ return NULL;
+}
+
+int
+main()
+{
+ int ret = EXIT_FAILURE;
+ bool ready_go_flag = false;
+ pthread_mutex_t ready_go_lock = PTHREAD_MUTEX_INITIALIZER;
+ pthread_cond_t ready_go_cond = PTHREAD_COND_INITIALIZER;
+ pthread_key_create(&name_key, NULL);
+ pthread_setspecific(name_key, "Execution Thread");
+
+ printf("Running cold start at the execution thread...\n");
+ wasm_vm_t *base_vm;
+ if (!run_code_start(&base_vm))
+ goto quit;
+ run_test(base_vm);
+
+ printf("Running warm start at other threads...\n");
+
+ pthread_t tids[WORKER_NUMBER] = { 0 };
+ thread_arg_t thrd_args[WORKER_NUMBER] = { 0 };
+ for (size_t i = 0; i < sizeof(tids) / sizeof(tids[0]); i++) {
+ thread_arg_t *thrd_arg = thrd_args + i;
+
+ snprintf(thrd_arg->name, 32, "Worker#%lu", i);
+ thrd_arg->ready_go_cond = &ready_go_cond;
+ thrd_arg->ready_go_lock = &ready_go_lock;
+ thrd_arg->ready_go_flag = &ready_go_flag;
+ thrd_arg->base_vm = base_vm;
+
+ int ret = pthread_create(&tids[i], NULL, thrd_func, thrd_arg);
+ if (ret != 0)
+ break;
+ }
+
+ sleep(1);
+ pthread_mutex_trylock(&ready_go_lock);
+ ready_go_flag = true;
+ pthread_mutex_unlock(&ready_go_lock);
+ pthread_cond_broadcast(&ready_go_cond);
+
+ sleep(3);
+ for (size_t i = 0; i < sizeof(tids) / sizeof(tids[0]); i++) {
+ if (tids[i] != 0)
+ pthread_join(tids[i], NULL);
+ }
+
+ vm_release(base_vm);
+ ret = EXIT_SUCCESS;
+quit:
+ return ret;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.wat
new file mode 100644
index 000000000..e9934cc0d
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/clone.wat
@@ -0,0 +1,15 @@
+(module
+ (func $report (import "" "report") (param i32))
+
+ (memory (export "mem") 1 1)
+
+ (func $wasm_set_byte (export "set_byte") (param i32 i32)
+ (call $report (i32.const 1))
+ (i32.store8 (local.get 0) (local.get 1))
+ )
+
+ (func $wasm_get_byte (export "get_byte") (param i32) (result i32)
+ (call $report (i32.const 2))
+ (i32.load(local.get 0))
+ )
+) \ No newline at end of file
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.c
new file mode 100644
index 000000000..c8788b51a
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.c
@@ -0,0 +1,122 @@
+#include <stdio.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("empty_imports.aot", "rb");
+#else
+ FILE* file = fopen("empty_imports.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Instantiate with non-null but empty imports array.
+ printf("Instantiating module...\n");
+ wasm_extern_vec_t imports = WASM_EMPTY_VEC;
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ // Run an exported function to verify that the instance was created correctly.
+ printf("Extracting export...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size == 0) {
+ printf("> Error accessing exports!\n");
+ return 1;
+ }
+
+ const wasm_func_t* add_func = wasm_extern_as_func(exports.data[0]);
+ if (add_func == NULL) {
+ printf("> Error accessing export!\n");
+ return 1;
+ }
+
+ wasm_module_delete(module);
+ wasm_instance_delete(instance);
+
+ printf("Calling export...\n");
+ wasm_val_t args[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
+ wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
+
+ wasm_val_t results[1] = { WASM_INIT_VAL };
+ wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
+
+ wasm_trap_t *trap = wasm_func_call(add_func, &args_vec, &results_vec);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ return 1;
+ }
+
+ if (results_vec.data[0].of.i32 != 7) {
+ printf("> Error calling function!\n");
+ return 1;
+ }
+
+ wasm_extern_vec_delete(&exports);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.wat
new file mode 100644
index 000000000..38639790b
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/empty_imports.wat
@@ -0,0 +1,5 @@
+(module
+ (func (export "add") (param i32 i32) (result i32)
+ (i32.add (local.get 0) (local.get 1))
+ )
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.c
new file mode 100644
index 000000000..91c8cb654
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.c
@@ -0,0 +1,278 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+wasm_global_t* get_export_global(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_global(exports->data[i])) {
+ printf("> Error accessing global export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_global(exports->data[i]);
+}
+
+wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
+ printf("> Error accessing function export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_func(exports->data[i]);
+}
+
+
+#define check(val, type, expected) \
+ if (val.of.type != expected) { \
+ printf("> Error reading value\n"); \
+ exit(1); \
+ }
+
+#define check_global(global, type, expected) \
+ { \
+ wasm_val_t val; \
+ wasm_global_get(global, &val); \
+ check(val, type, expected); \
+ }
+
+#define check_trap(trap) \
+ if (trap) { \
+ printf("> Error calling function\n"); \
+ wasm_trap_delete(trap); \
+ exit(1); \
+ }
+
+#define check_call(func, type, expected) \
+ { \
+ wasm_val_t vs[1]; \
+ wasm_val_vec_t args = WASM_EMPTY_VEC; \
+ wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
+ wasm_trap_t *trap = wasm_func_call(func, &args, &results); \
+ check_trap(trap); \
+ check(vs[0], type, expected); \
+ }
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("global.aot", "rb");
+#else
+ FILE* file = fopen("global.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Create external globals.
+ printf("Creating globals...\n");
+ own wasm_globaltype_t* const_f32_type = wasm_globaltype_new(
+ wasm_valtype_new(WASM_F32), WASM_CONST);
+ own wasm_globaltype_t* const_i64_type = wasm_globaltype_new(
+ wasm_valtype_new(WASM_I64), WASM_CONST);
+ own wasm_globaltype_t* var_f32_type = wasm_globaltype_new(
+ wasm_valtype_new(WASM_F32), WASM_VAR);
+ own wasm_globaltype_t* var_i64_type = wasm_globaltype_new(
+ wasm_valtype_new(WASM_I64), WASM_VAR);
+
+ wasm_val_t val_f32_1 = WASM_F32_VAL(1);
+ own wasm_global_t* const_f32_import =
+ wasm_global_new(store, const_f32_type, &val_f32_1);
+ wasm_val_t val_i64_2 = WASM_I64_VAL(2);
+ own wasm_global_t* const_i64_import =
+ wasm_global_new(store, const_i64_type, &val_i64_2);
+ wasm_val_t val_f32_3 = WASM_F32_VAL(3);
+ own wasm_global_t* var_f32_import =
+ wasm_global_new(store, var_f32_type, &val_f32_3);
+ wasm_val_t val_i64_4 = WASM_I64_VAL(4);
+ own wasm_global_t* var_i64_import =
+ wasm_global_new(store, var_i64_type, &val_i64_4);
+
+ wasm_globaltype_delete(const_f32_type);
+ wasm_globaltype_delete(const_i64_type);
+ wasm_globaltype_delete(var_f32_type);
+ wasm_globaltype_delete(var_i64_type);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_t* externs[] = {
+ wasm_global_as_extern(const_f32_import),
+ wasm_global_as_extern(const_i64_import),
+ wasm_global_as_extern(var_f32_import),
+ wasm_global_as_extern(var_i64_import)
+ };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ wasm_module_delete(module);
+
+ // Extract export.
+ printf("Extracting exports...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ size_t i = 0;
+ wasm_global_t* const_f32_export = get_export_global(&exports, i++);
+ wasm_global_t* const_i64_export = get_export_global(&exports, i++);
+ wasm_global_t* var_f32_export = get_export_global(&exports, i++);
+ wasm_global_t* var_i64_export = get_export_global(&exports, i++);
+ wasm_func_t* get_const_f32_import = get_export_func(&exports, i++);
+ wasm_func_t* get_const_i64_import = get_export_func(&exports, i++);
+ wasm_func_t* get_var_f32_import = get_export_func(&exports, i++);
+ wasm_func_t* get_var_i64_import = get_export_func(&exports, i++);
+ wasm_func_t* get_const_f32_export = get_export_func(&exports, i++);
+ wasm_func_t* get_const_i64_export = get_export_func(&exports, i++);
+ wasm_func_t* get_var_f32_export = get_export_func(&exports, i++);
+ wasm_func_t* get_var_i64_export = get_export_func(&exports, i++);
+ wasm_func_t* set_var_f32_import = get_export_func(&exports, i++);
+ wasm_func_t* set_var_i64_import = get_export_func(&exports, i++);
+ wasm_func_t* set_var_f32_export = get_export_func(&exports, i++);
+ wasm_func_t* set_var_i64_export = get_export_func(&exports, i++);
+
+ // Try cloning.
+ own wasm_global_t* copy = wasm_global_copy(var_f32_import);
+ assert(wasm_global_same(var_f32_import, copy));
+ wasm_global_delete(copy);
+
+ // Interact.
+ printf("Accessing globals...\n");
+
+ // Check initial values.
+ check_global(const_f32_import, f32, 1);
+ check_global(const_i64_import, i64, 2);
+ check_global(var_f32_import, f32, 3);
+ check_global(var_i64_import, i64, 4);
+ check_global(const_f32_export, f32, 5);
+ check_global(const_i64_export, i64, 6);
+ check_global(var_f32_export, f32, 7);
+ check_global(var_i64_export, i64, 8);
+
+ check_call(get_const_f32_import, f32, 1);
+ check_call(get_const_i64_import, i64, 2);
+ check_call(get_var_f32_import, f32, 3);
+ check_call(get_var_i64_import, i64, 4);
+ check_call(get_const_f32_export, f32, 5);
+ check_call(get_const_i64_export, i64, 6);
+ check_call(get_var_f32_export, f32, 7);
+ check_call(get_var_i64_export, i64, 8);
+
+ // Modify variables through API and check again.
+ wasm_val_t val33 = WASM_F32_VAL(33);
+ wasm_global_set(var_f32_import, &val33);
+ wasm_val_t val34 = WASM_I64_VAL(34);
+ wasm_global_set(var_i64_import, &val34);
+ wasm_val_t val37 = WASM_F32_VAL(37);
+ wasm_global_set(var_f32_export, &val37);
+ wasm_val_t val38 = WASM_I64_VAL(38);
+ wasm_global_set(var_i64_export, &val38);
+
+ check_global(var_f32_import, f32, 33);
+ check_global(var_i64_import, i64, 34);
+ check_global(var_f32_export, f32, 37);
+ check_global(var_i64_export, i64, 38);
+
+ check_call(get_var_f32_import, f32, 33);
+ check_call(get_var_i64_import, i64, 34);
+ check_call(get_var_f32_export, f32, 37);
+ check_call(get_var_i64_export, i64, 38);
+
+ // Modify variables through calls and check again.
+ wasm_val_vec_t res = WASM_EMPTY_VEC;
+ wasm_val_t vs73[] = { WASM_F32_VAL(73) };
+ wasm_val_vec_t args73 = WASM_ARRAY_VEC(vs73);
+ wasm_trap_t *trap = wasm_func_call(set_var_f32_import, &args73, &res);
+ check_trap(trap);
+
+ wasm_val_t vs74[] = { WASM_I64_VAL(74) };
+ wasm_val_vec_t args74 = WASM_ARRAY_VEC(vs74);
+ trap = wasm_func_call(set_var_i64_import, &args74, &res);
+ check_trap(trap);
+
+ wasm_val_t vs77[] = { WASM_F32_VAL(77) };
+ wasm_val_vec_t args77 = WASM_ARRAY_VEC(vs77);
+ trap = wasm_func_call(set_var_f32_export, &args77, &res);
+ check_trap(trap);
+
+ wasm_val_t vs78[] = { WASM_I64_VAL(78) };
+ wasm_val_vec_t args78 = WASM_ARRAY_VEC(vs78);
+ trap = wasm_func_call(set_var_i64_export, &args78, &res);
+ check_trap(trap);
+
+ check_global(var_f32_import, f32, 73);
+ check_global(var_i64_import, i64, 74);
+ check_global(var_f32_export, f32, 77);
+ check_global(var_i64_export, i64, 78);
+
+ check_call(get_var_f32_import, f32, 73);
+ check_call(get_var_i64_import, i64, 74);
+ check_call(get_var_f32_export, f32, 77);
+ check_call(get_var_i64_export, i64, 78);
+
+ wasm_global_delete(const_f32_import);
+ wasm_global_delete(const_i64_import);
+ wasm_global_delete(var_f32_import);
+ wasm_global_delete(var_i64_import);
+ wasm_extern_vec_delete(&exports);
+ wasm_instance_delete(instance);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.wat
new file mode 100644
index 000000000..dea085772
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/global.wat
@@ -0,0 +1,27 @@
+(module
+ (global $f32_import (import "" "const f32") f32)
+ (global $i64_import (import "" "const i64") i64)
+ (global $mut_f32_import (import "" "var f32") (mut f32))
+ (global $mut_i64_import (import "" "var i64") (mut i64))
+
+ (global $f32_export (export "const f32") f32 (f32.const 5))
+ (global $i64_export (export "const i64") i64 (i64.const 6))
+ (global $mut_f32_export (export "var f32") (mut f32) (f32.const 7))
+ (global $mut_i64_export (export "var i64") (mut i64) (i64.const 8))
+
+ (func (export "get const f32 import") (result f32) (global.get $f32_import))
+ (func (export "get const i64 import") (result i64) (global.get $i64_import))
+ (func (export "get var f32 import") (result f32) (global.get $mut_f32_import))
+ (func (export "get var i64 import") (result i64) (global.get $mut_i64_import))
+
+ (func (export "get const f32 export") (result f32) (global.get $f32_export))
+ (func (export "get const i64 export") (result i64) (global.get $i64_export))
+ (func (export "get var f32 export") (result f32) (global.get $mut_f32_export))
+ (func (export "get var i64 export") (result i64) (global.get $mut_i64_export))
+
+ (func (export "set var f32 import") (param f32) (global.set $mut_f32_import (local.get 0)))
+ (func (export "set var i64 import") (param i64) (global.set $mut_i64_import (local.get 0)))
+
+ (func (export "set var f32 export") (param f32) (global.set $mut_f32_export (local.get 0)))
+ (func (export "set var f64 export") (param i64) (global.set $mut_i64_export (local.get 0)))
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-0.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-0.wat
new file mode 100644
index 000000000..7ab897c73
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-0.wat
@@ -0,0 +1,8 @@
+;; Copyright (C) 2019 Intel Corporation. All rights reserved.
+;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+(module
+ (global $mut_f32_export (export "var f32") (mut f32) (f32.const 7))
+ (func (export "get var f32 export") (result f32) (global.get $mut_f32_export))
+ (func (export "set var f32 export") (param f32) (global.set $mut_f32_export (local.get 0)))
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-1.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-1.wat
new file mode 100644
index 000000000..927e420d7
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport-1.wat
@@ -0,0 +1,10 @@
+;; Copyright (C) 2019 Intel Corporation. All rights reserved.
+;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+(module
+ (global $mut_f32_import (export "var f32") (import "globalexportimport-0" "var f32") (mut f32))
+ (func (export "get var f32 export") (import "globalexportimport-0" "get var f32 export") (result f32))
+ (func (export "set var f32 export") (import "globalexportimport-0" "set var f32 export") (param f32))
+ (func (export "get var f32 import") (result f32) (global.get $mut_f32_import))
+ (func (export "set var f32 import") (param f32) (global.set $mut_f32_import (local.get 0)))
+) \ No newline at end of file
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport.c
new file mode 100644
index 000000000..1c1715f2e
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/globalexportimport.c
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+#include "wasm_export.h"
+#include "bh_platform.h"
+
+extern bool
+reader(const char *module_name, uint8 **p_buffer, uint32 *p_size);
+
+extern void
+destroyer(uint8 *buffer, uint32 size);
+
+#define own
+
+wasm_global_t* get_export_global(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_global(exports->data[i])) {
+ printf("> Error accessing global export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_global(exports->data[i]);
+}
+
+wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
+ printf("> Error accessing function export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_func(exports->data[i]);
+}
+
+
+#define check(val, type, expected) \
+ if (val.of.type != expected) { \
+ printf("> Expected reading value %f or %f \n", expected, expected); \
+ printf("> Error reading value %f or %f\n", val.of.type, val.of.type); \
+ }
+
+#define check_global(global, type, expected) \
+ { \
+ wasm_val_t val; \
+ wasm_global_get(global, &val); \
+ check(val, type, expected); \
+ }
+
+#define check_trap(trap) \
+ if (trap) { \
+ printf("> Error calling function\n"); \
+ wasm_trap_delete(trap); \
+ exit(1); \
+ }
+
+#define check_call(func, type, expected) \
+ { \
+ wasm_val_vec_t results; \
+ wasm_val_vec_new_uninitialized(&results, 1); \
+ wasm_trap_t *trap = wasm_func_call(func, NULL, &results); \
+ check_trap(trap); \
+ check(results.data[0], type, expected); \
+ }
+
+wasm_module_t * create_module_from_file(wasm_store_t* store, const char * filename)
+{
+ FILE* file = fopen(filename, "rb");
+ fseek(file, 0L, SEEK_END);
+ size_t file_size = ftell(file);
+ fseek(file, 0L, SEEK_SET);
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return NULL;
+ }
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return NULL;
+ }
+ wasm_byte_vec_delete(&binary);
+ fclose(file);
+ return module;
+}
+
+
+int main(int argc, const char* argv[]) {
+ wasm_runtime_set_module_reader(reader, destroyer);
+
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ wasm_module_t* moduleimport =
+ create_module_from_file(store, "globalimport.aot");
+#else
+ wasm_module_t* moduleimport =
+ create_module_from_file(store, "globalexportimport-1.wasm");
+#endif
+
+ if (!moduleimport) {
+ return 1;
+ }
+
+ // Instantiate.
+ printf("Instantiating Import module...\n");
+ own wasm_instance_t* instance_import =
+ wasm_instance_new(store, moduleimport, NULL, NULL); //after this var_f32_export->inst_comm_rt is module_import
+ if (!instance_import) {
+ printf("> Error instantiating Import module!\n");
+ return 1;
+ }
+ wasm_module_delete(moduleimport);
+
+ // Extract export.
+ printf("Extracting exports from Import module...\n");
+ own wasm_extern_vec_t exports_of_import;
+ wasm_instance_exports(instance_import, &exports_of_import);
+ int i = 0;
+ wasm_global_t *var_f32_export = get_export_global(&exports_of_import, i++);
+ wasm_func_t *get_var_f32_export = get_export_func(&exports_of_import, i++);
+ wasm_func_t* set_var_f32_export = get_export_func(&exports_of_import, i++);
+ wasm_func_t* get_var_f32_import = get_export_func(&exports_of_import, i++);
+ wasm_func_t* set_var_f32_import = get_export_func(&exports_of_import, i++);
+
+ // Interact.
+
+ // Check initial values.
+ printf("Check initial values...\n");
+ check_global(var_f32_export, f32, 7.0);
+ check_call(get_var_f32_export, f32, 7.0); //Call to module export
+ check_call(get_var_f32_import, f32, 7.0); //Call to module import
+
+
+ // Modify variables through API and check again.
+ printf("Modify the variable to 37.0...\n");
+ wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 37.0}};
+ wasm_global_set(var_f32_export, &val37); // var_f32_export->inst_comm_rt is module_import now
+
+ check_global(var_f32_export, f32, 37.0);
+ check_call(get_var_f32_export, f32, 37.0); //Call to module export Failed here, still 7
+ check_call(get_var_f32_import, f32, 37.0); //Call to module import
+
+ // Modify variables through calls and check again.
+ printf("Modify the variable to 77.0...\n");
+ wasm_val_vec_t args77;
+ wasm_val_vec_new(&args77, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 77.0}} });
+ wasm_trap_t *trap = wasm_func_call(set_var_f32_export, &args77,
+ NULL); // Call to module export
+ check_trap(trap);
+ check_call(get_var_f32_export, f32, 77.0); //Call to module export
+ check_global(var_f32_export, f32, 77.0); //Failed here, still 37
+ check_call(get_var_f32_import, f32, 77.0); //Call to module import Failed here, still 37
+
+
+ printf("Modify the variable to 78.0...\n");
+ wasm_val_vec_t args78;
+ wasm_val_vec_new(&args78, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 78.0}} });
+ trap = wasm_func_call(set_var_f32_import, &args78, NULL);
+ check_trap(trap);
+ check_global(var_f32_export, f32, 78.0);
+ check_call(get_var_f32_export, f32, 78.0); //Call to module export Failed here, still 77
+ check_call(get_var_f32_import, f32, 78.0); //Call to module import
+
+
+ // wasm_extern_vec_delete(&exports_of_export);
+ //wasm_instance_delete(instance_export);
+ wasm_extern_vec_delete(&exports_of_import);
+ //wasm_instance_delete(instance_import);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.c
new file mode 100644
index 000000000..3ebea87b0
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.c
@@ -0,0 +1,138 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+// A function to be called from Wasm code.
+own wasm_trap_t* hello_callback(
+ const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ printf("Calling back...\n");
+ printf("> Hello World!\n");
+ return NULL;
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("hello.aot", "rb");
+#else
+ FILE* file = fopen("hello.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Create external print functions.
+ printf("Creating callback...\n");
+ own wasm_functype_t* hello_type = wasm_functype_new_0_0();
+ own wasm_func_t* hello_func =
+ wasm_func_new(store, hello_type, hello_callback);
+
+ wasm_functype_delete(hello_type);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_t* externs[] = { wasm_func_as_extern(hello_func) };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ wasm_func_delete(hello_func);
+
+ // Extract export.
+ printf("Extracting export...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size == 0) {
+ printf("> Error accessing exports!\n");
+ return 1;
+ }
+
+ const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
+ if (run_func == NULL) {
+ printf("> Error accessing export!\n");
+ return 1;
+ }
+
+ wasm_module_delete(module);
+ wasm_instance_delete(instance);
+
+ // Call.
+ printf("Calling export...\n");
+ wasm_val_vec_t args = WASM_EMPTY_VEC;
+ wasm_val_vec_t results = WASM_EMPTY_VEC;
+ wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ return 1;
+ }
+
+ wasm_extern_vec_delete(&exports);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.wat
new file mode 100644
index 000000000..1c56c5582
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hello.wat
@@ -0,0 +1,4 @@
+(module
+ (func $hello (import "" "hello"))
+ (func (export "run") (call $hello))
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.c
new file mode 100644
index 000000000..219c862d6
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.c
@@ -0,0 +1,308 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+
+// A function to be called from Wasm code.
+own wasm_trap_t* callback(
+ const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ printf("Calling back...\n> ");
+ printf("> %p\n",
+ args->data[0].of.ref ? wasm_ref_get_host_info(args->data[0].of.ref) : NULL);
+ wasm_val_copy(&results->data[0], &args->data[0]);
+ return NULL;
+}
+
+
+wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
+ printf("> Error accessing function export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_func(exports->data[i]);
+}
+
+wasm_global_t* get_export_global(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_global(exports->data[i])) {
+ printf("> Error accessing global export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_global(exports->data[i]);
+}
+
+wasm_table_t* get_export_table(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_table(exports->data[i])) {
+ printf("> Error accessing table export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_table(exports->data[i]);
+}
+
+
+own wasm_ref_t* call_v_r(const wasm_func_t* func) {
+ printf("call_v_r... "); fflush(stdout);
+ wasm_val_t rs[] = { WASM_INIT_VAL };
+ wasm_val_vec_t args = WASM_EMPTY_VEC;
+ wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
+ wasm_trap_t *trap = wasm_func_call(func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+ printf("okay\n");
+ return rs[0].of.ref;
+}
+
+void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) {
+ printf("call_r_v... "); fflush(stdout);
+ wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
+ wasm_val_vec_t results = WASM_EMPTY_VEC;
+ wasm_trap_t *trap = wasm_func_call(func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+ printf("okay\n");
+}
+
+own wasm_ref_t* call_r_r(const wasm_func_t* func, wasm_ref_t* ref) {
+ printf("call_r_r... "); fflush(stdout);
+ wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
+ wasm_val_t rs[1] = { WASM_INIT_VAL };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
+ wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
+ wasm_trap_t *trap = wasm_func_call(func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+ printf("okay\n");
+ return rs[0].of.ref;
+}
+
+void call_ir_v(const wasm_func_t* func, int32_t i, wasm_ref_t* ref) {
+ printf("call_ir_v... "); fflush(stdout);
+ wasm_val_t vs[2] = { WASM_I32_VAL(i), WASM_REF_VAL(ref) };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
+ wasm_val_vec_t results = WASM_EMPTY_VEC;
+ wasm_trap_t *trap = wasm_func_call(func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+ printf("okay\n");
+}
+
+own wasm_ref_t* call_i_r(const wasm_func_t* func, int32_t i) {
+ printf("call_i_r... "); fflush(stdout);
+ wasm_val_t vs[1] = { WASM_I32_VAL(i) };
+ wasm_val_t rs[1] = { WASM_INIT_VAL };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
+ wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
+ wasm_trap_t *trap = wasm_func_call(func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+ printf("okay\n");
+ return rs[0].of.ref;
+}
+
+void check(own wasm_ref_t* actual, const wasm_ref_t* expected) {
+ if (actual != expected &&
+ !(actual && expected && wasm_ref_same(actual, expected))) {
+ printf("> Error reading reference, expected %p, got %p\n",
+ expected ? wasm_ref_get_host_info(expected) : NULL,
+ actual ? wasm_ref_get_host_info(actual) : NULL);
+ exit(1);
+ }
+ // if (actual) wasm_ref_delete(actual);
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("hostref.aot", "rb");
+#else
+ FILE* file = fopen("hostref.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Create external callback function.
+ printf("Creating callback...\n");
+ own wasm_functype_t* callback_type = wasm_functype_new_1_1(
+ wasm_valtype_new(WASM_ANYREF), wasm_valtype_new(WASM_ANYREF));
+ own wasm_func_t* callback_func =
+ wasm_func_new(store, callback_type, callback);
+
+ wasm_functype_delete(callback_type);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_t* externs[] = { wasm_func_as_extern(callback_func) };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ wasm_func_delete(callback_func);
+ wasm_module_delete(module);
+
+ // Extract export.
+ printf("Extracting exports...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ size_t i = 0;
+ wasm_global_t* global = get_export_global(&exports, i++);
+ wasm_table_t* table = get_export_table(&exports, i++);
+ wasm_func_t* global_set = get_export_func(&exports, i++);
+ wasm_func_t* global_get = get_export_func(&exports, i++);
+ wasm_func_t* table_set = get_export_func(&exports, i++);
+ wasm_func_t* table_get = get_export_func(&exports, i++);
+ wasm_func_t* func_call = get_export_func(&exports, i++);
+
+ wasm_instance_delete(instance);
+
+ // Create host references.
+ printf("Creating host references...\n");
+ own wasm_ref_t* host1 = wasm_foreign_as_ref(wasm_foreign_new(store));
+ own wasm_ref_t* host2 = wasm_foreign_as_ref(wasm_foreign_new(store));
+ wasm_ref_set_host_info(host1, (void*)1);
+ wasm_ref_set_host_info(host2, (void*)2);
+
+ // Some sanity checks.
+ check(NULL, NULL);
+ wasm_ref_t *host1_cp = wasm_ref_copy(host1);
+ wasm_ref_t *host2_cp = wasm_ref_copy(host2);
+ check(host1_cp, host1);
+ check(host2_cp, host2);
+ wasm_ref_delete(host1_cp);
+ wasm_ref_delete(host2_cp);
+
+ own wasm_val_t val;
+ val.kind = WASM_ANYREF;
+ val.of.ref = wasm_ref_copy(host1);
+ wasm_ref_t *ref_cp = wasm_ref_copy(val.of.ref);
+ check(ref_cp, host1);
+ check(val.of.ref, host1);
+ wasm_ref_delete(val.of.ref);
+ wasm_ref_delete(ref_cp);
+
+ // Interact.
+ printf("Accessing global...\n");
+ check(call_v_r(global_get), NULL);
+ call_r_v(global_set, host1);
+ check(call_v_r(global_get), host1);
+ call_r_v(global_set, host2);
+ check(call_v_r(global_get), host2);
+ call_r_v(global_set, NULL);
+ check(call_v_r(global_get), NULL);
+
+ wasm_global_get(global, &val);
+ assert(val.kind == WASM_ANYREF);
+ assert(val.of.ref == NULL);
+ val.of.ref = host2;
+ wasm_global_set(global, &val);
+ wasm_global_get(global, &val);
+ assert(val.kind == WASM_ANYREF);
+ assert(val.of.ref == host2);
+
+ printf("Accessing table...\n");
+ check(call_i_r(table_get, 0), NULL);
+ check(call_i_r(table_get, 1), NULL);
+ call_ir_v(table_set, 0, host1);
+ call_ir_v(table_set, 1, host2);
+ check(call_i_r(table_get, 0), host1);
+ check(call_i_r(table_get, 1), host2);
+ call_ir_v(table_set, 0, NULL);
+ check(call_i_r(table_get, 0), NULL);
+
+ check(wasm_table_get(table, 2), NULL);
+ wasm_table_set(table, 2, host1);
+ check(call_i_r(table_get, 2), host1);
+ check(wasm_table_get(table, 2), host1);
+
+ printf("Accessing function...\n");
+ check(call_r_r(func_call, NULL), NULL);
+ check(call_r_r(func_call, host1), host1);
+ check(call_r_r(func_call, host2), host2);
+
+ wasm_ref_delete(host1);
+ wasm_ref_delete(host2);
+
+ wasm_extern_vec_delete(&exports);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.wat
new file mode 100644
index 000000000..9ed43dbcb
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/hostref.wat
@@ -0,0 +1,24 @@
+(module
+ (import "" "f" (func $fun (param externref) (result externref)))
+
+ (global $glob (export "global") (mut externref) (ref.null extern))
+ (table $tab (export "table") 10 externref)
+
+ (func (export "global.set") (param $r externref)
+ (global.set $glob (local.get $r))
+ )
+ (func (export "global.get") (result externref)
+ (global.get $glob)
+ )
+
+ (func (export "table.set") (param $i i32) (param $r externref)
+ (table.set $tab (local.get $i) (local.get $r))
+ )
+ (func (export "table.get") (param $i i32) (result externref)
+ (table.get $tab (local.get $i))
+ )
+
+ (func (export "func.call") (param $r externref) (result externref)
+ (call $fun (local.get $r))
+ )
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.c
new file mode 100644
index 000000000..737dd8501
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.c
@@ -0,0 +1,235 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+
+wasm_memory_t* get_export_memory(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_memory(exports->data[i])) {
+ printf("> Error accessing memory export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_memory(exports->data[i]);
+}
+
+wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
+ printf("> Error accessing function export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_func(exports->data[i]);
+}
+
+
+void check(bool success) {
+ if (!success) {
+ printf("> Error, expected success\n");
+ exit(1);
+ }
+}
+
+void check_call(wasm_func_t* func, int i, wasm_val_t args[], int32_t expected) {
+ wasm_val_t r[] = {WASM_INIT_VAL};
+ wasm_val_vec_t args_ = {i, args, i, sizeof(wasm_val_t), NULL};
+ wasm_val_vec_t results = WASM_ARRAY_VEC(r);
+ wasm_trap_t *trap = wasm_func_call(func, &args_, &results);
+ if (trap) {
+ printf("> Error on result\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+
+ if (r[0].of.i32 != expected) {
+ printf("> Error on result\n");
+ exit(1);
+ }
+}
+
+void check_call0(wasm_func_t* func, int32_t expected) {
+ check_call(func, 0, NULL, expected);
+}
+
+void check_call1(wasm_func_t* func, int32_t arg, int32_t expected) {
+ wasm_val_t args[] = { WASM_I32_VAL(arg) };
+ check_call(func, 1, args, expected);
+}
+
+void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
+ wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
+ check_call(func, 2, args, expected);
+}
+
+void check_ok(wasm_func_t* func, int i, wasm_val_t args[]) {
+ wasm_val_vec_t args_ = {i, args, i, sizeof(wasm_val_t), NULL};
+ wasm_val_vec_t results = WASM_EMPTY_VEC;
+ wasm_trap_t *trap = wasm_func_call(func, &args_, &results);
+ if (trap) {
+ printf("> Error on result, expected empty\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+}
+
+void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
+ wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
+ check_ok(func, 2, args);
+}
+
+void check_trap(wasm_func_t* func, int i, wasm_val_t args[]) {
+ wasm_val_t r[] = {WASM_INIT_VAL};
+ wasm_val_vec_t args_ = {i, args, i, sizeof(wasm_val_t), NULL};
+ wasm_val_vec_t results = WASM_ARRAY_VEC(r);
+ own wasm_trap_t* trap = wasm_func_call(func, &args_, &results);
+ if (! trap) {
+ printf("> Error on result, expected trap\n");
+ exit(1);
+ }
+ wasm_trap_delete(trap);
+}
+
+void check_trap1(wasm_func_t* func, int32_t arg) {
+ wasm_val_t args[] = { WASM_I32_VAL(arg) };
+ check_trap(func, 1, args);
+}
+
+void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
+ wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
+ check_trap(func, 2, args);
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("memory.aot", "rb");
+#else
+ FILE* file = fopen("memory.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_vec_t imports = WASM_EMPTY_VEC;
+ own wasm_instance_t *instance = wasm_instance_new_with_args(
+ store, module, &imports, NULL, KILOBYTE(32), 0);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ // Extract export.
+ printf("Extracting exports...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ size_t i = 0;
+ wasm_memory_t* memory = get_export_memory(&exports, i++);
+ wasm_func_t* size_func = get_export_func(&exports, i++);
+ wasm_func_t* load_func = get_export_func(&exports, i++);
+ wasm_func_t* store_func = get_export_func(&exports, i++);
+
+ wasm_module_delete(module);
+
+ // Try cloning.
+ own wasm_memory_t* copy = wasm_memory_copy(memory);
+ assert(wasm_memory_same(memory, copy));
+ wasm_memory_delete(copy);
+
+ // Check initial memory.
+ printf("Checking memory...\n");
+ check(wasm_memory_size(memory) == 2);
+ check(wasm_memory_data_size(memory) == 0x20000);
+ check(wasm_memory_data(memory)[0] == 0);
+ check(wasm_memory_data(memory)[0x1000] == 1);
+ check(wasm_memory_data(memory)[0x1003] == 4);
+
+ check_call0(size_func, 2);
+ check_call1(load_func, 0, 0);
+ check_call1(load_func, 0x1000, 1);
+ check_call1(load_func, 0x1003, 4);
+ check_call1(load_func, 0x1ffff, 0);
+ check_trap1(load_func, 0x20000);
+
+ // Mutate memory.
+ printf("Mutating memory...\n");
+ wasm_memory_data(memory)[0x1003] = 5;
+ check_ok2(store_func, 0x1002, 6);
+ check_trap2(store_func, 0x20000, 0);
+
+ check(wasm_memory_data(memory)[0x1002] == 6);
+ check(wasm_memory_data(memory)[0x1003] == 5);
+ check_call1(load_func, 0x1002, 6);
+ check_call1(load_func, 0x1003, 5);
+
+ // Grow memory.
+ // DO NOT SUPPORT
+ printf("Bypass Growing memory...\n");
+ wasm_extern_vec_delete(&exports);
+ wasm_instance_delete(instance);
+
+ // Create stand-alone memory.
+ // DO NOT SUPPORT
+ // TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
+ printf("Bypass Creating stand-alone memory...\n");
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.wat
new file mode 100644
index 000000000..4cf43e2c7
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/memory.wat
@@ -0,0 +1,11 @@
+(module
+ (memory (export "memory") 2 3)
+
+ (func (export "size") (result i32) (memory.size))
+ (func (export "load") (param i32) (result i32) (i32.load8_s (local.get 0)))
+ (func (export "store") (param i32 i32)
+ (i32.store8 (local.get 0) (local.get 1))
+ )
+
+ (data (i32.const 0x1000) "\01\02\03\04")
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.c
new file mode 100644
index 000000000..a1c8fa9fc
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.c
@@ -0,0 +1,163 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+// A function to be called from Wasm code.
+own wasm_trap_t* callback(
+ const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ printf("Calling back...\n> ");
+ printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
+ args->data[0].of.i32, args->data[1].of.i64,
+ args->data[2].of.i64, args->data[3].of.i32);
+ printf("\n");
+
+ wasm_val_copy(&results->data[0], &args->data[3]);
+ wasm_val_copy(&results->data[1], &args->data[1]);
+ wasm_val_copy(&results->data[2], &args->data[2]);
+ wasm_val_copy(&results->data[3], &args->data[0]);
+ return NULL;
+}
+
+
+// A function closure.
+own wasm_trap_t* closure_callback(
+ void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ int i = *(int*)env;
+ printf("Calling back closure...\n");
+ printf("> %d\n", i);
+
+ results->data[0].kind = WASM_I32;
+ results->data[0].of.i32 = (int32_t)i;
+ return NULL;
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("multi.aot", "rb");
+#else
+ FILE* file = fopen("multi.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+ fseek(file, 0L, SEEK_END);
+ size_t file_size = ftell(file);
+ fseek(file, 0L, SEEK_SET);
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Create external print functions.
+ printf("Creating callback...\n");
+ wasm_valtype_t* types[4] = {
+ wasm_valtype_new_i32(), wasm_valtype_new_i64(),
+ wasm_valtype_new_i64(), wasm_valtype_new_i32()
+ };
+ own wasm_valtype_vec_t tuple1, tuple2;
+ wasm_valtype_vec_new(&tuple1, 4, types);
+ wasm_valtype_vec_copy(&tuple2, &tuple1);
+ own wasm_functype_t* callback_type = wasm_functype_new(&tuple1, &tuple2);
+ own wasm_func_t* callback_func =
+ wasm_func_new(store, callback_type, callback);
+
+ wasm_functype_delete(callback_type);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_t* externs[] = { wasm_func_as_extern(callback_func) };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ wasm_func_delete(callback_func);
+
+ // Extract export.
+ printf("Extracting export...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size == 0) {
+ printf("> Error accessing exports!\n");
+ return 1;
+ }
+ const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
+ if (run_func == NULL) {
+ printf("> Error accessing export!\n");
+ return 1;
+ }
+
+ wasm_module_delete(module);
+ wasm_instance_delete(instance);
+
+ // Call.
+ printf("Calling export...\n");
+ wasm_val_t vals[4] = {
+ WASM_I32_VAL(1), WASM_I64_VAL(2), WASM_I64_VAL(3), WASM_I32_VAL(4)
+ };
+ wasm_val_t res[4] = {
+ WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL
+ };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(vals);
+ wasm_val_vec_t results = WASM_ARRAY_VEC(res);
+ wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ return 1;
+ }
+
+ wasm_extern_vec_delete(&exports);
+
+ // Print result.
+ printf("Printing result...\n");
+ printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
+ res[0].of.i32, res[1].of.i64, res[2].of.i64, res[3].of.i32);
+
+ assert(res[0].of.i32 == 4);
+ assert(res[1].of.i64 == 3);
+ assert(res[2].of.i64 == 2);
+ assert(res[3].of.i32 == 1);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.wat
new file mode 100644
index 000000000..e7fb33112
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/multi.wat
@@ -0,0 +1,7 @@
+(module
+ (func $f (import "" "f") (param i32 i64 i64 i32) (result i32 i64 i64 i32))
+
+ (func $g (export "g") (param i32 i64 i64 i32) (result i32 i64 i64 i32)
+ (call $f (local.get 0) (local.get 2) (local.get 1) (local.get 3))
+ )
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.c
new file mode 100644
index 000000000..a595fd3e0
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.c
@@ -0,0 +1,207 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+void print_mutability(wasm_mutability_t mut) {
+ switch (mut) {
+ case WASM_VAR: printf("var"); break;
+ case WASM_CONST: printf("const"); break;
+ }
+}
+
+void print_limits(const wasm_limits_t* limits) {
+ if (!limits) {
+ printf("unknown limits");
+ return;
+ }
+ printf("%ud", limits->min);
+ if (limits->max < wasm_limits_max_default) printf(" %ud", limits->max);
+}
+
+void print_valtype(const wasm_valtype_t* type) {
+ switch (wasm_valtype_kind(type)) {
+ case WASM_I32: printf("i32"); break;
+ case WASM_I64: printf("i64"); break;
+ case WASM_F32: printf("f32"); break;
+ case WASM_F64: printf("f64"); break;
+ case WASM_ANYREF: printf("anyref"); break;
+ case WASM_FUNCREF: printf("funcref"); break;
+ }
+}
+
+void print_valtypes(const wasm_valtype_vec_t* types) {
+ bool first = true;
+ for (size_t i = 0; i < types->size; ++i) {
+ if (first) {
+ first = false;
+ } else {
+ printf(" ");
+ }
+ print_valtype(types->data[i]);
+ }
+}
+
+void print_externtype(const wasm_externtype_t* type) {
+ if (!type) {
+ printf("unknown extern type");
+ return;
+ }
+ switch (wasm_externtype_kind(type)) {
+ case WASM_EXTERN_FUNC: {
+ const wasm_functype_t* functype =
+ wasm_externtype_as_functype_const(type);
+ printf("func ");
+ print_valtypes(wasm_functype_params(functype));
+ printf(" -> ");
+ print_valtypes(wasm_functype_results(functype));
+ } break;
+ case WASM_EXTERN_GLOBAL: {
+ const wasm_globaltype_t* globaltype =
+ wasm_externtype_as_globaltype_const(type);
+ printf("global ");
+ print_mutability(wasm_globaltype_mutability(globaltype));
+ printf(" ");
+ print_valtype(wasm_globaltype_content(globaltype));
+ } break;
+ case WASM_EXTERN_TABLE: {
+ const wasm_tabletype_t* tabletype =
+ wasm_externtype_as_tabletype_const(type);
+ printf("table ");
+ print_limits(wasm_tabletype_limits(tabletype));
+ printf(" ");
+ print_valtype(wasm_tabletype_element(tabletype));
+ } break;
+ case WASM_EXTERN_MEMORY: {
+ const wasm_memorytype_t* memorytype =
+ wasm_externtype_as_memorytype_const(type);
+ printf("memory ");
+ print_limits(wasm_memorytype_limits(memorytype));
+ } break;
+ }
+}
+
+void print_name(const wasm_name_t* name) {
+ if (!name) {
+ printf("unknown name");
+ return;
+ }
+ printf("\"%.*s\"", (int)name->size, name->data);
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("reflect.aot", "rb");
+#else
+ FILE* file = fopen("reflect.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_vec_t imports = WASM_EMPTY_VEC;
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ // Extract export.
+ printf("Extracting export...\n");
+ own wasm_exporttype_vec_t export_types;
+ own wasm_extern_vec_t exports;
+ wasm_module_exports(module, &export_types);
+ wasm_instance_exports(instance, &exports);
+ assert(exports.size == export_types.size);
+
+ for (size_t i = 0; i < exports.size; ++i) {
+ assert(wasm_extern_kind(exports.data[i]) ==
+ wasm_externtype_kind(wasm_exporttype_type(export_types.data[i])));
+ printf("> export %zu ", i);
+ print_name(wasm_exporttype_name(export_types.data[i]));
+ printf("\n");
+ printf(">> initial: ");
+ print_externtype(wasm_exporttype_type(export_types.data[i]));
+ printf("\n");
+ printf(">> current: ");
+ own wasm_externtype_t* current = wasm_extern_type(exports.data[i]);
+ print_externtype(current);
+ wasm_externtype_delete(current);
+ printf("\n");
+ if (wasm_extern_kind(exports.data[i]) == WASM_EXTERN_FUNC) {
+ wasm_func_t* func = wasm_extern_as_func(exports.data[i]);
+ printf(">> in-arity: %zu", wasm_func_param_arity(func));
+ printf(", out-arity: %zu\n", wasm_func_result_arity(func));
+ }
+ }
+
+ wasm_module_delete(module);
+ wasm_instance_delete(instance);
+ wasm_extern_vec_delete(&exports);
+ wasm_exporttype_vec_delete(&export_types);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.wat
new file mode 100644
index 000000000..7a80e86dd
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/reflect.wat
@@ -0,0 +1,6 @@
+(module
+ (func (export "func") (param i32 f64 f32) (result i32) (unreachable))
+ (global (export "global") f64 (f64.const 0))
+ (table (export "table") 0 50 funcref)
+ (memory (export "memory") 1)
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.c
new file mode 100644
index 000000000..83436817f
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.c
@@ -0,0 +1,131 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+// A function to be called from Wasm code.
+own wasm_trap_t *
+hello_callback(const wasm_val_vec_t *args, wasm_val_vec_t *results)
+{
+ printf("Calling back...\n");
+ printf("> Hello World!\n");
+ return NULL;
+}
+
+int
+main(int argc, const char *argv[])
+{
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t *engine = wasm_engine_new();
+ wasm_store_t *store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+ FILE *file = fopen("serialize.wasm", "rb");
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+ fseek(file, 0L, SEEK_END);
+ size_t file_size = ftell(file);
+ fseek(file, 0L, SEEK_SET);
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t *module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Serialize module.
+ printf("Serializing module...\n");
+ own wasm_byte_vec_t serialized;
+ wasm_module_serialize(module, &serialized);
+
+ wasm_module_delete(module);
+
+ // Deserialize module.
+ printf("Deserializing module...\n");
+ own wasm_module_t *deserialized =
+ wasm_module_deserialize(store, &serialized);
+ if (!deserialized) {
+ printf("> Error deserializing module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&serialized);
+
+ // Create external print functions.
+ printf("Creating callback...\n");
+ own wasm_functype_t *hello_type = wasm_functype_new_0_0();
+ own wasm_func_t *hello_func =
+ wasm_func_new(store, hello_type, hello_callback);
+
+ wasm_functype_delete(hello_type);
+
+ // Instantiate.
+ printf("Instantiating deserialized module...\n");
+ wasm_extern_t *externs[] = { wasm_func_as_extern(hello_func) };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t *instance =
+ wasm_instance_new(store, deserialized, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ wasm_func_delete(hello_func);
+
+ // Extract export.
+ printf("Extracting export...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size == 0) {
+ printf("> Error accessing exports!\n");
+ return 1;
+ }
+ const wasm_func_t *run_func = wasm_extern_as_func(exports.data[0]);
+ if (run_func == NULL) {
+ printf("> Error accessing export!\n");
+ return 1;
+ }
+
+ wasm_module_delete(deserialized);
+ wasm_instance_delete(instance);
+
+ // Call.
+ printf("Calling export...\n");
+ wasm_val_vec_t empty = WASM_EMPTY_VEC;
+ wasm_trap_t *trap = wasm_func_call(run_func, &empty, &empty);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ return 1;
+ }
+
+ wasm_extern_vec_delete(&exports);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.wat
new file mode 100644
index 000000000..1c56c5582
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/serialize.wat
@@ -0,0 +1,4 @@
+(module
+ (func $hello (import "" "hello"))
+ (func (export "run") (call $hello))
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.c
new file mode 100644
index 000000000..c4a7da8bc
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.c
@@ -0,0 +1,218 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+// A function to be called from Wasm code.
+own wasm_trap_t* neg_callback(
+ const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ printf("Calling back...\n");
+ results->data[0].kind = WASM_I32;
+ results->data[0].of.i32 = -args->data[0].of.i32;
+ return NULL;
+}
+
+
+wasm_table_t* get_export_table(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_table(exports->data[i])) {
+ printf("> Error accessing table export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_table(exports->data[i]);
+}
+
+wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
+ if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
+ printf("> Error accessing function export %zu!\n", i);
+ exit(1);
+ }
+ return wasm_extern_as_func(exports->data[i]);
+}
+
+
+void check(bool success) {
+ if (!success) {
+ printf("> Error, expected success\n");
+ exit(1);
+ }
+}
+
+void check_table(wasm_table_t* table, int32_t i, bool expect_set) {
+ own wasm_ref_t* ref = wasm_table_get(table, i);
+ check((ref != NULL) == expect_set);
+ if (ref) wasm_ref_delete(ref);
+}
+
+void check_call(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
+ wasm_val_t vs[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
+ wasm_val_t r[1] = { WASM_INIT_VAL };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
+ wasm_val_vec_t results = WASM_ARRAY_VEC(r);
+ wasm_trap_t *trap = wasm_func_call(func, &args, &results);
+ if (trap) {
+ printf("> Error on calling\n");
+ wasm_trap_delete(trap);
+ exit(1);
+ }
+
+ if (r[0].of.i32 != expected) {
+ printf("> Error on result\n");
+ exit(1);
+ }
+}
+
+void check_trap(wasm_func_t* func, int32_t arg1, int32_t arg2) {
+ wasm_val_t vs[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
+ wasm_val_t r[1] = { WASM_INIT_VAL };
+ wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
+ wasm_val_vec_t results = WASM_ARRAY_VEC(r);
+ own wasm_trap_t* trap = wasm_func_call(func, &args, &results);
+ if (! trap) {
+ printf("> Error on result, expected trap\n");
+ exit(1);
+ }
+ wasm_trap_delete(trap);
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("table.aot", "rb");
+#else
+ FILE* file = fopen("table.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_vec_t imports = WASM_EMPTY_VEC;
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ // Extract export.
+ printf("Extracting exports...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ size_t i = 0;
+ wasm_table_t* table = get_export_table(&exports, i++);
+ wasm_func_t* call_indirect = get_export_func(&exports, i++);
+ wasm_func_t* f = get_export_func(&exports, i++);
+ wasm_func_t* g = get_export_func(&exports, i++);
+
+ wasm_module_delete(module);
+
+ // Create external function.
+ printf("Creating callback...\n");
+ own wasm_functype_t* neg_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
+ own wasm_func_t* h = wasm_func_new(store, neg_type, neg_callback);
+
+ wasm_functype_delete(neg_type);
+
+ // Try cloning.
+ own wasm_table_t* copy = wasm_table_copy(table);
+ assert(wasm_table_same(table, copy));
+ wasm_table_delete(copy);
+
+ // Check initial table.
+ printf("Checking table...\n");
+ check(wasm_table_size(table) == 2);
+ check_table(table, 0, false);
+ check_table(table, 1, true);
+ check_trap(call_indirect, 0, 0);
+ check_call(call_indirect, 7, 1, 7);
+ check_trap(call_indirect, 0, 2);
+
+ // Mutate table.
+ printf("Mutating table...\n");
+ check(wasm_table_set(table, 0, wasm_func_as_ref(g)));
+ check(wasm_table_set(table, 1, NULL));
+ wasm_ref_t *ref_f = wasm_func_as_ref(f);
+ check(! wasm_table_set(table, 2, ref_f));
+ wasm_ref_delete(ref_f);
+ check_table(table, 0, true);
+ check_table(table, 1, false);
+ check_call(call_indirect, 7, 0, 666);
+ check_trap(call_indirect, 0, 1);
+ check_trap(call_indirect, 0, 2);
+
+ // Grow table.
+ // DO NOT SUPPORT
+ printf("Bypass Growing table...\n");
+
+ wasm_func_delete(h);
+ wasm_extern_vec_delete(&exports);
+ wasm_instance_delete(instance);
+
+ // Create stand-alone table.
+ // DO NOT SUPPORT
+ // TODO(wasm+): Once Wasm allows multiple tables, turn this into import.
+ printf("Bypass Creating stand-alone table...\n");
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.wat
new file mode 100644
index 000000000..d3e3a945a
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/table.wat
@@ -0,0 +1,12 @@
+(module
+ (table (export "table") 2 10 funcref)
+
+ (func (export "call_indirect") (param i32 i32) (result i32)
+ (call_indirect (param i32) (result i32) (local.get 0) (local.get 1))
+ )
+
+ (func $f (export "f") (param i32) (result i32) (local.get 0))
+ (func (export "g") (param i32) (result i32) (i32.const 666))
+
+ (elem (i32.const 1) $f)
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.c
new file mode 100644
index 000000000..cbe4aaf44
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.c
@@ -0,0 +1,187 @@
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <unistd.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+const int N_THREADS = 10;
+const int N_REPS = 3;
+
+// A function to be called from Wasm code.
+own wasm_trap_t *
+callback(const wasm_val_vec_t *args, wasm_val_vec_t *results)
+{
+ assert(args->data[0].kind == WASM_I32);
+ printf("> Thread %d running\n", args->data[0].of.i32);
+ return NULL;
+}
+
+typedef struct {
+ wasm_engine_t *engine;
+ wasm_shared_module_t *module;
+ int id;
+} thread_args;
+
+void *
+run(void *args_abs)
+{
+ thread_args *args = (thread_args *)args_abs;
+
+ // Rereate store and module.
+ own wasm_store_t *store = wasm_store_new(args->engine);
+ own wasm_module_t *module = wasm_module_obtain(store, args->module);
+
+ // Run the example N times.
+ for (int i = 0; i < N_REPS; ++i) {
+ usleep(100000);
+
+ // Create imports.
+ own wasm_functype_t *func_type =
+ wasm_functype_new_1_0(wasm_valtype_new_i32());
+ own wasm_func_t *func = wasm_func_new(store, func_type, callback);
+ wasm_functype_delete(func_type);
+
+ wasm_val_t val = WASM_I32_VAL((int32_t)args->id);
+ own wasm_globaltype_t *global_type =
+ wasm_globaltype_new(wasm_valtype_new_i32(), WASM_CONST);
+ own wasm_global_t *global = wasm_global_new(store, global_type, &val);
+ wasm_globaltype_delete(global_type);
+
+ // Instantiate.
+ wasm_extern_t *externs[] = {
+ wasm_func_as_extern(func),
+ wasm_global_as_extern(global),
+ };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t *instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return NULL;
+ }
+
+ wasm_func_delete(func);
+ wasm_global_delete(global);
+
+ // Extract export.
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size == 0) {
+ printf("> Error accessing exports!\n");
+ return NULL;
+ }
+ const wasm_func_t *run_func = wasm_extern_as_func(exports.data[0]);
+ if (run_func == NULL) {
+ printf("> Error accessing export!\n");
+ return NULL;
+ }
+
+ wasm_instance_delete(instance);
+
+ // Call.
+ wasm_val_vec_t empty = WASM_EMPTY_VEC;
+ wasm_trap_t *trap = wasm_func_call(run_func, &empty, &empty);
+ if (trap) {
+ printf("> Error calling function!\n");
+ wasm_trap_delete(trap);
+ return NULL;
+ }
+
+ wasm_extern_vec_delete(&exports);
+ }
+
+ wasm_module_delete(module);
+ wasm_store_delete(store);
+
+ free(args_abs);
+
+ return NULL;
+}
+
+int
+main(int argc, const char *argv[])
+{
+ // Initialize.
+ wasm_engine_t *engine = wasm_engine_new();
+
+ // Load binary.
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE *file = fopen("threads.aot", "rb");
+#else
+ FILE *file = fopen("threads.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+ fclose(file);
+
+ // Compile and share.
+ own wasm_store_t *store = wasm_store_new(engine);
+ own wasm_module_t *module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ own wasm_shared_module_t *shared = wasm_module_share(module);
+
+ wasm_module_delete(module);
+ wasm_store_delete(store);
+
+ // Spawn threads.
+ pthread_t threads[N_THREADS];
+ for (int i = 0; i < N_THREADS; i++) {
+ thread_args *args = malloc(sizeof(thread_args));
+ args->id = i;
+ args->engine = engine;
+ args->module = shared;
+ printf("Initializing thread %d...\n", i);
+ pthread_create(&threads[i], NULL, &run, args);
+ }
+
+ for (int i = 0; i < N_THREADS; i++) {
+ printf("Waiting for thread: %d\n", i);
+ pthread_join(threads[i], NULL);
+ }
+
+ wasm_shared_module_delete(shared);
+ wasm_engine_delete(engine);
+
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.wat
new file mode 100644
index 000000000..29a3bbcc1
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/threads.wat
@@ -0,0 +1,5 @@
+(module
+ (func $message (import "" "hello") (param i32))
+ (global $id (import "" "id") i32)
+ (func (export "run") (call $message (global.get $id)))
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.c
new file mode 100644
index 000000000..2637b7346
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.c
@@ -0,0 +1,184 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "wasm_c_api.h"
+
+#define own
+
+// A function to be called from Wasm code.
+own wasm_trap_t* fail_callback(
+ void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
+) {
+ printf("Calling back...\n");
+ own wasm_name_t message;
+ wasm_name_new_from_string_nt(&message, "callback abort");
+ own wasm_trap_t* trap = wasm_trap_new((wasm_store_t*)env, &message);
+ wasm_name_delete(&message);
+ return trap;
+}
+
+
+void print_frame(wasm_frame_t* frame) {
+ printf("> %p @ 0x%zx = %"PRIu32".0x%zx\n",
+ wasm_frame_instance(frame),
+ wasm_frame_module_offset(frame),
+ wasm_frame_func_index(frame),
+ wasm_frame_func_offset(frame)
+ );
+}
+
+
+int main(int argc, const char* argv[]) {
+ // Initialize.
+ printf("Initializing...\n");
+ wasm_engine_t* engine = wasm_engine_new();
+ wasm_store_t* store = wasm_store_new(engine);
+
+ // Load binary.
+ printf("Loading binary...\n");
+#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
+ FILE* file = fopen("trap.aot", "rb");
+#else
+ FILE* file = fopen("trap.wasm", "rb");
+#endif
+ if (!file) {
+ printf("> Error loading module!\n");
+ return 1;
+ }
+
+ int ret = fseek(file, 0L, SEEK_END);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ long file_size = ftell(file);
+ if (file_size == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ ret = fseek(file, 0L, SEEK_SET);
+ if (ret == -1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+
+ wasm_byte_vec_t binary;
+ wasm_byte_vec_new_uninitialized(&binary, file_size);
+ if (fread(binary.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ fclose(file);
+ return 1;
+ }
+ fclose(file);
+
+ // Compile.
+ printf("Compiling module...\n");
+ own wasm_module_t* module = wasm_module_new(store, &binary);
+ if (!module) {
+ printf("> Error compiling module!\n");
+ return 1;
+ }
+
+ wasm_byte_vec_delete(&binary);
+
+ // Create external print functions.
+ printf("Creating callback...\n");
+ own wasm_functype_t* fail_type =
+ wasm_functype_new_0_1(wasm_valtype_new_i32());
+ own wasm_func_t* fail_func =
+ wasm_func_new_with_env(store, fail_type, fail_callback, store, NULL);
+
+ wasm_functype_delete(fail_type);
+
+ // Instantiate.
+ printf("Instantiating module...\n");
+ wasm_extern_t* externs[] = { wasm_func_as_extern(fail_func) };
+ wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
+ own wasm_instance_t* instance =
+ wasm_instance_new(store, module, &imports, NULL);
+ if (!instance) {
+ printf("> Error instantiating module!\n");
+ return 1;
+ }
+
+ wasm_func_delete(fail_func);
+
+ // Extract export.
+ printf("Extracting exports...\n");
+ own wasm_extern_vec_t exports;
+ wasm_instance_exports(instance, &exports);
+ if (exports.size < 2) {
+ printf("> Error accessing exports!\n");
+ return 1;
+ }
+
+ wasm_module_delete(module);
+ wasm_instance_delete(instance);
+
+ // Call.
+ for (int i = 0; i < 2; ++i) {
+ const wasm_func_t* func = wasm_extern_as_func(exports.data[i]);
+ if (func == NULL) {
+ printf("> Error accessing export!\n");
+ return 1;
+ }
+
+ printf("Calling export %d...\n", i);
+ wasm_val_vec_t args = WASM_EMPTY_VEC;
+ wasm_val_vec_t results = WASM_EMPTY_VEC;
+ own wasm_trap_t* trap = wasm_func_call(func, &args, &results);
+ if (!trap) {
+ printf("> Error calling function, expected trap!\n");
+ return 1;
+ }
+
+ printf("Printing message...\n");
+ own wasm_name_t message;
+ wasm_trap_message(trap, &message);
+ printf("> %s\n", message.data);
+ assert(message.num_elems > 0);
+ assert(strncmp(message.data, "Exception: ", strlen("Exception: ")) == 0);
+
+ printf("Printing origin...\n");
+ own wasm_frame_t* frame = wasm_trap_origin(trap);
+ if (frame) {
+ print_frame(frame);
+ wasm_frame_delete(frame);
+ } else {
+ printf("> Empty origin.\n");
+ }
+
+ printf("Printing trace...\n");
+ own wasm_frame_vec_t trace;
+ wasm_trap_trace(trap, &trace);
+ if (trace.size > 0) {
+ for (size_t i = 0; i < trace.size; ++i) {
+ print_frame(trace.data[i]);
+ }
+ } else {
+ printf("> Empty trace.\n");
+ }
+
+ wasm_frame_vec_delete(&trace);
+ wasm_trap_delete(trap);
+ wasm_name_delete(&message);
+ }
+
+ wasm_extern_vec_delete(&exports);
+
+ // Shut down.
+ printf("Shutting down...\n");
+ wasm_store_delete(store);
+ wasm_engine_delete(engine);
+
+ // All done.
+ printf("Done.\n");
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.wat b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.wat
new file mode 100644
index 000000000..dfd20fb12
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/trap.wat
@@ -0,0 +1,5 @@
+(module
+ (func $callback (import "" "callback") (result i32))
+ (func (export "callback") (result i32) (call $callback))
+ (func (export "unreachable") (result i32) (unreachable) (i32.const 1))
+)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/utils/multi_module_utils.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/utils/multi_module_utils.c
new file mode 100644
index 000000000..67010914b
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/utils/multi_module_utils.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_read_file.h"
+#include "wasm_export.h"
+
+static char *
+build_module_path(const char *module_name)
+{
+ const char *module_search_path = ".";
+ const char *format = "%s/%s.wasm";
+ int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
+ + strlen(".wasm") + 1;
+ char *wasm_file_name = BH_MALLOC(sz);
+ if (!wasm_file_name) {
+ return NULL;
+ }
+
+ snprintf(wasm_file_name, sz, format, module_search_path, module_name);
+ return wasm_file_name;
+}
+
+bool
+reader(const char *module_name, uint8 **p_buffer, uint32 *p_size)
+{
+ char *wasm_file_path = build_module_path(module_name);
+ if (!wasm_file_path) {
+ return false;
+ }
+
+ *p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_path, p_size);
+ BH_FREE(wasm_file_path);
+ return *p_buffer != NULL;
+}
+
+void
+destroyer(uint8 *buffer, uint32 size)
+{
+ if (!buffer) {
+ return;
+ }
+
+ BH_FREE(buffer);
+ buffer = NULL;
+}