Interpreter.cpp 37 KB

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