| 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_instance_h |
| 20 |
#define wasm_instance_h |
| 21 |
|
| 22 |
#include "mozilla/Atomics.h" |
| 23 |
#include "mozilla/Maybe.h" |
| 24 |
|
| 25 |
#include <functional> |
| 26 |
|
| 27 |
#include "gc/Barrier.h" |
| 28 |
#include "js/shadow/Zone.h" // for BarrierState |
| 29 |
#include "js/Stack.h" // JS::NativeStackLimit |
| 30 |
#include "js/TypeDecls.h" |
| 31 |
#include "vm/SharedMem.h" |
| 32 |
#include "wasm/WasmExprType.h" // for ResultType |
| 33 |
#include "wasm/WasmLog.h" // for PrintCallback |
| 34 |
#include "wasm/WasmModuleTypes.h" |
| 35 |
#include "wasm/WasmShareable.h" // for SeenSet |
| 36 |
#include "wasm/WasmTypeDecls.h" |
| 37 |
#include "wasm/WasmValue.h" |
| 38 |
|
| 39 |
namespace js { |
| 40 |
|
| 41 |
class SharedArrayRawBuffer; |
| 42 |
class WasmBreakpointSite; |
| 43 |
|
| 44 |
class WasmGcObject; |
| 45 |
class WasmStructObject; |
| 46 |
class WasmArrayObject; |
| 47 |
|
| 48 |
namespace gc { |
| 49 |
class StoreBuffer; |
| 50 |
} // namespace gc |
| 51 |
|
| 52 |
namespace wasm { |
| 53 |
|
| 54 |
struct CodeTailMetadata; |
| 55 |
struct FuncDefInstanceData; |
| 56 |
class FuncImport; |
| 57 |
struct FuncImportInstanceData; |
| 58 |
struct FuncExportInstanceData; |
| 59 |
struct MemoryDesc; |
| 60 |
struct MemoryInstanceData; |
| 61 |
class GlobalDesc; |
| 62 |
struct TableDesc; |
| 63 |
struct TableInstanceData; |
| 64 |
struct TagDesc; |
| 65 |
struct TagInstanceData; |
| 66 |
struct TypeDefInstanceData; |
| 67 |
struct CallRefMetrics; |
| 68 |
class WasmFrameIter; |
| 69 |
|
| 70 |
// Instance represents a wasm instance and provides all the support for runtime |
| 71 |
// execution of code in the instance. Instances share various immutable data |
| 72 |
// structures with the Module from which they were instantiated and other |
| 73 |
// instances instantiated from the same Module. However, an Instance has no |
| 74 |
// direct reference to its source Module which allows a Module to be destroyed |
| 75 |
// while it still has live Instances. |
| 76 |
// |
| 77 |
// The instance's code may be shared among multiple instances. |
| 78 |
// |
| 79 |
// An Instance is also known as a 'TlsData'. They used to be separate objects, |
| 80 |
// but have now been unified. Extant references to 'TlsData' will be cleaned |
| 81 |
// up over time. |
| 82 |
class alignas(16) Instance { |
| 83 |
// NOTE: The first fields of Instance are reserved for commonly accessed data |
| 84 |
// from the JIT, such that they have as small an offset as possible. See the |
| 85 |
// next note for the end of this region. |
| 86 |
|
| 87 |
// Pointer to the base of memory 0 (or null if there is no memories). This is |
| 88 |
// always in sync with the MemoryInstanceData for memory 0. |
| 89 |
uint8_t* memory0Base_; |
| 90 |
|
| 91 |
// Bounds check limit in bytes (or zero if there is no memory) for memory 0 |
| 92 |
// This is 64-bits on 64-bit systems so as to allow for heap lengths up to and |
| 93 |
// beyond 4GB, and 32-bits on 32-bit systems, where memories are limited to |
| 94 |
// 2GB. |
| 95 |
// |
| 96 |
// See "Linear memory addresses and bounds checking" in WasmMemory.cpp. |
| 97 |
uintptr_t memory0BoundsCheckLimit_; |
| 98 |
|
| 99 |
// Null or a pointer to a per-module builtin stub that will invoke the Debug |
| 100 |
// Trap Handler. |
| 101 |
void* debugStub_; |
| 102 |
|
| 103 |
// The containing JS::Realm. |
| 104 |
JS::Realm* realm_; |
| 105 |
|
| 106 |
// The containing JSContext. |
| 107 |
JSContext* cx_; |
| 108 |
|
| 109 |
// The pending exception that was found during stack unwinding after a throw. |
| 110 |
// |
| 111 |
// - Only non-null while unwinding the control stack from a wasm-exit stub. |
| 112 |
// until the nearest enclosing Wasm try-catch or try-delegate block. |
| 113 |
// - Set by wasm::HandleThrow, unset by Instance::consumePendingException. |
| 114 |
// - If the unwind target is a `try-delegate`, it is unset by the delegated |
| 115 |
// try-catch block or function body block. |
| 116 |
GCPtr<AnyRef> pendingException_; |
| 117 |
// The tag object of the pending exception. |
| 118 |
GCPtr<AnyRef> pendingExceptionTag_; |
| 119 |
|
| 120 |
// Usually equal to cx->stackLimitForJitCode(JS::StackForUntrustedScript), |
| 121 |
// but can be racily set to trigger immediate trap as an opportunity to |
| 122 |
// CheckForInterrupt without an additional branch. |
| 123 |
mozilla::Atomic<JS::NativeStackLimit, mozilla::Relaxed> stackLimit_; |
| 124 |
|
| 125 |
// Set to 1 when wasm should call CheckForInterrupt. |
| 126 |
mozilla::Atomic<uint32_t, mozilla::Relaxed> interrupt_; |
| 127 |
|
| 128 |
// Boolean value set to true when instance code is executed on a suspendable |
| 129 |
// stack. Aligned to int32_t to be used on JIT code. |
| 130 |
int32_t onSuspendableStack_; |
| 131 |
|
| 132 |
// The address of the realm()->zone()->needsIncrementalBarrier(). This is |
| 133 |
// specific to this instance and not a process wide field, and so it cannot |
| 134 |
// be linked into code. |
| 135 |
const JS::shadow::Zone::BarrierState* addressOfNeedsIncrementalBarrier_; |
| 136 |
|
| 137 |
// An array of AllocSites allocated for Wasm GC operations such as struct.new, |
| 138 |
// array.new, etc. |
| 139 |
js::gc::AllocSite* allocSites_; |
| 140 |
|
| 141 |
public: |
| 142 |
// NOTE: All fields commonly accessed by the JIT must be above this method, |
| 143 |
// and this method adapted for the last field present. This method is used |
| 144 |
// to assert that we can use compact offsets on x86(-64) for these fields. |
| 145 |
// We cannot have the assertion here, due to C++ 'offsetof' rules. |
| 146 |
static constexpr size_t offsetOfLastCommonJitField() { |
| 147 |
return offsetof(Instance, allocSites_); |
| 148 |
} |
| 149 |
|
| 150 |
// The number of baseline scratch storage words available. |
| 151 |
static constexpr size_t N_BASELINE_SCRATCH_WORDS = 4; |
| 152 |
|
| 153 |
private: |
| 154 |
// When compiling with tiering, the jumpTable has one entry for each |
| 155 |
// baseline-compiled function. |
| 156 |
void** jumpTable_; |
| 157 |
|
| 158 |
// 4 words of scratch storage for the baseline compiler, which can't always |
| 159 |
// use the stack for this. |
| 160 |
uintptr_t baselineScratchWords_[N_BASELINE_SCRATCH_WORDS]; |
| 161 |
|
| 162 |
// The class_ of WasmValueBox, this is a per-process value. We could patch |
| 163 |
// this into code, but the only use-sites are register restricted and cannot |
| 164 |
// easily use a symbolic address. |
| 165 |
const JSClass* valueBoxClass_; |
| 166 |
|
| 167 |
// Address of the JitRuntime's arguments rectifier trampoline |
| 168 |
void* jsJitArgsRectifier_; |
| 169 |
|
| 170 |
// Address of the JitRuntime's exception handler trampoline |
| 171 |
void* jsJitExceptionHandler_; |
| 172 |
|
| 173 |
// Address of the JitRuntime's object prebarrier trampoline |
| 174 |
void* preBarrierCode_; |
| 175 |
|
| 176 |
// Address of the store buffer for this instance |
| 177 |
gc::StoreBuffer* storeBuffer_; |
| 178 |
|
| 179 |
// Weak pointer to WasmInstanceObject that owns this instance |
| 180 |
WeakHeapPtr<WasmInstanceObject*> object_; |
| 181 |
|
| 182 |
// The wasm::Code for this instance |
| 183 |
const SharedCode code_; |
| 184 |
|
| 185 |
// The tables for this instance, if any |
| 186 |
const SharedTableVector tables_; |
| 187 |
|
| 188 |
// Passive data segments for use with bulk memory instructions |
| 189 |
DataSegmentVector passiveDataSegments_; |
| 190 |
|
| 191 |
// Passive elem segments for use with tables |
| 192 |
InstanceElemSegmentVector passiveElemSegments_; |
| 193 |
|
| 194 |
// The wasm::DebugState for this instance, if any |
| 195 |
const UniqueDebugState maybeDebug_; |
| 196 |
|
| 197 |
// If debugging, this is a per-funcIndex bit table denoting whether debugging |
| 198 |
// is currently enabled for the function within the instance. The flag is set |
| 199 |
// if any breakpoint or function entry or exit point needs to be visited. It |
| 200 |
// is OK to conservatively set this flag, but there is very significant |
| 201 |
// overhead to taking a breakpoint trap, so managing it precisely is |
| 202 |
// worthwhile. |
| 203 |
uint32_t* debugFilter_; |
| 204 |
|
| 205 |
// A pointer to an array of metrics for all the call_ref's in this instance. |
| 206 |
// This is only used with lazy tiering for collecting speculative inlining |
| 207 |
// information. |
| 208 |
CallRefMetrics* callRefMetrics_; |
| 209 |
|
| 210 |
// The exclusive maximum index of a global that has been initialized so far. |
| 211 |
uint32_t maxInitializedGlobalsIndexPlus1_; |
| 212 |
|
| 213 |
// Pointer that should be freed (due to padding before the Instance). |
| 214 |
void* allocatedBase_; |
| 215 |
|
| 216 |
// Fields from the JS context for memory allocation, stashed on the instance |
| 217 |
// so it can be accessed from JIT code. |
| 218 |
const void* addressOfNurseryPosition_; |
| 219 |
#ifdef JS_GC_ZEAL |
| 220 |
const void* addressOfGCZealModeBits_; |
| 221 |
#endif |
| 222 |
|
| 223 |
// A copy of the runtime's addressOfLastBufferedWholeCell, used for whole-cell |
| 224 |
// store buffer entries. |
| 225 |
const void* addressOfLastBufferedWholeCell_; |
| 226 |
|
| 227 |
// Pointer to a per-module builtin stub that will request tier-up for the |
| 228 |
// wasm function that calls it. |
| 229 |
void* requestTierUpStub_ = nullptr; |
| 230 |
|
| 231 |
// Pointer to a per-module builtin stub that does the OOL component of a |
| 232 |
// call-ref metrics update. |
| 233 |
void* updateCallRefMetricsStub_ = nullptr; |
| 234 |
|
| 235 |
// The data must be the last field. Globals for the module start here |
| 236 |
// and are inline in this structure. 16-byte alignment is required for SIMD |
| 237 |
// data. |
| 238 |
MOZ_ALIGNED_DECL(16, char data_); |
| 239 |
|
| 240 |
// Internal helpers: |
| 241 |
FuncDefInstanceData* funcDefInstanceData(uint32_t funcIndex) const; |
| 242 |
TypeDefInstanceData* typeDefInstanceData(uint32_t typeIndex) const; |
| 243 |
const void* addressOfGlobalCell(const GlobalDesc& globalDesc) const; |
| 244 |
FuncImportInstanceData& funcImportInstanceData(uint32_t funcIndex); |
| 245 |
FuncExportInstanceData& funcExportInstanceData(uint32_t funcExportIndex); |
| 246 |
MemoryInstanceData& memoryInstanceData(uint32_t memoryIndex) const; |
| 247 |
TableInstanceData& tableInstanceData(uint32_t tableIndex) const; |
| 248 |
TagInstanceData& tagInstanceData(uint32_t tagIndex) const; |
| 249 |
|
| 250 |
// Only WasmInstanceObject can call the private trace function. |
| 251 |
friend class js::WasmInstanceObject; |
| 252 |
void tracePrivate(JSTracer* trc); |
| 253 |
|
| 254 |
bool callImport(JSContext* cx, uint32_t funcImportIndex, unsigned argc, |
| 255 |
uint64_t* argv); |
| 256 |
|
| 257 |
Instance(JSContext* cx, Handle<WasmInstanceObject*> object, |
| 258 |
const SharedCode& code, SharedTableVector&& tables, |
| 259 |
UniqueDebugState maybeDebug); |
| 260 |
~Instance(); |
| 261 |
|
| 262 |
public: |
| 263 |
static Instance* create(JSContext* cx, Handle<WasmInstanceObject*> object, |
| 264 |
const SharedCode& code, uint32_t instanceDataLength, |
| 265 |
SharedTableVector&& tables, |
| 266 |
UniqueDebugState maybeDebug); |
| 267 |
static void destroy(Instance* instance); |
| 268 |
|
| 269 |
bool init(JSContext* cx, const JSObjectVector& funcImports, |
| 270 |
const ValVector& globalImportValues, |
| 271 |
Handle<WasmMemoryObjectVector> memories, |
| 272 |
const WasmGlobalObjectVector& globalObjs, |
| 273 |
const WasmTagObjectVector& tagObjs, |
| 274 |
const DataSegmentVector& dataSegments, |
| 275 |
const ModuleElemSegmentVector& elemSegments); |
| 276 |
|
| 277 |
// Trace any GC roots on the stack, for the frame associated with |wfi|, |
| 278 |
// whose next instruction to execute is |nextPC|. |
| 279 |
// |
| 280 |
// For consistency checking of StackMap sizes in debug builds, this also |
| 281 |
// takes |highestByteVisitedInPrevFrame|, which is the address of the |
| 282 |
// highest byte scanned in the frame below this one on the stack, and in |
| 283 |
// turn it returns the address of the highest byte scanned in this frame. |
| 284 |
// |
| 285 |
// The method does not assert RootMarkingPhase since it can be used to trace |
| 286 |
// suspended stacks. |
| 287 |
uintptr_t traceFrame(JSTracer* trc, const wasm::WasmFrameIter& wfi, |
| 288 |
uint8_t* nextPC, |
| 289 |
uintptr_t highestByteVisitedInPrevFrame); |
| 290 |
void updateFrameForMovingGC(const wasm::WasmFrameIter& wfi, uint8_t* nextPC); |
| 291 |
|
| 292 |
static constexpr size_t offsetOfMemory0Base() { |
| 293 |
return offsetof(Instance, memory0Base_); |
| 294 |
} |
| 295 |
static constexpr size_t offsetOfMemory0BoundsCheckLimit() { |
| 296 |
return offsetof(Instance, memory0BoundsCheckLimit_); |
| 297 |
} |
| 298 |
static constexpr size_t offsetOfDebugStub() { |
| 299 |
return offsetof(Instance, debugStub_); |
| 300 |
} |
| 301 |
static constexpr size_t offsetOfRequestTierUpStub() { |
| 302 |
return offsetof(Instance, requestTierUpStub_); |
| 303 |
} |
| 304 |
static constexpr size_t offsetOfUpdateCallRefMetricsStub() { |
| 305 |
return offsetof(Instance, updateCallRefMetricsStub_); |
| 306 |
} |
| 307 |
|
| 308 |
static constexpr size_t offsetOfRealm() { return offsetof(Instance, realm_); } |
| 309 |
static constexpr size_t offsetOfCx() { return offsetof(Instance, cx_); } |
| 310 |
static constexpr size_t offsetOfValueBoxClass() { |
| 311 |
return offsetof(Instance, valueBoxClass_); |
| 312 |
} |
| 313 |
static constexpr size_t offsetOfPendingException() { |
| 314 |
return offsetof(Instance, pendingException_); |
| 315 |
} |
| 316 |
static constexpr size_t offsetOfPendingExceptionTag() { |
| 317 |
return offsetof(Instance, pendingExceptionTag_); |
| 318 |
} |
| 319 |
static constexpr size_t offsetOfStackLimit() { |
| 320 |
return offsetof(Instance, stackLimit_); |
| 321 |
} |
| 322 |
static constexpr size_t offsetOfInterrupt() { |
| 323 |
return offsetof(Instance, interrupt_); |
| 324 |
} |
| 325 |
static constexpr size_t offsetOfOnSuspendableStack() { |
| 326 |
return offsetof(Instance, onSuspendableStack_); |
| 327 |
} |
| 328 |
static constexpr size_t offsetOfAllocSites() { |
| 329 |
return offsetof(Instance, allocSites_); |
| 330 |
} |
| 331 |
static constexpr size_t offsetOfAddressOfLastBufferedWholeCell() { |
| 332 |
return offsetof(Instance, addressOfLastBufferedWholeCell_); |
| 333 |
} |
| 334 |
static constexpr size_t offsetOfAddressOfNeedsIncrementalBarrier() { |
| 335 |
return offsetof(Instance, addressOfNeedsIncrementalBarrier_); |
| 336 |
} |
| 337 |
static constexpr size_t offsetOfJumpTable() { |
| 338 |
return offsetof(Instance, jumpTable_); |
| 339 |
} |
| 340 |
static constexpr size_t offsetOfBaselineScratchWords() { |
| 341 |
return offsetof(Instance, baselineScratchWords_); |
| 342 |
} |
| 343 |
static constexpr size_t sizeOfBaselineScratchWords() { |
| 344 |
return sizeof(baselineScratchWords_); |
| 345 |
} |
| 346 |
static constexpr size_t offsetOfJSJitArgsRectifier() { |
| 347 |
return offsetof(Instance, jsJitArgsRectifier_); |
| 348 |
} |
| 349 |
static constexpr size_t offsetOfJSJitExceptionHandler() { |
| 350 |
return offsetof(Instance, jsJitExceptionHandler_); |
| 351 |
} |
| 352 |
static constexpr size_t offsetOfPreBarrierCode() { |
| 353 |
return offsetof(Instance, preBarrierCode_); |
| 354 |
} |
| 355 |
static constexpr size_t offsetOfDebugFilter() { |
| 356 |
return offsetof(Instance, debugFilter_); |
| 357 |
} |
| 358 |
static constexpr size_t offsetOfCallRefMetrics() { |
| 359 |
return offsetof(Instance, callRefMetrics_); |
| 360 |
} |
| 361 |
static constexpr size_t offsetOfData() { return offsetof(Instance, data_); } |
| 362 |
static constexpr size_t offsetInData(size_t offset) { |
| 363 |
return offsetOfData() + offset; |
| 364 |
} |
| 365 |
static constexpr size_t offsetOfAddressOfNurseryPosition() { |
| 366 |
return offsetof(Instance, addressOfNurseryPosition_); |
| 367 |
} |
| 368 |
#ifdef JS_GC_ZEAL |
| 369 |
static constexpr size_t offsetOfAddressOfGCZealModeBits() { |
| 370 |
return offsetof(Instance, addressOfGCZealModeBits_); |
| 371 |
} |
| 372 |
#endif |
| 373 |
|
| 374 |
JSContext* cx() const { return cx_; } |
| 375 |
void* debugStub() const { return debugStub_; } |
| 376 |
void setDebugStub(void* newStub) { debugStub_ = newStub; } |
| 377 |
void setRequestTierUpStub(void* newStub) { requestTierUpStub_ = newStub; } |
| 378 |
void setUpdateCallRefMetricsStub(void* newStub) { |
| 379 |
updateCallRefMetricsStub_ = newStub; |
| 380 |
} |
| 381 |
JS::Realm* realm() const { return realm_; } |
| 382 |
bool debugEnabled() const { return !!maybeDebug_; } |
| 383 |
DebugState& debug() { return *maybeDebug_; } |
| 384 |
uint8_t* data() const { return (uint8_t*)&data_; } |
| 385 |
const SharedTableVector& tables() const { return tables_; } |
| 386 |
SharedMem<uint8_t*> memoryBase(uint32_t memoryIndex) const; |
| 387 |
WasmMemoryObject* memory(uint32_t memoryIndex) const; |
| 388 |
size_t memoryMappedSize(uint32_t memoryIndex) const; |
| 389 |
SharedArrayRawBuffer* sharedMemoryBuffer( |
| 390 |
uint32_t memoryIndex) const; // never null |
| 391 |
bool memoryAccessInGuardRegion(const uint8_t* addr, unsigned numBytes) const; |
| 392 |
|
| 393 |
// Methods to set, test and clear the interrupt fields. Both interrupt |
| 394 |
// fields are Relaxed and so no consistency/ordering can be assumed. |
| 395 |
|
| 396 |
void setInterrupt(); |
| 397 |
bool isInterrupted() const; |
| 398 |
void resetInterrupt(JSContext* cx); |
| 399 |
|
| 400 |
void setTemporaryStackLimit(JS::NativeStackLimit limit); |
| 401 |
void resetTemporaryStackLimit(JSContext* cx); |
| 402 |
|
| 403 |
int32_t computeInitialHotnessCounter(uint32_t funcIndex, |
| 404 |
size_t codeSectionSize); |
| 405 |
void resetHotnessCounter(uint32_t funcIndex); |
| 406 |
int32_t readHotnessCounter(uint32_t funcIndex) const; |
| 407 |
void submitCallRefHints(uint32_t funcIndex); |
| 408 |
|
| 409 |
bool debugFilter(uint32_t funcIndex) const; |
| 410 |
void setDebugFilter(uint32_t funcIndex, bool value); |
| 411 |
|
| 412 |
const Code& code() const { return *code_; } |
| 413 |
inline const CodeMetadata& codeMeta() const; |
| 414 |
inline const CodeTailMetadata& codeTailMeta() const; |
| 415 |
inline const CodeMetadataForAsmJS* codeMetaForAsmJS() const; |
| 416 |
inline bool isAsmJS() const; |
| 417 |
|
| 418 |
// This method returns a pointer to the GC object that owns this Instance. |
| 419 |
// Instances may be reached via weak edges (e.g., Realm::instances_) |
| 420 |
// so this perform a read-barrier on the returned object unless the barrier |
| 421 |
// is explicitly waived. |
| 422 |
|
| 423 |
WasmInstanceObject* object() const; |
| 424 |
WasmInstanceObject* objectUnbarriered() const; |
| 425 |
|
| 426 |
// Get or create the exported function wrapper for a function index. |
| 427 |
|
| 428 |
[[nodiscard]] bool getExportedFunction(JSContext* cx, uint32_t funcIndex, |
| 429 |
MutableHandleFunction result); |
| 430 |
|
| 431 |
// Execute the given export given the JS call arguments, storing the return |
| 432 |
// value in args.rval. |
| 433 |
|
| 434 |
[[nodiscard]] bool callExport(JSContext* cx, uint32_t funcIndex, |
| 435 |
const CallArgs& args, |
| 436 |
CoercionLevel level = CoercionLevel::Spec); |
| 437 |
|
| 438 |
// Exception handling support |
| 439 |
|
| 440 |
void setPendingException(Handle<WasmExceptionObject*> exn); |
| 441 |
|
| 442 |
// Constant expression support |
| 443 |
|
| 444 |
void constantGlobalGet(uint32_t globalIndex, MutableHandleVal result); |
| 445 |
WasmStructObject* constantStructNewDefault(JSContext* cx, uint32_t typeIndex); |
| 446 |
WasmArrayObject* constantArrayNewDefault(JSContext* cx, uint32_t typeIndex, |
| 447 |
uint32_t numElements); |
| 448 |
|
| 449 |
// Return the name associated with a given function index, or generate one |
| 450 |
// if none was given by the module. |
| 451 |
|
| 452 |
JSAtom* getFuncDisplayAtom(JSContext* cx, uint32_t funcIndex) const; |
| 453 |
void ensureProfilingLabels(bool profilingEnabled) const; |
| 454 |
|
| 455 |
// Called by Wasm(Memory|Table)Object when a moving resize occurs: |
| 456 |
|
| 457 |
void onMovingGrowMemory(const WasmMemoryObject* memory); |
| 458 |
void onMovingGrowTable(const Table* table); |
| 459 |
|
| 460 |
bool initSegments(JSContext* cx, const DataSegmentVector& dataSegments, |
| 461 |
const ModuleElemSegmentVector& elemSegments); |
| 462 |
|
| 463 |
// Called to apply a single ElemSegment at a given offset, assuming |
| 464 |
// that all bounds validation has already been performed. |
| 465 |
[[nodiscard]] bool initElems(JSContext* cx, uint32_t tableIndex, |
| 466 |
const ModuleElemSegment& seg, |
| 467 |
uint32_t dstOffset); |
| 468 |
|
| 469 |
// Iterates through elements of a ModuleElemSegment containing functions. |
| 470 |
// Unlike iterElemsAnyrefs, this method can get function data (instance and |
| 471 |
// code pointers) without creating intermediate JSFunctions. |
| 472 |
// |
| 473 |
// NOTE: This method only works for element segments that use the index |
| 474 |
// encoding. If the expression encoding is used, you must use |
| 475 |
// iterElemsAnyrefs. |
| 476 |
// |
| 477 |
// Signature for onFunc: |
| 478 |
// |
| 479 |
// (uint32_t index, void* code, Instance* instance) -> bool |
| 480 |
// |
| 481 |
template <typename F> |
| 482 |
[[nodiscard]] bool iterElemsFunctions(const ModuleElemSegment& seg, |
| 483 |
const F& onFunc); |
| 484 |
|
| 485 |
// Iterates through elements of a ModuleElemSegment. This method works for any |
| 486 |
// type of wasm ref and both element segment encodings. As required by AnyRef, |
| 487 |
// any functions will be wrapped in JSFunction - if possible, you should use |
| 488 |
// iterElemsFunctions to avoid this. |
| 489 |
// |
| 490 |
// Signature for onAnyRef: |
| 491 |
// |
| 492 |
// (uint32_t index, AnyRef ref) -> bool |
| 493 |
// |
| 494 |
template <typename F> |
| 495 |
[[nodiscard]] bool iterElemsAnyrefs(JSContext* cx, |
| 496 |
const ModuleElemSegment& seg, |
| 497 |
const F& onAnyRef); |
| 498 |
|
| 499 |
// Debugger support: |
| 500 |
|
| 501 |
JSString* createDisplayURL(JSContext* cx); |
| 502 |
WasmBreakpointSite* getOrCreateBreakpointSite(JSContext* cx, uint32_t offset); |
| 503 |
void destroyBreakpointSite(JS::GCContext* gcx, uint32_t offset); |
| 504 |
|
| 505 |
// about:memory reporting: |
| 506 |
|
| 507 |
void addSizeOfMisc(mozilla::MallocSizeOf mallocSizeOf, |
| 508 |
SeenSet<CodeMetadata>* seenCodeMeta, |
| 509 |
SeenSet<CodeMetadataForAsmJS>* seenCodeMetaForAsmJS, |
| 510 |
SeenSet<Code>* seenCode, SeenSet<Table>* seenTables, |
| 511 |
size_t* code, size_t* data) const; |
| 512 |
|
| 513 |
// Wasm disassembly support |
| 514 |
|
| 515 |
void disassembleExport(JSContext* cx, uint32_t funcIndex, Tier tier, |
| 516 |
PrintCallback printString) const; |
| 517 |
|
| 518 |
public: |
| 519 |
// Functions to be called directly from wasm code. |
| 520 |
static int32_t callImport_general(Instance*, int32_t, int32_t, uint64_t*); |
| 521 |
static uint32_t memoryGrow_m32(Instance* instance, uint32_t delta, |
| 522 |
uint32_t memoryIndex); |
| 523 |
static uint64_t memoryGrow_m64(Instance* instance, uint64_t delta, |
| 524 |
uint32_t memoryIndex); |
| 525 |
static uint32_t memorySize_m32(Instance* instance, uint32_t memoryIndex); |
| 526 |
static uint64_t memorySize_m64(Instance* instance, uint32_t memoryIndex); |
| 527 |
static int32_t memCopy_m32(Instance* instance, uint32_t dstByteOffset, |
| 528 |
uint32_t srcByteOffset, uint32_t len, |
| 529 |
uint8_t* memBase); |
| 530 |
static int32_t memCopyShared_m32(Instance* instance, uint32_t dstByteOffset, |
| 531 |
uint32_t srcByteOffset, uint32_t len, |
| 532 |
uint8_t* memBase); |
| 533 |
static int32_t memCopy_m64(Instance* instance, uint64_t dstByteOffset, |
| 534 |
uint64_t srcByteOffset, uint64_t len, |
| 535 |
uint8_t* memBase); |
| 536 |
static int32_t memCopyShared_m64(Instance* instance, uint64_t dstByteOffset, |
| 537 |
uint64_t srcByteOffset, uint64_t len, |
| 538 |
uint8_t* memBase); |
| 539 |
static int32_t memCopy_any(Instance* instance, uint64_t dstByteOffset, |
| 540 |
uint64_t srcByteOffset, uint64_t len, |
| 541 |
uint32_t dstMemIndex, uint32_t srcMemIndex); |
| 542 |
|
| 543 |
static int32_t memFill_m32(Instance* instance, uint32_t byteOffset, |
| 544 |
uint32_t value, uint32_t len, uint8_t* memBase); |
| 545 |
static int32_t memFillShared_m32(Instance* instance, uint32_t byteOffset, |
| 546 |
uint32_t value, uint32_t len, |
| 547 |
uint8_t* memBase); |
| 548 |
static int32_t memFill_m64(Instance* instance, uint64_t byteOffset, |
| 549 |
uint32_t value, uint64_t len, uint8_t* memBase); |
| 550 |
static int32_t memFillShared_m64(Instance* instance, uint64_t byteOffset, |
| 551 |
uint32_t value, uint64_t len, |
| 552 |
uint8_t* memBase); |
| 553 |
static int32_t memInit_m32(Instance* instance, uint32_t dstOffset, |
| 554 |
uint32_t srcOffset, uint32_t len, |
| 555 |
uint32_t segIndex, uint32_t memIndex); |
| 556 |
static int32_t memInit_m64(Instance* instance, uint64_t dstOffset, |
| 557 |
uint32_t srcOffset, uint32_t len, |
| 558 |
uint32_t segIndex, uint32_t memIndex); |
| 559 |
static int32_t dataDrop(Instance* instance, uint32_t segIndex); |
| 560 |
static int32_t tableCopy(Instance* instance, uint32_t dstOffset, |
| 561 |
uint32_t srcOffset, uint32_t len, |
| 562 |
uint32_t dstTableIndex, uint32_t srcTableIndex); |
| 563 |
static int32_t tableFill(Instance* instance, uint32_t start, void* value, |
| 564 |
uint32_t len, uint32_t tableIndex); |
| 565 |
static int32_t memDiscard_m32(Instance* instance, uint32_t byteOffset, |
| 566 |
uint32_t byteLen, uint8_t* memBase); |
| 567 |
static int32_t memDiscardShared_m32(Instance* instance, uint32_t byteOffset, |
| 568 |
uint32_t byteLen, uint8_t* memBase); |
| 569 |
static int32_t memDiscard_m64(Instance* instance, uint64_t byteOffset, |
| 570 |
uint64_t byteLen, uint8_t* memBase); |
| 571 |
static int32_t memDiscardShared_m64(Instance* instance, uint64_t byteOffset, |
| 572 |
uint64_t byteLen, uint8_t* memBase); |
| 573 |
static void* tableGet(Instance* instance, uint32_t address, |
| 574 |
uint32_t tableIndex); |
| 575 |
static uint32_t tableGrow(Instance* instance, void* initValue, uint32_t delta, |
| 576 |
uint32_t tableIndex); |
| 577 |
static int32_t tableSet(Instance* instance, uint32_t address, void* value, |
| 578 |
uint32_t tableIndex); |
| 579 |
static uint32_t tableSize(Instance* instance, uint32_t tableIndex); |
| 580 |
static int32_t tableInit(Instance* instance, uint32_t dstOffset, |
| 581 |
uint32_t srcOffset, uint32_t len, uint32_t segIndex, |
| 582 |
uint32_t tableIndex); |
| 583 |
static int32_t elemDrop(Instance* instance, uint32_t segIndex); |
| 584 |
static int32_t wait_i32_m32(Instance* instance, uint32_t byteOffset, |
| 585 |
int32_t value, int64_t timeout, |
| 586 |
uint32_t memoryIndex); |
| 587 |
static int32_t wait_i32_m64(Instance* instance, uint64_t byteOffset, |
| 588 |
int32_t value, int64_t timeout, |
| 589 |
uint32_t memoryIndex); |
| 590 |
static int32_t wait_i64_m32(Instance* instance, uint32_t byteOffset, |
| 591 |
int64_t value, int64_t timeout, |
| 592 |
uint32_t memoryIndex); |
| 593 |
static int32_t wait_i64_m64(Instance* instance, uint64_t byteOffset, |
| 594 |
int64_t value, int64_t timeout, |
| 595 |
uint32_t memoryIndex); |
| 596 |
static int32_t wake_m32(Instance* instance, uint32_t byteOffset, |
| 597 |
int32_t count, uint32_t memoryIndex); |
| 598 |
static int32_t wake_m64(Instance* instance, uint64_t byteOffset, |
| 599 |
int32_t count, uint32_t memoryIndex); |
| 600 |
static void* refFunc(Instance* instance, uint32_t funcIndex); |
| 601 |
static void postBarrierEdge(Instance* instance, AnyRef* location); |
| 602 |
static void postBarrierEdgePrecise(Instance* instance, AnyRef* location, |
| 603 |
void* prev); |
| 604 |
static void postBarrierWholeCell(Instance* instance, gc::Cell* object); |
| 605 |
static void* exceptionNew(Instance* instance, void* exceptionArg); |
| 606 |
static int32_t throwException(Instance* instance, void* exceptionArg); |
| 607 |
template <bool ZeroFields> |
| 608 |
static void* structNewIL(Instance* instance, uint32_t typeDefIndex, |
| 609 |
gc::AllocSite* allocSite); |
| 610 |
template <bool ZeroFields> |
| 611 |
static void* structNewOOL(Instance* instance, uint32_t typeDefIndex, |
| 612 |
gc::AllocSite* allocSite); |
| 613 |
template <bool ZeroFields> |
| 614 |
static void* arrayNew(Instance* instance, uint32_t numElements, |
| 615 |
uint32_t typeDefIndex, gc::AllocSite* allocSite); |
| 616 |
static void* arrayNewData(Instance* instance, uint32_t segByteOffset, |
| 617 |
uint32_t numElements, uint32_t typeDefIndex, |
| 618 |
gc::AllocSite* allocSite, uint32_t segIndex); |
| 619 |
static void* arrayNewElem(Instance* instance, uint32_t srcOffset, |
| 620 |
uint32_t numElements, uint32_t typeDefIndex, |
| 621 |
gc::AllocSite* allocSite, uint32_t segIndex); |
| 622 |
static int32_t arrayInitData(Instance* instance, void* array, uint32_t index, |
| 623 |
uint32_t segByteOffset, uint32_t numElements, |
| 624 |
uint32_t segIndex); |
| 625 |
static int32_t arrayInitElem(Instance* instance, void* array, uint32_t index, |
| 626 |
uint32_t segOffset, uint32_t numElements, |
| 627 |
uint32_t typeDefIndex, uint32_t segIndex); |
| 628 |
static int32_t arrayCopy(Instance* instance, void* dstArray, |
| 629 |
uint32_t dstIndex, void* srcArray, uint32_t srcIndex, |
| 630 |
uint32_t numElements, uint32_t elementSize); |
| 631 |
static int32_t arrayFill(Instance* instance, void* array, uint32_t index, |
| 632 |
uint32_t numElements); |
| 633 |
static int32_t refTest(Instance* instance, void* refPtr, |
| 634 |
const wasm::TypeDef* typeDef); |
| 635 |
static int32_t intrI8VecMul(Instance* instance, uint32_t dest, uint32_t src1, |
| 636 |
uint32_t src2, uint32_t len, uint8_t* memBase); |
| 637 |
|
| 638 |
#ifdef ENABLE_WASM_JS_STRING_BUILTINS |
| 639 |
static int32_t stringTest(Instance* instance, void* stringArg); |
| 640 |
static void* stringCast(Instance* instance, void* stringArg); |
| 641 |
static void* stringFromCharCodeArray(Instance* instance, void* arrayArg, |
| 642 |
uint32_t arrayStart, uint32_t arrayEnd); |
| 643 |
static int32_t stringIntoCharCodeArray(Instance* instance, void* stringArg, |
| 644 |
void* arrayArg, uint32_t arrayStart); |
| 645 |
static void* stringFromCharCode(Instance* instance, uint32_t charCode); |
| 646 |
static void* stringFromCodePoint(Instance* instance, uint32_t codePoint); |
| 647 |
static int32_t stringCharCodeAt(Instance* instance, void* stringArg, |
| 648 |
uint32_t index); |
| 649 |
static int32_t stringCodePointAt(Instance* instance, void* stringArg, |
| 650 |
uint32_t index); |
| 651 |
static int32_t stringLength(Instance* instance, void* stringArg); |
| 652 |
static void* stringConcat(Instance* instance, void* firstStringArg, |
| 653 |
void* secondStringArg); |
| 654 |
static void* stringSubstring(Instance* instance, void* stringArg, |
| 655 |
uint32_t startIndex, uint32_t endIndex); |
| 656 |
static int32_t stringEquals(Instance* instance, void* firstStringArg, |
| 657 |
void* secondStringArg); |
| 658 |
static int32_t stringCompare(Instance* instance, void* firstStringArg, |
| 659 |
void* secondStringArg); |
| 660 |
#endif // ENABLE_WASM_JS_STRING_BUILTINS |
| 661 |
}; |
| 662 |
|
| 663 |
bool ResultsToJSValue(JSContext* cx, ResultType type, void* registerResultLoc, |
| 664 |
mozilla::Maybe<char*> stackResultsLoc, |
| 665 |
MutableHandleValue rval, |
| 666 |
CoercionLevel level = CoercionLevel::Spec); |
| 667 |
|
| 668 |
// Report an error to `cx` and mark it as a 'trap' so that it cannot be caught |
| 669 |
// by wasm exception handlers. |
| 670 |
void ReportTrapError(JSContext* cx, unsigned errorNumber); |
| 671 |
|
| 672 |
// Mark an already reported error as a 'trap' so that it cannot be caught by |
| 673 |
// wasm exception handlers. |
| 674 |
void MarkPendingExceptionAsTrap(JSContext* cx); |
| 675 |
|
| 676 |
// Instance is not a GC thing itself but contains GC thing pointers. Ensure they |
| 677 |
// are traced appropriately. |
| 678 |
void TraceInstanceEdge(JSTracer* trc, Instance* instance, const char* name); |
| 679 |
|
| 680 |
} // namespace wasm |
| 681 |
} // namespace js |
| 682 |
|
| 683 |
#endif // wasm_instance_h |