1
+ diff --git a/include/proxy-wasm/exports.h b/include/proxy-wasm/exports.h
2
+ index f0fa5dbf0..dde0ec63c 100644
3
+ --- a/include/proxy-wasm/exports.h
4
+ +++ b/include/proxy-wasm/exports.h
5
+ @@ -39,12 +39,12 @@ template <typename Pairs> size_t pairsSize(const Pairs &result) {
6
+
7
+ template <typename Pairs> void marshalPairs(const Pairs &result, char *buffer) {
8
+ char *b = buffer;
9
+ - *reinterpret_cast<uint32_t *>(b) = result.size();
10
+ + *reinterpret_cast<uint32_t *>(b) = htole32(result.size());
11
+ b += sizeof(uint32_t);
12
+ for (auto &p : result) {
13
+ - *reinterpret_cast<uint32_t *>(b) = p.first.size();
14
+ + *reinterpret_cast<uint32_t *>(b) = htole32(p.first.size());
15
+ b += sizeof(uint32_t);
16
+ - *reinterpret_cast<uint32_t *>(b) = p.second.size();
17
+ + *reinterpret_cast<uint32_t *>(b) = htole32(p.second.size());
18
+ b += sizeof(uint32_t);
19
+ }
20
+ for (auto &p : result) {
21
+ diff --git a/include/proxy-wasm/word.h b/include/proxy-wasm/word.h
22
+ index e96fdfb94..549968342 100644
23
+ --- a/include/proxy-wasm/word.h
24
+ +++ b/include/proxy-wasm/word.h
25
+ @@ -17,6 +17,11 @@
26
+
27
+ #include <iostream>
28
+
29
+ + #ifdef __APPLE__
30
+ + #define htole32(x) (x)
31
+ + #define le32toh(x) (x)
32
+ + #endif
33
+ +
34
+ namespace proxy_wasm {
35
+
36
+ #include "proxy_wasm_common.h"
37
+ diff --git a/src/exports.cc b/src/exports.cc
38
+ index 1ffee3228..7772c9368 100644
39
+ --- a/src/exports.cc
40
+ +++ b/src/exports.cc
41
+ @@ -48,16 +48,16 @@ Pairs toPairs(std::string_view buffer) {
42
+ if (buffer.size() < sizeof(uint32_t)) {
43
+ return {};
44
+ }
45
+ - auto size = *reinterpret_cast<const uint32_t *>(b);
46
+ + auto size = le32toh(*reinterpret_cast<const uint32_t *>(b));
47
+ b += sizeof(uint32_t);
48
+ if (sizeof(uint32_t) + size * 2 * sizeof(uint32_t) > buffer.size()) {
49
+ return {};
50
+ }
51
+ result.resize(size);
52
+ for (uint32_t i = 0; i < size; i++) {
53
+ - result[i].first = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
54
+ + result[i].first = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
55
+ b += sizeof(uint32_t);
56
+ - result[i].second = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
57
+ + result[i].second = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
58
+ b += sizeof(uint32_t);
59
+ }
60
+ for (auto &p : result) {
61
+ @@ -685,7 +685,8 @@ Word writevImpl(void *raw_context, Word fd, Word iovs, Word iovs_len, Word *nwri
62
+ }
63
+ const uint32_t *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
64
+ if (iovec[1] /* buf_len */) {
65
+ - memslice = context->wasmVm()->getMemory(iovec[0] /* buf */, iovec[1] /* buf_len */);
66
+ + memslice = context->wasmVm()->getMemory(le32toh(iovec[0]) /* buf */,
67
+ + le32toh(iovec[1]) /* buf_len */);
68
+ if (!memslice) {
69
+ return 21; // __WASI_EFAULT
70
+ }
71
+ diff --git a/src/v8/v8.cc b/src/v8/v8.cc
72
+ index 9078999cd..2e20f07d4 100644
73
+ --- a/src/v8/v8.cc
74
+ +++ b/src/v8/v8.cc
75
+ @@ -616,7 +616,7 @@ bool V8::getWord(uint64_t pointer, Word *word) {
76
+ }
77
+ uint32_t word32;
78
+ ::memcpy(&word32, memory_->data() + pointer, size);
79
+ - word->u64_ = word32;
80
+ + word->u64_ = le32toh(word32);
81
+ return true;
82
+ }
83
+
84
+ @@ -625,7 +625,7 @@ bool V8::setWord(uint64_t pointer, Word word) {
85
+ if (pointer + size > memory_->data_size()) {
86
+ return false;
87
+ }
88
+ - uint32_t word32 = word.u32();
89
+ + uint32_t word32 = htole32(word.u32());
90
+ ::memcpy(memory_->data() + pointer, &word32, size);
91
+ return true;
92
+ }
93
+ diff --git a/src/wasmtime/wasmtime.cc b/src/wasmtime/wasmtime.cc
94
+ index b7d648b6e..19929efd7 100644
95
+ --- a/src/wasmtime/wasmtime.cc
96
+ +++ b/src/wasmtime/wasmtime.cc
97
+ @@ -461,7 +461,7 @@ bool Wasmtime::getWord(uint64_t pointer, Word *word) {
98
+
99
+ uint32_t word32;
100
+ ::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
101
+ - word->u64_ = word32;
102
+ + word->u64_ = le32toh(word32);
103
+ return true;
104
+ }
105
+
106
+ @@ -470,7 +470,7 @@ bool Wasmtime::setWord(uint64_t pointer, Word word) {
107
+ if (pointer + size > wasm_memory_data_size(memory_.get())) {
108
+ return false;
109
+ }
110
+ - uint32_t word32 = word.u32();
111
+ + uint32_t word32 = htole32(word.u32());
112
+ ::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
113
+ return true;
114
+ }
115
+ diff --git a/src/wavm/wavm.cc b/src/wavm/wavm.cc
116
+ index 00b3b7358..c8762699e 100644
117
+ --- a/src/wavm/wavm.cc
118
+ +++ b/src/wavm/wavm.cc
119
+ @@ -390,12 +390,12 @@ bool Wavm::getWord(uint64_t pointer, Word *data) {
120
+ auto p = reinterpret_cast<char *>(memory_base_ + pointer);
121
+ uint32_t data32;
122
+ memcpy(&data32, p, sizeof(uint32_t));
123
+ - data->u64_ = data32;
124
+ + data->u64_ = le32toh(data32);
125
+ return true;
126
+ }
127
+
128
+ bool Wavm::setWord(uint64_t pointer, Word data) {
129
+ - uint32_t data32 = data.u32();
130
+ + uint32_t data32 = htole32(data.u32());
131
+ return setMemory(pointer, sizeof(uint32_t), &data32);
132
+ }
133
+
134
+ diff --git a/test/runtime_test.cc b/test/runtime_test.cc
135
+ index 8f5c524a1..3aaa036cd 100644
136
+ --- a/test/runtime_test.cc
137
+ +++ b/test/runtime_test.cc
138
+ @@ -154,7 +154,7 @@ TEST_P(TestVM, Memory) {
139
+ ASSERT_TRUE(vm_->getWord(0x2000, &word));
140
+ ASSERT_EQ(100, word.u64_);
141
+
142
+ - int32_t data[2] = {-1, 200};
143
+ + uint32_t data[2] = {htole32(static_cast<uint32_t>(-1)), htole32(200)};
144
+ ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
145
+ ASSERT_TRUE(vm_->getWord(0x200, &word));
146
+ ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));
0 commit comments