Skip to content
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

avoid unaligned accesses to types casted from byte stream in WAVM's wasm parser #1648

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions libraries/chain/include/eosio/chain/wasm_eosio_binary_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ inline void pack( instruction_stream* stream, branchtabletype field ) {
template <typename Field>
struct field_specific_params {
static constexpr int skip_ahead = sizeof(uint16_t) + sizeof(Field);
static auto unpack( char* opcode, Field& f ) { f = *reinterpret_cast<Field*>(opcode); }
static auto unpack( char* opcode, Field& f ) { memcpy(&f, opcode, sizeof(f)); }
static void pack(instruction_stream* stream, Field& f) { return eosio::chain::wasm_ops::pack(stream, f); }
static auto to_string(Field& f) { return std::string(" ")+
eosio::chain::wasm_ops::to_string(f); }
Expand Down Expand Up @@ -664,7 +664,8 @@ struct EOSIO_OperatorDecoderStream

instr* decodeOp() {
EOS_ASSERT(nextByte + sizeof(IR::Opcode) <= end, wasm_exception, "");
IR::Opcode opcode = *(IR::Opcode*)nextByte;
IR::Opcode opcode;
memcpy(&opcode, nextByte, sizeof(opcode));
switch(opcode)
{
#define VISIT_OPCODE(opcode,name,nameString,Imm,...) \
Expand Down
9 changes: 6 additions & 3 deletions libraries/wasm-jit/Include/IR/Operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ namespace IR
});

// Specialize for the empty immediate structs so they don't take an extra byte of space.
PACKED_STRUCT(
template<>
struct OpcodeAndImm<NoImm>
{
Expand All @@ -305,7 +306,8 @@ namespace IR
Opcode opcode;
NoImm imm;
};
};
});
PACKED_STRUCT(
template<>
struct OpcodeAndImm<MemoryImm>
{
Expand All @@ -314,7 +316,7 @@ namespace IR
Opcode opcode;
MemoryImm imm;
};
};
});

// Decodes an operator from an input stream and dispatches by opcode.
struct OperatorDecoderStream
Expand All @@ -328,7 +330,8 @@ namespace IR
typename Visitor::Result decodeOp(Visitor& visitor)
{
WAVM_ASSERT_THROW(nextByte + sizeof(Opcode) <= end);
Opcode opcode = *(Opcode*)nextByte;
Opcode opcode;
memcpy(&opcode, nextByte, sizeof(opcode));
switch(opcode)
{
#define VISIT_OPCODE(opcode,name,nameString,Imm,...) \
Expand Down