@@ -64,16 +64,33 @@ Pairs toPairs(std::string_view buffer) {
64
64
if (buffer.size () < sizeof (uint32_t )) {
65
65
return {};
66
66
}
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
67
71
auto size = *reinterpret_cast <const uint32_t *>(b);
72
+ #endif
68
73
b += sizeof (uint32_t );
69
74
if (sizeof (uint32_t ) + size * 2 * sizeof (uint32_t ) > buffer.size ()) {
70
75
return {};
71
76
}
72
77
result.resize (size);
73
78
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
74
84
result[i].first = std::string_view (nullptr , *reinterpret_cast <const uint32_t *>(b));
85
+ #endif
75
86
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
76
92
result[i].second = std::string_view (nullptr , *reinterpret_cast <const uint32_t *>(b));
93
+ #endif
77
94
b += sizeof (uint32_t );
78
95
}
79
96
for (auto &p : result) {
@@ -94,10 +111,20 @@ bool getPairs(ContextBase *context, const Pairs &result, uint64_t ptr_ptr, uint6
94
111
uint64_t ptr;
95
112
char *buffer = static_cast <char *>(context->wasm ()->allocMemory (size, &ptr));
96
113
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
97
118
if (!context->wasmVm ()->setWord (ptr_ptr, Word (ptr))) {
119
+ #endif
98
120
return false ;
99
121
}
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
100
126
if (!context->wasmVm ()->setWord (size_ptr, Word (size))) {
127
+ #endif
101
128
return false ;
102
129
}
103
130
return true ;
@@ -257,10 +284,21 @@ Word call_foreign_function(Word function_name, Word function_name_size, Word arg
257
284
result_size = s;
258
285
return result;
259
286
});
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
260
291
if (results && !context->wasmVm ()->setWord (results, Word (address))) {
292
+ #endif
261
293
return WasmResult::InvalidMemoryAccess;
262
294
}
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
263
300
if (results_size && !context->wasmVm ()->setWord (results_size, Word (result_size))) {
301
+ #endif
264
302
return WasmResult::InvalidMemoryAccess;
265
303
}
266
304
if (!results) {
@@ -462,7 +500,12 @@ Word get_header_map_size(Word type, Word result_ptr) {
462
500
if (result != WasmResult::Ok) {
463
501
return result;
464
502
}
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
465
507
if (!context->wasmVm ()->setWord (result_ptr, Word (size))) {
508
+ #endif
466
509
return WasmResult::InvalidMemoryAccess;
467
510
}
468
511
return WasmResult::Ok;
@@ -503,7 +546,12 @@ Word get_buffer_status(Word type, Word length_ptr, Word flags_ptr) {
503
546
}
504
547
auto length = buffer->size ();
505
548
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
506
553
if (!context->wasmVm ()->setWord (length_ptr, Word (length))) {
554
+ #endif
507
555
return WasmResult::InvalidMemoryAccess;
508
556
}
509
557
if (!context->wasm ()->setDatatype (flags_ptr, flags)) {
@@ -712,7 +760,13 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
712
760
}
713
761
const uint32_t *iovec = reinterpret_cast <const uint32_t *>(memslice.value ().data ());
714
762
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
715
768
memslice = context->wasmVm ()->getMemory (iovec[0 ] /* buf */ , iovec[1 ] /* buf_len */ );
769
+ #endif
716
770
if (!memslice) {
717
771
return 21 ; // __WASI_EFAULT
718
772
}
@@ -744,7 +798,12 @@ Word wasi_unstable_fd_write(Word fd, Word iovs, Word iovs_len, Word nwritten_ptr
744
798
if (result != 0 ) { // __WASI_ESUCCESS
745
799
return result;
746
800
}
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
747
805
if (!context->wasmVm ()->setWord (nwritten_ptr, Word (nwritten))) {
806
+ #endif
748
807
return 21 ; // __WASI_EFAULT
749
808
}
750
809
return 0 ; // __WASI_ESUCCESS
@@ -798,7 +857,12 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
798
857
auto word_size = context->wasmVm ()->getWordSize ();
799
858
auto &envs = context->wasm ()->envs ();
800
859
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
801
864
if (!context->wasmVm ()->setWord (environ_array_ptr, environ_buf)) {
865
+ #endif
802
866
return 21 ; // __WASI_EFAULT
803
867
}
804
868
@@ -823,7 +887,12 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
823
887
Word wasi_unstable_environ_sizes_get (Word count_ptr, Word buf_size_ptr) {
824
888
auto context = contextOrEffectiveContext ();
825
889
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
826
894
if (!context->wasmVm ()->setWord (count_ptr, Word (envs.size ()))) {
895
+ #endif
827
896
return 21 ; // __WASI_EFAULT
828
897
}
829
898
@@ -832,7 +901,12 @@ Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
832
901
// len(key) + len(value) + 1('=') + 1(null terminator)
833
902
size += e.first .size () + e.second .size () + 2 ;
834
903
}
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
835
908
if (!context->wasmVm ()->setWord (buf_size_ptr, Word (size))) {
909
+ #endif
836
910
return 21 ; // __WASI_EFAULT
837
911
}
838
912
return 0 ; // __WASI_ESUCCESS
0 commit comments