| 1 | /* |
| 2 | * Copyright (C) 2016-2019 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 "JSWebAssemblyTable.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "JSCInlines.h" |
| 32 | #include "JSWebAssemblyInstance.h" |
| 33 | #include <wtf/CheckedArithmetic.h> |
| 34 | |
| 35 | namespace JSC { |
| 36 | |
| 37 | const ClassInfo JSWebAssemblyTable::s_info = { "WebAssembly.Table" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWebAssemblyTable) }; |
| 38 | |
| 39 | JSWebAssemblyTable* JSWebAssemblyTable::create(ExecState* exec, VM& vm, Structure* structure, Ref<Wasm::Table>&& table) |
| 40 | { |
| 41 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
| 42 | auto* globalObject = exec->lexicalGlobalObject(); |
| 43 | |
| 44 | if (!globalObject->webAssemblyEnabled()) { |
| 45 | throwException(exec, throwScope, createEvalError(exec, globalObject->webAssemblyDisabledErrorMessage())); |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
| 49 | auto* instance = new (NotNull, allocateCell<JSWebAssemblyTable>(vm.heap)) JSWebAssemblyTable(vm, structure, WTFMove(table)); |
| 50 | instance->finishCreation(vm); |
| 51 | return instance; |
| 52 | } |
| 53 | |
| 54 | Structure* JSWebAssemblyTable::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 55 | { |
| 56 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); |
| 57 | } |
| 58 | |
| 59 | JSWebAssemblyTable::JSWebAssemblyTable(VM& vm, Structure* structure, Ref<Wasm::Table>&& table) |
| 60 | : Base(vm, structure) |
| 61 | , m_table(WTFMove(table)) |
| 62 | { |
| 63 | // FIXME: It might be worth trying to pre-allocate maximum here. The spec recommends doing so. |
| 64 | // But for now, we're not doing that. |
| 65 | // FIXME this over-allocates and could be smarter about not committing all of that memory https://bugs.webkit.org/show_bug.cgi?id=181425 |
| 66 | m_jsFunctions = MallocPtr<WriteBarrier<JSObject>>::malloc((sizeof(WriteBarrier<JSObject>) * Checked<size_t>(allocatedLength())).unsafeGet()); |
| 67 | for (uint32_t i = 0; i < allocatedLength(); ++i) |
| 68 | new(&m_jsFunctions.get()[i]) WriteBarrier<JSObject>(); |
| 69 | } |
| 70 | |
| 71 | void JSWebAssemblyTable::finishCreation(VM& vm) |
| 72 | { |
| 73 | Base::finishCreation(vm); |
| 74 | ASSERT(inherits(vm, info())); |
| 75 | } |
| 76 | |
| 77 | void JSWebAssemblyTable::destroy(JSCell* cell) |
| 78 | { |
| 79 | static_cast<JSWebAssemblyTable*>(cell)->JSWebAssemblyTable::~JSWebAssemblyTable(); |
| 80 | } |
| 81 | |
| 82 | void JSWebAssemblyTable::visitChildren(JSCell* cell, SlotVisitor& visitor) |
| 83 | { |
| 84 | JSWebAssemblyTable* thisObject = jsCast<JSWebAssemblyTable*>(cell); |
| 85 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
| 86 | |
| 87 | Base::visitChildren(thisObject, visitor); |
| 88 | |
| 89 | for (unsigned i = 0; i < thisObject->length(); ++i) |
| 90 | visitor.append(thisObject->m_jsFunctions.get()[i]); |
| 91 | } |
| 92 | |
| 93 | bool JSWebAssemblyTable::grow(uint32_t delta) |
| 94 | { |
| 95 | if (delta == 0) |
| 96 | return true; |
| 97 | |
| 98 | size_t oldLength = length(); |
| 99 | |
| 100 | auto grew = m_table->grow(delta); |
| 101 | if (!grew) |
| 102 | return false; |
| 103 | |
| 104 | size_t newLength = grew.value(); |
| 105 | if (newLength > m_table->allocatedLength(oldLength)) |
| 106 | // FIXME this over-allocates and could be smarter about not committing all of that memory https://bugs.webkit.org/show_bug.cgi?id=181425 |
| 107 | m_jsFunctions.realloc((sizeof(WriteBarrier<JSObject>) * Checked<size_t>(m_table->allocatedLength(newLength))).unsafeGet()); |
| 108 | |
| 109 | for (size_t i = oldLength; i < m_table->allocatedLength(newLength); ++i) |
| 110 | new (&m_jsFunctions.get()[i]) WriteBarrier<JSObject>(); |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | JSObject* JSWebAssemblyTable::getFunction(uint32_t index) |
| 116 | { |
| 117 | RELEASE_ASSERT(index < length()); |
| 118 | return m_jsFunctions.get()[index & m_table->mask()].get(); |
| 119 | } |
| 120 | |
| 121 | void JSWebAssemblyTable::clearFunction(uint32_t index) |
| 122 | { |
| 123 | m_table->clearFunction(index); |
| 124 | m_jsFunctions.get()[index & m_table->mask()] = WriteBarrier<JSObject>(); |
| 125 | } |
| 126 | |
| 127 | void JSWebAssemblyTable::setFunction(VM& vm, uint32_t index, WebAssemblyFunction* function) |
| 128 | { |
| 129 | m_table->setFunction(index, function->importableFunction(), &function->instance()->instance()); |
| 130 | m_jsFunctions.get()[index & m_table->mask()].set(vm, this, function); |
| 131 | } |
| 132 | |
| 133 | void JSWebAssemblyTable::setFunction(VM& vm, uint32_t index, WebAssemblyWrapperFunction* function) |
| 134 | { |
| 135 | m_table->setFunction(index, function->importableFunction(), &function->instance()->instance()); |
| 136 | m_jsFunctions.get()[index & m_table->mask()].set(vm, this, function); |
| 137 | } |
| 138 | |
| 139 | } // namespace JSC |
| 140 | |
| 141 | #endif // ENABLE(WEBASSEMBLY) |
| 142 | |