test-wasm.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/File.h>
  7. #include <LibTest/JavaScriptTestRunner.h>
  8. #include <LibWasm/AbstractMachine/Interpreter.h>
  9. #include <LibWasm/Types.h>
  10. TEST_ROOT("Userland/Libraries/LibWasm/Tests");
  11. TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
  12. {
  13. auto filename = vm.argument(0).to_string(global_object);
  14. if (vm.exception())
  15. return {};
  16. auto file = Core::File::open(filename, Core::OpenMode::ReadOnly);
  17. if (file.is_error()) {
  18. vm.throw_exception<JS::TypeError>(global_object, file.error());
  19. return {};
  20. }
  21. auto contents = file.value()->read_all();
  22. auto array = JS::Uint8Array::create(global_object, contents.size());
  23. contents.span().copy_to(array->data());
  24. return array;
  25. }
  26. class WebAssemblyModule final : public JS::Object {
  27. JS_OBJECT(WebAssemblyModule, JS::Object);
  28. public:
  29. // FIXME: This should only contain an instantiated module, not the entire abstract machine!
  30. explicit WebAssemblyModule(JS::Object& prototype)
  31. : JS::Object(prototype)
  32. {
  33. }
  34. static WebAssemblyModule* create(JS::GlobalObject& global_object, Wasm::Module module)
  35. {
  36. auto instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype());
  37. instance->m_module = move(module);
  38. if (auto result = instance->m_machine.instantiate(*instance->m_module, {}); result.is_error())
  39. global_object.vm().throw_exception<JS::TypeError>(global_object, result.release_error().error);
  40. return instance;
  41. }
  42. void initialize(JS::GlobalObject&) override;
  43. ~WebAssemblyModule() override = default;
  44. private:
  45. JS_DECLARE_NATIVE_FUNCTION(get_export);
  46. JS_DECLARE_NATIVE_FUNCTION(wasm_invoke);
  47. Wasm::AbstractMachine m_machine;
  48. Optional<Wasm::Module> m_module;
  49. };
  50. TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
  51. {
  52. auto object = vm.argument(0).to_object(global_object);
  53. if (vm.exception())
  54. return {};
  55. if (!is<JS::Uint8Array>(object)) {
  56. vm.throw_exception<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module");
  57. return {};
  58. }
  59. auto& array = static_cast<JS::Uint8Array&>(*object);
  60. InputMemoryStream stream { array.data() };
  61. auto result = Wasm::Module::parse(stream);
  62. if (result.is_error()) {
  63. vm.throw_exception<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
  64. return {};
  65. }
  66. if (stream.handle_any_error()) {
  67. vm.throw_exception<JS::SyntaxError>(global_object, "Bianry stream contained errors");
  68. return {};
  69. }
  70. return WebAssemblyModule::create(global_object, result.release_value());
  71. }
  72. TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
  73. {
  74. auto lhs = vm.argument(0).to_object(global_object);
  75. if (vm.exception())
  76. return {};
  77. if (!is<JS::TypedArrayBase>(lhs)) {
  78. vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
  79. return {};
  80. }
  81. auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
  82. auto rhs = vm.argument(1).to_object(global_object);
  83. if (vm.exception())
  84. return {};
  85. if (!is<JS::TypedArrayBase>(rhs)) {
  86. vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
  87. return {};
  88. }
  89. auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
  90. return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
  91. }
  92. void WebAssemblyModule::initialize(JS::GlobalObject& global_object)
  93. {
  94. Base::initialize(global_object);
  95. define_native_function("getExport", get_export);
  96. define_native_function("invoke", wasm_invoke);
  97. }
  98. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
  99. {
  100. auto name = vm.argument(0).to_string(global_object);
  101. if (vm.exception())
  102. return {};
  103. auto this_value = vm.this_value(global_object);
  104. auto object = this_value.to_object(global_object);
  105. if (vm.exception())
  106. return {};
  107. if (!object || !is<WebAssemblyModule>(object)) {
  108. vm.throw_exception<JS::TypeError>(global_object, "Not a WebAssemblyModule");
  109. return {};
  110. }
  111. auto instance = static_cast<WebAssemblyModule*>(object);
  112. for (auto& entry : instance->m_machine.module_instance().exports()) {
  113. if (entry.name() == name) {
  114. auto& value = entry.value();
  115. if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
  116. return JS::Value(static_cast<unsigned long>(ptr->value()));
  117. vm.throw_exception<JS::TypeError>(global_object, String::formatted("'{}' does not refer to a function", name));
  118. return {};
  119. }
  120. }
  121. vm.throw_exception<JS::TypeError>(global_object, String::formatted("'{}' could not be found", name));
  122. return {};
  123. }
  124. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
  125. {
  126. auto address = static_cast<unsigned long>(vm.argument(0).to_double(global_object));
  127. if (vm.exception())
  128. return {};
  129. auto this_value = vm.this_value(global_object);
  130. auto object = this_value.to_object(global_object);
  131. if (vm.exception())
  132. return {};
  133. if (!object || !is<WebAssemblyModule>(object)) {
  134. vm.throw_exception<JS::TypeError>(global_object, "Not a WebAssemblyModule");
  135. return {};
  136. }
  137. auto instance = static_cast<WebAssemblyModule*>(object);
  138. Wasm::FunctionAddress function_address { address };
  139. auto function_instance = instance->m_machine.store().get(function_address);
  140. if (!function_instance) {
  141. vm.throw_exception<JS::TypeError>(global_object, "Invalid function address");
  142. return {};
  143. }
  144. const Wasm::FunctionType* type { nullptr };
  145. function_instance->visit([&](auto& value) { type = &value.type(); });
  146. if (!type) {
  147. vm.throw_exception<JS::TypeError>(global_object, "Invalid function found at given address");
  148. return {};
  149. }
  150. Vector<Wasm::Value> arguments;
  151. if (type->parameters().size() + 1 > vm.argument_count()) {
  152. vm.throw_exception<JS::TypeError>(global_object, String::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count()));
  153. return {};
  154. }
  155. size_t index = 1;
  156. for (auto& param : type->parameters()) {
  157. auto value = vm.argument(index++).to_double(global_object);
  158. switch (param.kind()) {
  159. case Wasm::ValueType::Kind::I32:
  160. arguments.append(Wasm::Value(static_cast<i32>(value)));
  161. break;
  162. case Wasm::ValueType::Kind::I64:
  163. arguments.append(Wasm::Value(static_cast<i64>(value)));
  164. break;
  165. case Wasm::ValueType::Kind::F32:
  166. arguments.append(Wasm::Value(static_cast<float>(value)));
  167. break;
  168. case Wasm::ValueType::Kind::F64:
  169. arguments.append(Wasm::Value(static_cast<double>(value)));
  170. break;
  171. case Wasm::ValueType::Kind::FunctionReference:
  172. arguments.append(Wasm::Value(Wasm::FunctionAddress { static_cast<u64>(value) }));
  173. break;
  174. case Wasm::ValueType::Kind::ExternReference:
  175. arguments.append(Wasm::Value(Wasm::ExternAddress { static_cast<u64>(value) }));
  176. break;
  177. }
  178. }
  179. auto result = instance->m_machine.invoke(function_address, arguments);
  180. if (result.is_trap()) {
  181. vm.throw_exception<JS::TypeError>(global_object, "Execution trapped");
  182. return {};
  183. }
  184. if (result.values().is_empty())
  185. return JS::js_null();
  186. JS::Value return_value;
  187. result.values().first().value().visit(
  188. [&](const auto& value) { return_value = JS::Value(static_cast<double>(value)); },
  189. [&](const Wasm::FunctionAddress& index) { return_value = JS::Value(static_cast<double>(index.value())); },
  190. [&](const Wasm::ExternAddress& index) { return_value = JS::Value(static_cast<double>(index.value())); });
  191. return return_value;
  192. }