Debian Codegraph @

js/src/wasm/WasmBuiltins.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 2017 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_builtins_h
20 #define wasm_builtins_h
21
22 #include "intgemm/IntegerGemmIntrinsic.h"
23 #include "jit/IonTypes.h"
24 #include "wasm/WasmBuiltinModuleGenerated.h"
25
26 namespace js {
27 class JitFrameIter;
28 namespace jit {
29 class AutoMarkJitCodeWritableForThread;
30 struct ResumeFromException;
31 } // namespace jit
32 namespace wasm {
33
34 class WasmFrameIter;
35 class CodeRange;
36 class FuncType;
37
38 // A wasm::SymbolicAddress represents a pointer to a well-known function/global
39 // that is embedded in wasm code. Since wasm code is serialized and later
40 // deserialized into a different address space, symbolic addresses must be used
41 // for *all* pointers into the address space. The MacroAssembler records a list
42 // of all SymbolicAddresses and the offsets of their use in the code for later
43 // patching during static linking.
44
45 enum class SymbolicAddress {
46 ToInt32,
47 #if defined(JS_CODEGEN_ARM)
48 aeabi_idivmod,
49 aeabi_uidivmod,
50 #endif
51 ModD,
52 SinNativeD,
53 SinFdlibmD,
54 CosNativeD,
55 CosFdlibmD,
56 TanNativeD,
57 TanFdlibmD,
58 ASinD,
59 ACosD,
60 ATanD,
61 CeilD,
62 CeilF,
63 FloorD,
64 FloorF,
65 TruncD,
66 TruncF,
67 NearbyIntD,
68 NearbyIntF,
69 ExpD,
70 LogD,
71 PowD,
72 ATan2D,
73 ArrayMemMove,
74 ArrayRefsMove,
75 HandleDebugTrap,
76 HandleRequestTierUp,
77 HandleThrow,
78 HandleTrap,
79 ReportV128JSCall,
80 CallImport_General,
81 CoerceInPlace_ToInt32,
82 CoerceInPlace_ToNumber,
83 CoerceInPlace_JitEntry,
84 CoerceInPlace_ToBigInt,
85 AllocateBigInt,
86 BoxValue_Anyref,
87 DivI64,
88 UDivI64,
89 ModI64,
90 UModI64,
91 TruncateDoubleToInt64,
92 TruncateDoubleToUint64,
93 SaturatingTruncateDoubleToInt64,
94 SaturatingTruncateDoubleToUint64,
95 Uint64ToFloat32,
96 Uint64ToDouble,
97 Int64ToFloat32,
98 Int64ToDouble,
99 MemoryGrowM32,
100 MemoryGrowM64,
101 MemorySizeM32,
102 MemorySizeM64,
103 WaitI32M32,
104 WaitI32M64,
105 WaitI64M32,
106 WaitI64M64,
107 WakeM32,
108 WakeM64,
109 MemCopyM32,
110 MemCopySharedM32,
111 MemCopyM64,
112 MemCopySharedM64,
113 MemCopyAny,
114 DataDrop,
115 MemFillM32,
116 MemFillSharedM32,
117 MemFillM64,
118 MemFillSharedM64,
119 MemDiscardM32,
120 MemDiscardSharedM32,
121 MemDiscardM64,
122 MemDiscardSharedM64,
123 MemInitM32,
124 MemInitM64,
125 TableCopy,
126 ElemDrop,
127 TableFill,
128 TableGet,
129 TableGrow,
130 TableInit,
131 TableSet,
132 TableSize,
133 RefFunc,
134 PostBarrierEdge,
135 PostBarrierEdgePrecise,
136 PostBarrierWholeCell,
137 ExceptionNew,
138 ThrowException,
139 StructNewIL_true,
140 StructNewIL_false,
141 StructNewOOL_true,
142 StructNewOOL_false,
143 ArrayNew_true,
144 ArrayNew_false,
145 ArrayNewData,
146 ArrayNewElem,
147 ArrayInitData,
148 ArrayInitElem,
149 ArrayCopy,
150 SlotsToAllocKindBytesTable,
151 #define VISIT_BUILTIN_FUNC(op, export, sa_name, ...) sa_name,
152 FOR_EACH_BUILTIN_MODULE_FUNC(VISIT_BUILTIN_FUNC)
153 #undef VISIT_BUILTIN_FUNC
154 #ifdef ENABLE_WASM_JSPI
155 UpdateSuspenderState,
156 #endif
157 #ifdef WASM_CODEGEN_DEBUG
158 PrintI32,
159 PrintPtr,
160 PrintF32,
161 PrintF64,
162 PrintText,
163 #endif
164 Limit
165 };
166
167 // The FailureMode indicates whether, immediately after a call to a builtin
168 // returns, the return value should be checked against an error condition
169 // (and if so, which one) which signals that the C++ calle has already
170 // reported an error and thus wasm needs to wasmTrap(Trap::ThrowReported).
171
172 enum class FailureMode : uint8_t {
173 Infallible,
174 FailOnNegI32,
175 FailOnMaxI32,
176 FailOnNullPtr,
177 FailOnInvalidRef
178 };
179
180 // SymbolicAddressSignature carries type information for a function referred
181 // to by a SymbolicAddress. In order that |argTypes| can be written out as a
182 // static initialiser, it has to have fixed length. At present
183 // SymbolicAddressType is used to describe functions with at most 14 arguments,
184 // so |argTypes| has 15 entries in order to allow the last value to be
185 // MIRType::None, in the hope of catching any accidental overruns of the
186 // defined section of the array.
187
188 static constexpr size_t SymbolicAddressSignatureMaxArgs = 14;
189
190 struct SymbolicAddressSignature {
191 // The SymbolicAddress that is described.
192 const SymbolicAddress identity;
193 // The return type, or MIRType::None to denote 'void'.
194 const jit::MIRType retType;
195 // The failure mode, which is checked by masm.wasmCallBuiltinInstanceMethod.
196 const FailureMode failureMode;
197 // The number of arguments, 0 .. SymbolicAddressSignatureMaxArgs only.
198 const uint8_t numArgs;
199 // The argument types; SymbolicAddressSignatureMaxArgs + 1 guard, which
200 // should be MIRType::None.
201 const jit::MIRType argTypes[SymbolicAddressSignatureMaxArgs + 1];
202 };
203
204 // The 32 in this assertion is derived as follows: SymbolicAddress is probably
205 // size-4 aligned-4, but it's at the start of the struct, so there's no
206 // alignment hole before it. All other components (MIRType and uint8_t) are
207 // size-1 aligned-1, and there are 18 in total, so it is reasonable to assume
208 // that they also don't create any alignment holes. Hence it is also
209 // reasonable to assume that the actual size is 1 * 4 + 18 * 1 == 22. The
210 // worst-plausible-case rounding will take that up to 32. Hence, the
211 // assertion uses 32.
212
213 static_assert(sizeof(SymbolicAddressSignature) <= 32,
214 "SymbolicAddressSignature unexpectedly large");
215
216 // These provide argument type information for a subset of the SymbolicAddress
217 // targets, for which type info is needed to generate correct stackmaps.
218
219 extern const SymbolicAddressSignature SASigSinNativeD;
220 extern const SymbolicAddressSignature SASigSinFdlibmD;
221 extern const SymbolicAddressSignature SASigCosNativeD;
222 extern const SymbolicAddressSignature SASigCosFdlibmD;
223 extern const SymbolicAddressSignature SASigTanNativeD;
224 extern const SymbolicAddressSignature SASigTanFdlibmD;
225 extern const SymbolicAddressSignature SASigASinD;
226 extern const SymbolicAddressSignature SASigACosD;
227 extern const SymbolicAddressSignature SASigATanD;
228 extern const SymbolicAddressSignature SASigCeilD;
229 extern const SymbolicAddressSignature SASigCeilF;
230 extern const SymbolicAddressSignature SASigFloorD;
231 extern const SymbolicAddressSignature SASigFloorF;
232 extern const SymbolicAddressSignature SASigTruncD;
233 extern const SymbolicAddressSignature SASigTruncF;
234 extern const SymbolicAddressSignature SASigNearbyIntD;
235 extern const SymbolicAddressSignature SASigNearbyIntF;
236 extern const SymbolicAddressSignature SASigExpD;
237 extern const SymbolicAddressSignature SASigLogD;
238 extern const SymbolicAddressSignature SASigPowD;
239 extern const SymbolicAddressSignature SASigATan2D;
240 extern const SymbolicAddressSignature SASigArrayMemMove;
241 extern const SymbolicAddressSignature SASigArrayRefsMove;
242 extern const SymbolicAddressSignature SASigMemoryGrowM32;
243 extern const SymbolicAddressSignature SASigMemoryGrowM64;
244 extern const SymbolicAddressSignature SASigMemorySizeM32;
245 extern const SymbolicAddressSignature SASigMemorySizeM64;
246 extern const SymbolicAddressSignature SASigWaitI32M32;
247 extern const SymbolicAddressSignature SASigWaitI32M64;
248 extern const SymbolicAddressSignature SASigWaitI64M32;
249 extern const SymbolicAddressSignature SASigWaitI64M64;
250 extern const SymbolicAddressSignature SASigWakeM32;
251 extern const SymbolicAddressSignature SASigWakeM64;
252 extern const SymbolicAddressSignature SASigMemCopyM32;
253 extern const SymbolicAddressSignature SASigMemCopySharedM32;
254 extern const SymbolicAddressSignature SASigMemCopyM64;
255 extern const SymbolicAddressSignature SASigMemCopySharedM64;
256 extern const SymbolicAddressSignature SASigMemCopyAny;
257 extern const SymbolicAddressSignature SASigDataDrop;
258 extern const SymbolicAddressSignature SASigMemFillM32;
259 extern const SymbolicAddressSignature SASigMemFillSharedM32;
260 extern const SymbolicAddressSignature SASigMemFillM64;
261 extern const SymbolicAddressSignature SASigMemFillSharedM64;
262 extern const SymbolicAddressSignature SASigMemDiscardM32;
263 extern const SymbolicAddressSignature SASigMemDiscardSharedM32;
264 extern const SymbolicAddressSignature SASigMemDiscardM64;
265 extern const SymbolicAddressSignature SASigMemDiscardSharedM64;
266 extern const SymbolicAddressSignature SASigMemInitM32;
267 extern const SymbolicAddressSignature SASigMemInitM64;
268 extern const SymbolicAddressSignature SASigTableCopy;
269 extern const SymbolicAddressSignature SASigElemDrop;
270 extern const SymbolicAddressSignature SASigTableFill;
271 extern const SymbolicAddressSignature SASigTableGet;
272 extern const SymbolicAddressSignature SASigTableGrow;
273 extern const SymbolicAddressSignature SASigTableInit;
274 extern const SymbolicAddressSignature SASigTableSet;
275 extern const SymbolicAddressSignature SASigTableSize;
276 extern const SymbolicAddressSignature SASigRefFunc;
277 extern const SymbolicAddressSignature SASigPostBarrierEdge;
278 extern const SymbolicAddressSignature SASigPostBarrierEdgePrecise;
279 extern const SymbolicAddressSignature SASigPostBarrierWholeCell;
280 extern const SymbolicAddressSignature SASigExceptionNew;
281 extern const SymbolicAddressSignature SASigThrowException;
282 extern const SymbolicAddressSignature SASigStructNewIL_true;
283 extern const SymbolicAddressSignature SASigStructNewIL_false;
284 extern const SymbolicAddressSignature SASigStructNewOOL_true;
285 extern const SymbolicAddressSignature SASigStructNewOOL_false;
286 extern const SymbolicAddressSignature SASigArrayNew_true;
287 extern const SymbolicAddressSignature SASigArrayNew_false;
288 extern const SymbolicAddressSignature SASigArrayNewData;
289 extern const SymbolicAddressSignature SASigArrayNewElem;
290 extern const SymbolicAddressSignature SASigArrayInitData;
291 extern const SymbolicAddressSignature SASigArrayInitElem;
292 extern const SymbolicAddressSignature SASigArrayCopy;
293 extern const SymbolicAddressSignature SASigUpdateSuspenderState;
294 #define VISIT_BUILTIN_FUNC(op, export, sa_name, ...) \
295 extern const SymbolicAddressSignature SASig##sa_name;
296 FOR_EACH_BUILTIN_MODULE_FUNC(VISIT_BUILTIN_FUNC)
297 #undef VISIT_BUILTIN_FUNC
298
299 bool IsRoundingFunction(SymbolicAddress callee, jit::RoundingMode* mode);
300
301 // A SymbolicAddress that NeedsBuiltinThunk() will call through a thunk to the
302 // C++ function. This will be true for all normal calls from normal wasm
303 // function code. Only calls to C++ from other exits/thunks do not need a thunk.
304 // See "The Wasm-builtin ABIs in WasmFrame.h".
305
306 bool NeedsBuiltinThunk(SymbolicAddress sym);
307
308 // This function queries whether pc is in one of the process's builtin thunks
309 // and, if so, returns the CodeRange and pointer to the code segment that the
310 // CodeRange is relative to.
311
312 bool LookupBuiltinThunk(void* pc, const CodeRange** codeRange,
313 const uint8_t** codeBase);
314
315 // EnsureBuiltinThunksInitialized() must be called, and must succeed, before
316 // SymbolicAddressTarget() or MaybeGetBuiltinThunk(). This function creates all
317 // thunks for the process. ReleaseBuiltinThunks() should be called before
318 // ReleaseProcessExecutableMemory() so that the latter can assert that all
319 // executable code has been released.
320
321 bool EnsureBuiltinThunksInitialized();
322 bool EnsureBuiltinThunksInitialized(
323 jit::AutoMarkJitCodeWritableForThread& writable);
324
325 void HandleExceptionWasm(JSContext* cx, JitFrameIter& iter,
326 jit::ResumeFromException* rfe);
327
328 void* SymbolicAddressTarget(SymbolicAddress sym);
329
330 void* ProvisionalLazyJitEntryStub();
331
332 void* MaybeGetBuiltinThunk(JSFunction* f, const FuncType& funcType);
333
334 void ReleaseBuiltinThunks();
335
336 void* AddressOf(SymbolicAddress imm, jit::ABIFunctionType* abiType);
337
338 #ifdef WASM_CODEGEN_DEBUG
339 void PrintI32(int32_t val);
340 void PrintF32(float val);
341 void PrintF64(double val);
342 void PrintPtr(uint8_t* val);
343 void PrintText(const char* out);
344 #endif
345
346 } // namespace wasm
347 } // namespace js
348
349 #endif // wasm_builtins_h