Skip to content

Commit ad11d07

Browse files
committed
Do not reverse bytes for nullvm
more changes: - renamed is_wasm_byte_order to vm_uses_wasm_byte_order - renamed isWasmByteOrder() to usesWasmByteOrder() Fixes #294 Signed-off-by: Konstantin Maksimov <konstantin.maksimov@ibm.com>
1 parent 6e273eb commit ad11d07

File tree

12 files changed

+30
-30
lines changed

12 files changed

+30
-30
lines changed

include/proxy-wasm/null_vm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct NullVm : public WasmVm {
6161
#undef _REGISTER_CALLBACK
6262

6363
void terminate() override {}
64-
bool isWasmByteOrder() override { return false; }
64+
bool usesWasmByteOrder() override { return false; }
6565

6666
std::string plugin_name_;
6767
std::unique_ptr<NullVmPlugin> plugin_;

include/proxy-wasm/wasm_vm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class WasmVm {
307307
* Byte order flag (host or wasm).
308308
* @return 'false' for a null VM and 'true' for a wasm VM.
309309
*/
310-
virtual bool isWasmByteOrder() = 0;
310+
virtual bool usesWasmByteOrder() = 0;
311311

312312
bool isFailed() { return failed_ != FailState::Ok; }
313313
void fail(FailState fail_state, std::string_view message) {

include/proxy-wasm/word.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ namespace proxy_wasm {
2424
// Use byteswap functions only when compiling for big-endian platforms.
2525
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
2626
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
27-
#define htowasm(x, is_wasm_byte_order) ((is_wasm_byte_order) ? __builtin_bswap32(x) : (x))
28-
#define wasmtoh(x, is_wasm_byte_order) ((is_wasm_byte_order) ? __builtin_bswap32(x) : (x))
27+
#define htowasm(x, vm_uses_wasm_byte_order) ((vm_uses_wasm_byte_order) ? __builtin_bswap32(x) : (x))
28+
#define wasmtoh(x, vm_uses_wasm_byte_order) ((vm_uses_wasm_byte_order) ? __builtin_bswap32(x) : (x))
2929
#else
30-
#define htowasm(x, is_wasm_byte_order) (x)
31-
#define wasmtoh(x, is_wasm_byte_order) (x)
30+
#define htowasm(x, vm_uses_wasm_byte_order) (x)
31+
#define wasmtoh(x, vm_uses_wasm_byte_order) (x)
3232
#endif
3333

3434
// Represents a Wasm-native word-sized datum. On 32-bit VMs, the high bits are always zero.

src/exports.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
694694
const auto *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
695695
if (iovec[1] != 0U /* buf_len */) {
696696
memslice = context->wasmVm()->getMemory(
697-
wasmtoh(iovec[0], context->wasmVm()->isWasmByteOrder()) /* buf */,
698-
wasmtoh(iovec[1], context->wasmVm()->isWasmByteOrder()) /* buf_len */);
697+
wasmtoh(iovec[0], context->wasmVm()->usesWasmByteOrder()) /* buf */,
698+
wasmtoh(iovec[1], context->wasmVm()->usesWasmByteOrder()) /* buf_len */);
699699
if (!memslice) {
700700
return 21; // __WASI_EFAULT
701701
}

src/pairs_util.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
4848
uint32_t num_pairs =
4949
htowasm(pairs.size(), contextOrEffectiveContext() == nullptr
5050
? false
51-
: contextOrEffectiveContext()->wasmVm()->isWasmByteOrder());
51+
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
5252
if (pos + sizeof(uint32_t) > end) {
5353
return false;
5454
}
@@ -60,7 +60,7 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
6060
uint32_t name_len =
6161
htowasm(p.first.size(), contextOrEffectiveContext() == nullptr
6262
? false
63-
: contextOrEffectiveContext()->wasmVm()->isWasmByteOrder());
63+
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
6464
if (pos + sizeof(uint32_t) > end) {
6565
return false;
6666
}
@@ -71,7 +71,7 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
7171
uint32_t value_len =
7272
htowasm(p.second.size(), contextOrEffectiveContext() == nullptr
7373
? false
74-
: contextOrEffectiveContext()->wasmVm()->isWasmByteOrder());
74+
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
7575
if (pos + sizeof(uint32_t) > end) {
7676
return false;
7777
}
@@ -115,7 +115,7 @@ Pairs PairsUtil::toPairs(std::string_view buffer) {
115115
uint32_t num_pairs = wasmtoh(*reinterpret_cast<const uint32_t *>(pos),
116116
contextOrEffectiveContext() == nullptr
117117
? false
118-
: contextOrEffectiveContext()->wasmVm()->isWasmByteOrder());
118+
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
119119
pos += sizeof(uint32_t);
120120

121121
// Check if we're not going to exceed the limit.
@@ -137,7 +137,7 @@ Pairs PairsUtil::toPairs(std::string_view buffer) {
137137
s.first = wasmtoh(*reinterpret_cast<const uint32_t *>(pos),
138138
contextOrEffectiveContext() == nullptr
139139
? false
140-
: contextOrEffectiveContext()->wasmVm()->isWasmByteOrder());
140+
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
141141
pos += sizeof(uint32_t);
142142

143143
// Read value length.
@@ -147,7 +147,7 @@ Pairs PairsUtil::toPairs(std::string_view buffer) {
147147
s.second = wasmtoh(*reinterpret_cast<const uint32_t *>(pos),
148148
contextOrEffectiveContext() == nullptr
149149
? false
150-
: contextOrEffectiveContext()->wasmVm()->isWasmByteOrder());
150+
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
151151
pos += sizeof(uint32_t);
152152
}
153153

src/v8/v8.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class V8 : public WasmVm {
105105
#undef _GET_MODULE_FUNCTION
106106

107107
void terminate() override;
108-
bool isWasmByteOrder() override { return true; }
108+
bool usesWasmByteOrder() override { return true; }
109109

110110
private:
111111
wasm::own<wasm::Trap> trap(std::string message);
@@ -504,7 +504,7 @@ bool V8::getWord(uint64_t pointer, Word *word) {
504504
}
505505
uint32_t word32;
506506
::memcpy(&word32, memory_->data() + pointer, size);
507-
word->u64_ = wasmtoh(word32, isWasmByteOrder());
507+
word->u64_ = wasmtoh(word32, usesWasmByteOrder());
508508
return true;
509509
}
510510

@@ -517,7 +517,7 @@ bool V8::setWord(uint64_t pointer, Word word) {
517517
if (pointer + size > memory_->data_size()) {
518518
return false;
519519
}
520-
uint32_t word32 = htowasm(word.u32(), isWasmByteOrder());
520+
uint32_t word32 = htowasm(word.u32(), usesWasmByteOrder());
521521
::memcpy(memory_->data() + pointer, &word32, size);
522522
return true;
523523
}

src/wamr/wamr.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Wamr : public WasmVm {
8787
#undef _GET_MODULE_FUNCTION
8888

8989
void terminate() override {}
90-
bool isWasmByteOrder() override { return true; }
90+
bool usesWasmByteOrder() override { return true; }
9191

9292
private:
9393
template <typename... Args>
@@ -369,7 +369,7 @@ bool Wamr::getWord(uint64_t pointer, Word *word) {
369369

370370
uint32_t word32;
371371
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
372-
word->u64_ = wasmtoh(word32, isWasmByteOrder());
372+
word->u64_ = wasmtoh(word32, usesWasmByteOrder());
373373
return true;
374374
}
375375

@@ -378,7 +378,7 @@ bool Wamr::setWord(uint64_t pointer, Word word) {
378378
if (pointer + size > wasm_memory_data_size(memory_.get())) {
379379
return false;
380380
}
381-
uint32_t word32 = htowasm(word.u32(), isWasmByteOrder());
381+
uint32_t word32 = htowasm(word.u32(), usesWasmByteOrder());
382382
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
383383
return true;
384384
}

src/wasmedge/wasmedge.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class WasmEdge : public WasmVm {
281281
std::function<R(ContextBase *, Args...)> *function);
282282

283283
void terminate() override {}
284-
bool isWasmByteOrder() override { return true; }
284+
bool usesWasmByteOrder() override { return true; }
285285

286286
WasmEdgeLoaderPtr loader_;
287287
WasmEdgeValidatorPtr validator_;

src/wasmtime/wasmtime.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Wasmtime : public WasmVm {
9898
std::function<R(ContextBase *, Args...)> *function);
9999

100100
void terminate() override {}
101-
bool isWasmByteOrder() override { return true; }
101+
bool usesWasmByteOrder() override { return true; }
102102

103103
WasmStorePtr store_;
104104
WasmModulePtr module_;
@@ -395,7 +395,7 @@ bool Wasmtime::getWord(uint64_t pointer, Word *word) {
395395

396396
uint32_t word32;
397397
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
398-
word->u64_ = wasmtoh(word32, isWasmByteOrder());
398+
word->u64_ = wasmtoh(word32, usesWasmByteOrder());
399399
return true;
400400
}
401401

@@ -404,7 +404,7 @@ bool Wasmtime::setWord(uint64_t pointer, Word word) {
404404
if (pointer + size > wasm_memory_data_size(memory_.get())) {
405405
return false;
406406
}
407-
uint32_t word32 = htowasm(word.u32(), isWasmByteOrder());
407+
uint32_t word32 = htowasm(word.u32(), usesWasmByteOrder());
408408
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
409409
return true;
410410
}

src/wavm/wavm.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ struct Wavm : public WasmVm {
230230
#undef _REGISTER_CALLBACK
231231

232232
void terminate() override {}
233-
bool isWasmByteOrder() override { return true; }
233+
bool usesWasmByteOrder() override { return true; }
234234

235235
IR::Module ir_module_;
236236
WAVM::Runtime::ModuleRef module_ = nullptr;
@@ -390,12 +390,12 @@ bool Wavm::getWord(uint64_t pointer, Word *data) {
390390
auto *p = reinterpret_cast<char *>(memory_base_ + pointer);
391391
uint32_t data32;
392392
memcpy(&data32, p, sizeof(uint32_t));
393-
data->u64_ = wasmtoh(data32, isWasmByteOrder());
393+
data->u64_ = wasmtoh(data32, usesWasmByteOrder());
394394
return true;
395395
}
396396

397397
bool Wavm::setWord(uint64_t pointer, Word data) {
398-
uint32_t data32 = htowasm(data.u32(), isWasmByteOrder());
398+
uint32_t data32 = htowasm(data.u32(), usesWasmByteOrder());
399399
return setMemory(pointer, sizeof(uint32_t), &data32);
400400
}
401401

test/null_vm_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ TEST_F(BaseVmTest, NullVmStartup) {
7777
TEST_F(BaseVmTest, ByteOrder) {
7878
auto wasm_vm = createNullVm();
7979
EXPECT_TRUE(wasm_vm->load("test_null_vm_plugin", {}, {}));
80-
EXPECT_FALSE(wasm_vm->isWasmByteOrder());
80+
EXPECT_FALSE(wasm_vm->usesWasmByteOrder());
8181
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
8282
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
8383
proxy_wasm::Pairs pairs1;

test/wasm_vm_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ TEST_P(TestVm, Memory) {
5353
ASSERT_TRUE(vm_->getWord(0x2000, &word));
5454
ASSERT_EQ(100, word.u64_);
5555

56-
uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1), vm_->isWasmByteOrder()),
57-
htowasm(200, vm_->isWasmByteOrder())};
56+
uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1), vm_->usesWasmByteOrder()),
57+
htowasm(200, vm_->usesWasmByteOrder())};
5858
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
5959
ASSERT_TRUE(vm_->getWord(0x200, &word));
6060
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));

0 commit comments

Comments
 (0)