| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> | 
					
						
							|  |  |  |  * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  | #include <LibJS/Runtime/SharedArrayBufferConstructor.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							|  |  |  | #include <LibWasm/Types.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  | #include <LibWeb/Bindings/MemoryPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | #include <LibWeb/WebAssembly/Memory.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-16 14:11:21 -04:00
										 |  |  | #include <LibWeb/WebAssembly/WebAssembly.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::WebAssembly { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(Memory); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<GC::Ref<Memory>> Memory::construct_impl(JS::Realm& realm, MemoryDescriptor& descriptor) | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = realm.vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  |     // https://webassembly.github.io/threads/js-api/index.html#dom-memory-memory
 | 
					
						
							|  |  |  |     // 4. Let share be shared if descriptor["shared"] is true and unshared otherwise.
 | 
					
						
							|  |  |  |     // 5. If share is shared and maximum is empty, throw a TypeError exception.
 | 
					
						
							|  |  |  |     auto shared = descriptor.shared.value_or(false); | 
					
						
							|  |  |  |     if (shared && !descriptor.maximum.has_value()) | 
					
						
							|  |  |  |         return vm.throw_completion<JS::TypeError>("Maximum has to be specified for shared memory."sv); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  |     Wasm::Limits limits { descriptor.initial, move(descriptor.maximum) }; | 
					
						
							|  |  |  |     Wasm::MemoryType memory_type { move(limits) }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-25 19:09:34 +01:00
										 |  |  |     auto& cache = Detail::get_cache(realm); | 
					
						
							|  |  |  |     auto address = cache.abstract_machine().store().allocate(memory_type); | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  |     if (!address.has_value()) | 
					
						
							|  |  |  |         return vm.throw_completion<JS::TypeError>("Wasm Memory allocation failed"sv); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  |     auto memory_object = realm.create<Memory>(realm, *address, shared ? Shared::Yes : Shared::No); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  |     return memory_object; | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  | Memory::Memory(JS::Realm& realm, Wasm::MemoryAddress address, Shared shared) | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  |     : Bindings::PlatformObject(realm) | 
					
						
							|  |  |  |     , m_address(address) | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  |     , m_shared(shared) | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-10 13:29:37 +00:00
										 |  |  |     auto& cache = Detail::get_cache(realm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cache.abstract_machine().store().get(address)->successful_grow_hook = [this] { | 
					
						
							|  |  |  |         MUST(reset_the_memory_buffer()); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void Memory::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(Memory, WebAssembly.Memory); | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-05 15:22:02 +02:00
										 |  |  | void Memory::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_buffer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | // https://webassembly.github.io/spec/js-api/#dom-memory-grow
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<u32> Memory::grow(u32 delta) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-25 19:09:34 +01:00
										 |  |  |     auto& context = Detail::get_cache(realm()); | 
					
						
							|  |  |  |     auto* memory = context.abstract_machine().store().get(address()); | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  |     if (!memory) | 
					
						
							|  |  |  |         return vm.throw_completion<JS::RangeError>("Could not find the memory instance to grow"sv); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto previous_size = memory->size() / Wasm::Constants::page_size; | 
					
						
							| 
									
										
										
										
											2024-07-10 15:05:20 -07:00
										 |  |  |     if (!memory->grow(delta * Wasm::Constants::page_size, Wasm::MemoryInstance::GrowType::No, Wasm::MemoryInstance::InhibitGrowCallback::Yes)) | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  |         return vm.throw_completion<JS::RangeError>("Memory.grow() grows past the stated limit of the memory instance"sv); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  |     TRY(reset_the_memory_buffer()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  |     return previous_size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  | // https://webassembly.github.io/spec/js-api/#refresh-the-memory-buffer
 | 
					
						
							|  |  |  | // FIXME: `refresh-the-memory-buffer` is a global abstract operation.
 | 
					
						
							|  |  |  | //        Implement it as a static function to align with the spec.
 | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  | WebIDL::ExceptionOr<void> Memory::reset_the_memory_buffer() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!m_buffer) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  |     if (m_buffer->is_fixed_length()) { | 
					
						
							|  |  |  |         // https://webassembly.github.io/threads/js-api/index.html#refresh-the-memory-buffer
 | 
					
						
							|  |  |  |         // 1. If IsSharedArrayBuffer(buffer) is false,
 | 
					
						
							|  |  |  |         if (!m_buffer->is_shared_array_buffer()) { | 
					
						
							|  |  |  |             // 1. Perform ! DetachArrayBuffer(buffer, "WebAssembly.Memory").
 | 
					
						
							|  |  |  |             MUST(JS::detach_array_buffer(vm, *m_buffer, JS::PrimitiveString::create(vm, "WebAssembly.Memory"_string))); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  |     m_buffer = TRY(create_a_fixed_length_memory_buffer(vm, realm, m_address, m_shared)); | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | // https://webassembly.github.io/spec/js-api/#dom-memory-buffer
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> Memory::buffer() const | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  |     if (!m_buffer) | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  |         m_buffer = TRY(create_a_fixed_length_memory_buffer(vm, realm, m_address, m_shared)); | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     return GC::Ref(*m_buffer); | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  | // https://webassembly.github.io/spec/js-api/#create-a-fixed-length-memory-buffer
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> Memory::create_a_fixed_length_memory_buffer(JS::VM& vm, JS::Realm& realm, Wasm::MemoryAddress address, Shared shared) | 
					
						
							| 
									
										
										
										
											2023-03-29 01:31:51 +03:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-25 19:09:34 +01:00
										 |  |  |     auto& context = Detail::get_cache(realm); | 
					
						
							|  |  |  |     auto* memory = context.abstract_machine().store().get(address); | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  |     if (!memory) | 
					
						
							|  |  |  |         return vm.throw_completion<JS::RangeError>("Could not find the memory instance"sv); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 21:17:20 +01:00
										 |  |  |     JS::ArrayBuffer* array_buffer; | 
					
						
							|  |  |  |     // https://webassembly.github.io/threads/js-api/index.html#create-a-fixed-length-memory-buffer
 | 
					
						
							|  |  |  |     // 3. If share is shared,
 | 
					
						
							|  |  |  |     if (shared == Shared::Yes) { | 
					
						
							|  |  |  |         // 1. Let block be a Shared Data Block which is identified with the underlying memory of memaddr.
 | 
					
						
							|  |  |  |         auto bytes = memory->data(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 2. Let buffer be a new SharedArrayBuffer with the internal slots [[ArrayBufferData]] and [[ArrayBufferByteLength]].
 | 
					
						
							|  |  |  |         array_buffer = TRY(JS::allocate_shared_array_buffer(vm, realm.intrinsics().shared_array_buffer_constructor(), bytes.size())); | 
					
						
							|  |  |  |         bytes.span().copy_to(array_buffer->buffer().span()); | 
					
						
							|  |  |  |         // 3. FIXME: Set buffer.[[ArrayBufferData]] to block.
 | 
					
						
							|  |  |  |         // 4. FIXME: Set buffer.[[ArrayBufferByteLength]] to the length of block.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 5. Perform ! SetIntegrityLevel(buffer, "frozen").
 | 
					
						
							|  |  |  |         MUST(array_buffer->set_integrity_level(JS::Object::IntegrityLevel::Frozen)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. Otherwise,
 | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         array_buffer = JS::ArrayBuffer::create(realm, &memory->data()); | 
					
						
							|  |  |  |         array_buffer->set_detach_key(JS::PrimitiveString::create(vm, "WebAssembly.Memory"_string)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     return GC::Ref(*array_buffer); | 
					
						
							| 
									
										
										
										
											2023-03-15 19:29:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |