diff options
Diffstat (limited to 'fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module')
8 files changed, 621 insertions, 0 deletions
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/CMakeLists.txt new file mode 100644 index 00000000..2769b977 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/CMakeLists.txt @@ -0,0 +1,159 @@ +# 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(multi_module) + +################ 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 () + +set(WAMR_BUILD_INTERP 1) +set(WAMR_BUILD_AOT 0) +set(WAMR_BUILD_JIT 0) +set(WAMR_BUILD_LIBC_BUILTIN 1) +set(WAMR_BUILD_LIBC_WASI 1) +set(WAMR_BUILD_MULTI_MODULE 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") +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 () + +# build out vmlib +set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) +include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) + +add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE}) +################################################ + +################ application related ################ + +################ WASM MODULES +include(ExternalProject) + +message(CHECK_START "Detecting WASI-SDK") +if(NOT (DEFINED WASI_SDK_DIR OR DEFINED CACHE{WASI_SDK_DIR})) + find_path(WASI_SDK_PARENT + wasi-sdk + PATHS /opt + NO_DEFAULT_PATH + NO_CMAKE_FIND_ROOT_PATH + ) + if(WASI_SDK_PARENT) + set(WASI_SDK_DIR ${WASI_SDK_PARENT}/wasi-sdk) + endif() +endif() +if(WASI_SDK_DIR) + message(CHECK_PASS "found") +else() + message(CHECK_FAIL "not found") +endif() + +message(CHECK_START "Detecting WASI_TOOLCHAIN_FILE at ${WASI_SDK_DIR}") +find_file(WASI_TOOLCHAIN_FILE + wasi-sdk.cmake + PATHS "${WASI_SDK_DIR}/share/cmake" + NO_DEFAULT_PATH + NO_CMAKE_FIND_ROOT_PATH +) +if(WASI_TOOLCHAIN_FILE) + message(CHECK_PASS "found") +else() + message(CHECK_FAIL "not found") +endif() + +message(CHECK_START "Detecting WASI_SYS_ROOT at ${WASI_SDK_DIR}") +find_path(WASI_SYS_ROOT + wasi-sysroot + PATHS "${WASI_SDK_DIR}/share" + NO_DEFAULT_PATH + NO_CMAKE_FIND_ROOT_PATH +) +if(WASI_SYS_ROOT) + message(CHECK_PASS "found") + set(WASI_SYS_ROOT ${WASI_SYS_ROOT}/wasi-sysroot) +else() + message(CHECK_FAIL "not found") +endif() + +if((NOT EXISTS ${WASI_SDK_DIR}) OR (NOT EXISTS ${WASI_TOOLCHAIN_FILE}) OR (NOT EXISTS ${WASI_SYS_ROOT})) + message(FATAL_ERROR "Please set the absolute path of wasi-sdk with \'cmake -DWASI_SDK_DIR=XXX\'") +else() + message(STATUS "WASI_SDK_DIR is ${WASI_SDK_DIR}") + message(STATUS "WASI_TOOLCHAIN_FILE is ${WASI_TOOLCHAIN_FILE}") + message(STATUS "WASI_SYS_ROOT is ${WASI_SYS_ROOT}") +endif() + +# .c -> .wasm +ExternalProject_Add(WASM_MODULE + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps + UPDATE_COMMAND "" + PATCH_COMMAND "" + CONFIGURE_COMMAND ${CMAKE_COMMAND} + -DWASI_SDK_PREFIX=${WASI_SDK_DIR} + -DCMAKE_TOOLCHAIN_FILE=${WASI_TOOLCHAIN_FILE} + -DCMAKE_SYSROOT=${WASI_SYS_ROOT} + -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps + BUILD_COMMAND ${CMAKE_COMMAND} --build . + INSTALL_COMMAND ${CMAKE_COMMAND} -E copy + ./mA.wasm ${CMAKE_BINARY_DIR} + ./mB.wasm ${CMAKE_BINARY_DIR} + ./mC.wasm ${CMAKE_BINARY_DIR} + ./mD.wasm ${CMAKE_BINARY_DIR} + ./mE.wasm ${CMAKE_BINARY_DIR} +) + +################ NATIVE +include_directories(${CMAKE_CURRENT_LIST_DIR}/src) +include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) + +add_executable(multi_module src/main.c ${UNCOMMON_SHARED_SOURCE}) + +add_dependencies(multi_module vmlib WASM_MODULE) + +check_pie_supported() +set_target_properties (multi_module PROPERTIES POSITION_INDEPENDENT_CODE ON) + +# libraries +target_link_libraries(multi_module PRIVATE vmlib -lpthread -lm) diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/src/main.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/src/main.c new file mode 100644 index 00000000..ca95ded5 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/src/main.c @@ -0,0 +1,167 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "bh_read_file.h" +#include "platform_common.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; +} + +static bool +module_reader_cb(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; + } + + printf("- bh_read_file_to_buffer %s\n", wasm_file_path); + *p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_path, p_size); + BH_FREE(wasm_file_path); + return *p_buffer != NULL; +} + +static void +module_destroyer_cb(uint8 *buffer, uint32 size) +{ + printf("- release the read file buffer\n"); + if (!buffer) { + return; + } + + BH_FREE(buffer); + buffer = NULL; +} + +/* 10M */ +static char sandbox_memory_space[10 * 1024 * 1024] = { 0 }; +int +main() +{ + bool ret = false; + /* 16K */ + const uint32 stack_size = 16 * 1024; + const uint32 heap_size = 16 * 1024; + + RuntimeInitArgs init_args = { 0 }; + char error_buf[128] = { 0 }; + /* parameters and return values */ + char *args[1] = { 0 }; + + uint8 *file_buf = NULL; + uint32 file_buf_size = 0; + wasm_module_t module = NULL; + wasm_module_t module1; + wasm_module_inst_t module_inst = NULL; + + /* all malloc() only from the given buffer */ + init_args.mem_alloc_type = Alloc_With_Pool; + init_args.mem_alloc_option.pool.heap_buf = sandbox_memory_space; + init_args.mem_alloc_option.pool.heap_size = sizeof(sandbox_memory_space); + + printf("- wasm_runtime_full_init\n"); + /* initialize runtime environment */ + if (!wasm_runtime_full_init(&init_args)) { + printf("Init runtime environment failed.\n"); + goto EXIT; + } + +#if WASM_ENABLE_MULTI_MODULE != 0 + printf("- wasm_runtime_set_module_reader\n"); + /* set module reader and destroyer */ + wasm_runtime_set_module_reader(module_reader_cb, module_destroyer_cb); +#endif + + /* load WASM byte buffer from WASM bin file */ + if (!module_reader_cb("mC", &file_buf, &file_buf_size)) { + goto RELEASE_RUNTIME; + } + + /* load mC and let WAMR load mA and mB */ + printf("- wasm_runtime_load\n"); + if (!(module = wasm_runtime_load(file_buf, file_buf_size, error_buf, + sizeof(error_buf)))) { + printf("%s\n", error_buf); + goto RELEASE_BINARY; + } + + /* instantiate the module */ + printf("- wasm_runtime_instantiate\n"); + if (!(module_inst = wasm_runtime_instantiate( + module, stack_size, heap_size, error_buf, sizeof(error_buf)))) { + printf("%s\n", error_buf); + goto UNLOAD_MODULE; + } + + /* call functions of mC */ + printf("\n----------------------------------------\n"); + printf("call \"C1\", it will return 0x1f:i32, ===> "); + wasm_application_execute_func(module_inst, "C1", 0, args); + printf("call \"C2\", it will call B1() of mB and return 0x15:i32, ===> "); + wasm_application_execute_func(module_inst, "C2", 0, args); + printf("call \"C3\", it will call A1() of mA and return 0xb:i32, ===> "); + wasm_application_execute_func(module_inst, "C3", 0, args); + printf("call \"C4\", it will call B2() of mB and call A1() of mA and " + "return 0xb:i32, ===> "); + wasm_application_execute_func(module_inst, "C4", 0, args); + printf( + "call \"C5\", it will be failed since it is a export function, ===> "); + wasm_application_execute_func(module_inst, "C5", 0, args); + + /* examine module registration a bit */ + module1 = wasm_runtime_find_module_registered("mC"); + if (module1 != NULL) { + printf("unexpected module mC %p != NULL\n", module1); + goto UNLOAD_MODULE; + } + module1 = wasm_runtime_find_module_registered("mA"); + if (module1 == NULL) { + printf("unexpected module mA\n"); + goto UNLOAD_MODULE; + } + module1 = wasm_runtime_find_module_registered("mB"); + if (module1 == NULL) { + printf("unexpected module mB\n"); + goto UNLOAD_MODULE; + } + if (!wasm_runtime_register_module("mC", module, error_buf, + sizeof(error_buf))) { + printf("%s\n", error_buf); + goto UNLOAD_MODULE; + } + module1 = wasm_runtime_find_module_registered("mC"); + if (module1 != module) { + printf("unexpected module mC %p != %p\n", module1, module); + goto UNLOAD_MODULE; + } + + ret = true; + + printf("- wasm_runtime_deinstantiate\n"); + wasm_runtime_deinstantiate(module_inst); +UNLOAD_MODULE: + printf("- wasm_runtime_unload\n"); + wasm_runtime_unload(module); +RELEASE_BINARY: + module_destroyer_cb(file_buf, file_buf_size); +RELEASE_RUNTIME: + printf("- wasm_runtime_destroy\n"); + wasm_runtime_destroy(); +EXIT: + return ret ? 0 : 1; +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/CMakeLists.txt new file mode 100644 index 00000000..0dbfcc2e --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/CMakeLists.txt @@ -0,0 +1,89 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required (VERSION 2.8...3.16) +project(wasm-apps) + +message(CHECK_START "Detecting WABT") +if(NOT (DEFINED WABT_DIR OR DEFINED CACHE{WABT_DIR})) + find_path(WABT_DIR + wabt + PATHS /opt + NO_DEFAULT_PATH + NO_CMAKE_FIND_ROOT_PATH + ) + if(DEFINED WABT_DIR) + set(WABT_DIR ${WABT_DIR}/wabt) + endif() +endif() +if(WABT_DIR) + message(CHECK_PASS "found") +else() + message(CHECK_FAIL "not found") +endif() + +message(CHECK_START "Detecting WASM_OBJDUMP at ${WABT_DIR}") +find_program(WASM_OBJDUMP + wasm-objdump + PATHS "${WABT_DIR}/bin" + NO_DEFAULT_PATH + NO_CMAKE_FIND_ROOT_PATH +) +if(WASM_OBJDUMP) + message(CHECK_PASS "found") +else() + message(CHECK_FAIL "not found") +endif() + +message(CHECK_START "Detecting WASM2WAT at ${WABT_DIR}") +find_program(WASM2WAT + wasm2wat + PATHS "${WABT_DIR}/bin" + NO_DEFAULT_PATH + NO_CMAKE_FIND_ROOT_PATH +) +if(WASM2WAT) + message(CHECK_PASS "found") +else() + message(CHECK_FAIL "not found") +endif() + +function(COMPILE_WITH_CLANG SOURCE_FILE COMMAND) + get_filename_component(FILE_NAME ${SOURCE_FILE} NAME_WLE) + + set(WASM_MODULE ${FILE_NAME}.wasm) + + set(MAIN_TARGET_NAME MODULE_${FILE_NAME}) + + add_executable(${MAIN_TARGET_NAME} ${SOURCE_FILE}) + set_target_properties(${MAIN_TARGET_NAME} PROPERTIES OUTPUT_NAME ${WASM_MODULE}) + + if(${COMMAND}) + message(STATUS "Generating ${WASM_MODULE} as COMMAND...") + else() + message(STATUS "Generating ${WASM_MODULE} as REACTOR...") + target_link_options(${MAIN_TARGET_NAME} PRIVATE -mexec-model=reactor) + endif() + + if(EXISTS ${WASM2WAT}) + message(STATUS "Dumping ${WASM_MODULE}...") + set(WASM_WAT ${FILE_NAME}.wat) + set(DUMP_TARGET_NAME DUMP_${FILE_NAME}) + add_custom_command(OUTPUT ${WASM_WAT} + COMMAND ${WASM2WAT} --enable-all -o ${WASM_WAT} ${WASM_MODULE} + COMMENT "Dumping ${WASM_MODULE}..." + DEPENDS ${MAIN_TARGET_NAME} + ) + + add_custom_target(${DUMP_TARGET_NAME} ALL + DEPENDS ${WASM_WAT} + ) + endif() +endfunction() + +compile_with_clang(mA.c OFF) +compile_with_clang(mB.c OFF) +compile_with_clang(mC.c ON) +compile_with_clang(mD.cpp ON) +compile_with_clang(mE.cpp OFF) + diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mA.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mA.c new file mode 100644 index 00000000..663ec816 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mA.c @@ -0,0 +1,13 @@ +__attribute__((export_name("A1"))) int +A1() +{ + return 11; +} + +int +A2() +{ + return 12; +} + +/* mA is a reactor. it doesn't need a main() */
\ No newline at end of file diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mB.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mB.c new file mode 100644 index 00000000..a912d1d7 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mB.c @@ -0,0 +1,23 @@ +__attribute__((import_module("mA"))) +__attribute__((import_name("A1"))) extern int +A1(); + +__attribute__((export_name("B1"))) int +B1() +{ + return 21; +} + +__attribute__((export_name("B2"))) int +B2() +{ + return A1(); +} + +int +B3() +{ + return 23; +} + +/* mA is a reactor. it doesn't need a main() */
\ No newline at end of file diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mC.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mC.c new file mode 100644 index 00000000..8b19a5b6 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mC.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <stdlib.h> + +__attribute__((import_module("mA"))) +__attribute__((import_name("A1"))) extern int +A1(); + +__attribute__((import_module("mB"))) +__attribute__((import_name("B1"))) extern int +B1(); + +__attribute__((import_module("mB"))) +__attribute__((import_name("B2"))) extern int +B2(); + +__attribute__((export_name("C1"))) int +C1() +{ + return 31; +} + +__attribute__((export_name("C2"))) int +C2() +{ + return B1(); +} + +__attribute__((export_name("C3"))) int +C3() +{ + return A1(); +} + +__attribute__((export_name("C4"))) int +C4() +{ + return B2(); +} + +int +C5() +{ + return C1() + C2() + C3() + 35; +} + +int +main() +{ + printf("%u\n", C5()); + return EXIT_SUCCESS; +}
\ No newline at end of file diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mD.cpp b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mD.cpp new file mode 100644 index 00000000..d70998c0 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mD.cpp @@ -0,0 +1,74 @@ +#include <cstdlib> +#include <cstdio> +#include <iostream> + +static void +bye_main() +{ + std::cout << "mD " << __FUNCTION__ << std::endl; +} + +static void +bye_setup() +{ + std::cout << "mD " << __FUNCTION__ << std::endl; +} + +static void +bye_func() +{ + std::cout << "mD " << __FUNCTION__ << std::endl; +} + +void +func3() __attribute__((__import_module__("mE"), __import_name__("func1"))); + +void +func4() __attribute__((__import_module__("mE"), __import_name__("func2"))); + +void +func1() +{ + std::printf("mD %s\n", __FUNCTION__); + if (std::atexit(bye_func) != 0) { + std::perror("register an atexit handler failed"); + } + func3(); +} + +void +func2() +{ + std::printf("mD %s\n", __FUNCTION__); + func4(); +} + +__attribute__((constructor)) void +setup() +{ + std::cout << "mD " << __FUNCTION__ << std::endl; + if (std::atexit(bye_setup) != 0) { + std::perror("register an atexit handler failed"); + } +} + +__attribute__((destructor)) void +teardown() +{ + std::cout << "mD " << __FUNCTION__ << std::endl; +} + +int +main() +{ + std::printf("mD %s\n", __FUNCTION__); + + if (std::atexit(bye_main) != 0) { + std::perror("register an atexit handler failed"); + return EXIT_FAILURE; + } + + func1(); + func2(); + return EXIT_SUCCESS; +} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mE.cpp b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mE.cpp new file mode 100644 index 00000000..11e70af1 --- /dev/null +++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/multi-module/wasm-apps/mE.cpp @@ -0,0 +1,45 @@ +#include <cstdlib> +#include <cstdio> +#include <iostream> + +static void +bye_setup() +{ + std::cout << "mE " << __FUNCTION__ << std::endl; +} + +static void +bye_func() +{ + std::cout << "mE " << __FUNCTION__ << std::endl; +} + +__attribute__((constructor)) void +setup() +{ + std::cout << "mE " << __FUNCTION__ << std::endl; + if (std::atexit(bye_setup) != 0) { + std::perror("register an atexit handler failed"); + } +} + +__attribute__((destructor)) void +teardown() +{ + std::cout << "mE " << __FUNCTION__ << std::endl; +} + +__attribute__((export_name("func1"))) void +func1() +{ + std::cout << "mE " << __FUNCTION__ << std::endl; + if (std::atexit(bye_func) != 0) { + std::perror("register an atexit handler failed"); + } +} + +__attribute__((export_name("func2"))) void +func2() +{ + std::cout << "mE " << __FUNCTION__ << std::endl; +} |