1 | /* |
2 | * Copyright (C) 2017-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 "WasmFormat.h" |
31 | #include "WasmMemory.h" |
32 | #include "WasmModule.h" |
33 | #include "WasmTable.h" |
34 | #include "WriteBarrier.h" |
35 | #include <wtf/BitVector.h> |
36 | #include <wtf/RefPtr.h> |
37 | #include <wtf/ThreadSafeRefCounted.h> |
38 | |
39 | namespace JSC { namespace Wasm { |
40 | |
41 | struct Context; |
42 | class Instance; |
43 | |
44 | EncodedJSValue getWasmTableElement(Instance*, unsigned, int32_t); |
45 | bool setWasmTableElement(Instance*, unsigned, int32_t, EncodedJSValue encValue); |
46 | EncodedJSValue doWasmRefFunc(Instance*, uint32_t); |
47 | int32_t doWasmTableGrow(Instance*, unsigned, EncodedJSValue fill, int32_t delta); |
48 | bool doWasmTableFill(Instance*, unsigned, int32_t offset, EncodedJSValue fill, int32_t count); |
49 | |
50 | class Instance : public ThreadSafeRefCounted<Instance>, public CanMakeWeakPtr<Instance> { |
51 | public: |
52 | using StoreTopCallFrameCallback = WTF::Function<void(void*)>; |
53 | using FunctionWrapperMap = HashMap<uint32_t, WriteBarrier<Unknown>, IntHash<uint32_t>, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>>; |
54 | |
55 | static Ref<Instance> create(Context*, Ref<Module>&&, EntryFrame** pointerToTopEntryFrame, void** pointerToActualStackLimit, StoreTopCallFrameCallback&&); |
56 | |
57 | void finalizeCreation(void* owner, Ref<CodeBlock>&& codeBlock) |
58 | { |
59 | m_owner = owner; |
60 | m_codeBlock = WTFMove(codeBlock); |
61 | } |
62 | |
63 | JS_EXPORT_PRIVATE ~Instance(); |
64 | |
65 | template<typename T> T* owner() const { return reinterpret_cast<T*>(m_owner); } |
66 | static ptrdiff_t offsetOfOwner() { return OBJECT_OFFSETOF(Instance, m_owner); } |
67 | |
68 | size_t () const; |
69 | |
70 | Wasm::Context* context() const { return m_context; } |
71 | |
72 | Module& module() { return m_module.get(); } |
73 | CodeBlock* codeBlock() { return m_codeBlock.get(); } |
74 | Memory* memory() { return m_memory.get(); } |
75 | Table* table(unsigned); |
76 | void setTable(unsigned, Ref<Table>&&); |
77 | |
78 | void* cachedMemory() const { return m_cachedMemory.getMayBeNull(cachedMemorySize()); } |
79 | size_t cachedMemorySize() const { return m_cachedMemorySize; } |
80 | |
81 | void setMemory(Ref<Memory>&& memory) |
82 | { |
83 | m_memory = WTFMove(memory); |
84 | m_memory.get()->registerInstance(this); |
85 | updateCachedMemory(); |
86 | } |
87 | void updateCachedMemory() |
88 | { |
89 | if (m_memory != nullptr) { |
90 | m_cachedMemory = CagedPtr<Gigacage::Primitive, void, tagCagedPtr>(memory()->memory(), memory()->size()); |
91 | m_cachedMemorySize = memory()->size(); |
92 | } |
93 | } |
94 | |
95 | int32_t loadI32Global(unsigned i) const { return m_globals.get()[i].primitive; } |
96 | int64_t loadI64Global(unsigned i) const { return m_globals.get()[i].primitive; } |
97 | float loadF32Global(unsigned i) const { return bitwise_cast<float>(loadI32Global(i)); } |
98 | double loadF64Global(unsigned i) const { return bitwise_cast<double>(loadI64Global(i)); } |
99 | void setGlobal(unsigned i, int64_t bits) { m_globals.get()[i].primitive = bits; } |
100 | void setGlobal(unsigned, JSValue); |
101 | const BitVector& globalsToMark() { return m_globalsToMark; } |
102 | JSValue getFunctionWrapper(unsigned) const; |
103 | typename FunctionWrapperMap::ValuesConstIteratorRange functionWrappers() const { return m_functionWrappers.values(); } |
104 | void setFunctionWrapper(unsigned, JSValue); |
105 | |
106 | static ptrdiff_t offsetOfMemory() { return OBJECT_OFFSETOF(Instance, m_memory); } |
107 | static ptrdiff_t offsetOfGlobals() { return OBJECT_OFFSETOF(Instance, m_globals); } |
108 | static ptrdiff_t offsetOfCachedMemory() { return OBJECT_OFFSETOF(Instance, m_cachedMemory); } |
109 | static ptrdiff_t offsetOfCachedMemorySize() { return OBJECT_OFFSETOF(Instance, m_cachedMemorySize); } |
110 | static ptrdiff_t offsetOfPointerToTopEntryFrame() { return OBJECT_OFFSETOF(Instance, m_pointerToTopEntryFrame); } |
111 | |
112 | static ptrdiff_t offsetOfPointerToActualStackLimit() { return OBJECT_OFFSETOF(Instance, m_pointerToActualStackLimit); } |
113 | static ptrdiff_t offsetOfCachedStackLimit() { return OBJECT_OFFSETOF(Instance, m_cachedStackLimit); } |
114 | void* cachedStackLimit() const |
115 | { |
116 | ASSERT(*m_pointerToActualStackLimit == m_cachedStackLimit); |
117 | return m_cachedStackLimit; |
118 | } |
119 | void setCachedStackLimit(void* limit) |
120 | { |
121 | ASSERT(*m_pointerToActualStackLimit == limit || bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()) == limit); |
122 | m_cachedStackLimit = limit; |
123 | } |
124 | |
125 | // Tail accessors. |
126 | static constexpr size_t offsetOfTail() { return WTF::roundUpToMultipleOf<sizeof(uint64_t)>(sizeof(Instance)); } |
127 | struct ImportFunctionInfo { |
128 | // Target instance and entrypoint are only set for wasm->wasm calls, and are otherwise nullptr. The embedder-specific logic occurs through import function. |
129 | Instance* targetInstance { nullptr }; |
130 | WasmToWasmImportableFunction::LoadLocation wasmEntrypointLoadLocation { nullptr }; |
131 | MacroAssemblerCodePtr<WasmEntryPtrTag> wasmToEmbedderStub; |
132 | void* importFunction { nullptr }; // In a JS embedding, this is a WriteBarrier<JSObject>. |
133 | }; |
134 | unsigned numImportFunctions() const { return m_numImportFunctions; } |
135 | ImportFunctionInfo* importFunctionInfo(size_t importFunctionNum) |
136 | { |
137 | RELEASE_ASSERT(importFunctionNum < m_numImportFunctions); |
138 | return &bitwise_cast<ImportFunctionInfo*>(bitwise_cast<char*>(this) + offsetOfTail())[importFunctionNum]; |
139 | } |
140 | static size_t offsetOfTargetInstance(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, targetInstance); } |
141 | static size_t offsetOfWasmEntrypointLoadLocation(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmEntrypointLoadLocation); } |
142 | static size_t offsetOfWasmToEmbedderStub(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, wasmToEmbedderStub); } |
143 | static size_t offsetOfImportFunction(size_t importFunctionNum) { return offsetOfTail() + importFunctionNum * sizeof(ImportFunctionInfo) + OBJECT_OFFSETOF(ImportFunctionInfo, importFunction); } |
144 | template<typename T> T* importFunction(unsigned importFunctionNum) { return reinterpret_cast<T*>(&importFunctionInfo(importFunctionNum)->importFunction); } |
145 | |
146 | static_assert(sizeof(ImportFunctionInfo) == WTF::roundUpToMultipleOf<sizeof(uint64_t)>(sizeof(ImportFunctionInfo)), "We rely on this for the alignment to be correct" ); |
147 | static constexpr size_t offsetOfTablePtr(unsigned numImportFunctions, unsigned i) { return offsetOfTail() + sizeof(ImportFunctionInfo) * numImportFunctions + sizeof(Table*) * i; } |
148 | |
149 | void storeTopCallFrame(void* callFrame) |
150 | { |
151 | m_storeTopCallFrame(callFrame); |
152 | } |
153 | |
154 | private: |
155 | Instance(Context*, Ref<Module>&&, EntryFrame**, void**, StoreTopCallFrameCallback&&); |
156 | |
157 | static size_t allocationSize(Checked<size_t> numImportFunctions, Checked<size_t> numTables) |
158 | { |
159 | return (offsetOfTail() + sizeof(ImportFunctionInfo) * numImportFunctions + sizeof(Table*) * numTables).unsafeGet(); |
160 | } |
161 | void* m_owner { nullptr }; // In a JS embedding, this is a JSWebAssemblyInstance*. |
162 | Context* m_context { nullptr }; |
163 | CagedPtr<Gigacage::Primitive, void, tagCagedPtr> m_cachedMemory; |
164 | size_t m_cachedMemorySize { 0 }; |
165 | Ref<Module> m_module; |
166 | RefPtr<CodeBlock> m_codeBlock; |
167 | RefPtr<Memory> m_memory; |
168 | |
169 | union GlobalValue { |
170 | WriteBarrier<Unknown> anyref; |
171 | uint64_t primitive; |
172 | }; |
173 | MallocPtr<GlobalValue> m_globals; |
174 | FunctionWrapperMap m_functionWrappers; |
175 | BitVector m_globalsToMark; |
176 | EntryFrame** m_pointerToTopEntryFrame { nullptr }; |
177 | void** m_pointerToActualStackLimit { nullptr }; |
178 | void* m_cachedStackLimit { bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max()) }; |
179 | StoreTopCallFrameCallback m_storeTopCallFrame; |
180 | unsigned m_numImportFunctions { 0 }; |
181 | }; |
182 | |
183 | } } // namespace JSC::Wasm |
184 | |
185 | #endif // ENABLE(WEBASSEMBLY) |
186 | |