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