Skip to content

Commit 3e83fb8

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

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,13 @@ 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+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
716+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
717+
memslice = context->wasmVm()->getMemory(__builtin_bswap32(iovec[0]) /* buf */,
718+
__builtin_bswap32(iovec[1]) /* buf_len */);
719+
#else
715720
memslice = context->wasmVm()->getMemory(iovec[0] /* buf */, iovec[1] /* buf_len */);
721+
#endif
716722
if (!memslice) {
717723
return 21; // __WASI_EFAULT
718724
}
@@ -744,7 +750,12 @@ Word wasi_unstable_fd_write(Word fd, Word iovs, Word iovs_len, Word nwritten_ptr
744750
if (result != 0) { // __WASI_ESUCCESS
745751
return result;
746752
}
753+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
754+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
755+
if (!context->wasmVm()->setWord(nwritten_ptr, Word(__builtin_bswap32(nwritten)))) {
756+
#else
747757
if (!context->wasmVm()->setWord(nwritten_ptr, Word(nwritten))) {
758+
#endif
748759
return 21; // __WASI_EFAULT
749760
}
750761
return 0; // __WASI_ESUCCESS
@@ -798,7 +809,12 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
798809
auto word_size = context->wasmVm()->getWordSize();
799810
auto &envs = context->wasm()->envs();
800811
for (auto e : envs) {
812+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
813+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
814+
if (!context->wasmVm()->setWord(environ_array_ptr, __builtin_bswap32(environ_buf))) {
815+
#else
801816
if (!context->wasmVm()->setWord(environ_array_ptr, environ_buf)) {
817+
#endif
802818
return 21; // __WASI_EFAULT
803819
}
804820

@@ -823,7 +839,12 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
823839
Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
824840
auto context = contextOrEffectiveContext();
825841
auto &envs = context->wasm()->envs();
842+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
843+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
844+
if (!context->wasmVm()->setWord(count_ptr, Word(__builtin_bswap32(envs.size())))) {
845+
#else
826846
if (!context->wasmVm()->setWord(count_ptr, Word(envs.size()))) {
847+
#endif
827848
return 21; // __WASI_EFAULT
828849
}
829850

@@ -832,7 +853,12 @@ Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
832853
// len(key) + len(value) + 1('=') + 1(null terminator)
833854
size += e.first.size() + e.second.size() + 2;
834855
}
856+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
857+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
858+
if (!context->wasmVm()->setWord(buf_size_ptr, Word(__builtin_bswap32(size)))) {
859+
#else
835860
if (!context->wasmVm()->setWord(buf_size_ptr, Word(size))) {
861+
#endif
836862
return 21; // __WASI_EFAULT
837863
}
838864
return 0; // __WASI_ESUCCESS

0 commit comments

Comments
 (0)