| 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 2021 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_module_types_h |
| 20 |
#define wasm_module_types_h |
| 21 |
|
| 22 |
#include "mozilla/RefPtr.h" |
| 23 |
#include "mozilla/Span.h" |
| 24 |
|
| 25 |
#include "js/AllocPolicy.h" |
| 26 |
#include "js/HashTable.h" |
| 27 |
#include "js/RefCounted.h" |
| 28 |
#include "js/Utility.h" |
| 29 |
#include "js/Vector.h" |
| 30 |
|
| 31 |
#include "wasm/WasmCompileArgs.h" |
| 32 |
#include "wasm/WasmConstants.h" |
| 33 |
#include "wasm/WasmExprType.h" |
| 34 |
#include "wasm/WasmInitExpr.h" |
| 35 |
#include "wasm/WasmMemory.h" |
| 36 |
#include "wasm/WasmSerialize.h" |
| 37 |
#include "wasm/WasmShareable.h" |
| 38 |
#include "wasm/WasmTypeDecls.h" |
| 39 |
#include "wasm/WasmValType.h" |
| 40 |
#include "wasm/WasmValue.h" |
| 41 |
|
| 42 |
namespace js { |
| 43 |
namespace wasm { |
| 44 |
|
| 45 |
class FuncType; |
| 46 |
|
| 47 |
// A Module can either be asm.js or wasm. |
| 48 |
|
| 49 |
enum ModuleKind { Wasm, AsmJS }; |
| 50 |
|
| 51 |
// CacheableChars is used to cacheably store UniqueChars. |
| 52 |
|
| 53 |
struct CacheableChars : UniqueChars { |
| 54 |
CacheableChars() = default; |
| 55 |
explicit CacheableChars(char* ptr) : UniqueChars(ptr) {} |
| 56 |
MOZ_IMPLICIT CacheableChars(UniqueChars&& rhs) |
| 57 |
: UniqueChars(std::move(rhs)) {} |
| 58 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 59 |
}; |
| 60 |
|
| 61 |
using CacheableCharsVector = Vector<CacheableChars, 0, SystemAllocPolicy>; |
| 62 |
|
| 63 |
// CacheableName is used to cacheably store a UTF-8 string that may contain |
| 64 |
// null terminators in sequence. |
| 65 |
|
| 66 |
struct CacheableName { |
| 67 |
private: |
| 68 |
UTF8Bytes bytes_; |
| 69 |
|
| 70 |
const char* begin() const { return (const char*)bytes_.begin(); } |
| 71 |
size_t length() const { return bytes_.length(); } |
| 72 |
|
| 73 |
public: |
| 74 |
CacheableName() = default; |
| 75 |
MOZ_IMPLICIT CacheableName(UTF8Bytes&& rhs) : bytes_(std::move(rhs)) {} |
| 76 |
|
| 77 |
bool isEmpty() const { return bytes_.length() == 0; } |
| 78 |
|
| 79 |
mozilla::Span<char> utf8Bytes() { return mozilla::Span<char>(bytes_); } |
| 80 |
mozilla::Span<const char> utf8Bytes() const { |
| 81 |
return mozilla::Span<const char>(bytes_); |
| 82 |
} |
| 83 |
|
| 84 |
static CacheableName fromUTF8Chars(UniqueChars&& utf8Chars); |
| 85 |
[[nodiscard]] static bool fromUTF8Chars(const char* utf8Chars, |
| 86 |
CacheableName* name); |
| 87 |
|
| 88 |
[[nodiscard]] JSString* toJSString(JSContext* cx) const; |
| 89 |
[[nodiscard]] JSAtom* toAtom(JSContext* cx) const; |
| 90 |
[[nodiscard]] bool toPropertyKey(JSContext* cx, |
| 91 |
MutableHandleId propertyKey) const; |
| 92 |
[[nodiscard]] UniqueChars toQuotedString(JSContext* cx) const; |
| 93 |
|
| 94 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 95 |
WASM_DECLARE_FRIEND_SERIALIZE(CacheableName); |
| 96 |
}; |
| 97 |
|
| 98 |
using CacheableNameVector = Vector<CacheableName, 0, SystemAllocPolicy>; |
| 99 |
|
| 100 |
// A hash policy for names. |
| 101 |
struct NameHasher { |
| 102 |
using Key = mozilla::Span<const char>; |
| 103 |
using Lookup = mozilla::Span<const char>; |
| 104 |
|
| 105 |
static HashNumber hash(const Lookup& aLookup) { |
| 106 |
return mozilla::HashString(aLookup.data(), aLookup.Length()); |
| 107 |
} |
| 108 |
|
| 109 |
static bool match(const Key& aKey, const Lookup& aLookup) { |
| 110 |
return aKey == aLookup; |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
// Import describes a single wasm import. An ImportVector describes all |
| 115 |
// of a single module's imports. |
| 116 |
// |
| 117 |
// ImportVector is built incrementally by ModuleGenerator and then stored |
| 118 |
// immutably by Module. |
| 119 |
|
| 120 |
struct Import { |
| 121 |
CacheableName module; |
| 122 |
CacheableName field; |
| 123 |
DefinitionKind kind; |
| 124 |
|
| 125 |
Import() = default; |
| 126 |
Import(CacheableName&& module, CacheableName&& field, DefinitionKind kind) |
| 127 |
: module(std::move(module)), field(std::move(field)), kind(kind) {} |
| 128 |
|
| 129 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 130 |
}; |
| 131 |
|
| 132 |
using ImportVector = Vector<Import, 0, SystemAllocPolicy>; |
| 133 |
|
| 134 |
// Export describes the export of a definition in a Module to a field in the |
| 135 |
// export object. The Export stores the index of the exported item in the |
| 136 |
// appropriate type-specific module data structure (function table, global |
| 137 |
// table, table table, and - eventually - memory table). |
| 138 |
// |
| 139 |
// Note a single definition can be exported by multiple Exports in the |
| 140 |
// ExportVector. |
| 141 |
// |
| 142 |
// ExportVector is built incrementally by ModuleGenerator and then stored |
| 143 |
// immutably by Module. |
| 144 |
|
| 145 |
class Export { |
| 146 |
public: |
| 147 |
struct CacheablePod { |
| 148 |
DefinitionKind kind_; |
| 149 |
uint32_t index_; |
| 150 |
|
| 151 |
WASM_CHECK_CACHEABLE_POD(kind_, index_); |
| 152 |
}; |
| 153 |
|
| 154 |
private: |
| 155 |
CacheableName fieldName_; |
| 156 |
CacheablePod pod; |
| 157 |
|
| 158 |
public: |
| 159 |
Export() = default; |
| 160 |
explicit Export(CacheableName&& fieldName, uint32_t index, |
| 161 |
DefinitionKind kind); |
| 162 |
|
| 163 |
const CacheableName& fieldName() const { return fieldName_; } |
| 164 |
|
| 165 |
DefinitionKind kind() const { return pod.kind_; } |
| 166 |
uint32_t funcIndex() const; |
| 167 |
uint32_t tagIndex() const; |
| 168 |
uint32_t memoryIndex() const; |
| 169 |
uint32_t globalIndex() const; |
| 170 |
uint32_t tableIndex() const; |
| 171 |
|
| 172 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 173 |
WASM_DECLARE_FRIEND_SERIALIZE(Export); |
| 174 |
}; |
| 175 |
|
| 176 |
WASM_DECLARE_CACHEABLE_POD(Export::CacheablePod); |
| 177 |
|
| 178 |
using ExportVector = Vector<Export, 0, SystemAllocPolicy>; |
| 179 |
|
| 180 |
// FuncFlags provides metadata for a function definition. |
| 181 |
|
| 182 |
enum class FuncFlags : uint8_t { |
| 183 |
None = 0x0, |
| 184 |
// The function maybe be accessible by JS and needs thunks generated for it. |
| 185 |
// See `[SMDOC] Exported wasm functions and the jit-entry stubs` in |
| 186 |
// WasmJS.cpp for more information. |
| 187 |
Exported = 0x1, |
| 188 |
// The function should have thunks generated upon instantiation, not upon |
| 189 |
// first call. May only be set if `Exported` is set. |
| 190 |
Eager = 0x2, |
| 191 |
// The function can be the target of a ref.func instruction in the code |
| 192 |
// section. May only be set if `Exported` is set. |
| 193 |
CanRefFunc = 0x4, |
| 194 |
}; |
| 195 |
|
| 196 |
// A FuncDesc describes a single function definition. |
| 197 |
|
| 198 |
struct FuncDesc { |
| 199 |
// Bit pack to keep this struct small on 32-bit systems |
| 200 |
uint32_t typeIndex : 24; |
| 201 |
FuncFlags flags : 8; |
| 202 |
|
| 203 |
WASM_CHECK_CACHEABLE_POD(typeIndex, flags); |
| 204 |
|
| 205 |
// Assert that the bit packing scheme is viable |
| 206 |
static_assert(MaxTypes <= (1 << 24) - 1); |
| 207 |
static_assert(sizeof(FuncFlags) == sizeof(uint8_t)); |
| 208 |
|
| 209 |
FuncDesc() = default; |
| 210 |
explicit FuncDesc(uint32_t typeIndex) |
| 211 |
: typeIndex(typeIndex), flags(FuncFlags::None) {} |
| 212 |
|
| 213 |
void declareFuncExported(bool eager, bool canRefFunc) { |
| 214 |
// Set the `Exported` flag, if not set. |
| 215 |
flags = FuncFlags(uint8_t(flags) | uint8_t(FuncFlags::Exported)); |
| 216 |
|
| 217 |
// Merge in the `Eager` and `CanRefFunc` flags, if they're set. Be sure |
| 218 |
// to not unset them if they've already been set. |
| 219 |
if (eager) { |
| 220 |
flags = FuncFlags(uint8_t(flags) | uint8_t(FuncFlags::Eager)); |
| 221 |
} |
| 222 |
if (canRefFunc) { |
| 223 |
flags = FuncFlags(uint8_t(flags) | uint8_t(FuncFlags::CanRefFunc)); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
bool isExported() const { |
| 228 |
return uint8_t(flags) & uint8_t(FuncFlags::Exported); |
| 229 |
} |
| 230 |
bool isEager() const { return uint8_t(flags) & uint8_t(FuncFlags::Eager); } |
| 231 |
bool canRefFunc() const { |
| 232 |
return uint8_t(flags) & uint8_t(FuncFlags::CanRefFunc); |
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
WASM_DECLARE_CACHEABLE_POD(FuncDesc); |
| 237 |
|
| 238 |
using FuncDescVector = Vector<FuncDesc, 0, SystemAllocPolicy>; |
| 239 |
|
| 240 |
struct CallRefMetricsRange { |
| 241 |
explicit CallRefMetricsRange() {} |
| 242 |
explicit CallRefMetricsRange(uint32_t begin, uint32_t length) |
| 243 |
: begin(begin), length(length) {} |
| 244 |
|
| 245 |
uint32_t begin = 0; |
| 246 |
uint32_t length = 0; |
| 247 |
|
| 248 |
void offsetBy(uint32_t offset) { begin += offset; } |
| 249 |
|
| 250 |
WASM_CHECK_CACHEABLE_POD(begin, length); |
| 251 |
}; |
| 252 |
|
| 253 |
struct AllocSitesRange { |
| 254 |
explicit AllocSitesRange() {} |
| 255 |
explicit AllocSitesRange(uint32_t begin, uint32_t length) |
| 256 |
: begin(begin), length(length) {} |
| 257 |
|
| 258 |
uint32_t begin = 0; |
| 259 |
uint32_t length = 0; |
| 260 |
|
| 261 |
void offsetBy(uint32_t offset) { begin += offset; } |
| 262 |
|
| 263 |
WASM_CHECK_CACHEABLE_POD(begin, length); |
| 264 |
}; |
| 265 |
|
| 266 |
// A compact plain data summary of CallRefMetrics for use by our function |
| 267 |
// compilers. See CallRefMetrics in WasmInstanceData.h for more information. |
| 268 |
// |
| 269 |
// We cannot allow the metrics collected by an instance to directly be read |
| 270 |
// from our function compilers because they contain thread-local data and are |
| 271 |
// written into without any synchronization. |
| 272 |
// |
| 273 |
// Instead, CodeMetadata contains an array of CallRefHint that every instance |
| 274 |
// writes into when it has a function that requests a tier-up. This array is |
| 275 |
// 1:1 with the non-threadsafe CallRefMetrics that is stored on the instance. |
| 276 |
// |
| 277 |
// This class must be thread safe, as it's read and written from different |
| 278 |
// threads. It is an array of up to 3 function indices, and the entire array |
| 279 |
// can be read/written atomically. Each function index is represented in 20 |
| 280 |
// bits, and 2 of the remaining 4 bits are used to indicate the array's current |
| 281 |
// size. |
| 282 |
// |
| 283 |
// Although unstated and unenforced here, it is expected that -- in the case |
| 284 |
// where more than one function index is stored -- the func index at `.get(0)` |
| 285 |
// is the "most important" in terms of inlining, that at `.get(1)` is the |
| 286 |
// second most important, etc. |
| 287 |
// |
| 288 |
// Note that the fact that this array has 3 elements is unrelated to the value |
| 289 |
// of CallRefMetrics::NUM_TRACKED. The target-collection mechanism will work |
| 290 |
// properly even if CallRefMetrics::NUM_TRACKED is greater than 3, in which |
| 291 |
// case at most only 3 targets (probably the hottest ones) will get baked into |
| 292 |
// the CallRefHint. |
| 293 |
class CallRefHint { |
| 294 |
public: |
| 295 |
using Repr = uint64_t; |
| 296 |
static constexpr size_t NUM_ENTRIES = 3; |
| 297 |
|
| 298 |
private: |
| 299 |
// Representation is: |
| 300 |
// |
| 301 |
// 63 61 42 41 22 21 2 1 0 |
| 302 |
// | | | | | | | | | |
| 303 |
// 00 index#2 index#1 index#0 length |
| 304 |
static constexpr uint32_t ElemBits = 20; |
| 305 |
static constexpr uint32_t LengthBits = 2; |
| 306 |
static constexpr uint64_t Mask = (uint64_t(1) << ElemBits) - 1; |
| 307 |
static_assert(js::wasm::MaxFuncs <= Mask); |
| 308 |
static_assert(3 * ElemBits + LengthBits <= 8 * sizeof(Repr)); |
| 309 |
|
| 310 |
Repr state_ = 0; |
| 311 |
|
| 312 |
bool valid() const { |
| 313 |
// Shift out the length field and all of the entries that the length field |
| 314 |
// implies are occupied. What remains should be all zeroes. |
| 315 |
return (state_ >> (length() * ElemBits + LengthBits)) == 0; |
| 316 |
} |
| 317 |
|
| 318 |
public: |
| 319 |
// We omit the obvious single-argument constructor that takes a `Repr`, |
| 320 |
// because that is too easily confused with one that takes a function index, |
| 321 |
// and in any case it is not necessary. |
| 322 |
|
| 323 |
uint32_t length() const { return state_ & 3; } |
| 324 |
bool empty() const { return length() == 0; } |
| 325 |
bool full() const { return length() == 3; } |
| 326 |
|
| 327 |
uint32_t get(uint32_t index) const { |
| 328 |
MOZ_ASSERT(index < length()); |
| 329 |
uint64_t res = (state_ >> (index * ElemBits + LengthBits)) & Mask; |
| 330 |
return uint32_t(res); |
| 331 |
} |
| 332 |
void set(uint32_t index, uint32_t funcIndex) { |
| 333 |
MOZ_ASSERT(index < length()); |
| 334 |
MOZ_ASSERT(funcIndex <= Mask); |
| 335 |
uint32_t shift = index * ElemBits + LengthBits; |
| 336 |
uint64_t c = uint64_t(Mask) << shift; |
| 337 |
uint64_t s = uint64_t(funcIndex) << shift; |
| 338 |
state_ = (state_ & ~c) | s; |
| 339 |
} |
| 340 |
|
| 341 |
void append(uint32_t funcIndex) { |
| 342 |
MOZ_RELEASE_ASSERT(!full()); |
| 343 |
// We know the lowest two bits of `state_` are not 0b11, so we can |
| 344 |
// increment the length field by incrementing `state_` as a whole. |
| 345 |
state_++; |
| 346 |
set(length() - 1, funcIndex); |
| 347 |
} |
| 348 |
|
| 349 |
static CallRefHint fromRepr(Repr repr) { |
| 350 |
CallRefHint res; |
| 351 |
res.state_ = repr; |
| 352 |
MOZ_ASSERT(res.valid()); |
| 353 |
return res; |
| 354 |
} |
| 355 |
Repr toRepr() const { return state_; } |
| 356 |
}; |
| 357 |
|
| 358 |
static_assert(sizeof(CallRefHint) == sizeof(CallRefHint::Repr)); |
| 359 |
|
| 360 |
using MutableCallRefHint = mozilla::Atomic<CallRefHint::Repr>; |
| 361 |
using MutableCallRefHints = |
| 362 |
mozilla::UniquePtr<MutableCallRefHint[], JS::FreePolicy>; |
| 363 |
|
| 364 |
WASM_DECLARE_CACHEABLE_POD(CallRefMetricsRange); |
| 365 |
|
| 366 |
using CallRefMetricsRangeVector = |
| 367 |
Vector<CallRefMetricsRange, 0, SystemAllocPolicy>; |
| 368 |
|
| 369 |
WASM_DECLARE_CACHEABLE_POD(AllocSitesRange); |
| 370 |
|
| 371 |
using AllocSitesRangeVector = Vector<AllocSitesRange, 0, SystemAllocPolicy>; |
| 372 |
|
| 373 |
enum class BranchHint : uint8_t { Unlikely = 0, Likely = 1, Invalid = 2 }; |
| 374 |
|
| 375 |
// Stores pairs of <BranchOffset, BranchHint> |
| 376 |
struct BranchHintEntry { |
| 377 |
uint32_t branchOffset; |
| 378 |
BranchHint value; |
| 379 |
|
| 380 |
BranchHintEntry() = default; |
| 381 |
BranchHintEntry(uint32_t branchOffset, BranchHint value) |
| 382 |
: branchOffset(branchOffset), value(value) {} |
| 383 |
}; |
| 384 |
|
| 385 |
// Branch hint sorted vector for a function, |
| 386 |
// stores tuples of <BranchOffset, BranchHint> |
| 387 |
using BranchHintVector = Vector<BranchHintEntry, 0, SystemAllocPolicy>; |
| 388 |
using BranchHintFuncMap = HashMap<uint32_t, BranchHintVector, |
| 389 |
DefaultHasher<uint32_t>, SystemAllocPolicy>; |
| 390 |
|
| 391 |
struct BranchHintCollection { |
| 392 |
private: |
| 393 |
// Used for lookups into the collection if a function |
| 394 |
// doesn't contain any hints. |
| 395 |
static BranchHintVector invalidVector_; |
| 396 |
|
| 397 |
// Map from function index to their collection of branch hints |
| 398 |
BranchHintFuncMap branchHintsMap_; |
| 399 |
// Whether the module had branch hints, but we failed to parse them. This |
| 400 |
// is not semantically visible to user code, but used for internal testing. |
| 401 |
bool failedParse_ = false; |
| 402 |
|
| 403 |
public: |
| 404 |
// Add all the branch hints for a function |
| 405 |
[[nodiscard]] bool addHintsForFunc(uint32_t functionIndex, |
| 406 |
BranchHintVector&& branchHints) { |
| 407 |
return branchHintsMap_.put(functionIndex, std::move(branchHints)); |
| 408 |
} |
| 409 |
|
| 410 |
// Return the vector with branch hints for a funcIndex. |
| 411 |
// If this function doesn't contain any hints, return an empty vector. |
| 412 |
BranchHintVector& getHintVector(uint32_t funcIndex) const { |
| 413 |
if (auto hintsVector = |
| 414 |
branchHintsMap_.readonlyThreadsafeLookup(funcIndex)) { |
| 415 |
return hintsVector->value(); |
| 416 |
} |
| 417 |
|
| 418 |
// If not found, return the empty invalid Vector |
| 419 |
return invalidVector_; |
| 420 |
} |
| 421 |
|
| 422 |
bool isEmpty() const { return branchHintsMap_.empty(); } |
| 423 |
|
| 424 |
void setFailedAndClear() { |
| 425 |
failedParse_ = true; |
| 426 |
branchHintsMap_.clearAndCompact(); |
| 427 |
} |
| 428 |
bool failedParse() const { return failedParse_; } |
| 429 |
}; |
| 430 |
|
| 431 |
enum class GlobalKind { Import, Constant, Variable }; |
| 432 |
|
| 433 |
// A GlobalDesc describes a single global variable. |
| 434 |
// |
| 435 |
// wasm can import and export mutable and immutable globals. |
| 436 |
// |
| 437 |
// asm.js can import mutable and immutable globals, but a mutable global has a |
| 438 |
// location that is private to the module, and its initial value is copied into |
| 439 |
// that cell from the environment. asm.js cannot export globals. |
| 440 |
class GlobalDesc { |
| 441 |
GlobalKind kind_; |
| 442 |
// Stores the value type of this global for all kinds, and the initializer |
| 443 |
// expression when `constant` or `variable`. |
| 444 |
InitExpr initial_; |
| 445 |
// Metadata for the global when `variable` or `import`. |
| 446 |
unsigned offset_; |
| 447 |
bool isMutable_; |
| 448 |
bool isWasm_; |
| 449 |
bool isExport_; |
| 450 |
// Metadata for the global when `import`. |
| 451 |
uint32_t importIndex_; |
| 452 |
|
| 453 |
// Private, as they have unusual semantics. |
| 454 |
|
| 455 |
bool isExport() const { return !isConstant() && isExport_; } |
| 456 |
bool isWasm() const { return !isConstant() && isWasm_; } |
| 457 |
|
| 458 |
public: |
| 459 |
GlobalDesc() = default; |
| 460 |
|
| 461 |
explicit GlobalDesc(InitExpr&& initial, bool isMutable, |
| 462 |
ModuleKind kind = ModuleKind::Wasm) |
| 463 |
: kind_((!isMutable && initial.isLiteral()) ? GlobalKind::Constant |
| 464 |
: GlobalKind::Variable) { |
| 465 |
initial_ = std::move(initial); |
| 466 |
if (isVariable()) { |
| 467 |
isMutable_ = isMutable; |
| 468 |
isWasm_ = kind == Wasm; |
| 469 |
isExport_ = false; |
| 470 |
offset_ = UINT32_MAX; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
explicit GlobalDesc(ValType type, bool isMutable, uint32_t importIndex, |
| 475 |
ModuleKind kind = ModuleKind::Wasm) |
| 476 |
: kind_(GlobalKind::Import) { |
| 477 |
initial_ = InitExpr(LitVal(type)); |
| 478 |
importIndex_ = importIndex; |
| 479 |
isMutable_ = isMutable; |
| 480 |
isWasm_ = kind == Wasm; |
| 481 |
isExport_ = false; |
| 482 |
offset_ = UINT32_MAX; |
| 483 |
} |
| 484 |
|
| 485 |
void setOffset(unsigned offset) { |
| 486 |
MOZ_ASSERT(!isConstant()); |
| 487 |
MOZ_ASSERT(offset_ == UINT32_MAX); |
| 488 |
offset_ = offset; |
| 489 |
} |
| 490 |
unsigned offset() const { |
| 491 |
MOZ_ASSERT(!isConstant()); |
| 492 |
MOZ_ASSERT(offset_ != UINT32_MAX); |
| 493 |
return offset_; |
| 494 |
} |
| 495 |
|
| 496 |
void setIsExport() { |
| 497 |
if (!isConstant()) { |
| 498 |
isExport_ = true; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
GlobalKind kind() const { return kind_; } |
| 503 |
bool isVariable() const { return kind_ == GlobalKind::Variable; } |
| 504 |
bool isConstant() const { return kind_ == GlobalKind::Constant; } |
| 505 |
bool isImport() const { return kind_ == GlobalKind::Import; } |
| 506 |
|
| 507 |
bool isMutable() const { return !isConstant() && isMutable_; } |
| 508 |
const InitExpr& initExpr() const { |
| 509 |
MOZ_ASSERT(!isImport()); |
| 510 |
return initial_; |
| 511 |
} |
| 512 |
uint32_t importIndex() const { |
| 513 |
MOZ_ASSERT(isImport()); |
| 514 |
return importIndex_; |
| 515 |
} |
| 516 |
|
| 517 |
LitVal constantValue() const { return initial_.literal(); } |
| 518 |
|
| 519 |
// If isIndirect() is true then storage for the value is not in the |
| 520 |
// instance's global area, but in a WasmGlobalObject::Cell hanging off a |
| 521 |
// WasmGlobalObject; the global area contains a pointer to the Cell. |
| 522 |
// |
| 523 |
// We don't want to indirect unless we must, so only mutable, exposed |
| 524 |
// globals are indirected - in all other cases we copy values into and out |
| 525 |
// of their module. |
| 526 |
// |
| 527 |
// Note that isIndirect() isn't equivalent to getting a WasmGlobalObject: |
| 528 |
// an immutable exported global will still get an object, but will not be |
| 529 |
// indirect. |
| 530 |
bool isIndirect() const { |
| 531 |
return isMutable() && isWasm() && (isImport() || isExport()); |
| 532 |
} |
| 533 |
|
| 534 |
ValType type() const { return initial_.type(); } |
| 535 |
|
| 536 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 537 |
WASM_DECLARE_FRIEND_SERIALIZE(GlobalDesc); |
| 538 |
}; |
| 539 |
|
| 540 |
using GlobalDescVector = Vector<GlobalDesc, 0, SystemAllocPolicy>; |
| 541 |
|
| 542 |
// A TagDesc represents fresh per-instance tags that are used for the |
| 543 |
// exception handling proposal and potentially other future proposals. |
| 544 |
|
| 545 |
// The TagOffsetVector represents the offsets in the layout of the |
| 546 |
// data buffer stored in a Wasm exception. |
| 547 |
using TagOffsetVector = Vector<uint32_t, 2, SystemAllocPolicy>; |
| 548 |
|
| 549 |
class TagType : public AtomicRefCounted<TagType> { |
| 550 |
SharedTypeDef type_; |
| 551 |
TagOffsetVector argOffsets_; |
| 552 |
uint32_t size_; |
| 553 |
|
| 554 |
public: |
| 555 |
TagType() : size_(0) {} |
| 556 |
|
| 557 |
[[nodiscard]] bool initialize(const SharedTypeDef& funcType); |
| 558 |
|
| 559 |
const TypeDef& type() const { return *type_; } |
| 560 |
const ValTypeVector& argTypes() const { return type_->funcType().args(); } |
| 561 |
const TagOffsetVector& argOffsets() const { return argOffsets_; } |
| 562 |
ResultType resultType() const { return ResultType::Vector(argTypes()); } |
| 563 |
|
| 564 |
uint32_t tagSize() const { return size_; } |
| 565 |
|
| 566 |
static bool matches(const TagType& a, const TagType& b) { |
| 567 |
// Note that this does NOT use subtyping. This is deliberate per the spec. |
| 568 |
return a.type_ == b.type_; |
| 569 |
} |
| 570 |
|
| 571 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 572 |
WASM_DECLARE_FRIEND_SERIALIZE(TagType); |
| 573 |
}; |
| 574 |
|
| 575 |
using MutableTagType = RefPtr<TagType>; |
| 576 |
using SharedTagType = RefPtr<const TagType>; |
| 577 |
|
| 578 |
struct TagDesc { |
| 579 |
TagKind kind; |
| 580 |
SharedTagType type; |
| 581 |
bool isExport; |
| 582 |
|
| 583 |
TagDesc() : isExport(false) {} |
| 584 |
TagDesc(TagKind kind, const SharedTagType& type, bool isExport = false) |
| 585 |
: kind(kind), type(type), isExport(isExport) {} |
| 586 |
|
| 587 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 588 |
}; |
| 589 |
|
| 590 |
using TagDescVector = Vector<TagDesc, 0, SystemAllocPolicy>; |
| 591 |
using ElemExprOffsetVector = Vector<size_t, 0, SystemAllocPolicy>; |
| 592 |
|
| 593 |
// This holds info about elem segments that is needed for instantiation. It |
| 594 |
// can be dropped when the associated wasm::Module is dropped. |
| 595 |
struct ModuleElemSegment { |
| 596 |
enum class Kind { |
| 597 |
Active, |
| 598 |
Passive, |
| 599 |
Declared, |
| 600 |
}; |
| 601 |
|
| 602 |
// The type of encoding used by this element segment. 0 is an invalid value to |
| 603 |
// make sure we notice if we fail to correctly initialize the element segment |
| 604 |
// - reading from the wrong representation could be a bad time. |
| 605 |
enum class Encoding { |
| 606 |
Indices = 1, |
| 607 |
Expressions, |
| 608 |
}; |
| 609 |
|
| 610 |
struct Expressions { |
| 611 |
size_t count = 0; |
| 612 |
Bytes exprBytes; |
| 613 |
|
| 614 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 615 |
}; |
| 616 |
|
| 617 |
Kind kind; |
| 618 |
uint32_t tableIndex; |
| 619 |
RefType elemType; |
| 620 |
mozilla::Maybe<InitExpr> offsetIfActive; |
| 621 |
|
| 622 |
// We store either an array of indices or the full bytecode of the element |
| 623 |
// expressions, depending on the encoding used for the element segment. |
| 624 |
Encoding encoding; |
| 625 |
Uint32Vector elemIndices; |
| 626 |
Expressions elemExpressions; |
| 627 |
|
| 628 |
bool active() const { return kind == Kind::Active; } |
| 629 |
|
| 630 |
const InitExpr& offset() const { return *offsetIfActive; } |
| 631 |
|
| 632 |
size_t numElements() const { |
| 633 |
switch (encoding) { |
| 634 |
case Encoding::Indices: |
| 635 |
return elemIndices.length(); |
| 636 |
case Encoding::Expressions: |
| 637 |
return elemExpressions.count; |
| 638 |
default: |
| 639 |
MOZ_CRASH("unknown element segment encoding"); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 644 |
}; |
| 645 |
|
| 646 |
using ModuleElemSegmentVector = Vector<ModuleElemSegment, 0, SystemAllocPolicy>; |
| 647 |
|
| 648 |
using InstanceElemSegment = GCVector<HeapPtr<AnyRef>, 0, SystemAllocPolicy>; |
| 649 |
using InstanceElemSegmentVector = |
| 650 |
GCVector<InstanceElemSegment, 0, SystemAllocPolicy>; |
| 651 |
|
| 652 |
// DataSegmentRange holds the initial results of decoding a data segment from |
| 653 |
// the bytecode and is stored in the ModuleMetadata. It contains the bytecode |
| 654 |
// bounds of the data segment, and some auxiliary information, but not the |
| 655 |
// segment contents itself. |
| 656 |
// |
| 657 |
// When compilation completes, each DataSegmentRange is transformed into a |
| 658 |
// DataSegment, which are also stored in the ModuleMetadata. DataSegment |
| 659 |
// contains the same information as DataSegmentRange but additionally contains |
| 660 |
// the segment contents itself. This allows non-compilation uses of wasm |
| 661 |
// validation to avoid expensive copies. |
| 662 |
// |
| 663 |
// A DataSegment that is "passive" is shared between a ModuleMetadata and its |
| 664 |
// wasm::Instances. To allow each segment to be released as soon as the last |
| 665 |
// Instance mem.drops it and the Module (hence, also the ModuleMetadata) is |
| 666 |
// destroyed, each DataSegment is individually atomically ref-counted. |
| 667 |
|
| 668 |
constexpr uint32_t InvalidMemoryIndex = UINT32_MAX; |
| 669 |
static_assert(InvalidMemoryIndex > MaxMemories, "Invariant"); |
| 670 |
|
| 671 |
struct DataSegmentRange { |
| 672 |
uint32_t memoryIndex; |
| 673 |
mozilla::Maybe<InitExpr> offsetIfActive; |
| 674 |
uint32_t bytecodeOffset; |
| 675 |
uint32_t length; |
| 676 |
}; |
| 677 |
|
| 678 |
using DataSegmentRangeVector = Vector<DataSegmentRange, 0, SystemAllocPolicy>; |
| 679 |
|
| 680 |
struct DataSegment : AtomicRefCounted<DataSegment> { |
| 681 |
uint32_t memoryIndex; |
| 682 |
mozilla::Maybe<InitExpr> offsetIfActive; |
| 683 |
Bytes bytes; |
| 684 |
|
| 685 |
DataSegment() = default; |
| 686 |
|
| 687 |
bool active() const { return !!offsetIfActive; } |
| 688 |
|
| 689 |
const InitExpr& offset() const { return *offsetIfActive; } |
| 690 |
|
| 691 |
[[nodiscard]] bool init(const BytecodeSource& bytecode, |
| 692 |
const DataSegmentRange& src) { |
| 693 |
memoryIndex = src.memoryIndex; |
| 694 |
if (src.offsetIfActive) { |
| 695 |
offsetIfActive.emplace(); |
| 696 |
if (!offsetIfActive->clone(*src.offsetIfActive)) { |
| 697 |
return false; |
| 698 |
} |
| 699 |
} |
| 700 |
MOZ_ASSERT(bytes.length() == 0); |
| 701 |
BytecodeSpan span = |
| 702 |
bytecode.getSpan(BytecodeRange(src.bytecodeOffset, src.length)); |
| 703 |
return bytes.append(span.data(), span.size()); |
| 704 |
} |
| 705 |
|
| 706 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 707 |
}; |
| 708 |
|
| 709 |
using MutableDataSegment = RefPtr<DataSegment>; |
| 710 |
using SharedDataSegment = RefPtr<const DataSegment>; |
| 711 |
using DataSegmentVector = Vector<SharedDataSegment, 0, SystemAllocPolicy>; |
| 712 |
|
| 713 |
// CustomSectionRange and CustomSection are related in the same way that |
| 714 |
// DataSegmentRange and DataSegment are: the CustomSectionRanges are stored in |
| 715 |
// the ModuleMetadata, and are transformed into CustomSections at the end of |
| 716 |
// compilation and stored in wasm::Module. |
| 717 |
|
| 718 |
struct CustomSectionRange { |
| 719 |
BytecodeRange name; |
| 720 |
BytecodeRange payload; |
| 721 |
|
| 722 |
WASM_CHECK_CACHEABLE_POD(name, payload); |
| 723 |
}; |
| 724 |
|
| 725 |
WASM_DECLARE_CACHEABLE_POD(CustomSectionRange); |
| 726 |
|
| 727 |
using CustomSectionRangeVector = |
| 728 |
Vector<CustomSectionRange, 0, SystemAllocPolicy>; |
| 729 |
|
| 730 |
struct CustomSection { |
| 731 |
Bytes name; |
| 732 |
SharedBytes payload; |
| 733 |
|
| 734 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 735 |
}; |
| 736 |
|
| 737 |
using CustomSectionVector = Vector<CustomSection, 0, SystemAllocPolicy>; |
| 738 |
|
| 739 |
// A Name represents a string of utf8 chars embedded within the name custom |
| 740 |
// section. The offset of a name is expressed relative to the beginning of the |
| 741 |
// name section's payload so that Names can stored in wasm::Code, which only |
| 742 |
// holds the name section's bytes, not the whole bytecode. |
| 743 |
|
| 744 |
struct Name { |
| 745 |
// All fields are treated as cacheable POD: |
| 746 |
uint32_t offsetInNamePayload; |
| 747 |
uint32_t length; |
| 748 |
|
| 749 |
WASM_CHECK_CACHEABLE_POD(offsetInNamePayload, length); |
| 750 |
|
| 751 |
Name() : offsetInNamePayload(UINT32_MAX), length(0) {} |
| 752 |
}; |
| 753 |
|
| 754 |
WASM_DECLARE_CACHEABLE_POD(Name); |
| 755 |
|
| 756 |
using NameVector = Vector<Name, 0, SystemAllocPolicy>; |
| 757 |
|
| 758 |
struct NameSection { |
| 759 |
Name moduleName; |
| 760 |
NameVector funcNames; |
| 761 |
uint32_t customSectionIndex; |
| 762 |
|
| 763 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 764 |
}; |
| 765 |
|
| 766 |
// The kind of limits to decode or convert from JS. |
| 767 |
|
| 768 |
enum class LimitsKind { |
| 769 |
Memory, |
| 770 |
Table, |
| 771 |
}; |
| 772 |
|
| 773 |
extern const char* ToString(LimitsKind kind); |
| 774 |
|
| 775 |
// Represents the resizable limits of memories and tables. |
| 776 |
|
| 777 |
struct Limits { |
| 778 |
// `addressType` may be I64 when memory64 is enabled. |
| 779 |
AddressType addressType; |
| 780 |
|
| 781 |
// The initial and maximum limit. The unit is pages for memories and elements |
| 782 |
// for tables. |
| 783 |
uint64_t initial; |
| 784 |
mozilla::Maybe<uint64_t> maximum; |
| 785 |
|
| 786 |
// `shared` is Shareable::False for tables but may be Shareable::True for |
| 787 |
// memories. |
| 788 |
Shareable shared; |
| 789 |
|
| 790 |
WASM_CHECK_CACHEABLE_POD(addressType, initial, maximum, shared); |
| 791 |
|
| 792 |
Limits() = default; |
| 793 |
explicit Limits(uint64_t initial, |
| 794 |
const mozilla::Maybe<uint64_t>& maximum = mozilla::Nothing(), |
| 795 |
Shareable shared = Shareable::False) |
| 796 |
: addressType(AddressType::I32), |
| 797 |
initial(initial), |
| 798 |
maximum(maximum), |
| 799 |
shared(shared) {} |
| 800 |
}; |
| 801 |
|
| 802 |
WASM_DECLARE_CACHEABLE_POD(Limits); |
| 803 |
|
| 804 |
// MemoryDesc describes a memory. |
| 805 |
|
| 806 |
struct MemoryDesc { |
| 807 |
Limits limits; |
| 808 |
|
| 809 |
WASM_CHECK_CACHEABLE_POD(limits); |
| 810 |
|
| 811 |
bool isShared() const { return limits.shared == Shareable::True; } |
| 812 |
|
| 813 |
// Whether a backing store for this memory may move when grown. |
| 814 |
bool canMovingGrow() const { return limits.maximum.isNothing(); } |
| 815 |
|
| 816 |
// Whether the bounds check limit (see the doc comment in |
| 817 |
// ArrayBufferObject.cpp regarding linear memory structure) can ever be |
| 818 |
// larger than 32-bits. |
| 819 |
bool boundsCheckLimitIs32Bits() const { |
| 820 |
return limits.maximum.isSome() && |
| 821 |
limits.maximum.value() < (0x100000000 / PageSize); |
| 822 |
} |
| 823 |
|
| 824 |
AddressType addressType() const { return limits.addressType; } |
| 825 |
|
| 826 |
// The initial length of this memory in pages. |
| 827 |
Pages initialPages() const { return Pages(limits.initial); } |
| 828 |
|
| 829 |
// The maximum length of this memory in pages. |
| 830 |
mozilla::Maybe<Pages> maximumPages() const { |
| 831 |
return limits.maximum.map([](uint64_t x) { return Pages(x); }); |
| 832 |
} |
| 833 |
|
| 834 |
// The initial length of this memory in bytes. Only valid for memory32. |
| 835 |
uint64_t initialLength32() const { |
| 836 |
MOZ_ASSERT(addressType() == AddressType::I32); |
| 837 |
// See static_assert after MemoryDesc for why this is safe. |
| 838 |
return limits.initial * PageSize; |
| 839 |
} |
| 840 |
|
| 841 |
uint64_t initialLength64() const { |
| 842 |
MOZ_ASSERT(addressType() == AddressType::I64); |
| 843 |
return limits.initial * PageSize; |
| 844 |
} |
| 845 |
|
| 846 |
MemoryDesc() {} |
| 847 |
explicit MemoryDesc(Limits limits) : limits(limits) {} |
| 848 |
}; |
| 849 |
|
| 850 |
WASM_DECLARE_CACHEABLE_POD(MemoryDesc); |
| 851 |
|
| 852 |
using MemoryDescVector = Vector<MemoryDesc, 1, SystemAllocPolicy>; |
| 853 |
|
| 854 |
// We don't need to worry about overflow with a Memory32 field when |
| 855 |
// using a uint64_t. |
| 856 |
static_assert(MaxMemory32PagesValidation <= UINT64_MAX / PageSize); |
| 857 |
|
| 858 |
struct TableDesc { |
| 859 |
Limits limits; |
| 860 |
RefType elemType; |
| 861 |
bool isImported; |
| 862 |
bool isExported; |
| 863 |
bool isAsmJS; |
| 864 |
mozilla::Maybe<InitExpr> initExpr; |
| 865 |
|
| 866 |
TableDesc() = default; |
| 867 |
TableDesc(Limits limits, RefType elemType, |
| 868 |
mozilla::Maybe<InitExpr>&& initExpr, bool isAsmJS, |
| 869 |
bool isImported = false, bool isExported = false) |
| 870 |
: limits(limits), |
| 871 |
elemType(elemType), |
| 872 |
isImported(isImported), |
| 873 |
isExported(isExported), |
| 874 |
isAsmJS(isAsmJS), |
| 875 |
initExpr(std::move(initExpr)) {} |
| 876 |
|
| 877 |
AddressType addressType() const { return limits.addressType; } |
| 878 |
|
| 879 |
uint64_t initialLength() const { return limits.initial; } |
| 880 |
|
| 881 |
mozilla::Maybe<uint64_t> maximumLength() const { return limits.maximum; } |
| 882 |
}; |
| 883 |
|
| 884 |
using TableDescVector = Vector<TableDesc, 0, SystemAllocPolicy>; |
| 885 |
|
| 886 |
} // namespace wasm |
| 887 |
} // namespace js |
| 888 |
|
| 889 |
#endif // wasm_module_types_h |