Interpreter.cpp 40 KB

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