Skip to content

Commit b86cd99

Browse files
knm3000Otto van der Schaaf
authored andcommitted
MAISTRA-2648: WASM fix for s390x
Fixes WASM on s390x https://issues.redhat.com/browse/MAISTRA-2648 Corresponding proxy-wasm/proxy-wasm-cpp-host PRs: proxy-wasm#198 proxy-wasm#282
1 parent b5fded2 commit b86cd99

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

include/proxy-wasm/exports.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ template <typename Pairs> size_t pairsSize(const Pairs &result) {
7474

7575
template <typename Pairs> void marshalPairs(const Pairs &result, char *buffer) {
7676
char *b = buffer;
77-
*reinterpret_cast<uint32_t *>(b) = htowasm(result.size());
77+
bool reverse = "null" != contextOrEffectiveContext()->wasmVm()->runtime();
78+
*reinterpret_cast<uint32_t *>(b) = reverse ? htowasm(result.size()) : result.size();
7879
b += sizeof(uint32_t);
7980
for (auto &p : result) {
80-
*reinterpret_cast<uint32_t *>(b) = htowasm(p.first.size());
81+
*reinterpret_cast<uint32_t *>(b) = reverse ? htowasm(p.first.size()) : p.first.size();
8182
b += sizeof(uint32_t);
82-
*reinterpret_cast<uint32_t *>(b) = htowasm(p.second.size());
83+
*reinterpret_cast<uint32_t *>(b) = reverse ? htowasm(p.second.size()) : p.second.size();
8384
b += sizeof(uint32_t);
8485
}
8586
for (auto &p : result) {

src/exports.cc

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,22 @@ Pairs toPairs(std::string_view buffer) {
6565
if (buffer.size() < sizeof(uint32_t)) {
6666
return {};
6767
}
68-
auto size = wasmtoh(*reinterpret_cast<const uint32_t *>(b));
68+
bool reverse = "null" != contextOrEffectiveContext()->wasmVm()->runtime();
69+
auto size = reverse ? wasmtoh(*reinterpret_cast<const uint32_t *>(b))
70+
: *reinterpret_cast<const uint32_t *>(b);
6971
b += sizeof(uint32_t);
7072
if (sizeof(uint32_t) + size * 2 * sizeof(uint32_t) > buffer.size()) {
7173
return {};
7274
}
7375
result.resize(size);
7476
for (uint32_t i = 0; i < size; i++) {
75-
result[i].first = std::string_view(nullptr, wasmtoh(*reinterpret_cast<const uint32_t *>(b)));
77+
result[i].first =
78+
std::string_view(nullptr, reverse ? wasmtoh(*reinterpret_cast<const uint32_t *>(b))
79+
: *reinterpret_cast<const uint32_t *>(b));
7680
b += sizeof(uint32_t);
77-
result[i].second = std::string_view(nullptr, wasmtoh(*reinterpret_cast<const uint32_t *>(b)));
81+
result[i].second =
82+
std::string_view(nullptr, reverse ? wasmtoh(*reinterpret_cast<const uint32_t *>(b))
83+
: *reinterpret_cast<const uint32_t *>(b));
7884
b += sizeof(uint32_t);
7985
}
8086
for (auto &p : result) {
@@ -690,7 +696,8 @@ Word wasi_unstable_fd_prestat_dir_name(Word /*fd*/, Word /*path_ptr*/, Word /*pa
690696
// Implementation of writev-like() syscall that redirects stdout/stderr to Envoy
691697
// logs.
692698
Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
693-
auto *context = contextOrEffectiveContext();
699+
auto context = contextOrEffectiveContext();
700+
bool reverse = "null" != context->wasmVm()->runtime();
694701

695702
// Read syscall args.
696703
uint64_t log_level;
@@ -712,10 +719,11 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
712719
if (!memslice) {
713720
return 21; // __WASI_EFAULT
714721
}
715-
const auto *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
716-
if (iovec[1] != 0U /* buf_len */) {
717-
memslice = context->wasmVm()->getMemory(wasmtoh(iovec[0]) /* buf */,
718-
wasmtoh(iovec[1]) /* buf_len */);
722+
const uint32_t *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
723+
if (iovec[1] /* buf_len */) {
724+
auto iovec0 = reverse ? wasmtoh(iovec[0]) : iovec[0];
725+
auto iovec1 = reverse ? wasmtoh(iovec[1]) : iovec[1];
726+
memslice = context->wasmVm()->getMemory(iovec0 /* buf */, iovec1 /* buf_len */);
719727
if (!memslice) {
720728
return 21; // __WASI_EFAULT
721729
}

src/signature_util.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
9393
}
9494

9595
const auto *signature = reinterpret_cast<const uint8_t *>(payload.data()) + sizeof(uint32_t);
96-
const auto sig_len = payload.size() - sizeof(uint32_t);
96+
const auto sig_len = payload.size() - sizeof(uint32_t);
9797

9898
SHA512_CTX ctx;
9999
SHA512_Init(&ctx);
@@ -108,8 +108,10 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
108108
static const auto ed25519_pubkey = hex2pubkey<32>(PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY);
109109

110110
bool retval = true;
111-
EVP_MD_CTX* mctx(EVP_MD_CTX_new());
112-
EVP_PKEY* key(EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, static_cast<const unsigned char*>(ed25519_pubkey.data()), ed25519_pubkey.size()));
111+
EVP_MD_CTX *mctx(EVP_MD_CTX_new());
112+
EVP_PKEY *key(EVP_PKEY_new_raw_public_key(
113+
EVP_PKEY_ED25519, NULL, static_cast<const unsigned char *>(ed25519_pubkey.data()),
114+
ed25519_pubkey.size()));
113115

114116
if (key == nullptr) {
115117
message = "Failed to load ed25519 public key";
@@ -119,15 +121,16 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
119121
message = "Failed to initialize ed25519 digest verify";
120122
retval = false;
121123
}
122-
if (retval && !EVP_DigestVerify(mctx, signature, sig_len, hash, sizeof(hash))) {
124+
if (retval && !EVP_DigestVerify(mctx, signature, sig_len, hash, sizeof(hash))) {
123125
message = "Signature mismatch";
124126
retval = false;
125127
}
126128

127129
EVP_PKEY_free(key);
128130
EVP_MD_CTX_free(mctx);
129131

130-
if (retval) message = "Wasm signature OK (Ed25519)";
132+
if (retval)
133+
message = "Wasm signature OK (Ed25519)";
131134
return retval;
132135

133136
#endif

test/runtime_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ TEST_P(TestVm, StraceLogLevel) {
8181
EXPECT_TRUE(host->isTraceLogged("[host<-vm] run return: void"));
8282
}
8383

84+
TEST_P(TestVM, Memory) {
85+
auto source = readTestWasmFile("abi_export.wasm");
86+
ASSERT_TRUE(vm_->load(source, {}, {}));
87+
ASSERT_TRUE(vm_->link(""));
88+
89+
Word word;
90+
ASSERT_TRUE(vm_->setWord(0x2000, Word(100)));
91+
ASSERT_TRUE(vm_->getWord(0x2000, &word));
92+
ASSERT_EQ(100, word.u64_);
93+
94+
uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1)), htowasm(200)};
95+
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
96+
ASSERT_TRUE(vm_->getWord(0x200, &word));
97+
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));
98+
ASSERT_TRUE(vm_->getWord(0x204, &word));
99+
ASSERT_EQ(200, static_cast<int32_t>(word.u64_));
100+
}
101+
84102
TEST_P(TestVm, TerminateExecution) {
85103
// TODO(chaoqin-li1123): implement execution termination for other runtime.
86104
if (engine_ != "v8") {

0 commit comments

Comments
 (0)