Skip to content

Commit cee566c

Browse files
committed
More changes for big endian machine. (proxy-wasm#197)
Use htole32() and le32toh() instead of __builtin_bswap32() Moved byte reversals into `setWord()` Defined htole32() and le32toh() for macos Signed-off-by: Konstantin Maksimov <konstantin.maksimov@ibm.com>
1 parent b040fd4 commit cee566c

File tree

6 files changed

+16
-94
lines changed

6 files changed

+16
-94
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/wasm.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,20 +421,10 @@ 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
428424
if (!wasm_vm_->setWord(ptr_ptr, Word(pointer))) {
429-
#endif
430425
return false;
431426
}
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
436427
if (!wasm_vm_->setWord(size_ptr, Word(size))) {
437-
#endif
438428
return false;
439429
}
440430
return true;

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 & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,16 @@ 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
71-
auto size = *reinterpret_cast<const uint32_t *>(b);
72-
#endif
67+
auto size = le32toh(*reinterpret_cast<const uint32_t *>(b));
7368
b += sizeof(uint32_t);
7469
if (sizeof(uint32_t) + size * 2 * sizeof(uint32_t) > buffer.size()) {
7570
return {};
7671
}
7772
result.resize(size);
7873
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
84-
result[i].first = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
85-
#endif
74+
result[i].first = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
8675
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
92-
result[i].second = std::string_view(nullptr, *reinterpret_cast<const uint32_t *>(b));
93-
#endif
76+
result[i].second = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
9477
b += sizeof(uint32_t);
9578
}
9679
for (auto &p : result) {
@@ -111,20 +94,10 @@ bool getPairs(ContextBase *context, const Pairs &result, uint64_t ptr_ptr, uint6
11194
uint64_t ptr;
11295
char *buffer = static_cast<char *>(context->wasm()->allocMemory(size, &ptr));
11396
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
11897
if (!context->wasmVm()->setWord(ptr_ptr, Word(ptr))) {
119-
#endif
12098
return false;
12199
}
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
126100
if (!context->wasmVm()->setWord(size_ptr, Word(size))) {
127-
#endif
128101
return false;
129102
}
130103
return true;
@@ -284,21 +257,10 @@ Word call_foreign_function(Word function_name, Word function_name_size, Word arg
284257
result_size = s;
285258
return result;
286259
});
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
291260
if (results && !context->wasmVm()->setWord(results, Word(address))) {
292-
#endif
293261
return WasmResult::InvalidMemoryAccess;
294262
}
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
300263
if (results_size && !context->wasmVm()->setWord(results_size, Word(result_size))) {
301-
#endif
302264
return WasmResult::InvalidMemoryAccess;
303265
}
304266
if (!results) {
@@ -500,12 +462,7 @@ Word get_header_map_size(Word type, Word result_ptr) {
500462
if (result != WasmResult::Ok) {
501463
return result;
502464
}
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
507465
if (!context->wasmVm()->setWord(result_ptr, Word(size))) {
508-
#endif
509466
return WasmResult::InvalidMemoryAccess;
510467
}
511468
return WasmResult::Ok;
@@ -546,12 +503,7 @@ Word get_buffer_status(Word type, Word length_ptr, Word flags_ptr) {
546503
}
547504
auto length = buffer->size();
548505
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
553506
if (!context->wasmVm()->setWord(length_ptr, Word(length))) {
554-
#endif
555507
return WasmResult::InvalidMemoryAccess;
556508
}
557509
if (!context->wasm()->setDatatype(flags_ptr, flags)) {
@@ -760,13 +712,8 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
760712
}
761713
const uint32_t *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
762714
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
768-
memslice = context->wasmVm()->getMemory(iovec[0] /* buf */, iovec[1] /* buf_len */);
769-
#endif
715+
memslice = context->wasmVm()->getMemory(le32toh(iovec[0]) /* buf */,
716+
le32toh(iovec[1]) /* buf_len */);
770717
if (!memslice) {
771718
return 21; // __WASI_EFAULT
772719
}
@@ -798,12 +745,7 @@ Word wasi_unstable_fd_write(Word fd, Word iovs, Word iovs_len, Word nwritten_ptr
798745
if (result != 0) { // __WASI_ESUCCESS
799746
return result;
800747
}
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
805748
if (!context->wasmVm()->setWord(nwritten_ptr, Word(nwritten))) {
806-
#endif
807749
return 21; // __WASI_EFAULT
808750
}
809751
return 0; // __WASI_ESUCCESS
@@ -857,12 +799,7 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
857799
auto word_size = context->wasmVm()->getWordSize();
858800
auto &envs = context->wasm()->envs();
859801
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
864802
if (!context->wasmVm()->setWord(environ_array_ptr, environ_buf)) {
865-
#endif
866803
return 21; // __WASI_EFAULT
867804
}
868805

@@ -887,12 +824,7 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
887824
Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
888825
auto context = contextOrEffectiveContext();
889826
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
894827
if (!context->wasmVm()->setWord(count_ptr, Word(envs.size()))) {
895-
#endif
896828
return 21; // __WASI_EFAULT
897829
}
898830

@@ -901,12 +833,7 @@ Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
901833
// len(key) + len(value) + 1('=') + 1(null terminator)
902834
size += e.first.size() + e.second.size() + 2;
903835
}
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
908836
if (!context->wasmVm()->setWord(buf_size_ptr, Word(size))) {
909-
#endif
910837
return 21; // __WASI_EFAULT
911838
}
912839
return 0; // __WASI_ESUCCESS

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
}

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)