test-wasm.cpp 12 KB

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