jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 1 | /* |
jfbastien@apple.com | 802693a | 2018-01-12 23:47:58 +0000 | [diff] [blame] | 2 | * Copyright (C) 2017-2018 Apple Inc. All rights reserved. |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 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 "WasmFormat.h" |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 31 | #include "WasmGlobal.h" |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 32 | #include "WasmMemory.h" |
| 33 | #include "WasmModule.h" |
| 34 | #include "WasmTable.h" |
justin_michaud@apple.com | c0e5bf2 | 2019-05-25 00:51:21 +0000 | [diff] [blame] | 35 | #include "WriteBarrier.h" |
| 36 | #include <wtf/BitVector.h> |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 37 | #include <wtf/RefPtr.h> |
| 38 | #include <wtf/ThreadSafeRefCounted.h> |
| 39 | |
keith_miller@apple.com | 8e7bd48 | 2019-10-01 16:38:26 +0000 | [diff] [blame] | 40 | namespace JSC { |
| 41 | |
tzagallo@apple.com | bf01be2 | 2019-10-31 22:32:52 +0000 | [diff] [blame] | 42 | class LLIntOffsetsExtractor; |
keith_miller@apple.com | 8e7bd48 | 2019-10-01 16:38:26 +0000 | [diff] [blame] | 43 | class JSWebAssemblyInstance; |
| 44 | |
| 45 | namespace Wasm { |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 46 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 47 | struct Context; |
justin_michaud@apple.com | b0b1a53 | 2019-06-17 18:44:18 +0000 | [diff] [blame] | 48 | class Instance; |
| 49 | |
cdumez@apple.com | 72332a6 | 2018-06-08 03:56:04 +0000 | [diff] [blame] | 50 | class Instance : public ThreadSafeRefCounted<Instance>, public CanMakeWeakPtr<Instance> { |
tzagallo@apple.com | bf01be2 | 2019-10-31 22:32:52 +0000 | [diff] [blame] | 51 | friend LLIntOffsetsExtractor; |
| 52 | |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 53 | public: |
sbarati@apple.com | d868807 | 2017-11-14 09:05:33 +0000 | [diff] [blame] | 54 | using StoreTopCallFrameCallback = WTF::Function<void(void*)>; |
justin_michaud@apple.com | b0b1a53 | 2019-06-17 18:44:18 +0000 | [diff] [blame] | 55 | using FunctionWrapperMap = HashMap<uint32_t, WriteBarrier<Unknown>, IntHash<uint32_t>, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>>; |
sbarati@apple.com | d868807 | 2017-11-14 09:05:33 +0000 | [diff] [blame] | 56 | |
jfbastien@apple.com | c4b8a5f | 2017-12-01 21:58:36 +0000 | [diff] [blame] | 57 | static Ref<Instance> create(Context*, Ref<Module>&&, EntryFrame** pointerToTopEntryFrame, void** pointerToActualStackLimit, StoreTopCallFrameCallback&&); |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 58 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 59 | void finalizeCreation(void* owner, Ref<CodeBlock>&& codeBlock) |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 60 | { |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 61 | m_owner = owner; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 62 | m_codeBlock = WTFMove(codeBlock); |
| 63 | } |
| 64 | |
| 65 | JS_EXPORT_PRIVATE ~Instance(); |
| 66 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 67 | template<typename T> T* owner() const { return reinterpret_cast<T*>(m_owner); } |
| 68 | static ptrdiff_t offsetOfOwner() { return OBJECT_OFFSETOF(Instance, m_owner); } |
| 69 | |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 70 | size_t extraMemoryAllocated() const; |
| 71 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 72 | Wasm::Context* context() const { return m_context; } |
| 73 | |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 74 | Module& module() { return m_module.get(); } |
| 75 | CodeBlock* codeBlock() { return m_codeBlock.get(); } |
| 76 | Memory* memory() { return m_memory.get(); } |
justin_michaud@apple.com | 6cae935 | 2019-06-18 22:01:02 +0000 | [diff] [blame] | 77 | Table* table(unsigned); |
| 78 | void setTable(unsigned, Ref<Table>&&); |
gskachkov@gmail.com | 0ac4757 | 2018-02-23 23:16:59 +0000 | [diff] [blame] | 79 | |
commit-queue@webkit.org | f79caa4 | 2019-06-12 18:40:56 +0000 | [diff] [blame] | 80 | void* cachedMemory() const { return m_cachedMemory.getMayBeNull(cachedMemorySize()); } |
gskachkov@gmail.com | 0ac4757 | 2018-02-23 23:16:59 +0000 | [diff] [blame] | 81 | size_t cachedMemorySize() const { return m_cachedMemorySize; } |
| 82 | |
gskachkov@gmail.com | 0ac4757 | 2018-02-23 23:16:59 +0000 | [diff] [blame] | 83 | void setMemory(Ref<Memory>&& memory) |
| 84 | { |
| 85 | m_memory = WTFMove(memory); |
| 86 | m_memory.get()->registerInstance(this); |
| 87 | updateCachedMemory(); |
| 88 | } |
| 89 | void updateCachedMemory() |
| 90 | { |
| 91 | if (m_memory != nullptr) { |
commit-queue@webkit.org | f79caa4 | 2019-06-12 18:40:56 +0000 | [diff] [blame] | 92 | m_cachedMemory = CagedPtr<Gigacage::Primitive, void, tagCagedPtr>(memory()->memory(), memory()->size()); |
gskachkov@gmail.com | 0ac4757 | 2018-02-23 23:16:59 +0000 | [diff] [blame] | 93 | m_cachedMemorySize = memory()->size(); |
gskachkov@gmail.com | 0ac4757 | 2018-02-23 23:16:59 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 96 | |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 97 | int32_t loadI32Global(unsigned i) const |
| 98 | { |
| 99 | Global::Value* slot = m_globals.get() + i; |
| 100 | if (m_globalsToBinding.get(i)) { |
| 101 | slot = slot->m_pointer; |
| 102 | if (!slot) |
| 103 | return 0; |
| 104 | } |
| 105 | return slot->m_primitive; |
| 106 | } |
| 107 | int64_t loadI64Global(unsigned i) const |
| 108 | { |
| 109 | Global::Value* slot = m_globals.get() + i; |
| 110 | if (m_globalsToBinding.get(i)) { |
| 111 | slot = slot->m_pointer; |
| 112 | if (!slot) |
| 113 | return 0; |
| 114 | } |
| 115 | return slot->m_primitive; |
| 116 | } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 117 | float loadF32Global(unsigned i) const { return bitwise_cast<float>(loadI32Global(i)); } |
| 118 | double loadF64Global(unsigned i) const { return bitwise_cast<double>(loadI64Global(i)); } |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 119 | void setGlobal(unsigned i, int64_t bits) |
| 120 | { |
| 121 | Global::Value* slot = m_globals.get() + i; |
| 122 | if (m_globalsToBinding.get(i)) { |
| 123 | slot = slot->m_pointer; |
| 124 | if (!slot) |
| 125 | return; |
| 126 | } |
| 127 | slot->m_primitive = bits; |
| 128 | } |
justin_michaud@apple.com | c0e5bf2 | 2019-05-25 00:51:21 +0000 | [diff] [blame] | 129 | void setGlobal(unsigned, JSValue); |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 130 | void linkGlobal(unsigned, Ref<Global>&&); |
justin_michaud@apple.com | c0e5bf2 | 2019-05-25 00:51:21 +0000 | [diff] [blame] | 131 | const BitVector& globalsToMark() { return m_globalsToMark; } |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 132 | const BitVector& globalsToBinding() { return m_globalsToBinding; } |
justin_michaud@apple.com | b0b1a53 | 2019-06-17 18:44:18 +0000 | [diff] [blame] | 133 | JSValue getFunctionWrapper(unsigned) const; |
| 134 | typename FunctionWrapperMap::ValuesConstIteratorRange functionWrappers() const { return m_functionWrappers.values(); } |
| 135 | void setFunctionWrapper(unsigned, JSValue); |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 136 | |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 137 | Wasm::Global* getGlobalBinding(unsigned i) |
| 138 | { |
| 139 | ASSERT(m_globalsToBinding.get(i)); |
| 140 | Wasm::Global::Value* pointer = m_globals.get()[i].m_pointer; |
| 141 | if (!pointer) |
| 142 | return nullptr; |
| 143 | return &Wasm::Global::fromBinding(*pointer); |
| 144 | } |
| 145 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 146 | static ptrdiff_t offsetOfMemory() { return OBJECT_OFFSETOF(Instance, m_memory); } |
| 147 | static ptrdiff_t offsetOfGlobals() { return OBJECT_OFFSETOF(Instance, m_globals); } |
gskachkov@gmail.com | 0ac4757 | 2018-02-23 23:16:59 +0000 | [diff] [blame] | 148 | static ptrdiff_t offsetOfCachedMemory() { return OBJECT_OFFSETOF(Instance, m_cachedMemory); } |
| 149 | static ptrdiff_t offsetOfCachedMemorySize() { return OBJECT_OFFSETOF(Instance, m_cachedMemorySize); } |
jfbastien@apple.com | c4b8a5f | 2017-12-01 21:58:36 +0000 | [diff] [blame] | 150 | static ptrdiff_t offsetOfPointerToTopEntryFrame() { return OBJECT_OFFSETOF(Instance, m_pointerToTopEntryFrame); } |
keith_miller@apple.com | 7683fac | 2017-10-24 03:20:31 +0000 | [diff] [blame] | 151 | |
jfbastien@apple.com | c4b8a5f | 2017-12-01 21:58:36 +0000 | [diff] [blame] | 152 | static ptrdiff_t offsetOfPointerToActualStackLimit() { return OBJECT_OFFSETOF(Instance, m_pointerToActualStackLimit); } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 153 | static ptrdiff_t offsetOfCachedStackLimit() { return OBJECT_OFFSETOF(Instance, m_cachedStackLimit); } |
jfbastien@apple.com | c4b8a5f | 2017-12-01 21:58:36 +0000 | [diff] [blame] | 154 | void* cachedStackLimit() const |
| 155 | { |
| 156 | ASSERT(*m_pointerToActualStackLimit == m_cachedStackLimit); |
| 157 | return m_cachedStackLimit; |
| 158 | } |
| 159 | void setCachedStackLimit(void* limit) |
| 160 | { |
| 161 | ASSERT(*m_pointerToActualStackLimit == limit || bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()) == limit); |
| 162 | m_cachedStackLimit = limit; |
| 163 | } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 164 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 165 | // Tail accessors. |
justin_michaud@apple.com | 6cae935 | 2019-06-18 22:01:02 +0000 | [diff] [blame] | 166 | static constexpr size_t offsetOfTail() { return WTF::roundUpToMultipleOf<sizeof(uint64_t)>(sizeof(Instance)); } |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 167 | struct ImportFunctionInfo { |
| 168 | // Target instance and entrypoint are only set for wasm->wasm calls, and are otherwise nullptr. The embedder-specific logic occurs through import function. |
| 169 | Instance* targetInstance { nullptr }; |
mark.lam@apple.com | 52fa2cc | 2018-03-30 05:04:44 +0000 | [diff] [blame] | 170 | WasmToWasmImportableFunction::LoadLocation wasmEntrypointLoadLocation { nullptr }; |
mark.lam@apple.com | de0dba7 | 2018-04-18 03:31:09 +0000 | [diff] [blame] | 171 | MacroAssemblerCodePtr<WasmEntryPtrTag> wasmToEmbedderStub; |
mark.lam@apple.com | 7725e2a | 2019-02-27 05:43:34 +0000 | [diff] [blame] | 172 | void* importFunction { nullptr }; // In a JS embedding, this is a WriteBarrier<JSObject>. |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 173 | }; |
| 174 | unsigned numImportFunctions() const { return m_numImportFunctions; } |
| 175 | ImportFunctionInfo* importFunctionInfo(size_t importFunctionNum) |
| 176 | { |
| 177 | RELEASE_ASSERT(importFunctionNum < m_numImportFunctions); |
| 178 | return &bitwise_cast<ImportFunctionInfo*>(bitwise_cast<char*>(this) + offsetOfTail())[importFunctionNum]; |
| 179 | } |
| 180 | static size_t offsetOfTargetInstance(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, targetInstance); } |
mark.lam@apple.com | 52fa2cc | 2018-03-30 05:04:44 +0000 | [diff] [blame] | 181 | static size_t offsetOfWasmEntrypointLoadLocation(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmEntrypointLoadLocation); } |
| 182 | static size_t offsetOfWasmToEmbedderStub(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmToEmbedderStub); } |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 183 | static size_t offsetOfImportFunction(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, importFunction); } |
| 184 | template<typename T> T* importFunction(unsigned importFunctionNum) { return reinterpret_cast<T*>(&importFunctionInfo(importFunctionNum)->importFunction); } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 185 | |
justin_michaud@apple.com | 6cae935 | 2019-06-18 22:01:02 +0000 | [diff] [blame] | 186 | static_assert(sizeof(ImportFunctionInfo) == WTF::roundUpToMultipleOf<sizeof(uint64_t)>(sizeof(ImportFunctionInfo)), "We rely on this for the alignment to be correct"); |
| 187 | static constexpr size_t offsetOfTablePtr(unsigned numImportFunctions, unsigned i) { return offsetOfTail() + sizeof(ImportFunctionInfo) * numImportFunctions + sizeof(Table*) * i; } |
| 188 | |
sbarati@apple.com | d868807 | 2017-11-14 09:05:33 +0000 | [diff] [blame] | 189 | void storeTopCallFrame(void* callFrame) |
| 190 | { |
| 191 | m_storeTopCallFrame(callFrame); |
| 192 | } |
| 193 | |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 194 | private: |
jfbastien@apple.com | c4b8a5f | 2017-12-01 21:58:36 +0000 | [diff] [blame] | 195 | Instance(Context*, Ref<Module>&&, EntryFrame**, void**, StoreTopCallFrameCallback&&); |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 196 | |
justin_michaud@apple.com | 6cae935 | 2019-06-18 22:01:02 +0000 | [diff] [blame] | 197 | static size_t allocationSize(Checked<size_t> numImportFunctions, Checked<size_t> numTables) |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 198 | { |
justin_michaud@apple.com | 6cae935 | 2019-06-18 22:01:02 +0000 | [diff] [blame] | 199 | return (offsetOfTail() + sizeof(ImportFunctionInfo) * numImportFunctions + sizeof(Table*) * numTables).unsafeGet(); |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 200 | } |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 201 | void* m_owner { nullptr }; // In a JS embedding, this is a JSWebAssemblyInstance*. |
| 202 | Context* m_context { nullptr }; |
commit-queue@webkit.org | f79caa4 | 2019-06-12 18:40:56 +0000 | [diff] [blame] | 203 | CagedPtr<Gigacage::Primitive, void, tagCagedPtr> m_cachedMemory; |
gskachkov@gmail.com | 0ac4757 | 2018-02-23 23:16:59 +0000 | [diff] [blame] | 204 | size_t m_cachedMemorySize { 0 }; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 205 | Ref<Module> m_module; |
| 206 | RefPtr<CodeBlock> m_codeBlock; |
| 207 | RefPtr<Memory> m_memory; |
justin_michaud@apple.com | c0e5bf2 | 2019-05-25 00:51:21 +0000 | [diff] [blame] | 208 | |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 209 | MallocPtr<Global::Value, VMMalloc> m_globals; |
justin_michaud@apple.com | b0b1a53 | 2019-06-17 18:44:18 +0000 | [diff] [blame] | 210 | FunctionWrapperMap m_functionWrappers; |
justin_michaud@apple.com | c0e5bf2 | 2019-05-25 00:51:21 +0000 | [diff] [blame] | 211 | BitVector m_globalsToMark; |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 212 | BitVector m_globalsToBinding; |
jfbastien@apple.com | c4b8a5f | 2017-12-01 21:58:36 +0000 | [diff] [blame] | 213 | EntryFrame** m_pointerToTopEntryFrame { nullptr }; |
| 214 | void** m_pointerToActualStackLimit { nullptr }; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 215 | void* m_cachedStackLimit { bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()) }; |
sbarati@apple.com | d868807 | 2017-11-14 09:05:33 +0000 | [diff] [blame] | 216 | StoreTopCallFrameCallback m_storeTopCallFrame; |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 217 | unsigned m_numImportFunctions { 0 }; |
ysuzuki@apple.com | 7d52c09 | 2019-12-04 01:36:56 +0000 | [diff] [blame] | 218 | HashMap<uint32_t, Ref<Global>, IntHash<uint32_t>, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>> m_linkedGlobals; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | } } // namespace JSC::Wasm |
| 222 | |
| 223 | #endif // ENABLE(WEBASSEMBLY) |