| 1 | /* |
| 2 | * Copyright (C) 2016-2018 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(WEBASSEMBLY) |
| 29 | |
| 30 | #include "ArityCheckMode.h" |
| 31 | #include "JSToWasmICCallee.h" |
| 32 | #include "MacroAssemblerCodeRef.h" |
| 33 | #include "WasmCallee.h" |
| 34 | #include "WebAssemblyFunctionBase.h" |
| 35 | #include <wtf/Noncopyable.h> |
| 36 | |
| 37 | namespace JSC { |
| 38 | |
| 39 | class JSGlobalObject; |
| 40 | struct ProtoCallFrame; |
| 41 | class WebAssemblyInstance; |
| 42 | using Wasm::WasmToWasmImportableFunction; |
| 43 | |
| 44 | namespace B3 { |
| 45 | class Compilation; |
| 46 | } |
| 47 | |
| 48 | class WebAssemblyFunction final : public WebAssemblyFunctionBase { |
| 49 | public: |
| 50 | using Base = WebAssemblyFunctionBase; |
| 51 | |
| 52 | const static unsigned StructureFlags = Base::StructureFlags; |
| 53 | |
| 54 | static const bool needsDestruction = true; |
| 55 | static void destroy(JSCell*); |
| 56 | |
| 57 | template<typename CellType, SubspaceAccess mode> |
| 58 | static IsoSubspace* subspaceFor(VM& vm) |
| 59 | { |
| 60 | return vm.webAssemblyFunctionSpace<mode>(); |
| 61 | } |
| 62 | |
| 63 | DECLARE_EXPORT_INFO; |
| 64 | |
| 65 | JS_EXPORT_PRIVATE static WebAssemblyFunction* create(VM&, JSGlobalObject*, Structure*, unsigned, const String&, JSWebAssemblyInstance*, Wasm::Callee& jsEntrypoint, WasmToWasmImportableFunction::LoadLocation, Wasm::SignatureIndex); |
| 66 | static Structure* createStructure(VM&, JSGlobalObject*, JSValue); |
| 67 | |
| 68 | Wasm::SignatureIndex signatureIndex() const { return m_importableFunction.signatureIndex; } |
| 69 | WasmToWasmImportableFunction::LoadLocation entrypointLoadLocation() const { return m_importableFunction.entrypointLoadLocation; } |
| 70 | WasmToWasmImportableFunction importableFunction() const { return m_importableFunction; } |
| 71 | |
| 72 | MacroAssemblerCodePtr<WasmEntryPtrTag> jsEntrypoint(ArityCheckMode arity) |
| 73 | { |
| 74 | if (arity == ArityCheckNotRequired) |
| 75 | return m_jsEntrypoint; |
| 76 | ASSERT(arity == MustCheckArity); |
| 77 | return m_jsEntrypoint; |
| 78 | } |
| 79 | |
| 80 | static ptrdiff_t offsetOfEntrypointLoadLocation() { return OBJECT_OFFSETOF(WebAssemblyFunction, m_importableFunction) + WasmToWasmImportableFunction::offsetOfEntrypointLoadLocation(); } |
| 81 | |
| 82 | MacroAssemblerCodePtr<JSEntryPtrTag> jsCallEntrypoint() |
| 83 | { |
| 84 | if (m_jsCallEntrypoint) |
| 85 | return m_jsCallEntrypoint.code(); |
| 86 | return jsCallEntrypointSlow(); |
| 87 | } |
| 88 | |
| 89 | RegisterAtOffsetList usedCalleeSaveRegisters() const; |
| 90 | Wasm::Instance* previousInstance(CallFrame*); |
| 91 | |
| 92 | private: |
| 93 | static void visitChildren(JSCell*, SlotVisitor&); |
| 94 | WebAssemblyFunction(VM&, JSGlobalObject*, Structure*, Wasm::Callee& jsEntrypoint, WasmToWasmImportableFunction::LoadLocation entrypointLoadLocation, Wasm::SignatureIndex); |
| 95 | |
| 96 | MacroAssemblerCodePtr<JSEntryPtrTag> jsCallEntrypointSlow(); |
| 97 | ptrdiff_t previousInstanceOffset() const; |
| 98 | bool useTagRegisters() const; |
| 99 | |
| 100 | RegisterSet calleeSaves() const; |
| 101 | |
| 102 | // It's safe to just hold the raw WasmToWasmImportableFunction/jsEntrypoint because we have a reference |
| 103 | // to our Instance, which points to the Module that exported us, which |
| 104 | // ensures that the actual Signature/code doesn't get deallocated. |
| 105 | MacroAssemblerCodePtr<WasmEntryPtrTag> m_jsEntrypoint; |
| 106 | WasmToWasmImportableFunction m_importableFunction; |
| 107 | WriteBarrier<JSToWasmICCallee> m_jsToWasmICCallee; |
| 108 | // Used for JS calling into Wasm. |
| 109 | MacroAssemblerCodeRef<JSEntryPtrTag> m_jsCallEntrypoint; |
| 110 | }; |
| 111 | |
| 112 | } // namespace JSC |
| 113 | |
| 114 | #endif // ENABLE(WEBASSEMBLY) |
| 115 | |