From 207379165f277cba25e3fce9032d93d6d66239a3 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 18 May 2021 00:23:41 +0430 Subject: [PATCH] LibWasm: Fix nested structured instruction parsing Previously, the ip would not be propagated correctly, and we would produce invalid jumps when more than one level of nesting was involved. This makes loops work :P --- .../Libraries/LibWasm/AbstractMachine/Interpreter.cpp | 11 +++++++---- Userland/Libraries/LibWasm/Parser/Parser.cpp | 9 +++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp b/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp index a1d39387ec..fa3dd06900 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/Interpreter.cpp @@ -28,10 +28,12 @@ void Interpreter::interpret(Configuration& configuration) while (current_ip_value < max_ip_value) { auto& instruction = instructions[current_ip_value.value()]; + auto old_ip = current_ip_value; interpret(configuration, current_ip_value, instruction); if (m_do_trap) return; - ++current_ip_value; + if (current_ip_value == old_ip) // If no jump occurred + ++current_ip_value; } } @@ -46,18 +48,19 @@ void Interpreter::branch_to_label(Configuration& configuration, LabelIndex index size_t drop_count = index.value() + 1; for (; !configuration.stack().is_empty();) { - auto entry = configuration.stack().pop(); + auto& entry = configuration.stack().peek(); if (entry.has>()) { if (drop_count-- == 0) break; } + configuration.stack().pop(); } // Push results in reverse for (size_t i = results.size(); i > 0; --i) configuration.stack().push(move(static_cast>&>(results)[i - 1])); - configuration.ip() = label->continuation() + 1; + configuration.ip() = label->continuation(); } ReadonlyBytes Interpreter::load_from_memory(Configuration& configuration, const Instruction& instruction, size_t size) @@ -321,7 +324,7 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip auto& args = instruction.arguments().get(); if (args.block_type.kind() != BlockType::Empty) arity = 1; - configuration.stack().push(make