| 1 | /* |
| 2 | * Copyright (C) 2016-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "JSWebAssemblyMemory.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "JSCInlines.h" |
| 32 | |
| 33 | #include "ArrayBuffer.h" |
| 34 | #include "JSArrayBuffer.h" |
| 35 | |
| 36 | namespace JSC { |
| 37 | |
| 38 | const ClassInfo JSWebAssemblyMemory::s_info = { "WebAssembly.Memory" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWebAssemblyMemory) }; |
| 39 | |
| 40 | JSWebAssemblyMemory* JSWebAssemblyMemory::create(ExecState* exec, VM& vm, Structure* structure) |
| 41 | { |
| 42 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
| 43 | auto* globalObject = exec->lexicalGlobalObject(); |
| 44 | |
| 45 | auto exception = [&] (JSObject* error) { |
| 46 | throwException(exec, throwScope, error); |
| 47 | return nullptr; |
| 48 | }; |
| 49 | |
| 50 | if (!globalObject->webAssemblyEnabled()) |
| 51 | return exception(createEvalError(exec, globalObject->webAssemblyDisabledErrorMessage())); |
| 52 | |
| 53 | auto* memory = new (NotNull, allocateCell<JSWebAssemblyMemory>(vm.heap)) JSWebAssemblyMemory(vm, structure); |
| 54 | memory->finishCreation(vm); |
| 55 | return memory; |
| 56 | } |
| 57 | |
| 58 | void JSWebAssemblyMemory::adopt(Ref<Wasm::Memory>&& memory) |
| 59 | { |
| 60 | m_memory.swap(memory); |
| 61 | ASSERT(m_memory->refCount() == 1); |
| 62 | m_memory->check(); |
| 63 | } |
| 64 | |
| 65 | Structure* JSWebAssemblyMemory::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 66 | { |
| 67 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); |
| 68 | } |
| 69 | |
| 70 | JSWebAssemblyMemory::JSWebAssemblyMemory(VM& vm, Structure* structure) |
| 71 | : Base(vm, structure) |
| 72 | , m_memory(Wasm::Memory::create()) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | JSArrayBuffer* JSWebAssemblyMemory::buffer(VM& vm, JSGlobalObject* globalObject) |
| 77 | { |
| 78 | if (m_bufferWrapper) |
| 79 | return m_bufferWrapper.get(); |
| 80 | |
| 81 | // We can't use a ref here since it doesn't have a copy constructor... |
| 82 | Ref<Wasm::Memory> protectedMemory = m_memory.get(); |
| 83 | auto destructor = [protectedMemory = WTFMove(protectedMemory)] (void*) { }; |
| 84 | m_buffer = ArrayBuffer::createFromBytes(memory().memory(), memory().size(), WTFMove(destructor)); |
| 85 | m_buffer->makeWasmMemory(); |
| 86 | m_bufferWrapper.set(vm, this, JSArrayBuffer::create(vm, globalObject->arrayBufferStructure(ArrayBufferSharingMode::Default), m_buffer.get())); |
| 87 | RELEASE_ASSERT(m_bufferWrapper); |
| 88 | return m_bufferWrapper.get(); |
| 89 | } |
| 90 | |
| 91 | Wasm::PageCount JSWebAssemblyMemory::grow(VM& vm, ExecState* exec, uint32_t delta) |
| 92 | { |
| 93 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
| 94 | |
| 95 | auto grown = memory().grow(Wasm::PageCount(delta)); |
| 96 | if (!grown) { |
| 97 | switch (grown.error()) { |
| 98 | case Wasm::Memory::GrowFailReason::InvalidDelta: |
| 99 | throwException(exec, throwScope, createRangeError(exec, "WebAssembly.Memory.grow expects the delta to be a valid page count"_s )); |
| 100 | break; |
| 101 | case Wasm::Memory::GrowFailReason::InvalidGrowSize: |
| 102 | throwException(exec, throwScope, createRangeError(exec, "WebAssembly.Memory.grow expects the grown size to be a valid page count"_s )); |
| 103 | break; |
| 104 | case Wasm::Memory::GrowFailReason::WouldExceedMaximum: |
| 105 | throwException(exec, throwScope, createRangeError(exec, "WebAssembly.Memory.grow would exceed the memory's declared maximum size"_s )); |
| 106 | break; |
| 107 | case Wasm::Memory::GrowFailReason::OutOfMemory: |
| 108 | throwException(exec, throwScope, createOutOfMemoryError(exec)); |
| 109 | break; |
| 110 | } |
| 111 | return Wasm::PageCount(); |
| 112 | } |
| 113 | |
| 114 | return grown.value(); |
| 115 | } |
| 116 | |
| 117 | void JSWebAssemblyMemory::growSuccessCallback(VM& vm, Wasm::PageCount oldPageCount, Wasm::PageCount newPageCount) |
| 118 | { |
| 119 | // We need to clear out the old array buffer because it might now be pointing to stale memory. |
| 120 | // Neuter the old array. |
| 121 | if (m_buffer) { |
| 122 | m_buffer->neuter(vm); |
| 123 | m_buffer = nullptr; |
| 124 | m_bufferWrapper.clear(); |
| 125 | } |
| 126 | |
| 127 | memory().check(); |
| 128 | |
| 129 | vm.heap.reportExtraMemoryAllocated(newPageCount.bytes() - oldPageCount.bytes()); |
| 130 | } |
| 131 | |
| 132 | void JSWebAssemblyMemory::finishCreation(VM& vm) |
| 133 | { |
| 134 | Base::finishCreation(vm); |
| 135 | ASSERT(inherits(vm, info())); |
| 136 | vm.heap.reportExtraMemoryAllocated(memory().size()); |
| 137 | } |
| 138 | |
| 139 | void JSWebAssemblyMemory::destroy(JSCell* cell) |
| 140 | { |
| 141 | auto memory = static_cast<JSWebAssemblyMemory*>(cell); |
| 142 | ASSERT(memory->classInfo() == info()); |
| 143 | |
| 144 | memory->JSWebAssemblyMemory::~JSWebAssemblyMemory(); |
| 145 | } |
| 146 | |
| 147 | void JSWebAssemblyMemory::visitChildren(JSCell* cell, SlotVisitor& visitor) |
| 148 | { |
| 149 | auto* thisObject = jsCast<JSWebAssemblyMemory*>(cell); |
| 150 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
| 151 | |
| 152 | Base::visitChildren(thisObject, visitor); |
| 153 | visitor.append(thisObject->m_bufferWrapper); |
| 154 | visitor.reportExtraMemoryVisited(thisObject->memory().size()); |
| 155 | } |
| 156 | |
| 157 | } // namespace JSC |
| 158 | |
| 159 | #endif // ENABLE(WEBASSEMBLY) |
| 160 | |