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