Skip to content

Commit c04bf3c

Browse files
committed
Fix for the crash on big endian machine. (#197)
Signed-off-by: Konstantin Maksimov <[email protected]>
1 parent bab2ef1 commit c04bf3c

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

include/proxy-wasm/wasm.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,20 @@ inline bool WasmBase::copyToPointerSize(std::string_view s, uint64_t ptr_ptr, ui
421421
}
422422
memcpy(p, s.data(), size);
423423
}
424+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
425+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
426+
if (!wasm_vm_->setWord(ptr_ptr, Word(__builtin_bswap32(pointer)))) {
427+
#else
424428
if (!wasm_vm_->setWord(ptr_ptr, Word(pointer))) {
429+
#endif
425430
return false;
426431
}
432+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
433+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
434+
if (!wasm_vm_->setWord(size_ptr, Word(__builtin_bswap32(size)))) {
435+
#else
427436
if (!wasm_vm_->setWord(size_ptr, Word(size))) {
437+
#endif
428438
return false;
429439
}
430440
return true;

src/exports.cc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,33 @@ Pairs toPairs(std::string_view buffer) {
6464
if (buffer.size() < sizeof(uint32_t)) {
6565
return {};
6666
}
67+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
68+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
69+
auto size = __builtin_bswap32(*reinterpret_cast<const uint32_t *>(b));
70+
#else
6771
auto size = *reinterpret_cast<const uint32_t *>(b);
72+
#endif
6873
b += sizeof(uint32_t);
6974
if (sizeof(uint32_t) + size * 2 * sizeof(uint32_t) > buffer.size()) {
7075
return {};
7176
}
7277
result.resize(size);
7378
for (uint32_t i = 0; i < size; i++) {
79+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
80+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
81+
result[i].first =
82+
std::string_view(nullptr, __builtin_bswap32(*reinterpret_cast<const uint32_t *>(b)));
83+
#else
7484
result[i].first = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
85+
#endif
7586
b += sizeof(uint32_t);
87+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
88+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
89+
result[i].second =
90+
std::string_view(nullptr, __builtin_bswap32(*reinterpret_cast<const uint32_t *>(b)));
91+
#else
7692
result[i].second = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
93+
#endif
7794
b += sizeof(uint32_t);
7895
}
7996
for (auto &p : result) {
@@ -94,10 +111,20 @@ bool getPairs(ContextBase *context, const Pairs &result, uint64_t ptr_ptr, uint6
94111
uint64_t ptr;
95112
char *buffer = static_cast<char *>(context->wasm()->allocMemory(size, &ptr));
96113
marshalPairs(result, buffer);
114+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
115+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
116+
if (!context->wasmVm()->setWord(ptr_ptr, Word(__builtin_bswap32(ptr)))) {
117+
#else
97118
if (!context->wasmVm()->setWord(ptr_ptr, Word(ptr))) {
119+
#endif
98120
return false;
99121
}
122+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
123+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
124+
if (!context->wasmVm()->setWord(size_ptr, Word(__builtin_bswap32(size)))) {
125+
#else
100126
if (!context->wasmVm()->setWord(size_ptr, Word(size))) {
127+
#endif
101128
return false;
102129
}
103130
return true;
@@ -257,10 +284,21 @@ Word call_foreign_function(Word function_name, Word function_name_size, Word arg
257284
result_size = s;
258285
return result;
259286
});
287+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
288+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
289+
if (results && !context->wasmVm()->setWord(results, Word(__builtin_bswap32(address)))) {
290+
#else
260291
if (results && !context->wasmVm()->setWord(results, Word(address))) {
292+
#endif
261293
return WasmResult::InvalidMemoryAccess;
262294
}
295+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
296+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
297+
if (results_size &&
298+
!context->wasmVm()->setWord(results_size, Word(__builtin_bswap32(result_size)))) {
299+
#else
263300
if (results_size && !context->wasmVm()->setWord(results_size, Word(result_size))) {
301+
#endif
264302
return WasmResult::InvalidMemoryAccess;
265303
}
266304
if (!results) {
@@ -462,7 +500,12 @@ Word get_header_map_size(Word type, Word result_ptr) {
462500
if (result != WasmResult::Ok) {
463501
return result;
464502
}
503+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
504+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
505+
if (!context->wasmVm()->setWord(result_ptr, Word(__builtin_bswap32(size)))) {
506+
#else
465507
if (!context->wasmVm()->setWord(result_ptr, Word(size))) {
508+
#endif
466509
return WasmResult::InvalidMemoryAccess;
467510
}
468511
return WasmResult::Ok;
@@ -503,7 +546,12 @@ Word get_buffer_status(Word type, Word length_ptr, Word flags_ptr) {
503546
}
504547
auto length = buffer->size();
505548
uint32_t flags = 0;
549+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
550+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
551+
if (!context->wasmVm()->setWord(length_ptr, Word(__builtin_bswap32(length)))) {
552+
#else
506553
if (!context->wasmVm()->setWord(length_ptr, Word(length))) {
554+
#endif
507555
return WasmResult::InvalidMemoryAccess;
508556
}
509557
if (!context->wasm()->setDatatype(flags_ptr, flags)) {
@@ -712,7 +760,13 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
712760
}
713761
const uint32_t *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
714762
if (iovec[1] /* buf_len */) {
763+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
764+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
765+
memslice = context->wasmVm()->getMemory(__builtin_bswap32(iovec[0]) /* buf */,
766+
__builtin_bswap32(iovec[1]) /* buf_len */);
767+
#else
715768
memslice = context->wasmVm()->getMemory(iovec[0] /* buf */, iovec[1] /* buf_len */);
769+
#endif
716770
if (!memslice) {
717771
return 21; // __WASI_EFAULT
718772
}
@@ -744,7 +798,12 @@ Word wasi_unstable_fd_write(Word fd, Word iovs, Word iovs_len, Word nwritten_ptr
744798
if (result != 0) { // __WASI_ESUCCESS
745799
return result;
746800
}
801+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
802+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
803+
if (!context->wasmVm()->setWord(nwritten_ptr, Word(__builtin_bswap32(nwritten)))) {
804+
#else
747805
if (!context->wasmVm()->setWord(nwritten_ptr, Word(nwritten))) {
806+
#endif
748807
return 21; // __WASI_EFAULT
749808
}
750809
return 0; // __WASI_ESUCCESS
@@ -798,7 +857,12 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
798857
auto word_size = context->wasmVm()->getWordSize();
799858
auto &envs = context->wasm()->envs();
800859
for (auto e : envs) {
860+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
861+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
862+
if (!context->wasmVm()->setWord(environ_array_ptr, __builtin_bswap32(environ_buf))) {
863+
#else
801864
if (!context->wasmVm()->setWord(environ_array_ptr, environ_buf)) {
865+
#endif
802866
return 21; // __WASI_EFAULT
803867
}
804868

@@ -823,7 +887,12 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
823887
Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
824888
auto context = contextOrEffectiveContext();
825889
auto &envs = context->wasm()->envs();
890+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
891+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
892+
if (!context->wasmVm()->setWord(count_ptr, Word(__builtin_bswap32(envs.size())))) {
893+
#else
826894
if (!context->wasmVm()->setWord(count_ptr, Word(envs.size()))) {
895+
#endif
827896
return 21; // __WASI_EFAULT
828897
}
829898

@@ -832,7 +901,12 @@ Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
832901
// len(key) + len(value) + 1('=') + 1(null terminator)
833902
size += e.first.size() + e.second.size() + 2;
834903
}
904+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
905+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
906+
if (!context->wasmVm()->setWord(buf_size_ptr, Word(__builtin_bswap32(size)))) {
907+
#else
835908
if (!context->wasmVm()->setWord(buf_size_ptr, Word(size))) {
909+
#endif
836910
return 21; // __WASI_EFAULT
837911
}
838912
return 0; // __WASI_ESUCCESS

0 commit comments

Comments
 (0)