test-wasm.cpp 14 KB

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