From 151e595e3c39ac5bb42fc2ff1eaf7fc7db8cbe21 Mon Sep 17 00:00:00 2001 From: Konstantin Maksimov Date: Tue, 22 Feb 2022 17:36:05 +0100 Subject: [PATCH] MAISTRA-2648: WASM fix for s390x Fixes https://issues.redhat.com/browse/MAISTRA-2648 The permanent fix is not a part of maistra/envoy yet: proxy-wasm/proxy-wasm-cpp-host#198 Signed-off-by: Konstantin Maksimov --- bazel/external/proxy_wasm_s390x.patch | 146 ++++++++++++++++++++++++++ bazel/repositories.bzl | 4 + 2 files changed, 150 insertions(+) create mode 100644 bazel/external/proxy_wasm_s390x.patch diff --git a/bazel/external/proxy_wasm_s390x.patch b/bazel/external/proxy_wasm_s390x.patch new file mode 100644 index 0000000000..824a89806b --- /dev/null +++ b/bazel/external/proxy_wasm_s390x.patch @@ -0,0 +1,146 @@ +diff --git a/include/proxy-wasm/exports.h b/include/proxy-wasm/exports.h +index f0fa5dbf0..dde0ec63c 100644 +--- a/include/proxy-wasm/exports.h ++++ b/include/proxy-wasm/exports.h +@@ -39,12 +39,12 @@ template size_t pairsSize(const Pairs &result) { + + template void marshalPairs(const Pairs &result, char *buffer) { + char *b = buffer; +- *reinterpret_cast(b) = result.size(); ++ *reinterpret_cast(b) = htole32(result.size()); + b += sizeof(uint32_t); + for (auto &p : result) { +- *reinterpret_cast(b) = p.first.size(); ++ *reinterpret_cast(b) = htole32(p.first.size()); + b += sizeof(uint32_t); +- *reinterpret_cast(b) = p.second.size(); ++ *reinterpret_cast(b) = htole32(p.second.size()); + b += sizeof(uint32_t); + } + for (auto &p : result) { +diff --git a/include/proxy-wasm/word.h b/include/proxy-wasm/word.h +index e96fdfb94..549968342 100644 +--- a/include/proxy-wasm/word.h ++++ b/include/proxy-wasm/word.h +@@ -17,6 +17,11 @@ + + #include + ++#ifdef __APPLE__ ++#define htole32(x) (x) ++#define le32toh(x) (x) ++#endif ++ + namespace proxy_wasm { + + #include "proxy_wasm_common.h" +diff --git a/src/exports.cc b/src/exports.cc +index 1ffee3228..7772c9368 100644 +--- a/src/exports.cc ++++ b/src/exports.cc +@@ -48,16 +48,16 @@ Pairs toPairs(std::string_view buffer) { + if (buffer.size() < sizeof(uint32_t)) { + return {}; + } +- auto size = *reinterpret_cast(b); ++ auto size = le32toh(*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, *reinterpret_cast(b)); ++ result[i].first = std::string_view(nullptr, le32toh(*reinterpret_cast(b))); + b += sizeof(uint32_t); +- result[i].second = std::string_view(nullptr, *reinterpret_cast(b)); ++ result[i].second = std::string_view(nullptr, le32toh(*reinterpret_cast(b))); + b += sizeof(uint32_t); + } + for (auto &p : result) { +@@ -685,7 +685,8 @@ Word writevImpl(void *raw_context, Word fd, Word iovs, Word iovs_len, Word *nwri + } + const uint32_t *iovec = reinterpret_cast(memslice.value().data()); + if (iovec[1] /* buf_len */) { +- memslice = context->wasmVm()->getMemory(iovec[0] /* buf */, iovec[1] /* buf_len */); ++ memslice = context->wasmVm()->getMemory(le32toh(iovec[0]) /* buf */, ++ le32toh(iovec[1]) /* buf_len */); + if (!memslice) { + return 21; // __WASI_EFAULT + } +diff --git a/src/v8/v8.cc b/src/v8/v8.cc +index 9078999cd..2e20f07d4 100644 +--- a/src/v8/v8.cc ++++ b/src/v8/v8.cc +@@ -616,7 +616,7 @@ bool V8::getWord(uint64_t pointer, Word *word) { + } + uint32_t word32; + ::memcpy(&word32, memory_->data() + pointer, size); +- word->u64_ = word32; ++ word->u64_ = le32toh(word32); + return true; + } + +@@ -625,7 +625,7 @@ bool V8::setWord(uint64_t pointer, Word word) { + if (pointer + size > memory_->data_size()) { + return false; + } +- uint32_t word32 = word.u32(); ++ uint32_t word32 = htole32(word.u32()); + ::memcpy(memory_->data() + pointer, &word32, size); + return true; + } +diff --git a/src/wasmtime/wasmtime.cc b/src/wasmtime/wasmtime.cc +index b7d648b6e..19929efd7 100644 +--- a/src/wasmtime/wasmtime.cc ++++ b/src/wasmtime/wasmtime.cc +@@ -461,7 +461,7 @@ bool Wasmtime::getWord(uint64_t pointer, Word *word) { + + uint32_t word32; + ::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size); +- word->u64_ = word32; ++ word->u64_ = le32toh(word32); + return true; + } + +@@ -470,7 +470,7 @@ bool Wasmtime::setWord(uint64_t pointer, Word word) { + if (pointer + size > wasm_memory_data_size(memory_.get())) { + return false; + } +- uint32_t word32 = word.u32(); ++ uint32_t word32 = htole32(word.u32()); + ::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size); + return true; + } +diff --git a/src/wavm/wavm.cc b/src/wavm/wavm.cc +index 00b3b7358..c8762699e 100644 +--- a/src/wavm/wavm.cc ++++ b/src/wavm/wavm.cc +@@ -390,12 +390,12 @@ bool Wavm::getWord(uint64_t pointer, Word *data) { + auto p = reinterpret_cast(memory_base_ + pointer); + uint32_t data32; + memcpy(&data32, p, sizeof(uint32_t)); +- data->u64_ = data32; ++ data->u64_ = le32toh(data32); + return true; + } + + bool Wavm::setWord(uint64_t pointer, Word data) { +- uint32_t data32 = data.u32(); ++ uint32_t data32 = htole32(data.u32()); + return setMemory(pointer, sizeof(uint32_t), &data32); + } + +diff --git a/test/runtime_test.cc b/test/runtime_test.cc +index 8f5c524a1..3aaa036cd 100644 +--- a/test/runtime_test.cc ++++ b/test/runtime_test.cc +@@ -154,7 +154,7 @@ TEST_P(TestVM, Memory) { + ASSERT_TRUE(vm_->getWord(0x2000, &word)); + ASSERT_EQ(100, word.u64_); + +- int32_t data[2] = {-1, 200}; ++ uint32_t data[2] = {htole32(static_cast(-1)), htole32(200)}; + ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast(data))); + ASSERT_TRUE(vm_->getWord(0x200, &word)); + ASSERT_EQ(-1, static_cast(word.u64_)); \ No newline at end of file diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index 8f80751228..253e26d824 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -795,6 +795,10 @@ def _proxy_wasm_cpp_host(): external_http_archive( name = "proxy_wasm_cpp_host", build_file = "@envoy//bazel/external:proxy_wasm_cpp_host.BUILD", + # The patch fixes WASM on s390x https://issues.redhat.com/browse/MAISTRA-2648 + # The permanent fix is https://github.com/proxy-wasm/proxy-wasm-cpp-host/pull/198 + patches = ["@envoy//bazel/external:proxy_wasm_s390x.patch"], + patch_args = ["-p1"], ) def _emscripten_toolchain():