| 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_type_def_h |
| 20 |
#define wasm_type_def_h |
| 21 |
|
| 22 |
#include "mozilla/Assertions.h" |
| 23 |
#include "mozilla/CheckedInt.h" |
| 24 |
#include "mozilla/HashTable.h" |
| 25 |
|
| 26 |
#include "js/RefCounted.h" |
| 27 |
|
| 28 |
#include "wasm/WasmCodegenConstants.h" |
| 29 |
#include "wasm/WasmCompileArgs.h" |
| 30 |
#include "wasm/WasmConstants.h" |
| 31 |
#include "wasm/WasmSerialize.h" |
| 32 |
#include "wasm/WasmUtility.h" |
| 33 |
#include "wasm/WasmValType.h" |
| 34 |
|
| 35 |
namespace js { |
| 36 |
namespace wasm { |
| 37 |
|
| 38 |
class RecGroup; |
| 39 |
|
| 40 |
//========================================================================= |
| 41 |
// Function types |
| 42 |
|
| 43 |
// The FuncType class represents a WebAssembly function signature which takes a |
| 44 |
// list of value types and returns an expression type. The engine uses two |
| 45 |
// in-memory representations of the argument Vector's memory (when elements do |
| 46 |
// not fit inline): normal malloc allocation (via SystemAllocPolicy) and |
| 47 |
// allocation in a LifoAlloc (via LifoAllocPolicy). The former FuncType objects |
| 48 |
// can have any lifetime since they own the memory. The latter FuncType objects |
| 49 |
// must not outlive the associated LifoAlloc mark/release interval (which is |
| 50 |
// currently the duration of module validation+compilation). Thus, long-lived |
| 51 |
// objects like WasmModule must use malloced allocation. |
| 52 |
|
| 53 |
class FuncType { |
| 54 |
ValTypeVector args_; |
| 55 |
ValTypeVector results_; |
| 56 |
// A packed structural type identifier for use in the call_indirect type |
| 57 |
// check in the prologue of functions. If this function type cannot fit in |
| 58 |
// this immediate, it will be NO_IMMEDIATE_TYPE_ID. |
| 59 |
// |
| 60 |
// This is initialized in DecodeTypeSection once we have the full recursion |
| 61 |
// group around. |
| 62 |
uint32_t immediateTypeId_ = NO_IMMEDIATE_TYPE_ID; |
| 63 |
|
| 64 |
// This function type cannot be packed into an immediate for call_indirect |
| 65 |
// signature checks. |
| 66 |
static const uint32_t NO_IMMEDIATE_TYPE_ID = UINT32_MAX; |
| 67 |
|
| 68 |
// Entry from JS to wasm via the JIT is currently unimplemented for |
| 69 |
// functions that return multiple values. |
| 70 |
bool temporarilyUnsupportedResultCountForJitEntry() const { |
| 71 |
return results().length() > MaxResultsForJitEntry; |
| 72 |
} |
| 73 |
// Calls out from wasm to JS that return multiple values is currently |
| 74 |
// unsupported. |
| 75 |
bool temporarilyUnsupportedResultCountForJitExit() const { |
| 76 |
return results().length() > MaxResultsForJitExit; |
| 77 |
} |
| 78 |
// For JS->wasm jit entries, temporarily disallow certain types until the |
| 79 |
// stubs generator is improved. |
| 80 |
// * ref params may be nullable externrefs |
| 81 |
// * ref results may not be type indices |
| 82 |
// V128 types are excluded per spec but are guarded against separately. |
| 83 |
bool temporarilyUnsupportedReftypeForEntry() const { |
| 84 |
for (ValType arg : args()) { |
| 85 |
if (arg.isRefType() && (!arg.isExternRef() || !arg.isNullable())) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
for (ValType result : results()) { |
| 90 |
if (result.isTypeRef()) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
} |
| 94 |
return false; |
| 95 |
} |
| 96 |
// For wasm->JS jit exits, temporarily disallow certain types until |
| 97 |
// the stubs generator is improved. |
| 98 |
// * ref results may be nullable externrefs |
| 99 |
// Unexposable types must be guarded against separately. |
| 100 |
bool temporarilyUnsupportedReftypeForExit() const { |
| 101 |
for (ValType result : results()) { |
| 102 |
if (result.isRefType() && |
| 103 |
(!result.isExternRef() || !result.isNullable())) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
} |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
public: |
| 111 |
FuncType() = default; |
| 112 |
FuncType(ValTypeVector&& args, ValTypeVector&& results) |
| 113 |
: args_(std::move(args)), results_(std::move(results)) { |
| 114 |
MOZ_ASSERT(args_.length() <= MaxParams); |
| 115 |
MOZ_ASSERT(results_.length() <= MaxResults); |
| 116 |
} |
| 117 |
|
| 118 |
FuncType(FuncType&&) = default; |
| 119 |
FuncType& operator=(FuncType&&) = default; |
| 120 |
|
| 121 |
[[nodiscard]] bool clone(const FuncType& src) { |
| 122 |
MOZ_ASSERT(args_.empty()); |
| 123 |
MOZ_ASSERT(results_.empty()); |
| 124 |
immediateTypeId_ = src.immediateTypeId_; |
| 125 |
return args_.appendAll(src.args_) && results_.appendAll(src.results_); |
| 126 |
} |
| 127 |
|
| 128 |
ValType arg(unsigned i) const { return args_[i]; } |
| 129 |
const ValTypeVector& args() const { return args_; } |
| 130 |
ValType result(unsigned i) const { return results_[i]; } |
| 131 |
const ValTypeVector& results() const { return results_; } |
| 132 |
|
| 133 |
void initImmediateTypeId(bool isFinal, const TypeDef* superTypeDef, |
| 134 |
uint32_t recGroupLength); |
| 135 |
bool hasImmediateTypeId() const { |
| 136 |
return immediateTypeId_ != NO_IMMEDIATE_TYPE_ID; |
| 137 |
} |
| 138 |
uint32_t immediateTypeId() const { |
| 139 |
MOZ_ASSERT(hasImmediateTypeId()); |
| 140 |
return immediateTypeId_; |
| 141 |
} |
| 142 |
|
| 143 |
// The lsb for every immediate type id is set to distinguish an immediate type |
| 144 |
// id from a type id represented by a pointer to the global hash type set. |
| 145 |
static const uint32_t ImmediateBit = 0x1; |
| 146 |
|
| 147 |
HashNumber hash(const RecGroup* recGroup) const { |
| 148 |
HashNumber hn = 0; |
| 149 |
for (const ValType& vt : args_) { |
| 150 |
hn = mozilla::AddToHash(hn, vt.forIsoEquals(recGroup).hash()); |
| 151 |
} |
| 152 |
for (const ValType& vt : results_) { |
| 153 |
hn = mozilla::AddToHash(hn, vt.forIsoEquals(recGroup).hash()); |
| 154 |
} |
| 155 |
return hn; |
| 156 |
} |
| 157 |
|
| 158 |
// Compares two function types for isorecursive equality. See |
| 159 |
// "Comparing type definitions" in WasmValType.h for more background. |
| 160 |
static bool isoEquals(const RecGroup* lhsRecGroup, const FuncType& lhs, |
| 161 |
const RecGroup* rhsRecGroup, const FuncType& rhs) { |
| 162 |
if (lhs.args_.length() != rhs.args_.length() || |
| 163 |
lhs.results_.length() != rhs.results_.length()) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
for (uint32_t i = 0; i < lhs.args_.length(); i++) { |
| 167 |
if (lhs.args_[i].forIsoEquals(lhsRecGroup) != |
| 168 |
rhs.args_[i].forIsoEquals(rhsRecGroup)) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
} |
| 172 |
for (uint32_t i = 0; i < lhs.results_.length(); i++) { |
| 173 |
if (lhs.results_[i].forIsoEquals(lhsRecGroup) != |
| 174 |
rhs.results_[i].forIsoEquals(rhsRecGroup)) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
// Checks if every arg and result of the specified function types are bitwise |
| 182 |
// equal. Type references must therefore point to exactly the same type |
| 183 |
// definition instance. |
| 184 |
static bool strictlyEquals(const FuncType& lhs, const FuncType& rhs) { |
| 185 |
return EqualContainers(lhs.args(), rhs.args()) && |
| 186 |
EqualContainers(lhs.results(), rhs.results()); |
| 187 |
} |
| 188 |
|
| 189 |
// Checks if two function types are compatible in a given subtyping |
| 190 |
// relationship. |
| 191 |
static bool canBeSubTypeOf(const FuncType& subType, |
| 192 |
const FuncType& superType) { |
| 193 |
// A subtype must have exactly as many arguments as its supertype |
| 194 |
if (subType.args().length() != superType.args().length()) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
// A subtype must have exactly as many returns as its supertype |
| 199 |
if (subType.results().length() != superType.results().length()) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
// Function result types are covariant |
| 204 |
for (uint32_t i = 0; i < superType.results().length(); i++) { |
| 205 |
if (!ValType::isSubTypeOf(subType.results()[i], superType.results()[i])) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// Function argument types are contravariant |
| 211 |
for (uint32_t i = 0; i < superType.args().length(); i++) { |
| 212 |
if (!ValType::isSubTypeOf(superType.args()[i], subType.args()[i])) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
bool canHaveJitEntry() const; |
| 221 |
bool canHaveJitExit() const; |
| 222 |
|
| 223 |
bool hasInt64Arg() const { |
| 224 |
for (ValType arg : args()) { |
| 225 |
if (arg.kind() == ValType::Kind::I64) { |
| 226 |
return true; |
| 227 |
} |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
bool hasUnexposableArgOrRet() const { |
| 233 |
for (ValType arg : args()) { |
| 234 |
if (!arg.isExposable()) { |
| 235 |
return true; |
| 236 |
} |
| 237 |
} |
| 238 |
for (ValType result : results()) { |
| 239 |
if (!result.isExposable()) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
} |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 247 |
WASM_DECLARE_FRIEND_SERIALIZE(FuncType); |
| 248 |
}; |
| 249 |
|
| 250 |
//========================================================================= |
| 251 |
// Structure types |
| 252 |
|
| 253 |
// The Module owns a dense array of StructType values that represent the |
| 254 |
// structure types that the module knows about. It is created from the sparse |
| 255 |
// array of types in the ModuleMetadata when the Module is created. |
| 256 |
|
| 257 |
struct FieldType { |
| 258 |
StorageType type; |
| 259 |
bool isMutable; |
| 260 |
|
| 261 |
FieldType() : isMutable(false) {} |
| 262 |
FieldType(StorageType type, bool isMutable) |
| 263 |
: type(type), isMutable(isMutable) {} |
| 264 |
|
| 265 |
HashNumber hash(const RecGroup* recGroup) const { |
| 266 |
HashNumber hn = 0; |
| 267 |
hn = mozilla::AddToHash(hn, type.forIsoEquals(recGroup).hash()); |
| 268 |
hn = mozilla::AddToHash(hn, HashNumber(isMutable)); |
| 269 |
return hn; |
| 270 |
} |
| 271 |
|
| 272 |
// Compares two field types for isorecursive equality. See |
| 273 |
// "Comparing type definitions" in WasmValType.h for more background. |
| 274 |
static bool isoEquals(const RecGroup* lhsRecGroup, const FieldType& lhs, |
| 275 |
const RecGroup* rhsRecGroup, const FieldType& rhs) { |
| 276 |
return lhs.isMutable == rhs.isMutable && |
| 277 |
lhs.type.forIsoEquals(lhsRecGroup) == |
| 278 |
rhs.type.forIsoEquals(rhsRecGroup); |
| 279 |
} |
| 280 |
|
| 281 |
// Checks if two struct fields are compatible in a given subtyping |
| 282 |
// relationship. |
| 283 |
static bool canBeSubTypeOf(const FieldType& subType, |
| 284 |
const FieldType& superType) { |
| 285 |
// Mutable fields are invariant w.r.t. field types |
| 286 |
if (subType.isMutable && superType.isMutable) { |
| 287 |
return subType.type == superType.type; |
| 288 |
} |
| 289 |
|
| 290 |
// Immutable fields are covariant w.r.t. field types |
| 291 |
if (!subType.isMutable && !superType.isMutable) { |
| 292 |
return StorageType::isSubTypeOf(subType.type, superType.type); |
| 293 |
} |
| 294 |
|
| 295 |
return false; |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
using FieldTypeVector = Vector<FieldType, 0, SystemAllocPolicy>; |
| 300 |
|
| 301 |
using FieldOffsetVector = Vector<uint32_t, 2, SystemAllocPolicy>; |
| 302 |
using InlineTraceOffsetVector = Vector<uint32_t, 2, SystemAllocPolicy>; |
| 303 |
using OutlineTraceOffsetVector = Vector<uint32_t, 0, SystemAllocPolicy>; |
| 304 |
|
| 305 |
class StructType { |
| 306 |
public: |
| 307 |
// Vector of the fields in this struct |
| 308 |
FieldTypeVector fields_; |
| 309 |
// The total size of this struct in bytes |
| 310 |
uint32_t size_; |
| 311 |
// The offset for every struct field |
| 312 |
FieldOffsetVector fieldOffsets_; |
| 313 |
// The offsets of fields that must be traced in the inline portion of wasm |
| 314 |
// struct object. |
| 315 |
InlineTraceOffsetVector inlineTraceOffsets_; |
| 316 |
// The offsets of fields that must be traced in the outline portion of wasm |
| 317 |
// struct object. |
| 318 |
OutlineTraceOffsetVector outlineTraceOffsets_; |
| 319 |
// Whether this struct only contains defaultable fields. |
| 320 |
bool isDefaultable_; |
| 321 |
|
| 322 |
public: |
| 323 |
StructType() : size_(0), isDefaultable_(false) {} |
| 324 |
|
| 325 |
explicit StructType(FieldTypeVector&& fields) |
| 326 |
: fields_(std::move(fields)), size_(0) { |
| 327 |
MOZ_ASSERT(fields_.length() <= MaxStructFields); |
| 328 |
} |
| 329 |
|
| 330 |
StructType(StructType&&) = default; |
| 331 |
StructType& operator=(StructType&&) = default; |
| 332 |
|
| 333 |
[[nodiscard]] bool init(); |
| 334 |
|
| 335 |
bool isDefaultable() const { return isDefaultable_; } |
| 336 |
|
| 337 |
uint32_t fieldOffset(uint32_t fieldIndex) const { |
| 338 |
return fieldOffsets_[fieldIndex]; |
| 339 |
} |
| 340 |
|
| 341 |
HashNumber hash(const RecGroup* recGroup) const { |
| 342 |
HashNumber hn = 0; |
| 343 |
for (const FieldType& field : fields_) { |
| 344 |
hn = mozilla::AddToHash(hn, field.hash(recGroup)); |
| 345 |
} |
| 346 |
return hn; |
| 347 |
} |
| 348 |
|
| 349 |
// Compares two struct types for isorecursive equality. See |
| 350 |
// "Comparing type definitions" in WasmValType.h for more background. |
| 351 |
static bool isoEquals(const RecGroup* lhsRecGroup, const StructType& lhs, |
| 352 |
const RecGroup* rhsRecGroup, const StructType& rhs) { |
| 353 |
if (lhs.fields_.length() != rhs.fields_.length()) { |
| 354 |
return false; |
| 355 |
} |
| 356 |
for (uint32_t i = 0; i < lhs.fields_.length(); i++) { |
| 357 |
const FieldType& lhsField = lhs.fields_[i]; |
| 358 |
const FieldType& rhsField = rhs.fields_[i]; |
| 359 |
if (!FieldType::isoEquals(lhsRecGroup, lhsField, rhsRecGroup, rhsField)) { |
| 360 |
return false; |
| 361 |
} |
| 362 |
} |
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
// Checks if two struct types are compatible in a given subtyping |
| 367 |
// relationship. |
| 368 |
static bool canBeSubTypeOf(const StructType& subType, |
| 369 |
const StructType& superType) { |
| 370 |
// A subtype must have at least as many fields as its supertype |
| 371 |
if (subType.fields_.length() < superType.fields_.length()) { |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
// Every field that is in both superType and subType must be compatible |
| 376 |
for (uint32_t i = 0; i < superType.fields_.length(); i++) { |
| 377 |
if (!FieldType::canBeSubTypeOf(subType.fields_[i], |
| 378 |
superType.fields_[i])) { |
| 379 |
return false; |
| 380 |
} |
| 381 |
} |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
static bool createImmutable(const ValTypeVector& types, StructType* struct_); |
| 386 |
|
| 387 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 388 |
WASM_DECLARE_FRIEND_SERIALIZE(StructType); |
| 389 |
}; |
| 390 |
|
| 391 |
using StructTypeVector = Vector<StructType, 0, SystemAllocPolicy>; |
| 392 |
|
| 393 |
// Utility for computing field offset and alignments, and total size for |
| 394 |
// structs and tags. This is complicated by fact that a WasmStructObject has |
| 395 |
// an inline area, which is used first, and if that fills up an optional |
| 396 |
// C++-heap-allocated outline area is used. We need to be careful not to |
| 397 |
// split any data item across the boundary. This is ensured as follows: |
| 398 |
// |
| 399 |
// (1) the possible field sizes are 1, 2, 4, 8 and 16 only. |
| 400 |
// (2) each field is "naturally aligned" -- aligned to its size. |
| 401 |
// (3) MaxInlineBytes (the size of the inline area) % 16 == 0. |
| 402 |
// |
| 403 |
// From (1) and (2), it follows that all fields are placed so that their first |
| 404 |
// and last bytes fall within the same 16-byte chunk. That is, |
| 405 |
// offset_of_first_byte_of_field / 16 == offset_of_last_byte_of_field / 16. |
| 406 |
// |
| 407 |
// Given that, it follows from (3) that all fields fall completely within |
| 408 |
// either the inline or outline areas; no field crosses the boundary. |
| 409 |
class StructLayout { |
| 410 |
mozilla::CheckedInt32 sizeSoFar = 0; |
| 411 |
uint32_t structAlignment = 1; |
| 412 |
|
| 413 |
public: |
| 414 |
// The field adders return the offset of the the field. |
| 415 |
mozilla::CheckedInt32 addField(StorageType type); |
| 416 |
|
| 417 |
// The close method rounds up the structure size to the appropriate |
| 418 |
// alignment and returns that size. |
| 419 |
mozilla::CheckedInt32 close(); |
| 420 |
}; |
| 421 |
|
| 422 |
//========================================================================= |
| 423 |
// Array types |
| 424 |
|
| 425 |
class ArrayType { |
| 426 |
public: |
| 427 |
// The kind of value stored in this array |
| 428 |
FieldType fieldType_; |
| 429 |
|
| 430 |
public: |
| 431 |
ArrayType() = default; |
| 432 |
ArrayType(StorageType elementType, bool isMutable) |
| 433 |
: fieldType_(FieldType(elementType, isMutable)) {} |
| 434 |
|
| 435 |
ArrayType(const ArrayType&) = default; |
| 436 |
ArrayType& operator=(const ArrayType&) = default; |
| 437 |
|
| 438 |
ArrayType(ArrayType&&) = default; |
| 439 |
ArrayType& operator=(ArrayType&&) = default; |
| 440 |
|
| 441 |
StorageType elementType() const { return fieldType_.type; } |
| 442 |
bool isMutable() const { return fieldType_.isMutable; } |
| 443 |
bool isDefaultable() const { return elementType().isDefaultable(); } |
| 444 |
|
| 445 |
HashNumber hash(const RecGroup* recGroup) const { |
| 446 |
return fieldType_.hash(recGroup); |
| 447 |
} |
| 448 |
|
| 449 |
// Compares two array types for isorecursive equality. See |
| 450 |
// "Comparing type definitions" in WasmValType.h for more background. |
| 451 |
static bool isoEquals(const RecGroup* lhsRecGroup, const ArrayType& lhs, |
| 452 |
const RecGroup* rhsRecGroup, const ArrayType& rhs) { |
| 453 |
return FieldType::isoEquals(lhsRecGroup, lhs.fieldType_, rhsRecGroup, |
| 454 |
rhs.fieldType_); |
| 455 |
} |
| 456 |
|
| 457 |
// Checks if two arrays are compatible in a given subtyping relationship. |
| 458 |
static bool canBeSubTypeOf(const ArrayType& subType, |
| 459 |
const ArrayType& superType) { |
| 460 |
return FieldType::canBeSubTypeOf(subType.fieldType_, superType.fieldType_); |
| 461 |
} |
| 462 |
|
| 463 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 464 |
}; |
| 465 |
|
| 466 |
WASM_DECLARE_CACHEABLE_POD(ArrayType); |
| 467 |
|
| 468 |
using ArrayTypeVector = Vector<ArrayType, 0, SystemAllocPolicy>; |
| 469 |
|
| 470 |
//========================================================================= |
| 471 |
// SuperTypeVector |
| 472 |
|
| 473 |
// [SMDOC] Super type vector |
| 474 |
// |
| 475 |
// A super type vector is a vector representation of the linked list of super |
| 476 |
// types that a type definition has. Every element is a raw pointer to another |
| 477 |
// super type vector - they are one-to-one with type definitions, so they are |
| 478 |
// functionally equivalent. It is possible to form a vector here because |
| 479 |
// subtypes in wasm form trees, not DAGs, with every type having at most one |
| 480 |
// super type. |
| 481 |
// |
| 482 |
// The first element in the vector is the 'root' type definition without a |
| 483 |
// super type. The last element is to the type definition itself. |
| 484 |
// |
| 485 |
// ## Subtype checking |
| 486 |
// |
| 487 |
// The only purpose of a super type vector is to support constant time |
| 488 |
// subtyping checks. This is not free, it comes at the cost of worst case N^2 |
| 489 |
// metadata size growth. We limit the max subtyping depth to counter this. |
| 490 |
// |
| 491 |
// To perform a subtype check we rely on the following: |
| 492 |
// (1) a type A is a subtype (<:) of type B iff: |
| 493 |
// type A == type B OR |
| 494 |
// type B is reachable by following declared super types of type A |
| 495 |
// (2) we order super type vectors from least to most derived types |
| 496 |
// (3) the 'subtyping depth' of all type definitions is statically known |
| 497 |
// |
| 498 |
// With the above, we know that if type B is a super type of type A, that it |
| 499 |
// must be in A's super type vector at type B's subtyping depth. We can |
| 500 |
// therefore just do an index and comparison to determine if that's the case. |
| 501 |
// |
| 502 |
// ## Example |
| 503 |
// |
| 504 |
// For the following type section: |
| 505 |
// .. |
| 506 |
// 12: (type (struct)) |
| 507 |
// .. |
| 508 |
// 34: (type (sub 12 (struct))) |
| 509 |
// .. |
| 510 |
// 56: (type (sub 34 (struct))) |
| 511 |
// .. |
| 512 |
// 78: (type (sub 56 (struct))) |
| 513 |
// .. |
| 514 |
// |
| 515 |
// (type 12) would have the following super type vector: |
| 516 |
// [(type 12)] |
| 517 |
// |
| 518 |
// (type 78) would have the following super type vector: |
| 519 |
// [(type 12), (type 34), (type 56), (type 78)] |
| 520 |
// |
| 521 |
// Checking that (type 78) <: (type 12) can use the fact that (type 12) will |
| 522 |
// always be present at depth 0 of any super type vector it is in, and |
| 523 |
// therefore check the vector at that index. |
| 524 |
// |
| 525 |
// ## Minimum sizing |
| 526 |
// |
| 527 |
// As a further optimization to avoid bounds checking, we guarantee that all |
| 528 |
// super type vectors are at least `MinSuperTypeVectorLength`. All checks |
| 529 |
// against indices that we know statically are at/below that can skip bounds |
| 530 |
// checking. Extra entries added to reach the minimum size are initialized to |
| 531 |
// null. |
| 532 |
class SuperTypeVector { |
| 533 |
SuperTypeVector() : typeDef_(nullptr), length_(0) {} |
| 534 |
|
| 535 |
// The TypeDef for which this is the supertype vector. That TypeDef should |
| 536 |
// point back to this SuperTypeVector. |
| 537 |
const TypeDef* typeDef_; |
| 538 |
|
| 539 |
// A cached copy of subTypingDepth from TypeDef. |
| 540 |
uint32_t subTypingDepth_; |
| 541 |
|
| 542 |
// The length of types stored inline below. |
| 543 |
uint32_t length_; |
| 544 |
|
| 545 |
public: |
| 546 |
// Raw pointers to the super types of this type definition. Ordered from |
| 547 |
// least-derived to most-derived. Do not add any fields after this point. |
| 548 |
const SuperTypeVector* types_[0]; |
| 549 |
|
| 550 |
// Batch allocate super type vectors for all the types in a recursion group. |
| 551 |
// Returns a pointer to the first super type vector, which can be used to |
| 552 |
// free all vectors. |
| 553 |
[[nodiscard]] static const SuperTypeVector* createMultipleForRecGroup( |
| 554 |
RecGroup* recGroup); |
| 555 |
|
| 556 |
const TypeDef* typeDef() const { return typeDef_; } |
| 557 |
|
| 558 |
uint32_t length() const { return length_; } |
| 559 |
|
| 560 |
const SuperTypeVector* type(size_t index) const { |
| 561 |
MOZ_ASSERT(index < length_); |
| 562 |
return types_[index]; |
| 563 |
} |
| 564 |
|
| 565 |
// The length of a super type vector for a specific type def. |
| 566 |
static size_t lengthForTypeDef(const TypeDef& typeDef); |
| 567 |
// The byte size of a super type vector for a specific type def. |
| 568 |
static size_t byteSizeForTypeDef(const TypeDef& typeDef); |
| 569 |
|
| 570 |
static size_t offsetOfSubTypingDepth() { |
| 571 |
return offsetof(SuperTypeVector, subTypingDepth_); |
| 572 |
} |
| 573 |
static size_t offsetOfLength() { return offsetof(SuperTypeVector, length_); } |
| 574 |
static size_t offsetOfSelfTypeDef() { |
| 575 |
return offsetof(SuperTypeVector, typeDef_); |
| 576 |
}; |
| 577 |
static size_t offsetOfSTVInVector(uint32_t subTypingDepth); |
| 578 |
}; |
| 579 |
|
| 580 |
// Ensure it is safe to use `sizeof(SuperTypeVector)` to find the offset of |
| 581 |
// `types_[0]`. |
| 582 |
static_assert(offsetof(SuperTypeVector, types_) == sizeof(SuperTypeVector)); |
| 583 |
|
| 584 |
//========================================================================= |
| 585 |
// TypeDef and supporting types |
| 586 |
|
| 587 |
// A tagged container for the various types that can be present in a wasm |
| 588 |
// module's type section. |
| 589 |
|
| 590 |
enum class TypeDefKind : uint8_t { |
| 591 |
None = 0, |
| 592 |
Func, |
| 593 |
Struct, |
| 594 |
Array, |
| 595 |
}; |
| 596 |
|
| 597 |
class TypeDef { |
| 598 |
uint32_t offsetToRecGroup_; |
| 599 |
|
| 600 |
// The supertype vector for this TypeDef. That SuperTypeVector should point |
| 601 |
// back to this TypeDef. |
| 602 |
const SuperTypeVector* superTypeVector_; |
| 603 |
|
| 604 |
const TypeDef* superTypeDef_; |
| 605 |
uint16_t subTypingDepth_; |
| 606 |
bool isFinal_; |
| 607 |
TypeDefKind kind_; |
| 608 |
union { |
| 609 |
FuncType funcType_; |
| 610 |
StructType structType_; |
| 611 |
ArrayType arrayType_; |
| 612 |
}; |
| 613 |
|
| 614 |
void setRecGroup(RecGroup* recGroup) { |
| 615 |
uintptr_t recGroupAddr = (uintptr_t)recGroup; |
| 616 |
uintptr_t typeDefAddr = (uintptr_t)this; |
| 617 |
MOZ_ASSERT(typeDefAddr > recGroupAddr); |
| 618 |
MOZ_ASSERT(typeDefAddr - recGroupAddr <= UINT32_MAX); |
| 619 |
offsetToRecGroup_ = typeDefAddr - recGroupAddr; |
| 620 |
} |
| 621 |
|
| 622 |
public: |
| 623 |
explicit TypeDef(RecGroup* recGroup) |
| 624 |
: offsetToRecGroup_(0), |
| 625 |
superTypeVector_(nullptr), |
| 626 |
superTypeDef_(nullptr), |
| 627 |
subTypingDepth_(0), |
| 628 |
isFinal_(true), |
| 629 |
kind_(TypeDefKind::None) { |
| 630 |
setRecGroup(recGroup); |
| 631 |
} |
| 632 |
|
| 633 |
~TypeDef() { |
| 634 |
switch (kind_) { |
| 635 |
case TypeDefKind::Func: |
| 636 |
funcType_.~FuncType(); |
| 637 |
break; |
| 638 |
case TypeDefKind::Struct: |
| 639 |
structType_.~StructType(); |
| 640 |
break; |
| 641 |
case TypeDefKind::Array: |
| 642 |
arrayType_.~ArrayType(); |
| 643 |
break; |
| 644 |
case TypeDefKind::None: |
| 645 |
break; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
TypeDef& operator=(FuncType&& that) noexcept { |
| 650 |
MOZ_ASSERT(isNone()); |
| 651 |
kind_ = TypeDefKind::Func; |
| 652 |
new (&funcType_) FuncType(std::move(that)); |
| 653 |
return *this; |
| 654 |
} |
| 655 |
|
| 656 |
TypeDef& operator=(StructType&& that) noexcept { |
| 657 |
MOZ_ASSERT(isNone()); |
| 658 |
kind_ = TypeDefKind::Struct; |
| 659 |
new (&structType_) StructType(std::move(that)); |
| 660 |
return *this; |
| 661 |
} |
| 662 |
|
| 663 |
TypeDef& operator=(ArrayType&& that) noexcept { |
| 664 |
MOZ_ASSERT(isNone()); |
| 665 |
kind_ = TypeDefKind::Array; |
| 666 |
new (&arrayType_) ArrayType(std::move(that)); |
| 667 |
return *this; |
| 668 |
} |
| 669 |
|
| 670 |
const SuperTypeVector* superTypeVector() const { return superTypeVector_; } |
| 671 |
|
| 672 |
void setSuperTypeVector(const SuperTypeVector* superTypeVector) { |
| 673 |
superTypeVector_ = superTypeVector; |
| 674 |
} |
| 675 |
|
| 676 |
static size_t offsetOfKind() { return offsetof(TypeDef, kind_); } |
| 677 |
|
| 678 |
static size_t offsetOfSuperTypeVector() { |
| 679 |
return offsetof(TypeDef, superTypeVector_); |
| 680 |
} |
| 681 |
|
| 682 |
static size_t offsetOfSubTypingDepth() { |
| 683 |
return offsetof(TypeDef, subTypingDepth_); |
| 684 |
} |
| 685 |
|
| 686 |
const TypeDef* superTypeDef() const { return superTypeDef_; } |
| 687 |
|
| 688 |
bool isFinal() const { return isFinal_; } |
| 689 |
|
| 690 |
uint16_t subTypingDepth() const { return subTypingDepth_; } |
| 691 |
|
| 692 |
const RecGroup& recGroup() const { |
| 693 |
uintptr_t typeDefAddr = (uintptr_t)this; |
| 694 |
uintptr_t recGroupAddr = typeDefAddr - offsetToRecGroup_; |
| 695 |
return *(const RecGroup*)recGroupAddr; |
| 696 |
} |
| 697 |
|
| 698 |
TypeDefKind kind() const { return kind_; } |
| 699 |
|
| 700 |
bool isNone() const { return kind_ == TypeDefKind::None; } |
| 701 |
|
| 702 |
bool isFuncType() const { return kind_ == TypeDefKind::Func; } |
| 703 |
|
| 704 |
bool isStructType() const { return kind_ == TypeDefKind::Struct; } |
| 705 |
|
| 706 |
bool isArrayType() const { return kind_ == TypeDefKind::Array; } |
| 707 |
|
| 708 |
bool isGcType() const { return isStructType() || isArrayType(); } |
| 709 |
|
| 710 |
const FuncType& funcType() const { |
| 711 |
MOZ_ASSERT(isFuncType()); |
| 712 |
return funcType_; |
| 713 |
} |
| 714 |
|
| 715 |
FuncType& funcType() { |
| 716 |
MOZ_ASSERT(isFuncType()); |
| 717 |
return funcType_; |
| 718 |
} |
| 719 |
|
| 720 |
const StructType& structType() const { |
| 721 |
MOZ_ASSERT(isStructType()); |
| 722 |
return structType_; |
| 723 |
} |
| 724 |
|
| 725 |
StructType& structType() { |
| 726 |
MOZ_ASSERT(isStructType()); |
| 727 |
return structType_; |
| 728 |
} |
| 729 |
|
| 730 |
const ArrayType& arrayType() const { |
| 731 |
MOZ_ASSERT(isArrayType()); |
| 732 |
return arrayType_; |
| 733 |
} |
| 734 |
|
| 735 |
ArrayType& arrayType() { |
| 736 |
MOZ_ASSERT(isArrayType()); |
| 737 |
return arrayType_; |
| 738 |
} |
| 739 |
|
| 740 |
// Get a value that can be used for comparing type definitions across |
| 741 |
// different recursion groups. |
| 742 |
static inline uintptr_t forIsoEquals(const TypeDef* typeDef, |
| 743 |
const RecGroup* recGroup); |
| 744 |
|
| 745 |
HashNumber hash() const { |
| 746 |
HashNumber hn = HashNumber(kind_); |
| 747 |
hn = mozilla::AddToHash(hn, |
| 748 |
TypeDef::forIsoEquals(superTypeDef_, &recGroup())); |
| 749 |
hn = mozilla::AddToHash(hn, isFinal_); |
| 750 |
switch (kind_) { |
| 751 |
case TypeDefKind::Func: |
| 752 |
hn = mozilla::AddToHash(hn, funcType_.hash(&recGroup())); |
| 753 |
break; |
| 754 |
case TypeDefKind::Struct: |
| 755 |
hn = mozilla::AddToHash(hn, structType_.hash(&recGroup())); |
| 756 |
break; |
| 757 |
case TypeDefKind::Array: |
| 758 |
hn = mozilla::AddToHash(hn, arrayType_.hash(&recGroup())); |
| 759 |
break; |
| 760 |
case TypeDefKind::None: |
| 761 |
break; |
| 762 |
} |
| 763 |
return hn; |
| 764 |
} |
| 765 |
|
| 766 |
// Compares two type definitions for isorecursive equality. See |
| 767 |
// "Comparing type definitions" in WasmValType.h for more background. |
| 768 |
static bool isoEquals(const TypeDef& lhs, const TypeDef& rhs) { |
| 769 |
if (lhs.kind_ != rhs.kind_) { |
| 770 |
return false; |
| 771 |
} |
| 772 |
if (lhs.isFinal_ != rhs.isFinal_) { |
| 773 |
return false; |
| 774 |
} |
| 775 |
if (TypeDef::forIsoEquals(lhs.superTypeDef_, &lhs.recGroup()) != |
| 776 |
TypeDef::forIsoEquals(rhs.superTypeDef_, &rhs.recGroup())) { |
| 777 |
return false; |
| 778 |
} |
| 779 |
switch (lhs.kind_) { |
| 780 |
case TypeDefKind::Func: |
| 781 |
return FuncType::isoEquals(&lhs.recGroup(), lhs.funcType_, |
| 782 |
&rhs.recGroup(), rhs.funcType_); |
| 783 |
case TypeDefKind::Struct: |
| 784 |
return StructType::isoEquals(&lhs.recGroup(), lhs.structType_, |
| 785 |
&rhs.recGroup(), rhs.structType_); |
| 786 |
case TypeDefKind::Array: |
| 787 |
return ArrayType::isoEquals(&lhs.recGroup(), lhs.arrayType_, |
| 788 |
&rhs.recGroup(), rhs.arrayType_); |
| 789 |
case TypeDefKind::None: |
| 790 |
MOZ_CRASH("can't match TypeDefKind::None"); |
| 791 |
} |
| 792 |
return false; |
| 793 |
} |
| 794 |
|
| 795 |
// Checks if two type definitions are compatible in a given subtyping |
| 796 |
// relationship. |
| 797 |
static bool canBeSubTypeOf(const TypeDef* subType, const TypeDef* superType) { |
| 798 |
if (subType->kind() != superType->kind()) { |
| 799 |
return false; |
| 800 |
} |
| 801 |
|
| 802 |
// A subtype can't declare a final super type. |
| 803 |
if (superType->isFinal()) { |
| 804 |
return false; |
| 805 |
} |
| 806 |
|
| 807 |
switch (subType->kind_) { |
| 808 |
case TypeDefKind::Func: |
| 809 |
return FuncType::canBeSubTypeOf(subType->funcType_, |
| 810 |
superType->funcType_); |
| 811 |
case TypeDefKind::Struct: |
| 812 |
return StructType::canBeSubTypeOf(subType->structType_, |
| 813 |
superType->structType_); |
| 814 |
case TypeDefKind::Array: |
| 815 |
return ArrayType::canBeSubTypeOf(subType->arrayType_, |
| 816 |
superType->arrayType_); |
| 817 |
case TypeDefKind::None: |
| 818 |
MOZ_CRASH(); |
| 819 |
} |
| 820 |
return false; |
| 821 |
} |
| 822 |
|
| 823 |
void setSuperTypeDef(const TypeDef* superTypeDef) { |
| 824 |
superTypeDef_ = superTypeDef; |
| 825 |
subTypingDepth_ = superTypeDef_->subTypingDepth_ + 1; |
| 826 |
} |
| 827 |
|
| 828 |
void setFinal(const bool value) { isFinal_ = value; } |
| 829 |
|
| 830 |
// Checks if `subTypeDef` is a declared sub type of `superTypeDef`. |
| 831 |
static bool isSubTypeOf(const TypeDef* subTypeDef, |
| 832 |
const TypeDef* superTypeDef) { |
| 833 |
// Fast path for when the types are equal |
| 834 |
if (MOZ_LIKELY(subTypeDef == superTypeDef)) { |
| 835 |
return true; |
| 836 |
} |
| 837 |
const SuperTypeVector* subSTV = subTypeDef->superTypeVector(); |
| 838 |
const SuperTypeVector* superSTV = superTypeDef->superTypeVector(); |
| 839 |
|
| 840 |
// During construction of a recursion group, the super type vectors may not |
| 841 |
// have been computed yet, in which case we need to fall back to a linear |
| 842 |
// search. |
| 843 |
if (!subSTV || !superSTV) { |
| 844 |
while (subTypeDef) { |
| 845 |
if (subTypeDef == superTypeDef) { |
| 846 |
return true; |
| 847 |
} |
| 848 |
subTypeDef = subTypeDef->superTypeDef(); |
| 849 |
} |
| 850 |
return false; |
| 851 |
} |
| 852 |
|
| 853 |
// The supertype vectors do exist. Check that they point to the right |
| 854 |
// places. |
| 855 |
MOZ_ASSERT(subSTV); |
| 856 |
MOZ_ASSERT(superSTV); |
| 857 |
MOZ_ASSERT(subSTV->typeDef() == subTypeDef); |
| 858 |
MOZ_ASSERT(superSTV->typeDef() == superTypeDef); |
| 859 |
|
| 860 |
// We need to check if `superTypeDef` is one of `subTypeDef`s super types |
| 861 |
// by checking in `subTypeDef`s super type vector. We can use the static |
| 862 |
// information of the depth of `superTypeDef` to index directly into the |
| 863 |
// vector. |
| 864 |
uint32_t subTypingDepth = superTypeDef->subTypingDepth(); |
| 865 |
if (subTypingDepth >= subSTV->length()) { |
| 866 |
return false; |
| 867 |
} |
| 868 |
|
| 869 |
return subSTV->type(subTypingDepth) == superSTV; |
| 870 |
} |
| 871 |
|
| 872 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 873 |
WASM_DECLARE_FRIEND_SERIALIZE(TypeDef); |
| 874 |
|
| 875 |
inline void AddRef() const; |
| 876 |
inline void Release() const; |
| 877 |
}; |
| 878 |
|
| 879 |
using SharedTypeDef = RefPtr<const TypeDef>; |
| 880 |
using MutableTypeDef = RefPtr<TypeDef>; |
| 881 |
|
| 882 |
using TypeDefVector = Vector<TypeDef, 0, SystemAllocPolicy>; |
| 883 |
using TypeDefPtrVector = Vector<const TypeDef*, 0, SystemAllocPolicy>; |
| 884 |
|
| 885 |
using TypeDefPtrToIndexMap = |
| 886 |
HashMap<const TypeDef*, uint32_t, PointerHasher<const TypeDef*>, |
| 887 |
SystemAllocPolicy>; |
| 888 |
|
| 889 |
//========================================================================= |
| 890 |
// RecGroup |
| 891 |
|
| 892 |
// A recursion group is a set of type definitions that may refer to each other |
| 893 |
// or to type definitions in another recursion group. There is an ordering |
| 894 |
// restriction on type references such that references across recursion groups |
| 895 |
// must be acyclic. |
| 896 |
// |
| 897 |
// Type definitions are stored inline in their containing recursion group, and |
| 898 |
// have an offset to their containing recursion group. Recursion groups are |
| 899 |
// atomically refcounted and hold strong references to other recursion groups |
| 900 |
// they depend on. |
| 901 |
// |
| 902 |
// Type equality is structural in WebAssembly, and we canonicalize recursion |
| 903 |
// groups while building them so that pointer equality of types implies |
| 904 |
// equality of types. There is a global hash set of weak pointers to recursion |
| 905 |
// groups that holds the current canonical instance of a recursion group. |
| 906 |
class RecGroup : public AtomicRefCounted<RecGroup> { |
| 907 |
// Whether this recursion group has been finished and acquired strong |
| 908 |
// references to external recursion groups. |
| 909 |
bool finalizedTypes_; |
| 910 |
// The number of types stored in this recursion group. |
| 911 |
uint32_t numTypes_; |
| 912 |
// The batch allocated super type vectors for all type definitions in this |
| 913 |
// recursion group. |
| 914 |
const SuperTypeVector* vectors_; |
| 915 |
// The first type definition stored inline in this recursion group. |
| 916 |
TypeDef types_[0]; |
| 917 |
|
| 918 |
friend class TypeContext; |
| 919 |
|
| 920 |
explicit RecGroup(uint32_t numTypes) |
| 921 |
: finalizedTypes_(false), numTypes_(numTypes), vectors_(nullptr) {} |
| 922 |
|
| 923 |
// Compute the size in bytes of a recursion group with the specified amount |
| 924 |
// of types. |
| 925 |
static constexpr size_t sizeOfRecGroup(uint32_t numTypes) { |
| 926 |
// TypeDef can find containing RecGroup using only a 32-bit offset |
| 927 |
static_assert(offsetof(RecGroup, types_) + sizeof(TypeDef) * MaxTypes <= |
| 928 |
UINT32_MAX); |
| 929 |
// We will not overflow in our calculation here |
| 930 |
static_assert(MaxTypes <= SIZE_MAX / sizeof(TypeDef)); |
| 931 |
return sizeof(RecGroup) + sizeof(TypeDef) * numTypes; |
| 932 |
} |
| 933 |
|
| 934 |
// Allocate a recursion group with the specified amount of types. The type |
| 935 |
// definitions will be ready to be filled in. Users must call `finish` once |
| 936 |
// type definitions are initialized so that strong references to external |
| 937 |
// recursion groups are taken. |
| 938 |
static RefPtr<RecGroup> allocate(uint32_t numTypes) { |
| 939 |
// Validation must ensure this |
| 940 |
MOZ_RELEASE_ASSERT(numTypes <= MaxTypes); |
| 941 |
|
| 942 |
// Allocate the recursion group with the correct size |
| 943 |
RecGroup* recGroup = (RecGroup*)js_malloc(sizeOfRecGroup(numTypes)); |
| 944 |
if (!recGroup) { |
| 945 |
return nullptr; |
| 946 |
} |
| 947 |
|
| 948 |
// Construct the recursion group and types that are stored inline |
| 949 |
new (recGroup) RecGroup(numTypes); |
| 950 |
for (uint32_t i = 0; i < numTypes; i++) { |
| 951 |
new (recGroup->types_ + i) TypeDef(recGroup); |
| 952 |
} |
| 953 |
return recGroup; |
| 954 |
} |
| 955 |
|
| 956 |
// Finish initialization by acquiring strong references to groups referenced |
| 957 |
// by type definitions. |
| 958 |
[[nodiscard]] bool finalizeDefinitions() { |
| 959 |
MOZ_ASSERT(!finalizedTypes_); |
| 960 |
// Super type vectors are only needed for GC and have a size/time impact |
| 961 |
// that we don't want to encur until we're ready for it. Only use them when |
| 962 |
// GC is built into the binary. |
| 963 |
vectors_ = SuperTypeVector::createMultipleForRecGroup(this); |
| 964 |
if (!vectors_) { |
| 965 |
return false; |
| 966 |
} |
| 967 |
visitReferencedGroups([](const RecGroup* recGroup) { recGroup->AddRef(); }); |
| 968 |
finalizedTypes_ = true; |
| 969 |
return true; |
| 970 |
} |
| 971 |
|
| 972 |
// Visit every external recursion group that is referenced by the types in |
| 973 |
// this recursion group. |
| 974 |
template <typename Visitor> |
| 975 |
void visitReferencedGroups(Visitor visitor) const { |
| 976 |
auto visitValType = [this, visitor](ValType type) { |
| 977 |
if (type.isTypeRef() && &type.typeDef()->recGroup() != this) { |
| 978 |
visitor(&type.typeDef()->recGroup()); |
| 979 |
} |
| 980 |
}; |
| 981 |
auto visitStorageType = [this, visitor](StorageType type) { |
| 982 |
if (type.isTypeRef() && &type.typeDef()->recGroup() != this) { |
| 983 |
visitor(&type.typeDef()->recGroup()); |
| 984 |
} |
| 985 |
}; |
| 986 |
|
| 987 |
for (uint32_t i = 0; i < numTypes_; i++) { |
| 988 |
const TypeDef& typeDef = types_[i]; |
| 989 |
|
| 990 |
if (typeDef.superTypeDef() && |
| 991 |
&typeDef.superTypeDef()->recGroup() != this) { |
| 992 |
visitor(&typeDef.superTypeDef()->recGroup()); |
| 993 |
} |
| 994 |
|
| 995 |
switch (typeDef.kind()) { |
| 996 |
case TypeDefKind::Func: { |
| 997 |
const FuncType& funcType = typeDef.funcType(); |
| 998 |
MOZ_RELEASE_ASSERT(funcType.args().length() <= MaxParams); |
| 999 |
MOZ_RELEASE_ASSERT(funcType.results().length() <= MaxResults); |
| 1000 |
for (auto type : funcType.args()) { |
| 1001 |
visitValType(type); |
| 1002 |
} |
| 1003 |
for (auto type : funcType.results()) { |
| 1004 |
visitValType(type); |
| 1005 |
} |
| 1006 |
break; |
| 1007 |
} |
| 1008 |
case TypeDefKind::Struct: { |
| 1009 |
const StructType& structType = typeDef.structType(); |
| 1010 |
MOZ_RELEASE_ASSERT(structType.fields_.length() <= MaxStructFields); |
| 1011 |
for (const auto& field : structType.fields_) { |
| 1012 |
visitStorageType(field.type); |
| 1013 |
} |
| 1014 |
break; |
| 1015 |
} |
| 1016 |
case TypeDefKind::Array: { |
| 1017 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 1018 |
visitStorageType(arrayType.elementType()); |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
case TypeDefKind::None: { |
| 1022 |
MOZ_CRASH(); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
public: |
| 1029 |
~RecGroup() { |
| 1030 |
// Release the referenced recursion groups if we acquired references to |
| 1031 |
// them. Do this before the type definitions are destroyed below. |
| 1032 |
if (finalizedTypes_) { |
| 1033 |
finalizedTypes_ = false; |
| 1034 |
visitReferencedGroups( |
| 1035 |
[](const RecGroup* recGroup) { recGroup->Release(); }); |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (vectors_) { |
| 1039 |
js_free((void*)vectors_); |
| 1040 |
vectors_ = nullptr; |
| 1041 |
} |
| 1042 |
|
| 1043 |
// Call destructors on all the type definitions. |
| 1044 |
for (uint32_t i = 0; i < numTypes_; i++) { |
| 1045 |
type(i).~TypeDef(); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
// Recursion groups cannot be copied or moved |
| 1050 |
RecGroup& operator=(const RecGroup&) = delete; |
| 1051 |
RecGroup& operator=(RecGroup&&) = delete; |
| 1052 |
|
| 1053 |
// Get the type definition at the group type index (not module type index). |
| 1054 |
TypeDef& type(uint32_t groupTypeIndex) { |
| 1055 |
// We cannot mutate type definitions after we've finalized them |
| 1056 |
MOZ_ASSERT(!finalizedTypes_); |
| 1057 |
return types_[groupTypeIndex]; |
| 1058 |
} |
| 1059 |
const TypeDef& type(uint32_t groupTypeIndex) const { |
| 1060 |
return types_[groupTypeIndex]; |
| 1061 |
} |
| 1062 |
|
| 1063 |
// The number of types stored in this recursion group. |
| 1064 |
uint32_t numTypes() const { return numTypes_; } |
| 1065 |
|
| 1066 |
// Get the index of a type definition that's in this recursion group. |
| 1067 |
uint32_t indexOf(const TypeDef* typeDef) const { |
| 1068 |
MOZ_ASSERT(typeDef >= types_); |
| 1069 |
size_t groupTypeIndex = (size_t)(typeDef - types_); |
| 1070 |
MOZ_ASSERT(groupTypeIndex < numTypes()); |
| 1071 |
return (uint32_t)groupTypeIndex; |
| 1072 |
} |
| 1073 |
|
| 1074 |
bool hasGcType() const { |
| 1075 |
for (uint32_t groupTypeIndex = 0; groupTypeIndex < numTypes(); |
| 1076 |
groupTypeIndex++) { |
| 1077 |
if (type(groupTypeIndex).isGcType()) { |
| 1078 |
return true; |
| 1079 |
} |
| 1080 |
} |
| 1081 |
return false; |
| 1082 |
} |
| 1083 |
|
| 1084 |
HashNumber hash() const { |
| 1085 |
HashNumber hn = 0; |
| 1086 |
for (uint32_t i = 0; i < numTypes(); i++) { |
| 1087 |
hn = mozilla::AddToHash(hn, types_[i].hash()); |
| 1088 |
} |
| 1089 |
return hn; |
| 1090 |
} |
| 1091 |
|
| 1092 |
// Compares two recursion groups for isorecursive equality. See |
| 1093 |
// "Comparing type definitions" in WasmValType.h for more background. |
| 1094 |
static bool isoEquals(const RecGroup& lhs, const RecGroup& rhs) { |
| 1095 |
if (lhs.numTypes() != rhs.numTypes()) { |
| 1096 |
return false; |
| 1097 |
} |
| 1098 |
for (uint32_t i = 0; i < lhs.numTypes(); i++) { |
| 1099 |
if (!TypeDef::isoEquals(lhs.type(i), rhs.type(i))) { |
| 1100 |
return false; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
return true; |
| 1104 |
} |
| 1105 |
}; |
| 1106 |
|
| 1107 |
// Remove all types from the canonical type set that are not referenced from |
| 1108 |
// outside the type set. |
| 1109 |
extern void PurgeCanonicalTypes(); |
| 1110 |
|
| 1111 |
using SharedRecGroup = RefPtr<const RecGroup>; |
| 1112 |
using MutableRecGroup = RefPtr<RecGroup>; |
| 1113 |
using SharedRecGroupVector = Vector<SharedRecGroup, 0, SystemAllocPolicy>; |
| 1114 |
|
| 1115 |
//========================================================================= |
| 1116 |
// TypeContext |
| 1117 |
|
| 1118 |
// A type context holds the recursion groups and corresponding type definitions |
| 1119 |
// defined in a module. |
| 1120 |
class TypeContext : public AtomicRefCounted<TypeContext> { |
| 1121 |
// The pending recursion group that is currently being constructed |
| 1122 |
MutableRecGroup pendingRecGroup_; |
| 1123 |
// An in-order list of all the recursion groups defined in this module |
| 1124 |
SharedRecGroupVector recGroups_; |
| 1125 |
// An in-order list of the type definitions in the module. Each type is |
| 1126 |
// stored in a recursion group. |
| 1127 |
TypeDefPtrVector types_; |
| 1128 |
// A map from type definition to the original module index. |
| 1129 |
TypeDefPtrToIndexMap moduleIndices_; |
| 1130 |
|
| 1131 |
static SharedRecGroup canonicalizeGroup(SharedRecGroup recGroup); |
| 1132 |
|
| 1133 |
public: |
| 1134 |
TypeContext() = default; |
| 1135 |
~TypeContext(); |
| 1136 |
|
| 1137 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const { |
| 1138 |
return types_.sizeOfExcludingThis(mallocSizeOf) + |
| 1139 |
moduleIndices_.shallowSizeOfExcludingThis(mallocSizeOf); |
| 1140 |
} |
| 1141 |
|
| 1142 |
// Disallow copy, allow move initialization |
| 1143 |
TypeContext(const TypeContext&) = delete; |
| 1144 |
TypeContext& operator=(const TypeContext&) = delete; |
| 1145 |
TypeContext(TypeContext&&) = delete; |
| 1146 |
TypeContext& operator=(TypeContext&&) = delete; |
| 1147 |
|
| 1148 |
// Begin creating a recursion group with the specified number of types. |
| 1149 |
// Returns a recursion group to be filled in with type definitions. This must |
| 1150 |
// be paired with `endGroup`. |
| 1151 |
[[nodiscard]] MutableRecGroup startRecGroup(uint32_t numTypes) { |
| 1152 |
// We must not have a pending group |
| 1153 |
MOZ_ASSERT(!pendingRecGroup_); |
| 1154 |
|
| 1155 |
// Create the group and add it to the list of groups |
| 1156 |
MutableRecGroup recGroup = RecGroup::allocate(numTypes); |
| 1157 |
if (!recGroup || !addRecGroup(recGroup)) { |
| 1158 |
return nullptr; |
| 1159 |
} |
| 1160 |
|
| 1161 |
// Store this group for later use in endRecGroup |
| 1162 |
pendingRecGroup_ = recGroup; |
| 1163 |
return recGroup; |
| 1164 |
} |
| 1165 |
|
| 1166 |
// Finish creation of a recursion group after type definitions have been |
| 1167 |
// initialized. This must be paired with `startGroup`. |
| 1168 |
[[nodiscard]] bool endRecGroup() { |
| 1169 |
// We must have started a recursion group |
| 1170 |
MOZ_ASSERT(pendingRecGroup_); |
| 1171 |
MutableRecGroup recGroup = pendingRecGroup_; |
| 1172 |
pendingRecGroup_ = nullptr; |
| 1173 |
|
| 1174 |
// Finalize the type definitions in the recursion group |
| 1175 |
if (!recGroup->finalizeDefinitions()) { |
| 1176 |
return false; |
| 1177 |
} |
| 1178 |
|
| 1179 |
// Canonicalize the recursion group |
| 1180 |
SharedRecGroup canonicalRecGroup = canonicalizeGroup(recGroup); |
| 1181 |
if (!canonicalRecGroup) { |
| 1182 |
return false; |
| 1183 |
} |
| 1184 |
|
| 1185 |
// Nothing left to do if this group became the canonical group |
| 1186 |
if (canonicalRecGroup == recGroup) { |
| 1187 |
return true; |
| 1188 |
} |
| 1189 |
|
| 1190 |
// Store the canonical group into the list |
| 1191 |
recGroups_.back() = canonicalRecGroup; |
| 1192 |
|
| 1193 |
// Overwrite all the entries we stored into the index space maps when we |
| 1194 |
// started this group. |
| 1195 |
MOZ_ASSERT(recGroup->numTypes() == canonicalRecGroup->numTypes()); |
| 1196 |
for (uint32_t groupTypeIndex = 0; groupTypeIndex < recGroup->numTypes(); |
| 1197 |
groupTypeIndex++) { |
| 1198 |
uint32_t typeIndex = length() - recGroup->numTypes() + groupTypeIndex; |
| 1199 |
const TypeDef* oldTypeDef = types_[typeIndex]; |
| 1200 |
const TypeDef* canonTypeDef = &canonicalRecGroup->type(groupTypeIndex); |
| 1201 |
|
| 1202 |
types_[typeIndex] = canonTypeDef; |
| 1203 |
moduleIndices_.remove(oldTypeDef); |
| 1204 |
|
| 1205 |
// Ensure there is an module index entry pointing to the canonical type |
| 1206 |
// definition. Don't overwrite it if it already exists, serialization |
| 1207 |
// relies on the module index map pointing to the first occurrence of a |
| 1208 |
// type definition to avoid creating forward references that didn't exist |
| 1209 |
// in the original module. |
| 1210 |
auto canonTypeIndexEntry = moduleIndices_.lookupForAdd(canonTypeDef); |
| 1211 |
if (!canonTypeIndexEntry && |
| 1212 |
!moduleIndices_.add(canonTypeIndexEntry, canonTypeDef, typeIndex)) { |
| 1213 |
return false; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
return true; |
| 1218 |
} |
| 1219 |
|
| 1220 |
// Add a pre-existing recursion group to this type context. |
| 1221 |
[[nodiscard]] bool addRecGroup(SharedRecGroup recGroup) { |
| 1222 |
// We must not have a pending group |
| 1223 |
MOZ_ASSERT(!pendingRecGroup_); |
| 1224 |
|
| 1225 |
// Add it to the list of groups |
| 1226 |
if (!recGroups_.append(recGroup)) { |
| 1227 |
return false; |
| 1228 |
} |
| 1229 |
|
| 1230 |
// Store the types of the group into our index space maps. These may get |
| 1231 |
// overwritten if this group is being added by `startRecGroup` and we |
| 1232 |
// overwrite it with a canonical group in `endRecGroup`. We need to do |
| 1233 |
// this before finishing though, because these entries will be used by |
| 1234 |
// decoding and error printing. |
| 1235 |
for (uint32_t groupTypeIndex = 0; groupTypeIndex < recGroup->numTypes(); |
| 1236 |
groupTypeIndex++) { |
| 1237 |
const TypeDef* typeDef = &recGroup->type(groupTypeIndex); |
| 1238 |
uint32_t typeIndex = types_.length(); |
| 1239 |
if (!types_.append(typeDef) || !moduleIndices_.put(typeDef, typeIndex)) { |
| 1240 |
return false; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
return true; |
| 1244 |
} |
| 1245 |
|
| 1246 |
template <typename T> |
| 1247 |
[[nodiscard]] const TypeDef* addType(T&& type) { |
| 1248 |
MutableRecGroup recGroup = startRecGroup(1); |
| 1249 |
if (!recGroup) { |
| 1250 |
return nullptr; |
| 1251 |
} |
| 1252 |
recGroup->type(0) = std::move(type); |
| 1253 |
if (!endRecGroup()) { |
| 1254 |
return nullptr; |
| 1255 |
} |
| 1256 |
return &this->type(length() - 1); |
| 1257 |
} |
| 1258 |
|
| 1259 |
const TypeDef& type(uint32_t index) const { return *types_[index]; } |
| 1260 |
const TypeDef& operator[](uint32_t index) const { return *types_[index]; } |
| 1261 |
|
| 1262 |
bool empty() const { return types_.empty(); } |
| 1263 |
uint32_t length() const { return types_.length(); } |
| 1264 |
|
| 1265 |
const SharedRecGroupVector& groups() const { return recGroups_; } |
| 1266 |
|
| 1267 |
bool hasGcType() const { |
| 1268 |
for (const SharedRecGroup& recGroup : groups()) { |
| 1269 |
if (recGroup->hasGcType()) { |
| 1270 |
return true; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
return false; |
| 1274 |
} |
| 1275 |
|
| 1276 |
// Map from type definition to index |
| 1277 |
|
| 1278 |
uint32_t indexOf(const TypeDef& typeDef) const { |
| 1279 |
auto moduleIndex = moduleIndices_.readonlyThreadsafeLookup(&typeDef); |
| 1280 |
MOZ_RELEASE_ASSERT(moduleIndex.found()); |
| 1281 |
return moduleIndex->value(); |
| 1282 |
} |
| 1283 |
}; |
| 1284 |
|
| 1285 |
using SharedTypeContext = RefPtr<const TypeContext>; |
| 1286 |
using MutableTypeContext = RefPtr<TypeContext>; |
| 1287 |
|
| 1288 |
//========================================================================= |
| 1289 |
// misc |
| 1290 |
|
| 1291 |
/* static */ |
| 1292 |
inline uintptr_t TypeDef::forIsoEquals(const TypeDef* typeDef, |
| 1293 |
const RecGroup* recGroup) { |
| 1294 |
// TypeDef is aligned sufficiently to allow a tag to distinguish a local type |
| 1295 |
// reference (index) from a non-local type reference (pointer). |
| 1296 |
static_assert(alignof(TypeDef) > 1); |
| 1297 |
MOZ_ASSERT((uintptr_t(typeDef) & 0x1) == 0); |
| 1298 |
|
| 1299 |
// Return a tagged index for local type references |
| 1300 |
if (typeDef && &typeDef->recGroup() == recGroup) { |
| 1301 |
// recGroup->indexOf result is expected to be not greater than MaxTypes, |
| 1302 |
// and shall fit in 32-bit pointer without loss. |
| 1303 |
static_assert(MaxTypes <= 0x7FFFFFFF); |
| 1304 |
return (uintptr_t(recGroup->indexOf(typeDef)) << 1) | 0x1; |
| 1305 |
} |
| 1306 |
|
| 1307 |
// Return an untagged pointer for non-local type references |
| 1308 |
return uintptr_t(typeDef); |
| 1309 |
} |
| 1310 |
|
| 1311 |
/* static */ |
| 1312 |
inline IsoEqualsTypeCode IsoEqualsTypeCode::forIsoEquals( |
| 1313 |
PackedTypeCode ptc, const RecGroup* recGroup) { |
| 1314 |
IsoEqualsTypeCode mtc = {}; |
| 1315 |
mtc.typeCode = PackedRepr(ptc.typeCode()); |
| 1316 |
mtc.typeRef = TypeDef::forIsoEquals(ptc.typeDef(), recGroup); |
| 1317 |
mtc.nullable = ptc.isNullable(); |
| 1318 |
return mtc; |
| 1319 |
} |
| 1320 |
|
| 1321 |
template <class T> |
| 1322 |
void PackedType<T>::AddRef() const { |
| 1323 |
if (!isRefType()) { |
| 1324 |
return; |
| 1325 |
} |
| 1326 |
refType().AddRef(); |
| 1327 |
} |
| 1328 |
template <class T> |
| 1329 |
void PackedType<T>::Release() const { |
| 1330 |
if (!isRefType()) { |
| 1331 |
return; |
| 1332 |
} |
| 1333 |
refType().Release(); |
| 1334 |
} |
| 1335 |
|
| 1336 |
void RefType::AddRef() const { |
| 1337 |
if (!isTypeRef()) { |
| 1338 |
return; |
| 1339 |
} |
| 1340 |
typeDef()->AddRef(); |
| 1341 |
} |
| 1342 |
void RefType::Release() const { |
| 1343 |
if (!isTypeRef()) { |
| 1344 |
return; |
| 1345 |
} |
| 1346 |
typeDef()->Release(); |
| 1347 |
} |
| 1348 |
|
| 1349 |
void TypeDef::AddRef() const { recGroup().AddRef(); } |
| 1350 |
void TypeDef::Release() const { recGroup().Release(); } |
| 1351 |
|
| 1352 |
inline RefTypeHierarchy RefType::hierarchy() const { |
| 1353 |
switch (kind()) { |
| 1354 |
case RefType::Func: |
| 1355 |
case RefType::NoFunc: |
| 1356 |
return RefTypeHierarchy::Func; |
| 1357 |
case RefType::Extern: |
| 1358 |
case RefType::NoExtern: |
| 1359 |
return RefTypeHierarchy::Extern; |
| 1360 |
case RefType::Exn: |
| 1361 |
case RefType::NoExn: |
| 1362 |
return RefTypeHierarchy::Exn; |
| 1363 |
case RefType::Any: |
| 1364 |
case RefType::None: |
| 1365 |
case RefType::I31: |
| 1366 |
case RefType::Eq: |
| 1367 |
case RefType::Struct: |
| 1368 |
case RefType::Array: |
| 1369 |
return RefTypeHierarchy::Any; |
| 1370 |
case RefType::TypeRef: |
| 1371 |
switch (typeDef()->kind()) { |
| 1372 |
case TypeDefKind::Struct: |
| 1373 |
case TypeDefKind::Array: |
| 1374 |
return RefTypeHierarchy::Any; |
| 1375 |
case TypeDefKind::Func: |
| 1376 |
return RefTypeHierarchy::Func; |
| 1377 |
case TypeDefKind::None: |
| 1378 |
MOZ_CRASH(); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
MOZ_CRASH("switch is exhaustive"); |
| 1382 |
} |
| 1383 |
|
| 1384 |
inline TableRepr RefType::tableRepr() const { |
| 1385 |
switch (hierarchy()) { |
| 1386 |
case RefTypeHierarchy::Any: |
| 1387 |
case RefTypeHierarchy::Extern: |
| 1388 |
case RefTypeHierarchy::Exn: |
| 1389 |
return TableRepr::Ref; |
| 1390 |
case RefTypeHierarchy::Func: |
| 1391 |
return TableRepr::Func; |
| 1392 |
} |
| 1393 |
MOZ_CRASH("switch is exhaustive"); |
| 1394 |
} |
| 1395 |
|
| 1396 |
inline bool RefType::isFuncHierarchy() const { |
| 1397 |
return hierarchy() == RefTypeHierarchy::Func; |
| 1398 |
} |
| 1399 |
inline bool RefType::isExternHierarchy() const { |
| 1400 |
return hierarchy() == RefTypeHierarchy::Extern; |
| 1401 |
} |
| 1402 |
inline bool RefType::isAnyHierarchy() const { |
| 1403 |
return hierarchy() == RefTypeHierarchy::Any; |
| 1404 |
} |
| 1405 |
inline bool RefType::isExnHierarchy() const { |
| 1406 |
return hierarchy() == RefTypeHierarchy::Exn; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* static */ |
| 1410 |
inline bool RefType::isSubTypeOf(RefType subType, RefType superType) { |
| 1411 |
// Anything is a subtype of itself. |
| 1412 |
if (subType == superType) { |
| 1413 |
return true; |
| 1414 |
} |
| 1415 |
|
| 1416 |
// A subtype must have the same nullability as the supertype or the |
| 1417 |
// supertype must be nullable. |
| 1418 |
if (subType.isNullable() && !superType.isNullable()) { |
| 1419 |
return false; |
| 1420 |
} |
| 1421 |
|
| 1422 |
// Non type-index references are subtypes if they have the same kind |
| 1423 |
if (!subType.isTypeRef() && !superType.isTypeRef() && |
| 1424 |
subType.kind() == superType.kind()) { |
| 1425 |
return true; |
| 1426 |
} |
| 1427 |
|
| 1428 |
// eq is a subtype of any |
| 1429 |
if (subType.isEq() && superType.isAny()) { |
| 1430 |
return true; |
| 1431 |
} |
| 1432 |
|
| 1433 |
// i31 is a subtype of eq and any |
| 1434 |
if (subType.isI31() && (superType.isAny() || superType.isEq())) { |
| 1435 |
return true; |
| 1436 |
} |
| 1437 |
|
| 1438 |
// Abstract struct/array are subtypes of eq and any |
| 1439 |
if ((subType.isStruct() || subType.isArray()) && |
| 1440 |
(superType.isAny() || superType.isEq())) { |
| 1441 |
return true; |
| 1442 |
} |
| 1443 |
|
| 1444 |
// Concrete struct types are subtypes of struct, eq, and any |
| 1445 |
if (subType.isTypeRef() && subType.typeDef()->isStructType() && |
| 1446 |
(superType.isAny() || superType.isEq() || superType.isStruct())) { |
| 1447 |
return true; |
| 1448 |
} |
| 1449 |
|
| 1450 |
// Concrete array types are subtypes of array, eq, and any |
| 1451 |
if (subType.isTypeRef() && subType.typeDef()->isArrayType() && |
| 1452 |
(superType.isAny() || superType.isEq() || superType.isArray())) { |
| 1453 |
return true; |
| 1454 |
} |
| 1455 |
|
| 1456 |
// Concrete func types are subtypes of func |
| 1457 |
if (subType.isTypeRef() && subType.typeDef()->isFuncType() && |
| 1458 |
superType.isFunc()) { |
| 1459 |
return true; |
| 1460 |
} |
| 1461 |
|
| 1462 |
// Type references can be subtypes |
| 1463 |
if (subType.isTypeRef() && superType.isTypeRef()) { |
| 1464 |
return TypeDef::isSubTypeOf(subType.typeDef(), superType.typeDef()); |
| 1465 |
} |
| 1466 |
|
| 1467 |
// nofunc is the bottom type of the func hierarchy |
| 1468 |
if (subType.isNoFunc() && superType.hierarchy() == RefTypeHierarchy::Func) { |
| 1469 |
return true; |
| 1470 |
} |
| 1471 |
|
| 1472 |
// noextern is the bottom type of the extern hierarchy |
| 1473 |
if (subType.isNoExtern() && |
| 1474 |
superType.hierarchy() == RefTypeHierarchy::Extern) { |
| 1475 |
return true; |
| 1476 |
} |
| 1477 |
|
| 1478 |
// none is the bottom type of the any hierarchy |
| 1479 |
if (subType.isNone() && superType.hierarchy() == RefTypeHierarchy::Any) { |
| 1480 |
return true; |
| 1481 |
} |
| 1482 |
|
| 1483 |
// noexn is the bottom type of the exn hierarchy |
| 1484 |
if (subType.isNoExn() && superType.hierarchy() == RefTypeHierarchy::Exn) { |
| 1485 |
return true; |
| 1486 |
} |
| 1487 |
|
| 1488 |
return false; |
| 1489 |
} |
| 1490 |
|
| 1491 |
/* static */ |
| 1492 |
inline bool RefType::castPossible(RefType sourceType, RefType destType) { |
| 1493 |
// Nullable types always have null in common. |
| 1494 |
if (sourceType.isNullable() && destType.isNullable()) { |
| 1495 |
return true; |
| 1496 |
} |
| 1497 |
|
| 1498 |
// At least one of the types is non-nullable, so the only common values can be |
| 1499 |
// non-null. Therefore, if either type is a bottom type, common values are |
| 1500 |
// impossible. |
| 1501 |
if (sourceType.isRefBottom() || destType.isRefBottom()) { |
| 1502 |
return false; |
| 1503 |
} |
| 1504 |
|
| 1505 |
// After excluding bottom types, our type hierarchy is a tree, and after |
| 1506 |
// excluding nulls, subtype relationships are sufficient to tell if the types |
| 1507 |
// share any values. If neither type is a subtype of the other, then they are |
| 1508 |
// on different branches of the tree and completely disjoint. |
| 1509 |
RefType sourceNonNull = sourceType.withIsNullable(false); |
| 1510 |
RefType destNonNull = destType.withIsNullable(false); |
| 1511 |
return RefType::isSubTypeOf(sourceNonNull, destNonNull) || |
| 1512 |
RefType::isSubTypeOf(destNonNull, sourceNonNull); |
| 1513 |
} |
| 1514 |
|
| 1515 |
//========================================================================= |
| 1516 |
// [SMDOC] Signatures and runtime types |
| 1517 |
// |
| 1518 |
// TypeIdDesc describes the runtime representation of a TypeDef suitable for |
| 1519 |
// type equality checks. The kind of representation depends on whether the type |
| 1520 |
// is a function or a GC type. This design is in flux and will evolve. |
| 1521 |
// |
| 1522 |
// # Function types |
| 1523 |
// |
| 1524 |
// For functions in the general case, a FuncType is allocated and stored in a |
| 1525 |
// process-wide hash table, so that pointer equality implies structural |
| 1526 |
// equality. This process does not correctly handle type references (which would |
| 1527 |
// require hash-consing of infinite-trees), but that's okay while |
| 1528 |
// function-references and gc-types are experimental. |
| 1529 |
// |
| 1530 |
// A pointer to the hash table entry is stored in the global data |
| 1531 |
// area for each instance, and TypeIdDesc gives the offset to this entry. |
| 1532 |
// |
| 1533 |
// ## Immediate function types |
| 1534 |
// |
| 1535 |
// As an optimization for the 99% case where the FuncType has a small number of |
| 1536 |
// parameters, the FuncType is bit-packed into a uint32 immediate value so that |
| 1537 |
// integer equality implies structural equality. Both cases can be handled with |
| 1538 |
// a single comparison by always setting the LSB for the immediates |
| 1539 |
// (the LSB is necessarily 0 for allocated FuncType pointers due to alignment). |
| 1540 |
// |
| 1541 |
// # GC types |
| 1542 |
// |
| 1543 |
// For GC types, an entry is always created in the global data area and a |
| 1544 |
// unique RttValue (see wasm/WasmGcObject.h) is stored there. This RttValue |
| 1545 |
// is the value given by 'rtt.canon $t' for each type definition. As each entry |
| 1546 |
// is given a unique value and no canonicalization is done (which would require |
| 1547 |
// hash-consing of infinite-trees), this is not yet spec compliant. |
| 1548 |
// |
| 1549 |
// # wasm::Instance and the global type context |
| 1550 |
// |
| 1551 |
// As GC objects may outlive the module they are created in, types are |
| 1552 |
// additionally transferred to a wasm::Context (which is part of JSContext) upon |
| 1553 |
// instantiation. This wasm::Context contains the 'global type context' that |
| 1554 |
// RTTValues refer to by type index. Types are never freed from the global type |
| 1555 |
// context as that would shift the index space. In the future, this will be |
| 1556 |
// fixed. |
| 1557 |
|
| 1558 |
} // namespace wasm |
| 1559 |
} // namespace js |
| 1560 |
|
| 1561 |
#endif // wasm_type_def_h |