Skip to content

MAISTRA-2648: WASM fix for s390x #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/proxy-wasm/exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ template <typename Pairs> size_t pairsSize(const Pairs &result) {

template <typename Pairs> void marshalPairs(const Pairs &result, char *buffer) {
char *b = buffer;
*reinterpret_cast<uint32_t *>(b) = result.size();
bool reverse = "null" != contextOrEffectiveContext()->wasmVm()->runtime();
*reinterpret_cast<uint32_t *>(b) = reverse ? htowasm(result.size()) : result.size();
b += sizeof(uint32_t);
for (auto &p : result) {
*reinterpret_cast<uint32_t *>(b) = p.first.size();
*reinterpret_cast<uint32_t *>(b) = reverse ? htowasm(p.first.size()) : p.first.size();
b += sizeof(uint32_t);
*reinterpret_cast<uint32_t *>(b) = p.second.size();
*reinterpret_cast<uint32_t *>(b) = reverse ? htowasm(p.second.size()) : p.second.size();
b += sizeof(uint32_t);
}
for (auto &p : result) {
Expand Down
10 changes: 10 additions & 0 deletions include/proxy-wasm/word.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ namespace proxy_wasm {

#include "proxy_wasm_common.h"

// Use byteswap functions only when compiling for big-endian platforms.
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define htowasm(x) __builtin_bswap32(x)
#define wasmtoh(x) __builtin_bswap32(x)
#else
#define htowasm(x) (x)
#define wasmtoh(x) (x)
#endif

// Represents a Wasm-native word-sized datum. On 32-bit VMs, the high bits are always zero.
// The Wasm/VM API treats all bits as significant.
struct Word {
Expand Down
17 changes: 13 additions & 4 deletions src/exports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@ Pairs toPairs(std::string_view buffer) {
if (buffer.size() < sizeof(uint32_t)) {
return {};
}
auto size = *reinterpret_cast<const uint32_t *>(b);
bool reverse = "null" != contextOrEffectiveContext()->wasmVm()->runtime();
auto size = reverse ? wasmtoh(*reinterpret_cast<const uint32_t *>(b))
: *reinterpret_cast<const uint32_t *>(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<const uint32_t *>(b));
result[i].first =
std::string_view(nullptr, reverse ? wasmtoh(*reinterpret_cast<const uint32_t *>(b))
: *reinterpret_cast<const uint32_t *>(b));
b += sizeof(uint32_t);
result[i].second = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
result[i].second =
std::string_view(nullptr, reverse ? wasmtoh(*reinterpret_cast<const uint32_t *>(b))
: *reinterpret_cast<const uint32_t *>(b));
b += sizeof(uint32_t);
}
for (auto &p : result) {
Expand Down Expand Up @@ -652,6 +658,7 @@ Word grpc_send(Word token, Word message_ptr, Word message_size, Word end_stream)
// logs.
Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
auto context = contextOrEffectiveContext();
bool reverse = "null" != context->wasmVm()->runtime();

// Read syscall args.
uint64_t log_level;
Expand All @@ -675,7 +682,9 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
}
const uint32_t *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
if (iovec[1] /* buf_len */) {
memslice = context->wasmVm()->getMemory(iovec[0] /* buf */, 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
}
Expand Down
14 changes: 9 additions & 5 deletions src/signature_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess

uint32_t alg_id;
std::memcpy(&alg_id, payload.data(), sizeof(uint32_t));
alg_id = wasmtoh(alg_id);

if (alg_id != 2) {
message = "Signature has a wrong alg_id (want: 2, is: " + std::to_string(alg_id) + ")";
return false;
}

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

SHA512_CTX ctx;
SHA512_Init(&ctx);
Expand All @@ -105,8 +106,10 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
static const auto ed25519_pubkey = hex2pubkey<32>(PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY);

bool retval = true;
EVP_MD_CTX* mctx(EVP_MD_CTX_new());
EVP_PKEY* key(EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, static_cast<const unsigned char*>(ed25519_pubkey.data()), ed25519_pubkey.size()));
EVP_MD_CTX *mctx(EVP_MD_CTX_new());
EVP_PKEY *key(EVP_PKEY_new_raw_public_key(
EVP_PKEY_ED25519, NULL, static_cast<const unsigned char *>(ed25519_pubkey.data()),
ed25519_pubkey.size()));

if (key == nullptr) {
message = "Failed to load ed25519 public key";
Expand All @@ -116,15 +119,16 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
message = "Failed to initialize ed25519 digest verify";
retval = false;
}
if (retval && !EVP_DigestVerify(mctx, signature, sig_len, hash, sizeof(hash))) {
if (retval && !EVP_DigestVerify(mctx, signature, sig_len, hash, sizeof(hash))) {
message = "Signature mismatch";
retval = false;
}

EVP_PKEY_free(key);
EVP_MD_CTX_free(mctx);

if (retval) message = "Wasm signature OK (Ed25519)";
if (retval)
message = "Wasm signature OK (Ed25519)";
return retval;

#endif
Expand Down
4 changes: 2 additions & 2 deletions src/v8/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ bool V8::getWord(uint64_t pointer, Word *word) {
}
uint32_t word32;
::memcpy(&word32, memory_->data() + pointer, size);
word->u64_ = word32;
word->u64_ = wasmtoh(word32);
return true;
}

Expand All @@ -441,7 +441,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 = htowasm(word.u32());
::memcpy(memory_->data() + pointer, &word32, size);
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/wamr/wamr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ bool Wamr::getWord(uint64_t pointer, Word *word) {

uint32_t word32;
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
word->u64_ = word32;
word->u64_ = wasmtoh(word32);
return true;
}

Expand All @@ -348,7 +348,7 @@ bool Wamr::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 = htowasm(word.u32());
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/wasmtime/wasmtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,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_ = wasmtoh(word32);
return true;
}

Expand All @@ -363,7 +363,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 = htowasm(word.u32());
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/wavm/wavm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,12 @@ bool Wavm::getWord(uint64_t pointer, Word *data) {
auto p = reinterpret_cast<char *>(memory_base_ + pointer);
uint32_t data32;
memcpy(&data32, p, sizeof(uint32_t));
data->u64_ = data32;
data->u64_ = wasmtoh(data32);
return true;
}

bool Wavm::setWord(uint64_t pointer, Word data) {
uint32_t data32 = data.u32();
uint32_t data32 = htowasm(data.u32());
return setMemory(pointer, sizeof(uint32_t), &data32);
}

Expand Down
2 changes: 1 addition & 1 deletion test/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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] = {htowasm(static_cast<uint32_t>(-1)), htowasm(200)};
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
ASSERT_TRUE(vm_->getWord(0x200, &word));
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));
Expand Down