test-wasm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/MemoryStream.h>
  7. #include <LibCore/Stream.h>
  8. #include <LibTest/JavaScriptTestRunner.h>
  9. #include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
  10. #include <LibWasm/Types.h>
  11. #include <string.h>
  12. TEST_ROOT("Userland/Libraries/LibWasm/Tests");
  13. TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
  14. {
  15. auto& realm = *vm.current_realm();
  16. auto filename = TRY(vm.argument(0).to_deprecated_string(vm));
  17. auto file = Core::Stream::File::open(filename, Core::Stream::OpenMode::Read);
  18. if (file.is_error())
  19. return vm.throw_completion<JS::TypeError>(strerror(file.error().code()));
  20. auto file_size = file.value()->size();
  21. if (file_size.is_error())
  22. return vm.throw_completion<JS::TypeError>(strerror(file_size.error().code()));
  23. auto array = TRY(JS::Uint8Array::create(realm, file_size.value()));
  24. auto read = file.value()->read(array->data());
  25. if (read.is_error())
  26. return vm.throw_completion<JS::TypeError>(strerror(read.error().code()));
  27. return JS::Value(array);
  28. }
  29. class WebAssemblyModule final : public JS::Object {
  30. JS_OBJECT(WebAssemblyModule, JS::Object);
  31. public:
  32. explicit WebAssemblyModule(JS::Object& prototype)
  33. : JS::Object(ConstructWithPrototypeTag::Tag, prototype)
  34. {
  35. m_machine.enable_instruction_count_limit();
  36. }
  37. static Wasm::AbstractMachine& machine() { return m_machine; }
  38. Wasm::Module& module() { return *m_module; }
  39. Wasm::ModuleInstance& module_instance() { return *m_module_instance; }
  40. static JS::ThrowCompletionOr<WebAssemblyModule*> create(JS::Realm& realm, Wasm::Module module, HashMap<Wasm::Linker::Name, Wasm::ExternValue> const& imports)
  41. {
  42. auto& vm = realm.vm();
  43. auto instance = realm.heap().allocate<WebAssemblyModule>(realm, *realm.intrinsics().object_prototype());
  44. instance->m_module = move(module);
  45. Wasm::Linker linker(*instance->m_module);
  46. linker.link(imports);
  47. linker.link(spec_test_namespace());
  48. auto link_result = linker.finish();
  49. if (link_result.is_error())
  50. return vm.throw_completion<JS::TypeError>("Link failed");
  51. auto result = machine().instantiate(*instance->m_module, link_result.release_value());
  52. if (result.is_error())
  53. return vm.throw_completion<JS::TypeError>(result.release_error().error);
  54. instance->m_module_instance = result.release_value();
  55. return instance.ptr();
  56. }
  57. void initialize(JS::Realm&) override;
  58. ~WebAssemblyModule() override = default;
  59. private:
  60. JS_DECLARE_NATIVE_FUNCTION(get_export);
  61. JS_DECLARE_NATIVE_FUNCTION(wasm_invoke);
  62. static HashMap<Wasm::Linker::Name, Wasm::ExternValue> const& spec_test_namespace()
  63. {
  64. if (!s_spec_test_namespace.is_empty())
  65. return s_spec_test_namespace;
  66. Wasm::FunctionType print_i32_type { { Wasm::ValueType(Wasm::ValueType::I32) }, {} };
  67. auto address = m_machine.store().allocate(Wasm::HostFunction {
  68. [](auto&, auto&) -> Wasm::Result {
  69. // Noop, this just needs to exist.
  70. return Wasm::Result { Vector<Wasm::Value> {} };
  71. },
  72. print_i32_type });
  73. s_spec_test_namespace.set({ "spectest", "print_i32", print_i32_type }, Wasm::ExternValue { *address });
  74. return s_spec_test_namespace;
  75. }
  76. static HashMap<Wasm::Linker::Name, Wasm::ExternValue> s_spec_test_namespace;
  77. static Wasm::AbstractMachine m_machine;
  78. Optional<Wasm::Module> m_module;
  79. OwnPtr<Wasm::ModuleInstance> m_module_instance;
  80. };
  81. Wasm::AbstractMachine WebAssemblyModule::m_machine;
  82. HashMap<Wasm::Linker::Name, Wasm::ExternValue> WebAssemblyModule::s_spec_test_namespace;
  83. TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
  84. {
  85. auto& realm = *vm.current_realm();
  86. auto* object = TRY(vm.argument(0).to_object(vm));
  87. if (!is<JS::Uint8Array>(object))
  88. return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module");
  89. auto& array = static_cast<JS::Uint8Array&>(*object);
  90. auto stream = Core::Stream::FixedMemoryStream::construct(array.data()).release_value_but_fixme_should_propagate_errors();
  91. auto result = Wasm::Module::parse(*stream);
  92. if (result.is_error())
  93. return vm.throw_completion<JS::SyntaxError>(Wasm::parse_error_to_deprecated_string(result.error()));
  94. HashMap<Wasm::Linker::Name, Wasm::ExternValue> imports;
  95. auto import_value = vm.argument(1);
  96. if (import_value.is_object()) {
  97. auto& import_object = import_value.as_object();
  98. for (auto& property : import_object.shape().property_table()) {
  99. auto value = import_object.get_without_side_effects(property.key);
  100. if (!value.is_object() || !is<WebAssemblyModule>(value.as_object()))
  101. continue;
  102. auto& module_object = static_cast<WebAssemblyModule&>(value.as_object());
  103. for (auto& entry : module_object.module_instance().exports()) {
  104. // FIXME: Don't pretend that everything is a function
  105. imports.set({ property.key.as_string(), entry.name(), Wasm::TypeIndex(0) }, entry.value());
  106. }
  107. }
  108. }
  109. return JS::Value(TRY(WebAssemblyModule::create(realm, result.release_value(), imports)));
  110. }
  111. TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
  112. {
  113. auto* lhs = TRY(vm.argument(0).to_object(vm));
  114. if (!is<JS::TypedArrayBase>(lhs))
  115. return vm.throw_completion<JS::TypeError>("Expected a TypedArray");
  116. auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
  117. auto* rhs = TRY(vm.argument(1).to_object(vm));
  118. if (!is<JS::TypedArrayBase>(rhs))
  119. return vm.throw_completion<JS::TypeError>("Expected a TypedArray");
  120. auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
  121. return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
  122. }
  123. void WebAssemblyModule::initialize(JS::Realm& realm)
  124. {
  125. Base::initialize(realm);
  126. define_native_function(realm, "getExport", get_export, 1, JS::default_attributes);
  127. define_native_function(realm, "invoke", wasm_invoke, 1, JS::default_attributes);
  128. }
  129. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
  130. {
  131. auto name = TRY(vm.argument(0).to_deprecated_string(vm));
  132. auto this_value = vm.this_value();
  133. auto* object = TRY(this_value.to_object(vm));
  134. if (!is<WebAssemblyModule>(object))
  135. return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule");
  136. auto instance = static_cast<WebAssemblyModule*>(object);
  137. for (auto& entry : instance->module_instance().exports()) {
  138. if (entry.name() == name) {
  139. auto& value = entry.value();
  140. if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
  141. return JS::Value(static_cast<unsigned long>(ptr->value()));
  142. if (auto v = value.get_pointer<Wasm::GlobalAddress>()) {
  143. return m_machine.store().get(*v)->value().value().visit(
  144. [&](auto const& value) -> JS::Value { return JS::Value(static_cast<double>(value)); },
  145. [&](i32 value) { return JS::Value(static_cast<double>(value)); },
  146. [&](i64 value) -> JS::Value { return JS::BigInt::create(vm, Crypto::SignedBigInteger { value }); },
  147. [&](Wasm::Reference const& reference) -> JS::Value {
  148. return reference.ref().visit(
  149. [&](const Wasm::Reference::Null&) -> JS::Value { return JS::js_null(); },
  150. [&](const auto& ref) -> JS::Value { return JS::Value(static_cast<double>(ref.address.value())); });
  151. });
  152. }
  153. return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("'{}' does not refer to a function or a global", name));
  154. }
  155. }
  156. return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("'{}' could not be found", name));
  157. }
  158. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
  159. {
  160. auto address = static_cast<unsigned long>(TRY(vm.argument(0).to_double(vm)));
  161. Wasm::FunctionAddress function_address { address };
  162. auto function_instance = WebAssemblyModule::machine().store().get(function_address);
  163. if (!function_instance)
  164. return vm.throw_completion<JS::TypeError>("Invalid function address");
  165. Wasm::FunctionType const* type { nullptr };
  166. function_instance->visit([&](auto& value) { type = &value.type(); });
  167. if (!type)
  168. return vm.throw_completion<JS::TypeError>("Invalid function found at given address");
  169. Vector<Wasm::Value> arguments;
  170. if (type->parameters().size() + 1 > vm.argument_count())
  171. return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count()));
  172. size_t index = 1;
  173. for (auto& param : type->parameters()) {
  174. auto argument = vm.argument(index++);
  175. double double_value = 0;
  176. if (!argument.is_bigint())
  177. double_value = TRY(argument.to_double(vm));
  178. switch (param.kind()) {
  179. case Wasm::ValueType::Kind::I32:
  180. arguments.append(Wasm::Value(param, static_cast<i64>(double_value)));
  181. break;
  182. case Wasm::ValueType::Kind::I64:
  183. if (argument.is_bigint()) {
  184. auto value = TRY(argument.to_bigint_int64(vm));
  185. arguments.append(Wasm::Value(param, value));
  186. } else {
  187. arguments.append(Wasm::Value(param, static_cast<i64>(double_value)));
  188. }
  189. break;
  190. case Wasm::ValueType::Kind::F32:
  191. arguments.append(Wasm::Value(static_cast<float>(double_value)));
  192. break;
  193. case Wasm::ValueType::Kind::F64:
  194. arguments.append(Wasm::Value(static_cast<double>(double_value)));
  195. break;
  196. case Wasm::ValueType::Kind::FunctionReference:
  197. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Func { static_cast<u64>(double_value) } }));
  198. break;
  199. case Wasm::ValueType::Kind::ExternReference:
  200. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Func { static_cast<u64>(double_value) } }));
  201. break;
  202. case Wasm::ValueType::Kind::NullFunctionReference:
  203. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::FunctionReference) } }));
  204. break;
  205. case Wasm::ValueType::Kind::NullExternReference:
  206. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::ExternReference) } }));
  207. break;
  208. }
  209. }
  210. auto result = WebAssemblyModule::machine().invoke(function_address, arguments);
  211. if (result.is_trap())
  212. return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("Execution trapped: {}", result.trap().reason));
  213. if (result.values().is_empty())
  214. return JS::js_null();
  215. JS::Value return_value;
  216. result.values().first().value().visit(
  217. [&](auto const& value) { return_value = JS::Value(static_cast<double>(value)); },
  218. [&](i32 value) { return_value = JS::Value(static_cast<double>(value)); },
  219. [&](i64 value) { return_value = JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger { value })); },
  220. [&](Wasm::Reference const& reference) {
  221. reference.ref().visit(
  222. [&](const Wasm::Reference::Null&) { return_value = JS::js_null(); },
  223. [&](const auto& ref) { return_value = JS::Value(static_cast<double>(ref.address.value())); });
  224. });
  225. return return_value;
  226. }