test-wasm.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/BytecodeInterpreter.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. explicit WebAssemblyModule(JS::Object& prototype)
  30. : JS::Object(prototype)
  31. {
  32. }
  33. static Wasm::AbstractMachine& machine() { return m_machine; }
  34. Wasm::Module& module() { return *m_module; }
  35. Wasm::ModuleInstance& module_instance() { return *m_module_instance; }
  36. static WebAssemblyModule* create(JS::GlobalObject& global_object, Wasm::Module module, HashMap<Wasm::Linker::Name, Wasm::ExternValue> const& imports)
  37. {
  38. auto instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype());
  39. instance->m_module = move(module);
  40. Wasm::Linker linker(*instance->m_module);
  41. linker.link(imports);
  42. linker.link(spec_test_namespace());
  43. auto link_result = linker.finish();
  44. if (link_result.is_error()) {
  45. global_object.vm().throw_exception<JS::TypeError>(global_object, "Link failed");
  46. } else {
  47. if (auto result = machine().instantiate(*instance->m_module, link_result.release_value()); result.is_error())
  48. global_object.vm().throw_exception<JS::TypeError>(global_object, result.release_error().error);
  49. else
  50. instance->m_module_instance = result.release_value();
  51. }
  52. return instance;
  53. }
  54. void initialize(JS::GlobalObject&) override;
  55. ~WebAssemblyModule() override = default;
  56. private:
  57. JS_DECLARE_NATIVE_FUNCTION(get_export);
  58. JS_DECLARE_NATIVE_FUNCTION(wasm_invoke);
  59. static HashMap<Wasm::Linker::Name, Wasm::ExternValue> const& spec_test_namespace()
  60. {
  61. if (!s_spec_test_namespace.is_empty())
  62. return s_spec_test_namespace;
  63. Wasm::FunctionType print_i32_type { { Wasm::ValueType(Wasm::ValueType::I32) }, {} };
  64. auto address = m_machine.store().allocate(Wasm::HostFunction {
  65. [](auto&, auto&) -> Wasm::Result {
  66. // Noop, this just needs to exist.
  67. return Wasm::Result { Vector<Wasm::Value> {} };
  68. },
  69. print_i32_type });
  70. s_spec_test_namespace.set({ "spectest", "print_i32", print_i32_type }, Wasm::ExternValue { *address });
  71. return s_spec_test_namespace;
  72. }
  73. static HashMap<Wasm::Linker::Name, Wasm::ExternValue> s_spec_test_namespace;
  74. static Wasm::AbstractMachine m_machine;
  75. Optional<Wasm::Module> m_module;
  76. OwnPtr<Wasm::ModuleInstance> m_module_instance;
  77. };
  78. Wasm::AbstractMachine WebAssemblyModule::m_machine;
  79. HashMap<Wasm::Linker::Name, Wasm::ExternValue> WebAssemblyModule::s_spec_test_namespace;
  80. TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
  81. {
  82. auto object = vm.argument(0).to_object(global_object);
  83. if (vm.exception())
  84. return {};
  85. if (!is<JS::Uint8Array>(object)) {
  86. vm.throw_exception<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module");
  87. return {};
  88. }
  89. auto& array = static_cast<JS::Uint8Array&>(*object);
  90. InputMemoryStream stream { array.data() };
  91. auto result = Wasm::Module::parse(stream);
  92. if (result.is_error()) {
  93. vm.throw_exception<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
  94. return {};
  95. }
  96. if (stream.handle_any_error()) {
  97. vm.throw_exception<JS::SyntaxError>(global_object, "Bianry stream contained errors");
  98. return {};
  99. }
  100. HashMap<Wasm::Linker::Name, Wasm::ExternValue> imports;
  101. auto import_value = vm.argument(1);
  102. if (import_value.is_object()) {
  103. auto& import_object = import_value.as_object();
  104. for (auto& property : import_object.shape().property_table()) {
  105. auto value = import_object.get_without_side_effects(property.key);
  106. if (!value.is_object() || !is<WebAssemblyModule>(value.as_object()))
  107. continue;
  108. auto& module_object = static_cast<WebAssemblyModule&>(value.as_object());
  109. for (auto& entry : module_object.module_instance().exports()) {
  110. // FIXME: Don't pretend that everything is a function
  111. imports.set({ property.key.as_string(), entry.name(), Wasm::TypeIndex(0) }, entry.value());
  112. }
  113. }
  114. }
  115. return WebAssemblyModule::create(global_object, result.release_value(), imports);
  116. }
  117. TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
  118. {
  119. auto lhs = vm.argument(0).to_object(global_object);
  120. if (vm.exception())
  121. return {};
  122. if (!is<JS::TypedArrayBase>(lhs)) {
  123. vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
  124. return {};
  125. }
  126. auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
  127. auto rhs = vm.argument(1).to_object(global_object);
  128. if (vm.exception())
  129. return {};
  130. if (!is<JS::TypedArrayBase>(rhs)) {
  131. vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
  132. return {};
  133. }
  134. auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
  135. return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
  136. }
  137. void WebAssemblyModule::initialize(JS::GlobalObject& global_object)
  138. {
  139. Base::initialize(global_object);
  140. define_native_function("getExport", get_export, 1, JS::default_attributes);
  141. define_native_function("invoke", wasm_invoke, 1, JS::default_attributes);
  142. }
  143. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
  144. {
  145. auto name = vm.argument(0).to_string(global_object);
  146. if (vm.exception())
  147. return {};
  148. auto this_value = vm.this_value(global_object);
  149. auto object = this_value.to_object(global_object);
  150. if (vm.exception())
  151. return {};
  152. if (!object || !is<WebAssemblyModule>(object)) {
  153. vm.throw_exception<JS::TypeError>(global_object, "Not a WebAssemblyModule");
  154. return {};
  155. }
  156. auto instance = static_cast<WebAssemblyModule*>(object);
  157. for (auto& entry : instance->module_instance().exports()) {
  158. if (entry.name() == name) {
  159. auto& value = entry.value();
  160. if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
  161. return JS::Value(static_cast<unsigned long>(ptr->value()));
  162. if (auto v = value.get_pointer<Wasm::GlobalAddress>()) {
  163. return m_machine.store().get(*v)->value().value().visit(
  164. [&](const auto& value) -> JS::Value { return JS::Value(static_cast<double>(value)); },
  165. [&](const Wasm::Reference& reference) -> JS::Value {
  166. return reference.ref().visit(
  167. [&](const Wasm::Reference::Null&) -> JS::Value { return JS::js_null(); },
  168. [&](const auto& ref) -> JS::Value { return JS::Value(static_cast<double>(ref.address.value())); });
  169. });
  170. }
  171. vm.throw_exception<JS::TypeError>(global_object, String::formatted("'{}' does not refer to a function or a global", name));
  172. return {};
  173. }
  174. }
  175. vm.throw_exception<JS::TypeError>(global_object, String::formatted("'{}' could not be found", name));
  176. return {};
  177. }
  178. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
  179. {
  180. auto address = static_cast<unsigned long>(vm.argument(0).to_double(global_object));
  181. if (vm.exception())
  182. return {};
  183. Wasm::FunctionAddress function_address { address };
  184. auto function_instance = WebAssemblyModule::machine().store().get(function_address);
  185. if (!function_instance) {
  186. vm.throw_exception<JS::TypeError>(global_object, "Invalid function address");
  187. return {};
  188. }
  189. const Wasm::FunctionType* type { nullptr };
  190. function_instance->visit([&](auto& value) { type = &value.type(); });
  191. if (!type) {
  192. vm.throw_exception<JS::TypeError>(global_object, "Invalid function found at given address");
  193. return {};
  194. }
  195. Vector<Wasm::Value> arguments;
  196. if (type->parameters().size() + 1 > vm.argument_count()) {
  197. vm.throw_exception<JS::TypeError>(global_object, String::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count()));
  198. return {};
  199. }
  200. size_t index = 1;
  201. for (auto& param : type->parameters()) {
  202. auto value = vm.argument(index++).to_double(global_object);
  203. switch (param.kind()) {
  204. case Wasm::ValueType::Kind::I32:
  205. arguments.append(Wasm::Value(param, static_cast<u64>(value)));
  206. break;
  207. case Wasm::ValueType::Kind::I64:
  208. arguments.append(Wasm::Value(param, static_cast<u64>(value)));
  209. break;
  210. case Wasm::ValueType::Kind::F32:
  211. arguments.append(Wasm::Value(static_cast<float>(value)));
  212. break;
  213. case Wasm::ValueType::Kind::F64:
  214. arguments.append(Wasm::Value(static_cast<double>(value)));
  215. break;
  216. case Wasm::ValueType::Kind::FunctionReference:
  217. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Func { static_cast<u64>(value) } }));
  218. break;
  219. case Wasm::ValueType::Kind::ExternReference:
  220. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Func { static_cast<u64>(value) } }));
  221. break;
  222. case Wasm::ValueType::Kind::NullFunctionReference:
  223. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::FunctionReference) } }));
  224. break;
  225. case Wasm::ValueType::Kind::NullExternReference:
  226. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::ExternReference) } }));
  227. break;
  228. }
  229. }
  230. auto result = WebAssemblyModule::machine().invoke(function_address, arguments);
  231. if (result.is_trap()) {
  232. vm.throw_exception<JS::TypeError>(global_object, String::formatted("Execution trapped: {}", result.trap().reason));
  233. return {};
  234. }
  235. if (result.values().is_empty())
  236. return JS::js_null();
  237. JS::Value return_value;
  238. result.values().first().value().visit(
  239. [&](const auto& value) { return_value = JS::Value(static_cast<double>(value)); },
  240. [&](const Wasm::Reference& reference) {
  241. reference.ref().visit(
  242. [&](const Wasm::Reference::Null&) { return_value = JS::js_null(); },
  243. [&](const auto& ref) { return_value = JS::Value(static_cast<double>(ref.address.value())); });
  244. });
  245. return return_value;
  246. }