Skip to content

Commit b402508

Browse files
authored
Add support for big-endian platforms. (#198)
Signed-off-by: Konstantin Maksimov <[email protected]>
1 parent 819dcc0 commit b402508

File tree

8 files changed

+22
-16
lines changed

8 files changed

+22
-16
lines changed

include/proxy-wasm/exports.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ 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) = result.size();
77+
*reinterpret_cast<uint32_t *>(b) = htole32(result.size());
7878
b += sizeof(uint32_t);
7979
for (auto &p : result) {
80-
*reinterpret_cast<uint32_t *>(b) = p.first.size();
80+
*reinterpret_cast<uint32_t *>(b) = htole32(p.first.size());
8181
b += sizeof(uint32_t);
82-
*reinterpret_cast<uint32_t *>(b) = p.second.size();
82+
*reinterpret_cast<uint32_t *>(b) = htole32(p.second.size());
8383
b += sizeof(uint32_t);
8484
}
8585
for (auto &p : result) {

include/proxy-wasm/word.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
#include <iostream>
1919

20+
#ifdef __APPLE__
21+
#define htole32(x) (x)
22+
#define le32toh(x) (x)
23+
#endif
24+
2025
namespace proxy_wasm {
2126

2227
#include "proxy_wasm_common.h"

src/exports.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ Pairs toPairs(std::string_view buffer) {
6464
if (buffer.size() < sizeof(uint32_t)) {
6565
return {};
6666
}
67-
auto size = *reinterpret_cast<const uint32_t *>(b);
67+
auto size = le32toh(*reinterpret_cast<const uint32_t *>(b));
6868
b += sizeof(uint32_t);
6969
if (sizeof(uint32_t) + size * 2 * sizeof(uint32_t) > buffer.size()) {
7070
return {};
7171
}
7272
result.resize(size);
7373
for (uint32_t i = 0; i < size; i++) {
74-
result[i].first = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
74+
result[i].first = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
7575
b += sizeof(uint32_t);
76-
result[i].second = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
76+
result[i].second = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
7777
b += sizeof(uint32_t);
7878
}
7979
for (auto &p : result) {
@@ -712,7 +712,8 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
712712
}
713713
const uint32_t *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
714714
if (iovec[1] /* buf_len */) {
715-
memslice = context->wasmVm()->getMemory(iovec[0] /* buf */, iovec[1] /* buf_len */);
715+
memslice = context->wasmVm()->getMemory(le32toh(iovec[0]) /* buf */,
716+
le32toh(iovec[1]) /* buf_len */);
716717
if (!memslice) {
717718
return 21; // __WASI_EFAULT
718719
}

src/v8/v8.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ bool V8::getWord(uint64_t pointer, Word *word) {
432432
}
433433
uint32_t word32;
434434
::memcpy(&word32, memory_->data() + pointer, size);
435-
word->u64_ = word32;
435+
word->u64_ = le32toh(word32);
436436
return true;
437437
}
438438

@@ -441,7 +441,7 @@ bool V8::setWord(uint64_t pointer, Word word) {
441441
if (pointer + size > memory_->data_size()) {
442442
return false;
443443
}
444-
uint32_t word32 = word.u32();
444+
uint32_t word32 = htole32(word.u32());
445445
::memcpy(memory_->data() + pointer, &word32, size);
446446
return true;
447447
}

src/wamr/wamr.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ bool Wamr::getWord(uint64_t pointer, Word *word) {
340340

341341
uint32_t word32;
342342
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
343-
word->u64_ = word32;
343+
word->u64_ = le32toh(word32);
344344
return true;
345345
}
346346

@@ -349,7 +349,7 @@ bool Wamr::setWord(uint64_t pointer, Word word) {
349349
if (pointer + size > wasm_memory_data_size(memory_.get())) {
350350
return false;
351351
}
352-
uint32_t word32 = word.u32();
352+
uint32_t word32 = htole32(word.u32());
353353
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
354354
return true;
355355
}

src/wasmtime/wasmtime.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ bool Wasmtime::getWord(uint64_t pointer, Word *word) {
354354

355355
uint32_t word32;
356356
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
357-
word->u64_ = word32;
357+
word->u64_ = le32toh(word32);
358358
return true;
359359
}
360360

@@ -363,7 +363,7 @@ bool Wasmtime::setWord(uint64_t pointer, Word word) {
363363
if (pointer + size > wasm_memory_data_size(memory_.get())) {
364364
return false;
365365
}
366-
uint32_t word32 = word.u32();
366+
uint32_t word32 = htole32(word.u32());
367367
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
368368
return true;
369369
}

src/wavm/wavm.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,12 @@ bool Wavm::getWord(uint64_t pointer, Word *data) {
344344
auto p = reinterpret_cast<char *>(memory_base_ + pointer);
345345
uint32_t data32;
346346
memcpy(&data32, p, sizeof(uint32_t));
347-
data->u64_ = data32;
347+
data->u64_ = le32toh(data32);
348348
return true;
349349
}
350350

351351
bool Wavm::setWord(uint64_t pointer, Word data) {
352-
uint32_t data32 = data.u32();
352+
uint32_t data32 = htole32(data.u32());
353353
return setMemory(pointer, sizeof(uint32_t), &data32);
354354
}
355355

test/runtime_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TEST_P(TestVM, Memory) {
5656
ASSERT_TRUE(vm_->getWord(0x2000, &word));
5757
ASSERT_EQ(100, word.u64_);
5858

59-
int32_t data[2] = {-1, 200};
59+
uint32_t data[2] = {htole32(static_cast<uint32_t>(-1)), htole32(200)};
6060
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
6161
ASSERT_TRUE(vm_->getWord(0x200, &word));
6262
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));

0 commit comments

Comments
 (0)