test-wasm.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 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)
  37. {
  38. auto instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype());
  39. instance->m_module = move(module);
  40. if (auto result = machine().instantiate(*instance->m_module, {}); result.is_error())
  41. global_object.vm().throw_exception<JS::TypeError>(global_object, result.release_error().error);
  42. else
  43. instance->m_module_instance = result.release_value();
  44. return instance;
  45. }
  46. void initialize(JS::GlobalObject&) override;
  47. ~WebAssemblyModule() override = default;
  48. private:
  49. JS_DECLARE_NATIVE_FUNCTION(get_export);
  50. JS_DECLARE_NATIVE_FUNCTION(wasm_invoke);
  51. static Wasm::AbstractMachine m_machine;
  52. Optional<Wasm::Module> m_module;
  53. OwnPtr<Wasm::ModuleInstance> m_module_instance;
  54. };
  55. Wasm::AbstractMachine WebAssemblyModule::m_machine;
  56. TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
  57. {
  58. auto object = vm.argument(0).to_object(global_object);
  59. if (vm.exception())
  60. return {};
  61. if (!is<JS::Uint8Array>(object)) {
  62. vm.throw_exception<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module");
  63. return {};
  64. }
  65. auto& array = static_cast<JS::Uint8Array&>(*object);
  66. InputMemoryStream stream { array.data() };
  67. auto result = Wasm::Module::parse(stream);
  68. if (result.is_error()) {
  69. vm.throw_exception<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
  70. return {};
  71. }
  72. if (stream.handle_any_error()) {
  73. vm.throw_exception<JS::SyntaxError>(global_object, "Bianry stream contained errors");
  74. return {};
  75. }
  76. return WebAssemblyModule::create(global_object, result.release_value());
  77. }
  78. TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
  79. {
  80. auto lhs = vm.argument(0).to_object(global_object);
  81. if (vm.exception())
  82. return {};
  83. if (!is<JS::TypedArrayBase>(lhs)) {
  84. vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
  85. return {};
  86. }
  87. auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
  88. auto rhs = vm.argument(1).to_object(global_object);
  89. if (vm.exception())
  90. return {};
  91. if (!is<JS::TypedArrayBase>(rhs)) {
  92. vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
  93. return {};
  94. }
  95. auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
  96. return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
  97. }
  98. void WebAssemblyModule::initialize(JS::GlobalObject& global_object)
  99. {
  100. Base::initialize(global_object);
  101. define_native_function("getExport", get_export);
  102. define_native_function("invoke", wasm_invoke);
  103. }
  104. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
  105. {
  106. auto name = vm.argument(0).to_string(global_object);
  107. if (vm.exception())
  108. return {};
  109. auto this_value = vm.this_value(global_object);
  110. auto object = this_value.to_object(global_object);
  111. if (vm.exception())
  112. return {};
  113. if (!object || !is<WebAssemblyModule>(object)) {
  114. vm.throw_exception<JS::TypeError>(global_object, "Not a WebAssemblyModule");
  115. return {};
  116. }
  117. auto instance = static_cast<WebAssemblyModule*>(object);
  118. for (auto& entry : instance->module_instance().exports()) {
  119. if (entry.name() == name) {
  120. auto& value = entry.value();
  121. if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
  122. return JS::Value(static_cast<unsigned long>(ptr->value()));
  123. vm.throw_exception<JS::TypeError>(global_object, String::formatted("'{}' does not refer to a function", name));
  124. return {};
  125. }
  126. }
  127. vm.throw_exception<JS::TypeError>(global_object, String::formatted("'{}' could not be found", name));
  128. return {};
  129. }
  130. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
  131. {
  132. auto address = static_cast<unsigned long>(vm.argument(0).to_double(global_object));
  133. if (vm.exception())
  134. return {};
  135. Wasm::FunctionAddress function_address { address };
  136. auto function_instance = WebAssemblyModule::machine().store().get(function_address);
  137. if (!function_instance) {
  138. vm.throw_exception<JS::TypeError>(global_object, "Invalid function address");
  139. return {};
  140. }
  141. const Wasm::FunctionType* type { nullptr };
  142. function_instance->visit([&](auto& value) { type = &value.type(); });
  143. if (!type) {
  144. vm.throw_exception<JS::TypeError>(global_object, "Invalid function found at given address");
  145. return {};
  146. }
  147. Vector<Wasm::Value> arguments;
  148. if (type->parameters().size() + 1 > vm.argument_count()) {
  149. vm.throw_exception<JS::TypeError>(global_object, String::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count()));
  150. return {};
  151. }
  152. size_t index = 1;
  153. for (auto& param : type->parameters()) {
  154. auto value = vm.argument(index++).to_double(global_object);
  155. switch (param.kind()) {
  156. case Wasm::ValueType::Kind::I32:
  157. arguments.append(Wasm::Value(param, static_cast<u64>(value)));
  158. break;
  159. case Wasm::ValueType::Kind::I64:
  160. arguments.append(Wasm::Value(param, static_cast<u64>(value)));
  161. break;
  162. case Wasm::ValueType::Kind::F32:
  163. arguments.append(Wasm::Value(static_cast<float>(value)));
  164. break;
  165. case Wasm::ValueType::Kind::F64:
  166. arguments.append(Wasm::Value(static_cast<double>(value)));
  167. break;
  168. case Wasm::ValueType::Kind::FunctionReference:
  169. arguments.append(Wasm::Value(Wasm::FunctionAddress { static_cast<u64>(value) }));
  170. break;
  171. case Wasm::ValueType::Kind::ExternReference:
  172. arguments.append(Wasm::Value(Wasm::ExternAddress { static_cast<u64>(value) }));
  173. break;
  174. case Wasm::ValueType::Kind::NullFunctionReference:
  175. arguments.append(Wasm::Value(Wasm::Value::Null { Wasm::ValueType(Wasm::ValueType::Kind::FunctionReference) }));
  176. break;
  177. case Wasm::ValueType::Kind::NullExternReference:
  178. arguments.append(Wasm::Value(Wasm::Value::Null { Wasm::ValueType(Wasm::ValueType::Kind::ExternReference) }));
  179. break;
  180. }
  181. }
  182. auto result = WebAssemblyModule::machine().invoke(function_address, arguments);
  183. if (result.is_trap()) {
  184. vm.throw_exception<JS::TypeError>(global_object, "Execution trapped");
  185. return {};
  186. }
  187. if (result.values().is_empty())
  188. return JS::js_null();
  189. JS::Value return_value;
  190. result.values().first().value().visit(
  191. [&](const auto& value) { return_value = JS::Value(static_cast<double>(value)); },
  192. [&](const Wasm::FunctionAddress& index) { return_value = JS::Value(static_cast<double>(index.value())); },
  193. [&](const Wasm::ExternAddress& index) { return_value = JS::Value(static_cast<double>(index.value())); },
  194. [&](const Wasm::Value::Null&) { return_value = JS::js_null(); });
  195. return return_value;
  196. }