diff --git a/include/proxy-wasm/exports.h b/include/proxy-wasm/exports.h index 15305a2ad..7fe8a4c57 100644 --- a/include/proxy-wasm/exports.h +++ b/include/proxy-wasm/exports.h @@ -74,12 +74,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 673fd1fe9..3e4f2622f 100644 --- a/src/exports.cc +++ b/src/exports.cc @@ -64,16 +64,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) { @@ -712,7 +712,8 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) { } 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 29f3de090..057c364ca 100644 --- a/src/v8/v8.cc +++ b/src/v8/v8.cc @@ -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_ = le32toh(word32); return true; } @@ -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 = htole32(word.u32()); ::memcpy(memory_->data() + pointer, &word32, size); return true; } diff --git a/src/wamr/wamr.cc b/src/wamr/wamr.cc index 590de6823..de9cee494 100644 --- a/src/wamr/wamr.cc +++ b/src/wamr/wamr.cc @@ -340,7 +340,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_ = le32toh(word32); return true; } @@ -349,7 +349,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 = htole32(word.u32()); ::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size); return true; } diff --git a/src/wasmtime/wasmtime.cc b/src/wasmtime/wasmtime.cc index 8bef920a6..ef47f7d39 100644 --- a/src/wasmtime/wasmtime.cc +++ b/src/wasmtime/wasmtime.cc @@ -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_ = le32toh(word32); return true; } @@ -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 = 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 1b0d7383c..e7bbcf040 100644 --- a/src/wavm/wavm.cc +++ b/src/wavm/wavm.cc @@ -344,12 +344,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 7bfd39211..d741919a9 100644 --- a/test/runtime_test.cc +++ b/test/runtime_test.cc @@ -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] = {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_));