summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib
diff options
context:
space:
mode:
Diffstat (limited to 'fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib')
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/CMakeLists.txt91
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/README.md76
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_add.c32
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello.c34
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello2.c59
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_sqrt.c32
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/CMakeLists.txt35
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/main.c70
8 files changed, 429 insertions, 0 deletions
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/CMakeLists.txt
new file mode 100644
index 000000000..d8201bae4
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/CMakeLists.txt
@@ -0,0 +1,91 @@
+# 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)
+
+project(native_lib)
+
+################ 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 are:
+# "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 ()
+
+set (WAMR_BUILD_INTERP 1)
+set (WAMR_BUILD_AOT 1)
+set (WAMR_BUILD_JIT 0)
+set (WAMR_BUILD_LIBC_BUILTIN 1)
+set (WAMR_BUILD_FAST_INTERP 1)
+
+# 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 libiwasm
+set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
+
+# Note: we build libiwasm as a shared library here so that it can be
+# shared between iwasm and native libraries.
+add_library(libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})
+set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
+
+################ wamr runtime ###################
+include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
+
+set (RUNTIME_SOURCE_ALL
+ ${WAMR_ROOT_DIR}/product-mini/platforms/posix/main.c
+ ${UNCOMMON_SHARED_SOURCE}
+)
+
+add_executable (iwasm ${RUNTIME_SOURCE_ALL})
+
+check_pie_supported()
+set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
+
+target_link_libraries(iwasm libiwasm -lpthread -lm -ldl)
+
+################ native libraries ###############
+add_library (test_add SHARED test_add.c)
+add_library (test_sqrt SHARED test_sqrt.c)
+add_library (test_hello SHARED test_hello.c)
+# Note: Unlike simpler examples above, test_hello2 directly uses
+# the API provided by the libiwasm library.
+add_library (test_hello2 SHARED test_hello2.c)
+target_link_libraries(test_hello2 libiwasm)
+
+################ wasm application ###############
+add_subdirectory(wasm-app)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/README.md b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/README.md
new file mode 100644
index 000000000..2bf65814d
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/README.md
@@ -0,0 +1,76 @@
+# "native-lib" sample introduction
+
+This sample demonstrates how to write required interfaces in native library, build it into a shared library and register the shared library to iwasm.
+
+The native library should provide `get_native_lib` API for iwasm to return the native library info, including the module name, the native symbol list and the native symbol count, so that iwasm can use them to regiter the native library, for example:
+
+```C
+static int
+foo_wrapper(wasm_exec_env_t exec_env, int x, int y)
+{
+ return x + y;
+}
+
+#define REG_NATIVE_FUNC(func_name, signature) \
+ { #func_name, func_name##_wrapper, signature, NULL }
+
+static NativeSymbol native_symbols[] = {
+ REG_NATIVE_FUNC(foo, "(ii)i")
+};
+
+uint32_t
+get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
+{
+ *p_module_name = "env";
+ *p_native_symbols = native_symbols;
+ return sizeof(native_symbols) / sizeof(NativeSymbol);
+}
+```
+
+## Preparation
+
+Please install WASI SDK, download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`.
+
+## Build the sample
+
+```bash
+mkdir build
+cd build
+cmake ..
+make
+```
+
+`iwasm`, one wasm module `test.wasm` and two shared libraries `libtest_add.so`, `libtest_sqrt.so`
+will be generated.
+
+## Run workload
+
+### Linux
+
+```bash
+cd build
+./iwasm --native-lib=./libtest_add.so --native-lib=./libtest_sqrt.so --native-lib=./libtest_hello.so --native-lib=./libtest_hello2.so wasm-app/test.wasm
+```
+
+### macOS
+
+```bash
+cd build
+./iwasm --native-lib=libtest_add.dylib --native-lib=libtest_sqrt.dylib --native-lib=libtest_hello.dylib --native-lib=libtest_hello2.dylib wasm-app/test.wasm
+```
+
+The output is:
+
+```bash
+Hello World!
+10 + 20 = 30
+sqrt(10, 20) = 500
+test_hello("main", 0x0, 0) = 41
+malloc(42) = 0x24e8
+test_hello("main", 0x24e8, 42) = 41
+Message from test_hello: Hello, main. This is test_hello_wrapper!
+test_hello2("main", 0x0, 0) = 85
+malloc(86) = 0x24e8
+test_hello2("main", 0x24e8, 86) = 85
+Message from test_hello2: Hello, main. This is test_hello2_wrapper! Your wasm_module_inst_t is 0x7fd443704990.
+```
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_add.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_add.c
new file mode 100644
index 000000000..ac44c6192
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_add.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "wasm_export.h"
+
+static int
+test_add_wrapper(wasm_exec_env_t exec_env, int x, int y)
+{
+ return x + y;
+}
+
+/* clang-format off */
+#define REG_NATIVE_FUNC(func_name, signature) \
+ { #func_name, func_name##_wrapper, signature, NULL }
+
+static NativeSymbol native_symbols[] = {
+ REG_NATIVE_FUNC(test_add, "(ii)i")
+};
+/* clang-format on */
+
+uint32_t
+get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
+{
+ *p_module_name = "env";
+ *p_native_symbols = native_symbols;
+ return sizeof(native_symbols) / sizeof(NativeSymbol);
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello.c
new file mode 100644
index 000000000..c322ec24e
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "wasm_export.h"
+
+static int
+test_hello_wrapper(wasm_exec_env_t exec_env, const char *name, char *result,
+ size_t resultlen)
+{
+ return snprintf(result, resultlen, "Hello, %s. This is %s!\n", name,
+ __func__);
+}
+
+/* clang-format off */
+#define REG_NATIVE_FUNC(func_name, signature) \
+ { #func_name, func_name##_wrapper, signature, NULL }
+
+static NativeSymbol native_symbols[] = {
+ REG_NATIVE_FUNC(test_hello, "($*~)i")
+};
+/* clang-format on */
+
+uint32_t
+get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
+{
+ *p_module_name = "env";
+ *p_native_symbols = native_symbols;
+ return sizeof(native_symbols) / sizeof(NativeSymbol);
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello2.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello2.c
new file mode 100644
index 000000000..2c8f69ed6
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_hello2.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+/*
+ * This example basically does the same thing as test_hello.c,
+ * using wasm_export.h API.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "wasm_export.h"
+
+static int
+test_hello2_wrapper(wasm_exec_env_t exec_env, uint32_t nameaddr,
+ uint32_t resultaddr, uint32_t resultlen)
+{
+ /*
+ * Perform wasm_runtime_malloc to check if the runtime has been
+ * initialized as expected.
+ * This would fail with "memory hasn't been initialize" error
+ * unless we are not sharing a runtime with the loader app. (iwasm)
+ */
+ void *p = wasm_runtime_malloc(1);
+ if (p == NULL) {
+ return -1;
+ }
+ wasm_runtime_free(p);
+
+ wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
+ if (!wasm_runtime_validate_app_str_addr(inst, nameaddr)
+ || !wasm_runtime_validate_app_addr(inst, resultaddr, resultlen)) {
+ return -1;
+ }
+ const char *name = wasm_runtime_addr_app_to_native(inst, nameaddr);
+ char *result = wasm_runtime_addr_app_to_native(inst, resultaddr);
+ return snprintf(result, resultlen,
+ "Hello, %s. This is %s! Your wasm_module_inst_t is %p.\n",
+ name, __func__, inst);
+}
+
+/* clang-format off */
+#define REG_NATIVE_FUNC(func_name, signature) \
+ { #func_name, func_name##_wrapper, signature, NULL }
+
+static NativeSymbol native_symbols[] = {
+ REG_NATIVE_FUNC(test_hello2, "(iii)i")
+};
+/* clang-format on */
+
+uint32_t
+get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
+{
+ *p_module_name = "env";
+ *p_native_symbols = native_symbols;
+ return sizeof(native_symbols) / sizeof(NativeSymbol);
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_sqrt.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_sqrt.c
new file mode 100644
index 000000000..ecf7c989c
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/test_sqrt.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "wasm_export.h"
+
+static int
+test_sqrt_wrapper(wasm_exec_env_t exec_env, int x, int y)
+{
+ return x * x + y * y;
+}
+
+/* clang-format off */
+#define REG_NATIVE_FUNC(func_name, signature) \
+ { #func_name, func_name##_wrapper, signature, NULL }
+
+static NativeSymbol native_symbols[] = {
+ REG_NATIVE_FUNC(test_sqrt, "(ii)i")
+};
+/* clang-format on */
+
+uint32_t
+get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
+{
+ *p_module_name = "env";
+ *p_native_symbols = native_symbols;
+ return sizeof(native_symbols) / sizeof(NativeSymbol);
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/CMakeLists.txt
new file mode 100644
index 000000000..ffcd9005a
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/CMakeLists.txt
@@ -0,0 +1,35 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required(VERSION 3.0)
+project(wasm-app)
+
+set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
+
+if (APPLE)
+ set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
+ set (CMAKE_C_LINK_FLAGS "")
+ set (CMAKE_CXX_LINK_FLAGS "")
+endif ()
+
+set (CMAKE_SYSTEM_PROCESSOR wasm32)
+set (CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
+
+if (NOT DEFINED WASI_SDK_DIR)
+ set (WASI_SDK_DIR "/opt/wasi-sdk")
+endif ()
+
+set (CMAKE_C_FLAGS "-nostdlib")
+set (CMAKE_C_COMPILER_TARGET "wasm32")
+set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
+
+set (CMAKE_EXE_LINKER_FLAGS
+ "-Wl,--max-memory=131072 -z stack-size=8192 \
+ -Wl,--no-entry,--strip-all \
+ -Wl,--export=__main_argc_argv \
+ -Wl,--export=__heap_base,--export=__data_end \
+ -Wl,--allow-undefined"
+)
+
+add_executable(test.wasm main.c)
+target_link_libraries(test.wasm)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/main.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/main.c
new file mode 100644
index 000000000..dba652daf
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/wasm-app/main.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+test_add(int x, int y);
+
+int
+test_sqrt(int x, int y);
+
+int
+test_hello(const char *name, char *buf, size_t buflen);
+
+int
+test_hello2(const char *name, char *buf, size_t buflen);
+
+int
+main(int argc, char **argv)
+{
+ const char *name = __func__;
+ char *buf;
+ size_t buflen;
+ int x = 10, y = 20, res;
+
+ printf("Hello World!\n");
+
+ res = test_add(x, y);
+ printf("%d + %d = %d\n", x, y, res);
+
+ res = test_sqrt(x, y);
+ printf("sqrt(%d, %d) = %d\n", x, y, res);
+
+ res = test_hello(name, NULL, 0);
+ printf("test_hello(\"%s\", %p, %zu) = %d\n", name, NULL, (size_t)0, res);
+ if (res == -1) {
+ return -1;
+ }
+ buflen = res + 1;
+ buf = malloc(buflen);
+ printf("malloc(%zu) = %p\n", buflen, buf);
+ res = test_hello(__func__, buf, buflen);
+ if (res == -1) {
+ return -1;
+ }
+ printf("test_hello(\"%s\", %p, %zu) = %d\n", name, buf, buflen, res);
+ printf("Message from test_hello: %s", buf);
+ free(buf);
+
+ res = test_hello2(name, NULL, 0);
+ printf("test_hello2(\"%s\", %p, %zu) = %d\n", name, NULL, (size_t)0, res);
+ if (res == -1) {
+ return -1;
+ }
+ buflen = res + 1;
+ buf = malloc(buflen);
+ printf("malloc(%zu) = %p\n", buflen, buf);
+ res = test_hello2(__func__, buf, buflen);
+ if (res == -1) {
+ return -1;
+ }
+ printf("test_hello2(\"%s\", %p, %zu) = %d\n", name, buf, buflen, res);
+ printf("Message from test_hello2: %s", buf);
+ free(buf);
+
+ return 0;
+}