From e4e0c4822b23770739024b0c4e5caad616da4008 Mon Sep 17 00:00:00 2001 From: Konstantin Maksimov Date: Thu, 22 Sep 2022 12:08:57 +0200 Subject: [PATCH] [maistra-2.3] OSSM-1956: WASM fix for s390x Fixes https://issues.redhat.com/browse/OSSM-1815 error in maistra-2.3 proxy-wasm fix: https://github.com/proxy-wasm/proxy-wasm-cpp-host/pull/282 maistra-2.3 issue: https://issues.redhat.com/browse/OSSM-1956 Signed-off-by: Konstantin Maksimov --- .../proxy-wasm-cpp-host-s390x-support.patch | 72 +++++++++++++++++++ bazel/repositories.bzl | 9 ++- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 bazel/external/proxy-wasm-cpp-host-s390x-support.patch diff --git a/bazel/external/proxy-wasm-cpp-host-s390x-support.patch b/bazel/external/proxy-wasm-cpp-host-s390x-support.patch new file mode 100644 index 0000000000..4716908176 --- /dev/null +++ b/bazel/external/proxy-wasm-cpp-host-s390x-support.patch @@ -0,0 +1,72 @@ +diff --git a/include/proxy-wasm/exports.h b/include/proxy-wasm/exports.h +index 2b3d0db745..4f3efc3152 100644 +--- a/include/proxy-wasm/exports.h ++++ b/include/proxy-wasm/exports.h +@@ -74,12 +74,13 @@ template size_t pairsSize(const Pairs &result) { + + template void marshalPairs(const Pairs &result, char *buffer) { + char *b = buffer; +- *reinterpret_cast(b) = htowasm(result.size()); ++ bool reverse = "null" != contextOrEffectiveContext()->wasmVm()->getEngineName(); ++ *reinterpret_cast(b) = reverse ? htowasm(result.size()) : result.size(); + b += sizeof(uint32_t); + for (auto &p : result) { +- *reinterpret_cast(b) = htowasm(p.first.size()); ++ *reinterpret_cast(b) = reverse ? htowasm(p.first.size()) : p.first.size(); + b += sizeof(uint32_t); +- *reinterpret_cast(b) = htowasm(p.second.size()); ++ *reinterpret_cast(b) = reverse ? htowasm(p.second.size()) : p.second.size(); + b += sizeof(uint32_t); + } + for (auto &p : result) { +diff --git a/src/exports.cc b/src/exports.cc +index c203946b8b..d7a59bc903 100644 +--- a/src/exports.cc ++++ b/src/exports.cc +@@ -65,16 +65,22 @@ Pairs toPairs(std::string_view buffer) { + if (buffer.size() < sizeof(uint32_t)) { + return {}; + } +- auto size = wasmtoh(*reinterpret_cast(b)); ++ bool reverse = "null" != contextOrEffectiveContext()->wasmVm()->getEngineName(); ++ auto size = reverse ? wasmtoh(*reinterpret_cast(b)) ++ : *reinterpret_cast(b); + b += sizeof(uint32_t); + if (sizeof(uint32_t) + size * 2 * sizeof(uint32_t) > buffer.size()) { + return {}; + } + result.resize(size); + for (uint32_t i = 0; i < size; i++) { +- result[i].first = std::string_view(nullptr, wasmtoh(*reinterpret_cast(b))); ++ result[i].first = ++ std::string_view(nullptr, reverse ? wasmtoh(*reinterpret_cast(b)) ++ : *reinterpret_cast(b)); + b += sizeof(uint32_t); +- result[i].second = std::string_view(nullptr, wasmtoh(*reinterpret_cast(b))); ++ result[i].second = ++ std::string_view(nullptr, reverse ? wasmtoh(*reinterpret_cast(b)) ++ : *reinterpret_cast(b)); + b += sizeof(uint32_t); + } + for (auto &p : result) { +@@ -691,6 +697,7 @@ Word wasi_unstable_fd_prestat_dir_name(Word /*fd*/, Word /*path_ptr*/, Word /*pa + // logs. + Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) { + auto *context = contextOrEffectiveContext(); ++ bool reverse = "null" != context->wasmVm()->getEngineName(); + + // Read syscall args. + uint64_t log_level; +@@ -714,8 +721,9 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) { + } + const auto *iovec = reinterpret_cast(memslice.value().data()); + if (iovec[1] != 0U /* buf_len */) { +- memslice = context->wasmVm()->getMemory(wasmtoh(iovec[0]) /* buf */, +- wasmtoh(iovec[1]) /* buf_len */); ++ auto iovec0 = reverse ? wasmtoh(iovec[0]) : iovec[0]; ++ auto iovec1 = reverse ? wasmtoh(iovec[1]) : iovec[1]; ++ memslice = context->wasmVm()->getMemory(iovec0 /* buf */, iovec1 /* buf_len */); + if (!memslice) { + return 21; // __WASI_EFAULT + } + diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index 971f2e4218..9da015e092 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -1035,7 +1035,14 @@ def _proxy_wasm_cpp_sdk(): external_http_archive(name = "proxy_wasm_cpp_sdk") def _proxy_wasm_cpp_host(): - external_http_archive(name = "proxy_wasm_cpp_host") + external_http_archive( + name = "proxy_wasm_cpp_host", + # proxy-wasm-cpp-host-s390x-support.patch fixes WASM on s390x error https://issues.redhat.com/browse/OSSM-1815 + # maistra-2.3 issue: https://issues.redhat.com/browse/OSSM-1956 + # The permanent fix is: https://github.com/proxy-wasm/proxy-wasm-cpp-host/pull/282 + patches = ["@envoy//bazel/external:proxy-wasm-cpp-host-s390x-support.patch"], + patch_args = ["-p1"], + ) def _emsdk(): external_http_archive(name = "emsdk")