Skip to content

Commit

Permalink
Merge pull request #1648 from AntelopeIO/wavm_unaligned_fix
Browse files Browse the repository at this point in the history
avoid unaligned accesses to types casted from byte stream in WAVM's wasm parser
  • Loading branch information
spoonincode authored Sep 18, 2023
2 parents 7c93130 + 281dcd3 commit cf0d9f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
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

0 comments on commit cf0d9f4

Please sign in to comment.