test-wasm.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. Wasm::FunctionType print_type { {}, {} };
  68. auto address_print = alloc_noop_function(print_type);
  69. s_spec_test_namespace.set({ "spectest", "print", print_type }, Wasm::ExternValue { *address_print });
  70. Wasm::FunctionType print_i32_type { { Wasm::ValueType(Wasm::ValueType::I32) }, {} };
  71. auto address_i32 = alloc_noop_function(print_i32_type);
  72. s_spec_test_namespace.set({ "spectest", "print_i32", print_i32_type }, Wasm::ExternValue { *address_i32 });
  73. Wasm::FunctionType print_i64_type { { Wasm::ValueType(Wasm::ValueType::I64) }, {} };
  74. auto address_i64 = alloc_noop_function(print_i64_type);
  75. s_spec_test_namespace.set({ "spectest", "print_i64", print_i64_type }, Wasm::ExternValue { *address_i64 });
  76. Wasm::FunctionType print_f32_type { { Wasm::ValueType(Wasm::ValueType::F32) }, {} };
  77. auto address_f32 = alloc_noop_function(print_f32_type);
  78. s_spec_test_namespace.set({ "spectest", "print_f32", print_f32_type }, Wasm::ExternValue { *address_f32 });
  79. Wasm::FunctionType print_f64_type { { Wasm::ValueType(Wasm::ValueType::F64) }, {} };
  80. auto address_f64 = alloc_noop_function(print_f64_type);
  81. s_spec_test_namespace.set({ "spectest", "print_f64", print_f64_type }, Wasm::ExternValue { *address_f64 });
  82. Wasm::FunctionType print_i32_f32_type { { Wasm::ValueType(Wasm::ValueType::I32), Wasm::ValueType(Wasm::ValueType::F32) }, {} };
  83. auto address_i32_f32 = alloc_noop_function(print_i32_f32_type);
  84. s_spec_test_namespace.set({ "spectest", "print_i32_f32", print_i32_f32_type }, Wasm::ExternValue { *address_i32_f32 });
  85. Wasm::FunctionType print_f64_f64_type { { Wasm::ValueType(Wasm::ValueType::F64), Wasm::ValueType(Wasm::ValueType::F64) }, {} };
  86. auto address_f64_f64 = alloc_noop_function(print_f64_f64_type);
  87. s_spec_test_namespace.set({ "spectest", "print_f64_f64", print_f64_f64_type }, Wasm::ExternValue { *address_f64_f64 });
  88. Wasm::TableType table_type { Wasm::ValueType(Wasm::ValueType::FunctionReference), Wasm::Limits(10, 20) };
  89. auto table_address = m_machine.store().allocate(table_type);
  90. s_spec_test_namespace.set({ "spectest", "table", table_type }, Wasm::ExternValue { *table_address });
  91. Wasm::MemoryType memory_type { Wasm::Limits(1, 2) };
  92. auto memory_address = m_machine.store().allocate(memory_type);
  93. s_spec_test_namespace.set({ "spectest", "memory", memory_type }, Wasm::ExternValue { *memory_address });
  94. Wasm::GlobalType global_i32 { Wasm::ValueType(Wasm::ValueType::I32), false };
  95. auto global_i32_address = m_machine.store().allocate(global_i32, Wasm::Value(666));
  96. s_spec_test_namespace.set({ "spectest", "global_i32", global_i32 }, Wasm::ExternValue { *global_i32_address });
  97. Wasm::GlobalType global_i64 { Wasm::ValueType(Wasm::ValueType::I64), false };
  98. auto global_i64_address = m_machine.store().allocate(global_i64, Wasm::Value((i64)666));
  99. s_spec_test_namespace.set({ "spectest", "global_i64", global_i64 }, Wasm::ExternValue { *global_i64_address });
  100. Wasm::GlobalType global_f32 { Wasm::ValueType(Wasm::ValueType::F32), false };
  101. auto global_f32_address = m_machine.store().allocate(global_f32, Wasm::Value(666.6f));
  102. s_spec_test_namespace.set({ "spectest", "global_f32", global_f32 }, Wasm::ExternValue { *global_f32_address });
  103. Wasm::GlobalType global_f64 { Wasm::ValueType(Wasm::ValueType::F64), false };
  104. auto global_f64_address = m_machine.store().allocate(global_f64, Wasm::Value(666.6));
  105. s_spec_test_namespace.set({ "spectest", "global_f64", global_f64 }, Wasm::ExternValue { *global_f64_address });
  106. return s_spec_test_namespace;
  107. }
  108. static Optional<Wasm::FunctionAddress> alloc_noop_function(Wasm::FunctionType type)
  109. {
  110. return m_machine.store().allocate(Wasm::HostFunction {
  111. [](auto&, auto&) -> Wasm::Result {
  112. // Noop, this just needs to exist.
  113. return Wasm::Result { Vector<Wasm::Value> {} };
  114. },
  115. type });
  116. }
  117. static HashMap<Wasm::Linker::Name, Wasm::ExternValue> s_spec_test_namespace;
  118. static Wasm::AbstractMachine m_machine;
  119. Optional<Wasm::Module> m_module;
  120. OwnPtr<Wasm::ModuleInstance> m_module_instance;
  121. };
  122. Wasm::AbstractMachine WebAssemblyModule::m_machine;
  123. HashMap<Wasm::Linker::Name, Wasm::ExternValue> WebAssemblyModule::s_spec_test_namespace;
  124. TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
  125. {
  126. auto& realm = *vm.current_realm();
  127. auto object = TRY(vm.argument(0).to_object(vm));
  128. if (!is<JS::Uint8Array>(*object))
  129. return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module"sv);
  130. auto& array = static_cast<JS::Uint8Array&>(*object);
  131. FixedMemoryStream stream { array.data() };
  132. auto result = Wasm::Module::parse(stream);
  133. if (result.is_error())
  134. return vm.throw_completion<JS::SyntaxError>(Wasm::parse_error_to_byte_string(result.error()));
  135. HashMap<Wasm::Linker::Name, Wasm::ExternValue> imports;
  136. auto import_value = vm.argument(1);
  137. if (import_value.is_object()) {
  138. auto& import_object = import_value.as_object();
  139. for (auto& property : import_object.shape().property_table()) {
  140. auto value = import_object.get_without_side_effects(property.key);
  141. if (!value.is_object() || !is<WebAssemblyModule>(value.as_object()))
  142. continue;
  143. auto& module_object = static_cast<WebAssemblyModule&>(value.as_object());
  144. for (auto& entry : module_object.module_instance().exports()) {
  145. // FIXME: Don't pretend that everything is a function
  146. imports.set({ property.key.as_string(), entry.name(), Wasm::TypeIndex(0) }, entry.value());
  147. }
  148. }
  149. }
  150. return JS::Value(TRY(WebAssemblyModule::create(realm, result.release_value(), imports)));
  151. }
  152. TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
  153. {
  154. auto lhs = TRY(vm.argument(0).to_object(vm));
  155. if (!is<JS::TypedArrayBase>(*lhs))
  156. return vm.throw_completion<JS::TypeError>("Expected a TypedArray"sv);
  157. auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
  158. auto rhs = TRY(vm.argument(1).to_object(vm));
  159. if (!is<JS::TypedArrayBase>(*rhs))
  160. return vm.throw_completion<JS::TypeError>("Expected a TypedArray"sv);
  161. auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
  162. return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
  163. }
  164. void WebAssemblyModule::initialize(JS::Realm& realm)
  165. {
  166. Base::initialize(realm);
  167. define_native_function(realm, "getExport", get_export, 1, JS::default_attributes);
  168. define_native_function(realm, "invoke", wasm_invoke, 1, JS::default_attributes);
  169. }
  170. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
  171. {
  172. auto name = TRY(vm.argument(0).to_byte_string(vm));
  173. auto this_value = vm.this_value();
  174. auto object = TRY(this_value.to_object(vm));
  175. if (!is<WebAssemblyModule>(*object))
  176. return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule"sv);
  177. auto& instance = static_cast<WebAssemblyModule&>(*object);
  178. for (auto& entry : instance.module_instance().exports()) {
  179. if (entry.name() == name) {
  180. auto& value = entry.value();
  181. if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
  182. return JS::Value(static_cast<unsigned long>(ptr->value()));
  183. if (auto v = value.get_pointer<Wasm::GlobalAddress>()) {
  184. return m_machine.store().get(*v)->value().value().visit(
  185. [&](auto const& value) -> JS::Value { return JS::Value(static_cast<double>(value)); },
  186. [&](i32 value) { return JS::Value(static_cast<double>(value)); },
  187. [&](i64 value) -> JS::Value { return JS::BigInt::create(vm, Crypto::SignedBigInteger { value }); },
  188. [&](u128 value) -> JS::Value { return JS::BigInt::create(vm, Crypto::SignedBigInteger::import_data(bit_cast<u8 const*>(&value), sizeof(value))); },
  189. [&](Wasm::Reference const& reference) -> JS::Value {
  190. return reference.ref().visit(
  191. [&](Wasm::Reference::Null const&) -> JS::Value { return JS::js_null(); },
  192. [&](auto const& ref) -> JS::Value { return JS::Value(static_cast<double>(ref.address.value())); });
  193. });
  194. }
  195. return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' does not refer to a function or a global", name)));
  196. }
  197. }
  198. return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' could not be found", name)));
  199. }
  200. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
  201. {
  202. auto address = static_cast<unsigned long>(TRY(vm.argument(0).to_double(vm)));
  203. Wasm::FunctionAddress function_address { address };
  204. auto function_instance = WebAssemblyModule::machine().store().get(function_address);
  205. if (!function_instance)
  206. return vm.throw_completion<JS::TypeError>("Invalid function address"sv);
  207. Wasm::FunctionType const* type { nullptr };
  208. function_instance->visit([&](auto& value) { type = &value.type(); });
  209. if (!type)
  210. return vm.throw_completion<JS::TypeError>("Invalid function found at given address"sv);
  211. Vector<Wasm::Value> arguments;
  212. if (type->parameters().size() + 1 > vm.argument_count())
  213. 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())));
  214. size_t index = 1;
  215. for (auto& param : type->parameters()) {
  216. auto argument = vm.argument(index++);
  217. double double_value = 0;
  218. if (!argument.is_bigint())
  219. double_value = TRY(argument.to_double(vm));
  220. switch (param.kind()) {
  221. case Wasm::ValueType::Kind::I32:
  222. arguments.append(Wasm::Value(param, static_cast<i64>(double_value)));
  223. break;
  224. case Wasm::ValueType::Kind::I64:
  225. if (argument.is_bigint()) {
  226. auto value = TRY(argument.to_bigint_int64(vm));
  227. arguments.append(Wasm::Value(param, value));
  228. } else {
  229. arguments.append(Wasm::Value(param, static_cast<i64>(double_value)));
  230. }
  231. break;
  232. case Wasm::ValueType::Kind::F32:
  233. arguments.append(Wasm::Value(static_cast<float>(double_value)));
  234. break;
  235. case Wasm::ValueType::Kind::F64:
  236. arguments.append(Wasm::Value(static_cast<double>(double_value)));
  237. break;
  238. case Wasm::ValueType::Kind::V128: {
  239. if (!argument.is_bigint()) {
  240. if (argument.is_number())
  241. argument = JS::BigInt::create(vm, Crypto::SignedBigInteger { TRY(argument.to_double(vm)) });
  242. else
  243. argument = TRY(argument.to_bigint(vm));
  244. }
  245. u128 bits = 0;
  246. (void)argument.as_bigint().big_integer().unsigned_value().export_data({ bit_cast<u8*>(&bits), sizeof(bits) });
  247. VERIFY(!argument.as_bigint().big_integer().is_negative());
  248. arguments.append(Wasm::Value(bits));
  249. break;
  250. }
  251. case Wasm::ValueType::Kind::FunctionReference:
  252. if (argument.is_null()) {
  253. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::FunctionReference) } }));
  254. break;
  255. }
  256. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Func { static_cast<u64>(double_value) } }));
  257. break;
  258. case Wasm::ValueType::Kind::ExternReference:
  259. if (argument.is_null()) {
  260. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::ExternReference) } }));
  261. break;
  262. }
  263. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Extern { static_cast<u64>(double_value) } }));
  264. break;
  265. case Wasm::ValueType::Kind::NullFunctionReference:
  266. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::FunctionReference) } }));
  267. break;
  268. case Wasm::ValueType::Kind::NullExternReference:
  269. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::ExternReference) } }));
  270. break;
  271. }
  272. }
  273. auto result = WebAssemblyModule::machine().invoke(function_address, arguments);
  274. if (result.is_trap())
  275. return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("Execution trapped: {}", result.trap().reason)));
  276. if (result.is_completion())
  277. return result.completion();
  278. if (result.values().is_empty())
  279. return JS::js_null();
  280. auto to_js_value = [&](Wasm::Value const& value) {
  281. return value.value().visit(
  282. [](auto const& value) { return JS::Value(static_cast<double>(value)); },
  283. [](i32 value) { return JS::Value(static_cast<double>(value)); },
  284. [&](i64 value) { return JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger { value })); },
  285. [&](u128 value) {
  286. auto unsigned_bigint_value = Crypto::UnsignedBigInteger::import_data(bit_cast<u8 const*>(&value), sizeof(value));
  287. return JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger(move(unsigned_bigint_value), false)));
  288. },
  289. [](Wasm::Reference const& reference) {
  290. return reference.ref().visit(
  291. [](Wasm::Reference::Null const&) { return JS::js_null(); },
  292. [](auto const& ref) { return JS::Value(static_cast<double>(ref.address.value())); });
  293. });
  294. };
  295. if (result.values().size() == 1)
  296. return to_js_value(result.values().first());
  297. return JS::Array::create_from<Wasm::Value>(*vm.current_realm(), result.values(), [&](Wasm::Value value) {
  298. return to_js_value(value);
  299. });
  300. }