2021-05-01 01:08:51 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-25 20:19:05 +01:00
|
|
|
#include <AK/MemoryStream.h>
|
2021-05-01 01:08:51 +04:30
|
|
|
#include <LibWasm/AbstractMachine/Configuration.h>
|
|
|
|
|
#include <LibWasm/AbstractMachine/Interpreter.h>
|
2021-06-04 03:30:09 +04:30
|
|
|
#include <LibWasm/Printer/Printer.h>
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
|
namespace Wasm {
|
|
|
|
|
|
2025-09-22 11:38:44 +02:00
|
|
|
void Configuration::unwind_impl()
|
2021-05-24 01:28:02 +04:30
|
|
|
{
|
2025-12-04 03:06:50 +01:00
|
|
|
auto last_frame = m_frame_stack.take_last();
|
2021-05-24 01:28:02 +04:30
|
|
|
m_depth--;
|
2025-08-02 20:30:44 +02:00
|
|
|
m_locals_base = m_frame_stack.is_empty() ? nullptr : m_frame_stack.unchecked_last().locals().data();
|
2026-01-16 02:32:08 +01:00
|
|
|
release_arguments_allocation(last_frame.locals(), m_locals_base != nullptr);
|
2021-05-24 01:28:02 +04:30
|
|
|
}
|
|
|
|
|
|
2025-12-04 03:06:50 +01:00
|
|
|
Result Configuration::call(Interpreter& interpreter, FunctionAddress address, Vector<Value, ArgumentsStaticSize>& arguments)
|
2025-09-22 11:38:44 +02:00
|
|
|
{
|
|
|
|
|
if (auto fn = TRY(prepare_call(address, arguments)); fn.has_value())
|
2025-11-07 11:29:20 +01:00
|
|
|
return fn->function()(*this, arguments.span());
|
2025-09-22 11:38:44 +02:00
|
|
|
m_ip = 0;
|
|
|
|
|
return execute(interpreter);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 03:06:50 +01:00
|
|
|
ErrorOr<Optional<HostFunction&>, Trap> Configuration::prepare_call(FunctionAddress address, Vector<Value, ArgumentsStaticSize>& arguments, bool is_tailcall)
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
|
auto* function = m_store.get(address);
|
|
|
|
|
if (!function)
|
2025-04-22 09:48:26 +02:00
|
|
|
return Trap::from_string("Attempt to call nonexistent function by address");
|
2025-09-22 11:38:44 +02:00
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
if (auto* wasm_function = function->get_pointer<WasmFunction>()) {
|
2025-09-22 11:38:44 +02:00
|
|
|
if (is_tailcall)
|
|
|
|
|
unwind_impl(); // Unwind the current frame, the "return" in the tail-called function will unwind the frame we're gonna push now.
|
2026-01-23 12:30:50 +01:00
|
|
|
arguments.ensure_capacity(arguments.size() + wasm_function->code().func().total_local_count());
|
2024-07-27 16:44:36 -07:00
|
|
|
for (auto& local : wasm_function->code().func().locals()) {
|
|
|
|
|
for (size_t i = 0; i < local.n(); ++i)
|
2026-01-23 12:30:50 +01:00
|
|
|
arguments.unchecked_append(Value(local.type()));
|
2024-07-27 16:44:36 -07:00
|
|
|
}
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2025-12-04 03:06:50 +01:00
|
|
|
set_frame(
|
|
|
|
|
is_tailcall ? IsTailcall::Yes : IsTailcall::No,
|
|
|
|
|
wasm_function->module(),
|
|
|
|
|
move(arguments),
|
|
|
|
|
wasm_function->code().func().body(),
|
|
|
|
|
wasm_function->type().results().size());
|
2025-09-22 11:38:44 +02:00
|
|
|
return OptionalNone {};
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
|
2025-09-22 11:38:44 +02:00
|
|
|
return function->get<HostFunction>();
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
|
2021-05-24 02:04:58 +04:30
|
|
|
Result Configuration::execute(Interpreter& interpreter)
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
|
interpreter.interpret(*this);
|
2021-05-07 09:59:18 +04:30
|
|
|
if (interpreter.did_trap())
|
2025-04-22 09:48:26 +02:00
|
|
|
return interpreter.trap();
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2025-06-06 14:20:07 +02:00
|
|
|
Vector<Value> results { value_stack().span().slice_from_end(frame().arity()) };
|
|
|
|
|
value_stack().shrink(value_stack().size() - results.size(), true);
|
|
|
|
|
results.reverse();
|
2024-08-02 20:51:40 -07:00
|
|
|
|
2026-01-19 12:54:57 +01:00
|
|
|
// If we reached here from a tailcall -> return, we might not have a label to pop (because the return already popped it)
|
|
|
|
|
if (!label_stack().is_empty())
|
|
|
|
|
label_stack().take_last();
|
|
|
|
|
|
2021-05-23 21:21:17 +04:30
|
|
|
return Result { move(results) };
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
|
2021-05-01 03:19:01 +04:30
|
|
|
void Configuration::dump_stack()
|
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
auto print_value = []<typename... Ts>(CheckedFormatString<Ts...> format, Ts... vs) {
|
2023-01-25 20:19:05 +01:00
|
|
|
AllocatingMemoryStream memory_stream;
|
2023-01-21 10:29:21 +01:00
|
|
|
Printer { memory_stream }.print(vs...);
|
2023-01-09 12:06:13 +01:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(memory_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
|
2023-03-01 15:27:35 +01:00
|
|
|
memory_stream.read_until_filled(buffer).release_value_but_fixme_should_propagate_errors();
|
2021-09-10 23:43:11 +02:00
|
|
|
dbgln(format.view(), StringView(buffer).trim_whitespace());
|
2021-06-04 03:30:09 +04:30
|
|
|
};
|
2024-08-02 20:51:40 -07:00
|
|
|
for (auto const& value : value_stack()) {
|
|
|
|
|
print_value(" {}", value);
|
2021-05-01 03:19:01 +04:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|