test-wasm.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. "__TEST" });
  117. }
  118. static HashMap<Wasm::Linker::Name, Wasm::ExternValue> s_spec_test_namespace;
  119. static Wasm::AbstractMachine m_machine;
  120. Optional<Wasm::Module> m_module;
  121. OwnPtr<Wasm::ModuleInstance> m_module_instance;
  122. };
  123. Wasm::AbstractMachine WebAssemblyModule::m_machine;
  124. HashMap<Wasm::Linker::Name, Wasm::ExternValue> WebAssemblyModule::s_spec_test_namespace;
  125. TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
  126. {
  127. auto& realm = *vm.current_realm();
  128. auto object = TRY(vm.argument(0).to_object(vm));
  129. if (!is<JS::Uint8Array>(*object))
  130. return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module"sv);
  131. auto& array = static_cast<JS::Uint8Array&>(*object);
  132. FixedMemoryStream stream { array.data() };
  133. auto result = Wasm::Module::parse(stream);
  134. if (result.is_error())
  135. return vm.throw_completion<JS::SyntaxError>(Wasm::parse_error_to_byte_string(result.error()));
  136. HashMap<Wasm::Linker::Name, Wasm::ExternValue> imports;
  137. auto import_value = vm.argument(1);
  138. if (import_value.is_object()) {
  139. auto& import_object = import_value.as_object();
  140. for (auto& property : import_object.shape().property_table()) {
  141. auto value = import_object.get_without_side_effects(property.key);
  142. if (!value.is_object() || !is<WebAssemblyModule>(value.as_object()))
  143. continue;
  144. auto& module_object = static_cast<WebAssemblyModule&>(value.as_object());
  145. for (auto& entry : module_object.module_instance().exports()) {
  146. // FIXME: Don't pretend that everything is a function
  147. imports.set({ property.key.as_string(), entry.name(), Wasm::TypeIndex(0) }, entry.value());
  148. }
  149. }
  150. }
  151. return JS::Value(TRY(WebAssemblyModule::create(realm, result.release_value(), imports)));
  152. }
  153. TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
  154. {
  155. auto lhs = TRY(vm.argument(0).to_object(vm));
  156. if (!is<JS::TypedArrayBase>(*lhs))
  157. return vm.throw_completion<JS::TypeError>("Expected a TypedArray"sv);
  158. auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
  159. auto rhs = TRY(vm.argument(1).to_object(vm));
  160. if (!is<JS::TypedArrayBase>(*rhs))
  161. return vm.throw_completion<JS::TypeError>("Expected a TypedArray"sv);
  162. auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
  163. return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
  164. }
  165. void WebAssemblyModule::initialize(JS::Realm& realm)
  166. {
  167. Base::initialize(realm);
  168. define_native_function(realm, "getExport", get_export, 1, JS::default_attributes);
  169. define_native_function(realm, "invoke", wasm_invoke, 1, JS::default_attributes);
  170. }
  171. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
  172. {
  173. auto name = TRY(vm.argument(0).to_byte_string(vm));
  174. auto this_value = vm.this_value();
  175. auto object = TRY(this_value.to_object(vm));
  176. if (!is<WebAssemblyModule>(*object))
  177. return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule"sv);
  178. auto& instance = static_cast<WebAssemblyModule&>(*object);
  179. for (auto& entry : instance.module_instance().exports()) {
  180. if (entry.name() == name) {
  181. auto& value = entry.value();
  182. if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
  183. return JS::Value(static_cast<unsigned long>(ptr->value()));
  184. if (auto v = value.get_pointer<Wasm::GlobalAddress>()) {
  185. return m_machine.store().get(*v)->value().value().visit(
  186. [&](auto const& value) -> JS::Value { return JS::Value(static_cast<double>(value)); },
  187. [&](i32 value) { return JS::Value(static_cast<double>(value)); },
  188. [&](i64 value) -> JS::Value { return JS::BigInt::create(vm, Crypto::SignedBigInteger { value }); },
  189. [&](u128 value) -> JS::Value { return JS::BigInt::create(vm, Crypto::SignedBigInteger::import_data(bit_cast<u8 const*>(&value), sizeof(value))); },
  190. [&](Wasm::Reference const& reference) -> JS::Value {
  191. return reference.ref().visit(
  192. [&](Wasm::Reference::Null const&) -> JS::Value { return JS::js_null(); },
  193. [&](auto const& ref) -> JS::Value { return JS::Value(static_cast<double>(ref.address.value())); });
  194. });
  195. }
  196. return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' does not refer to a function or a global", name)));
  197. }
  198. }
  199. return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' could not be found", name)));
  200. }
  201. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
  202. {
  203. auto address = static_cast<unsigned long>(TRY(vm.argument(0).to_double(vm)));
  204. Wasm::FunctionAddress function_address { address };
  205. auto function_instance = WebAssemblyModule::machine().store().get(function_address);
  206. if (!function_instance)
  207. return vm.throw_completion<JS::TypeError>("Invalid function address"sv);
  208. Wasm::FunctionType const* type { nullptr };
  209. function_instance->visit([&](auto& value) { type = &value.type(); });
  210. if (!type)
  211. return vm.throw_completion<JS::TypeError>("Invalid function found at given address"sv);
  212. Vector<Wasm::Value> arguments;
  213. if (type->parameters().size() + 1 > vm.argument_count())
  214. 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())));
  215. size_t index = 1;
  216. for (auto& param : type->parameters()) {
  217. auto argument = vm.argument(index++);
  218. double double_value = 0;
  219. if (argument.is_object()) {
  220. auto object = MUST(argument.to_object(vm));
  221. // Uint8Array allows for raw bytes to be passed into Wasm. This is
  222. // particularly useful for NaN bit patterns
  223. if (!is<JS::Uint8Array>(*object))
  224. return vm.throw_completion<JS::TypeError>("Expected a Uint8Array object"sv);
  225. auto& array = static_cast<JS::Uint8Array&>(*object);
  226. if (array.array_length().length() > 8)
  227. return vm.throw_completion<JS::TypeError>("Expected a Uint8Array of size <= 8"sv);
  228. memcpy(&double_value, array.data().data(), array.array_length().length());
  229. } else if (!argument.is_bigint())
  230. double_value = TRY(argument.to_double(vm));
  231. switch (param.kind()) {
  232. case Wasm::ValueType::Kind::I32:
  233. arguments.append(Wasm::Value(param, static_cast<i64>(double_value)));
  234. break;
  235. case Wasm::ValueType::Kind::I64:
  236. if (argument.is_bigint()) {
  237. auto value = TRY(argument.to_bigint_int64(vm));
  238. arguments.append(Wasm::Value(param, value));
  239. } else {
  240. arguments.append(Wasm::Value(param, static_cast<i64>(double_value)));
  241. }
  242. break;
  243. case Wasm::ValueType::Kind::F32:
  244. // double_value should contain up to 8 bytes of information,
  245. // if we were passed a Uint8Array. If the expected arg is a
  246. // float, we were probably passed a Uint8Array of size 4. So
  247. // we copy those bytes into a float value.
  248. if (argument.is_object()) {
  249. float float_value = 0;
  250. memcpy(&float_value, &double_value, sizeof(float));
  251. arguments.append(Wasm::Value(float_value));
  252. } else {
  253. arguments.append(Wasm::Value(static_cast<float>(double_value)));
  254. }
  255. break;
  256. case Wasm::ValueType::Kind::F64:
  257. arguments.append(Wasm::Value(static_cast<double>(double_value)));
  258. break;
  259. case Wasm::ValueType::Kind::V128: {
  260. if (!argument.is_bigint()) {
  261. if (argument.is_number())
  262. argument = JS::BigInt::create(vm, Crypto::SignedBigInteger { TRY(argument.to_double(vm)) });
  263. else
  264. argument = TRY(argument.to_bigint(vm));
  265. }
  266. u128 bits = 0;
  267. auto bytes = argument.as_bigint().big_integer().unsigned_value().export_data({ bit_cast<u8*>(&bits), sizeof(bits) });
  268. VERIFY(!argument.as_bigint().big_integer().is_negative());
  269. if constexpr (AK::HostIsLittleEndian)
  270. arguments.append(Wasm::Value(bits << (128 - bytes * 8)));
  271. else
  272. arguments.append(Wasm::Value(bits >> (128 - bytes * 8)));
  273. break;
  274. }
  275. case Wasm::ValueType::Kind::FunctionReference:
  276. if (argument.is_null()) {
  277. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::FunctionReference) } }));
  278. break;
  279. }
  280. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Func { static_cast<u64>(double_value) } }));
  281. break;
  282. case Wasm::ValueType::Kind::ExternReference:
  283. if (argument.is_null()) {
  284. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Null { Wasm::ValueType(Wasm::ValueType::Kind::ExternReference) } }));
  285. break;
  286. }
  287. arguments.append(Wasm::Value(Wasm::Reference { Wasm::Reference::Extern { static_cast<u64>(double_value) } }));
  288. break;
  289. }
  290. }
  291. auto result = WebAssemblyModule::machine().invoke(function_address, arguments);
  292. if (result.is_trap())
  293. return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("Execution trapped: {}", result.trap().reason)));
  294. if (result.is_completion())
  295. return result.completion();
  296. if (result.values().is_empty())
  297. return JS::js_null();
  298. auto to_js_value = [&](Wasm::Value const& value) {
  299. return value.value().visit(
  300. [](auto const& value) { return JS::Value(static_cast<double>(value)); },
  301. [](i32 value) { return JS::Value(static_cast<double>(value)); },
  302. [&](i64 value) { return JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger { value })); },
  303. [&](u128 value) {
  304. auto unsigned_bigint_value = Crypto::UnsignedBigInteger::import_data(bit_cast<u8 const*>(&value), sizeof(value));
  305. return JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger(move(unsigned_bigint_value), false)));
  306. },
  307. [](Wasm::Reference const& reference) {
  308. return reference.ref().visit(
  309. [](Wasm::Reference::Null const&) { return JS::js_null(); },
  310. [](auto const& ref) { return JS::Value(static_cast<double>(ref.address.value())); });
  311. });
  312. };
  313. if (result.values().size() == 1)
  314. return to_js_value(result.values().first());
  315. return JS::Array::create_from<Wasm::Value>(*vm.current_realm(), result.values(), [&](Wasm::Value value) {
  316. return to_js_value(value);
  317. });
  318. }