Skip to content

Commit 449a8e0

Browse files
committed
Fix endianness conversions for bigendian platforms
Signed-off-by: Matt Leon <mattleon@google.com>
1 parent 672f8a6 commit 449a8e0

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/wasmtime/wasmtime.cc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string>
2222

2323
#include "include/proxy-wasm/limits.h"
24+
#include "include/proxy-wasm/word.h"
2425

2526
#include "crates/c-api/include/wasmtime.hh" // IWYU pragma: keep
2627

@@ -69,6 +70,19 @@ template <typename Arg, typename... Args> std::string printValues(Arg arg, Args.
6970
return printValue(arg) + ((", " + printValue(args)) + ... + "");
7071
}
7172

73+
template <typename... Args> void InPlaceConvertWasmToHostEndianness(Args &...args) {
74+
(void(args = wasmtoh(convertWordToUint32(args), true)), ...);
75+
}
76+
proxy_wasm::Word ConvertWasmToHostEndianness(const proxy_wasm::Word &arg) {
77+
return proxy_wasm::Word(wasmtoh(convertWordToUint32(arg), true));
78+
}
79+
template <typename... Args> void InPlaceConvertHostToWasmEndianness(Args &...args) {
80+
(void(args = htowasm(convertWordToUint32(args), true)), ...);
81+
}
82+
template <typename... Args> auto ConvertHostToWasmEndianness(const Args &...args) {
83+
return std::make_tuple((htowasm(convertWordToUint32(args), true))...);
84+
}
85+
7286
} // namespace
7387

7488
class Wasmtime : public WasmVm {
@@ -280,6 +294,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
280294
module_name, function_name,
281295
[this, function,
282296
function_name = std::string(module_name) + "." + std::string(function_name)](Args... args) {
297+
InPlaceConvertWasmToHostEndianness(args...);
283298
const bool log = cmpLogLevel(LogLevel::trace);
284299
if (log) {
285300
integration()->trace("[vm->host] " + function_name + "(" + printValues(args...) + ")");
@@ -301,6 +316,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
301316
module_name, function_name,
302317
[this, function,
303318
function_name = std::string(module_name) + "." + std::string(function_name)](Args... args) {
319+
InPlaceConvertWasmToHostEndianness(args...);
304320
const bool log = cmpLogLevel(LogLevel::trace);
305321
if (log) {
306322
integration()->trace("[vm->host] " + function_name + "(" + printValues(args...) + ")");
@@ -309,7 +325,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
309325
if (log) {
310326
integration()->trace("[vm<-host] " + function_name + " return: " + printValue(result));
311327
}
312-
return result;
328+
return ConvertHostToWasmEndianness(result);
313329
});
314330
if (!result) {
315331
fail(FailState::ConfigureFailed, "Failed to register host function: " + result.err().message());
@@ -337,6 +353,7 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
337353
integration()->trace("[host->vm] " + std::string(function_name) + "(" + printValues(args...) +
338354
")");
339355
}
356+
InPlaceConvertHostToWasmEndianness(args...);
340357
TrapResult<std::monostate> result = func.call(store_->context(), {args...});
341358
if (!result) {
342359
fail(FailState::RuntimeError,
@@ -369,17 +386,19 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
369386
integration()->trace("[host->vm] " + std::string(function_name) + "(" + printValues(args...) +
370387
")");
371388
}
389+
InPlaceConvertHostToWasmEndianness(args...);
372390
TrapResult<R> result = func.call(store_->context(), {args...});
373391
if (!result) {
374392
fail(FailState::RuntimeError,
375393
"Function: " + std::string(function_name) + " failed: " + result.err().message());
376394
return R{};
377395
}
396+
R result_host = ConvertWasmToHostEndianness(result.ok());
378397
if (log) {
379398
integration()->trace("[host<-vm] " + std::string(function_name) +
380-
" return: " + printValue(result.unwrap()));
399+
" return: " + printValue(result_host));
381400
}
382-
return result.ok();
401+
return result_host;
383402
};
384403
};
385404

test/wasm_vm_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ TEST_P(TestVm, Memory) {
8585
ASSERT_EQ(100, word.u64_);
8686

8787
uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1), vm_->usesWasmByteOrder()),
88-
htowasm(200, vm_->usesWasmByteOrder())};
88+
htowasm(static_cast<uint32_t>(200), vm_->usesWasmByteOrder())};
8989
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
9090
ASSERT_TRUE(vm_->getWord(0x200, &word));
9191
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));

0 commit comments

Comments
 (0)