This is an automated email from the ASF dual-hosted git repository. cmcfarlen pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit ce27946effff4a326226cd2e4060359a0eda92f1 Author: Kit Chan <[email protected]> AuthorDate: Thu May 8 10:44:17 2025 -0700 Fix compilation problem with wasm plugin + Update the use of proxy-wasm library (#12222) * Fix compilation problem with wasm plugin * Fix format error * Fix format error * Fix format error in cmake files * Fix engine requirement problem * Remove wamr fro fedora build for now * Fix fedora compile requirements * Turn off wamr engine support for experimental plugin * Fix typo * Fix another missing line * Enable wamr * Disable wamr because the fedora has an older version * wasmtime and wamr cannot be used together (cherry picked from commit 8206ab363b325b1255511a4cb058ccaf5af8bfbf) --- CMakePresets.json | 7 ++- cmake/ExperimentalPlugins.cmake | 22 +++++++++- cmake/Findwamr.cmake | 2 +- cmake/{Findwamr.cmake => Findwasmedge.cmake} | 32 +++++++------- cmake/{Findwamr.cmake => Findwasmtime.cmake} | 32 +++++++------- doc/admin-guide/plugins/wasm.en.rst | 21 ++++----- plugins/experimental/CMakeLists.txt | 5 ++- plugins/experimental/wasm/CMakeLists.txt | 23 +++++++++- plugins/experimental/wasm/lib/CMakeLists.txt | 10 +++++ .../wasm/lib/README.proxy-wasm-cpp-host.md | 4 ++ .../wasm/lib/include/proxy-wasm/context.h | 4 +- .../lib/include/proxy-wasm/context_interface.h | 5 +-- .../wasm/lib/include/proxy-wasm/exports.h | 10 +++-- .../wasm/lib/include/proxy-wasm/pairs_util.h | 1 + .../experimental/wasm/lib/include/proxy-wasm/sdk.h | 50 ++++++++++++++++++++++ .../wasm/lib/include/proxy-wasm/vm_id_handle.h | 4 +- .../wasm/lib/include/proxy-wasm/wasm.h | 3 +- .../wasm/lib/include/proxy-wasm/wasm_api_impl.h | 4 +- .../wasm/lib/include/proxy-wasm/wasm_vm.h | 4 +- .../wasm/lib/include/proxy-wasm/word.h | 4 +- plugins/experimental/wasm/lib/src/exports.cc | 25 +++++++++++ plugins/experimental/wasm/lib/src/wamr/wamr.cc | 33 ++++++++++---- plugins/experimental/wasm/lib/src/wasm.cc | 5 ++- plugins/experimental/wasm/lib/src/wasmtime/types.h | 2 +- .../experimental/wasm/lib/src/wasmtime/wasmtime.cc | 6 +-- 25 files changed, 235 insertions(+), 83 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 218a465424..8a68e24b35 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -47,6 +47,7 @@ "CMAKE_COMPILE_WARNING_AS_ERROR": "OFF", "CMAKE_INSTALL_PREFIX": "/opt/ats", "BUILD_EXPERIMENTAL_PLUGINS": "ON", + "ENABLE_WASM_WAMR": "OFF", "ENABLE_VERIFY_PLUGINS": "OFF" } }, @@ -58,6 +59,7 @@ "ENABLE_AUTEST": "ON", "CMAKE_INSTALL_PREFIX": "/tmp/ts-autest", "BUILD_EXPERIMENTAL_PLUGINS": "ON", + "ENABLE_WASM_WAMR": "OFF", "ENABLE_EXAMPLE": "ON" } }, @@ -106,6 +108,7 @@ "CMAKE_COMPILE_WARNING_AS_ERROR": "ON", "ENABLE_CCACHE": "ON", "BUILD_EXPERIMENTAL_PLUGINS": "ON", + "ENABLE_WASM_WAMR": "OFF", "ENABLE_EXAMPLE": "ON", "CMAKE_INSTALL_PREFIX": "/tmp/ats" } @@ -210,7 +213,8 @@ "binaryDir": "${sourceDir}/build-${presetName}", "cacheVariables": { "CMAKE_INSTALL_PREFIX": "/tmp/ats", - "BUILD_EXPERIMENTAL_PLUGINS": "ON" + "BUILD_EXPERIMENTAL_PLUGINS": "ON", + "ENABLE_WASM_WAMR": "OFF" } }, { @@ -391,6 +395,7 @@ "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "BUILD_EXPERIMENTAL_PLUGINS": "ON", + "ENABLE_WASM_WAMR": "OFF", "ENABLE_EXAMPLE": "ON", "ENABLE_CCACHE": "OFF" } diff --git a/cmake/ExperimentalPlugins.cmake b/cmake/ExperimentalPlugins.cmake index 754dfe926a..2fa1524c77 100644 --- a/cmake/ExperimentalPlugins.cmake +++ b/cmake/ExperimentalPlugins.cmake @@ -115,13 +115,31 @@ auto_option( ) auto_option(URL_SIG FEATURE_VAR BUILD_URL_SIG DEFAULT ${_DEFAULT}) auto_option( - WASM + WASM_WAMR FEATURE_VAR - BUILD_WASM + BUILD_WASM_WAMR PACKAGE_DEPENDS wamr DEFAULT ${_DEFAULT} ) +auto_option( + WASM_WASMTIME + FEATURE_VAR + BUILD_WASM_WASMTIME + PACKAGE_DEPENDS + wasmtime + DEFAULT + ${_DEFAULT} +) +auto_option( + WASM_WASMEDGE + FEATURE_VAR + BUILD_WASM_WASMEDGE + PACKAGE_DEPENDS + wasmedge + DEFAULT + ${_DEFAULT} +) unset(_DEFAULT) diff --git a/cmake/Findwamr.cmake b/cmake/Findwamr.cmake index e74b2f023f..4c599d9980 100644 --- a/cmake/Findwamr.cmake +++ b/cmake/Findwamr.cmake @@ -29,7 +29,7 @@ # find_library(iwasm_LIBRARY NAMES iwasm) -find_path(wamr_INCLUDE_DIR NAMES wasm_export.h) +find_path(wamr_INCLUDE_DIR NAMES wasm_c_api.h) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(wamr REQUIRED_VARS iwasm_LIBRARY wamr_INCLUDE_DIR) diff --git a/cmake/Findwamr.cmake b/cmake/Findwasmedge.cmake similarity index 56% copy from cmake/Findwamr.cmake copy to cmake/Findwasmedge.cmake index e74b2f023f..11df6a54f2 100644 --- a/cmake/Findwamr.cmake +++ b/cmake/Findwasmedge.cmake @@ -15,33 +15,33 @@ # ####################### -# Findwamr.cmake +# Findwasmedge.cmake # # This will define the following variables # -# wamr_FOUND -# wamr_LIBRARY -# wamr_INCLUDE_DIR +# wasmedge_FOUND +# wasmedge_LIBRARY +# wasmedge_INCLUDE_DIR # # and the following imported targets # -# wamr::wamr +# wasmedge::wasmedge # -find_library(iwasm_LIBRARY NAMES iwasm) -find_path(wamr_INCLUDE_DIR NAMES wasm_export.h) +find_library(lwasmedge_LIBRARY NAMES wasmedge) +find_path(wasmedge_INCLUDE_DIR NAMES wasmedge/wasmedge.h) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(wamr REQUIRED_VARS iwasm_LIBRARY wamr_INCLUDE_DIR) +find_package_handle_standard_args(wasmedge REQUIRED_VARS lwasmedge_LIBRARY wasmedge_INCLUDE_DIR) -if(wamr_FOUND) - mark_as_advanced(wamr_FOUND wamr_LIBRARY) - set(wamr_INCLUDE_DIRS ${wamr_INCLUDE_DIR}) - set(wamr_LIBRARY ${iwasm_LIBRARY}) +if(wasmedge_FOUND) + mark_as_advanced(wasmedge_FOUND wasmedge_LIBRARY) + set(wasmedge_INCLUDE_DIRS ${wasmedge_INCLUDE_DIR}) + set(wasmedge_LIBRARY ${lwasmedge_LIBRARY}) endif() -if(wamr_FOUND AND NOT TARGET wamr::wamr) - add_library(wamr::wamr INTERFACE IMPORTED) - target_include_directories(wamr::wamr INTERFACE ${wamr_INCLUDE_DIRS}) - target_link_libraries(wamr::wamr INTERFACE ${wamr_LIBRARY}) +if(wasmedge_FOUND AND NOT TARGET wasmedge::wasmedge) + add_library(wasmedge::wasmedge INTERFACE IMPORTED) + target_include_directories(wasmedge::wasmedge INTERFACE ${wasmedge_INCLUDE_DIRS}) + target_link_libraries(wasmedge::wasmedge INTERFACE ${wasmedge_LIBRARY}) endif() diff --git a/cmake/Findwamr.cmake b/cmake/Findwasmtime.cmake similarity index 56% copy from cmake/Findwamr.cmake copy to cmake/Findwasmtime.cmake index e74b2f023f..4685f916cb 100644 --- a/cmake/Findwamr.cmake +++ b/cmake/Findwasmtime.cmake @@ -15,33 +15,33 @@ # ####################### -# Findwamr.cmake +# Findwasmtime.cmake # # This will define the following variables # -# wamr_FOUND -# wamr_LIBRARY -# wamr_INCLUDE_DIR +# wasmtime_FOUND +# wasmtime_LIBRARY +# wasmtime_INCLUDE_DIR # # and the following imported targets # -# wamr::wamr +# wasmtime::wasmtime # -find_library(iwasm_LIBRARY NAMES iwasm) -find_path(wamr_INCLUDE_DIR NAMES wasm_export.h) +find_library(lwasmtime_LIBRARY NAMES wasmtime) +find_path(wasmtime_INCLUDE_DIR NAMES crates/c-api/include/wasm.h) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(wamr REQUIRED_VARS iwasm_LIBRARY wamr_INCLUDE_DIR) +find_package_handle_standard_args(wasmtime REQUIRED_VARS lwasmtime_LIBRARY wasmtime_INCLUDE_DIR) -if(wamr_FOUND) - mark_as_advanced(wamr_FOUND wamr_LIBRARY) - set(wamr_INCLUDE_DIRS ${wamr_INCLUDE_DIR}) - set(wamr_LIBRARY ${iwasm_LIBRARY}) +if(wasmtime_FOUND) + mark_as_advanced(wasmtime_FOUND wasmtime_LIBRARY) + set(wasmtime_INCLUDE_DIRS ${wasmtime_INCLUDE_DIR}) + set(wasmtime_LIBRARY ${lwasmtime_LIBRARY}) endif() -if(wamr_FOUND AND NOT TARGET wamr::wamr) - add_library(wamr::wamr INTERFACE IMPORTED) - target_include_directories(wamr::wamr INTERFACE ${wamr_INCLUDE_DIRS}) - target_link_libraries(wamr::wamr INTERFACE ${wamr_LIBRARY}) +if(wasmtime_FOUND AND NOT TARGET wasmtime::wasmtime) + add_library(wasmtime::wasmtime INTERFACE IMPORTED) + target_include_directories(wasmtime::wasmtime INTERFACE ${wasmtime_INCLUDE_DIRS}) + target_link_libraries(wasmtime::wasmtime INTERFACE ${wasmtime_LIBRARY}) endif() diff --git a/doc/admin-guide/plugins/wasm.en.rst b/doc/admin-guide/plugins/wasm.en.rst index 0e58777285..459318490e 100644 --- a/doc/admin-guide/plugins/wasm.en.rst +++ b/doc/admin-guide/plugins/wasm.en.rst @@ -39,11 +39,11 @@ How it Works The plugin uses the library and header files from the Proxy-Wasm project. -* https://github.com/proxy-wasm/proxy-wasm-cpp-host/tree/b7e690703c7f26707438a2f1ebd7c197bc8f0296 +* https://github.com/proxy-wasm/proxy-wasm-cpp-host/tree/c4d7bb0fda912e24c64daf2aa749ec54cec99412 * https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/tree/fd0be8405db25de0264bdb78fae3a82668c03782 -Proxy-Wasm in turn uses an underlying WebAssembly runtime to execute the WebAssembly module. (Currently only WAMR and -WasmEdge are supported) +Proxy-Wasm in turn uses an underlying WebAssembly runtime to execute the WebAssembly module. (Currently only WAMR, +Wasmtime and WasmEdge are supported) The plugin creates a root context when ATS starts and a new context will be created out of the root context for each transaction. ATS plugin events will trigger the corresponding functions in the WebAssembly module to be executed through @@ -61,9 +61,9 @@ Compiling the Plugin :: - wget https://github.com/bytecodealliance/wasm-micro-runtime/archive/refs/tags/WAMR-1.2.1.tar.gz - tar zxvf WAMR-1.2.1.tar.gz - cd wasm-micro-runtime-WAMR-1.2.1 + wget https://github.com/bytecodealliance/wasm-micro-runtime/archive/refs/tags/WAMR-2.1.1.tar.gz + tar zxvf WAMR-2.1.1.tar.gz + cd wasm-micro-runtime-WAMR-2.1.1 cp core/iwasm/include/* /usr/local/include/ cd product-mini/platforms/linux mkdir build @@ -78,16 +78,13 @@ Compiling the Plugin git clone https://github.com/bytecodealliance/wasmtime.git cd wasmtime/ - git checkout release-9.0.0 + git checkout release-24.0.0 git submodule update --init cargo build cargo build --release --manifest-path crates/c-api/Cargo.toml sudo cp target/release/libwasmtime.so /usr/local/lib - wget https://github.com/WebAssembly/wasm-c-api/archive/c9d31284651b975f05ac27cee0bab1377560b87e.tar.gz - tar zxvf c9d31284651b975f05ac27cee0bab1377560b87e.tar.gz - cd wasm-c-api-c9d31284651b975f05ac27cee0bab1377560b87e/ - sudo mkdir /usr/local/include/include/ - sudo cp include/wasm.h /usr/local/include/include/ + sudo mkdir -p /usr/local/include/crates/c-api/include/ + sudo cp crates/c-api/include/wasm.h /usr/local/include/crates/c-api/include/ **Install WasmEdge** diff --git a/plugins/experimental/CMakeLists.txt b/plugins/experimental/CMakeLists.txt index bec349b33d..38b01049b5 100644 --- a/plugins/experimental/CMakeLists.txt +++ b/plugins/experimental/CMakeLists.txt @@ -116,6 +116,9 @@ endif() if(BUILD_URL_SIG) add_subdirectory(url_sig) endif() -if(BUILD_WASM) +if(BUILD_WASM_WAMR + OR BUILD_WASM_WASMTIME + OR BUILD_WASM_WASMEDGE +) add_subdirectory(wasm) endif() diff --git a/plugins/experimental/wasm/CMakeLists.txt b/plugins/experimental/wasm/CMakeLists.txt index d5ae90cbd6..0cccbc2082 100644 --- a/plugins/experimental/wasm/CMakeLists.txt +++ b/plugins/experimental/wasm/CMakeLists.txt @@ -18,5 +18,26 @@ add_subdirectory(lib) add_atsplugin(wasm ats_context.cc ats_wasm.cc wasm_main.cc) -target_link_libraries(wasm PRIVATE wasmlib wamr::wamr) + +set(WASM_RUNTIME wasmlib) +if(wamr_FOUND) + list(APPEND WASM_RUNTIME wamr::wamr) +elseif(wasmtime_FOUND) + list(APPEND WASM_RUNTIME wasmtime::wasmtime) +endif() +if(wasmedge_FOUND) + list(APPEND WASM_RUNTIME wasmedge::wasmedge) +endif() + +target_link_libraries(wasm PRIVATE ${WASM_RUNTIME}) + +if(wamr_FOUND) + target_compile_options(wasm PRIVATE -DWAMR) +elseif(wasmtime_FOUND) + target_compile_options(wasm PRIVATE -DWASMTIME) +endif() +if(wasmedge_FOUND) + target_compile_options(wasm PRIVATE -DWASMEDGE) +endif() + verify_global_plugin(wasm) diff --git a/plugins/experimental/wasm/lib/CMakeLists.txt b/plugins/experimental/wasm/lib/CMakeLists.txt index be158adcad..f838461d7b 100644 --- a/plugins/experimental/wasm/lib/CMakeLists.txt +++ b/plugins/experimental/wasm/lib/CMakeLists.txt @@ -29,6 +29,11 @@ set(CC_FILES ) if(wamr_FOUND) list(APPEND CC_FILES src/wamr/wamr.cc) +elseif(wasmtime_FOUND) + list(APPEND CC_FILES src/wasmtime/wasmtime.cc) +endif() +if(wasmedge_FOUND) + list(APPEND CC_FILES src/wasmedge/wasmedge.cc) endif() add_library(wasmlib STATIC ${CC_FILES}) @@ -36,6 +41,11 @@ target_compile_options(wasmlib PUBLIC -Wno-unused-parameter) if(wamr_FOUND) target_compile_options(wasmlib PRIVATE -Wno-missing-field-initializers) target_link_libraries(wasmlib PUBLIC wamr::wamr) +elseif(wasmtime_FOUND) + target_link_libraries(wasmlib PUBLIC wasmtime::wasmtime) +endif() +if(wasmedge_FOUND) + target_link_libraries(wasmlib PUBLIC wasmedge::wasmedge) endif() target_include_directories(wasmlib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/plugins/experimental/wasm/lib/README.proxy-wasm-cpp-host.md b/plugins/experimental/wasm/lib/README.proxy-wasm-cpp-host.md index 8cb5694ee8..bd198e7236 100644 --- a/plugins/experimental/wasm/lib/README.proxy-wasm-cpp-host.md +++ b/plugins/experimental/wasm/lib/README.proxy-wasm-cpp-host.md @@ -1 +1,5 @@ # WebAssembly for Proxies (C++ host implementation) + +## How to Contribute + +See [CONTRIBUTING](CONTRIBUTING.md). diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/context.h b/plugins/experimental/wasm/lib/include/proxy-wasm/context.h index ab99cad70a..12937041f4 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/context.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/context.h @@ -26,13 +26,11 @@ #include <string_view> #include <vector> +#include "include/proxy-wasm/sdk.h" #include "include/proxy-wasm/context_interface.h" namespace proxy_wasm { -#include "proxy_wasm_common.h" -#include "proxy_wasm_enums.h" - class PluginHandleBase; class WasmBase; class WasmVm; diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/context_interface.h b/plugins/experimental/wasm/lib/include/proxy-wasm/context_interface.h index 81973eed2b..48fce76d9e 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/context_interface.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/context_interface.h @@ -25,10 +25,9 @@ #include <memory> #include <vector> -namespace proxy_wasm { +#include "include/proxy-wasm/sdk.h" -#include "proxy_wasm_common.h" -#include "proxy_wasm_enums.h" +namespace proxy_wasm { using Pairs = std::vector<std::pair<std::string_view, std::string_view>>; using PairsWithStringValues = std::vector<std::pair<std::string_view, std::string>>; diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/exports.h b/plugins/experimental/wasm/lib/include/proxy-wasm/exports.h index 376a4d3b60..91c51150c4 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/exports.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/exports.h @@ -139,10 +139,13 @@ Word wasi_unstable_fd_read(Word, Word, Word, Word); Word wasi_unstable_fd_seek(Word, int64_t, Word, Word); Word wasi_unstable_fd_close(Word); Word wasi_unstable_fd_fdstat_get(Word fd, Word statOut); +Word wasi_unstable_fd_fdstat_set_flags(Word fd, Word flags); Word wasi_unstable_environ_get(Word, Word); Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr); Word wasi_unstable_args_get(Word argc_ptr, Word argv_buf_size_ptr); Word wasi_unstable_args_sizes_get(Word argc_ptr, Word argv_buf_size_ptr); +Word wasi_unstable_sched_yield(); +Word wasi_unstable_poll_oneoff(Word in, Word out, Word nsubscriptions, Word nevents); void wasi_unstable_proc_exit(Word); Word wasi_unstable_clock_time_get(Word, uint64_t, Word); Word wasi_unstable_random_get(Word, Word); @@ -170,9 +173,10 @@ void emscripten_notify_memory_growth(Word); _f(continue_stream) _f(close_stream) _f(get_log_level) #define FOR_ALL_WASI_FUNCTIONS(_f) \ - _f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(environ_get) \ - _f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) _f(random_get) \ - _f(proc_exit) _f(path_open) _f(fd_prestat_get) _f(fd_prestat_dir_name) + _f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(fd_fdstat_set_flags) \ + _f(environ_get) _f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) \ + _f(random_get) _f(sched_yield) _f(poll_oneoff) _f(proc_exit) _f(path_open) \ + _f(fd_prestat_get) _f(fd_prestat_dir_name) // Helpers to generate a stub to pass to VM, in place of a restricted proxy-wasm capability. #define _CREATE_PROXY_WASM_STUB(_fn) \ diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/pairs_util.h b/plugins/experimental/wasm/lib/include/proxy-wasm/pairs_util.h index 54df2fd886..019c970bae 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/pairs_util.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/pairs_util.h @@ -15,6 +15,7 @@ #pragma once +#include <cstddef> #include <string> #include <string_view> #include <vector> diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/sdk.h b/plugins/experimental/wasm/lib/include/proxy-wasm/sdk.h new file mode 100644 index 0000000000..1058543731 --- /dev/null +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/sdk.h @@ -0,0 +1,50 @@ +// Copyright 2016-2019 Envoy Project Authors +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include <string> +#include <cstdint> + +namespace proxy_wasm { + +namespace internal { + +// isolate those includes to prevent ::proxy_wasm namespace pollution with std +// namespace definitions. +#include "proxy_wasm_common.h" +#include "proxy_wasm_enums.h" + +} // namespace internal + +// proxy_wasm_common.h +using WasmResult = internal::WasmResult; +using WasmHeaderMapType = internal::WasmHeaderMapType; +using WasmBufferType = internal::WasmBufferType; +using WasmBufferFlags = internal::WasmBufferFlags; +using WasmStreamType = internal::WasmStreamType; + +// proxy_wasm_enums.h +using LogLevel = internal::LogLevel; +using FilterStatus = internal::FilterStatus; +using FilterHeadersStatus = internal::FilterHeadersStatus; +using FilterMetadataStatus = internal::FilterMetadataStatus; +using FilterTrailersStatus = internal::FilterTrailersStatus; +using FilterDataStatus = internal::FilterDataStatus; +using GrpcStatus = internal::GrpcStatus; +using MetricType = internal::MetricType; +using CloseType = internal::CloseType; + +} // namespace proxy_wasm diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/vm_id_handle.h b/plugins/experimental/wasm/lib/include/proxy-wasm/vm_id_handle.h index 547eca229e..16ebb4bf03 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/vm_id_handle.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/vm_id_handle.h @@ -15,8 +15,10 @@ #pragma once #include <functional> -#include <mutex> #include <memory> +#include <mutex> +#include <string> +#include <string_view> #include <unordered_map> namespace proxy_wasm { diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/wasm.h b/plugins/experimental/wasm/lib/include/proxy-wasm/wasm.h index 1a785a8f9a..9fa2bda1f1 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/wasm.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/wasm.h @@ -24,6 +24,7 @@ #include <unordered_map> #include <unordered_set> +#include "include/proxy-wasm/sdk.h" #include "include/proxy-wasm/context.h" #include "include/proxy-wasm/exports.h" #include "include/proxy-wasm/wasm_vm.h" @@ -31,8 +32,6 @@ namespace proxy_wasm { -#include "proxy_wasm_common.h" - class ContextBase; class WasmHandleBase; diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_api_impl.h b/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_api_impl.h index 8bd9626ff7..899af7e415 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_api_impl.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_api_impl.h @@ -30,15 +30,13 @@ #include <utility> #include <vector> +#include "include/proxy-wasm/sdk.h" #include "include/proxy-wasm/exports.h" #include "include/proxy-wasm/word.h" namespace proxy_wasm { namespace null_plugin { -#include "proxy_wasm_enums.h" -#include "proxy_wasm_common.h" - #define WS(_x) Word(static_cast<uint64_t>(_x)) #define WR(_x) Word(reinterpret_cast<uint64_t>(_x)) diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_vm.h b/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_vm.h index acf0ccf3bc..a573212e00 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_vm.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/wasm_vm.h @@ -19,16 +19,16 @@ #include <memory> #include <optional> #include <string> +#include <string_view> #include <unordered_map> #include <unordered_set> #include <vector> +#include "include/proxy-wasm/sdk.h" #include "include/proxy-wasm/word.h" namespace proxy_wasm { -#include "proxy_wasm_enums.h" - class ContextBase; // These are templates and its helper for constructing signatures of functions calling into Wasm diff --git a/plugins/experimental/wasm/lib/include/proxy-wasm/word.h b/plugins/experimental/wasm/lib/include/proxy-wasm/word.h index 90ea932df2..bc0d23a8cb 100644 --- a/plugins/experimental/wasm/lib/include/proxy-wasm/word.h +++ b/plugins/experimental/wasm/lib/include/proxy-wasm/word.h @@ -17,9 +17,9 @@ #include <iostream> -namespace proxy_wasm { +#include "include/proxy-wasm/sdk.h" -#include "proxy_wasm_common.h" +namespace proxy_wasm { // Use byteswap functions only when compiling for big-endian platforms. #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ diff --git a/plugins/experimental/wasm/lib/src/exports.cc b/plugins/experimental/wasm/lib/src/exports.cc index 0290dcf0ff..25ca06c9d4 100644 --- a/plugins/experimental/wasm/lib/src/exports.cc +++ b/plugins/experimental/wasm/lib/src/exports.cc @@ -646,6 +646,9 @@ Word grpc_send(Word token, Word message_ptr, Word message_size, Word end_stream) return context->grpcSend(token, message.value(), end_stream != 0U); } +// WASIp1 typings in comments sourced from +// https://github.com/WebAssembly/wasi-libc/blob/446cb3f1aa21f9b1a1eab372f82d65d19003e924/libc-bottom-half/headers/public/wasi/api.h + // __wasi_errno_t path_open(__wasi_fd_t fd, __wasi_lookupflags_t dirflags, const char *path, // size_t path_len, __wasi_oflags_t oflags, __wasi_rights_t fs_rights_base, __wasi_rights_t // fs_rights_inheriting, __wasi_fdflags_t fdflags, __wasi_fd_t *retptr0) @@ -776,6 +779,13 @@ Word wasi_unstable_fd_fdstat_get(Word fd, Word statOut) { return 0; // __WASI_ESUCCESS } +// __wasi_errno_t __wasi_fd_fdstat_set_flags(__wasi_fd_t fd, __wasi_fdflags_t flags) +Word wasi_unstable_fd_fdstat_set_flags(Word /*fd*/, Word /*flags*/) { + // Flags that can be specified: append, dsync, nonblock, rsync, and sync. Proxy-wasm only supports + // STDOUT and STDERR, but none of these flags have any effect in Proxy-Wasm. + return 52; // __WASI_ERRNO_ENOSYS +} + // __wasi_errno_t __wasi_environ_get(char **environ, char *environ_buf); Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) { auto *context = contextOrEffectiveContext(); @@ -879,6 +889,21 @@ Word wasi_unstable_random_get(Word result_buf_ptr, Word buf_len) { return 0; // __WASI_ESUCCESS } +// __wasi_errno_t __wasi_sched_yield() +Word wasi_unstable_sched_yield() { + // Per POSIX man pages, it is valid to return success if the calling thread is the only thread in + // the highest priority list. This is vacuously true for wasm without threads. There are no valid + // error cases defined. + return 0; // __WASI_ESUCCESS +} + +// __wasi_errno_t __wasi_poll_oneoff(const __wasi_subscription_t *in, __wasi_event_t *out, +// __wasi_size_t nsubscriptions, __wasi_size_t *nevents) +Word wasi_unstable_poll_oneoff(Word /*in*/, Word /*out*/, Word /*nsubscriptions*/, + Word /*nevents_ptr*/) { + return 52; // __WASI_ERRNO_ENOSYS +} + // void __wasi_proc_exit(__wasi_exitcode_t rval); void wasi_unstable_proc_exit(Word /*exit_code*/) { auto *context = contextOrEffectiveContext(); diff --git a/plugins/experimental/wasm/lib/src/wamr/wamr.cc b/plugins/experimental/wasm/lib/src/wamr/wamr.cc index ea3d140e7d..482a59bf1b 100644 --- a/plugins/experimental/wasm/lib/src/wamr/wamr.cc +++ b/plugins/experimental/wasm/lib/src/wamr/wamr.cc @@ -56,7 +56,8 @@ public: Wamr() = default; std::string_view getEngineName() override { return "wamr"; } - std::string_view getPrecompiledSectionName() override { return ""; } + // must use the exact name given by test-tools/append-aot-to-wasm/append_aot_to_wasm.py + std::string_view getPrecompiledSectionName() override { return "wamr-aot"; } Cloneable cloneable() override { return Cloneable::CompiledBytecode; } std::unique_ptr<WasmVm> clone() override; @@ -118,18 +119,32 @@ private: std::unordered_map<std::string, WasmFuncPtr> module_functions_; }; -bool Wamr::load(std::string_view bytecode, std::string_view /*precompiled*/, +bool Wamr::load(std::string_view bytecode, std::string_view precompiled, const std::unordered_map<uint32_t, std::string> & /*function_names*/) { store_ = wasm_store_new(engine()); if (store_ == nullptr) { return false; } - wasm_byte_vec_t binary = {.size = bytecode.size(), - .data = (char *)bytecode.data(), - .num_elems = bytecode.size(), - .size_of_elem = sizeof(byte_t), - .lock = nullptr}; + wasm_byte_vec_t binary = {0}; + if (precompiled.empty()) { + binary.size = bytecode.size(); + binary.data = const_cast<char *>(bytecode.data()); + binary.num_elems = bytecode.size(); + binary.size_of_elem = sizeof(byte_t); + binary.lock = nullptr; + } else { + // skip leading paddings + auto padding_count = static_cast<uint8_t>(precompiled[0]); + precompiled.remove_prefix(padding_count + 1); + + binary.size = precompiled.size(); + binary.data = const_cast<char *>(precompiled.data()); + binary.num_elems = precompiled.size(); + binary.size_of_elem = sizeof(byte_t); + binary.lock = nullptr; + } + module_ = wasm_module_new(store_.get(), &binary); if (module_ == nullptr) { return false; @@ -224,8 +239,8 @@ static const char *printValKind(wasm_valkind_t kind) { return "f32"; case WASM_F64: return "f64"; - case WASM_ANYREF: - return "anyref"; + case WASM_EXTERNREF: + return "externref"; case WASM_FUNCREF: return "funcref"; default: diff --git a/plugins/experimental/wasm/lib/src/wasm.cc b/plugins/experimental/wasm/lib/src/wasm.cc index cb1dd9b3ac..e8a7ce4366 100644 --- a/plugins/experimental/wasm/lib/src/wasm.cc +++ b/plugins/experimental/wasm/lib/src/wasm.cc @@ -394,7 +394,10 @@ void WasmBase::startVm(ContextBase *root_context) { // time "wasi_unstable.clock_time_get", "wasi_snapshot_preview1.clock_time_get", // random - "wasi_unstable.random_get", "wasi_snapshot_preview1.random_get"}); + "wasi_unstable.random_get", "wasi_snapshot_preview1.random_get", + // Go runtime initialization + "wasi_unstable.fd_fdstat_get", "wasi_snapshot_preview1.fd_fdstat_get", + "wasi_unstable.fd_fdstat_set_flags", "wasi_snapshot_preview1.fd_fdstat_set_flags"}); if (_initialize_) { // WASI reactor. _initialize_(root_context); diff --git a/plugins/experimental/wasm/lib/src/wasmtime/types.h b/plugins/experimental/wasm/lib/src/wasmtime/types.h index 4ab725c04f..14fe750530 100644 --- a/plugins/experimental/wasm/lib/src/wasmtime/types.h +++ b/plugins/experimental/wasm/lib/src/wasmtime/types.h @@ -13,7 +13,7 @@ // limitations under the License. #include "src/common/types.h" -#include "include/wasm.h" +#include "crates/c-api/include/wasm.h" namespace proxy_wasm::wasmtime { diff --git a/plugins/experimental/wasm/lib/src/wasmtime/wasmtime.cc b/plugins/experimental/wasm/lib/src/wasmtime/wasmtime.cc index 2cfc21888f..c4a7646f0e 100644 --- a/plugins/experimental/wasm/lib/src/wasmtime/wasmtime.cc +++ b/plugins/experimental/wasm/lib/src/wasmtime/wasmtime.cc @@ -26,7 +26,7 @@ #include "src/wasmtime/types.h" -#include "include/wasm.h" +#include "crates/c-api/include/wasm.h" namespace proxy_wasm { namespace wasmtime { @@ -215,8 +215,8 @@ static const char *printValKind(wasm_valkind_t kind) { return "f32"; case WASM_F64: return "f64"; - case WASM_ANYREF: - return "anyref"; + case WASM_EXTERNREF: + return "externref"; case WASM_FUNCREF: return "funcref"; default:
