AbstractMachine.h 16 KB

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