From 281dcd3ce631238da57142f81b667373ec9ae17d Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Fri, 18 Aug 2023 19:26:07 -0400 Subject: [PATCH] avoid unaligned accesses to types casted from byte stream --- .../chain/include/eosio/chain/wasm_eosio_binary_ops.hpp | 5 +++-- libraries/wasm-jit/Include/IR/Operators.h | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libraries/chain/include/eosio/chain/wasm_eosio_binary_ops.hpp b/libraries/chain/include/eosio/chain/wasm_eosio_binary_ops.hpp index 36c6327981..968a039316 100644 --- a/libraries/chain/include/eosio/chain/wasm_eosio_binary_ops.hpp +++ b/libraries/chain/include/eosio/chain/wasm_eosio_binary_ops.hpp @@ -135,7 +135,7 @@ inline void pack( instruction_stream* stream, branchtabletype field ) { template struct field_specific_params { static constexpr int skip_ahead = sizeof(uint16_t) + sizeof(Field); - static auto unpack( char* opcode, Field& f ) { f = *reinterpret_cast(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); } @@ -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,...) \ diff --git a/libraries/wasm-jit/Include/IR/Operators.h b/libraries/wasm-jit/Include/IR/Operators.h index 6a82e5d7ae..397b6fc28a 100644 --- a/libraries/wasm-jit/Include/IR/Operators.h +++ b/libraries/wasm-jit/Include/IR/Operators.h @@ -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 { @@ -305,7 +306,8 @@ namespace IR Opcode opcode; NoImm imm; }; - }; + }); + PACKED_STRUCT( template<> struct OpcodeAndImm { @@ -314,7 +316,7 @@ namespace IR Opcode opcode; MemoryImm imm; }; - }; + }); // Decodes an operator from an input stream and dispatches by opcode. struct OperatorDecoderStream @@ -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,...) \