Interpreter.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  8. #include <LibWasm/AbstractMachine/Configuration.h>
  9. #include <LibWasm/AbstractMachine/Interpreter.h>
  10. #include <LibWasm/Opcode.h>
  11. #include <LibWasm/Printer/Printer.h>
  12. #include <math.h>
  13. namespace Wasm {
  14. #define TRAP_IF_NOT(x) \
  15. do { \
  16. if (trap_if_not(x)) \
  17. return; \
  18. } while (false)
  19. void Interpreter::interpret(Configuration& configuration)
  20. {
  21. auto& instructions = configuration.frame()->expression().instructions();
  22. auto max_ip_value = InstructionPointer { instructions.size() };
  23. auto& current_ip_value = configuration.ip();
  24. while (current_ip_value < max_ip_value) {
  25. auto& instruction = instructions[current_ip_value.value()];
  26. interpret(configuration, current_ip_value, instruction);
  27. if (m_do_trap)
  28. return;
  29. ++current_ip_value;
  30. }
  31. }
  32. void Interpreter::branch_to_label(Configuration& configuration, LabelIndex index)
  33. {
  34. dbgln_if(WASM_TRACE_DEBUG, "Branch to label with index {}...", index.value());
  35. auto label = configuration.nth_label(index.value());
  36. TRAP_IF_NOT(label.has_value());
  37. dbgln_if(WASM_TRACE_DEBUG, "...which is actually IP {}, and has {} result(s)", label->continuation().value(), label->arity());
  38. auto results = pop_values(configuration, label->arity());
  39. size_t drop_count = index.value() + 1;
  40. for (; !configuration.stack().is_empty();) {
  41. auto entry = configuration.stack().pop();
  42. if (entry.has<NonnullOwnPtr<Label>>()) {
  43. if (drop_count-- == 0)
  44. break;
  45. }
  46. }
  47. // Push results in reverse
  48. for (size_t i = results.size(); i > 0; --i)
  49. configuration.stack().push(move(static_cast<Vector<NonnullOwnPtr<Value>>&>(results)[i - 1]));
  50. configuration.ip() = label->continuation() + 1;
  51. }
  52. ReadonlyBytes Interpreter::load_from_memory(Configuration& configuration, const Instruction& instruction, size_t size)
  53. {
  54. auto& address = configuration.frame()->module().memories().first();
  55. auto memory = configuration.store().get(address);
  56. if (!memory) {
  57. m_do_trap = true;
  58. return {};
  59. }
  60. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  61. auto base = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>();
  62. if (!base.has_value()) {
  63. m_do_trap = true;
  64. return {};
  65. }
  66. auto instance_address = base.value() + static_cast<i64>(arg.offset);
  67. if (instance_address < 0 || static_cast<u64>(instance_address + size) > memory->size()) {
  68. m_do_trap = true;
  69. dbgln("LibWasm: Memory access out of bounds (expected 0 > {} and {} > {})", instance_address, instance_address + size, memory->size());
  70. return {};
  71. }
  72. dbgln_if(WASM_TRACE_DEBUG, "load({} : {}) -> stack", instance_address, size);
  73. return memory->data().bytes().slice(instance_address, size);
  74. }
  75. void Interpreter::store_to_memory(Configuration& configuration, const Instruction& instruction, ReadonlyBytes data)
  76. {
  77. auto& address = configuration.frame()->module().memories().first();
  78. auto memory = configuration.store().get(address);
  79. TRAP_IF_NOT(memory);
  80. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  81. auto base = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>();
  82. TRAP_IF_NOT(base.has_value());
  83. auto instance_address = base.value() + static_cast<i64>(arg.offset);
  84. if (instance_address < 0 || static_cast<u64>(instance_address + data.size()) > memory->size()) {
  85. m_do_trap = true;
  86. dbgln("LibWasm: Memory access out of bounds (expected 0 > {} and {} > {})", instance_address, instance_address + data.size(), memory->size());
  87. return;
  88. }
  89. dbgln_if(WASM_TRACE_DEBUG, "tempoaray({}b) -> store({})", data.size(), instance_address);
  90. data.copy_to(memory->data().bytes().slice(instance_address, data.size()));
  91. }
  92. void Interpreter::call_address(Configuration& configuration, FunctionAddress address)
  93. {
  94. auto instance = configuration.store().get(address);
  95. TRAP_IF_NOT(instance);
  96. const FunctionType* type { nullptr };
  97. instance->visit([&](const auto& function) { type = &function.type(); });
  98. TRAP_IF_NOT(type);
  99. Vector<Value> args;
  100. args.ensure_capacity(type->parameters().size());
  101. for (size_t i = 0; i < type->parameters().size(); ++i) {
  102. args.prepend(move(*configuration.stack().pop().get<NonnullOwnPtr<Value>>()));
  103. }
  104. Configuration function_configuration { configuration.store() };
  105. function_configuration.depth() = configuration.depth() + 1;
  106. auto result = function_configuration.call(address, move(args));
  107. if (result.is_trap()) {
  108. m_do_trap = true;
  109. return;
  110. }
  111. for (auto& entry : result.values())
  112. configuration.stack().push(make<Value>(move(entry)));
  113. }
  114. #define BINARY_NUMERIC_OPERATION(type, operator, cast, ...) \
  115. do { \
  116. auto rhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \
  117. auto lhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \
  118. TRAP_IF_NOT(lhs.has_value()); \
  119. TRAP_IF_NOT(rhs.has_value()); \
  120. __VA_ARGS__; \
  121. auto result = lhs.value() operator rhs.value(); \
  122. dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", lhs.value(), #operator, rhs.value(), result); \
  123. configuration.stack().push(make<Value>(cast(result))); \
  124. return; \
  125. } while (false)
  126. #define OVF_CHECKED_BINARY_NUMERIC_OPERATION(type, operator, cast, ...) \
  127. do { \
  128. auto rhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \
  129. auto ulhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \
  130. TRAP_IF_NOT(ulhs.has_value()); \
  131. TRAP_IF_NOT(rhs.has_value()); \
  132. dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = ??", ulhs.value(), #operator, rhs.value()); \
  133. __VA_ARGS__; \
  134. Checked lhs = ulhs.value(); \
  135. lhs operator##= rhs.value(); \
  136. TRAP_IF_NOT(!lhs.has_overflow()); \
  137. auto result = lhs.value(); \
  138. dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", ulhs.value(), #operator, rhs.value(), result); \
  139. configuration.stack().push(make<Value>(cast(result))); \
  140. return; \
  141. } while (false)
  142. #define BINARY_PREFIX_NUMERIC_OPERATION(type, operation, cast, ...) \
  143. do { \
  144. auto rhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \
  145. auto lhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \
  146. TRAP_IF_NOT(lhs.has_value()); \
  147. TRAP_IF_NOT(rhs.has_value()); \
  148. auto result = operation(lhs.value(), rhs.value()); \
  149. dbgln_if(WASM_TRACE_DEBUG, "{}({} {}) = {}", #operation, lhs.value(), rhs.value(), result); \
  150. configuration.stack().push(make<Value>(cast(result))); \
  151. return; \
  152. } while (false)
  153. #define UNARY_MAP(pop_type, operation, ...) \
  154. do { \
  155. auto value = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<pop_type>(); \
  156. TRAP_IF_NOT(value.has_value()); \
  157. auto result = operation(value.value()); \
  158. dbgln_if(WASM_TRACE_DEBUG, "map({}) {} = {}", #operation, value.value(), result); \
  159. configuration.stack().push(make<Value>(__VA_ARGS__(result))); \
  160. return; \
  161. } while (false)
  162. #define UNARY_NUMERIC_OPERATION(type, operation) \
  163. UNARY_MAP(type, operation, type)
  164. #define LOAD_AND_PUSH(read_type, push_type) \
  165. do { \
  166. auto slice = load_from_memory(configuration, instruction, sizeof(read_type)); \
  167. TRAP_IF_NOT(slice.size() == sizeof(read_type)); \
  168. if constexpr (sizeof(read_type) == 1) \
  169. configuration.stack().push(make<Value>(static_cast<push_type>(slice[0]))); \
  170. else \
  171. configuration.stack().push(make<Value>(read_value<push_type>(slice))); \
  172. return; \
  173. } while (false)
  174. #define POP_AND_STORE(pop_type, store_type) \
  175. do { \
  176. auto value = ConvertToRaw<pop_type> {}(*configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<pop_type>()); \
  177. dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(store_type)); \
  178. store_to_memory(configuration, instruction, { &value, sizeof(store_type) }); \
  179. return; \
  180. } while (false)
  181. template<typename T>
  182. static T read_value(ReadonlyBytes data)
  183. {
  184. T value;
  185. InputMemoryStream stream { data };
  186. auto ok = IsSigned<T> ? LEB128::read_signed(stream, value) : LEB128::read_unsigned(stream, value);
  187. VERIFY(ok);
  188. return value;
  189. }
  190. template<>
  191. float read_value<float>(ReadonlyBytes data)
  192. {
  193. InputMemoryStream stream { data };
  194. LittleEndian<u32> raw_value;
  195. stream >> raw_value;
  196. VERIFY(!stream.has_any_error());
  197. return bit_cast<float>(static_cast<u32>(raw_value));
  198. }
  199. template<>
  200. double read_value<double>(ReadonlyBytes data)
  201. {
  202. InputMemoryStream stream { data };
  203. LittleEndian<u64> raw_value;
  204. stream >> raw_value;
  205. VERIFY(!stream.has_any_error());
  206. return bit_cast<double>(static_cast<u64>(raw_value));
  207. }
  208. template<typename T>
  209. struct ConvertToRaw {
  210. T operator()(T value)
  211. {
  212. return value;
  213. }
  214. };
  215. template<>
  216. struct ConvertToRaw<float> {
  217. u32 operator()(float value)
  218. {
  219. LittleEndian<u32> res;
  220. ReadonlyBytes bytes { &value, sizeof(float) };
  221. InputMemoryStream stream { bytes };
  222. stream >> res;
  223. VERIFY(!stream.has_any_error());
  224. return static_cast<u32>(res);
  225. }
  226. };
  227. template<>
  228. struct ConvertToRaw<double> {
  229. u64 operator()(double value)
  230. {
  231. LittleEndian<u64> res;
  232. ReadonlyBytes bytes { &value, sizeof(double) };
  233. InputMemoryStream stream { bytes };
  234. stream >> res;
  235. VERIFY(!stream.has_any_error());
  236. return static_cast<u64>(res);
  237. }
  238. };
  239. Vector<NonnullOwnPtr<Value>> Interpreter::pop_values(Configuration& configuration, size_t count)
  240. {
  241. Vector<NonnullOwnPtr<Value>> results;
  242. // Pop results in order
  243. for (size_t i = 0; i < count; ++i) {
  244. auto top_of_stack = configuration.stack().pop();
  245. if (auto value = top_of_stack.get_pointer<NonnullOwnPtr<Value>>())
  246. results.append(move(*value));
  247. else
  248. trap_if_not(value);
  249. }
  250. return results;
  251. }
  252. void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip, const Instruction& instruction)
  253. {
  254. dbgln_if(WASM_TRACE_DEBUG, "Executing instruction {} at ip {}", instruction_name(instruction.opcode()), ip.value());
  255. if constexpr (WASM_TRACE_DEBUG)
  256. configuration.dump_stack();
  257. switch (instruction.opcode().value()) {
  258. case Instructions::unreachable.value():
  259. m_do_trap = true;
  260. return;
  261. case Instructions::nop.value():
  262. return;
  263. case Instructions::local_get.value():
  264. configuration.stack().push(make<Value>(configuration.frame()->locals()[instruction.arguments().get<LocalIndex>().value()]));
  265. return;
  266. case Instructions::local_set.value(): {
  267. auto entry = configuration.stack().pop();
  268. configuration.frame()->locals()[instruction.arguments().get<LocalIndex>().value()] = move(*entry.get<NonnullOwnPtr<Value>>());
  269. return;
  270. }
  271. case Instructions::i32_const.value():
  272. configuration.stack().push(make<Value>(ValueType { ValueType::I32 }, static_cast<i64>(instruction.arguments().get<i32>())));
  273. return;
  274. case Instructions::i64_const.value():
  275. configuration.stack().push(make<Value>(ValueType { ValueType::I64 }, instruction.arguments().get<i64>()));
  276. return;
  277. case Instructions::f32_const.value():
  278. configuration.stack().push(make<Value>(ValueType { ValueType::F32 }, static_cast<double>(instruction.arguments().get<float>())));
  279. return;
  280. case Instructions::f64_const.value():
  281. configuration.stack().push(make<Value>(ValueType { ValueType::F64 }, instruction.arguments().get<double>()));
  282. return;
  283. case Instructions::block.value(): {
  284. size_t arity = 0;
  285. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  286. if (args.block_type.kind() != BlockType::Empty)
  287. arity = 1;
  288. configuration.stack().push(make<Label>(arity, args.end_ip));
  289. return;
  290. }
  291. case Instructions::loop.value(): {
  292. size_t arity = 0;
  293. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  294. if (args.block_type.kind() != BlockType::Empty)
  295. arity = 1;
  296. configuration.stack().push(make<Label>(arity, ip.value() + 1));
  297. return;
  298. }
  299. case Instructions::if_.value(): {
  300. size_t arity = 0;
  301. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  302. if (args.block_type.kind() != BlockType::Empty)
  303. arity = 1;
  304. auto entry = configuration.stack().pop();
  305. auto value = entry.get<NonnullOwnPtr<Value>>()->to<i32>();
  306. TRAP_IF_NOT(value.has_value());
  307. configuration.stack().push(make<Label>(arity, args.end_ip));
  308. if (value.value() == 0) {
  309. if (args.else_ip.has_value()) {
  310. configuration.ip() = args.else_ip.value();
  311. } else {
  312. configuration.ip() = args.end_ip;
  313. configuration.stack().pop();
  314. }
  315. }
  316. return;
  317. }
  318. case Instructions::structured_end.value():
  319. return;
  320. case Instructions::structured_else.value(): {
  321. auto label = configuration.nth_label(0);
  322. TRAP_IF_NOT(label.has_value());
  323. auto results = pop_values(configuration, label->arity());
  324. // drop all locals
  325. for (; !configuration.stack().is_empty();) {
  326. auto entry = configuration.stack().pop();
  327. if (entry.has<NonnullOwnPtr<Label>>())
  328. break;
  329. }
  330. // Push results in reverse
  331. for (size_t i = 1; i < results.size() + 1; ++i)
  332. configuration.stack().push(move(static_cast<Vector<NonnullOwnPtr<Value>>&>(results)[results.size() - i]));
  333. if (instruction.opcode() == Instructions::structured_end)
  334. return;
  335. // Jump to the end label
  336. configuration.ip() = label->continuation();
  337. return;
  338. }
  339. case Instructions::return_.value(): {
  340. Vector<Stack::EntryType> results;
  341. auto& frame = *configuration.frame();
  342. results.ensure_capacity(frame.arity());
  343. for (size_t i = 0; i < frame.arity(); ++i)
  344. results.prepend(configuration.stack().pop());
  345. // drop all locals
  346. OwnPtr<Label> last_label;
  347. for (; !configuration.stack().is_empty();) {
  348. auto entry = configuration.stack().pop();
  349. if (entry.has<NonnullOwnPtr<Label>>()) {
  350. last_label = move(entry.get<NonnullOwnPtr<Label>>());
  351. continue;
  352. }
  353. if (entry.has<NonnullOwnPtr<Frame>>()) {
  354. // Push the frame back
  355. configuration.stack().push(move(entry));
  356. // Push its label back (if there is one)
  357. if (last_label)
  358. configuration.stack().push(last_label.release_nonnull());
  359. break;
  360. }
  361. last_label.clear();
  362. }
  363. // Push the results back
  364. for (auto& result : results)
  365. configuration.stack().push(move(result));
  366. // Jump past the call/indirect instruction
  367. configuration.ip() = configuration.frame()->expression().instructions().size() - 1;
  368. return;
  369. }
  370. case Instructions::br.value():
  371. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  372. case Instructions::br_if.value(): {
  373. if (configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>().value_or(0) == 0)
  374. return;
  375. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  376. }
  377. case Instructions::br_table.value():
  378. goto unimplemented;
  379. case Instructions::call.value(): {
  380. auto index = instruction.arguments().get<FunctionIndex>();
  381. auto address = configuration.frame()->module().functions()[index.value()];
  382. dbgln_if(WASM_TRACE_DEBUG, "call({})", address.value());
  383. call_address(configuration, address);
  384. return;
  385. }
  386. case Instructions::call_indirect.value(): {
  387. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  388. auto table_address = configuration.frame()->module().tables()[args.table.value()];
  389. auto table_instance = configuration.store().get(table_address);
  390. auto index = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>();
  391. TRAP_IF_NOT(index.has_value());
  392. if (index.value() < 0 || static_cast<size_t>(index.value()) >= table_instance->elements().size()) {
  393. dbgln("LibWasm: Element access out of bounds, expected {0} > 0 and {0} < {1}", index.value(), table_instance->elements().size());
  394. m_do_trap = true;
  395. return;
  396. }
  397. auto element = table_instance->elements()[index.value()];
  398. if (!element.has_value() || !element->ref().has<FunctionAddress>()) {
  399. dbgln("LibWasm: call_indirect attempted with invalid address element (not a function)");
  400. m_do_trap = true;
  401. return;
  402. }
  403. auto address = element->ref().get<FunctionAddress>();
  404. dbgln_if(WASM_TRACE_DEBUG, "call_indirect({} -> {})", index.value(), address.value());
  405. call_address(configuration, address);
  406. return;
  407. }
  408. case Instructions::i32_load.value():
  409. LOAD_AND_PUSH(i32, i32);
  410. case Instructions::i64_load.value():
  411. LOAD_AND_PUSH(i64, i64);
  412. case Instructions::f32_load.value():
  413. LOAD_AND_PUSH(float, float);
  414. case Instructions::f64_load.value():
  415. LOAD_AND_PUSH(double, double);
  416. case Instructions::i32_load8_s.value():
  417. LOAD_AND_PUSH(i8, i32);
  418. case Instructions::i32_load8_u.value():
  419. LOAD_AND_PUSH(u8, i32);
  420. case Instructions::i32_load16_s.value():
  421. LOAD_AND_PUSH(i16, i32);
  422. case Instructions::i32_load16_u.value():
  423. LOAD_AND_PUSH(u16, i32);
  424. case Instructions::i64_load8_s.value():
  425. LOAD_AND_PUSH(i8, i64);
  426. case Instructions::i64_load8_u.value():
  427. LOAD_AND_PUSH(u8, i64);
  428. case Instructions::i64_load16_s.value():
  429. LOAD_AND_PUSH(i16, i64);
  430. case Instructions::i64_load16_u.value():
  431. LOAD_AND_PUSH(u16, i64);
  432. case Instructions::i64_load32_s.value():
  433. LOAD_AND_PUSH(i32, i64);
  434. case Instructions::i64_load32_u.value():
  435. LOAD_AND_PUSH(u32, i64);
  436. case Instructions::i32_store.value():
  437. POP_AND_STORE(i32, i32);
  438. case Instructions::i64_store.value():
  439. POP_AND_STORE(i64, i64);
  440. case Instructions::f32_store.value():
  441. POP_AND_STORE(float, float);
  442. case Instructions::f64_store.value():
  443. POP_AND_STORE(double, double);
  444. case Instructions::i32_store8.value():
  445. POP_AND_STORE(i32, i8);
  446. case Instructions::i32_store16.value():
  447. POP_AND_STORE(i32, i16);
  448. case Instructions::i64_store8.value():
  449. POP_AND_STORE(i64, i8);
  450. case Instructions::i64_store16.value():
  451. POP_AND_STORE(i64, i16);
  452. case Instructions::i64_store32.value():
  453. POP_AND_STORE(i64, i32);
  454. case Instructions::local_tee.value(): {
  455. auto value = *configuration.stack().peek().get<NonnullOwnPtr<Value>>();
  456. auto local_index = instruction.arguments().get<LocalIndex>();
  457. TRAP_IF_NOT(configuration.frame()->locals().size() > local_index.value());
  458. dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value());
  459. configuration.frame()->locals()[local_index.value()] = move(value);
  460. return;
  461. }
  462. case Instructions::global_get.value(): {
  463. auto global_index = instruction.arguments().get<GlobalIndex>();
  464. TRAP_IF_NOT(configuration.frame()->module().globals().size() > global_index.value());
  465. auto address = configuration.frame()->module().globals()[global_index.value()];
  466. dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value());
  467. auto global = configuration.store().get(address);
  468. configuration.stack().push(make<Value>(global->value()));
  469. return;
  470. }
  471. case Instructions::global_set.value(): {
  472. auto global_index = instruction.arguments().get<GlobalIndex>();
  473. TRAP_IF_NOT(configuration.frame()->module().globals().size() > global_index.value());
  474. auto address = configuration.frame()->module().globals()[global_index.value()];
  475. auto value = *configuration.stack().pop().get<NonnullOwnPtr<Value>>();
  476. dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value());
  477. auto global = configuration.store().get(address);
  478. global->set_value(move(value));
  479. return;
  480. }
  481. case Instructions::memory_size.value(): {
  482. auto address = configuration.frame()->module().memories()[0];
  483. auto instance = configuration.store().get(address);
  484. auto pages = instance->size() / Constants::page_size;
  485. dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
  486. configuration.stack().push(make<Value>((i32)pages));
  487. return;
  488. }
  489. case Instructions::memory_grow.value(): {
  490. auto address = configuration.frame()->module().memories()[0];
  491. auto instance = configuration.store().get(address);
  492. i32 old_pages = instance->size() / Constants::page_size;
  493. auto new_pages = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>();
  494. TRAP_IF_NOT(new_pages.has_value());
  495. dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", *new_pages, old_pages);
  496. if (instance->grow(new_pages.value() * Constants::page_size))
  497. configuration.stack().push(make<Value>((i32)old_pages));
  498. else
  499. configuration.stack().push(make<Value>((i32)-1));
  500. return;
  501. }
  502. case Instructions::table_get.value():
  503. case Instructions::table_set.value():
  504. case Instructions::ref_null.value():
  505. case Instructions::ref_func.value():
  506. case Instructions::ref_is_null.value():
  507. goto unimplemented;
  508. case Instructions::drop.value():
  509. configuration.stack().pop();
  510. return;
  511. case Instructions::select.value():
  512. case Instructions::select_typed.value(): {
  513. // Note: The type seems to only be used for validation.
  514. auto value = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>();
  515. TRAP_IF_NOT(value.has_value());
  516. dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value());
  517. auto rhs = move(configuration.stack().pop().get<NonnullOwnPtr<Value>>());
  518. auto lhs = move(configuration.stack().pop().get<NonnullOwnPtr<Value>>());
  519. configuration.stack().push(value.value() != 0 ? move(lhs) : move(rhs));
  520. return;
  521. }
  522. case Instructions::i32_eqz.value():
  523. UNARY_NUMERIC_OPERATION(i32, 0 ==);
  524. case Instructions::i32_eq.value():
  525. BINARY_NUMERIC_OPERATION(i32, ==, i32);
  526. case Instructions::i32_ne.value():
  527. BINARY_NUMERIC_OPERATION(i32, !=, i32);
  528. case Instructions::i32_lts.value():
  529. BINARY_NUMERIC_OPERATION(i32, <, i32);
  530. case Instructions::i32_ltu.value():
  531. BINARY_NUMERIC_OPERATION(u32, <, i32);
  532. case Instructions::i32_gts.value():
  533. BINARY_NUMERIC_OPERATION(i32, >, i32);
  534. case Instructions::i32_gtu.value():
  535. BINARY_NUMERIC_OPERATION(u32, >, i32);
  536. case Instructions::i32_les.value():
  537. BINARY_NUMERIC_OPERATION(i32, <=, i32);
  538. case Instructions::i32_leu.value():
  539. BINARY_NUMERIC_OPERATION(u32, <=, i32);
  540. case Instructions::i32_ges.value():
  541. BINARY_NUMERIC_OPERATION(i32, >=, i32);
  542. case Instructions::i32_geu.value():
  543. BINARY_NUMERIC_OPERATION(u32, >=, i32);
  544. case Instructions::i64_eqz.value():
  545. UNARY_NUMERIC_OPERATION(i64, 0ull ==);
  546. case Instructions::i64_eq.value():
  547. BINARY_NUMERIC_OPERATION(i64, ==, i32);
  548. case Instructions::i64_ne.value():
  549. BINARY_NUMERIC_OPERATION(i64, !=, i32);
  550. case Instructions::i64_lts.value():
  551. BINARY_NUMERIC_OPERATION(i64, <, i32);
  552. case Instructions::i64_ltu.value():
  553. BINARY_NUMERIC_OPERATION(u64, <, i32);
  554. case Instructions::i64_gts.value():
  555. BINARY_NUMERIC_OPERATION(i64, >, i32);
  556. case Instructions::i64_gtu.value():
  557. BINARY_NUMERIC_OPERATION(u64, >, i32);
  558. case Instructions::i64_les.value():
  559. BINARY_NUMERIC_OPERATION(i64, <=, i32);
  560. case Instructions::i64_leu.value():
  561. BINARY_NUMERIC_OPERATION(u64, <=, i32);
  562. case Instructions::i64_ges.value():
  563. BINARY_NUMERIC_OPERATION(i64, >=, i32);
  564. case Instructions::i64_geu.value():
  565. BINARY_NUMERIC_OPERATION(u64, >=, i32);
  566. case Instructions::f32_eq.value():
  567. BINARY_NUMERIC_OPERATION(float, ==, i32);
  568. case Instructions::f32_ne.value():
  569. BINARY_NUMERIC_OPERATION(float, !=, i32);
  570. case Instructions::f32_lt.value():
  571. BINARY_NUMERIC_OPERATION(float, <, i32);
  572. case Instructions::f32_gt.value():
  573. BINARY_NUMERIC_OPERATION(float, >, i32);
  574. case Instructions::f32_le.value():
  575. BINARY_NUMERIC_OPERATION(float, <=, i32);
  576. case Instructions::f32_ge.value():
  577. BINARY_NUMERIC_OPERATION(float, >=, i32);
  578. case Instructions::f64_eq.value():
  579. BINARY_NUMERIC_OPERATION(double, ==, i32);
  580. case Instructions::f64_ne.value():
  581. BINARY_NUMERIC_OPERATION(double, !=, i32);
  582. case Instructions::f64_lt.value():
  583. BINARY_NUMERIC_OPERATION(double, <, i32);
  584. case Instructions::f64_gt.value():
  585. BINARY_NUMERIC_OPERATION(double, >, i32);
  586. case Instructions::f64_le.value():
  587. BINARY_NUMERIC_OPERATION(double, <=, i32);
  588. case Instructions::f64_ge.value():
  589. BINARY_NUMERIC_OPERATION(double, >, i32);
  590. case Instructions::i32_clz.value():
  591. case Instructions::i32_ctz.value():
  592. case Instructions::i32_popcnt.value():
  593. goto unimplemented;
  594. case Instructions::i32_add.value():
  595. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, +, i32);
  596. case Instructions::i32_sub.value():
  597. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, -, i32);
  598. case Instructions::i32_mul.value():
  599. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, *, i32);
  600. case Instructions::i32_divs.value():
  601. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, /, i32, TRAP_IF_NOT(rhs.value() != 0));
  602. case Instructions::i32_divu.value():
  603. OVF_CHECKED_BINARY_NUMERIC_OPERATION(u32, /, i32, TRAP_IF_NOT(rhs.value() != 0));
  604. case Instructions::i32_rems.value():
  605. BINARY_NUMERIC_OPERATION(i32, %, i32, TRAP_IF_NOT(rhs.value() != 0));
  606. case Instructions::i32_remu.value():
  607. BINARY_NUMERIC_OPERATION(u32, %, i32, TRAP_IF_NOT(rhs.value() != 0));
  608. case Instructions::i32_and.value():
  609. BINARY_NUMERIC_OPERATION(i32, &, i32);
  610. case Instructions::i32_or.value():
  611. BINARY_NUMERIC_OPERATION(i32, |, i32);
  612. case Instructions::i32_xor.value():
  613. BINARY_NUMERIC_OPERATION(i32, ^, i32);
  614. case Instructions::i32_shl.value():
  615. BINARY_NUMERIC_OPERATION(i32, <<, i32);
  616. case Instructions::i32_shrs.value():
  617. BINARY_NUMERIC_OPERATION(i32, >>, i32);
  618. case Instructions::i32_shru.value():
  619. BINARY_NUMERIC_OPERATION(u32, >>, i32);
  620. case Instructions::i32_rotl.value():
  621. case Instructions::i32_rotr.value():
  622. case Instructions::i64_clz.value():
  623. case Instructions::i64_ctz.value():
  624. case Instructions::i64_popcnt.value():
  625. goto unimplemented;
  626. case Instructions::i64_add.value():
  627. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, +, i64);
  628. case Instructions::i64_sub.value():
  629. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, -, i64);
  630. case Instructions::i64_mul.value():
  631. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, *, i64);
  632. case Instructions::i64_divs.value():
  633. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, /, i64, TRAP_IF_NOT(rhs.value() != 0));
  634. case Instructions::i64_divu.value():
  635. OVF_CHECKED_BINARY_NUMERIC_OPERATION(u64, /, i64, TRAP_IF_NOT(rhs.value() != 0));
  636. case Instructions::i64_rems.value():
  637. BINARY_NUMERIC_OPERATION(i64, %, i64, TRAP_IF_NOT(rhs.value() != 0));
  638. case Instructions::i64_remu.value():
  639. BINARY_NUMERIC_OPERATION(u64, %, i64, TRAP_IF_NOT(rhs.value() != 0));
  640. case Instructions::i64_and.value():
  641. BINARY_NUMERIC_OPERATION(i64, &, i64);
  642. case Instructions::i64_or.value():
  643. BINARY_NUMERIC_OPERATION(i64, |, i64);
  644. case Instructions::i64_xor.value():
  645. BINARY_NUMERIC_OPERATION(i64, ^, i64);
  646. case Instructions::i64_shl.value():
  647. BINARY_NUMERIC_OPERATION(i64, <<, i64);
  648. case Instructions::i64_shrs.value():
  649. BINARY_NUMERIC_OPERATION(i64, >>, i64);
  650. case Instructions::i64_shru.value():
  651. BINARY_NUMERIC_OPERATION(u64, >>, i64);
  652. case Instructions::i64_rotl.value():
  653. case Instructions::i64_rotr.value():
  654. goto unimplemented;
  655. case Instructions::f32_abs.value():
  656. UNARY_NUMERIC_OPERATION(float, fabsf);
  657. case Instructions::f32_neg.value():
  658. UNARY_NUMERIC_OPERATION(float, -);
  659. case Instructions::f32_ceil.value():
  660. UNARY_NUMERIC_OPERATION(float, ceilf);
  661. case Instructions::f32_floor.value():
  662. UNARY_NUMERIC_OPERATION(float, floorf);
  663. case Instructions::f32_trunc.value():
  664. UNARY_NUMERIC_OPERATION(float, truncf);
  665. case Instructions::f32_nearest.value():
  666. UNARY_NUMERIC_OPERATION(float, roundf);
  667. case Instructions::f32_sqrt.value():
  668. UNARY_NUMERIC_OPERATION(float, sqrtf);
  669. case Instructions::f32_add.value():
  670. UNARY_NUMERIC_OPERATION(float, +);
  671. case Instructions::f32_sub.value():
  672. UNARY_NUMERIC_OPERATION(float, -);
  673. case Instructions::f32_mul.value():
  674. BINARY_NUMERIC_OPERATION(float, *, float);
  675. case Instructions::f32_div.value():
  676. BINARY_NUMERIC_OPERATION(float, /, float);
  677. case Instructions::f32_min.value():
  678. BINARY_PREFIX_NUMERIC_OPERATION(float, min, float);
  679. case Instructions::f32_max.value():
  680. BINARY_PREFIX_NUMERIC_OPERATION(float, max, float);
  681. case Instructions::f32_copysign.value():
  682. BINARY_PREFIX_NUMERIC_OPERATION(float, copysignf, float);
  683. case Instructions::f64_abs.value():
  684. UNARY_NUMERIC_OPERATION(double, fabs);
  685. case Instructions::f64_neg.value():
  686. UNARY_NUMERIC_OPERATION(double, -);
  687. case Instructions::f64_ceil.value():
  688. UNARY_NUMERIC_OPERATION(double, ceil);
  689. case Instructions::f64_floor.value():
  690. UNARY_NUMERIC_OPERATION(double, floor);
  691. case Instructions::f64_trunc.value():
  692. UNARY_NUMERIC_OPERATION(double, trunc);
  693. case Instructions::f64_nearest.value():
  694. UNARY_NUMERIC_OPERATION(double, round);
  695. case Instructions::f64_sqrt.value():
  696. UNARY_NUMERIC_OPERATION(double, sqrt);
  697. case Instructions::f64_add.value():
  698. BINARY_NUMERIC_OPERATION(double, +, double);
  699. case Instructions::f64_sub.value():
  700. BINARY_NUMERIC_OPERATION(double, -, double);
  701. case Instructions::f64_mul.value():
  702. BINARY_NUMERIC_OPERATION(double, *, double);
  703. case Instructions::f64_div.value():
  704. BINARY_NUMERIC_OPERATION(double, /, double);
  705. case Instructions::f64_min.value():
  706. BINARY_PREFIX_NUMERIC_OPERATION(double, min, double);
  707. case Instructions::f64_max.value():
  708. BINARY_PREFIX_NUMERIC_OPERATION(double, max, double);
  709. case Instructions::f64_copysign.value():
  710. BINARY_PREFIX_NUMERIC_OPERATION(double, copysign, double);
  711. case Instructions::i32_wrap_i64.value():
  712. UNARY_MAP(i64, i32, i32);
  713. case Instructions::i32_trunc_sf32.value():
  714. case Instructions::i32_trunc_uf32.value():
  715. case Instructions::i32_trunc_sf64.value():
  716. case Instructions::i32_trunc_uf64.value():
  717. goto unimplemented;
  718. case Instructions::i64_extend_si32.value():
  719. UNARY_MAP(i32, i64, i64);
  720. case Instructions::i64_extend_ui32.value():
  721. UNARY_MAP(u32, i64, i64);
  722. case Instructions::i64_trunc_sf32.value():
  723. case Instructions::i64_trunc_uf32.value():
  724. case Instructions::i64_trunc_sf64.value():
  725. case Instructions::i64_trunc_uf64.value():
  726. goto unimplemented;
  727. case Instructions::f32_convert_si32.value():
  728. UNARY_MAP(i32, float, float);
  729. case Instructions::f32_convert_ui32.value():
  730. UNARY_MAP(u32, float, float);
  731. case Instructions::f32_convert_si64.value():
  732. UNARY_MAP(i64, float, float);
  733. case Instructions::f32_convert_ui64.value():
  734. UNARY_MAP(u32, float, float);
  735. case Instructions::f32_demote_f64.value():
  736. UNARY_MAP(double, float, float);
  737. case Instructions::f64_convert_si32.value():
  738. UNARY_MAP(i32, double, double);
  739. case Instructions::f64_convert_ui32.value():
  740. UNARY_MAP(u32, double, double);
  741. case Instructions::f64_convert_si64.value():
  742. UNARY_MAP(i64, double, double);
  743. case Instructions::f64_convert_ui64.value():
  744. UNARY_MAP(u64, double, double);
  745. case Instructions::f64_promote_f32.value():
  746. UNARY_MAP(float, double, double);
  747. case Instructions::i32_reinterpret_f32.value():
  748. UNARY_MAP(float, bit_cast<i32>, i32);
  749. case Instructions::i64_reinterpret_f64.value():
  750. UNARY_MAP(double, bit_cast<i64>, i64);
  751. case Instructions::f32_reinterpret_i32.value():
  752. UNARY_MAP(i32, bit_cast<float>, float);
  753. case Instructions::f64_reinterpret_i64.value():
  754. UNARY_MAP(i64, bit_cast<double>, double);
  755. case Instructions::i32_trunc_sat_f32_s.value():
  756. case Instructions::i32_trunc_sat_f32_u.value():
  757. case Instructions::i32_trunc_sat_f64_s.value():
  758. case Instructions::i32_trunc_sat_f64_u.value():
  759. case Instructions::i64_trunc_sat_f32_s.value():
  760. case Instructions::i64_trunc_sat_f32_u.value():
  761. case Instructions::i64_trunc_sat_f64_s.value():
  762. case Instructions::i64_trunc_sat_f64_u.value():
  763. case Instructions::memory_init.value():
  764. case Instructions::data_drop.value():
  765. case Instructions::memory_copy.value():
  766. case Instructions::memory_fill.value():
  767. case Instructions::table_init.value():
  768. case Instructions::elem_drop.value():
  769. case Instructions::table_copy.value():
  770. case Instructions::table_grow.value():
  771. case Instructions::table_size.value():
  772. case Instructions::table_fill.value():
  773. default:
  774. unimplemented:;
  775. dbgln("Instruction '{}' not implemented", instruction_name(instruction.opcode()));
  776. m_do_trap = true;
  777. return;
  778. }
  779. }
  780. }