Skip to content

Do not reverse bytes for NullVM. #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/proxy-wasm/null_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct NullVm : public WasmVm {
#undef _REGISTER_CALLBACK

void terminate() override {}
bool isWasmByteOrder() override { return false; }

std::string plugin_name_;
std::unique_ptr<NullVmPlugin> plugin_;
Expand Down
9 changes: 5 additions & 4 deletions include/proxy-wasm/pairs_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ class PairsUtil {
* @param size size of the output buffer.
* @return indicates whether serialization succeeded or not.
*/
static bool marshalPairs(const Pairs &pairs, char *buffer, size_t size);
static bool marshalPairs(const Pairs &pairs, char *buffer, size_t size, bool is_wasm_byte_order);

static bool marshalPairs(const StringPairs &stringpairs, char *buffer, size_t size) {
static bool marshalPairs(const StringPairs &stringpairs, char *buffer, size_t size,
bool is_wasm_byte_order) {
Pairs views(stringpairs.begin(), stringpairs.end());
return marshalPairs(views, buffer, size);
return marshalPairs(views, buffer, size, is_wasm_byte_order);
}

/**
* toPairs deserializes input buffer to Pairs.
* @param buffer serialized input buffer.
* @return deserialized Pairs or an empty instance in case of deserialization failure.
*/
static Pairs toPairs(std::string_view buffer);
static Pairs toPairs(std::string_view buffer, bool is_wasm_byte_order);
};

} // namespace proxy_wasm
6 changes: 6 additions & 0 deletions include/proxy-wasm/wasm_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ class WasmVm {
*/
virtual void terminate() = 0;

/**
* Byte order flag (host or wasm).
* @return 'false' for a null VM and 'true' for a wasm VM.
*/
virtual bool isWasmByteOrder() = 0;

bool isFailed() { return failed_ != FailState::Ok; }
void fail(FailState fail_state, std::string_view message) {
integration()->error(message);
Expand Down
9 changes: 5 additions & 4 deletions include/proxy-wasm/word.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ namespace proxy_wasm {
// Use byteswap functions only when compiling for big-endian platforms.
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define htowasm(x) __builtin_bswap32(x)
#define wasmtoh(x) __builtin_bswap32(x)
#define htowasm1111(x, is_null) is_null ? (x) : __builtin_bswap32(x)
#define htowasm(x, is_wasm_byte_order) is_wasm_byte_order ? __builtin_bswap32(x) : (x)
#define wasmtoh(x, is_wasm_byte_order) is_wasm_byte_order ? __builtin_bswap32(x) : (x)
#else
#define htowasm(x) (x)
#define wasmtoh(x) (x)
#define htowasm(x, is_wasm_byte_order) (x)
#define wasmtoh(x, is_wasm_byte_order) (x)
#endif

// Represents a Wasm-native word-sized datum. On 32-bit VMs, the high bits are always zero.
Expand Down
25 changes: 15 additions & 10 deletions src/exports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ Word send_local_response(Word response_code, Word response_code_details_ptr,
if (!details || !body || !additional_response_header_pairs) {
return WasmResult::InvalidMemoryAccess;
}
auto additional_headers = PairsUtil::toPairs(additional_response_header_pairs.value());
auto additional_headers = PairsUtil::toPairs(additional_response_header_pairs.value(),
context->wasmVm()->isWasmByteOrder());
context->sendLocalResponse(response_code, body.value(), std::move(additional_headers),
grpc_status, details.value());
context->wasm()->stopNextIteration(true);
Expand Down Expand Up @@ -399,7 +400,7 @@ Word get_header_map_pairs(Word type, Word ptr_ptr, Word size_ptr) {
if (buffer == nullptr) {
return WasmResult::InvalidMemoryAccess;
}
if (!PairsUtil::marshalPairs(pairs, buffer, size)) {
if (!PairsUtil::marshalPairs(pairs, buffer, size, context->wasmVm()->isWasmByteOrder())) {
return WasmResult::InvalidMemoryAccess;
}
if (!context->wasmVm()->setWord(ptr_ptr, Word(ptr))) {
Expand All @@ -420,8 +421,9 @@ Word set_header_map_pairs(Word type, Word ptr, Word size) {
if (!data) {
return WasmResult::InvalidMemoryAccess;
}
return context->setHeaderMapPairs(static_cast<WasmHeaderMapType>(type.u64_),
PairsUtil::toPairs(data.value()));
return context->setHeaderMapPairs(
static_cast<WasmHeaderMapType>(type.u64_),
PairsUtil::toPairs(data.value(), context->wasmVm()->isWasmByteOrder()));
}

Word get_header_map_size(Word type, Word result_ptr) {
Expand Down Expand Up @@ -519,8 +521,8 @@ Word http_call(Word uri_ptr, Word uri_size, Word header_pairs_ptr, Word header_p
if (!uri || !body || !header_pairs || !trailer_pairs) {
return WasmResult::InvalidMemoryAccess;
}
auto headers = PairsUtil::toPairs(header_pairs.value());
auto trailers = PairsUtil::toPairs(trailer_pairs.value());
auto headers = PairsUtil::toPairs(header_pairs.value(), context->wasmVm()->isWasmByteOrder());
auto trailers = PairsUtil::toPairs(trailer_pairs.value(), context->wasmVm()->isWasmByteOrder());
uint32_t token = 0;
// NB: try to write the token to verify the memory before starting the async
// operation.
Expand Down Expand Up @@ -589,7 +591,8 @@ Word grpc_call(Word service_ptr, Word service_size, Word service_name_ptr, Word
return WasmResult::InvalidMemoryAccess;
}
uint32_t token = 0;
auto initial_metadata = PairsUtil::toPairs(initial_metadata_pairs.value());
auto initial_metadata =
PairsUtil::toPairs(initial_metadata_pairs.value(), context->wasmVm()->isWasmByteOrder());
auto result = context->grpcCall(service.value(), service_name.value(), method_name.value(),
initial_metadata, request.value(),
std::chrono::milliseconds(timeout_milliseconds), &token);
Expand All @@ -615,7 +618,8 @@ Word grpc_stream(Word service_ptr, Word service_size, Word service_name_ptr, Wor
return WasmResult::InvalidMemoryAccess;
}
uint32_t token = 0;
auto initial_metadata = PairsUtil::toPairs(initial_metadata_pairs.value());
auto initial_metadata =
PairsUtil::toPairs(initial_metadata_pairs.value(), context->wasmVm()->isWasmByteOrder());
auto result = context->grpcStream(service.value(), service_name.value(), method_name.value(),
initial_metadata, &token);
if (result != WasmResult::Ok) {
Expand Down Expand Up @@ -693,8 +697,9 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
}
const auto *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
if (iovec[1] != 0U /* buf_len */) {
memslice = context->wasmVm()->getMemory(wasmtoh(iovec[0]) /* buf */,
wasmtoh(iovec[1]) /* buf_len */);
memslice = context->wasmVm()->getMemory(
wasmtoh(iovec[0], context->wasmVm()->isWasmByteOrder()) /* buf */,
wasmtoh(iovec[1], context->wasmVm()->isWasmByteOrder()) /* buf_len */);
if (!memslice) {
return 21; // __WASI_EFAULT
}
Expand Down
17 changes: 9 additions & 8 deletions src/pairs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ size_t PairsUtil::pairsSize(const Pairs &pairs) {
return size;
}

bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size,
[[maybe_unused]] bool is_wasm_byte_order) {
if (buffer == nullptr) {
return false;
}
Expand All @@ -45,7 +46,7 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
const char *end = buffer + size;

// Write number of pairs.
uint32_t num_pairs = htowasm(pairs.size());
uint32_t num_pairs = htowasm(pairs.size(), is_wasm_byte_order);
if (pos + sizeof(uint32_t) > end) {
return false;
}
Expand All @@ -54,15 +55,15 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {

for (const auto &p : pairs) {
// Write name length.
uint32_t name_len = htowasm(p.first.size());
uint32_t name_len = htowasm(p.first.size(), is_wasm_byte_order);
if (pos + sizeof(uint32_t) > end) {
return false;
}
::memcpy(pos, &name_len, sizeof(uint32_t));
pos += sizeof(uint32_t);

// Write value length.
uint32_t value_len = htowasm(p.second.size());
uint32_t value_len = htowasm(p.second.size(), is_wasm_byte_order);
if (pos + sizeof(uint32_t) > end) {
return false;
}
Expand Down Expand Up @@ -91,7 +92,7 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
return pos == end;
}

Pairs PairsUtil::toPairs(std::string_view buffer) {
Pairs PairsUtil::toPairs(std::string_view buffer, [[maybe_unused]] bool is_wasm_byte_order) {
if (buffer.data() == nullptr || buffer.size() > PROXY_WASM_HOST_PAIRS_MAX_BYTES) {
return {};
}
Expand All @@ -103,7 +104,7 @@ Pairs PairsUtil::toPairs(std::string_view buffer) {
if (pos + sizeof(uint32_t) > end) {
return {};
}
uint32_t num_pairs = wasmtoh(*reinterpret_cast<const uint32_t *>(pos));
uint32_t num_pairs = wasmtoh(*reinterpret_cast<const uint32_t *>(pos), is_wasm_byte_order);
pos += sizeof(uint32_t);

// Check if we're not going to exceed the limit.
Expand All @@ -122,14 +123,14 @@ Pairs PairsUtil::toPairs(std::string_view buffer) {
if (pos + sizeof(uint32_t) > end) {
return {};
}
s.first = wasmtoh(*reinterpret_cast<const uint32_t *>(pos));
s.first = wasmtoh(*reinterpret_cast<const uint32_t *>(pos), is_wasm_byte_order);
pos += sizeof(uint32_t);

// Read value length.
if (pos + sizeof(uint32_t) > end) {
return {};
}
s.second = wasmtoh(*reinterpret_cast<const uint32_t *>(pos));
s.second = wasmtoh(*reinterpret_cast<const uint32_t *>(pos), is_wasm_byte_order);
pos += sizeof(uint32_t);
}

Expand Down
2 changes: 1 addition & 1 deletion src/signature_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess

uint32_t alg_id;
std::memcpy(&alg_id, payload.data(), sizeof(uint32_t));
alg_id = wasmtoh(alg_id);
alg_id = wasmtoh(alg_id, true);

if (alg_id != 2) {
message = "Signature has a wrong alg_id (want: 2, is: " + std::to_string(alg_id) + ")";
Expand Down
5 changes: 3 additions & 2 deletions src/v8/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class V8 : public WasmVm {
#undef _GET_MODULE_FUNCTION

void terminate() override;
bool isWasmByteOrder() override { return true; }

private:
wasm::own<wasm::Trap> trap(std::string message);
Expand Down Expand Up @@ -503,7 +504,7 @@ bool V8::getWord(uint64_t pointer, Word *word) {
}
uint32_t word32;
::memcpy(&word32, memory_->data() + pointer, size);
word->u64_ = wasmtoh(word32);
word->u64_ = wasmtoh(word32, isWasmByteOrder());
return true;
}

Expand All @@ -516,7 +517,7 @@ bool V8::setWord(uint64_t pointer, Word word) {
if (pointer + size > memory_->data_size()) {
return false;
}
uint32_t word32 = htowasm(word.u32());
uint32_t word32 = htowasm(word.u32(), isWasmByteOrder());
::memcpy(memory_->data() + pointer, &word32, size);
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/wamr/wamr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Wamr : public WasmVm {
#undef _GET_MODULE_FUNCTION

void terminate() override {}
bool isWasmByteOrder() override { return true; }

private:
template <typename... Args>
Expand Down Expand Up @@ -368,7 +369,7 @@ bool Wamr::getWord(uint64_t pointer, Word *word) {

uint32_t word32;
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
word->u64_ = wasmtoh(word32);
word->u64_ = wasmtoh(word32, isWasmByteOrder());
return true;
}

Expand All @@ -377,7 +378,7 @@ bool Wamr::setWord(uint64_t pointer, Word word) {
if (pointer + size > wasm_memory_data_size(memory_.get())) {
return false;
}
uint32_t word32 = htowasm(word.u32());
uint32_t word32 = htowasm(word.u32(), isWasmByteOrder());
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/wasmedge/wasmedge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ class WasmEdge : public WasmVm {
std::function<R(ContextBase *, Args...)> *function);

void terminate() override {}
bool isWasmByteOrder() override { return true; }

WasmEdgeLoaderPtr loader_;
WasmEdgeValidatorPtr validator_;
Expand Down
5 changes: 3 additions & 2 deletions src/wasmtime/wasmtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Wasmtime : public WasmVm {
std::function<R(ContextBase *, Args...)> *function);

void terminate() override {}
bool isWasmByteOrder() override { return true; }

WasmStorePtr store_;
WasmModulePtr module_;
Expand Down Expand Up @@ -394,7 +395,7 @@ bool Wasmtime::getWord(uint64_t pointer, Word *word) {

uint32_t word32;
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
word->u64_ = wasmtoh(word32);
word->u64_ = wasmtoh(word32, isWasmByteOrder());
return true;
}

Expand All @@ -403,7 +404,7 @@ bool Wasmtime::setWord(uint64_t pointer, Word word) {
if (pointer + size > wasm_memory_data_size(memory_.get())) {
return false;
}
uint32_t word32 = htowasm(word.u32());
uint32_t word32 = htowasm(word.u32(), isWasmByteOrder());
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/wavm/wavm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ struct Wavm : public WasmVm {
#undef _REGISTER_CALLBACK

void terminate() override {}
bool isWasmByteOrder() override { return true; }

IR::Module ir_module_;
WAVM::Runtime::ModuleRef module_ = nullptr;
Expand Down Expand Up @@ -389,12 +390,12 @@ bool Wavm::getWord(uint64_t pointer, Word *data) {
auto *p = reinterpret_cast<char *>(memory_base_ + pointer);
uint32_t data32;
memcpy(&data32, p, sizeof(uint32_t));
data->u64_ = wasmtoh(data32);
data->u64_ = wasmtoh(data32, isWasmByteOrder());
return true;
}

bool Wavm::setWord(uint64_t pointer, Word data) {
uint32_t data32 = htowasm(data.u32());
uint32_t data32 = htowasm(data.u32(), isWasmByteOrder());
return setMemory(pointer, sizeof(uint32_t), &data32);
}

Expand Down
4 changes: 2 additions & 2 deletions test/fuzz/pairs_util_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
auto input = std::string_view(reinterpret_cast<const char *>(data), size);

auto pairs = PairsUtil::toPairs(input);
auto pairs = PairsUtil::toPairs(input, true);

if (!pairs.empty()) {
// Verify that non-empty Pairs serializes back to the same bytes.
Expand All @@ -31,7 +31,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
__builtin_trap();
}
std::vector<char> new_data(new_size);
if (!PairsUtil::marshalPairs(pairs, new_data.data(), new_data.size())) {
if (!PairsUtil::marshalPairs(pairs, new_data.data(), new_data.size(), true)) {
__builtin_trap();
}
if (::memcmp(new_data.data(), data, size) != 0) {
Expand Down
21 changes: 21 additions & 0 deletions test/null_vm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "include/proxy-wasm/null.h"
#include "include/proxy-wasm/null_vm_plugin.h"
#include "include/proxy-wasm/pairs_util.h"

#include "gtest/gtest.h"

Expand Down Expand Up @@ -73,4 +74,24 @@ TEST_F(BaseVmTest, NullVmStartup) {
EXPECT_NE(test_null_vm_plugin, nullptr);
}

TEST_F(BaseVmTest, ByteOrder) {
auto wasm_vm = createNullVm();
EXPECT_TRUE(wasm_vm->load("test_null_vm_plugin", {}, {}));
EXPECT_FALSE(wasm_vm->isWasmByteOrder());
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
proxy_wasm::Pairs pairs1;
std::string data1("some_data");
pairs1.push_back({data1.data(), std::to_string(data1.size())});
std::vector<char> buffer(PairsUtil::pairsSize(pairs1));
// encode using null_vm byte order
EXPECT_TRUE(
PairsUtil::marshalPairs(pairs1, buffer.data(), buffer.size(), wasm_vm->isWasmByteOrder()));
// decode using host byte order
auto pairs2 = PairsUtil::toPairs(std::string_view(buffer.data(), buffer.size()), false);
EXPECT_EQ(pairs2.size(), pairs1.size());
EXPECT_EQ(pairs2[0].second, pairs1[0].second);
#endif
}

} // namespace proxy_wasm
3 changes: 2 additions & 1 deletion test/wasm_vm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ TEST_P(TestVm, Memory) {
ASSERT_TRUE(vm_->getWord(0x2000, &word));
ASSERT_EQ(100, word.u64_);

uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1)), htowasm(200)};
uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1), vm_->isWasmByteOrder()),
htowasm(200, vm_->isWasmByteOrder())};
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
ASSERT_TRUE(vm_->getWord(0x200, &word));
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));
Expand Down