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