AbstractMachine.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 <LibWasm/Types.h>
  13. namespace Wasm {
  14. class Configuration;
  15. struct Interpreter;
  16. struct InstantiationError {
  17. String error { "Unknown error" };
  18. };
  19. struct LinkError {
  20. enum OtherErrors {
  21. InvalidImportedModule,
  22. };
  23. Vector<String> missing_imports;
  24. Vector<OtherErrors> other_errors;
  25. };
  26. TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, FunctionAddress);
  27. TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, ExternAddress);
  28. TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, TableAddress);
  29. TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, GlobalAddress);
  30. TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, ElementAddress);
  31. TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, MemoryAddress);
  32. // FIXME: These should probably be made generic/virtual if/when we decide to do something more
  33. // fancy than just a dumb interpreter.
  34. class Reference {
  35. public:
  36. struct Null {
  37. ValueType type;
  38. };
  39. struct Func {
  40. FunctionAddress address;
  41. };
  42. struct Extern {
  43. ExternAddress address;
  44. };
  45. using RefType = Variant<Null, Func, Extern>;
  46. explicit Reference(RefType ref)
  47. : m_ref(move(ref))
  48. {
  49. }
  50. auto& ref() const { return m_ref; }
  51. private:
  52. RefType m_ref;
  53. };
  54. class Value {
  55. public:
  56. Value()
  57. : m_value(0)
  58. {
  59. }
  60. using AnyValueType = Variant<i32, i64, float, double, Reference>;
  61. explicit Value(AnyValueType value)
  62. : m_value(move(value))
  63. {
  64. }
  65. template<typename T>
  66. requires(sizeof(T) == sizeof(u64)) explicit Value(ValueType type, T raw_value)
  67. : m_value(0)
  68. {
  69. switch (type.kind()) {
  70. case ValueType::Kind::ExternReference:
  71. m_value = Reference { Reference::Extern { { bit_cast<u64>(raw_value) } } };
  72. break;
  73. case ValueType::Kind::FunctionReference:
  74. m_value = Reference { Reference::Func { { bit_cast<u64>(raw_value) } } };
  75. break;
  76. case ValueType::Kind::I32:
  77. m_value = static_cast<i32>(bit_cast<i64>(raw_value));
  78. break;
  79. case ValueType::Kind::I64:
  80. m_value = static_cast<i64>(bit_cast<u64>(raw_value));
  81. break;
  82. case ValueType::Kind::F32:
  83. m_value = static_cast<float>(bit_cast<double>(raw_value));
  84. break;
  85. case ValueType::Kind::F64:
  86. m_value = bit_cast<double>(raw_value);
  87. break;
  88. case ValueType::Kind::NullFunctionReference:
  89. VERIFY(raw_value == 0);
  90. m_value = Reference { Reference::Null { ValueType(ValueType::Kind::FunctionReference) } };
  91. break;
  92. case ValueType::Kind::NullExternReference:
  93. VERIFY(raw_value == 0);
  94. m_value = Reference { Reference::Null { ValueType(ValueType::Kind::ExternReference) } };
  95. break;
  96. default:
  97. VERIFY_NOT_REACHED();
  98. }
  99. }
  100. ALWAYS_INLINE Value(Value const& value) = default;
  101. ALWAYS_INLINE Value(Value&& value) = default;
  102. ALWAYS_INLINE Value& operator=(Value&& value) = default;
  103. ALWAYS_INLINE Value& operator=(Value const& value) = default;
  104. template<typename T>
  105. ALWAYS_INLINE Optional<T> to()
  106. {
  107. Optional<T> result;
  108. m_value.visit(
  109. [&](auto value) {
  110. if constexpr (IsSame<T, decltype(value)>)
  111. result = value;
  112. else if constexpr (!IsFloatingPoint<T> && IsSame<decltype(value), MakeSigned<T>>)
  113. result = value;
  114. },
  115. [&](Reference const& value) {
  116. if constexpr (IsSame<T, Reference>) {
  117. result = value;
  118. } else if constexpr (IsSame<T, Reference::Func>) {
  119. if (auto ptr = value.ref().template get_pointer<Reference::Func>())
  120. result = *ptr;
  121. } else if constexpr (IsSame<T, Reference::Extern>) {
  122. if (auto ptr = value.ref().template get_pointer<Reference::Extern>())
  123. result = *ptr;
  124. } else if constexpr (IsSame<T, Reference::Null>) {
  125. if (auto ptr = value.ref().template get_pointer<Reference::Null>())
  126. result = *ptr;
  127. }
  128. });
  129. return result;
  130. }
  131. ValueType type() const
  132. {
  133. return ValueType(m_value.visit(
  134. [](i32) { return ValueType::Kind::I32; },
  135. [](i64) { return ValueType::Kind::I64; },
  136. [](float) { return ValueType::Kind::F32; },
  137. [](double) { return ValueType::Kind::F64; },
  138. [&](Reference const& type) {
  139. return type.ref().visit(
  140. [](Reference::Func const&) { return ValueType::Kind::FunctionReference; },
  141. [](Reference::Null const& null_type) {
  142. return null_type.type.kind() == ValueType::ExternReference ? ValueType::Kind::NullExternReference : ValueType::Kind::NullFunctionReference;
  143. },
  144. [](Reference::Extern const&) { return ValueType::Kind::ExternReference; });
  145. }));
  146. }
  147. auto& value() const { return m_value; }
  148. private:
  149. AnyValueType m_value;
  150. };
  151. struct Trap {
  152. String reason;
  153. };
  154. class Result {
  155. public:
  156. explicit Result(Vector<Value> values)
  157. : m_result(move(values))
  158. {
  159. }
  160. Result(Trap trap)
  161. : m_result(move(trap))
  162. {
  163. }
  164. auto is_trap() const { return m_result.has<Trap>(); }
  165. auto& values() const { return m_result.get<Vector<Value>>(); }
  166. auto& values() { return m_result.get<Vector<Value>>(); }
  167. auto& trap() const { return m_result.get<Trap>(); }
  168. auto& trap() { return m_result.get<Trap>(); }
  169. private:
  170. Variant<Vector<Value>, Trap> m_result;
  171. };
  172. using ExternValue = Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>;
  173. class ExportInstance {
  174. public:
  175. explicit ExportInstance(String name, ExternValue value)
  176. : m_name(move(name))
  177. , m_value(move(value))
  178. {
  179. }
  180. auto& name() const { return m_name; }
  181. auto& value() const { return m_value; }
  182. private:
  183. String m_name;
  184. ExternValue m_value;
  185. };
  186. class ModuleInstance {
  187. public:
  188. explicit ModuleInstance(
  189. Vector<FunctionType> types, Vector<FunctionAddress> function_addresses, Vector<TableAddress> table_addresses,
  190. Vector<MemoryAddress> memory_addresses, Vector<GlobalAddress> global_addresses, Vector<ExportInstance> exports)
  191. : m_types(move(types))
  192. , m_functions(move(function_addresses))
  193. , m_tables(move(table_addresses))
  194. , m_memories(move(memory_addresses))
  195. , m_globals(move(global_addresses))
  196. , m_exports(move(exports))
  197. {
  198. }
  199. ModuleInstance() = default;
  200. auto& types() const { return m_types; }
  201. auto& functions() const { return m_functions; }
  202. auto& tables() const { return m_tables; }
  203. auto& memories() const { return m_memories; }
  204. auto& globals() const { return m_globals; }
  205. auto& elements() const { return m_elements; }
  206. auto& exports() const { return m_exports; }
  207. auto& types() { return m_types; }
  208. auto& functions() { return m_functions; }
  209. auto& tables() { return m_tables; }
  210. auto& memories() { return m_memories; }
  211. auto& globals() { return m_globals; }
  212. auto& elements() { return m_elements; }
  213. auto& exports() { return m_exports; }
  214. private:
  215. Vector<FunctionType> m_types;
  216. Vector<FunctionAddress> m_functions;
  217. Vector<TableAddress> m_tables;
  218. Vector<MemoryAddress> m_memories;
  219. Vector<GlobalAddress> m_globals;
  220. Vector<ElementAddress> m_elements;
  221. Vector<ExportInstance> m_exports;
  222. };
  223. class WasmFunction {
  224. public:
  225. explicit WasmFunction(FunctionType const& type, ModuleInstance const& module, Module::Function const& code)
  226. : m_type(type)
  227. , m_module(module)
  228. , m_code(code)
  229. {
  230. }
  231. auto& type() const { return m_type; }
  232. auto& module() const { return m_module; }
  233. auto& code() const { return m_code; }
  234. private:
  235. FunctionType m_type;
  236. ModuleInstance const& m_module;
  237. Module::Function const& m_code;
  238. };
  239. class HostFunction {
  240. public:
  241. explicit HostFunction(AK::Function<Result(Configuration&, Vector<Value>&)> function, FunctionType const& type)
  242. : m_function(move(function))
  243. , m_type(type)
  244. {
  245. }
  246. auto& function() { return m_function; }
  247. auto& type() const { return m_type; }
  248. private:
  249. AK::Function<Result(Configuration&, Vector<Value>&)> m_function;
  250. FunctionType m_type;
  251. };
  252. using FunctionInstance = Variant<WasmFunction, HostFunction>;
  253. class TableInstance {
  254. public:
  255. explicit TableInstance(TableType const& type, Vector<Optional<Reference>> elements)
  256. : m_elements(move(elements))
  257. , m_type(type)
  258. {
  259. }
  260. auto& elements() const { return m_elements; }
  261. auto& elements() { return m_elements; }
  262. auto& type() const { return m_type; }
  263. private:
  264. Vector<Optional<Reference>> m_elements;
  265. TableType const& m_type;
  266. };
  267. class MemoryInstance {
  268. public:
  269. explicit MemoryInstance(MemoryType const& type)
  270. : m_type(type)
  271. {
  272. grow(m_type.limits().min() * Constants::page_size);
  273. }
  274. auto& type() const { return m_type; }
  275. auto size() const { return m_size; }
  276. auto& data() const { return m_data; }
  277. auto& data() { return m_data; }
  278. bool grow(size_t size_to_grow)
  279. {
  280. if (size_to_grow == 0)
  281. return true;
  282. auto new_size = m_data.size() + size_to_grow;
  283. // Can't grow past 2^16 pages.
  284. if (new_size >= Constants::page_size * 65536)
  285. return false;
  286. if (auto max = m_type.limits().max(); max.has_value()) {
  287. if (max.value() * Constants::page_size < new_size)
  288. return false;
  289. }
  290. auto previous_size = m_size;
  291. m_data.resize(new_size);
  292. m_size = new_size;
  293. // The spec requires that we zero out everything on grow
  294. __builtin_memset(m_data.offset_pointer(previous_size), 0, size_to_grow);
  295. return true;
  296. }
  297. private:
  298. MemoryType const& m_type;
  299. size_t m_size { 0 };
  300. ByteBuffer m_data;
  301. };
  302. class GlobalInstance {
  303. public:
  304. explicit GlobalInstance(Value value, bool is_mutable)
  305. : m_mutable(is_mutable)
  306. , m_value(move(value))
  307. {
  308. }
  309. auto is_mutable() const { return m_mutable; }
  310. auto& value() const { return m_value; }
  311. void set_value(Value value)
  312. {
  313. VERIFY(is_mutable());
  314. m_value = move(value);
  315. }
  316. private:
  317. bool m_mutable { false };
  318. Value m_value;
  319. };
  320. class ElementInstance {
  321. public:
  322. explicit ElementInstance(ValueType type, Vector<Reference> references)
  323. : m_type(move(type))
  324. , m_references(move(references))
  325. {
  326. }
  327. auto& type() const { return m_type; }
  328. auto& references() const { return m_references; }
  329. private:
  330. ValueType m_type;
  331. Vector<Reference> m_references;
  332. };
  333. class Store {
  334. public:
  335. Store() = default;
  336. Optional<FunctionAddress> allocate(ModuleInstance& module, Module::Function const& function);
  337. Optional<FunctionAddress> allocate(HostFunction&&);
  338. Optional<TableAddress> allocate(TableType const&);
  339. Optional<MemoryAddress> allocate(MemoryType const&);
  340. Optional<GlobalAddress> allocate(GlobalType const&, Value);
  341. Optional<ElementAddress> allocate(ValueType const&, Vector<Reference>);
  342. FunctionInstance* get(FunctionAddress);
  343. TableInstance* get(TableAddress);
  344. MemoryInstance* get(MemoryAddress);
  345. GlobalInstance* get(GlobalAddress);
  346. ElementInstance* get(ElementAddress);
  347. private:
  348. Vector<FunctionInstance> m_functions;
  349. Vector<TableInstance> m_tables;
  350. Vector<MemoryInstance> m_memories;
  351. Vector<GlobalInstance> m_globals;
  352. Vector<ElementInstance> m_elements;
  353. };
  354. class Label {
  355. public:
  356. explicit Label(size_t arity, InstructionPointer continuation)
  357. : m_arity(arity)
  358. , m_continuation(continuation)
  359. {
  360. }
  361. auto continuation() const { return m_continuation; }
  362. auto arity() const { return m_arity; }
  363. private:
  364. size_t m_arity { 0 };
  365. InstructionPointer m_continuation { 0 };
  366. };
  367. class Frame {
  368. public:
  369. explicit Frame(ModuleInstance const& module, Vector<Value> locals, Expression const& expression, size_t arity)
  370. : m_module(module)
  371. , m_locals(move(locals))
  372. , m_expression(expression)
  373. , m_arity(arity)
  374. {
  375. }
  376. auto& module() const { return m_module; }
  377. auto& locals() const { return m_locals; }
  378. auto& locals() { return m_locals; }
  379. auto& expression() const { return m_expression; }
  380. auto arity() const { return m_arity; }
  381. private:
  382. ModuleInstance const& m_module;
  383. Vector<Value> m_locals;
  384. Expression const& m_expression;
  385. size_t m_arity { 0 };
  386. };
  387. class Stack {
  388. public:
  389. using EntryType = Variant<Value, Label, Frame>;
  390. Stack() = default;
  391. [[nodiscard]] ALWAYS_INLINE bool is_empty() const { return m_data.is_empty(); }
  392. ALWAYS_INLINE void push(EntryType entry) { m_data.append(move(entry)); }
  393. ALWAYS_INLINE auto pop() { return m_data.take_last(); }
  394. ALWAYS_INLINE auto& peek() const { return m_data.last(); }
  395. ALWAYS_INLINE auto& peek() { return m_data.last(); }
  396. ALWAYS_INLINE auto size() const { return m_data.size(); }
  397. ALWAYS_INLINE auto& entries() const { return m_data; }
  398. ALWAYS_INLINE auto& entries() { return m_data; }
  399. private:
  400. Vector<EntryType, 1024> m_data;
  401. };
  402. using InstantiationResult = AK::Result<NonnullOwnPtr<ModuleInstance>, InstantiationError>;
  403. class AbstractMachine {
  404. public:
  405. explicit AbstractMachine() = default;
  406. // Load and instantiate a module, and link it into this interpreter.
  407. InstantiationResult instantiate(Module const&, Vector<ExternValue>);
  408. Result invoke(FunctionAddress, Vector<Value>);
  409. Result invoke(Interpreter&, FunctionAddress, Vector<Value>);
  410. auto& store() const { return m_store; }
  411. auto& store() { return m_store; }
  412. void enable_instruction_count_limit() { m_should_limit_instruction_count = true; }
  413. private:
  414. Optional<InstantiationError> allocate_all_initial_phase(Module const&, ModuleInstance&, Vector<ExternValue>&, Vector<Value>& global_values);
  415. Optional<InstantiationError> allocate_all_final_phase(Module const&, ModuleInstance&, Vector<Vector<Reference>>& elements);
  416. Store m_store;
  417. bool m_should_limit_instruction_count { false };
  418. };
  419. class Linker {
  420. public:
  421. struct Name {
  422. String module;
  423. String name;
  424. ImportSection::Import::ImportDesc type;
  425. };
  426. explicit Linker(Module const& module)
  427. : m_module(module)
  428. {
  429. }
  430. // Link a module, the import 'module name' is ignored with this.
  431. void link(ModuleInstance const&);
  432. // Link a bunch of qualified values, also matches 'module name'.
  433. void link(HashMap<Name, ExternValue> const&);
  434. auto& unresolved_imports()
  435. {
  436. populate();
  437. return m_unresolved_imports;
  438. }
  439. AK::Result<Vector<ExternValue>, LinkError> finish();
  440. private:
  441. void populate();
  442. Module const& m_module;
  443. HashMap<Name, ExternValue> m_resolved_imports;
  444. HashTable<Name> m_unresolved_imports;
  445. Vector<Name> m_ordered_imports;
  446. Optional<LinkError> m_error;
  447. };
  448. }
  449. template<>
  450. struct AK::Traits<Wasm::Linker::Name> : public AK::GenericTraits<Wasm::Linker::Name> {
  451. static constexpr bool is_trivial() { return false; }
  452. static unsigned hash(Wasm::Linker::Name const& entry) { return pair_int_hash(entry.module.hash(), entry.name.hash()); }
  453. static bool equals(Wasm::Linker::Name const& a, Wasm::Linker::Name const& b) { return a.name == b.name && a.module == b.module; }
  454. };