diff --git a/.github/workflows/build-macos.yml b/.github/workflows/build-macos.yml index 87cdce6..780134a 100644 --- a/.github/workflows/build-macos.yml +++ b/.github/workflows/build-macos.yml @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@v2 - name: Install Dependency - run: brew install cxxopts fmt llvm@14 + run: brew install cxxopts fmt llvm@14 google-benchmark - name: Checkout run: git submodule update --init diff --git a/.github/workflows/build-ubuntu.yml b/.github/workflows/build-ubuntu.yml index 2478f9f..5baaefc 100644 --- a/.github/workflows/build-ubuntu.yml +++ b/.github/workflows/build-ubuntu.yml @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@v2 - name: Install Dependency - run: sudo apt install libssl-dev gcc-13 g++-13 libprotobuf-c-dev libprotobuf-dev libgrpc-dev libfmt-dev llvm-14-dev libedit-dev libcxxopts-dev libpfm4-dev liblldb-dev + run: sudo apt install libssl-dev gcc-13 g++-13 libprotobuf-c-dev libprotobuf-dev libgrpc-dev libfmt-dev llvm-14-dev libedit-dev libcxxopts-dev libpfm4-dev liblldb-dev libbenchmark-dev - name: Checkout run: git submodule update --init @@ -36,7 +36,7 @@ jobs: - name: Install Instrumented WASI SDK run: | cd /opt - sudo wget https://asplos.dev/wasi-sdk-20.0-linux.tar.gz + sudo wget http://108.181.27.85/wasi-sdk-20.0-linux.tar.gz sudo tar -xzf wasi-sdk-*.tar.gz # - name: Make WASM LD no check features diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml index f8f6d77..68608d9 100644 --- a/.github/workflows/build-windows.yml +++ b/.github/workflows/build-windows.yml @@ -34,7 +34,7 @@ jobs: git clone https://github.com/microsoft/vcpkg cd vcpkg ./bootstrap-vcpkg.bat - ./vcpkg install fmt:x64-windows cxxopts:x64-windows + ./vcpkg install fmt:x64-windows cxxopts:x64-windows benchmark:x64-windows echo "C:/tools/msys64/usr/bin/" >> "$GITHUB_PATH" C:/tools/msys64/usr/bin/pacman.exe --noconfirm -S gcc @@ -53,7 +53,7 @@ jobs: - name: Install LLVM SDK run: | cd "C:/Program Files/" - wget https://asplos.dev/LLVM.zip -O LLVM.zip + wget http://108.181.27.85/LLVM.zip -O LLVM.zip unzip LLVM.zip cp "C:/Program Files/LLVM/tools/llvm/wasm-ld.exe" D:/wasi-sdk/bin/wasm-ld.exe diff --git a/.gitignore b/.gitignore index 9fb14da..9028a9d 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ build .idea cmake-* .cache -.DS_Store \ No newline at end of file +.DS_Store +foo \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index ccc4a46..124e8bb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,15 +1,16 @@ [submodule "lib/wasm-micro-runtime"] path = lib/wasm-micro-runtime - url = https://github.com/Multi-V-VM/wasm-micro-runtime/ + url = https://github.com/Multi-V-VM/wasm-micro-runtime.git [submodule "lib/yalantinglibs"] path = lib/yalantinglibs - url = https://github.com/alibaba/yalantinglibs + url = https://github.com/alibaba/yalantinglibs.git [submodule "bench/gapbs"] path = bench/gapbs url = https://github.com/victoryang00/gapbs.git [submodule "bench/clickhouse"] path = bench/clickhouse url = https://github.com/victoryang00/ClickHouse.git + branch = Alucardik/WASM_for_clickhouse-local [submodule "bench/llama"] path = bench/llama - url = https://github.com/second-state/WasmEdge-WASINN-examples + url = https://github.com/victoryang00/llamma-wamr.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 71e3d4d..26786a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,10 @@ cmake_minimum_required(VERSION 3.22) project(MVVM LANGUAGES C CXX) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 23) -find_package(FMT REQUIRED) +find_package(fmt REQUIRED) # find_package(LLVM REQUIRED) +find_package(benchmark REQUIRED) find_package(cxxopts REQUIRED) include(CTest) if(WIN32) @@ -19,7 +20,6 @@ if(APPLE) else() set(LLVM_LLDB_LIB /usr/local/Cellar/llvm@14/14.0.6/lib/liblldb.dylib) endif() - set(WAMR_DISABLE_HW_BOUND_CHECK 1) elseif(LINUX) set(WAMR_BUILD_PLATFORM "linux") set(LLVM_LLDB_LIB /usr/lib/liblldb.so) @@ -28,7 +28,7 @@ else() set(WAMR_BUILD_LIBC_UVWASI 1) set(CMAKE_MSVC_RUNTIME_LIBRARY "") set(CMAKE_GENERATOR_PLATFORM "") - set(CMAKE_CXX_FLAGS "/MD") + set(CMAKE_CXX_FLAGS "/MDd") set(LLVM_LLDB_LIB uv -lws2_32 "C:/Program Files/LLVM/lib/liblldb.lib") endif() @@ -78,6 +78,7 @@ set(WAMR_BUILD_REF_TYPES 0) set(WAMR_BUILD_SIMD 1) set(WAMR_BUILD_CUSTOM_NAME_SECTION 1) set(WAMR_BUILD_CHECKPOINT_RESTORE 1) +set(WAMR_DISABLE_HW_BOUND_CHECK 1) set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-U,_get_ext_lib_export_apis") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") @@ -93,15 +94,15 @@ include(${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/lib/yalantinglibs/include ${LLVM_INCLUDE_DIRS}) file(GLOB_RECURSE SOURCE_FILES src/logging.cpp src/wamr*.cpp) +add_library(MVVM_export ${SOURCE_FILES} ${UNCOMMON_SHARED_SOURCE}) +add_executable(MVVM_restore src/restore.cpp ${UNCOMMON_SHARED_SOURCE}) +add_executable(MVVM_checkpoint src/checkpoint.cpp ${UNCOMMON_SHARED_SOURCE}) -add_executable(MVVM_restore ${SOURCE_FILES} src/restore.cpp ${UNCOMMON_SHARED_SOURCE}) -add_executable(MVVM_checkpoint ${SOURCE_FILES} src/checkpoint.cpp ${UNCOMMON_SHARED_SOURCE}) - -target_link_libraries(MVVM_restore fmt::fmt cxxopts::cxxopts vmlib -lm -ldl -lpthread ${LLVM_LLDB_LIB} ${LLVM_AVAILABLE_LIBS}) -target_link_libraries(MVVM_checkpoint fmt::fmt cxxopts::cxxopts vmlib -lm -ldl -lpthread ${LLVM_LLDB_LIB} ${LLVM_AVAILABLE_LIBS}) +target_link_libraries(MVVM_export fmt::fmt vmlib -lm -ldl -lpthread ${LLVM_LLDB_LIB} ${LLVM_AVAILABLE_LIBS}) +target_link_libraries(MVVM_restore fmt::fmt cxxopts::cxxopts MVVM_export vmlib -lm -ldl -lpthread ${LLVM_LLDB_LIB} ${LLVM_AVAILABLE_LIBS}) +target_link_libraries(MVVM_checkpoint fmt::fmt cxxopts::cxxopts MVVM_export vmlib -lm -ldl -lpthread ${LLVM_LLDB_LIB} ${LLVM_AVAILABLE_LIBS}) add_definitions(-DMVVM_INTERP=1) add_definitions(-DMVVM_DEBUG=1) -add_definitions(-DMVVM_WASI=1) add_definitions(-DCXXOPTS_NO_RTTI=1) add_subdirectory(test) add_subdirectory(bench) diff --git a/bench/CMakeLists.txt b/bench/CMakeLists.txt index 2fc3d5b..8f69c47 100644 --- a/bench/CMakeLists.txt +++ b/bench/CMakeLists.txt @@ -1,36 +1,58 @@ -find_package(benchmark REQUIRED) -add_executable(mvvm_benchmark bench.cpp) +add_executable(MVVM_benchmark bench.cpp) +target_link_libraries(MVVM_benchmark benchmark::benchmark MVVM_export) + +if(WIN32) + set(WASI_SDK "D:/wasi-sdk") + set(EMSDK_DIR "D:/emsdk/upstream") + set(WASI_EXE_POSTFIX ".exe") +else() + set(WASI_SDK "/opt/wasi-sdk") + set(EMSDK_DIR "/opt/emsdk/upstream") + set(WASI_EXE_POSTFIX "") +endif() -# Link benchmark library to your test target -target_link_libraries(mvvm_benchmark benchmark) include(ExternalProject) -# add_subdirectory(gapbs) -# add_subdirectory(clickhouse) -# ExternalProject_Add(gapbs -# SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gapbs -# UPDATE_COMMAND "" -# PATCH_COMMAND "" -# CONFIGURE_COMMAND ${CMAKE_COMMAND} -# -DCMAKE_C_COMPILER_WORKS=1 -DCMAKE_CXX_COMPILER_WORKS=1 -# -GNinja -# -DWASI_SDK_DIR=${WASI_SDK_DIR} -# -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_DIR}/share/cmake/wasi-sdk.cmake -# -DCMAKE_SYSROOT=${WASI_SDK_DIR}/share/wasi-sysroot -# ${CMAKE_CURRENT_SOURCE_DIR}/gapbs -# BUILD_COMMAND ${CMAKE_COMMAND} --build . -# ) +ExternalProject_Add(gapbs + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gapbs + UPDATE_COMMAND "" + PATCH_COMMAND "" + CONFIGURE_COMMAND ${CMAKE_COMMAND} + -GNinja -DWASI_SDK_DIR=${WASI_SDK} -DWASI_SDK_PREFIX=${WASI_SDK} + -DCMAKE_C_COMPILER=${WASI_SDK}/bin/clang${WASI_EXE_POSTFIX} + -DCMAKE_CXX_COMPILER=${WASI_SDK}/bin/clang++${WASI_EXE_POSTFIX} + -DCMAKE_CXX_FLAGS="-fno-exceptions" + -DCMAKE_SYSROOT=${WASI_SDK}/share/wasi-sysroot + ${CMAKE_CURRENT_SOURCE_DIR}/gapbs + BUILD_COMMAND ${CMAKE_COMMAND} --build . + INSTALL_COMMAND "" +) +if(NOT WIN32) + ExternalProject_Add(clickhouse + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/clickhouse + UPDATE_COMMAND "" + PATCH_COMMAND "" + CONFIGURE_COMMAND ${CMAKE_COMMAND} + -GNinja + -DCMAKE_C_COMPILER=${EMSDK_DIR}/bin/clang${WASI_EXE_POSTFIX} + -DCMAKE_CXX_COMPILER=${EMSDK_DIR}/bin/clang++${WASI_EXE_POSTFIX} + -DCMAKE_SYSROOT=${EMSDK_DIR}/emscripten/cache/sysroot -DEMSCRIPTEN=1 + ${CMAKE_CURRENT_SOURCE_DIR}/clickhouse + BUILD_COMMAND ${CMAKE_COMMAND} --build . + INSTALL_COMMAND "" + ) -# ExternalProject_Add(clickhouse -# SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/clickhouse -# UPDATE_COMMAND "" -# PATCH_COMMAND "" -# CONFIGURE_COMMAND ${CMAKE_COMMAND} -# -DCMAKE_C_COMPILER_WORKS=1 -DCMAKE_CXX_COMPILER_WORKS=1 -# -GNinja -# -DWASI_SDK_DIR=${WASI_SDK_DIR} -# -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_DIR}/share/cmake/wasi-sdk.cmake -# -DCMAKE_SYSROOT=${WASI_SDK_DIR}/share/wasi-sysroot -# ${CMAKE_CURRENT_SOURCE_DIR}/clickhouse -# BUILD_COMMAND ${CMAKE_COMMAND} --build . -# ) \ No newline at end of file + ExternalProject_Add(llama + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/llama + UPDATE_COMMAND "" + PATCH_COMMAND "" + CONFIGURE_COMMAND ${CMAKE_COMMAND} + -GNinja -DWASI_SDK_DIR=${WASI_SDK} -DWASI_SDK_PREFIX=${WASI_SDK} + -DCMAKE_C_COMPILER=${WASI_SDK}/bin/clang${WASI_EXE_POSTFIX} + -DCMAKE_CXX_COMPILER=${WASI_SDK}/bin/clang++${WASI_EXE_POSTFIX} + -DCMAKE_SYSROOT=${WASI_SDK}/share/wasi-sysroot + ${CMAKE_CURRENT_SOURCE_DIR}/llama + BUILD_COMMAND ${CMAKE_COMMAND} --build . + INSTALL_COMMAND "" + ) +endif() \ No newline at end of file diff --git a/bench/bench.cpp b/bench/bench.cpp index 019d6ee..72736c2 100644 --- a/bench/bench.cpp +++ b/bench/bench.cpp @@ -1,13 +1,69 @@ +#define _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH 1 #include #include +WAMRInstance * wamr; + +void serialize_to_file(WASMExecEnv *instance) { + /** Sounds like AoT/JIT is in this?*/ + // Note: insert fd + // a fast restore interface for benchmarking + std::ifstream stdoutput; +} +//./bfs -g10 -n0 > test/out/generate-g10.out +// \033[92mPASS\033[0m Generates g10 +//./bfs -u10 -n0 > test/out/generate-u10.out +// \033[91mFAIL\033[0m Generates u10 +//./bfs -f test/graphs/4.gr -n0 > test/out/load-4.gr.out +// \033[92mPASS\033[0m Load 4.gr +//./bfs -f test/graphs/4.el -n0 > test/out/load-4.el.out +// \033[92mPASS\033[0m Load 4.el +//./bfs -f test/graphs/4.wel -n0 > test/out/load-4.wel.out +// \033[92mPASS\033[0m Load 4.wel +//./bfs -f test/graphs/4.graph -n0 > test/out/load-4.graph.out +// \033[92mPASS\033[0m Load 4.graph +//./bfs -f test/graphs/4w.graph -n0 > test/out/load-4w.graph.out +// \033[92mPASS\033[0m Load 4w.graph +//./bfs -f test/graphs/4.mtx -n0 > test/out/load-4.mtx.out +// \033[92mPASS\033[0m Load 4.mtx +//./bfs -f test/graphs/4w.mtx -n0 > test/out/load-4w.mtx.out +// \033[92mPASS\033[0m Load 4w.mtx +//./bc -g10 -vn1 > test/out/verify-bc-g10.out +// \033[92mPASS\033[0m Verify bc +//./bfs -g10 -vn1 > test/out/verify-bfs-g10.out +// \033[92mPASS\033[0m Verify bfs +//./cc -g10 -vn1 > test/out/verify-cc-g10.out +// \033[92mPASS\033[0m Verify cc +//./cc_sv -g10 -vn1 > test/out/verify-cc_sv-g10.out +// \033[92mPASS\033[0m Verify cc_sv +//./pr -g10 -vn1 > test/out/verify-pr-g10.out +// \033[92mPASS\033[0m Verify pr +//./pr_spmv -g10 -vn1 > test/out/verify-pr_spmv-g10.out +// \033[92mPASS\033[0m Verify pr_spmv +//./sssp -g10 -vn1 > test/out/verify-sssp-g10.out +// \033[92mPASS\033[0m Verify sssp +//./tc -g10 -vn1 > test/out/verify-tc-g10.out static void BM_gapbs(benchmark::State& state) { + auto dir = {std::string("./")}; + auto map_dir = {std::string("./")}; + std::vector env = {}; + std::vector cmd = {"bfs.wasm","bfs.wasm", "bfs.wasm", "bfs.wasm", "bfs.wasm", "bfs.wasm", "bfs.wasm", "bfs.wasm", "bfs.wasm", "bc.wasm", "bfs.wasm", "cc.wasm", "cc_sv.wasm", "pr.wasm", "pr_spmv.wasm", "sssp.wasm", "tc.wasm"}; + std::vector> arg = {{"-g10","-n0"},{"-u10","-n0"},{"-f","test/graphs/4.gr","-n0"},{"-f","test/graphs/4.el","-n0"},{"-f","test/graphs/4.wel","-n0"},{"-f","test/graphs/4.graph","-n0"},{"-f","test/graphs/4w.graph","-n0"},{"-f","test/graphs/4.mtx","-n0"},{"-f","test/graphs/4w.mtx","-n0"},{"-g10","-vn1"},{"-g10","-vn1"},{"-g10","-vn1"},{"-g10","-vn1"},{"-g10","-vn1"},{"-g10","-vn1"},{"-g10","-vn1"},{"-g10","-vn1"}}; + std::vector addr = {}; + std::vector ns_pool = {}; + auto is_jit = false; for (auto _ : state) { - // Your code to benchmark goes here + for (auto const &[k, v] : cmd | enumerate) { + wamr = new WAMRInstance(v.c_str(), is_jit); + wamr->set_wasi_args(dir, map_dir, env, arg[k], addr, ns_pool); + wamr->instantiate(); + wamr->invoke_main(); + } } } BENCHMARK(BM_gapbs); +#if !defined(__WIN32__) static void BM_clickhouse(benchmark::State& state) { for (auto _ : state) { // Your code to benchmark goes here @@ -16,10 +72,38 @@ static void BM_clickhouse(benchmark::State& state) { BENCHMARK(BM_clickhouse); static void BM_llama(benchmark::State& state) { + auto dir = {std::string("./")}; + auto map_dir = {std::string("./")}; + std::vector env = {}; + std::vector arg = {}; + std::vector addr = {}; + std::vector ns_pool = {}; + auto is_jit = false; for (auto _ : state) { - // Your code to benchmark goes here + wamr = new WAMRInstance("./llama.wasm", is_jit); + wamr->set_wasi_args(dir, map_dir, env, arg, addr, ns_pool); + wamr->instantiate(); + wamr->invoke_main(); } } BENCHMARK(BM_llama); +static void BM_llama_aot(benchmark::State& state) { + auto dir = {std::string("./")}; + auto map_dir = {std::string("./")}; + std::vector env = {}; + std::vector arg = {}; + std::vector addr = {}; + std::vector ns_pool = {}; + auto is_jit = false; + for (auto _ : state) { + wamr = new WAMRInstance("./llama.aot", is_jit); + wamr->set_wasi_args(dir, map_dir, env, arg, addr, ns_pool); + wamr->instantiate(); + wamr->invoke_main(); + } +} +BENCHMARK(BM_llama_aot); +#endif + BENCHMARK_MAIN(); \ No newline at end of file diff --git a/bench/gapbs b/bench/gapbs index 62258d0..9b657ec 160000 --- a/bench/gapbs +++ b/bench/gapbs @@ -1 +1 @@ -Subproject commit 62258d0edb4fce97d1080a41444fb7abc987a123 +Subproject commit 9b657ec7180548f4267901bd2cc8d5fc0bc90ae5 diff --git a/bench/llama b/bench/llama index b825987..76bd793 160000 --- a/bench/llama +++ b/bench/llama @@ -1 +1 @@ -Subproject commit b825987c4813d153f1d11b96aa7734c52408faf4 +Subproject commit 76bd7935cf97d259b55b85b7980235d856f9e8b6 diff --git a/include/logging.h b/include/logging.h index 4c69b72..0657b5f 100644 --- a/include/logging.h +++ b/include/logging.h @@ -18,9 +18,18 @@ #include #include #include -#include +#include #include +/** Barry's work*/ +struct Enumerate : std::ranges::range_adaptor_closure { + template constexpr auto operator()(R &&r) const { + return std::views::zip(std::views::iota(0), (R &&)r); + } +}; + +inline constexpr Enumerate enumerate; + using std::list; using std::string; struct LocationInfo { diff --git a/include/wamr.h b/include/wamr.h index 0e91331..3706b77 100644 --- a/include/wamr.h +++ b/include/wamr.h @@ -8,6 +8,7 @@ #include "bh_read_file.h" #include "logging.h" #include "wamr_exec_env.h" +#include "wamr_export.h" #include "wamr_read_write.h" #include "wamr_wasi_context.h" #include "wasm_runtime.h" @@ -28,9 +29,10 @@ class WAMRInstance { std::vector arg_; std::vector addr_; std::vector ns_pool_; - std::map> fd_map_; + std::map> fd_map_; // add offset to pair->tuple, 3rd param 'int' - std::map socket_fd_map_; + std::map socket_fd_map_; + bool is_jit; char *buffer{}; char error_buf[128]{}; @@ -58,9 +60,19 @@ class WAMRInstance { const std::vector &addr_list, const std::vector &ns_lookup_pool); int invoke_main(); - int invoke_fopen(uint32 fd,const std::string& path, uint32 option); + int invoke_fopen(std::string &path, uint32 option); + int invoke_frenumber(uint32 fd, uint32 to); int invoke_fseek(uint32 fd, uint32 offset); - int invoke_preopen(uint32 fd,const std::string& path); + int invoke_preopen(uint32 fd, const std::string &path); + int invoke_socket(uint32 domain, const std::string &path); + int invoke_(uint32 domain, const std::string &path); +#if !defined(__WINCRYPT_H__) + int invoke_sock_sendto(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len); + int invoke_sock_recvfrom(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len); +#endif + int invoke_sock_open(uint32_t poolfd, int af, int socktype, uint32_t *sockfd); ~WAMRInstance(); }; #endif // MVVM_WAMR_H diff --git a/include/wamr_export.h b/include/wamr_export.h index 2a87e54..c4dfd53 100644 --- a/include/wamr_export.h +++ b/include/wamr_export.h @@ -1,17 +1,36 @@ // // Created by victoryang00 on 6/17/23. // - +#include "wasm_runtime.h" #ifdef __cplusplus extern "C" { #endif -#ifdef MVVM_WASI -#include "wasm_runtime.h" -void serialize_to_file(struct WASMExecEnv *); -#endif +#if !defined(MVVM_WASI) +#define MVVM_WASI +struct SocketAddrPool { + uint16 ip4[4]; + uint16 ip6[8]; + bool is_4; + uint16 port; +}; +#if !defined(__WINCRYPT_H__) +typedef struct iovec_app { + uint32 buf_offset; + uint32 buf_len; +} iovec_app_t; +#endif +void serialize_to_file(struct WASMExecEnv *); +#endif +#if !defined(__WINCRYPT_H__) +void insert_sock_send_to_data(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len); +void insert_sock_open_data(uint32_t, int, int, uint32_t); +void insert_sock_recv_from_data(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len); +#endif void insert_fd(int, char const *, int, int); void remove_fd(int); void insert_socket(int, int, int, int); diff --git a/include/wamr_module_instance.h b/include/wamr_module_instance.h index b0c5fcd..6a5ffff 100644 --- a/include/wamr_module_instance.h +++ b/include/wamr_module_instance.h @@ -127,11 +127,11 @@ struct WAMRModuleInstance { memories.push_back(local_mem); } global_data = std::vector(env->global_data, env->global_data + env->global_data_size); - LOGV(DEBUG) << env->global_data_size; - for (int i = 0; i < env->global_data_size; i++) { - LOGV(DEBUG) << env->global_data[i]; - } -// tables = std::vector(env->tables, env->tables + env->table_count); + LOGV(DEBUG) << env->global_data_size; + for (int i = 0; i < env->global_data_size; i++) { + LOGV(DEBUG) << env->global_data[i]; + } + // tables = std::vector(env->tables, env->tables + env->table_count); tables.reserve(env->table_count); std::generate_n( std::back_inserter(tables), env->table_count, @@ -156,13 +156,13 @@ struct WAMRModuleInstance { } memcpy(env->global_data, global_data.data(), global_data.size()); env->global_data_size = global_data.size(); -// env->global_data = global_data.data(); -// env->global_data_size = global_data.size() - 1; - LOGV(DEBUG) << env->global_data_size; - LOGV(DEBUG) << env->global_data; - for (int i = 0; i < env->global_data_size; i++) { - LOGV(DEBUG) << env->global_data[i]; - } + // env->global_data = global_data.data(); + // env->global_data_size = global_data.size() - 1; + LOGV(DEBUG) << env->global_data_size; + LOGV(DEBUG) << env->global_data; + for (int i = 0; i < env->global_data_size; i++) { + LOGV(DEBUG) << env->global_data[i]; + } env->table_count = tables.size(); for (int i = 0; i < env->table_count; i++) { *env->tables[i] = tables[i]; diff --git a/include/wamr_module_instance_extra.h b/include/wamr_module_instance_extra.h index 8e3133e..2a793f3 100644 --- a/include/wamr_module_instance_extra.h +++ b/include/wamr_module_instance_extra.h @@ -5,7 +5,7 @@ #ifndef MVVM_WAMR_MODULE_INSTANCE_EXTRA_H #define MVVM_WAMR_MODULE_INSTANCE_EXTRA_H -#if WASM_ENABLE_WASI_NN!=0 +#if WASM_ENABLE_WASI_NN != 0 #include "wamr_wasi_nn_context.h" #endif #include "wasm_runtime.h" @@ -42,27 +42,28 @@ struct WAMRModuleInstanceExtra { // WASMModuleInstance *next; // #endif // -#if WASM_ENABLE_WASI_NN!=0 +#if WASM_ENABLE_WASI_NN != 0 WAMRWASINNContext wasi_nn_ctx{}; #endif void dump_impl(WASMModuleInstanceExtra *env) { global_count = env->global_count; function_count = env->function_count; -#if WASM_ENABLE_WASI_NN!=0 +#if WASM_ENABLE_WASI_NN != 0 dump(&wasi_nn_ctx, env->wasi_nn_ctx); #endif }; - void restore_impl(WASMModuleInstanceExtra *env) const { + void restore_impl(WASMModuleInstanceExtra *env) { env->global_count = global_count; env->function_count = function_count; -#if WASM_ENABLE_WASI_NN!=0 +#if WASM_ENABLE_WASI_NN != 0 restore(&wasi_nn_ctx, env->wasi_nn_ctx); #endif }; }; template T> void dump(T t, WASMModuleInstanceExtra *env) { - t->dump_impl(env); } + t->dump_impl(env); +} template T> void restore(T t, WASMModuleInstanceExtra *env) { t->restore_impl(env); diff --git a/include/wamr_wasi_context.h b/include/wamr_wasi_context.h index 51bca1c..c4b5f47 100644 --- a/include/wamr_wasi_context.h +++ b/include/wamr_wasi_context.h @@ -6,7 +6,7 @@ #define MVVM_WAMR_WASI_CONTEXT_H #include "logging.h" -#include "platform_common.h" +#include "wamr_export.h" #include "wamr_serializer.h" #include "wasm_runtime.h" #include @@ -29,15 +29,49 @@ struct WAMRAddrPool { bool is_4; uint8 mask; }; +struct WAMRWasiAddr { + WAMRAddrPool ip; + uint16 port; +}; +struct WasiSockOpenData { + uint32 poolfd; + int af; + int socktype; + uint32 sockfd; +}; +#if !defined(__WINCRYPT_H__) +struct WasiSockSendToData { + uint32 sock; + iovec_app_t si_data; + uint32 si_data_len; + uint16_t si_flags; + WAMRWasiAddr dest_addr; + uint32 so_data_len; +}; +struct WasiSockRecvFromData { + uint32_t sock; + iovec_app_t ri_data; + uint32 ri_data_len; + uint16_t ri_flags; + WAMRWasiAddr src_addr; + uint32 ro_data_len; +}; +#endif struct SocketMetaData { int domain{}; int type{}; int protocol{}; - SocketAddrPool socketAddress; + SocketAddrPool socketAddress{}; + WasiSockOpenData socketOpenData{}; +#if !defined(__WINCRYPT_H__) + WasiSockSendToData socketSentToData{}; + WasiSockRecvFromData socketRecvFromData{}; +#endif }; - struct WAMRWASIContext { + std::map> fd_map; + std::map socket_fd_map; std::vector dir; std::vector map_dir; WAMRArgvEnvironValues argv_environ; diff --git a/include/wamr_wasi_nn_context.h b/include/wamr_wasi_nn_context.h index 2fa36d4..b1fdd1f 100644 --- a/include/wamr_wasi_nn_context.h +++ b/include/wamr_wasi_nn_context.h @@ -4,18 +4,46 @@ #ifndef MVVM_WAMR_WASI_NN_CONTEXT_H #define MVVM_WAMR_WASI_NN_CONTEXT_H -#include "wamr_serializer.h +#include "tensorflow/lite/interpreter.h" +#include "tensorflow/lite/model.h" +#include "wamr_serializer.h" #include "wasi_nn.h" +#include "wasi_nn_private.h" +#include +#include +// https://github.com/WebAssembly/wasi-nn/blob/0f77c48ec195748990ff67928a4b3eef5f16c2de/wasi-nn.wit.md +/* Maximum number of graphs per WASM instance */ +#define MAX_GRAPHS_PER_INST 10 +/* Maximum number of graph execution context per WASM instance*/ +#define MAX_GRAPH_EXEC_CONTEXTS_PER_INST 10 + +struct WAMRWASINNGraph { + std::vector buffer; +}; + +struct WAMRWASINNInterpreter { + // std::unique_ptr interpreter; + uint32_t a; //placeholder +}; + +struct WAMRWASINNModel { + // std::unique_ptr model; + execution_target target; +}; + struct WAMRWASINNContext { bool is_initialized; graph_encoding current_encoding; + std::vector graph; // TODO: support multiple graph uint32_t current_models; - Model models[MAX_GRAPHS_PER_INST]; + WAMRWASINNModel models[MAX_GRAPHS_PER_INST]; uint32_t current_interpreters; - Interpreter interpreters[MAX_GRAPH_EXEC_CONTEXTS_PER_INST]; -}; + WAMRWASINNInterpreter interpreters[MAX_GRAPH_EXEC_CONTEXTS_PER_INST]; -template T> void dump(T t, WASINNContext *env) { t->dump_impl(env); } + void dump_impl(WASINNContext *env); + void restore_impl(WASINNContext *env); +}; -template T> void restore(T t, WASINNContext *env) { t->restore_impl(env); } +template T> void dump(T t, WASINNContext *env) { t->dump_impl(env); } +template T> void restore(T t, WASINNContext *env) { t->restore_impl(env); } #endif // MVVM_WAMR_WASI_NN_CONTEXT_H diff --git a/lib/wasm-micro-runtime b/lib/wasm-micro-runtime index 15ee584..67e1ae0 160000 --- a/lib/wasm-micro-runtime +++ b/lib/wasm-micro-runtime @@ -1 +1 @@ -Subproject commit 15ee5849d63cf2856a0e0b8faa1ff55bee576978 +Subproject commit 67e1ae0b50026ab4f375297256e9893379c99635 diff --git a/src/checkpoint.cpp b/src/checkpoint.cpp index 8a4cce7..0ed79e9 100644 --- a/src/checkpoint.cpp +++ b/src/checkpoint.cpp @@ -17,110 +17,24 @@ WAMRInstance *wamr = nullptr; std::ostringstream re{}; -auto writer = FwriteStream("test.bin"); -constinit std::vector> as; -constinit std::mutex as_mtx; -/**fopen, fseek*/ -void insert_fd(int fd, const char *path, int flags, int offset) { - printf("\n #insert_fd(fd,filename,flags, offset) %d %s %d %d \n\n",fd, path,flags, offset); - - if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) { - LOGV(ERROR) << "fd already exist" << fd; - if(offset == 0) { - // fOpen call - std::string curPath; - int curFlags; - int curOffset; - std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; - wamr->fd_map_[fd] = std::make_tuple(std::string(path), flags, curOffset); - } else { - // fSeek call - std::string curPath; - int curFlags; - int curOffset; - std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; - wamr->fd_map_[fd] = std::make_tuple(curPath, curFlags, offset); - } - - } else - wamr->fd_map_.insert(std::make_pair(fd, std::make_tuple(std::string(path), flags, offset))); -} - -/**fclose */ -void remove_fd(int fd) { - if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) - wamr->fd_map_.erase(fd); - else - LOGV(ERROR)<< "fd not found" << fd; -} - -/* - create fd-socketmetadata map and store the "domain", "type", "protocol" value -*/ -void insert_socket(int fd, int domain, int type, int protocol){ - printf("\n #insert_socket(fd, domain, type, protocol) %d %d %d %d \n\n",fd, domain, type, protocol); - - if (wamr->socket_fd_map_.find(fd) != wamr->socket_fd_map_.end()) { - LOGV(ERROR) << "socket_fd already exist" << fd; - } else{ - SocketMetaData metaData{}; - metaData.domain = domain; - metaData.type = type; - metaData.protocol = protocol; - wamr->socket_fd_map_.insert(std::make_pair(fd, metaData)); - } -} - -void update_socket_fd_address(int fd, SocketAddrPool *address) { - printf("\n #update_socket_fd_address(fd, address) %d \n\n",fd); - - if (wamr->socket_fd_map_.find(fd) == wamr->socket_fd_map_.end()) { - // note: ? fd here is not same as insert_socket? - // set default value - insert_socket(fd, 0, 0, 0); - } - - SocketMetaData metaData{}; - metaData.domain = wamr->socket_fd_map_[fd].domain; - metaData.type = wamr->socket_fd_map_[fd].type; - metaData.protocol = wamr->socket_fd_map_[fd].protocol; - - metaData.socketAddress.port = address->port; - if(address->is_4) { - metaData.socketAddress.is_4 = true; - metaData.socketAddress.ip4[0] = address->ip4[0]; - metaData.socketAddress.ip4[1] = address->ip4[1]; - metaData.socketAddress.ip4[2] = address->ip4[2]; - metaData.socketAddress.ip4[3] = address->ip4[3]; - - } else { - metaData.socketAddress.ip6[0] = address->ip6[0]; - metaData.socketAddress.ip6[1] = address->ip6[1]; - metaData.socketAddress.ip6[2] = address->ip6[2]; - metaData.socketAddress.ip6[3] = address->ip6[3]; - metaData.socketAddress.ip6[4] = address->ip6[4]; - metaData.socketAddress.ip6[5] = address->ip6[5]; - metaData.socketAddress.ip6[6] = address->ip6[6]; - metaData.socketAddress.ip6[7] = address->ip6[7]; - } - wamr->socket_fd_map_[fd] = metaData; - -} - +FwriteStream *writer; +std::vector> as; +std::mutex as_mtx; void serialize_to_file(WASMExecEnv *instance) { /** Sounds like AoT/JIT is in this?*/ // Note: insert fd - std::ifstream stdoutput; stdoutput.open("output.txt"); - std:string current_str; + std::ifstream stdoutput; +std: + string current_str; std::string fd_output; std::string filename_output; std::string flags_output; - - if(stdoutput.is_open()) { - while(stdoutput.good()) { + + if (stdoutput.is_open()) { + while (stdoutput.good()) { stdoutput >> current_str; - if(current_str == "fopen_test(fd,filename,flags)") { - stdoutput >> fd_output; + if (current_str == "fopen_test(fd,filename,flags)") { + stdoutput >> fd_output; stdoutput >> filename_output; stdoutput >> flags_output; insert_fd(std::stoi(fd_output), filename_output.c_str(), std::stoi(flags_output), 0); @@ -147,7 +61,7 @@ void serialize_to_file(WASMExecEnv *instance) { as.emplace_back(a); as.back().get()->cur_count = cur_count; if (as.size() == all_count) { - struct_pack::serialize_to(writer, as); + struct_pack::serialize_to(*writer, as); LOGV(INFO) << "serialize to file" << cur_count << " " << all_count << "\n"; } as_mtx.unlock(); @@ -158,7 +72,7 @@ void serialize_to_file(WASMExecEnv *instance) { dump(a, instance); as.emplace_back(a); as.back().get()->cur_count = 0; - struct_pack::serialize_to(writer, as); + struct_pack::serialize_to(*writer, as); } exit(0); @@ -203,8 +117,7 @@ int main(int argc, char *argv[]) { options.add_options()("t,target", "The webassembly file to execute", cxxopts::value()->default_value("./test/counter.wasm"))( "j,jit", "Whether the jit mode or interp mode", cxxopts::value()->default_value("false"))( - "d,dir", "The directory list exposed to WAMR", - cxxopts::value>()->default_value("./"))( + "d,dir", "The directory list exposed to WAMR", cxxopts::value>()->default_value("./"))( "m,map_dir", "The mapped directory list exposed to WAMRe", cxxopts::value>()->default_value(""))( "e,env", "The environment list exposed to WAMR", @@ -214,7 +127,18 @@ int main(int argc, char *argv[]) { "n,ns_pool", "The ns lookup pool exposed to WAMR", cxxopts::value>()->default_value(""))("h,help", "The value for epoch value", cxxopts::value()->default_value("false")); - + auto removeExtension = [](std::string &filename) { + size_t dotPos = filename.find_last_of('.'); + std::string res; + if (dotPos != std::string::npos) { + // Extract the substring before the period + res = filename.substr(0, dotPos); + } else { + // If there's no period in the string, it means there's no extension. + LOGV(ERROR) << "No extension found."; + } + return res; + }; auto result = options.parse(argc, argv); if (result["help"].as()) { std::cout << options.help() << std::endl; @@ -228,10 +152,10 @@ int main(int argc, char *argv[]) { auto arg = result["arg"].as>(); auto addr = result["addr"].as>(); auto ns_pool = result["ns_pool"].as>(); + writer = new FwriteStream((removeExtension(target) + ".bin").c_str()); wamr = new WAMRInstance(target.c_str(), is_jit); wamr->set_wasi_args(dir, map_dir, env, arg, addr, ns_pool); wamr->instantiate(); - freopen("output.txt","w",stdout); #ifndef MVVM_DEBUG // Define the sigaction structure diff --git a/src/restore.cpp b/src/restore.cpp index 23635f5..385c3f2 100644 --- a/src/restore.cpp +++ b/src/restore.cpp @@ -12,18 +12,9 @@ #include #include -auto reader = FreadStream("test.bin"); +FreadStream *reader; WAMRInstance *wamr = nullptr; -void insert_fd(int fd, const char *path, int flags, int offset){}; -void remove_fd(int fd) {} void serialize_to_file(WASMExecEnv *instance) {} -void insert_socket(int fd, int domain, int type, int protocol){}; -void update_socket_fd_address(int fd, SocketAddrPool *address){}; -void remove_socket(char const *){}; -void insert_lock(char const *, int){}; -void insert_sem(char const *, int){}; -void remove_lock(char const *){}; -void remove_sem(char const *){}; int main(int argc, char **argv) { cxxopts::Options options("MVVM", "Migratable Velocity Virtual Machine, to ship the VM state to another machine"); options.add_options()("t,target", "The webassembly file to execute", @@ -31,6 +22,18 @@ int main(int argc, char **argv) { "j,jit", "Whether the jit mode or interp mode", cxxopts::value()->default_value("false"))( "h,help", "The value for epoch value", cxxopts::value()->default_value("false")); // Can first discover from the wasi context. + auto removeExtension = [](std::string &filename) { + size_t dotPos = filename.find_last_of('.'); + std::string res; + if (dotPos != std::string::npos) { + // Extract the substring before the period + res = filename.substr(0, dotPos); + } else { + // If there's no period in the string, it means there's no extension. + LOGV(ERROR) << "No extension found."; + } + return res; + }; auto result = options.parse(argc, argv); if (result["help"].as()) { @@ -38,8 +41,9 @@ int main(int argc, char **argv) { exit(0); } auto target = result["target"].as(); + reader = new FreadStream((removeExtension(target) + ".bin").c_str()); wamr = new WAMRInstance(target.c_str(), false); - auto a = struct_pack::deserialize>>(reader).value(); + auto a = struct_pack::deserialize>>(*reader).value(); wamr->instantiate(); wamr->recover(&a); return 0; diff --git a/src/wamr.cpp b/src/wamr.cpp index 52945fe..d696108 100644 --- a/src/wamr.cpp +++ b/src/wamr.cpp @@ -3,8 +3,9 @@ // #include "wamr.h" +#include "platform_common.h" #include "thread_manager.h" -#include "wasm_exec_env.h" +#include "wasm_export.h" #include "wasm_interp.h" #include "wasm_runtime.h" #include @@ -23,6 +24,7 @@ WAMRInstance::WAMRInstance(const char *wasm_path, bool is_jit) : is_jit(is_jit) wasm_args.running_mode = RunningMode::Mode_Interp; else wasm_args.running_mode = RunningMode::Mode_LLVM_JIT; + // static char global_heap_buf[512 * 1024];// what is this? // wasm_args.mem_alloc_type = Alloc_With_Pool; // wasm_args.mem_alloc_option.pool.heap_buf = global_heap_buf; // wasm_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf); @@ -77,60 +79,298 @@ int WAMRInstance::invoke_main() { return wasm_runtime_call_wasm(exec_env, func, 0, nullptr); } -int WAMRInstance::invoke_fopen(uint32 fd, const std::string &path, uint32 option) { - if (!(func = wasm_runtime_lookup_function(module_inst, "open", "($i)i"))) { - LOGV(ERROR) << "The wasi open function is not found."; - return -1; +int WAMRInstance::invoke_fopen(std::string &path, uint32 option) { + auto name = "__wasilibc_open_nomode"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi fopen function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } } char *buffer_ = nullptr; uint32_t buffer_for_wasm; - buffer_for_wasm = wasm_runtime_module_malloc(module_inst, 100, reinterpret_cast(&buffer_)); + buffer_for_wasm = wasm_runtime_module_malloc(module_inst, path.size(), (void **)&buffer_); if (buffer_for_wasm != 0) { uint32 argv[2]; - strncpy(buffer_, path.c_str(), path.size()); // use native address for accessing in runtime argv[0] = buffer_for_wasm; // pass the buffer_ address for WASM space argv[1] = option; // the size of buffer_ - auto res = wasm_runtime_call_wasm(exec_env, func, 2, argv); + strncpy(buffer_, path.c_str(), path.size()); // use native address for accessing in runtime + wasm_runtime_call_wasm(exec_env, func, 2, argv); wasm_runtime_module_free(module_inst, buffer_for_wasm); - return res; + return ((int)argv[0]); } return -1; }; -int WAMRInstance::invoke_fseek(uint32 fd, uint32 offset) { - if (!(func = wasm_runtime_lookup_function(module_inst, "fseek", "($i)i"))) { - LOGV(ERROR) << "The wasi open function is not found."; - return -1; +int WAMRInstance::invoke_frenumber(uint32 fd, uint32 to) { + auto name = "__wasi_fd_renumber"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi fopen function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } } - char *buffer_ = nullptr; + uint32 argv[2] = {fd, to}; + return wasm_runtime_call_wasm(exec_env, func, 2, argv); +}; + +int WAMRInstance::invoke_sock_open(uint32_t poolfd, int af, int socktype, uint32_t *sockfd) { + auto name = "__wasi_sock_open"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi fopen function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } + } + void *buffer_ = nullptr; uint32_t buffer_for_wasm; - buffer_for_wasm = wasm_runtime_module_malloc(module_inst, 100, reinterpret_cast(&buffer_)); + buffer_for_wasm = wasm_runtime_module_malloc(module_inst, sizeof(uint32), reinterpret_cast(&buffer_)); if (buffer_for_wasm != 0) { - uint32 argv[2]; + memcpy(buffer_, &sockfd, sizeof(uint32)); // use native address for accessing in runtime + uint32 argv[4] = {poolfd, static_cast(af), static_cast(socktype), buffer_for_wasm}; + auto res = wasm_runtime_call_wasm(exec_env, func, 4, argv); + wasm_runtime_module_free(module_inst, buffer_for_wasm); + return res; + } + return -1; +} +#if !defined(__WINCRYPT_H__) +int WAMRInstance::invoke_sock_sendto(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len) { + auto name = "__wasi_sock_send_to"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi fopen function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } + } + + void *si_data_ = nullptr; + uint32_t si_data_for_wasm; + void *dest_addr_ = nullptr; + uint32_t dest_addr_for_wasm; + void *so_data_len_ = nullptr; + uint32_t so_data_len_for_wasm; + int res = -1; + + si_data_for_wasm = + wasm_runtime_module_malloc(module_inst, sizeof(iovec_app_t), reinterpret_cast(&si_data_)); + if (si_data_for_wasm != 0) { + memcpy(si_data_, &si_data, sizeof(iovec_app_t)); // use native address for accessing in runtime + + dest_addr_for_wasm = + wasm_runtime_module_malloc(module_inst, sizeof(__wasi_addr_t), reinterpret_cast(&dest_addr_)); + if (dest_addr_for_wasm != 0) { + memcpy(dest_addr_, &dest_addr, + sizeof(__wasi_addr_t)); // use native address for accessing in runtime + + so_data_len_for_wasm = + wasm_runtime_module_malloc(module_inst, sizeof(uint32), reinterpret_cast(&so_data_len_)); + if (so_data_len_for_wasm != 0) { + memcpy(so_data_len_, &so_data_len, + sizeof(uint32)); // use native address for accessing in runtime + uint32 argv[6] = {sock, si_data_for_wasm, si_data_len, + si_flags, dest_addr_for_wasm, so_data_len_for_wasm}; + + wasm_runtime_call_wasm(exec_env, func, 6, argv); + res = argv[0]; + wasm_runtime_module_free(module_inst, si_data_for_wasm); + } + wasm_runtime_module_free(module_inst, dest_addr_for_wasm); + } + wasm_runtime_module_free(module_inst, so_data_len_for_wasm); + return res; + } + return -1; +} + +int WAMRInstance::invoke_sock_recvfrom(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len) { + auto name = "__wasi_sock_recv_from"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi fopen function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } + } + + void *ri_data_ = nullptr; + uint32_t ri_data_for_wasm; + void *src_addr_ = nullptr; + uint32_t src_addr_for_wasm; + void *ro_data_len_ = nullptr; + uint32_t ro_data_len_for_wasm; + int res = -1; + + ri_data_for_wasm = + wasm_runtime_module_malloc(module_inst, sizeof(iovec_app_t), reinterpret_cast(&ri_data_)); + if (ri_data_for_wasm != 0) { + memcpy(ri_data_, ((void *)ri_data), sizeof(iovec_app_t)); // use native address for accessing in runtime + + src_addr_for_wasm = + wasm_runtime_module_malloc(module_inst, sizeof(__wasi_addr_t), reinterpret_cast(&src_addr_)); + if (src_addr_for_wasm != 0) { + memcpy(src_addr_, ((void *)src_addr), sizeof(__wasi_addr_t)); // use native address for accessing in runtime + + ro_data_len_for_wasm = + wasm_runtime_module_malloc(module_inst, sizeof(uint32), reinterpret_cast(&ro_data_len_)); + if (ro_data_len_for_wasm != 0) { + memcpy(ro_data_len_, ((void *)ro_data_len), + sizeof(uint32)); // use native address for accessing in runtime + uint32 argv[6] = {sock, ri_data_for_wasm, ri_data_len, + ri_flags, src_addr_for_wasm, ro_data_len_for_wasm}; + + wasm_runtime_call_wasm(exec_env, func, 6, argv); + res = argv[0]; + wasm_runtime_module_free(module_inst, ri_data_for_wasm); + } + wasm_runtime_module_free(module_inst, src_addr_for_wasm); + } + wasm_runtime_module_free(module_inst, ro_data_len_for_wasm); + return res; } return -1; +} +#endif +int WAMRInstance::invoke_fseek(uint32 fd, uint32 offset) { + auto name = "__wasi_fd_seek"; + if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { + LOGV(ERROR) << "The wasi fopen function is not found."; + auto target_module = get_module_instance()->e; + for (int i = 0; i < target_module->function_count; i++) { + auto cur_func = &target_module->functions[i]; + if (cur_func->is_import_func) { + LOGV(DEBUG) << cur_func->u.func_import->field_name; + if (!strcmp(cur_func->u.func_import->field_name, name)) { + + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + + } else { + LOGV(DEBUG) << cur_func->u.func->field_name; + + if (!strcmp(cur_func->u.func->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } + } + } + uint32 argv[2] = {fd, offset}; + return wasm_runtime_call_wasm(exec_env, func, 2, argv); }; int WAMRInstance::invoke_preopen(uint32 fd, const std::string &path) { auto name = "__wasilibc_register_preopened_fd"; if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { - LOGV(ERROR) << "The __wasilibc_register_preopened_fd function is not found."; + LOGV(ERROR) << "The wasi fopen function is not found."; auto target_module = get_module_instance()->e; for (int i = 0; i < target_module->function_count; i++) { auto cur_func = &target_module->functions[i]; if (cur_func->is_import_func) { LOGV(DEBUG) << cur_func->u.func_import->field_name; - if (!strcmp(cur_func->u.func_import->field_name, name)) + if (!strcmp(cur_func->u.func_import->field_name, name)) { + func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } + } else { LOGV(DEBUG) << cur_func->u.func->field_name; - if (!strcmp(cur_func->u.func->field_name, name)) + if (!strcmp(cur_func->u.func->field_name, name)) { func = ((WASMFunctionInstanceCommon *)cur_func); + break; + } } } } - char *buffer_ = nullptr; uint32_t buffer_for_wasm; @@ -140,7 +380,8 @@ int WAMRInstance::invoke_preopen(uint32 fd, const std::string &path) { strncpy(buffer_, path.c_str(), path.size()); // use native address for accessing in runtime argv[0] = fd; // pass the buffer_ address for WASM space argv[1] = buffer_for_wasm; // the size of buffer_ - auto res = wasm_runtime_call_wasm(exec_env, func, 2, argv); + wasm_runtime_call_wasm(exec_env, func, 2, argv); + int res = argv[0]; wasm_runtime_module_free(module_inst, buffer_for_wasm); return res; } @@ -238,7 +479,6 @@ void WAMRInstance::set_wasi_args(const std::vector &dir_list, const wasm_runtime_set_wasi_addr_pool(module, addr_.data(), addr_.size()); wasm_runtime_set_wasi_ns_lookup_pool(module, ns_pool_.data(), ns_pool_.size()); } -void restart_execution(uint32 id) {} void WAMRInstance::set_wasi_args(WAMRWASIContext &context) { auto get_addr_from_context = [](const WAMRWASIContext &wasiContext) { auto addr_pool = std::vector(wasiContext.addr_pool.size()); diff --git a/src/wamr_export.cpp b/src/wamr_export.cpp new file mode 100644 index 0000000..d43a4b4 --- /dev/null +++ b/src/wamr_export.cpp @@ -0,0 +1,195 @@ +// +// Created by victoryang00 on 10/19/23. +// + +#include "wamr.h" +#include "wamr_block_addr.h" +#include "wamr_wasi_context.h" +extern WAMRInstance *wamr; + +void insert_sock_open_data(uint32_t poolfd, int af, int socktype, uint32_t sockfd) { + SocketMetaData newSocketData{}; + newSocketData = wamr->socket_fd_map_[sockfd]; + + WasiSockOpenData openData{}; + openData.poolfd = poolfd; + openData.af = af; + openData.socktype = socktype; + openData.sockfd = sockfd; + + newSocketData.socketOpenData = openData; + wamr->socket_fd_map_[sockfd] = newSocketData; +} +#if !defined(__WINCRYPT_H__) +void insert_sock_send_to_data(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len) { + SocketMetaData newSocketData{}; + newSocketData = wamr->socket_fd_map_[sock]; + + WasiSockSendToData sendToData{}; + sendToData.sock = sock; + sendToData.si_data_len = si_data_len; + sendToData.si_flags = si_flags; + + sendToData.si_data.buf_offset = si_data->buf_offset; + sendToData.si_data.buf_len = si_data->buf_len; + + if(dest_addr->kind == IPv4){ + sendToData.dest_addr.ip.is_4 = true; + sendToData.dest_addr.ip.ip4[0] = dest_addr->addr.ip4.addr.n0; + sendToData.dest_addr.ip.ip4[1] = dest_addr->addr.ip4.addr.n1; + sendToData.dest_addr.ip.ip4[2] = dest_addr->addr.ip4.addr.n2; + sendToData.dest_addr.ip.ip4[3] = dest_addr->addr.ip4.addr.n3; + + sendToData.dest_addr.ip.is_4 = true; + sendToData.dest_addr.port = dest_addr->addr.ip4.port; + }else{ + sendToData.dest_addr.ip.is_4 = false; + sendToData.dest_addr.ip.ip6[0] = dest_addr->addr.ip6.addr.n0; + sendToData.dest_addr.ip.ip6[1] = dest_addr->addr.ip6.addr.n1; + sendToData.dest_addr.ip.ip6[2] = dest_addr->addr.ip6.addr.n2; + sendToData.dest_addr.ip.ip6[3] = dest_addr->addr.ip6.addr.n3; + sendToData.dest_addr.ip.ip6[4] = dest_addr->addr.ip6.addr.h0; + sendToData.dest_addr.ip.ip6[5] = dest_addr->addr.ip6.addr.h1; + sendToData.dest_addr.ip.ip6[6] = dest_addr->addr.ip6.addr.h2; + sendToData.dest_addr.ip.ip6[7] = dest_addr->addr.ip6.addr.h3; + + sendToData.dest_addr.ip.is_4 = false; + sendToData.dest_addr.port = dest_addr->addr.ip6.port; + } + + newSocketData.socketSentToData = sendToData; + + wamr->socket_fd_map_[sock] = newSocketData; +} + +void insert_sock_recv_from_data(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len) { + SocketMetaData newSocketData{}; + newSocketData = wamr->socket_fd_map_[sock]; + + WasiSockRecvFromData recvFromData{}; + recvFromData.sock = sock; + recvFromData.ri_data_len = ri_data_len; + recvFromData.ri_flags = ri_flags; + + recvFromData.ri_data.buf_offset = ri_data->buf_offset; + recvFromData.ri_data.buf_len = ri_data->buf_len; + + if(src_addr->kind == IPv4){ + recvFromData.src_addr.ip.is_4 = true; + recvFromData.src_addr.ip.ip4[0] = src_addr->addr.ip4.addr.n0; + recvFromData.src_addr.ip.ip4[1] = src_addr->addr.ip4.addr.n1; + recvFromData.src_addr.ip.ip4[2] = src_addr->addr.ip4.addr.n2; + recvFromData.src_addr.ip.ip4[3] = src_addr->addr.ip4.addr.n3; + + recvFromData.src_addr.ip.is_4 = true; + recvFromData.src_addr.port = src_addr->addr.ip4.port; + }else{ + recvFromData.src_addr.ip.is_4 = false; + recvFromData.src_addr.ip.ip6[0] = src_addr->addr.ip6.addr.n0; + recvFromData.src_addr.ip.ip6[1] = src_addr->addr.ip6.addr.n1; + recvFromData.src_addr.ip.ip6[2] = src_addr->addr.ip6.addr.n2; + recvFromData.src_addr.ip.ip6[3] = src_addr->addr.ip6.addr.n3; + recvFromData.src_addr.ip.ip6[4] = src_addr->addr.ip6.addr.h0; + recvFromData.src_addr.ip.ip6[5] = src_addr->addr.ip6.addr.h1; + recvFromData.src_addr.ip.ip6[6] = src_addr->addr.ip6.addr.h2; + recvFromData.src_addr.ip.ip6[7] = src_addr->addr.ip6.addr.h3; + + recvFromData.src_addr.ip.is_4 = false; + recvFromData.src_addr.port = src_addr->addr.ip6.port; + } + + newSocketData.socketRecvFromData = recvFromData; + wamr->socket_fd_map_[sock] = newSocketData; +} +#endif + +/**fopen, fseek*/ +void insert_fd(int fd, const char *path, int flags, int offset) { + if (fd > 2) { + printf("\n #insert_fd(fd,filename,flags, offset) %d %s %d %d \n\n", fd, path, flags, offset); + + if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) { + LOGV(ERROR) << "fd already exist" << fd; + if (offset == 0) { + // fOpen call + std::string curPath; + int curFlags; + int curOffset; + std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; + wamr->fd_map_[fd] = std::make_tuple(std::string(path), flags, curOffset); + } else { + // fSeek call + std::string curPath; + int curFlags; + int curOffset; + std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; + wamr->fd_map_[fd] = std::make_tuple(curPath, curFlags, offset); + } + + } else + wamr->fd_map_.insert(std::make_pair(fd, std::make_tuple(std::string(path), flags, offset))); + } +} +/* update fd->offset**/ +void insert_fd_fseek(); +/**fclose */ +void remove_fd(int fd) { + if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) + wamr->fd_map_.erase(fd); + else + LOGV(ERROR) << "fd not found" << fd; +} + +/* + create fd-socketmetadata map and store the "domain", "type", "protocol" value +*/ +void insert_socket(int fd, int domain, int type, int protocol) { + printf("\n #insert_socket(fd, domain, type, protocol) %d %d %d %d \n\n", fd, domain, type, protocol); + + if (wamr->socket_fd_map_.find(fd) != wamr->socket_fd_map_.end()) { + LOGV(ERROR) << "socket_fd already exist" << fd; + } else { + SocketMetaData metaData{}; + metaData.domain = domain; + metaData.type = type; + metaData.protocol = protocol; + wamr->socket_fd_map_.insert(std::make_pair(fd, metaData)); + } +} + +void update_socket_fd_address(int fd, SocketAddrPool *address) { + printf("\n #update_socket_fd_address(fd, address) %d \n\n", fd); + + if (wamr->socket_fd_map_.find(fd) == wamr->socket_fd_map_.end()) { + // note: ? fd here is not same as insert_socket? + // set default value + insert_socket(fd, 0, 0, 0); + } + + SocketMetaData metaData{}; + metaData.domain = wamr->socket_fd_map_[fd].domain; + metaData.type = wamr->socket_fd_map_[fd].type; + metaData.protocol = wamr->socket_fd_map_[fd].protocol; + + metaData.socketAddress.port = address->port; + if (address->is_4) { + metaData.socketAddress.is_4 = true; + metaData.socketAddress.ip4[0] = address->ip4[0]; + metaData.socketAddress.ip4[1] = address->ip4[1]; + metaData.socketAddress.ip4[2] = address->ip4[2]; + metaData.socketAddress.ip4[3] = address->ip4[3]; + + } else { + metaData.socketAddress.ip6[0] = address->ip6[0]; + metaData.socketAddress.ip6[1] = address->ip6[1]; + metaData.socketAddress.ip6[2] = address->ip6[2]; + metaData.socketAddress.ip6[3] = address->ip6[3]; + metaData.socketAddress.ip6[4] = address->ip6[4]; + metaData.socketAddress.ip6[5] = address->ip6[5]; + metaData.socketAddress.ip6[6] = address->ip6[6]; + metaData.socketAddress.ip6[7] = address->ip6[7]; + } + wamr->socket_fd_map_[fd] = metaData; +} diff --git a/src/wamr_wasi_context.cpp b/src/wamr_wasi_context.cpp index ade43c5..ae01d52 100644 --- a/src/wamr_wasi_context.cpp +++ b/src/wamr_wasi_context.cpp @@ -3,6 +3,7 @@ // #include "wamr_wasi_context.h" #include "wamr.h" +#include extern WAMRInstance *wamr; void WAMRWASIContext::dump_impl(WASIContext *env) { for (int i = 0; i < wamr->dir_.size(); i++) { @@ -12,46 +13,106 @@ void WAMRWASIContext::dump_impl(WASIContext *env) { map_dir.emplace_back(wamr->map_dir_[i]); } this->exit_code = env->exit_code; -} -void WAMRWASIContext::git checkout main(WASIContext *env) { -#if 0 - // Need to open the file and reinitialize the file descripter by map. - env->curfds->size = this->curfds.size; - env->curfds->used = this->curfds.used; - env->curfds->entries = (fd_entry *)malloc(sizeof(wasi_fd_entry) * this->curfds.size); - - auto i = 0; - auto entries = (wasi_fd_entry *)env->curfds->entries; - for (auto &&entry : this->curfds.entries) { - entries[i].object = (wasi_fd_object *)malloc(sizeof(wasi_fd_object)); - if (entry.number) { - entries[i].object->type = entry.type; - entries[i].object->number = entry.number; - // need to open to this number - } - if (!entry.dir.empty()) { - entries[i].object->directory.handle = opendir(entry.dir.c_str()); - entries[i].object->directory.offset = entry.offset; - } - entries[i].rights_base = entry.rights_base; - entries[i].rights_inheriting = entry.rights_inheriting; - i++; + for (auto [fd, res] : wamr->fd_map_) { + auto [path, flags, offset] = res; + auto dumped_res = std::make_tuple(path, flags, offset); + this->fd_map[fd] = dumped_res; } - env->prestats->size = this->prestats.size; - env->prestats->used = this->prestats.used; - i = 0; - env->prestats->prestats = (fd_prestat *)malloc(sizeof(wasi_fd_prestat) * this->prestats.size); - auto prestats_ = (wasi_fd_prestat *)env->prestats->prestats; - for (auto &&pre : this->prestats.prestats) { - if (!pre.dir.empty()) { - prestats_[i].dir = pre.dir.c_str(); - } - i++; + + for (auto [fd, socketMetaData] : wamr->socket_fd_map_) { + SocketMetaData socketMetaDataCopy = socketMetaData; + this->socket_fd_map[fd] = socketMetaDataCopy; } -#endif +} +void WAMRWASIContext::restore_impl(WASIContext *env) { + int r; + // std::string stdin_fd= "/dev/stdin"; + // wamr->invoke_fopen(stdin_fd, 0); + // std::string stdout_fd= "/dev/stdout"; + // wamr->invoke_fopen(stdout_fd, 0); + // std::string stderr_fd= "/dev/stderr"; + // wamr->invoke_fopen(stderr_fd, 0); for (auto [fd, res] : this->fd_map) { // differ from path from file - wamr->invoke_fopen(fd, std::get<0>(res),std::get<1>(res)); + LOGV(INFO) << "fd: " << fd << " path: " << std::get<0>(res) << " flags: " << std::get<1>(res) + << " offset: " << std::get<2>(res); + r = wamr->invoke_fopen(std::get<0>(res), std::get<1>(res)); + if (r != fd) + wamr->invoke_frenumber(r, fd); wamr->invoke_fseek(fd, std::get<2>(res)); } +#if !defined(__WINCRYPT_H__) + for (auto [fd, socketMetaData] : this->socket_fd_map) { + LOGV(INFO) << "fd: " << fd << " SocketMetaData[domain]: " << socketMetaData.domain + // << " SocketMetaData[socketAddress]: " << socketMetaData.socketAddress + << " SocketMetaData[protocol]: " << socketMetaData.protocol + << " SocketMetaData[type]: " << socketMetaData.type; + uint32 tmp_sock_fd = socketMetaData.socketOpenData.sockfd; + wamr->invoke_sock_open(socketMetaData.socketOpenData.poolfd, socketMetaData.socketOpenData.af, + socketMetaData.socketOpenData.socktype, &tmp_sock_fd); + // renumber or not? + if (tmp_sock_fd != fd) + wamr->invoke_frenumber(tmp_sock_fd, fd); + // if socket不一样 renumber + auto * tmp_addr = (__wasi_addr_t*)malloc(sizeof(__wasi_addr_t)); + tmp_addr->kind = socketMetaData.socketSentToData.dest_addr.ip.is_4?IPv4:IPv6; + if(tmp_addr->kind == IPv4) { + tmp_addr->addr.ip4.addr.n0 = socketMetaData.socketSentToData.dest_addr.ip.ip4[0]; + tmp_addr->addr.ip4.addr.n1 = socketMetaData.socketSentToData.dest_addr.ip.ip4[1]; + tmp_addr->addr.ip4.addr.n2 = socketMetaData.socketSentToData.dest_addr.ip.ip4[2]; + tmp_addr->addr.ip4.addr.n3 = socketMetaData.socketSentToData.dest_addr.ip.ip4[3]; + + tmp_addr->addr.ip4.port = socketMetaData.socketSentToData.dest_addr.port; + } else { + tmp_addr->addr.ip6.addr.n0 = socketMetaData.socketSentToData.dest_addr.ip.ip6[0]; + tmp_addr->addr.ip6.addr.n1 = socketMetaData.socketSentToData.dest_addr.ip.ip6[1]; + tmp_addr->addr.ip6.addr.n2 = socketMetaData.socketSentToData.dest_addr.ip.ip6[2]; + tmp_addr->addr.ip6.addr.n3 = socketMetaData.socketSentToData.dest_addr.ip.ip6[3]; + tmp_addr->addr.ip6.addr.h0 = socketMetaData.socketSentToData.dest_addr.ip.ip6[4]; + tmp_addr->addr.ip6.addr.h1 = socketMetaData.socketSentToData.dest_addr.ip.ip6[5]; + tmp_addr->addr.ip6.addr.h2 = socketMetaData.socketSentToData.dest_addr.ip.ip6[6]; + tmp_addr->addr.ip6.addr.h3 = socketMetaData.socketSentToData.dest_addr.ip.ip6[7]; + + tmp_addr->addr.ip6.port = socketMetaData.socketSentToData.dest_addr.port; + } + + uint32_t tmp_so_len = 0; + wamr->invoke_sock_sendto(socketMetaData.socketSentToData.sock, &socketMetaData.socketSentToData.si_data, + socketMetaData.socketSentToData.si_data_len, socketMetaData.socketSentToData.si_flags, + tmp_addr,//翻译 + &tmp_so_len ); + + + + //---------------------------------------------------------------------------------------------------------------- + + auto * tmp_addr_rev = (__wasi_addr_t*)malloc(sizeof(__wasi_addr_t)); + tmp_addr_rev->kind = socketMetaData.socketRecvFromData.src_addr.ip.is_4?IPv4:IPv6; + if(tmp_addr_rev->kind == IPv4) { + tmp_addr_rev->addr.ip4.addr.n0 = socketMetaData.socketRecvFromData.src_addr.ip.ip4[0]; + tmp_addr_rev->addr.ip4.addr.n1 = socketMetaData.socketRecvFromData.src_addr.ip.ip4[1]; + tmp_addr_rev->addr.ip4.addr.n2 = socketMetaData.socketRecvFromData.src_addr.ip.ip4[2]; + tmp_addr_rev->addr.ip4.addr.n3 = socketMetaData.socketRecvFromData.src_addr.ip.ip4[3]; + + tmp_addr_rev->addr.ip4.port = socketMetaData.socketRecvFromData.src_addr.port; + } else { + tmp_addr_rev->addr.ip6.addr.n0 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[0]; + tmp_addr_rev->addr.ip6.addr.n1 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[1]; + tmp_addr_rev->addr.ip6.addr.n2 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[2]; + tmp_addr_rev->addr.ip6.addr.n3 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[3]; + tmp_addr_rev->addr.ip6.addr.h0 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[4]; + tmp_addr_rev->addr.ip6.addr.h1 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[5]; + tmp_addr_rev->addr.ip6.addr.h2 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[6]; + tmp_addr_rev->addr.ip6.addr.h3 = socketMetaData.socketRecvFromData.src_addr.ip.ip6[7]; + + tmp_addr_rev->addr.ip6.port = socketMetaData.socketRecvFromData.src_addr.port; + } + + uint32_t tmp_ro_data_len = 0; + wamr->invoke_sock_recvfrom(socketMetaData.socketRecvFromData.sock, &socketMetaData.socketRecvFromData.ri_data, + socketMetaData.socketRecvFromData.ri_data_len, socketMetaData.socketRecvFromData.ri_flags, + tmp_addr_rev, + &tmp_ro_data_len); + } +#endif }; \ No newline at end of file diff --git a/src/wamr_wasi_nn_context.cpp b/src/wamr_wasi_nn_context.cpp new file mode 100644 index 0000000..51628c3 --- /dev/null +++ b/src/wamr_wasi_nn_context.cpp @@ -0,0 +1,10 @@ +// +// Created by victoryang00 on 10/25/2023. +// +#if WASM_ENABLE_WASI_NN != 0 +#include "wamr_wasi_nn_context.h" +#include "wamr.h" +extern WAMRInstance *wamr; +void WAMRWASINNContext::dump_impl(WASINNContext *env) {} +void WAMRWASINNContext::restore_impl(WASINNContext *env) {} +#endif \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 15ce21d..a9faf07 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,7 +1,8 @@ set(WAMR_DIR ${PROJECT_SOURCE_DIR}/lib/wasm-micro-runtime/) +set(HOST_PREFIX ${WASI_SDK_DIR}/bin/) enable_testing() function(wamr_app input) - add_custom_target(${input}_wamr ALL COMMAND ${WASI_SDK_DIR}/bin/clang -Wno-implicit-function-declaration -Wno-int-conversion --target=wasm32-unknown-wasi -g -pthread -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt --sysroot=${WASI_SDK_DIR}/share/wasi-sysroot ${CMAKE_CURRENT_SOURCE_DIR}/${input}.c -o ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) + add_custom_target(${input}_wamr ALL COMMAND ${HOST_PREFIX}clang -Wno-implicit-function-declaration -Wno-int-conversion --target=wasm32-unknown-wasi -g -pthread -Wl,--export=malloc -Wl,--export=free -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt --sysroot=${HOST_PREFIX}/../share/wasi-sysroot ${CMAKE_CURRENT_SOURCE_DIR}/${input}.c -o ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) add_test(NAME ${input}_checkpoint COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../MVVM_checkpoint --target ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) add_test(NAME ${input}_restore COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../MVVM_restore --target ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) endfunction() @@ -12,7 +13,7 @@ wamr_app(read-file) wamr_app(gups) function(wamr_socket_app input) - add_custom_target(${input}_wamr ALL COMMAND ${WASI_SDK_DIR}/bin/clang --target=wasm32-unknown-wasi -g -pthread -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt --sysroot=${WASI_SDK_DIR}/share/wasi-sysroot ${WAMR_DIR}/samples/socket-api/wasm-app-prefix/src/wasm-app-build/libsocket_wasi_ext.a -I${WAMR_DIR}/core/iwasm/libraries/lib-socket/inc/ ${CMAKE_CURRENT_SOURCE_DIR}/${input}.c -o ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) + add_custom_target(${input}_wamr ALL COMMAND ${HOST_PREFIX}clang --target=wasm32-unknown-wasi -g -pthread -Wl,--export=malloc -Wl,--export=free -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt --sysroot=${HOST_PREFIX}/../share/wasi-sysroot ${WAMR_DIR}/samples/socket-api/wasm-app-prefix/src/wasm-app-build/libsocket_wasi_ext.a -I${WAMR_DIR}/core/iwasm/libraries/lib-socket/inc/ ${CMAKE_CURRENT_SOURCE_DIR}/${input}.c -o ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) add_test(NAME ${input}_checkpoint COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../MVVM_checkpoint --target ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) add_test(NAME ${input}_restore COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../MVVM_restore --target ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm) endfunction() diff --git a/test/read-file.c b/test/read-file.c index fd966a2..41f49b1 100644 --- a/test/read-file.c +++ b/test/read-file.c @@ -9,11 +9,16 @@ #include // #include +FILE *a_file = NULL; + FILE *fopen_test(const char *restrict filename, const char *restrict mode) { FILE *f; int fd; int flags; + if(!a_file){ + a_file = fopen("./test.txt","a"); + } /* Check for valid initial mode character */ if (!strchr("rwa", *mode)) { @@ -28,7 +33,7 @@ FILE *fopen_test(const char *restrict filename, const char *restrict mode) #else // WASI libc ignores the mode parameter anyway, so skip the varargs. fd = __wasilibc_open_nomode(filename, flags); - printf("\n fopen_test(fd,filename,flags) %d %s %d \n\n",fd,filename,flags); + fprintf(a_file,"\n fopen_test(fd,filename,flags) %d %s %d \n\n",fd,filename,flags); #endif if (fd < 0) return 0; #ifdef __wasilibc_unmodified_upstream // WASI has no syscall @@ -65,6 +70,8 @@ int main() { size_t len2 = strlen(line2); fwrite(line1, sizeof(char), len1, file3); + //ntwritten + // checkpoint: lib_wasi_wrapper fwrite system record for( int i =0;i<10000;i++){ c++; @@ -75,5 +82,5 @@ int main() { fclose(file1); fclose(file2); fclose(file3); + __wasi_fd_renumber(3,10); } -