Interpreter.cpp 41 KB

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