From cf7937e369c85472fef72bcacbc11c1ad172e206 Mon Sep 17 00:00:00 2001 From: Diego <96022404+dzfrias@users.noreply.github.com> Date: Wed, 12 Jun 2024 22:15:04 -0700 Subject: [PATCH] LibWasm: Make `memory.fill` fill with single bytes Previously, `memory.fill` filled memory with 4-byte values, even though `memory.fill` should fill with just one byte. Also fixes some other issues with some of the bulk memory instructions, like `memory.init`. --- .../AbstractMachine/BytecodeInterpreter.cpp | 24 ++++++++++--------- .../AbstractMachine/BytecodeInterpreter.h | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp index 97a95c9baaf..eccfdd58bfe 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp @@ -382,12 +382,12 @@ void BytecodeInterpreter::pop_and_store(Configuration& configuration, Instructio store_to_memory(configuration, instruction, { &value, sizeof(StoreT) }, *base); } -void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruction const& instruction, ReadonlyBytes data, i32 base) +void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruction const& instruction, ReadonlyBytes data, u32 base) { auto& arg = instruction.arguments().get(); auto& address = configuration.frame().module().memories()[arg.memory_index.value()]; auto memory = configuration.store().get(address); - u64 instance_address = static_cast(bit_cast(base)) + arg.offset; + u64 instance_address = static_cast(base) + arg.offset; Checked addition { instance_address }; addition += data.size(); if (addition.has_overflow() || addition.value() > memory->size()) { @@ -771,9 +771,9 @@ void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPoi auto& args = instruction.arguments().get(); auto address = configuration.frame().module().memories()[args.memory_index.value()]; auto instance = configuration.store().get(address); - auto count = configuration.stack().pop().get().to().value(); - auto value = configuration.stack().pop().get().to().value(); - auto destination_offset = configuration.stack().pop().get().to().value(); + auto count = configuration.stack().pop().get().to().value(); + u8 value = static_cast(configuration.stack().pop().get().to().value()); + auto destination_offset = configuration.stack().pop().get().to().value(); TRAP_IF_NOT(static_cast(destination_offset + count) <= instance->data().size()); @@ -785,8 +785,8 @@ void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPoi Instruction::MemoryArgument { 0, 0 } }; - for (auto i = 0; i < count; ++i) { - store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset); + for (u32 i = 0; i < count; ++i) { + store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset + i); } return; } @@ -836,11 +836,13 @@ void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPoi auto& args = instruction.arguments().get(); auto& data_address = configuration.frame().module().datas()[args.data_index.value()]; auto& data = *configuration.store().get(data_address); - auto count = *configuration.stack().pop().get().to(); - auto source_offset = *configuration.stack().pop().get().to(); - auto destination_offset = *configuration.stack().pop().get().to(); + auto count = *configuration.stack().pop().get().to(); + auto source_offset = *configuration.stack().pop().get().to(); + auto destination_offset = *configuration.stack().pop().get().to(); + + if (count == 0) + return; - TRAP_IF_NOT(count > 0); TRAP_IF_NOT(source_offset + count > 0); TRAP_IF_NOT(static_cast(source_offset + count) <= data.size()); diff --git a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h index b5844177eb1..1c9c1cf2c5e 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h @@ -62,7 +62,7 @@ protected: Optional pop_vector(Configuration&); template typename SetSign, typename VectorType = Native128ByteVectorOf> Optional peek_vector(Configuration&); - void store_to_memory(Configuration&, Instruction const&, ReadonlyBytes data, i32 base); + void store_to_memory(Configuration&, Instruction const&, ReadonlyBytes data, u32 base); void call_address(Configuration&, FunctionAddress); template