Configuration.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/MemoryStream.h>
  7. #include <LibWasm/AbstractMachine/Configuration.h>
  8. #include <LibWasm/AbstractMachine/Interpreter.h>
  9. #include <LibWasm/Printer/Printer.h>
  10. namespace Wasm {
  11. Optional<size_t> Configuration::nth_label_index(size_t i)
  12. {
  13. for (size_t index = m_stack.size(); index > 0; --index) {
  14. auto& entry = m_stack.entries()[index - 1];
  15. if (entry.has<Label>()) {
  16. if (i == 0)
  17. return index - 1;
  18. --i;
  19. }
  20. }
  21. return {};
  22. }
  23. void Configuration::unwind(Badge<CallFrameHandle>, CallFrameHandle const& frame_handle)
  24. {
  25. if (m_stack.size() == frame_handle.stack_size && frame_handle.frame_index == m_current_frame_index)
  26. return;
  27. VERIFY(m_stack.size() > frame_handle.stack_size);
  28. m_stack.entries().remove(frame_handle.stack_size, m_stack.size() - frame_handle.stack_size);
  29. m_current_frame_index = frame_handle.frame_index;
  30. m_depth--;
  31. m_ip = frame_handle.ip;
  32. VERIFY(m_stack.size() == frame_handle.stack_size);
  33. }
  34. Result Configuration::call(Interpreter& interpreter, FunctionAddress address, Vector<Value> arguments)
  35. {
  36. auto* function = m_store.get(address);
  37. if (!function)
  38. return Trap {};
  39. if (auto* wasm_function = function->get_pointer<WasmFunction>()) {
  40. Vector<Value> locals = move(arguments);
  41. locals.ensure_capacity(locals.size() + wasm_function->code().locals().size());
  42. for (auto& type : wasm_function->code().locals())
  43. locals.empend(type, 0ull);
  44. set_frame(Frame {
  45. wasm_function->module(),
  46. move(locals),
  47. wasm_function->code().body(),
  48. wasm_function->type().results().size(),
  49. });
  50. m_ip = 0;
  51. return execute(interpreter);
  52. }
  53. // It better be a host function, else something is really wrong.
  54. auto& host_function = function->get<HostFunction>();
  55. return host_function.function()(*this, arguments);
  56. }
  57. Result Configuration::execute(Interpreter& interpreter)
  58. {
  59. interpreter.interpret(*this);
  60. if (interpreter.did_trap())
  61. return Trap { interpreter.trap_reason() };
  62. if (stack().size() <= frame().arity() + 1)
  63. return Trap { "Not enough values to return from call" };
  64. Vector<Value> results;
  65. results.ensure_capacity(frame().arity());
  66. for (size_t i = 0; i < frame().arity(); ++i)
  67. results.append(move(stack().pop().get<Value>()));
  68. auto label = stack().pop();
  69. // ASSERT: label == current frame
  70. if (!label.has<Label>())
  71. return Trap { "Invalid stack configuration" };
  72. return Result { move(results) };
  73. }
  74. void Configuration::dump_stack()
  75. {
  76. auto print_value = []<typename... Ts>(CheckedFormatString<Ts...> format, Ts... vs) {
  77. AllocatingMemoryStream memory_stream;
  78. Printer { memory_stream }.print(vs...);
  79. auto buffer = ByteBuffer::create_uninitialized(memory_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
  80. memory_stream.read_until_filled(buffer).release_value_but_fixme_should_propagate_errors();
  81. dbgln(format.view(), StringView(buffer).trim_whitespace());
  82. };
  83. for (auto const& entry : stack().entries()) {
  84. entry.visit(
  85. [&](Value const& v) {
  86. print_value(" {}", v);
  87. },
  88. [&](Frame const& f) {
  89. dbgln(" frame({})", f.arity());
  90. for (auto& local : f.locals()) {
  91. print_value(" {}", local);
  92. }
  93. },
  94. [](Label const& l) {
  95. dbgln(" label({}) -> {}", l.arity(), l.continuation());
  96. });
  97. }
  98. }
  99. }