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

Conversation

spoonincode
Copy link
Member

WAVM's parser casts Opcode and OpcodeAndImm<> to pointers at arbitrary bytes within the WASM byte stream. For example,

template<typename Visitor>
typename Visitor::Result decodeOp(Visitor& visitor)
{
WAVM_ASSERT_THROW(nextByte + sizeof(Opcode) <= end);
Opcode opcode = *(Opcode*)nextByte;
switch(opcode)
{
#define VISIT_OPCODE(opcode,name,nameString,Imm,...) \
case Opcode::name: \
{ \
WAVM_ASSERT_THROW(nextByte + sizeof(OpcodeAndImm<Imm>) <= end); \
OpcodeAndImm<Imm>* encodedOperator = (OpcodeAndImm<Imm>*)nextByte; \
nextByte += sizeof(OpcodeAndImm<Imm>); \
return visitor.name(encodedOperator->imm); \
}

However, Opcode (including the Opcode in the OpcodeAndImm<> struct), is a uint16_t which gives the compiler leeway to use instructions that require 2-byte alignment on access. On x86 and ARM8 this is not a problem but it's still UB to spec, so prudent to resolve.

@spoonincode spoonincode merged commit cf0d9f4 into main Sep 18, 2023
@spoonincode spoonincode deleted the wavm_unaligned_fix branch September 18, 2023 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants