AbstractMachine.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/HashTable.h>
  10. #include <AK/OwnPtr.h>
  11. #include <AK/Result.h>
  12. #include <AK/StackInfo.h>
  13. #include <LibWasm/Types.h>
  14. // NOTE: Special case for Wasm::Result.
  15. #include <LibJS/Runtime/Completion.h>
  16. namespace Wasm {
  17. class Configuration;
  18. struct Interpreter;
  19. struct InstantiationError {
  20. DeprecatedString error { "Unknown error" };
  21. };
  22. struct LinkError {
  23. enum OtherErrors {
  24. InvalidImportedModule,
  25. };
  26. Vector<DeprecatedString> missing_imports;
  27. Vector<OtherErrors> other_errors;
  28. };
  29. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic, Comparison, Increment);
  30. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ExternAddress, Arithmetic, Comparison, Increment);
  31. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, TableAddress, Arithmetic, Comparison, Increment);
  32. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, GlobalAddress, Arithmetic, Comparison, Increment);
  33. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ElementAddress, Arithmetic, Comparison, Increment);
  34. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, DataAddress, Arithmetic, Comparison, Increment);
  35. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, MemoryAddress, Arithmetic, Comparison, Increment);
  36. // FIXME: These should probably be made generic/virtual if/when we decide to do something more
  37. // fancy than just a dumb interpreter.
  38. class Reference {
  39. public:
  40. struct Null {
  41. ValueType type;
  42. };
  43. struct Func {
  44. FunctionAddress address;
  45. };
  46. struct Extern {
  47. ExternAddress address;
  48. };
  49. using RefType = Variant<Null, Func, Extern>;
  50. explicit Reference(RefType ref)
  51. : m_ref(move(ref))
  52. {
  53. }
  54. auto& ref() const { return m_ref; }
  55. private:
  56. RefType m_ref;
  57. };
  58. class Value {
  59. public:
  60. Value()
  61. : m_value(0)
  62. {
  63. }
  64. using AnyValueType = Variant<i32, i64, float, double, Reference>;
  65. explicit Value(AnyValueType value)
  66. : m_value(move(value))
  67. {
  68. }
  69. template<typename T>
  70. requires(sizeof(T) == sizeof(u64)) explicit Value(ValueType type, T raw_value)
  71. : m_value(0)
  72. {
  73. switch (type.kind()) {
  74. case ValueType::Kind::ExternReference:
  75. m_value = Reference { Reference::Extern { { bit_cast<u64>(raw_value) } } };
  76. break;
  77. case ValueType::Kind::FunctionReference:
  78. m_value = Reference { Reference::Func { { bit_cast<u64>(raw_value) } } };
  79. break;
  80. case ValueType::Kind::I32:
  81. m_value = static_cast<i32>(bit_cast<i64>(raw_value));
  82. break;
  83. case ValueType::Kind::I64:
  84. m_value = static_cast<i64>(bit_cast<u64>(raw_value));
  85. break;
  86. case ValueType::Kind::F32:
  87. m_value = static_cast<float>(bit_cast<double>(raw_value));
  88. break;
  89. case ValueType::Kind::F64:
  90. m_value = bit_cast<double>(raw_value);
  91. break;
  92. case ValueType::Kind::NullFunctionReference:
  93. VERIFY(raw_value == 0);
  94. m_value = Reference { Reference::Null { ValueType(ValueType::Kind::FunctionReference) } };
  95. break;
  96. case ValueType::Kind::NullExternReference:
  97. VERIFY(raw_value == 0);
  98. m_value = Reference { Reference::Null { ValueType(ValueType::Kind::ExternReference) } };
  99. break;
  100. default:
  101. VERIFY_NOT_REACHED();
  102. }
  103. }
  104. ALWAYS_INLINE Value(Value const& value) = default;
  105. ALWAYS_INLINE Value(Value&& value) = default;
  106. ALWAYS_INLINE Value& operator=(Value&& value) = default;
  107. ALWAYS_INLINE Value& operator=(Value const& value) = default;
  108. template<typename T>
  109. ALWAYS_INLINE Optional<T> to() const
  110. {
  111. Optional<T> result;
  112. m_value.visit(
  113. [&](auto value) {
  114. if constexpr (IsSame<T, decltype(value)> || (!IsFloatingPoint<T> && IsSame<decltype(value), MakeSigned<T>>)) {
  115. result = static_cast<T>(value);
  116. } else if constexpr (!IsFloatingPoint<T> && IsConvertible<decltype(value), T>) {
  117. if (AK::is_within_range<T>(value))
  118. result = static_cast<T>(value);
  119. }
  120. },
  121. [&](Reference const& value) {
  122. if constexpr (IsSame<T, Reference>) {
  123. result = value;
  124. } else if constexpr (IsSame<T, Reference::Func>) {
  125. if (auto ptr = value.ref().template get_pointer<Reference::Func>())
  126. result = *ptr;
  127. } else if constexpr (IsSame<T, Reference::Extern>) {
  128. if (auto ptr = value.ref().template get_pointer<Reference::Extern>())
  129. result = *ptr;
  130. } else if constexpr (IsSame<T, Reference::Null>) {
  131. if (auto ptr = value.ref().template get_pointer<Reference::Null>())
  132. result = *ptr;
  133. }
  134. });
  135. return result;
  136. }
  137. ValueType type() const
  138. {
  139. return ValueType(m_value.visit(
  140. [](i32) { return ValueType::Kind::I32; },
  141. [](i64) { return ValueType::Kind::I64; },
  142. [](float) { return ValueType::Kind::F32; },
  143. [](double) { return ValueType::Kind::F64; },
  144. [&](Reference const& type) {
  145. return type.ref().visit(
  146. [](Reference::Func const&) { return ValueType::Kind::FunctionReference; },
  147. [](Reference::Null const& null_type) {
  148. return null_type.type.kind() == ValueType::ExternReference ? ValueType::Kind::NullExternReference : ValueType::Kind::NullFunctionReference;
  149. },
  150. [](Reference::Extern const&) { return ValueType::Kind::ExternReference; });
  151. }));
  152. }
  153. auto& value() const { return m_value; }
  154. private:
  155. AnyValueType m_value;
  156. };
  157. struct Trap {
  158. DeprecatedString reason;
  159. };
  160. // A variant of Result that does not include external reasons for error (JS::Completion, for now).
  161. class PureResult {
  162. public:
  163. explicit PureResult(Vector<Value> values)
  164. : m_result(move(values))
  165. {
  166. }
  167. PureResult(Trap trap)
  168. : m_result(move(trap))
  169. {
  170. }
  171. auto is_trap() const { return m_result.has<Trap>(); }
  172. auto& values() const { return m_result.get<Vector<Value>>(); }
  173. auto& values() { return m_result.get<Vector<Value>>(); }
  174. auto& trap() const { return m_result.get<Trap>(); }
  175. auto& trap() { return m_result.get<Trap>(); }
  176. private:
  177. friend class Result;
  178. explicit PureResult(Variant<Vector<Value>, Trap>&& result)
  179. : m_result(move(result))
  180. {
  181. }
  182. Variant<Vector<Value>, Trap> m_result;
  183. };
  184. class Result {
  185. public:
  186. explicit Result(Vector<Value> values)
  187. : m_result(move(values))
  188. {
  189. }
  190. Result(Trap trap)
  191. : m_result(move(trap))
  192. {
  193. }
  194. Result(JS::Completion completion)
  195. : m_result(move(completion))
  196. {
  197. VERIFY(m_result.get<JS::Completion>().is_abrupt());
  198. }
  199. Result(PureResult&& result)
  200. : m_result(result.m_result.downcast<decltype(m_result)>())
  201. {
  202. }
  203. auto is_trap() const { return m_result.has<Trap>(); }
  204. auto is_completion() const { return m_result.has<JS::Completion>(); }
  205. auto& values() const { return m_result.get<Vector<Value>>(); }
  206. auto& values() { return m_result.get<Vector<Value>>(); }
  207. auto& trap() const { return m_result.get<Trap>(); }
  208. auto& trap() { return m_result.get<Trap>(); }
  209. auto& completion() { return m_result.get<JS::Completion>(); }
  210. auto& completion() const { return m_result.get<JS::Completion>(); }
  211. PureResult assert_wasm_result() &&
  212. {
  213. VERIFY(!is_completion());
  214. return PureResult(move(m_result).downcast<Vector<Value>, Trap>());
  215. }
  216. private:
  217. Variant<Vector<Value>, Trap, JS::Completion> m_result;
  218. };
  219. using ExternValue = Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>;
  220. class ExportInstance {
  221. public:
  222. explicit ExportInstance(DeprecatedString name, ExternValue value)
  223. : m_name(move(name))
  224. , m_value(move(value))
  225. {
  226. }
  227. auto& name() const { return m_name; }
  228. auto& value() const { return m_value; }
  229. private:
  230. DeprecatedString m_name;
  231. ExternValue m_value;
  232. };
  233. class ModuleInstance {
  234. public:
  235. explicit ModuleInstance(
  236. Vector<FunctionType> types, Vector<FunctionAddress> function_addresses, Vector<TableAddress> table_addresses,
  237. Vector<MemoryAddress> memory_addresses, Vector<GlobalAddress> global_addresses, Vector<DataAddress> data_addresses,
  238. Vector<ExportInstance> exports)
  239. : m_types(move(types))
  240. , m_functions(move(function_addresses))
  241. , m_tables(move(table_addresses))
  242. , m_memories(move(memory_addresses))
  243. , m_globals(move(global_addresses))
  244. , m_datas(move(data_addresses))
  245. , m_exports(move(exports))
  246. {
  247. }
  248. ModuleInstance() = default;
  249. auto& types() const { return m_types; }
  250. auto& functions() const { return m_functions; }
  251. auto& tables() const { return m_tables; }
  252. auto& memories() const { return m_memories; }
  253. auto& globals() const { return m_globals; }
  254. auto& elements() const { return m_elements; }
  255. auto& datas() const { return m_datas; }
  256. auto& exports() const { return m_exports; }
  257. auto& types() { return m_types; }
  258. auto& functions() { return m_functions; }
  259. auto& tables() { return m_tables; }
  260. auto& memories() { return m_memories; }
  261. auto& globals() { return m_globals; }
  262. auto& elements() { return m_elements; }
  263. auto& datas() { return m_datas; }
  264. auto& exports() { return m_exports; }
  265. private:
  266. Vector<FunctionType> m_types;
  267. Vector<FunctionAddress> m_functions;
  268. Vector<TableAddress> m_tables;
  269. Vector<MemoryAddress> m_memories;
  270. Vector<GlobalAddress> m_globals;
  271. Vector<ElementAddress> m_elements;
  272. Vector<DataAddress> m_datas;
  273. Vector<ExportInstance> m_exports;
  274. };
  275. class WasmFunction {
  276. public:
  277. explicit WasmFunction(FunctionType const& type, ModuleInstance const& module, Module::Function const& code)
  278. : m_type(type)
  279. , m_module(module)
  280. , m_code(code)
  281. {
  282. }
  283. auto& type() const { return m_type; }
  284. auto& module() const { return m_module; }
  285. auto& code() const { return m_code; }
  286. private:
  287. FunctionType m_type;
  288. ModuleInstance const& m_module;
  289. Module::Function const& m_code;
  290. };
  291. class HostFunction {
  292. public:
  293. explicit HostFunction(AK::Function<Result(Configuration&, Vector<Value>&)> function, FunctionType const& type)
  294. : m_function(move(function))
  295. , m_type(type)
  296. {
  297. }
  298. auto& function() { return m_function; }
  299. auto& type() const { return m_type; }
  300. private:
  301. AK::Function<Result(Configuration&, Vector<Value>&)> m_function;
  302. FunctionType m_type;
  303. };
  304. using FunctionInstance = Variant<WasmFunction, HostFunction>;
  305. class TableInstance {
  306. public:
  307. explicit TableInstance(TableType const& type, Vector<Optional<Reference>> elements)
  308. : m_elements(move(elements))
  309. , m_type(type)
  310. {
  311. }
  312. auto& elements() const { return m_elements; }
  313. auto& elements() { return m_elements; }
  314. auto& type() const { return m_type; }
  315. bool grow(size_t size_to_grow, Reference const& fill_value)
  316. {
  317. if (size_to_grow == 0)
  318. return true;
  319. auto new_size = m_elements.size() + size_to_grow;
  320. if (auto max = m_type.limits().max(); max.has_value()) {
  321. if (max.value() < new_size)
  322. return false;
  323. }
  324. auto previous_size = m_elements.size();
  325. if (m_elements.try_resize(new_size).is_error())
  326. return false;
  327. for (size_t i = previous_size; i < m_elements.size(); ++i)
  328. m_elements[i] = fill_value;
  329. return true;
  330. }
  331. private:
  332. Vector<Optional<Reference>> m_elements;
  333. TableType const& m_type;
  334. };
  335. class MemoryInstance {
  336. public:
  337. static ErrorOr<MemoryInstance> create(MemoryType const& type)
  338. {
  339. MemoryInstance instance { type };
  340. if (!instance.grow(type.limits().min() * Constants::page_size))
  341. return Error::from_string_literal("Failed to grow to requested size");
  342. return { move(instance) };
  343. }
  344. auto& type() const { return m_type; }
  345. auto size() const { return m_size; }
  346. auto& data() const { return m_data; }
  347. auto& data() { return m_data; }
  348. enum class InhibitGrowCallback {
  349. No,
  350. Yes,
  351. };
  352. bool grow(size_t size_to_grow, InhibitGrowCallback inhibit_callback = InhibitGrowCallback::No)
  353. {
  354. if (size_to_grow == 0)
  355. return true;
  356. u64 new_size = m_data.size() + size_to_grow;
  357. // Can't grow past 2^16 pages.
  358. if (new_size >= Constants::page_size * 65536)
  359. return false;
  360. if (auto max = m_type.limits().max(); max.has_value()) {
  361. if (max.value() * Constants::page_size < new_size)
  362. return false;
  363. }
  364. auto previous_size = m_size;
  365. if (m_data.try_resize(new_size).is_error())
  366. return false;
  367. m_size = new_size;
  368. // The spec requires that we zero out everything on grow
  369. __builtin_memset(m_data.offset_pointer(previous_size), 0, size_to_grow);
  370. // NOTE: This exists because wasm-js-api wants to execute code after a successful grow,
  371. // See [this issue](https://github.com/WebAssembly/spec/issues/1635) for more details.
  372. if (inhibit_callback == InhibitGrowCallback::No && successful_grow_hook)
  373. successful_grow_hook();
  374. return true;
  375. }
  376. Function<void()> successful_grow_hook;
  377. private:
  378. explicit MemoryInstance(MemoryType const& type)
  379. : m_type(type)
  380. {
  381. }
  382. MemoryType const& m_type;
  383. size_t m_size { 0 };
  384. ByteBuffer m_data;
  385. };
  386. class GlobalInstance {
  387. public:
  388. explicit GlobalInstance(Value value, bool is_mutable)
  389. : m_mutable(is_mutable)
  390. , m_value(move(value))
  391. {
  392. }
  393. auto is_mutable() const { return m_mutable; }
  394. auto& value() const { return m_value; }
  395. GlobalType type() const { return { m_value.type(), is_mutable() }; }
  396. void set_value(Value value)
  397. {
  398. VERIFY(is_mutable());
  399. m_value = move(value);
  400. }
  401. private:
  402. bool m_mutable { false };
  403. Value m_value;
  404. };
  405. class DataInstance {
  406. public:
  407. explicit DataInstance(Vector<u8> data)
  408. : m_data(move(data))
  409. {
  410. }
  411. size_t size() const { return m_data.size(); }
  412. Vector<u8>& data() { return m_data; }
  413. Vector<u8> const& data() const { return m_data; }
  414. private:
  415. Vector<u8> m_data;
  416. };
  417. class ElementInstance {
  418. public:
  419. explicit ElementInstance(ValueType type, Vector<Reference> references)
  420. : m_type(move(type))
  421. , m_references(move(references))
  422. {
  423. }
  424. auto& type() const { return m_type; }
  425. auto& references() const { return m_references; }
  426. private:
  427. ValueType m_type;
  428. Vector<Reference> m_references;
  429. };
  430. class Store {
  431. public:
  432. Store() = default;
  433. Optional<FunctionAddress> allocate(ModuleInstance& module, Module::Function const& function);
  434. Optional<FunctionAddress> allocate(HostFunction&&);
  435. Optional<TableAddress> allocate(TableType const&);
  436. Optional<MemoryAddress> allocate(MemoryType const&);
  437. Optional<DataAddress> allocate_data(Vector<u8>);
  438. Optional<GlobalAddress> allocate(GlobalType const&, Value);
  439. Optional<ElementAddress> allocate(ValueType const&, Vector<Reference>);
  440. FunctionInstance* get(FunctionAddress);
  441. TableInstance* get(TableAddress);
  442. MemoryInstance* get(MemoryAddress);
  443. GlobalInstance* get(GlobalAddress);
  444. DataInstance* get(DataAddress);
  445. ElementInstance* get(ElementAddress);
  446. private:
  447. Vector<FunctionInstance> m_functions;
  448. Vector<TableInstance> m_tables;
  449. Vector<MemoryInstance> m_memories;
  450. Vector<GlobalInstance> m_globals;
  451. Vector<ElementInstance> m_elements;
  452. Vector<DataInstance> m_datas;
  453. };
  454. class Label {
  455. public:
  456. explicit Label(size_t arity, InstructionPointer continuation)
  457. : m_arity(arity)
  458. , m_continuation(continuation)
  459. {
  460. }
  461. auto continuation() const { return m_continuation; }
  462. auto arity() const { return m_arity; }
  463. private:
  464. size_t m_arity { 0 };
  465. InstructionPointer m_continuation { 0 };
  466. };
  467. class Frame {
  468. public:
  469. explicit Frame(ModuleInstance const& module, Vector<Value> locals, Expression const& expression, size_t arity)
  470. : m_module(module)
  471. , m_locals(move(locals))
  472. , m_expression(expression)
  473. , m_arity(arity)
  474. {
  475. }
  476. auto& module() const { return m_module; }
  477. auto& locals() const { return m_locals; }
  478. auto& locals() { return m_locals; }
  479. auto& expression() const { return m_expression; }
  480. auto arity() const { return m_arity; }
  481. private:
  482. ModuleInstance const& m_module;
  483. Vector<Value> m_locals;
  484. Expression const& m_expression;
  485. size_t m_arity { 0 };
  486. };
  487. class Stack {
  488. public:
  489. using EntryType = Variant<Value, Label, Frame>;
  490. Stack() = default;
  491. [[nodiscard]] ALWAYS_INLINE bool is_empty() const { return m_data.is_empty(); }
  492. ALWAYS_INLINE void push(EntryType entry) { m_data.append(move(entry)); }
  493. ALWAYS_INLINE auto pop() { return m_data.take_last(); }
  494. ALWAYS_INLINE auto& peek() const { return m_data.last(); }
  495. ALWAYS_INLINE auto& peek() { return m_data.last(); }
  496. ALWAYS_INLINE auto size() const { return m_data.size(); }
  497. ALWAYS_INLINE auto& entries() const { return m_data; }
  498. ALWAYS_INLINE auto& entries() { return m_data; }
  499. private:
  500. Vector<EntryType, 1024> m_data;
  501. };
  502. using InstantiationResult = AK::Result<NonnullOwnPtr<ModuleInstance>, InstantiationError>;
  503. class AbstractMachine {
  504. public:
  505. explicit AbstractMachine() = default;
  506. // Validate a module; permanently sets the module's validity status.
  507. ErrorOr<void, ValidationError> validate(Module&);
  508. // Load and instantiate a module, and link it into this interpreter.
  509. InstantiationResult instantiate(Module const&, Vector<ExternValue>);
  510. Result invoke(FunctionAddress, Vector<Value>);
  511. Result invoke(Interpreter&, FunctionAddress, Vector<Value>);
  512. auto& store() const { return m_store; }
  513. auto& store() { return m_store; }
  514. void enable_instruction_count_limit() { m_should_limit_instruction_count = true; }
  515. private:
  516. Optional<InstantiationError> allocate_all_initial_phase(Module const&, ModuleInstance&, Vector<ExternValue>&, Vector<Value>& global_values);
  517. Optional<InstantiationError> allocate_all_final_phase(Module const&, ModuleInstance&, Vector<Vector<Reference>>& elements);
  518. Store m_store;
  519. StackInfo m_stack_info;
  520. bool m_should_limit_instruction_count { false };
  521. };
  522. class Linker {
  523. public:
  524. struct Name {
  525. DeprecatedString module;
  526. DeprecatedString name;
  527. ImportSection::Import::ImportDesc type;
  528. };
  529. explicit Linker(Module const& module)
  530. : m_module(module)
  531. {
  532. }
  533. // Link a module, the import 'module name' is ignored with this.
  534. void link(ModuleInstance const&);
  535. // Link a bunch of qualified values, also matches 'module name'.
  536. void link(HashMap<Name, ExternValue> const&);
  537. auto& unresolved_imports()
  538. {
  539. populate();
  540. return m_unresolved_imports;
  541. }
  542. AK::Result<Vector<ExternValue>, LinkError> finish();
  543. private:
  544. void populate();
  545. Module const& m_module;
  546. HashMap<Name, ExternValue> m_resolved_imports;
  547. HashTable<Name> m_unresolved_imports;
  548. Vector<Name> m_ordered_imports;
  549. Optional<LinkError> m_error;
  550. };
  551. }
  552. template<>
  553. struct AK::Traits<Wasm::Linker::Name> : public AK::GenericTraits<Wasm::Linker::Name> {
  554. static constexpr bool is_trivial() { return false; }
  555. static unsigned hash(Wasm::Linker::Name const& entry) { return pair_int_hash(entry.module.hash(), entry.name.hash()); }
  556. static bool equals(Wasm::Linker::Name const& a, Wasm::Linker::Name const& b) { return a.name == b.name && a.module == b.module; }
  557. };