Interpreter.cpp 40 KB

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