Debian Codegraph @

js/src/wasm/WasmPI.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 *
4 * Copyright 2016 Mozilla Foundation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #ifndef wasm_pi_h
20 #define wasm_pi_h
21
22 #include "mozilla/DoublyLinkedList.h" // for DoublyLinkedListElement
23
24 #include "js/TypeDecls.h"
25 #include "wasm/WasmAnyRef.h"
26 #include "wasm/WasmTypeDef.h"
27
28 // [SMDOC] JS Promise Integration
29 //
30 // The API provides relatively efficient and relatively ergonomic interop
31 // between JavaScript promises and WebAssembly but works under the constraint
32 // that the only changes are to the JS API and not to the core wasm.
33 //
34 // Secondary (suspendable) stacks are introduced at the entrance into the wasm
35 // code -- a promising function. A suspendable stack can contain/store only
36 // wasm frames and be part of one activation. If there is a need to execute
37 // a JS script, the stack must be switched back (to the main stack).
38 //
39 // There is a special exit from the suspendable stack where it is expected to
40 // receive a promise from a JS script -- a suspending function/import. If wasm
41 // code calls such import, the suspendable stack will be unlinked from
42 // the current activation allowing the main stack to continue returning to
43 // the event loop.
44 //
45 // Here is a small example that uses JS Promise Integration API:
46 //
47 // const suspending = new WebAssembly.Suspending(async () => 42)
48 // const ins = wasmTextEval(`(module
49 // (import "" "suspending" (func $imp (result i32)))
50 // (func (export "entry") (result i32) (call $imp))
51 // )`, {"": { suspending, }})
52 // const promising = WebAssembly.promising(ins.exports.entry)
53 // assertEq(await promising(), 42)
54 //
55 // The states transitions can be described by the following diagram:
56 //
57 // Invoke
58 // Promising Promise
59 // +-------+ Export +----------+ Resolved +---------+
60 // |Initial+----------->|Wasm Logic|<-----------+Suspended|
61 // +-------+ ++-+------++ +---------+
62 // | | ^ |Invoke ^ Suspending Function
63 // Return from| | | |Suspending | Returns a Promise
64 // +--------+ Wasm Call | | | |Import +----+---+
65 // |Finished|<-----------+ | | +------------>|JS Logic|
66 // +--------+ | | +----+---+
67 // +------------+ | | Re-entry
68 // |Invoke Other |Return +------>
69 // |Import +--+-----+
70 // +---------->|JS Logic|
71 // +--------+
72 //
73 // The Invoke Promising Export transition creates a suspendable stack,
74 // switches to it, and continues execution of wasm code there. When the callee
75 // frame is popped, the promise is returned to the JS caller.
76 //
77 // The Invoke Suspending Import switches stack to the main one and sets
78 // the suspended stack aside. It is expected that the suspending promise
79 // is returned by JS. The callee returns to the moment the promising call was
80 // instantiated.
81 //
82 // The Return from Wasm Call transition destroys the suspendable stack,
83 // continues execution on the main stack, and resolves the promising promise
84 // with the results of the call.
85 //
86 // The Promise Resolve transition is invoked when the suspending promise is
87 // resolved, which wakes the suspendable stack and extends the main one.
88 // The execution will continue on the suspendable stack that returns the
89 // resolution values to the wasm code as return values.
90 //
91 // The Invoke Other Import transition temporary switches to the main stack and
92 // invokes the JS code. The suspendable stack will not be removed from the
93 // chain of frames.
94 //
95 // Notice that calling wasm and then invoking a suspendable import from
96 // the main stack is not allowed. For example, re-exporting $imp, from
97 // the small example above, and calling it directly from the JS main thread
98 // will fail.
99 //
100 // The `new WebAssembly.Suspending(fn)` logic is implemented in a Wasm module
101 // generated by `SuspendingFunctionModuleFactory` utility (see WasmPI.cpp).
102 // The `WebAssembly.promising(wasmfn)` logic is implemented in a Wasm module
103 // generated by `PromisingFunctionModuleFactory` utility.
104
105 namespace js {
106
107 class PromiseObject;
108 class WasmStructObject;
109
110 namespace wasm {
111
112 class SuspenderContext;
113 class SuspenderObject;
114
115 static const uint32_t SuspenderObjectDataSlot = 0;
116
117 enum SuspenderState {
118 Initial,
119 Moribund,
120 Active,
121 Suspended,
122 };
123
124 class SuspenderObjectData
125 : public mozilla::DoublyLinkedListElement<SuspenderObjectData> {
126 void* stackMemory_;
127
128 // Stored main stack FP register.
129 void* mainFP_;
130
131 // Stored main stack SP register.
132 void* mainSP_;
133
134 // Stored suspendable stack FP register.
135 void* suspendableFP_;
136
137 // Stored suspendable stack SP register.
138 void* suspendableSP_;
139
140 // Stored suspendable stack exit/bottom frame pointer.
141 void* suspendableExitFP_;
142
143 // Stored return address for return to suspendable stack.
144 void* suspendedReturnAddress_;
145
146 // Stored main stack exit/top frame pointer.
147 void* mainExitFP_;
148
149 SuspenderState state_;
150
151 // Identify context that is holding suspended stack, otherwise nullptr.
152 SuspenderContext* suspendedBy_;
153
154 #if defined(_WIN32)
155 // The storage of main stack limits during stack switching.
156 // See updateTibFields and restoreTibFields below.
157 void* savedStackBase_;
158 void* savedStackLimit_;
159 #endif
160
161 public:
162 explicit SuspenderObjectData(void* stackMemory);
163
164 inline SuspenderState state() const { return state_; }
165 void setState(SuspenderState state) { state_ = state; }
166
167 inline bool traceable() const { return suspendedBy_ != nullptr; }
168 inline SuspenderContext* suspendedBy() const { return suspendedBy_; }
169 void setSuspendedBy(SuspenderContext* suspendedBy) {
170 suspendedBy_ = suspendedBy;
171 }
172
173 inline void* stackMemory() const { return stackMemory_; }
174 inline void* mainFP() const { return mainFP_; }
175 inline void* mainSP() const { return mainSP_; }
176 inline void* mainExitFP() const { return mainExitFP_; }
177 inline void* suspendableFP() const { return suspendableFP_; }
178 inline void* suspendableSP() const { return suspendableSP_; }
179 inline void* suspendableExitFP() const { return suspendableExitFP_; }
180 inline void* suspendedReturnAddress() const {
181 return suspendedReturnAddress_;
182 }
183
184 void releaseStackMemory();
185
186 #if defined(_WIN32)
187 void updateTIBStackFields();
188 void restoreTIBStackFields();
189 #endif
190
191 #if defined(JS_SIMULATOR_ARM64) || defined(JS_SIMULATOR_ARM)
192 void switchSimulatorToMain();
193 void switchSimulatorToSuspendable();
194 #endif
195
196 static constexpr size_t offsetOfMainFP() {
197 return offsetof(SuspenderObjectData, mainFP_);
198 }
199
200 static constexpr size_t offsetOfMainSP() {
201 return offsetof(SuspenderObjectData, mainSP_);
202 }
203
204 static constexpr size_t offsetOfSuspendableFP() {
205 return offsetof(SuspenderObjectData, suspendableFP_);
206 }
207
208 static constexpr size_t offsetOfSuspendableSP() {
209 return offsetof(SuspenderObjectData, suspendableSP_);
210 }
211
212 static constexpr size_t offsetOfSuspendableExitFP() {
213 return offsetof(SuspenderObjectData, suspendableExitFP_);
214 }
215
216 static constexpr size_t offsetOfMainExitFP() {
217 return offsetof(SuspenderObjectData, mainExitFP_);
218 }
219
220 static constexpr size_t offsetOfSuspendedReturnAddress() {
221 return offsetof(SuspenderObjectData, suspendedReturnAddress_);
222 }
223 };
224
225 #ifdef ENABLE_WASM_JSPI
226
227 using CallOnMainStackFn = bool (*)(void* data);
228 bool CallOnMainStack(JSContext* cx, CallOnMainStackFn fn, void* data);
229
230 JSFunction* WasmSuspendingFunctionCreate(JSContext* cx, HandleObject func,
231 wasm::ValTypeVector&& params,
232 wasm::ValTypeVector&& results);
233
234 JSFunction* WasmSuspendingFunctionCreate(JSContext* cx, HandleObject func,
235 const FuncType& type);
236
237 JSFunction* WasmPromisingFunctionCreate(JSContext* cx, HandleObject func,
238 wasm::ValTypeVector&& params,
239 wasm::ValTypeVector&& results);
240
241 SuspenderObject* CurrentSuspender(Instance* instance, int reserved);
242
243 SuspenderObject* CreateSuspender(Instance* instance, int reserved);
244
245 PromiseObject* CreatePromisingPromise(Instance* instance,
246 SuspenderObject* suspender);
247
248 JSObject* GetSuspendingPromiseResult(Instance* instance, void* result,
249 SuspenderObject* suspender);
250
251 void* AddPromiseReactions(Instance* instance, SuspenderObject* suspender,
252 void* result, JSFunction* continueOnSuspendable);
253
254 void* ForwardExceptionToSuspended(Instance* instance,
255 SuspenderObject* suspender, void* exception);
256
257 int32_t SetPromisingPromiseResults(Instance* instance,
258 SuspenderObject* suspender,
259 WasmStructObject* results);
260
261 void UpdateSuspenderState(Instance* instance, SuspenderObject* suspender,
262 UpdateSuspenderStateAction action);
263
264 #endif // ENABLE_WASM_JSPI
265
266 } // namespace wasm
267 } // namespace js
268
269 #endif // wasm_pi_h