| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* |
| 4 |
* Copyright 2016 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef wasm_op_iter_h |
| 20 |
#define wasm_op_iter_h |
| 21 |
|
| 22 |
#include "mozilla/CompactPair.h" |
| 23 |
#include "mozilla/Poison.h" |
| 24 |
|
| 25 |
#include <type_traits> |
| 26 |
|
| 27 |
#include "js/Printf.h" |
| 28 |
#include "wasm/WasmBinary.h" |
| 29 |
#include "wasm/WasmBuiltinModule.h" |
| 30 |
#include "wasm/WasmMetadata.h" |
| 31 |
#include "wasm/WasmUtility.h" |
| 32 |
|
| 33 |
namespace js { |
| 34 |
namespace wasm { |
| 35 |
|
| 36 |
// The kind of a control-flow stack item. |
| 37 |
enum class LabelKind : uint8_t { |
| 38 |
Body, |
| 39 |
Block, |
| 40 |
Loop, |
| 41 |
Then, |
| 42 |
Else, |
| 43 |
Try, |
| 44 |
Catch, |
| 45 |
CatchAll, |
| 46 |
TryTable, |
| 47 |
}; |
| 48 |
|
| 49 |
// The type of values on the operand stack during validation. This is either a |
| 50 |
// ValType or the special type "Bottom". |
| 51 |
|
| 52 |
class StackType { |
| 53 |
PackedTypeCode tc_; |
| 54 |
|
| 55 |
explicit StackType(PackedTypeCode tc) : tc_(tc) {} |
| 56 |
|
| 57 |
public: |
| 58 |
StackType() : tc_(PackedTypeCode::invalid()) {} |
| 59 |
|
| 60 |
explicit StackType(const ValType& t) : tc_(t.packed()) { |
| 61 |
MOZ_ASSERT(tc_.isValid()); |
| 62 |
MOZ_ASSERT(!isStackBottom()); |
| 63 |
} |
| 64 |
|
| 65 |
static StackType bottom() { |
| 66 |
return StackType(PackedTypeCode::pack(TypeCode::Limit)); |
| 67 |
} |
| 68 |
|
| 69 |
bool isStackBottom() const { |
| 70 |
MOZ_ASSERT(tc_.isValid()); |
| 71 |
return tc_.typeCode() == TypeCode::Limit; |
| 72 |
} |
| 73 |
|
| 74 |
// Returns whether this input is nullable when interpreted as an operand. |
| 75 |
// When the type is bottom for unreachable code, this returns false as that |
| 76 |
// is the most permissive option. |
| 77 |
bool isNullableAsOperand() const { |
| 78 |
MOZ_ASSERT(tc_.isValid()); |
| 79 |
return isStackBottom() ? false : tc_.isNullable(); |
| 80 |
} |
| 81 |
|
| 82 |
ValType valType() const { |
| 83 |
MOZ_ASSERT(tc_.isValid()); |
| 84 |
MOZ_ASSERT(!isStackBottom()); |
| 85 |
return ValType(tc_); |
| 86 |
} |
| 87 |
|
| 88 |
ValType valTypeOr(ValType ifBottom) const { |
| 89 |
MOZ_ASSERT(tc_.isValid()); |
| 90 |
if (isStackBottom()) { |
| 91 |
return ifBottom; |
| 92 |
} |
| 93 |
return valType(); |
| 94 |
} |
| 95 |
|
| 96 |
ValType asNonNullable() const { |
| 97 |
MOZ_ASSERT(tc_.isValid()); |
| 98 |
MOZ_ASSERT(!isStackBottom()); |
| 99 |
return ValType(tc_.withIsNullable(false)); |
| 100 |
} |
| 101 |
|
| 102 |
bool isValidForUntypedSelect() const { |
| 103 |
MOZ_ASSERT(tc_.isValid()); |
| 104 |
if (isStackBottom()) { |
| 105 |
return true; |
| 106 |
} |
| 107 |
switch (valType().kind()) { |
| 108 |
case ValType::I32: |
| 109 |
case ValType::F32: |
| 110 |
case ValType::I64: |
| 111 |
case ValType::F64: |
| 112 |
#ifdef ENABLE_WASM_SIMD |
| 113 |
case ValType::V128: |
| 114 |
#endif |
| 115 |
return true; |
| 116 |
default: |
| 117 |
return false; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
bool operator==(const StackType& that) const { |
| 122 |
MOZ_ASSERT(tc_.isValid() && that.tc_.isValid()); |
| 123 |
return tc_ == that.tc_; |
| 124 |
} |
| 125 |
|
| 126 |
bool operator!=(const StackType& that) const { |
| 127 |
MOZ_ASSERT(tc_.isValid() && that.tc_.isValid()); |
| 128 |
return tc_ != that.tc_; |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
#ifdef DEBUG |
| 133 |
// Families of opcodes that share a signature and validation logic. |
| 134 |
enum class OpKind { |
| 135 |
Block, |
| 136 |
Loop, |
| 137 |
Unreachable, |
| 138 |
Drop, |
| 139 |
I32Const, |
| 140 |
I64Const, |
| 141 |
F32Const, |
| 142 |
F64Const, |
| 143 |
V128Const, |
| 144 |
Br, |
| 145 |
BrIf, |
| 146 |
BrTable, |
| 147 |
Nop, |
| 148 |
Unary, |
| 149 |
Binary, |
| 150 |
Ternary, |
| 151 |
Comparison, |
| 152 |
Conversion, |
| 153 |
Load, |
| 154 |
Store, |
| 155 |
TeeStore, |
| 156 |
MemorySize, |
| 157 |
MemoryGrow, |
| 158 |
Select, |
| 159 |
GetLocal, |
| 160 |
SetLocal, |
| 161 |
TeeLocal, |
| 162 |
GetGlobal, |
| 163 |
SetGlobal, |
| 164 |
TeeGlobal, |
| 165 |
Call, |
| 166 |
ReturnCall, |
| 167 |
CallIndirect, |
| 168 |
ReturnCallIndirect, |
| 169 |
CallRef, |
| 170 |
ReturnCallRef, |
| 171 |
OldCallDirect, |
| 172 |
OldCallIndirect, |
| 173 |
Return, |
| 174 |
If, |
| 175 |
Else, |
| 176 |
End, |
| 177 |
Wait, |
| 178 |
Notify, |
| 179 |
Fence, |
| 180 |
AtomicLoad, |
| 181 |
AtomicStore, |
| 182 |
AtomicRMW, |
| 183 |
AtomicCmpXchg, |
| 184 |
MemOrTableCopy, |
| 185 |
DataOrElemDrop, |
| 186 |
MemFill, |
| 187 |
MemOrTableInit, |
| 188 |
TableFill, |
| 189 |
MemDiscard, |
| 190 |
TableGet, |
| 191 |
TableGrow, |
| 192 |
TableSet, |
| 193 |
TableSize, |
| 194 |
RefNull, |
| 195 |
RefFunc, |
| 196 |
RefIsNull, |
| 197 |
RefAsNonNull, |
| 198 |
BrOnNull, |
| 199 |
BrOnNonNull, |
| 200 |
StructNew, |
| 201 |
StructNewDefault, |
| 202 |
StructGet, |
| 203 |
StructSet, |
| 204 |
ArrayNew, |
| 205 |
ArrayNewFixed, |
| 206 |
ArrayNewDefault, |
| 207 |
ArrayNewData, |
| 208 |
ArrayNewElem, |
| 209 |
ArrayInitData, |
| 210 |
ArrayInitElem, |
| 211 |
ArrayGet, |
| 212 |
ArraySet, |
| 213 |
ArrayLen, |
| 214 |
ArrayCopy, |
| 215 |
ArrayFill, |
| 216 |
RefTest, |
| 217 |
RefCast, |
| 218 |
BrOnCast, |
| 219 |
RefConversion, |
| 220 |
# ifdef ENABLE_WASM_SIMD |
| 221 |
ExtractLane, |
| 222 |
ReplaceLane, |
| 223 |
LoadLane, |
| 224 |
StoreLane, |
| 225 |
VectorShift, |
| 226 |
VectorShuffle, |
| 227 |
# endif |
| 228 |
Catch, |
| 229 |
CatchAll, |
| 230 |
Delegate, |
| 231 |
Throw, |
| 232 |
ThrowRef, |
| 233 |
Rethrow, |
| 234 |
Try, |
| 235 |
TryTable, |
| 236 |
CallBuiltinModuleFunc, |
| 237 |
StackSwitch, |
| 238 |
}; |
| 239 |
|
| 240 |
// Return the OpKind for a given Op. This is used for sanity-checking that |
| 241 |
// API users use the correct read function for a given Op. |
| 242 |
OpKind Classify(OpBytes op); |
| 243 |
#endif |
| 244 |
|
| 245 |
// Common fields for linear memory access. |
| 246 |
template <typename Value> |
| 247 |
struct LinearMemoryAddress { |
| 248 |
Value base; |
| 249 |
uint32_t memoryIndex; |
| 250 |
uint64_t offset; |
| 251 |
uint32_t align; |
| 252 |
|
| 253 |
LinearMemoryAddress() : memoryIndex(0), offset(0), align(0) {} |
| 254 |
LinearMemoryAddress(Value base, uint32_t memoryIndex, uint64_t offset, |
| 255 |
uint32_t align) |
| 256 |
: base(base), memoryIndex(memoryIndex), offset(offset), align(align) {} |
| 257 |
}; |
| 258 |
|
| 259 |
template <typename ControlItem> |
| 260 |
class ControlStackEntry { |
| 261 |
// Use a pair to optimize away empty ControlItem. |
| 262 |
mozilla::CompactPair<BlockType, ControlItem> typeAndItem_; |
| 263 |
|
| 264 |
// The "base" of a control stack entry is valueStack_.length() minus |
| 265 |
// type().params().length(), i.e., the size of the value stack "below" |
| 266 |
// this block. |
| 267 |
uint32_t valueStackBase_; |
| 268 |
bool polymorphicBase_; |
| 269 |
|
| 270 |
LabelKind kind_; |
| 271 |
|
| 272 |
public: |
| 273 |
ControlStackEntry(LabelKind kind, BlockType type, uint32_t valueStackBase) |
| 274 |
: typeAndItem_(type, ControlItem()), |
| 275 |
valueStackBase_(valueStackBase), |
| 276 |
polymorphicBase_(false), |
| 277 |
kind_(kind) { |
| 278 |
MOZ_ASSERT(type != BlockType()); |
| 279 |
} |
| 280 |
|
| 281 |
LabelKind kind() const { return kind_; } |
| 282 |
BlockType type() const { return typeAndItem_.first(); } |
| 283 |
ResultType resultType() const { return type().results(); } |
| 284 |
ResultType branchTargetType() const { |
| 285 |
return kind_ == LabelKind::Loop ? type().params() : type().results(); |
| 286 |
} |
| 287 |
uint32_t valueStackBase() const { return valueStackBase_; } |
| 288 |
ControlItem& controlItem() { return typeAndItem_.second(); } |
| 289 |
const ControlItem& controlItem() const { return typeAndItem_.second(); } |
| 290 |
void setPolymorphicBase() { polymorphicBase_ = true; } |
| 291 |
bool polymorphicBase() const { return polymorphicBase_; } |
| 292 |
|
| 293 |
void switchToElse() { |
| 294 |
MOZ_ASSERT(kind() == LabelKind::Then); |
| 295 |
kind_ = LabelKind::Else; |
| 296 |
polymorphicBase_ = false; |
| 297 |
} |
| 298 |
|
| 299 |
void switchToCatch() { |
| 300 |
MOZ_ASSERT(kind() == LabelKind::Try || kind() == LabelKind::Catch); |
| 301 |
kind_ = LabelKind::Catch; |
| 302 |
polymorphicBase_ = false; |
| 303 |
} |
| 304 |
|
| 305 |
void switchToCatchAll() { |
| 306 |
MOZ_ASSERT(kind() == LabelKind::Try || kind() == LabelKind::Catch); |
| 307 |
kind_ = LabelKind::CatchAll; |
| 308 |
polymorphicBase_ = false; |
| 309 |
} |
| 310 |
}; |
| 311 |
|
| 312 |
// Track state of the non-defaultable locals. Every time such local is |
| 313 |
// initialized, the stack will record at what depth and which local was set. |
| 314 |
// On a block end, the "unset" state will be rolled back to how it was before |
| 315 |
// the block started. |
| 316 |
// |
| 317 |
// It is very likely only a few functions will have non-defaultable locals and |
| 318 |
// very few locals will be non-defaultable. This class is optimized to be fast |
| 319 |
// for this common case. |
| 320 |
class UnsetLocalsState { |
| 321 |
struct SetLocalEntry { |
| 322 |
uint32_t depth; |
| 323 |
uint32_t localUnsetIndex; |
| 324 |
SetLocalEntry(uint32_t depth_, uint32_t localUnsetIndex_) |
| 325 |
: depth(depth_), localUnsetIndex(localUnsetIndex_) {} |
| 326 |
}; |
| 327 |
using SetLocalsStack = Vector<SetLocalEntry, 16, SystemAllocPolicy>; |
| 328 |
using UnsetLocals = Vector<uint32_t, 16, SystemAllocPolicy>; |
| 329 |
|
| 330 |
static constexpr size_t WordSize = 4; |
| 331 |
static constexpr size_t WordBits = WordSize * 8; |
| 332 |
|
| 333 |
// Bit array of "unset" function locals. Stores only unset states of the |
| 334 |
// locals that are declared after the first non-defaultable local. |
| 335 |
UnsetLocals unsetLocals_; |
| 336 |
// Stack of "set" operations. Contains pair where the first field is a depth, |
| 337 |
// and the second field is local id (offset by firstNonDefaultLocal_). |
| 338 |
SetLocalsStack setLocalsStack_; |
| 339 |
uint32_t firstNonDefaultLocal_; |
| 340 |
|
| 341 |
public: |
| 342 |
UnsetLocalsState() : firstNonDefaultLocal_(UINT32_MAX) {} |
| 343 |
|
| 344 |
[[nodiscard]] bool init(const ValTypeVector& locals, size_t numParams); |
| 345 |
|
| 346 |
inline bool isUnset(uint32_t id) const { |
| 347 |
if (MOZ_LIKELY(id < firstNonDefaultLocal_)) { |
| 348 |
return false; |
| 349 |
} |
| 350 |
uint32_t localUnsetIndex = id - firstNonDefaultLocal_; |
| 351 |
return unsetLocals_[localUnsetIndex / WordBits] & |
| 352 |
(1 << (localUnsetIndex % WordBits)); |
| 353 |
} |
| 354 |
|
| 355 |
inline void set(uint32_t id, uint32_t depth) { |
| 356 |
MOZ_ASSERT(isUnset(id)); |
| 357 |
MOZ_ASSERT(id >= firstNonDefaultLocal_ && |
| 358 |
(id - firstNonDefaultLocal_) / WordBits < unsetLocals_.length()); |
| 359 |
uint32_t localUnsetIndex = id - firstNonDefaultLocal_; |
| 360 |
unsetLocals_[localUnsetIndex / WordBits] ^= 1 |
| 361 |
<< (localUnsetIndex % WordBits); |
| 362 |
// The setLocalsStack_ is reserved upfront in the UnsetLocalsState::init. |
| 363 |
// A SetLocalEntry will be pushed only once per local. |
| 364 |
setLocalsStack_.infallibleEmplaceBack(depth, localUnsetIndex); |
| 365 |
} |
| 366 |
|
| 367 |
inline void resetToBlock(uint32_t controlDepth) { |
| 368 |
while (MOZ_UNLIKELY(setLocalsStack_.length() > 0) && |
| 369 |
setLocalsStack_.back().depth > controlDepth) { |
| 370 |
uint32_t localUnsetIndex = setLocalsStack_.back().localUnsetIndex; |
| 371 |
MOZ_ASSERT(!(unsetLocals_[localUnsetIndex / WordBits] & |
| 372 |
(1 << (localUnsetIndex % WordBits)))); |
| 373 |
unsetLocals_[localUnsetIndex / WordBits] |= |
| 374 |
1 << (localUnsetIndex % WordBits); |
| 375 |
setLocalsStack_.popBack(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
int empty() const { return setLocalsStack_.empty(); } |
| 380 |
}; |
| 381 |
|
| 382 |
template <typename Value> |
| 383 |
class TypeAndValueT { |
| 384 |
// Use a Pair to optimize away empty Value. |
| 385 |
mozilla::CompactPair<StackType, Value> tv_; |
| 386 |
|
| 387 |
public: |
| 388 |
TypeAndValueT() : tv_(StackType::bottom(), Value()) {} |
| 389 |
explicit TypeAndValueT(StackType type) : tv_(type, Value()) {} |
| 390 |
explicit TypeAndValueT(ValType type) : tv_(StackType(type), Value()) {} |
| 391 |
TypeAndValueT(StackType type, Value value) : tv_(type, value) {} |
| 392 |
TypeAndValueT(ValType type, Value value) : tv_(StackType(type), value) {} |
| 393 |
StackType type() const { return tv_.first(); } |
| 394 |
void setType(StackType type) { tv_.first() = type; } |
| 395 |
Value value() const { return tv_.second(); } |
| 396 |
void setValue(Value value) { tv_.second() = value; } |
| 397 |
}; |
| 398 |
|
| 399 |
// An iterator over the bytes of a function body. It performs validation |
| 400 |
// and unpacks the data into a usable form. |
| 401 |
// |
| 402 |
// The MOZ_STACK_CLASS attribute here is because of the use of DebugOnly. |
| 403 |
// There's otherwise nothing inherent in this class which would require |
| 404 |
// it to be used on the stack. |
| 405 |
template <typename Policy> |
| 406 |
class MOZ_STACK_CLASS OpIter : private Policy { |
| 407 |
public: |
| 408 |
using Value = typename Policy::Value; |
| 409 |
using ValueVector = typename Policy::ValueVector; |
| 410 |
using TypeAndValue = TypeAndValueT<Value>; |
| 411 |
using TypeAndValueStack = Vector<TypeAndValue, 32, SystemAllocPolicy>; |
| 412 |
using ControlItem = typename Policy::ControlItem; |
| 413 |
using Control = ControlStackEntry<ControlItem>; |
| 414 |
using ControlStack = Vector<Control, 16, SystemAllocPolicy>; |
| 415 |
|
| 416 |
enum Kind { |
| 417 |
Func, |
| 418 |
InitExpr, |
| 419 |
}; |
| 420 |
|
| 421 |
private: |
| 422 |
Kind kind_; |
| 423 |
Decoder& d_; |
| 424 |
const CodeMetadata& codeMeta_; |
| 425 |
const ValTypeVector& locals_; |
| 426 |
|
| 427 |
TypeAndValueStack valueStack_; |
| 428 |
TypeAndValueStack elseParamStack_; |
| 429 |
ControlStack controlStack_; |
| 430 |
UnsetLocalsState unsetLocals_; |
| 431 |
FeatureUsage featureUsage_; |
| 432 |
uint32_t lastBranchHintIndex_; |
| 433 |
BranchHintVector* branchHintVector_; |
| 434 |
|
| 435 |
#ifdef DEBUG |
| 436 |
OpBytes op_; |
| 437 |
#endif |
| 438 |
size_t offsetOfLastReadOp_; |
| 439 |
|
| 440 |
[[nodiscard]] bool readFixedU8(uint8_t* out) { return d_.readFixedU8(out); } |
| 441 |
[[nodiscard]] bool readFixedU32(uint32_t* out) { |
| 442 |
return d_.readFixedU32(out); |
| 443 |
} |
| 444 |
[[nodiscard]] bool readVarS32(int32_t* out) { return d_.readVarS32(out); } |
| 445 |
[[nodiscard]] bool readVarU32(uint32_t* out) { return d_.readVarU32(out); } |
| 446 |
[[nodiscard]] bool readVarS64(int64_t* out) { return d_.readVarS64(out); } |
| 447 |
[[nodiscard]] bool readVarU64(uint64_t* out) { return d_.readVarU64(out); } |
| 448 |
[[nodiscard]] bool readFixedF32(float* out) { return d_.readFixedF32(out); } |
| 449 |
[[nodiscard]] bool readFixedF64(double* out) { return d_.readFixedF64(out); } |
| 450 |
|
| 451 |
[[nodiscard]] bool readLinearMemoryAddress(uint32_t byteSize, |
| 452 |
LinearMemoryAddress<Value>* addr); |
| 453 |
[[nodiscard]] bool readLinearMemoryAddressAligned( |
| 454 |
uint32_t byteSize, LinearMemoryAddress<Value>* addr); |
| 455 |
[[nodiscard]] bool readBlockType(BlockType* type); |
| 456 |
[[nodiscard]] bool readGcTypeIndex(uint32_t* typeIndex); |
| 457 |
[[nodiscard]] bool readStructTypeIndex(uint32_t* typeIndex); |
| 458 |
[[nodiscard]] bool readArrayTypeIndex(uint32_t* typeIndex); |
| 459 |
[[nodiscard]] bool readFuncTypeIndex(uint32_t* typeIndex); |
| 460 |
[[nodiscard]] bool readFieldIndex(uint32_t* fieldIndex, |
| 461 |
const StructType& structType); |
| 462 |
|
| 463 |
[[nodiscard]] bool popCallArgs(const ValTypeVector& expectedTypes, |
| 464 |
ValueVector* values); |
| 465 |
|
| 466 |
[[nodiscard]] bool failEmptyStack(); |
| 467 |
[[nodiscard]] bool popStackType(StackType* type, Value* value); |
| 468 |
[[nodiscard]] bool popWithType(ValType expected, Value* value, |
| 469 |
StackType* stackType); |
| 470 |
[[nodiscard]] bool popWithType(ValType expected, Value* value); |
| 471 |
[[nodiscard]] bool popWithType(ResultType expected, ValueVector* values); |
| 472 |
template <typename ValTypeSpanT> |
| 473 |
[[nodiscard]] bool popWithTypes(ValTypeSpanT expected, ValueVector* values); |
| 474 |
[[nodiscard]] bool popWithRefType(Value* value, StackType* type); |
| 475 |
// Check that the top of the value stack has type `expected`, bearing in |
| 476 |
// mind that it may be a block type, hence involving multiple values. |
| 477 |
// |
| 478 |
// If the block's stack contains polymorphic values at its base (because we |
| 479 |
// are in unreachable code) then suitable extra values are inserted into the |
| 480 |
// value stack, as controlled by `rewriteStackTypes`: if this is true, |
| 481 |
// polymorphic values have their types created/updated from `expected`. If |
| 482 |
// it is false, such values are left as `StackType::bottom()`. |
| 483 |
// |
| 484 |
// If `values` is non-null, it is filled in with Value components of the |
| 485 |
// relevant stack entries, including those of any new entries created. |
| 486 |
[[nodiscard]] bool checkTopTypeMatches(ResultType expected, |
| 487 |
ValueVector* values, |
| 488 |
bool rewriteStackTypes); |
| 489 |
|
| 490 |
[[nodiscard]] bool pushControl(LabelKind kind, BlockType type); |
| 491 |
[[nodiscard]] bool checkStackAtEndOfBlock(ResultType* type, |
| 492 |
ValueVector* values); |
| 493 |
[[nodiscard]] bool getControl(uint32_t relativeDepth, Control** controlEntry); |
| 494 |
[[nodiscard]] bool checkBranchValueAndPush(uint32_t relativeDepth, |
| 495 |
ResultType* type, |
| 496 |
ValueVector* values, |
| 497 |
bool rewriteStackTypes); |
| 498 |
[[nodiscard]] bool checkBrTableEntryAndPush(uint32_t* relativeDepth, |
| 499 |
ResultType prevBranchType, |
| 500 |
ResultType* branchType, |
| 501 |
ValueVector* branchValues); |
| 502 |
|
| 503 |
[[nodiscard]] bool push(StackType t) { return valueStack_.emplaceBack(t); } |
| 504 |
[[nodiscard]] bool push(ValType t) { return valueStack_.emplaceBack(t); } |
| 505 |
[[nodiscard]] bool push(TypeAndValue tv) { return valueStack_.append(tv); } |
| 506 |
[[nodiscard]] bool push(ResultType t) { |
| 507 |
for (size_t i = 0; i < t.length(); i++) { |
| 508 |
if (!push(t[i])) { |
| 509 |
return false; |
| 510 |
} |
| 511 |
} |
| 512 |
return true; |
| 513 |
} |
| 514 |
void infalliblePush(StackType t) { valueStack_.infallibleEmplaceBack(t); } |
| 515 |
void infalliblePush(ValType t) { |
| 516 |
valueStack_.infallibleEmplaceBack(StackType(t)); |
| 517 |
} |
| 518 |
void infalliblePush(TypeAndValue tv) { valueStack_.infallibleAppend(tv); } |
| 519 |
|
| 520 |
void afterUnconditionalBranch() { |
| 521 |
valueStack_.shrinkTo(controlStack_.back().valueStackBase()); |
| 522 |
controlStack_.back().setPolymorphicBase(); |
| 523 |
} |
| 524 |
|
| 525 |
inline bool checkIsSubtypeOf(StorageType actual, StorageType expected); |
| 526 |
|
| 527 |
inline bool checkIsSubtypeOf(RefType actual, RefType expected) { |
| 528 |
return checkIsSubtypeOf(ValType(actual).storageType(), |
| 529 |
ValType(expected).storageType()); |
| 530 |
} |
| 531 |
inline bool checkIsSubtypeOf(ValType actual, ValType expected) { |
| 532 |
return checkIsSubtypeOf(actual.storageType(), expected.storageType()); |
| 533 |
} |
| 534 |
|
| 535 |
inline bool checkIsSubtypeOf(ResultType params, ResultType results); |
| 536 |
|
| 537 |
inline bool checkIsSubtypeOf(uint32_t actualTypeIndex, |
| 538 |
uint32_t expectedTypeIndex); |
| 539 |
|
| 540 |
public: |
| 541 |
explicit OpIter(const CodeMetadata& codeMeta, Decoder& decoder, |
| 542 |
const ValTypeVector& locals, Kind kind = OpIter::Func) |
| 543 |
: kind_(kind), |
| 544 |
d_(decoder), |
| 545 |
codeMeta_(codeMeta), |
| 546 |
locals_(locals), |
| 547 |
featureUsage_(FeatureUsage::None), |
| 548 |
branchHintVector_(nullptr), |
| 549 |
#ifdef DEBUG |
| 550 |
op_(OpBytes(Op::Limit)), |
| 551 |
#endif |
| 552 |
offsetOfLastReadOp_(0) { |
| 553 |
} |
| 554 |
|
| 555 |
FeatureUsage featureUsage() const { return featureUsage_; } |
| 556 |
void addFeatureUsage(FeatureUsage featureUsage) { |
| 557 |
featureUsage_ |= featureUsage; |
| 558 |
} |
| 559 |
|
| 560 |
// Return the decoding byte offset. |
| 561 |
uint32_t currentOffset() const { return d_.currentOffset(); } |
| 562 |
|
| 563 |
// Return the offset within the entire module of the last-read op. |
| 564 |
size_t lastOpcodeOffset() const { |
| 565 |
return offsetOfLastReadOp_ ? offsetOfLastReadOp_ : d_.currentOffset(); |
| 566 |
} |
| 567 |
|
| 568 |
// Return a BytecodeOffset describing where the current op should be reported |
| 569 |
// to trap/call. |
| 570 |
BytecodeOffset bytecodeOffset() const { |
| 571 |
return BytecodeOffset(lastOpcodeOffset()); |
| 572 |
} |
| 573 |
|
| 574 |
// Test whether the iterator has reached the end of the buffer. |
| 575 |
bool done() const { return d_.done(); } |
| 576 |
|
| 577 |
// Return a pointer to the end of the buffer being decoded by this iterator. |
| 578 |
const uint8_t* end() const { return d_.end(); } |
| 579 |
|
| 580 |
// Report a general failure. |
| 581 |
[[nodiscard]] bool fail(const char* msg) MOZ_COLD; |
| 582 |
|
| 583 |
// Report a general failure with a context |
| 584 |
[[nodiscard]] bool fail_ctx(const char* fmt, const char* context) MOZ_COLD; |
| 585 |
|
| 586 |
// Report an unrecognized opcode. |
| 587 |
[[nodiscard]] bool unrecognizedOpcode(const OpBytes* expr) MOZ_COLD; |
| 588 |
|
| 589 |
// Return whether the innermost block has a polymorphic base of its stack. |
| 590 |
// Ideally this accessor would be removed; consider using something else. |
| 591 |
bool currentBlockHasPolymorphicBase() const { |
| 592 |
return !controlStack_.empty() && controlStack_.back().polymorphicBase(); |
| 593 |
} |
| 594 |
|
| 595 |
// If it exists, return the BranchHint value from a function index and a |
| 596 |
// branch offset. |
| 597 |
// Branch hints are stored in a sorted vector. Because code in compiled in |
| 598 |
// order, we keep track of the most recently accessed index. |
| 599 |
// Retrieving branch hints is also done in order inside a function. |
| 600 |
BranchHint getBranchHint(uint32_t funcIndex, uint32_t branchOffset) { |
| 601 |
if (!codeMeta_.branchHintingEnabled()) { |
| 602 |
return BranchHint::Invalid; |
| 603 |
} |
| 604 |
|
| 605 |
// Get the next hint in the collection |
| 606 |
while (lastBranchHintIndex_ < branchHintVector_->length() && |
| 607 |
(*branchHintVector_)[lastBranchHintIndex_].branchOffset < |
| 608 |
branchOffset) { |
| 609 |
lastBranchHintIndex_++; |
| 610 |
} |
| 611 |
|
| 612 |
// No hint found for this branch. |
| 613 |
if (lastBranchHintIndex_ >= branchHintVector_->length()) { |
| 614 |
return BranchHint::Invalid; |
| 615 |
} |
| 616 |
|
| 617 |
// The last index is saved, now return the hint. |
| 618 |
return (*branchHintVector_)[lastBranchHintIndex_].value; |
| 619 |
} |
| 620 |
|
| 621 |
// ------------------------------------------------------------------------ |
| 622 |
// Decoding and validation interface. |
| 623 |
|
| 624 |
// Initialization and termination |
| 625 |
|
| 626 |
[[nodiscard]] bool startFunction(uint32_t funcIndex); |
| 627 |
[[nodiscard]] bool endFunction(const uint8_t* bodyEnd); |
| 628 |
|
| 629 |
[[nodiscard]] bool startInitExpr(ValType expected); |
| 630 |
[[nodiscard]] bool endInitExpr(); |
| 631 |
|
| 632 |
// Value and reference types |
| 633 |
|
| 634 |
[[nodiscard]] bool readValType(ValType* type); |
| 635 |
[[nodiscard]] bool readHeapType(bool nullable, RefType* type); |
| 636 |
|
| 637 |
// Instructions |
| 638 |
|
| 639 |
[[nodiscard]] bool readOp(OpBytes* op); |
| 640 |
[[nodiscard]] bool readReturn(ValueVector* values); |
| 641 |
[[nodiscard]] bool readBlock(BlockType* type); |
| 642 |
[[nodiscard]] bool readLoop(BlockType* type); |
| 643 |
[[nodiscard]] bool readIf(BlockType* type, Value* condition); |
| 644 |
[[nodiscard]] bool readElse(ResultType* paramType, ResultType* resultType, |
| 645 |
ValueVector* thenResults); |
| 646 |
[[nodiscard]] bool readEnd(LabelKind* kind, ResultType* type, |
| 647 |
ValueVector* results, |
| 648 |
ValueVector* resultsForEmptyElse); |
| 649 |
void popEnd(); |
| 650 |
[[nodiscard]] bool readBr(uint32_t* relativeDepth, ResultType* type, |
| 651 |
ValueVector* values); |
| 652 |
[[nodiscard]] bool readBrIf(uint32_t* relativeDepth, ResultType* type, |
| 653 |
ValueVector* values, Value* condition); |
| 654 |
[[nodiscard]] bool readBrTable(Uint32Vector* depths, uint32_t* defaultDepth, |
| 655 |
ResultType* defaultBranchType, |
| 656 |
ValueVector* branchValues, Value* index); |
| 657 |
[[nodiscard]] bool readTry(BlockType* type); |
| 658 |
[[nodiscard]] bool readTryTable(BlockType* type, |
| 659 |
TryTableCatchVector* catches); |
| 660 |
[[nodiscard]] bool readCatch(LabelKind* kind, uint32_t* tagIndex, |
| 661 |
ResultType* paramType, ResultType* resultType, |
| 662 |
ValueVector* tryResults); |
| 663 |
[[nodiscard]] bool readCatchAll(LabelKind* kind, ResultType* paramType, |
| 664 |
ResultType* resultType, |
| 665 |
ValueVector* tryResults); |
| 666 |
[[nodiscard]] bool readDelegate(uint32_t* relativeDepth, |
| 667 |
ResultType* resultType, |
| 668 |
ValueVector* tryResults); |
| 669 |
void popDelegate(); |
| 670 |
[[nodiscard]] bool readThrow(uint32_t* tagIndex, ValueVector* argValues); |
| 671 |
[[nodiscard]] bool readThrowRef(Value* exnRef); |
| 672 |
[[nodiscard]] bool readRethrow(uint32_t* relativeDepth); |
| 673 |
[[nodiscard]] bool readUnreachable(); |
| 674 |
[[nodiscard]] bool readDrop(); |
| 675 |
[[nodiscard]] bool readUnary(ValType operandType, Value* input); |
| 676 |
[[nodiscard]] bool readConversion(ValType operandType, ValType resultType, |
| 677 |
Value* input); |
| 678 |
[[nodiscard]] bool readBinary(ValType operandType, Value* lhs, Value* rhs); |
| 679 |
[[nodiscard]] bool readComparison(ValType operandType, Value* lhs, |
| 680 |
Value* rhs); |
| 681 |
[[nodiscard]] bool readTernary(ValType operandType, Value* v0, Value* v1, |
| 682 |
Value* v2); |
| 683 |
[[nodiscard]] bool readLoad(ValType resultType, uint32_t byteSize, |
| 684 |
LinearMemoryAddress<Value>* addr); |
| 685 |
[[nodiscard]] bool readStore(ValType resultType, uint32_t byteSize, |
| 686 |
LinearMemoryAddress<Value>* addr, Value* value); |
| 687 |
[[nodiscard]] bool readTeeStore(ValType resultType, uint32_t byteSize, |
| 688 |
LinearMemoryAddress<Value>* addr, |
| 689 |
Value* value); |
| 690 |
[[nodiscard]] bool readNop(); |
| 691 |
[[nodiscard]] bool readMemorySize(uint32_t* memoryIndex); |
| 692 |
[[nodiscard]] bool readMemoryGrow(uint32_t* memoryIndex, Value* input); |
| 693 |
[[nodiscard]] bool readSelect(bool typed, StackType* type, Value* trueValue, |
| 694 |
Value* falseValue, Value* condition); |
| 695 |
[[nodiscard]] bool readGetLocal(uint32_t* id); |
| 696 |
[[nodiscard]] bool readSetLocal(uint32_t* id, Value* value); |
| 697 |
[[nodiscard]] bool readTeeLocal(uint32_t* id, Value* value); |
| 698 |
[[nodiscard]] bool readGetGlobal(uint32_t* id); |
| 699 |
[[nodiscard]] bool readSetGlobal(uint32_t* id, Value* value); |
| 700 |
[[nodiscard]] bool readTeeGlobal(uint32_t* id, Value* value); |
| 701 |
[[nodiscard]] bool readI32Const(int32_t* i32); |
| 702 |
[[nodiscard]] bool readI64Const(int64_t* i64); |
| 703 |
[[nodiscard]] bool readF32Const(float* f32); |
| 704 |
[[nodiscard]] bool readF64Const(double* f64); |
| 705 |
[[nodiscard]] bool readRefFunc(uint32_t* funcIndex); |
| 706 |
[[nodiscard]] bool readRefNull(RefType* type); |
| 707 |
[[nodiscard]] bool readRefIsNull(Value* input); |
| 708 |
[[nodiscard]] bool readRefAsNonNull(Value* input); |
| 709 |
[[nodiscard]] bool readBrOnNull(uint32_t* relativeDepth, ResultType* type, |
| 710 |
ValueVector* values, Value* condition); |
| 711 |
[[nodiscard]] bool readBrOnNonNull(uint32_t* relativeDepth, ResultType* type, |
| 712 |
ValueVector* values, Value* condition); |
| 713 |
[[nodiscard]] bool readCall(uint32_t* funcIndex, ValueVector* argValues); |
| 714 |
[[nodiscard]] bool readCallIndirect(uint32_t* funcTypeIndex, |
| 715 |
uint32_t* tableIndex, Value* callee, |
| 716 |
ValueVector* argValues); |
| 717 |
[[nodiscard]] bool readReturnCall(uint32_t* funcIndex, |
| 718 |
ValueVector* argValues); |
| 719 |
[[nodiscard]] bool readReturnCallIndirect(uint32_t* funcTypeIndex, |
| 720 |
uint32_t* tableIndex, Value* callee, |
| 721 |
ValueVector* argValues); |
| 722 |
[[nodiscard]] bool readCallRef(uint32_t* funcTypeIndex, Value* callee, |
| 723 |
ValueVector* argValues); |
| 724 |
[[nodiscard]] bool readReturnCallRef(uint32_t* funcTypeIndex, Value* callee, |
| 725 |
ValueVector* argValues); |
| 726 |
[[nodiscard]] bool readOldCallDirect(uint32_t numFuncImports, |
| 727 |
uint32_t* funcIndex, |
| 728 |
ValueVector* argValues); |
| 729 |
[[nodiscard]] bool readOldCallIndirect(uint32_t* funcTypeIndex, Value* callee, |
| 730 |
ValueVector* argValues); |
| 731 |
[[nodiscard]] bool readNotify(LinearMemoryAddress<Value>* addr, Value* count); |
| 732 |
[[nodiscard]] bool readWait(LinearMemoryAddress<Value>* addr, |
| 733 |
ValType valueType, uint32_t byteSize, |
| 734 |
Value* value, Value* timeout); |
| 735 |
[[nodiscard]] bool readFence(); |
| 736 |
[[nodiscard]] bool readAtomicLoad(LinearMemoryAddress<Value>* addr, |
| 737 |
ValType resultType, uint32_t byteSize); |
| 738 |
[[nodiscard]] bool readAtomicStore(LinearMemoryAddress<Value>* addr, |
| 739 |
ValType resultType, uint32_t byteSize, |
| 740 |
Value* value); |
| 741 |
[[nodiscard]] bool readAtomicRMW(LinearMemoryAddress<Value>* addr, |
| 742 |
ValType resultType, uint32_t byteSize, |
| 743 |
Value* value); |
| 744 |
[[nodiscard]] bool readAtomicCmpXchg(LinearMemoryAddress<Value>* addr, |
| 745 |
ValType resultType, uint32_t byteSize, |
| 746 |
Value* oldValue, Value* newValue); |
| 747 |
[[nodiscard]] bool readMemOrTableCopy(bool isMem, |
| 748 |
uint32_t* dstMemOrTableIndex, |
| 749 |
Value* dst, |
| 750 |
uint32_t* srcMemOrTableIndex, |
| 751 |
Value* src, Value* len); |
| 752 |
[[nodiscard]] bool readDataOrElemDrop(bool isData, uint32_t* segIndex); |
| 753 |
[[nodiscard]] bool readMemFill(uint32_t* memoryIndex, Value* start, |
| 754 |
Value* val, Value* len); |
| 755 |
[[nodiscard]] bool readMemOrTableInit(bool isMem, uint32_t* segIndex, |
| 756 |
uint32_t* dstMemOrTableIndex, |
| 757 |
Value* dst, Value* src, Value* len); |
| 758 |
[[nodiscard]] bool readTableFill(uint32_t* tableIndex, Value* start, |
| 759 |
Value* val, Value* len); |
| 760 |
[[nodiscard]] bool readMemDiscard(uint32_t* memoryIndex, Value* start, |
| 761 |
Value* len); |
| 762 |
[[nodiscard]] bool readTableGet(uint32_t* tableIndex, Value* address); |
| 763 |
[[nodiscard]] bool readTableGrow(uint32_t* tableIndex, Value* initValue, |
| 764 |
Value* delta); |
| 765 |
[[nodiscard]] bool readTableSet(uint32_t* tableIndex, Value* address, |
| 766 |
Value* value); |
| 767 |
|
| 768 |
[[nodiscard]] bool readTableSize(uint32_t* tableIndex); |
| 769 |
|
| 770 |
[[nodiscard]] bool readStructNew(uint32_t* typeIndex, ValueVector* argValues); |
| 771 |
[[nodiscard]] bool readStructNewDefault(uint32_t* typeIndex); |
| 772 |
[[nodiscard]] bool readStructGet(uint32_t* typeIndex, uint32_t* fieldIndex, |
| 773 |
FieldWideningOp wideningOp, Value* ptr); |
| 774 |
[[nodiscard]] bool readStructSet(uint32_t* typeIndex, uint32_t* fieldIndex, |
| 775 |
Value* ptr, Value* val); |
| 776 |
[[nodiscard]] bool readArrayNew(uint32_t* typeIndex, Value* numElements, |
| 777 |
Value* argValue); |
| 778 |
[[nodiscard]] bool readArrayNewFixed(uint32_t* typeIndex, |
| 779 |
uint32_t* numElements, |
| 780 |
ValueVector* values); |
| 781 |
[[nodiscard]] bool readArrayNewDefault(uint32_t* typeIndex, |
| 782 |
Value* numElements); |
| 783 |
[[nodiscard]] bool readArrayNewData(uint32_t* typeIndex, uint32_t* segIndex, |
| 784 |
Value* offset, Value* numElements); |
| 785 |
[[nodiscard]] bool readArrayNewElem(uint32_t* typeIndex, uint32_t* segIndex, |
| 786 |
Value* offset, Value* numElements); |
| 787 |
[[nodiscard]] bool readArrayInitData(uint32_t* typeIndex, uint32_t* segIndex, |
| 788 |
Value* array, Value* arrayIndex, |
| 789 |
Value* segOffset, Value* length); |
| 790 |
[[nodiscard]] bool readArrayInitElem(uint32_t* typeIndex, uint32_t* segIndex, |
| 791 |
Value* array, Value* arrayIndex, |
| 792 |
Value* segOffset, Value* length); |
| 793 |
[[nodiscard]] bool readArrayGet(uint32_t* typeIndex, |
| 794 |
FieldWideningOp wideningOp, Value* index, |
| 795 |
Value* ptr); |
| 796 |
[[nodiscard]] bool readArraySet(uint32_t* typeIndex, Value* val, Value* index, |
| 797 |
Value* ptr); |
| 798 |
[[nodiscard]] bool readArrayLen(Value* ptr); |
| 799 |
[[nodiscard]] bool readArrayCopy(uint32_t* dstArrayTypeIndex, |
| 800 |
uint32_t* srcArrayTypeIndex, Value* dstArray, |
| 801 |
Value* dstIndex, Value* srcArray, |
| 802 |
Value* srcIndex, Value* numElements); |
| 803 |
[[nodiscard]] bool readArrayFill(uint32_t* typeIndex, Value* array, |
| 804 |
Value* index, Value* val, Value* length); |
| 805 |
[[nodiscard]] bool readRefTest(bool nullable, RefType* sourceType, |
| 806 |
RefType* destType, Value* ref); |
| 807 |
[[nodiscard]] bool readRefCast(bool nullable, RefType* sourceType, |
| 808 |
RefType* destType, Value* ref); |
| 809 |
[[nodiscard]] bool readBrOnCast(bool onSuccess, uint32_t* labelRelativeDepth, |
| 810 |
RefType* sourceType, RefType* destType, |
| 811 |
ResultType* labelType, ValueVector* values); |
| 812 |
[[nodiscard]] bool readRefConversion(RefType operandType, RefType resultType, |
| 813 |
Value* operandValue); |
| 814 |
|
| 815 |
#ifdef ENABLE_WASM_SIMD |
| 816 |
[[nodiscard]] bool readLaneIndex(uint32_t inputLanes, uint32_t* laneIndex); |
| 817 |
[[nodiscard]] bool readExtractLane(ValType resultType, uint32_t inputLanes, |
| 818 |
uint32_t* laneIndex, Value* input); |
| 819 |
[[nodiscard]] bool readReplaceLane(ValType operandType, uint32_t inputLanes, |
| 820 |
uint32_t* laneIndex, Value* baseValue, |
| 821 |
Value* operand); |
| 822 |
[[nodiscard]] bool readVectorShift(Value* baseValue, Value* shift); |
| 823 |
[[nodiscard]] bool readVectorShuffle(Value* v1, Value* v2, V128* selectMask); |
| 824 |
[[nodiscard]] bool readV128Const(V128* value); |
| 825 |
[[nodiscard]] bool readLoadSplat(uint32_t byteSize, |
| 826 |
LinearMemoryAddress<Value>* addr); |
| 827 |
[[nodiscard]] bool readLoadExtend(LinearMemoryAddress<Value>* addr); |
| 828 |
[[nodiscard]] bool readLoadLane(uint32_t byteSize, |
| 829 |
LinearMemoryAddress<Value>* addr, |
| 830 |
uint32_t* laneIndex, Value* input); |
| 831 |
[[nodiscard]] bool readStoreLane(uint32_t byteSize, |
| 832 |
LinearMemoryAddress<Value>* addr, |
| 833 |
uint32_t* laneIndex, Value* input); |
| 834 |
#endif |
| 835 |
|
| 836 |
[[nodiscard]] bool readCallBuiltinModuleFunc( |
| 837 |
const BuiltinModuleFunc** builtinModuleFunc, ValueVector* params); |
| 838 |
|
| 839 |
#ifdef ENABLE_WASM_JSPI |
| 840 |
[[nodiscard]] bool readStackSwitch(StackSwitchKind* kind, Value* suspender, |
| 841 |
Value* fn, Value* data); |
| 842 |
#endif |
| 843 |
|
| 844 |
// At a location where readOp is allowed, peek at the next opcode |
| 845 |
// without consuming it or updating any internal state. |
| 846 |
// Never fails: returns uint16_t(Op::Limit) in op->b0 if it can't read. |
| 847 |
void peekOp(OpBytes* op); |
| 848 |
|
| 849 |
// ------------------------------------------------------------------------ |
| 850 |
// Stack management. |
| 851 |
|
| 852 |
// Set the top N result values. |
| 853 |
void setResults(size_t count, const ValueVector& values) { |
| 854 |
MOZ_ASSERT(valueStack_.length() >= count); |
| 855 |
size_t base = valueStack_.length() - count; |
| 856 |
for (size_t i = 0; i < count; i++) { |
| 857 |
valueStack_[base + i].setValue(values[i]); |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
bool getResults(size_t count, ValueVector* values) { |
| 862 |
MOZ_ASSERT(valueStack_.length() >= count); |
| 863 |
if (!values->resize(count)) { |
| 864 |
return false; |
| 865 |
} |
| 866 |
size_t base = valueStack_.length() - count; |
| 867 |
for (size_t i = 0; i < count; i++) { |
| 868 |
(*values)[i] = valueStack_[base + i].value(); |
| 869 |
} |
| 870 |
return true; |
| 871 |
} |
| 872 |
|
| 873 |
// Set the result value of the current top-of-value-stack expression. |
| 874 |
void setResult(Value value) { valueStack_.back().setValue(value); } |
| 875 |
|
| 876 |
// Return the result value of the current top-of-value-stack expression. |
| 877 |
Value getResult() { return valueStack_.back().value(); } |
| 878 |
|
| 879 |
// Return a reference to the top of the control stack. |
| 880 |
ControlItem& controlItem() { return controlStack_.back().controlItem(); } |
| 881 |
|
| 882 |
// Return a reference to an element in the control stack. |
| 883 |
ControlItem& controlItem(uint32_t relativeDepth) { |
| 884 |
return controlStack_[controlStack_.length() - 1 - relativeDepth] |
| 885 |
.controlItem(); |
| 886 |
} |
| 887 |
|
| 888 |
// Return the LabelKind of an element in the control stack. |
| 889 |
LabelKind controlKind(uint32_t relativeDepth) { |
| 890 |
return controlStack_[controlStack_.length() - 1 - relativeDepth].kind(); |
| 891 |
} |
| 892 |
|
| 893 |
// Return a reference to the outermost element on the control stack. |
| 894 |
ControlItem& controlOutermost() { return controlStack_[0].controlItem(); } |
| 895 |
|
| 896 |
// Test whether the control-stack is empty, meaning we've consumed the final |
| 897 |
// end of the function body. |
| 898 |
bool controlStackEmpty() const { return controlStack_.empty(); } |
| 899 |
|
| 900 |
// Return the depth of the control stack. |
| 901 |
size_t controlStackDepth() const { return controlStack_.length(); } |
| 902 |
|
| 903 |
// Find the innermost control item matching a predicate, starting to search |
| 904 |
// from a certain relative depth, and returning true if such innermost |
| 905 |
// control item is found. The relative depth of the found item is returned |
| 906 |
// via a parameter. |
| 907 |
template <typename Predicate> |
| 908 |
bool controlFindInnermostFrom(Predicate predicate, uint32_t fromRelativeDepth, |
| 909 |
uint32_t* foundRelativeDepth) const { |
| 910 |
int32_t fromAbsoluteDepth = controlStack_.length() - fromRelativeDepth - 1; |
| 911 |
for (int32_t i = fromAbsoluteDepth; i >= 0; i--) { |
| 912 |
if (predicate(controlStack_[i].kind(), controlStack_[i].controlItem())) { |
| 913 |
*foundRelativeDepth = controlStack_.length() - 1 - i; |
| 914 |
return true; |
| 915 |
} |
| 916 |
} |
| 917 |
return false; |
| 918 |
} |
| 919 |
}; |
| 920 |
|
| 921 |
template <typename Policy> |
| 922 |
inline bool OpIter<Policy>::checkIsSubtypeOf(StorageType subType, |
| 923 |
StorageType superType) { |
| 924 |
return CheckIsSubtypeOf(d_, codeMeta_, lastOpcodeOffset(), subType, |
| 925 |
superType); |
| 926 |
} |
| 927 |
|
| 928 |
template <typename Policy> |
| 929 |
inline bool OpIter<Policy>::checkIsSubtypeOf(ResultType params, |
| 930 |
ResultType results) { |
| 931 |
return CheckIsSubtypeOf(d_, codeMeta_, lastOpcodeOffset(), params, results); |
| 932 |
} |
| 933 |
|
| 934 |
template <typename Policy> |
| 935 |
inline bool OpIter<Policy>::checkIsSubtypeOf(uint32_t actualTypeIndex, |
| 936 |
uint32_t expectedTypeIndex) { |
| 937 |
const TypeDef& actualTypeDef = codeMeta_.types->type(actualTypeIndex); |
| 938 |
const TypeDef& expectedTypeDef = codeMeta_.types->type(expectedTypeIndex); |
| 939 |
return CheckIsSubtypeOf( |
| 940 |
d_, codeMeta_, lastOpcodeOffset(), |
| 941 |
ValType(RefType::fromTypeDef(&actualTypeDef, true)), |
| 942 |
ValType(RefType::fromTypeDef(&expectedTypeDef, true))); |
| 943 |
} |
| 944 |
|
| 945 |
template <typename Policy> |
| 946 |
inline bool OpIter<Policy>::unrecognizedOpcode(const OpBytes* expr) { |
| 947 |
UniqueChars error(JS_smprintf("unrecognized opcode: %x %x", expr->b0, |
| 948 |
IsPrefixByte(expr->b0) ? expr->b1 : 0)); |
| 949 |
if (!error) { |
| 950 |
return false; |
| 951 |
} |
| 952 |
|
| 953 |
return fail(error.get()); |
| 954 |
} |
| 955 |
|
| 956 |
template <typename Policy> |
| 957 |
inline bool OpIter<Policy>::fail(const char* msg) { |
| 958 |
return d_.fail(lastOpcodeOffset(), msg); |
| 959 |
} |
| 960 |
|
| 961 |
template <typename Policy> |
| 962 |
inline bool OpIter<Policy>::fail_ctx(const char* fmt, const char* context) { |
| 963 |
UniqueChars error(JS_smprintf(fmt, context)); |
| 964 |
if (!error) { |
| 965 |
return false; |
| 966 |
} |
| 967 |
return fail(error.get()); |
| 968 |
} |
| 969 |
|
| 970 |
template <typename Policy> |
| 971 |
inline bool OpIter<Policy>::failEmptyStack() { |
| 972 |
return valueStack_.empty() ? fail("popping value from empty stack") |
| 973 |
: fail("popping value from outside block"); |
| 974 |
} |
| 975 |
|
| 976 |
// This function pops exactly one value from the stack, yielding Bottom types in |
| 977 |
// various cases and therefore making it the caller's responsibility to do the |
| 978 |
// right thing for StackType::Bottom. Prefer (pop|top)WithType. This is an |
| 979 |
// optimization for the super-common case where the caller is statically |
| 980 |
// expecting the resulttype `[valtype]`. |
| 981 |
template <typename Policy> |
| 982 |
inline bool OpIter<Policy>::popStackType(StackType* type, Value* value) { |
| 983 |
Control& block = controlStack_.back(); |
| 984 |
|
| 985 |
MOZ_ASSERT(valueStack_.length() >= block.valueStackBase()); |
| 986 |
if (MOZ_UNLIKELY(valueStack_.length() == block.valueStackBase())) { |
| 987 |
// If the base of this block's stack is polymorphic, then we can pop a |
| 988 |
// dummy value of the bottom type; it won't be used since we're in |
| 989 |
// unreachable code. |
| 990 |
if (block.polymorphicBase()) { |
| 991 |
*type = StackType::bottom(); |
| 992 |
*value = Value(); |
| 993 |
|
| 994 |
// Maintain the invariant that, after a pop, there is always memory |
| 995 |
// reserved to push a value infallibly. |
| 996 |
return valueStack_.reserve(valueStack_.length() + 1); |
| 997 |
} |
| 998 |
|
| 999 |
return failEmptyStack(); |
| 1000 |
} |
| 1001 |
|
| 1002 |
TypeAndValue& tv = valueStack_.back(); |
| 1003 |
*type = tv.type(); |
| 1004 |
*value = tv.value(); |
| 1005 |
valueStack_.popBack(); |
| 1006 |
return true; |
| 1007 |
} |
| 1008 |
|
| 1009 |
// This function pops exactly one value from the stack, checking that it has the |
| 1010 |
// expected type which can either be a specific value type or the bottom type. |
| 1011 |
template <typename Policy> |
| 1012 |
inline bool OpIter<Policy>::popWithType(ValType expectedType, Value* value, |
| 1013 |
StackType* stackType) { |
| 1014 |
if (!popStackType(stackType, value)) { |
| 1015 |
return false; |
| 1016 |
} |
| 1017 |
|
| 1018 |
return stackType->isStackBottom() || |
| 1019 |
checkIsSubtypeOf(stackType->valType(), expectedType); |
| 1020 |
} |
| 1021 |
|
| 1022 |
// This function pops exactly one value from the stack, checking that it has the |
| 1023 |
// expected type which can either be a specific value type or the bottom type. |
| 1024 |
template <typename Policy> |
| 1025 |
inline bool OpIter<Policy>::popWithType(ValType expectedType, Value* value) { |
| 1026 |
StackType stackType; |
| 1027 |
return popWithType(expectedType, value, &stackType); |
| 1028 |
} |
| 1029 |
|
| 1030 |
template <typename Policy> |
| 1031 |
inline bool OpIter<Policy>::popWithType(ResultType expected, |
| 1032 |
ValueVector* values) { |
| 1033 |
return popWithTypes(expected, values); |
| 1034 |
} |
| 1035 |
|
| 1036 |
// Pops each of the given expected types (in reverse, because it's a stack). |
| 1037 |
template <typename Policy> |
| 1038 |
template <typename ValTypeSpanT> |
| 1039 |
inline bool OpIter<Policy>::popWithTypes(ValTypeSpanT expected, |
| 1040 |
ValueVector* values) { |
| 1041 |
size_t expectedLength = expected.size(); |
| 1042 |
if (!values->resize(expectedLength)) { |
| 1043 |
return false; |
| 1044 |
} |
| 1045 |
for (size_t i = 0; i < expectedLength; i++) { |
| 1046 |
size_t reverseIndex = expectedLength - i - 1; |
| 1047 |
ValType expectedType = expected[reverseIndex]; |
| 1048 |
Value* value = &(*values)[reverseIndex]; |
| 1049 |
if (!popWithType(expectedType, value)) { |
| 1050 |
return false; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
return true; |
| 1054 |
} |
| 1055 |
|
| 1056 |
// This function pops exactly one value from the stack, checking that it is a |
| 1057 |
// reference type. |
| 1058 |
template <typename Policy> |
| 1059 |
inline bool OpIter<Policy>::popWithRefType(Value* value, StackType* type) { |
| 1060 |
if (!popStackType(type, value)) { |
| 1061 |
return false; |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (type->isStackBottom() || type->valType().isRefType()) { |
| 1065 |
return true; |
| 1066 |
} |
| 1067 |
|
| 1068 |
UniqueChars actualText = ToString(type->valType(), codeMeta_.types); |
| 1069 |
if (!actualText) { |
| 1070 |
return false; |
| 1071 |
} |
| 1072 |
|
| 1073 |
UniqueChars error(JS_smprintf( |
| 1074 |
"type mismatch: expression has type %s but expected a reference type", |
| 1075 |
actualText.get())); |
| 1076 |
if (!error) { |
| 1077 |
return false; |
| 1078 |
} |
| 1079 |
|
| 1080 |
return fail(error.get()); |
| 1081 |
} |
| 1082 |
|
| 1083 |
template <typename Policy> |
| 1084 |
inline bool OpIter<Policy>::checkTopTypeMatches(ResultType expected, |
| 1085 |
ValueVector* values, |
| 1086 |
bool rewriteStackTypes) { |
| 1087 |
if (expected.empty()) { |
| 1088 |
return true; |
| 1089 |
} |
| 1090 |
|
| 1091 |
Control& block = controlStack_.back(); |
| 1092 |
|
| 1093 |
size_t expectedLength = expected.length(); |
| 1094 |
if (values && !values->resize(expectedLength)) { |
| 1095 |
return false; |
| 1096 |
} |
| 1097 |
|
| 1098 |
for (size_t i = 0; i != expectedLength; i++) { |
| 1099 |
// We're iterating as-if we were popping each expected/actual type one by |
| 1100 |
// one, which means iterating the array of expected results backwards. |
| 1101 |
// The "current" value stack length refers to what the value stack length |
| 1102 |
// would have been if we were popping it. |
| 1103 |
size_t reverseIndex = expectedLength - i - 1; |
| 1104 |
ValType expectedType = expected[reverseIndex]; |
| 1105 |
auto collectValue = [&](const Value& v) { |
| 1106 |
if (values) { |
| 1107 |
(*values)[reverseIndex] = v; |
| 1108 |
} |
| 1109 |
}; |
| 1110 |
|
| 1111 |
size_t currentValueStackLength = valueStack_.length() - i; |
| 1112 |
|
| 1113 |
MOZ_ASSERT(currentValueStackLength >= block.valueStackBase()); |
| 1114 |
if (currentValueStackLength == block.valueStackBase()) { |
| 1115 |
if (!block.polymorphicBase()) { |
| 1116 |
return failEmptyStack(); |
| 1117 |
} |
| 1118 |
|
| 1119 |
// If the base of this block's stack is polymorphic, then we can just |
| 1120 |
// pull out as many fake values as we need to validate, and create dummy |
| 1121 |
// stack entries accordingly; they won't be used since we're in |
| 1122 |
// unreachable code. However, if `rewriteStackTypes` is true, we must |
| 1123 |
// set the types on these new entries to whatever `expected` requires |
| 1124 |
// them to be. |
| 1125 |
TypeAndValue newTandV = |
| 1126 |
rewriteStackTypes ? TypeAndValue(expectedType) : TypeAndValue(); |
| 1127 |
if (!valueStack_.insert(valueStack_.begin() + currentValueStackLength, |
| 1128 |
newTandV)) { |
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
|
| 1132 |
collectValue(Value()); |
| 1133 |
} else { |
| 1134 |
TypeAndValue& observed = valueStack_[currentValueStackLength - 1]; |
| 1135 |
|
| 1136 |
if (observed.type().isStackBottom()) { |
| 1137 |
collectValue(Value()); |
| 1138 |
} else { |
| 1139 |
if (!checkIsSubtypeOf(observed.type().valType(), expectedType)) { |
| 1140 |
return false; |
| 1141 |
} |
| 1142 |
|
| 1143 |
collectValue(observed.value()); |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (rewriteStackTypes) { |
| 1147 |
observed.setType(StackType(expectedType)); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
} |
| 1151 |
return true; |
| 1152 |
} |
| 1153 |
|
| 1154 |
template <typename Policy> |
| 1155 |
inline bool OpIter<Policy>::pushControl(LabelKind kind, BlockType type) { |
| 1156 |
ResultType paramType = type.params(); |
| 1157 |
|
| 1158 |
ValueVector values; |
| 1159 |
if (!checkTopTypeMatches(paramType, &values, /*rewriteStackTypes=*/true)) { |
| 1160 |
return false; |
| 1161 |
} |
| 1162 |
MOZ_ASSERT(valueStack_.length() >= paramType.length()); |
| 1163 |
uint32_t valueStackBase = valueStack_.length() - paramType.length(); |
| 1164 |
return controlStack_.emplaceBack(kind, type, valueStackBase); |
| 1165 |
} |
| 1166 |
|
| 1167 |
template <typename Policy> |
| 1168 |
inline bool OpIter<Policy>::checkStackAtEndOfBlock(ResultType* expectedType, |
| 1169 |
ValueVector* values) { |
| 1170 |
Control& block = controlStack_.back(); |
| 1171 |
*expectedType = block.type().results(); |
| 1172 |
|
| 1173 |
MOZ_ASSERT(valueStack_.length() >= block.valueStackBase()); |
| 1174 |
if (expectedType->length() < valueStack_.length() - block.valueStackBase()) { |
| 1175 |
return fail("unused values not explicitly dropped by end of block"); |
| 1176 |
} |
| 1177 |
|
| 1178 |
return checkTopTypeMatches(*expectedType, values, |
| 1179 |
/*rewriteStackTypes=*/true); |
| 1180 |
} |
| 1181 |
|
| 1182 |
template <typename Policy> |
| 1183 |
inline bool OpIter<Policy>::getControl(uint32_t relativeDepth, |
| 1184 |
Control** controlEntry) { |
| 1185 |
if (relativeDepth >= controlStack_.length()) { |
| 1186 |
return fail("branch depth exceeds current nesting level"); |
| 1187 |
} |
| 1188 |
|
| 1189 |
*controlEntry = &controlStack_[controlStack_.length() - 1 - relativeDepth]; |
| 1190 |
return true; |
| 1191 |
} |
| 1192 |
|
| 1193 |
template <typename Policy> |
| 1194 |
inline bool OpIter<Policy>::readBlockType(BlockType* type) { |
| 1195 |
uint8_t nextByte; |
| 1196 |
if (!d_.peekByte(&nextByte)) { |
| 1197 |
return fail("unable to read block type"); |
| 1198 |
} |
| 1199 |
|
| 1200 |
if (nextByte == uint8_t(TypeCode::BlockVoid)) { |
| 1201 |
d_.uncheckedReadFixedU8(); |
| 1202 |
*type = BlockType::VoidToVoid(); |
| 1203 |
return true; |
| 1204 |
} |
| 1205 |
|
| 1206 |
if ((nextByte & SLEB128SignMask) == SLEB128SignBit) { |
| 1207 |
ValType v; |
| 1208 |
if (!readValType(&v)) { |
| 1209 |
return false; |
| 1210 |
} |
| 1211 |
*type = BlockType::VoidToSingle(v); |
| 1212 |
return true; |
| 1213 |
} |
| 1214 |
|
| 1215 |
int32_t x; |
| 1216 |
if (!d_.readVarS32(&x) || x < 0 || uint32_t(x) >= codeMeta_.types->length()) { |
| 1217 |
return fail("invalid block type type index"); |
| 1218 |
} |
| 1219 |
|
| 1220 |
const TypeDef* typeDef = &codeMeta_.types->type(x); |
| 1221 |
if (!typeDef->isFuncType()) { |
| 1222 |
return fail("block type type index must be func type"); |
| 1223 |
} |
| 1224 |
|
| 1225 |
*type = BlockType::Func(typeDef->funcType()); |
| 1226 |
|
| 1227 |
return true; |
| 1228 |
} |
| 1229 |
|
| 1230 |
template <typename Policy> |
| 1231 |
inline bool OpIter<Policy>::readOp(OpBytes* op) { |
| 1232 |
MOZ_ASSERT(!controlStack_.empty()); |
| 1233 |
|
| 1234 |
offsetOfLastReadOp_ = d_.currentOffset(); |
| 1235 |
|
| 1236 |
if (MOZ_UNLIKELY(!d_.readOp(op))) { |
| 1237 |
return fail("unable to read opcode"); |
| 1238 |
} |
| 1239 |
|
| 1240 |
#ifdef DEBUG |
| 1241 |
op_ = *op; |
| 1242 |
#endif |
| 1243 |
|
| 1244 |
return true; |
| 1245 |
} |
| 1246 |
|
| 1247 |
template <typename Policy> |
| 1248 |
inline void OpIter<Policy>::peekOp(OpBytes* op) { |
| 1249 |
const uint8_t* pos = d_.currentPosition(); |
| 1250 |
|
| 1251 |
if (MOZ_UNLIKELY(!d_.readOp(op))) { |
| 1252 |
op->b0 = uint16_t(Op::Limit); |
| 1253 |
} |
| 1254 |
|
| 1255 |
d_.rollbackPosition(pos); |
| 1256 |
} |
| 1257 |
|
| 1258 |
template <typename Policy> |
| 1259 |
inline bool OpIter<Policy>::startFunction(uint32_t funcIndex) { |
| 1260 |
MOZ_ASSERT(kind_ == OpIter::Func); |
| 1261 |
MOZ_ASSERT(elseParamStack_.empty()); |
| 1262 |
MOZ_ASSERT(valueStack_.empty()); |
| 1263 |
MOZ_ASSERT(controlStack_.empty()); |
| 1264 |
MOZ_ASSERT(op_.b0 == uint16_t(Op::Limit)); |
| 1265 |
BlockType type = BlockType::FuncResults(codeMeta_.getFuncType(funcIndex)); |
| 1266 |
|
| 1267 |
// Initialize information related to branch hinting. |
| 1268 |
lastBranchHintIndex_ = 0; |
| 1269 |
if (codeMeta_.branchHintingEnabled()) { |
| 1270 |
branchHintVector_ = &codeMeta_.branchHints.getHintVector(funcIndex); |
| 1271 |
} |
| 1272 |
|
| 1273 |
size_t numArgs = codeMeta_.getFuncType(funcIndex).args().length(); |
| 1274 |
if (!unsetLocals_.init(locals_, numArgs)) { |
| 1275 |
return false; |
| 1276 |
} |
| 1277 |
|
| 1278 |
return pushControl(LabelKind::Body, type); |
| 1279 |
} |
| 1280 |
|
| 1281 |
template <typename Policy> |
| 1282 |
inline bool OpIter<Policy>::endFunction(const uint8_t* bodyEnd) { |
| 1283 |
if (d_.currentPosition() != bodyEnd) { |
| 1284 |
return fail("function body length mismatch"); |
| 1285 |
} |
| 1286 |
|
| 1287 |
if (!controlStack_.empty()) { |
| 1288 |
return fail("unbalanced function body control flow"); |
| 1289 |
} |
| 1290 |
MOZ_ASSERT(elseParamStack_.empty()); |
| 1291 |
MOZ_ASSERT(unsetLocals_.empty()); |
| 1292 |
|
| 1293 |
#ifdef DEBUG |
| 1294 |
op_ = OpBytes(Op::Limit); |
| 1295 |
#endif |
| 1296 |
valueStack_.clear(); |
| 1297 |
return true; |
| 1298 |
} |
| 1299 |
|
| 1300 |
template <typename Policy> |
| 1301 |
inline bool OpIter<Policy>::startInitExpr(ValType expected) { |
| 1302 |
MOZ_ASSERT(kind_ == OpIter::InitExpr); |
| 1303 |
MOZ_ASSERT(locals_.length() == 0); |
| 1304 |
MOZ_ASSERT(elseParamStack_.empty()); |
| 1305 |
MOZ_ASSERT(valueStack_.empty()); |
| 1306 |
MOZ_ASSERT(controlStack_.empty()); |
| 1307 |
MOZ_ASSERT(op_.b0 == uint16_t(Op::Limit)); |
| 1308 |
lastBranchHintIndex_ = 0; |
| 1309 |
|
| 1310 |
BlockType type = BlockType::VoidToSingle(expected); |
| 1311 |
return pushControl(LabelKind::Body, type); |
| 1312 |
} |
| 1313 |
|
| 1314 |
template <typename Policy> |
| 1315 |
inline bool OpIter<Policy>::endInitExpr() { |
| 1316 |
MOZ_ASSERT(controlStack_.empty()); |
| 1317 |
MOZ_ASSERT(elseParamStack_.empty()); |
| 1318 |
|
| 1319 |
#ifdef DEBUG |
| 1320 |
op_ = OpBytes(Op::Limit); |
| 1321 |
#endif |
| 1322 |
valueStack_.clear(); |
| 1323 |
return true; |
| 1324 |
} |
| 1325 |
|
| 1326 |
template <typename Policy> |
| 1327 |
inline bool OpIter<Policy>::readValType(ValType* type) { |
| 1328 |
return d_.readValType(*codeMeta_.types, codeMeta_.features(), type); |
| 1329 |
} |
| 1330 |
|
| 1331 |
template <typename Policy> |
| 1332 |
inline bool OpIter<Policy>::readHeapType(bool nullable, RefType* type) { |
| 1333 |
return d_.readHeapType(*codeMeta_.types, codeMeta_.features(), nullable, |
| 1334 |
type); |
| 1335 |
} |
| 1336 |
|
| 1337 |
template <typename Policy> |
| 1338 |
inline bool OpIter<Policy>::readReturn(ValueVector* values) { |
| 1339 |
MOZ_ASSERT(Classify(op_) == OpKind::Return); |
| 1340 |
|
| 1341 |
Control& body = controlStack_[0]; |
| 1342 |
MOZ_ASSERT(body.kind() == LabelKind::Body); |
| 1343 |
|
| 1344 |
if (!popWithType(body.resultType(), values)) { |
| 1345 |
return false; |
| 1346 |
} |
| 1347 |
|
| 1348 |
afterUnconditionalBranch(); |
| 1349 |
return true; |
| 1350 |
} |
| 1351 |
|
| 1352 |
template <typename Policy> |
| 1353 |
inline bool OpIter<Policy>::readBlock(BlockType* type) { |
| 1354 |
MOZ_ASSERT(Classify(op_) == OpKind::Block); |
| 1355 |
|
| 1356 |
if (!readBlockType(type)) { |
| 1357 |
return false; |
| 1358 |
} |
| 1359 |
|
| 1360 |
return pushControl(LabelKind::Block, *type); |
| 1361 |
} |
| 1362 |
|
| 1363 |
template <typename Policy> |
| 1364 |
inline bool OpIter<Policy>::readLoop(BlockType* type) { |
| 1365 |
MOZ_ASSERT(Classify(op_) == OpKind::Loop); |
| 1366 |
|
| 1367 |
if (!readBlockType(type)) { |
| 1368 |
return false; |
| 1369 |
} |
| 1370 |
|
| 1371 |
return pushControl(LabelKind::Loop, *type); |
| 1372 |
} |
| 1373 |
|
| 1374 |
template <typename Policy> |
| 1375 |
inline bool OpIter<Policy>::readIf(BlockType* type, Value* condition) { |
| 1376 |
MOZ_ASSERT(Classify(op_) == OpKind::If); |
| 1377 |
|
| 1378 |
if (!readBlockType(type)) { |
| 1379 |
return false; |
| 1380 |
} |
| 1381 |
|
| 1382 |
if (!popWithType(ValType::I32, condition)) { |
| 1383 |
return false; |
| 1384 |
} |
| 1385 |
|
| 1386 |
if (!pushControl(LabelKind::Then, *type)) { |
| 1387 |
return false; |
| 1388 |
} |
| 1389 |
|
| 1390 |
size_t paramsLength = type->params().length(); |
| 1391 |
return elseParamStack_.append(valueStack_.end() - paramsLength, paramsLength); |
| 1392 |
} |
| 1393 |
|
| 1394 |
template <typename Policy> |
| 1395 |
inline bool OpIter<Policy>::readElse(ResultType* paramType, |
| 1396 |
ResultType* resultType, |
| 1397 |
ValueVector* thenResults) { |
| 1398 |
MOZ_ASSERT(Classify(op_) == OpKind::Else); |
| 1399 |
|
| 1400 |
Control& block = controlStack_.back(); |
| 1401 |
if (block.kind() != LabelKind::Then) { |
| 1402 |
return fail("else can only be used within an if"); |
| 1403 |
} |
| 1404 |
|
| 1405 |
*paramType = block.type().params(); |
| 1406 |
if (!checkStackAtEndOfBlock(resultType, thenResults)) { |
| 1407 |
return false; |
| 1408 |
} |
| 1409 |
|
| 1410 |
valueStack_.shrinkTo(block.valueStackBase()); |
| 1411 |
|
| 1412 |
size_t nparams = block.type().params().length(); |
| 1413 |
MOZ_ASSERT(elseParamStack_.length() >= nparams); |
| 1414 |
valueStack_.infallibleAppend(elseParamStack_.end() - nparams, nparams); |
| 1415 |
elseParamStack_.shrinkBy(nparams); |
| 1416 |
|
| 1417 |
// Reset local state to the beginning of the 'if' block for the new block |
| 1418 |
// started by 'else'. |
| 1419 |
unsetLocals_.resetToBlock(controlStack_.length() - 1); |
| 1420 |
|
| 1421 |
block.switchToElse(); |
| 1422 |
return true; |
| 1423 |
} |
| 1424 |
|
| 1425 |
template <typename Policy> |
| 1426 |
inline bool OpIter<Policy>::readEnd(LabelKind* kind, ResultType* type, |
| 1427 |
ValueVector* results, |
| 1428 |
ValueVector* resultsForEmptyElse) { |
| 1429 |
MOZ_ASSERT(Classify(op_) == OpKind::End); |
| 1430 |
|
| 1431 |
Control& block = controlStack_.back(); |
| 1432 |
|
| 1433 |
if (!checkStackAtEndOfBlock(type, results)) { |
| 1434 |
return false; |
| 1435 |
} |
| 1436 |
|
| 1437 |
if (block.kind() == LabelKind::Then) { |
| 1438 |
ResultType params = block.type().params(); |
| 1439 |
// If an `if` block ends with `end` instead of `else`, then the `else` block |
| 1440 |
// implicitly passes the `if` parameters as the `else` results. In that |
| 1441 |
// case, assert that the `if`'s param type matches the result type. |
| 1442 |
if (!checkIsSubtypeOf(params, block.type().results())) { |
| 1443 |
return fail( |
| 1444 |
"the parameters to an if without an else must be compatible with the " |
| 1445 |
"if's result type"); |
| 1446 |
} |
| 1447 |
|
| 1448 |
size_t nparams = params.length(); |
| 1449 |
MOZ_ASSERT(elseParamStack_.length() >= nparams); |
| 1450 |
if (!resultsForEmptyElse->resize(nparams)) { |
| 1451 |
return false; |
| 1452 |
} |
| 1453 |
const TypeAndValue* elseParams = elseParamStack_.end() - nparams; |
| 1454 |
for (size_t i = 0; i < nparams; i++) { |
| 1455 |
(*resultsForEmptyElse)[i] = elseParams[i].value(); |
| 1456 |
} |
| 1457 |
elseParamStack_.shrinkBy(nparams); |
| 1458 |
} |
| 1459 |
|
| 1460 |
*kind = block.kind(); |
| 1461 |
return true; |
| 1462 |
} |
| 1463 |
|
| 1464 |
template <typename Policy> |
| 1465 |
inline void OpIter<Policy>::popEnd() { |
| 1466 |
MOZ_ASSERT(Classify(op_) == OpKind::End); |
| 1467 |
|
| 1468 |
controlStack_.popBack(); |
| 1469 |
unsetLocals_.resetToBlock(controlStack_.length()); |
| 1470 |
} |
| 1471 |
|
| 1472 |
template <typename Policy> |
| 1473 |
inline bool OpIter<Policy>::checkBranchValueAndPush(uint32_t relativeDepth, |
| 1474 |
ResultType* type, |
| 1475 |
ValueVector* values, |
| 1476 |
bool rewriteStackTypes) { |
| 1477 |
Control* block = nullptr; |
| 1478 |
if (!getControl(relativeDepth, &block)) { |
| 1479 |
return false; |
| 1480 |
} |
| 1481 |
|
| 1482 |
*type = block->branchTargetType(); |
| 1483 |
return checkTopTypeMatches(*type, values, rewriteStackTypes); |
| 1484 |
} |
| 1485 |
|
| 1486 |
template <typename Policy> |
| 1487 |
inline bool OpIter<Policy>::readBr(uint32_t* relativeDepth, ResultType* type, |
| 1488 |
ValueVector* values) { |
| 1489 |
MOZ_ASSERT(Classify(op_) == OpKind::Br); |
| 1490 |
|
| 1491 |
if (!readVarU32(relativeDepth)) { |
| 1492 |
return fail("unable to read br depth"); |
| 1493 |
} |
| 1494 |
|
| 1495 |
if (!checkBranchValueAndPush(*relativeDepth, type, values, |
| 1496 |
/*rewriteStackTypes=*/false)) { |
| 1497 |
return false; |
| 1498 |
} |
| 1499 |
|
| 1500 |
afterUnconditionalBranch(); |
| 1501 |
return true; |
| 1502 |
} |
| 1503 |
|
| 1504 |
template <typename Policy> |
| 1505 |
inline bool OpIter<Policy>::readBrIf(uint32_t* relativeDepth, ResultType* type, |
| 1506 |
ValueVector* values, Value* condition) { |
| 1507 |
MOZ_ASSERT(Classify(op_) == OpKind::BrIf); |
| 1508 |
|
| 1509 |
if (!readVarU32(relativeDepth)) { |
| 1510 |
return fail("unable to read br_if depth"); |
| 1511 |
} |
| 1512 |
|
| 1513 |
if (!popWithType(ValType::I32, condition)) { |
| 1514 |
return false; |
| 1515 |
} |
| 1516 |
|
| 1517 |
return checkBranchValueAndPush(*relativeDepth, type, values, |
| 1518 |
/*rewriteStackTypes=*/true); |
| 1519 |
} |
| 1520 |
|
| 1521 |
#define UNKNOWN_ARITY UINT32_MAX |
| 1522 |
|
| 1523 |
template <typename Policy> |
| 1524 |
inline bool OpIter<Policy>::checkBrTableEntryAndPush( |
| 1525 |
uint32_t* relativeDepth, ResultType prevBranchType, ResultType* type, |
| 1526 |
ValueVector* branchValues) { |
| 1527 |
if (!readVarU32(relativeDepth)) { |
| 1528 |
return fail("unable to read br_table depth"); |
| 1529 |
} |
| 1530 |
|
| 1531 |
Control* block = nullptr; |
| 1532 |
if (!getControl(*relativeDepth, &block)) { |
| 1533 |
return false; |
| 1534 |
} |
| 1535 |
|
| 1536 |
*type = block->branchTargetType(); |
| 1537 |
|
| 1538 |
if (prevBranchType.valid()) { |
| 1539 |
if (prevBranchType.length() != type->length()) { |
| 1540 |
return fail("br_table targets must all have the same arity"); |
| 1541 |
} |
| 1542 |
|
| 1543 |
// Avoid re-collecting the same values for subsequent branch targets. |
| 1544 |
branchValues = nullptr; |
| 1545 |
} |
| 1546 |
|
| 1547 |
return checkTopTypeMatches(*type, branchValues, /*rewriteStackTypes=*/false); |
| 1548 |
} |
| 1549 |
|
| 1550 |
template <typename Policy> |
| 1551 |
inline bool OpIter<Policy>::readBrTable(Uint32Vector* depths, |
| 1552 |
uint32_t* defaultDepth, |
| 1553 |
ResultType* defaultBranchType, |
| 1554 |
ValueVector* branchValues, |
| 1555 |
Value* index) { |
| 1556 |
MOZ_ASSERT(Classify(op_) == OpKind::BrTable); |
| 1557 |
|
| 1558 |
uint32_t tableLength; |
| 1559 |
if (!readVarU32(&tableLength)) { |
| 1560 |
return fail("unable to read br_table table length"); |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (tableLength > MaxBrTableElems) { |
| 1564 |
return fail("br_table too big"); |
| 1565 |
} |
| 1566 |
|
| 1567 |
if (!popWithType(ValType::I32, index)) { |
| 1568 |
return false; |
| 1569 |
} |
| 1570 |
|
| 1571 |
if (!depths->resize(tableLength)) { |
| 1572 |
return false; |
| 1573 |
} |
| 1574 |
|
| 1575 |
ResultType prevBranchType; |
| 1576 |
for (uint32_t i = 0; i < tableLength; i++) { |
| 1577 |
ResultType branchType; |
| 1578 |
if (!checkBrTableEntryAndPush(&(*depths)[i], prevBranchType, &branchType, |
| 1579 |
branchValues)) { |
| 1580 |
return false; |
| 1581 |
} |
| 1582 |
prevBranchType = branchType; |
| 1583 |
} |
| 1584 |
|
| 1585 |
if (!checkBrTableEntryAndPush(defaultDepth, prevBranchType, defaultBranchType, |
| 1586 |
branchValues)) { |
| 1587 |
return false; |
| 1588 |
} |
| 1589 |
|
| 1590 |
MOZ_ASSERT(defaultBranchType->valid()); |
| 1591 |
|
| 1592 |
afterUnconditionalBranch(); |
| 1593 |
return true; |
| 1594 |
} |
| 1595 |
|
| 1596 |
#undef UNKNOWN_ARITY |
| 1597 |
|
| 1598 |
template <typename Policy> |
| 1599 |
inline bool OpIter<Policy>::readTry(BlockType* type) { |
| 1600 |
MOZ_ASSERT(Classify(op_) == OpKind::Try); |
| 1601 |
featureUsage_ |= FeatureUsage::LegacyExceptions; |
| 1602 |
|
| 1603 |
if (!readBlockType(type)) { |
| 1604 |
return false; |
| 1605 |
} |
| 1606 |
|
| 1607 |
return pushControl(LabelKind::Try, *type); |
| 1608 |
} |
| 1609 |
|
| 1610 |
enum class TryTableCatchFlags : uint8_t { |
| 1611 |
CaptureExnRef = 0x1, |
| 1612 |
CatchAll = 0x1 << 1, |
| 1613 |
AllowedMask = uint8_t(CaptureExnRef) | uint8_t(CatchAll), |
| 1614 |
}; |
| 1615 |
|
| 1616 |
template <typename Policy> |
| 1617 |
inline bool OpIter<Policy>::readTryTable(BlockType* type, |
| 1618 |
TryTableCatchVector* catches) { |
| 1619 |
MOZ_ASSERT(Classify(op_) == OpKind::TryTable); |
| 1620 |
|
| 1621 |
if (!readBlockType(type)) { |
| 1622 |
return false; |
| 1623 |
} |
| 1624 |
|
| 1625 |
if (!pushControl(LabelKind::TryTable, *type)) { |
| 1626 |
return false; |
| 1627 |
} |
| 1628 |
|
| 1629 |
uint32_t catchesLength; |
| 1630 |
if (!readVarU32(&catchesLength)) { |
| 1631 |
return fail("failed to read catches length"); |
| 1632 |
} |
| 1633 |
|
| 1634 |
if (catchesLength > MaxTryTableCatches) { |
| 1635 |
return fail("too many catches"); |
| 1636 |
} |
| 1637 |
|
| 1638 |
if (!catches->reserve(catchesLength)) { |
| 1639 |
return false; |
| 1640 |
} |
| 1641 |
|
| 1642 |
for (uint32_t i = 0; i < catchesLength; i++) { |
| 1643 |
TryTableCatch tryTableCatch; |
| 1644 |
|
| 1645 |
// Decode the flags |
| 1646 |
uint8_t flags; |
| 1647 |
if (!readFixedU8(&flags)) { |
| 1648 |
return fail("expected flags"); |
| 1649 |
} |
| 1650 |
if ((flags & ~uint8_t(TryTableCatchFlags::AllowedMask)) != 0) { |
| 1651 |
return fail("invalid try_table catch flags"); |
| 1652 |
} |
| 1653 |
|
| 1654 |
// Decode if this catch wants to capture an exnref |
| 1655 |
tryTableCatch.captureExnRef = |
| 1656 |
(flags & uint8_t(TryTableCatchFlags::CaptureExnRef)) != 0; |
| 1657 |
|
| 1658 |
// Decode the tag, if any |
| 1659 |
if ((flags & uint8_t(TryTableCatchFlags::CatchAll)) != 0) { |
| 1660 |
tryTableCatch.tagIndex = CatchAllIndex; |
| 1661 |
} else { |
| 1662 |
if (!readVarU32(&tryTableCatch.tagIndex)) { |
| 1663 |
return fail("expected tag index"); |
| 1664 |
} |
| 1665 |
if (tryTableCatch.tagIndex >= codeMeta_.tags.length()) { |
| 1666 |
return fail("tag index out of range"); |
| 1667 |
} |
| 1668 |
} |
| 1669 |
|
| 1670 |
// Decode the target branch and construct the type we need to compare |
| 1671 |
// against the branch |
| 1672 |
if (!readVarU32(&tryTableCatch.labelRelativeDepth)) { |
| 1673 |
return fail("unable to read catch depth"); |
| 1674 |
} |
| 1675 |
|
| 1676 |
// The target branch depth is relative to the control labels outside of |
| 1677 |
// this try_table. e.g. `0` is a branch to the control outside of this |
| 1678 |
// try_table, not to the try_table itself. However, we've already pushed |
| 1679 |
// the control block for the try_table, and users will read it after we've |
| 1680 |
// returned, so we need to return the relative depth adjusted by 1 to |
| 1681 |
// account for our own control block. |
| 1682 |
if (tryTableCatch.labelRelativeDepth == UINT32_MAX) { |
| 1683 |
return fail("catch depth out of range"); |
| 1684 |
} |
| 1685 |
tryTableCatch.labelRelativeDepth += 1; |
| 1686 |
|
| 1687 |
// Tagged catches will unpack the exception package and pass it to the |
| 1688 |
// branch |
| 1689 |
if (tryTableCatch.tagIndex != CatchAllIndex) { |
| 1690 |
const TagType& tagType = *codeMeta_.tags[tryTableCatch.tagIndex].type; |
| 1691 |
ResultType tagResult = tagType.resultType(); |
| 1692 |
if (!tagResult.cloneToVector(&tryTableCatch.labelType)) { |
| 1693 |
return false; |
| 1694 |
} |
| 1695 |
} |
| 1696 |
|
| 1697 |
// Any captured exnref is the final parameter |
| 1698 |
if (tryTableCatch.captureExnRef && |
| 1699 |
!tryTableCatch.labelType.append(ValType(RefType::exn().asNonNullable()))) { |
| 1700 |
return false; |
| 1701 |
} |
| 1702 |
|
| 1703 |
Control* block; |
| 1704 |
if (!getControl(tryTableCatch.labelRelativeDepth, &block)) { |
| 1705 |
return false; |
| 1706 |
} |
| 1707 |
|
| 1708 |
ResultType blockTargetType = block->branchTargetType(); |
| 1709 |
if (!checkIsSubtypeOf(ResultType::Vector(tryTableCatch.labelType), |
| 1710 |
blockTargetType)) { |
| 1711 |
return false; |
| 1712 |
} |
| 1713 |
|
| 1714 |
catches->infallibleAppend(std::move(tryTableCatch)); |
| 1715 |
} |
| 1716 |
|
| 1717 |
return true; |
| 1718 |
} |
| 1719 |
|
| 1720 |
template <typename Policy> |
| 1721 |
inline bool OpIter<Policy>::readCatch(LabelKind* kind, uint32_t* tagIndex, |
| 1722 |
ResultType* paramType, |
| 1723 |
ResultType* resultType, |
| 1724 |
ValueVector* tryResults) { |
| 1725 |
MOZ_ASSERT(Classify(op_) == OpKind::Catch); |
| 1726 |
|
| 1727 |
if (!readVarU32(tagIndex)) { |
| 1728 |
return fail("expected tag index"); |
| 1729 |
} |
| 1730 |
if (*tagIndex >= codeMeta_.tags.length()) { |
| 1731 |
return fail("tag index out of range"); |
| 1732 |
} |
| 1733 |
|
| 1734 |
Control& block = controlStack_.back(); |
| 1735 |
if (block.kind() == LabelKind::CatchAll) { |
| 1736 |
return fail("catch cannot follow a catch_all"); |
| 1737 |
} |
| 1738 |
if (block.kind() != LabelKind::Try && block.kind() != LabelKind::Catch) { |
| 1739 |
return fail("catch can only be used within a try-catch"); |
| 1740 |
} |
| 1741 |
*kind = block.kind(); |
| 1742 |
*paramType = block.type().params(); |
| 1743 |
|
| 1744 |
if (!checkStackAtEndOfBlock(resultType, tryResults)) { |
| 1745 |
return false; |
| 1746 |
} |
| 1747 |
|
| 1748 |
valueStack_.shrinkTo(block.valueStackBase()); |
| 1749 |
block.switchToCatch(); |
| 1750 |
// Reset local state to the beginning of the 'try' block. |
| 1751 |
unsetLocals_.resetToBlock(controlStack_.length() - 1); |
| 1752 |
|
| 1753 |
return push(codeMeta_.tags[*tagIndex].type->resultType()); |
| 1754 |
} |
| 1755 |
|
| 1756 |
template <typename Policy> |
| 1757 |
inline bool OpIter<Policy>::readCatchAll(LabelKind* kind, ResultType* paramType, |
| 1758 |
ResultType* resultType, |
| 1759 |
ValueVector* tryResults) { |
| 1760 |
MOZ_ASSERT(Classify(op_) == OpKind::CatchAll); |
| 1761 |
|
| 1762 |
Control& block = controlStack_.back(); |
| 1763 |
if (block.kind() != LabelKind::Try && block.kind() != LabelKind::Catch) { |
| 1764 |
return fail("catch_all can only be used within a try-catch"); |
| 1765 |
} |
| 1766 |
*kind = block.kind(); |
| 1767 |
*paramType = block.type().params(); |
| 1768 |
|
| 1769 |
if (!checkStackAtEndOfBlock(resultType, tryResults)) { |
| 1770 |
return false; |
| 1771 |
} |
| 1772 |
|
| 1773 |
valueStack_.shrinkTo(block.valueStackBase()); |
| 1774 |
block.switchToCatchAll(); |
| 1775 |
// Reset local state to the beginning of the 'try' block. |
| 1776 |
unsetLocals_.resetToBlock(controlStack_.length() - 1); |
| 1777 |
return true; |
| 1778 |
} |
| 1779 |
|
| 1780 |
template <typename Policy> |
| 1781 |
inline bool OpIter<Policy>::readDelegate(uint32_t* relativeDepth, |
| 1782 |
ResultType* resultType, |
| 1783 |
ValueVector* tryResults) { |
| 1784 |
MOZ_ASSERT(Classify(op_) == OpKind::Delegate); |
| 1785 |
|
| 1786 |
Control& block = controlStack_.back(); |
| 1787 |
if (block.kind() != LabelKind::Try) { |
| 1788 |
return fail("delegate can only be used within a try"); |
| 1789 |
} |
| 1790 |
|
| 1791 |
uint32_t delegateDepth; |
| 1792 |
if (!readVarU32(&delegateDepth)) { |
| 1793 |
return fail("unable to read delegate depth"); |
| 1794 |
} |
| 1795 |
|
| 1796 |
// Depths for delegate start counting in the surrounding block. |
| 1797 |
if (delegateDepth >= controlStack_.length() - 1) { |
| 1798 |
return fail("delegate depth exceeds current nesting level"); |
| 1799 |
} |
| 1800 |
*relativeDepth = delegateDepth + 1; |
| 1801 |
|
| 1802 |
// Because `delegate` acts like `end` and ends the block, we will check |
| 1803 |
// the stack here. |
| 1804 |
return checkStackAtEndOfBlock(resultType, tryResults); |
| 1805 |
} |
| 1806 |
|
| 1807 |
// We need popDelegate because readDelegate cannot pop the control stack |
| 1808 |
// itself, as its caller may need to use the control item for delegate. |
| 1809 |
template <typename Policy> |
| 1810 |
inline void OpIter<Policy>::popDelegate() { |
| 1811 |
MOZ_ASSERT(Classify(op_) == OpKind::Delegate); |
| 1812 |
|
| 1813 |
controlStack_.popBack(); |
| 1814 |
unsetLocals_.resetToBlock(controlStack_.length()); |
| 1815 |
} |
| 1816 |
|
| 1817 |
template <typename Policy> |
| 1818 |
inline bool OpIter<Policy>::readThrow(uint32_t* tagIndex, |
| 1819 |
ValueVector* argValues) { |
| 1820 |
MOZ_ASSERT(Classify(op_) == OpKind::Throw); |
| 1821 |
|
| 1822 |
if (!readVarU32(tagIndex)) { |
| 1823 |
return fail("expected tag index"); |
| 1824 |
} |
| 1825 |
if (*tagIndex >= codeMeta_.tags.length()) { |
| 1826 |
return fail("tag index out of range"); |
| 1827 |
} |
| 1828 |
|
| 1829 |
if (!popWithType(codeMeta_.tags[*tagIndex].type->resultType(), argValues)) { |
| 1830 |
return false; |
| 1831 |
} |
| 1832 |
|
| 1833 |
afterUnconditionalBranch(); |
| 1834 |
return true; |
| 1835 |
} |
| 1836 |
|
| 1837 |
template <typename Policy> |
| 1838 |
inline bool OpIter<Policy>::readThrowRef(Value* exnRef) { |
| 1839 |
MOZ_ASSERT(Classify(op_) == OpKind::ThrowRef); |
| 1840 |
|
| 1841 |
if (!popWithType(ValType(RefType::exn()), exnRef)) { |
| 1842 |
return false; |
| 1843 |
} |
| 1844 |
|
| 1845 |
afterUnconditionalBranch(); |
| 1846 |
return true; |
| 1847 |
} |
| 1848 |
|
| 1849 |
template <typename Policy> |
| 1850 |
inline bool OpIter<Policy>::readRethrow(uint32_t* relativeDepth) { |
| 1851 |
MOZ_ASSERT(Classify(op_) == OpKind::Rethrow); |
| 1852 |
|
| 1853 |
if (!readVarU32(relativeDepth)) { |
| 1854 |
return fail("unable to read rethrow depth"); |
| 1855 |
} |
| 1856 |
|
| 1857 |
if (*relativeDepth >= controlStack_.length()) { |
| 1858 |
return fail("rethrow depth exceeds current nesting level"); |
| 1859 |
} |
| 1860 |
LabelKind kind = controlKind(*relativeDepth); |
| 1861 |
if (kind != LabelKind::Catch && kind != LabelKind::CatchAll) { |
| 1862 |
return fail("rethrow target was not a catch block"); |
| 1863 |
} |
| 1864 |
|
| 1865 |
afterUnconditionalBranch(); |
| 1866 |
return true; |
| 1867 |
} |
| 1868 |
|
| 1869 |
template <typename Policy> |
| 1870 |
inline bool OpIter<Policy>::readUnreachable() { |
| 1871 |
MOZ_ASSERT(Classify(op_) == OpKind::Unreachable); |
| 1872 |
|
| 1873 |
afterUnconditionalBranch(); |
| 1874 |
return true; |
| 1875 |
} |
| 1876 |
|
| 1877 |
template <typename Policy> |
| 1878 |
inline bool OpIter<Policy>::readDrop() { |
| 1879 |
MOZ_ASSERT(Classify(op_) == OpKind::Drop); |
| 1880 |
StackType type; |
| 1881 |
Value value; |
| 1882 |
return popStackType(&type, &value); |
| 1883 |
} |
| 1884 |
|
| 1885 |
template <typename Policy> |
| 1886 |
inline bool OpIter<Policy>::readUnary(ValType operandType, Value* input) { |
| 1887 |
MOZ_ASSERT(Classify(op_) == OpKind::Unary); |
| 1888 |
|
| 1889 |
if (!popWithType(operandType, input)) { |
| 1890 |
return false; |
| 1891 |
} |
| 1892 |
|
| 1893 |
infalliblePush(operandType); |
| 1894 |
|
| 1895 |
return true; |
| 1896 |
} |
| 1897 |
|
| 1898 |
template <typename Policy> |
| 1899 |
inline bool OpIter<Policy>::readConversion(ValType operandType, |
| 1900 |
ValType resultType, Value* input) { |
| 1901 |
MOZ_ASSERT(Classify(op_) == OpKind::Conversion); |
| 1902 |
|
| 1903 |
if (!popWithType(operandType, input)) { |
| 1904 |
return false; |
| 1905 |
} |
| 1906 |
|
| 1907 |
infalliblePush(resultType); |
| 1908 |
|
| 1909 |
return true; |
| 1910 |
} |
| 1911 |
|
| 1912 |
template <typename Policy> |
| 1913 |
inline bool OpIter<Policy>::readBinary(ValType operandType, Value* lhs, |
| 1914 |
Value* rhs) { |
| 1915 |
MOZ_ASSERT(Classify(op_) == OpKind::Binary); |
| 1916 |
|
| 1917 |
if (!popWithType(operandType, rhs)) { |
| 1918 |
return false; |
| 1919 |
} |
| 1920 |
|
| 1921 |
if (!popWithType(operandType, lhs)) { |
| 1922 |
return false; |
| 1923 |
} |
| 1924 |
|
| 1925 |
infalliblePush(operandType); |
| 1926 |
|
| 1927 |
return true; |
| 1928 |
} |
| 1929 |
|
| 1930 |
template <typename Policy> |
| 1931 |
inline bool OpIter<Policy>::readComparison(ValType operandType, Value* lhs, |
| 1932 |
Value* rhs) { |
| 1933 |
MOZ_ASSERT(Classify(op_) == OpKind::Comparison); |
| 1934 |
|
| 1935 |
if (!popWithType(operandType, rhs)) { |
| 1936 |
return false; |
| 1937 |
} |
| 1938 |
|
| 1939 |
if (!popWithType(operandType, lhs)) { |
| 1940 |
return false; |
| 1941 |
} |
| 1942 |
|
| 1943 |
infalliblePush(ValType::I32); |
| 1944 |
|
| 1945 |
return true; |
| 1946 |
} |
| 1947 |
|
| 1948 |
template <typename Policy> |
| 1949 |
inline bool OpIter<Policy>::readTernary(ValType operandType, Value* v0, |
| 1950 |
Value* v1, Value* v2) { |
| 1951 |
MOZ_ASSERT(Classify(op_) == OpKind::Ternary); |
| 1952 |
|
| 1953 |
if (!popWithType(operandType, v2)) { |
| 1954 |
return false; |
| 1955 |
} |
| 1956 |
|
| 1957 |
if (!popWithType(operandType, v1)) { |
| 1958 |
return false; |
| 1959 |
} |
| 1960 |
|
| 1961 |
if (!popWithType(operandType, v0)) { |
| 1962 |
return false; |
| 1963 |
} |
| 1964 |
|
| 1965 |
infalliblePush(operandType); |
| 1966 |
|
| 1967 |
return true; |
| 1968 |
} |
| 1969 |
|
| 1970 |
template <typename Policy> |
| 1971 |
inline bool OpIter<Policy>::readLinearMemoryAddress( |
| 1972 |
uint32_t byteSize, LinearMemoryAddress<Value>* addr) { |
| 1973 |
uint32_t flags; |
| 1974 |
if (!readVarU32(&flags)) { |
| 1975 |
return fail("unable to read load alignment"); |
| 1976 |
} |
| 1977 |
|
| 1978 |
uint8_t alignLog2 = flags & ((1 << 6) - 1); |
| 1979 |
uint8_t hasMemoryIndex = flags & (1 << 6); |
| 1980 |
uint8_t undefinedBits = flags & ~((1 << 7) - 1); |
| 1981 |
|
| 1982 |
if (undefinedBits != 0) { |
| 1983 |
return fail("invalid memory flags"); |
| 1984 |
} |
| 1985 |
|
| 1986 |
if (hasMemoryIndex != 0) { |
| 1987 |
if (!readVarU32(&addr->memoryIndex)) { |
| 1988 |
return fail("unable to read memory index"); |
| 1989 |
} |
| 1990 |
} else { |
| 1991 |
addr->memoryIndex = 0; |
| 1992 |
} |
| 1993 |
|
| 1994 |
if (addr->memoryIndex >= codeMeta_.numMemories()) { |
| 1995 |
return fail("memory index out of range"); |
| 1996 |
} |
| 1997 |
|
| 1998 |
if (!readVarU64(&addr->offset)) { |
| 1999 |
return fail("unable to read load offset"); |
| 2000 |
} |
| 2001 |
|
| 2002 |
AddressType at = codeMeta_.memories[addr->memoryIndex].addressType(); |
| 2003 |
if (at == AddressType::I32 && addr->offset > UINT32_MAX) { |
| 2004 |
return fail("offset too large for memory type"); |
| 2005 |
} |
| 2006 |
|
| 2007 |
if (alignLog2 >= 32 || (uint32_t(1) << alignLog2) > byteSize) { |
| 2008 |
return fail("greater than natural alignment"); |
| 2009 |
} |
| 2010 |
|
| 2011 |
if (!popWithType(ToValType(at), &addr->base)) { |
| 2012 |
return false; |
| 2013 |
} |
| 2014 |
|
| 2015 |
addr->align = uint32_t(1) << alignLog2; |
| 2016 |
return true; |
| 2017 |
} |
| 2018 |
|
| 2019 |
template <typename Policy> |
| 2020 |
inline bool OpIter<Policy>::readLinearMemoryAddressAligned( |
| 2021 |
uint32_t byteSize, LinearMemoryAddress<Value>* addr) { |
| 2022 |
if (!readLinearMemoryAddress(byteSize, addr)) { |
| 2023 |
return false; |
| 2024 |
} |
| 2025 |
|
| 2026 |
if (addr->align != byteSize) { |
| 2027 |
return fail("not natural alignment"); |
| 2028 |
} |
| 2029 |
|
| 2030 |
return true; |
| 2031 |
} |
| 2032 |
|
| 2033 |
template <typename Policy> |
| 2034 |
inline bool OpIter<Policy>::readLoad(ValType resultType, uint32_t byteSize, |
| 2035 |
LinearMemoryAddress<Value>* addr) { |
| 2036 |
MOZ_ASSERT(Classify(op_) == OpKind::Load); |
| 2037 |
|
| 2038 |
if (!readLinearMemoryAddress(byteSize, addr)) { |
| 2039 |
return false; |
| 2040 |
} |
| 2041 |
|
| 2042 |
infalliblePush(resultType); |
| 2043 |
|
| 2044 |
return true; |
| 2045 |
} |
| 2046 |
|
| 2047 |
template <typename Policy> |
| 2048 |
inline bool OpIter<Policy>::readStore(ValType resultType, uint32_t byteSize, |
| 2049 |
LinearMemoryAddress<Value>* addr, |
| 2050 |
Value* value) { |
| 2051 |
MOZ_ASSERT(Classify(op_) == OpKind::Store); |
| 2052 |
|
| 2053 |
if (!popWithType(resultType, value)) { |
| 2054 |
return false; |
| 2055 |
} |
| 2056 |
|
| 2057 |
return readLinearMemoryAddress(byteSize, addr); |
| 2058 |
} |
| 2059 |
|
| 2060 |
template <typename Policy> |
| 2061 |
inline bool OpIter<Policy>::readTeeStore(ValType resultType, uint32_t byteSize, |
| 2062 |
LinearMemoryAddress<Value>* addr, |
| 2063 |
Value* value) { |
| 2064 |
MOZ_ASSERT(Classify(op_) == OpKind::TeeStore); |
| 2065 |
|
| 2066 |
if (!popWithType(resultType, value)) { |
| 2067 |
return false; |
| 2068 |
} |
| 2069 |
|
| 2070 |
if (!readLinearMemoryAddress(byteSize, addr)) { |
| 2071 |
return false; |
| 2072 |
} |
| 2073 |
|
| 2074 |
infalliblePush(TypeAndValue(resultType, *value)); |
| 2075 |
return true; |
| 2076 |
} |
| 2077 |
|
| 2078 |
template <typename Policy> |
| 2079 |
inline bool OpIter<Policy>::readNop() { |
| 2080 |
MOZ_ASSERT(Classify(op_) == OpKind::Nop); |
| 2081 |
|
| 2082 |
return true; |
| 2083 |
} |
| 2084 |
|
| 2085 |
template <typename Policy> |
| 2086 |
inline bool OpIter<Policy>::readMemorySize(uint32_t* memoryIndex) { |
| 2087 |
MOZ_ASSERT(Classify(op_) == OpKind::MemorySize); |
| 2088 |
|
| 2089 |
if (!readVarU32(memoryIndex)) { |
| 2090 |
return fail("failed to read memory flags"); |
| 2091 |
} |
| 2092 |
|
| 2093 |
if (*memoryIndex >= codeMeta_.numMemories()) { |
| 2094 |
return fail("memory index out of range for memory.size"); |
| 2095 |
} |
| 2096 |
|
| 2097 |
ValType ptrType = ToValType(codeMeta_.memories[*memoryIndex].addressType()); |
| 2098 |
return push(ptrType); |
| 2099 |
} |
| 2100 |
|
| 2101 |
template <typename Policy> |
| 2102 |
inline bool OpIter<Policy>::readMemoryGrow(uint32_t* memoryIndex, |
| 2103 |
Value* input) { |
| 2104 |
MOZ_ASSERT(Classify(op_) == OpKind::MemoryGrow); |
| 2105 |
|
| 2106 |
if (!readVarU32(memoryIndex)) { |
| 2107 |
return fail("failed to read memory flags"); |
| 2108 |
} |
| 2109 |
|
| 2110 |
if (*memoryIndex >= codeMeta_.numMemories()) { |
| 2111 |
return fail("memory index out of range for memory.grow"); |
| 2112 |
} |
| 2113 |
|
| 2114 |
ValType ptrType = ToValType(codeMeta_.memories[*memoryIndex].addressType()); |
| 2115 |
if (!popWithType(ptrType, input)) { |
| 2116 |
return false; |
| 2117 |
} |
| 2118 |
|
| 2119 |
infalliblePush(ptrType); |
| 2120 |
|
| 2121 |
return true; |
| 2122 |
} |
| 2123 |
|
| 2124 |
template <typename Policy> |
| 2125 |
inline bool OpIter<Policy>::readSelect(bool typed, StackType* type, |
| 2126 |
Value* trueValue, Value* falseValue, |
| 2127 |
Value* condition) { |
| 2128 |
MOZ_ASSERT(Classify(op_) == OpKind::Select); |
| 2129 |
|
| 2130 |
if (typed) { |
| 2131 |
uint32_t length; |
| 2132 |
if (!readVarU32(&length)) { |
| 2133 |
return fail("unable to read select result length"); |
| 2134 |
} |
| 2135 |
if (length != 1) { |
| 2136 |
return fail("bad number of results"); |
| 2137 |
} |
| 2138 |
ValType result; |
| 2139 |
if (!readValType(&result)) { |
| 2140 |
return fail("invalid result type for select"); |
| 2141 |
} |
| 2142 |
|
| 2143 |
if (!popWithType(ValType::I32, condition)) { |
| 2144 |
return false; |
| 2145 |
} |
| 2146 |
if (!popWithType(result, falseValue)) { |
| 2147 |
return false; |
| 2148 |
} |
| 2149 |
if (!popWithType(result, trueValue)) { |
| 2150 |
return false; |
| 2151 |
} |
| 2152 |
|
| 2153 |
*type = StackType(result); |
| 2154 |
infalliblePush(*type); |
| 2155 |
return true; |
| 2156 |
} |
| 2157 |
|
| 2158 |
if (!popWithType(ValType::I32, condition)) { |
| 2159 |
return false; |
| 2160 |
} |
| 2161 |
|
| 2162 |
StackType falseType; |
| 2163 |
if (!popStackType(&falseType, falseValue)) { |
| 2164 |
return false; |
| 2165 |
} |
| 2166 |
|
| 2167 |
StackType trueType; |
| 2168 |
if (!popStackType(&trueType, trueValue)) { |
| 2169 |
return false; |
| 2170 |
} |
| 2171 |
|
| 2172 |
if (!falseType.isValidForUntypedSelect() || |
| 2173 |
!trueType.isValidForUntypedSelect()) { |
| 2174 |
return fail("invalid types for untyped select"); |
| 2175 |
} |
| 2176 |
|
| 2177 |
if (falseType.isStackBottom()) { |
| 2178 |
*type = trueType; |
| 2179 |
} else if (trueType.isStackBottom() || falseType == trueType) { |
| 2180 |
*type = falseType; |
| 2181 |
} else { |
| 2182 |
return fail("select operand types must match"); |
| 2183 |
} |
| 2184 |
|
| 2185 |
infalliblePush(*type); |
| 2186 |
return true; |
| 2187 |
} |
| 2188 |
|
| 2189 |
template <typename Policy> |
| 2190 |
inline bool OpIter<Policy>::readGetLocal(uint32_t* id) { |
| 2191 |
MOZ_ASSERT(Classify(op_) == OpKind::GetLocal); |
| 2192 |
|
| 2193 |
if (!readVarU32(id)) { |
| 2194 |
return fail("unable to read local index"); |
| 2195 |
} |
| 2196 |
|
| 2197 |
if (*id >= locals_.length()) { |
| 2198 |
return fail("local.get index out of range"); |
| 2199 |
} |
| 2200 |
|
| 2201 |
if (unsetLocals_.isUnset(*id)) { |
| 2202 |
return fail("local.get read from unset local"); |
| 2203 |
} |
| 2204 |
|
| 2205 |
return push(locals_[*id]); |
| 2206 |
} |
| 2207 |
|
| 2208 |
template <typename Policy> |
| 2209 |
inline bool OpIter<Policy>::readSetLocal(uint32_t* id, Value* value) { |
| 2210 |
MOZ_ASSERT(Classify(op_) == OpKind::SetLocal); |
| 2211 |
|
| 2212 |
if (!readVarU32(id)) { |
| 2213 |
return fail("unable to read local index"); |
| 2214 |
} |
| 2215 |
|
| 2216 |
if (*id >= locals_.length()) { |
| 2217 |
return fail("local.set index out of range"); |
| 2218 |
} |
| 2219 |
|
| 2220 |
if (unsetLocals_.isUnset(*id)) { |
| 2221 |
unsetLocals_.set(*id, controlStackDepth()); |
| 2222 |
} |
| 2223 |
|
| 2224 |
return popWithType(locals_[*id], value); |
| 2225 |
} |
| 2226 |
|
| 2227 |
template <typename Policy> |
| 2228 |
inline bool OpIter<Policy>::readTeeLocal(uint32_t* id, Value* value) { |
| 2229 |
MOZ_ASSERT(Classify(op_) == OpKind::TeeLocal); |
| 2230 |
|
| 2231 |
if (!readVarU32(id)) { |
| 2232 |
return fail("unable to read local index"); |
| 2233 |
} |
| 2234 |
|
| 2235 |
if (*id >= locals_.length()) { |
| 2236 |
return fail("local.set index out of range"); |
| 2237 |
} |
| 2238 |
|
| 2239 |
if (unsetLocals_.isUnset(*id)) { |
| 2240 |
unsetLocals_.set(*id, controlStackDepth()); |
| 2241 |
} |
| 2242 |
|
| 2243 |
ValueVector single; |
| 2244 |
if (!checkTopTypeMatches(ResultType::Single(locals_[*id]), &single, |
| 2245 |
/*rewriteStackTypes=*/true)) { |
| 2246 |
return false; |
| 2247 |
} |
| 2248 |
|
| 2249 |
*value = single[0]; |
| 2250 |
return true; |
| 2251 |
} |
| 2252 |
|
| 2253 |
template <typename Policy> |
| 2254 |
inline bool OpIter<Policy>::readGetGlobal(uint32_t* id) { |
| 2255 |
MOZ_ASSERT(Classify(op_) == OpKind::GetGlobal); |
| 2256 |
|
| 2257 |
if (!d_.readGlobalIndex(id)) { |
| 2258 |
return false; |
| 2259 |
} |
| 2260 |
|
| 2261 |
if (*id >= codeMeta_.globals.length()) { |
| 2262 |
return fail("global.get index out of range"); |
| 2263 |
} |
| 2264 |
|
| 2265 |
// Initializer expressions can only access previously-defined immutable |
| 2266 |
// globals. |
| 2267 |
if (kind_ == OpIter::InitExpr && codeMeta_.globals[*id].isMutable()) { |
| 2268 |
return fail( |
| 2269 |
"global.get in initializer expression must reference a " |
| 2270 |
"previously-defined immutable global"); |
| 2271 |
} |
| 2272 |
|
| 2273 |
return push(codeMeta_.globals[*id].type()); |
| 2274 |
} |
| 2275 |
|
| 2276 |
template <typename Policy> |
| 2277 |
inline bool OpIter<Policy>::readSetGlobal(uint32_t* id, Value* value) { |
| 2278 |
MOZ_ASSERT(Classify(op_) == OpKind::SetGlobal); |
| 2279 |
|
| 2280 |
if (!d_.readGlobalIndex(id)) { |
| 2281 |
return false; |
| 2282 |
} |
| 2283 |
|
| 2284 |
if (*id >= codeMeta_.globals.length()) { |
| 2285 |
return fail("global.set index out of range"); |
| 2286 |
} |
| 2287 |
|
| 2288 |
if (!codeMeta_.globals[*id].isMutable()) { |
| 2289 |
return fail("can't write an immutable global"); |
| 2290 |
} |
| 2291 |
|
| 2292 |
return popWithType(codeMeta_.globals[*id].type(), value); |
| 2293 |
} |
| 2294 |
|
| 2295 |
template <typename Policy> |
| 2296 |
inline bool OpIter<Policy>::readTeeGlobal(uint32_t* id, Value* value) { |
| 2297 |
MOZ_ASSERT(Classify(op_) == OpKind::TeeGlobal); |
| 2298 |
|
| 2299 |
if (!d_.readGlobalIndex(id)) { |
| 2300 |
return false; |
| 2301 |
} |
| 2302 |
|
| 2303 |
if (*id >= codeMeta_.globals.length()) { |
| 2304 |
return fail("global.set index out of range"); |
| 2305 |
} |
| 2306 |
|
| 2307 |
if (!codeMeta_.globals[*id].isMutable()) { |
| 2308 |
return fail("can't write an immutable global"); |
| 2309 |
} |
| 2310 |
|
| 2311 |
ValueVector single; |
| 2312 |
if (!checkTopTypeMatches(ResultType::Single(codeMeta_.globals[*id].type()), |
| 2313 |
&single, |
| 2314 |
/*rewriteStackTypes=*/true)) { |
| 2315 |
return false; |
| 2316 |
} |
| 2317 |
|
| 2318 |
MOZ_ASSERT(single.length() == 1); |
| 2319 |
*value = single[0]; |
| 2320 |
return true; |
| 2321 |
} |
| 2322 |
|
| 2323 |
template <typename Policy> |
| 2324 |
inline bool OpIter<Policy>::readI32Const(int32_t* i32) { |
| 2325 |
MOZ_ASSERT(Classify(op_) == OpKind::I32Const); |
| 2326 |
|
| 2327 |
if (!d_.readI32Const(i32)) { |
| 2328 |
return false; |
| 2329 |
} |
| 2330 |
|
| 2331 |
return push(ValType::I32); |
| 2332 |
} |
| 2333 |
|
| 2334 |
template <typename Policy> |
| 2335 |
inline bool OpIter<Policy>::readI64Const(int64_t* i64) { |
| 2336 |
MOZ_ASSERT(Classify(op_) == OpKind::I64Const); |
| 2337 |
|
| 2338 |
if (!d_.readI64Const(i64)) { |
| 2339 |
return false; |
| 2340 |
} |
| 2341 |
|
| 2342 |
return push(ValType::I64); |
| 2343 |
} |
| 2344 |
|
| 2345 |
template <typename Policy> |
| 2346 |
inline bool OpIter<Policy>::readF32Const(float* f32) { |
| 2347 |
MOZ_ASSERT(Classify(op_) == OpKind::F32Const); |
| 2348 |
|
| 2349 |
if (!d_.readF32Const(f32)) { |
| 2350 |
return false; |
| 2351 |
} |
| 2352 |
|
| 2353 |
return push(ValType::F32); |
| 2354 |
} |
| 2355 |
|
| 2356 |
template <typename Policy> |
| 2357 |
inline bool OpIter<Policy>::readF64Const(double* f64) { |
| 2358 |
MOZ_ASSERT(Classify(op_) == OpKind::F64Const); |
| 2359 |
|
| 2360 |
if (!d_.readF64Const(f64)) { |
| 2361 |
return false; |
| 2362 |
} |
| 2363 |
|
| 2364 |
return push(ValType::F64); |
| 2365 |
} |
| 2366 |
|
| 2367 |
template <typename Policy> |
| 2368 |
inline bool OpIter<Policy>::readRefFunc(uint32_t* funcIndex) { |
| 2369 |
MOZ_ASSERT(Classify(op_) == OpKind::RefFunc); |
| 2370 |
|
| 2371 |
if (!d_.readFuncIndex(funcIndex)) { |
| 2372 |
return false; |
| 2373 |
} |
| 2374 |
if (*funcIndex >= codeMeta_.funcs.length()) { |
| 2375 |
return fail("function index out of range"); |
| 2376 |
} |
| 2377 |
if (kind_ == OpIter::Func && !codeMeta_.funcs[*funcIndex].canRefFunc()) { |
| 2378 |
return fail( |
| 2379 |
"function index is not declared in a section before the code section"); |
| 2380 |
} |
| 2381 |
|
| 2382 |
const uint32_t typeIndex = codeMeta_.funcs[*funcIndex].typeIndex; |
| 2383 |
const TypeDef& typeDef = codeMeta_.types->type(typeIndex); |
| 2384 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 2385 |
} |
| 2386 |
|
| 2387 |
template <typename Policy> |
| 2388 |
inline bool OpIter<Policy>::readRefNull(RefType* type) { |
| 2389 |
MOZ_ASSERT(Classify(op_) == OpKind::RefNull); |
| 2390 |
|
| 2391 |
if (!d_.readRefNull(*codeMeta_.types, codeMeta_.features(), type)) { |
| 2392 |
return false; |
| 2393 |
} |
| 2394 |
return push(*type); |
| 2395 |
} |
| 2396 |
|
| 2397 |
template <typename Policy> |
| 2398 |
inline bool OpIter<Policy>::readRefIsNull(Value* input) { |
| 2399 |
MOZ_ASSERT(Classify(op_) == OpKind::RefIsNull); |
| 2400 |
|
| 2401 |
StackType type; |
| 2402 |
if (!popWithRefType(input, &type)) { |
| 2403 |
return false; |
| 2404 |
} |
| 2405 |
return push(ValType::I32); |
| 2406 |
} |
| 2407 |
|
| 2408 |
template <typename Policy> |
| 2409 |
inline bool OpIter<Policy>::readRefAsNonNull(Value* input) { |
| 2410 |
MOZ_ASSERT(Classify(op_) == OpKind::RefAsNonNull); |
| 2411 |
|
| 2412 |
StackType type; |
| 2413 |
if (!popWithRefType(input, &type)) { |
| 2414 |
return false; |
| 2415 |
} |
| 2416 |
|
| 2417 |
if (type.isStackBottom()) { |
| 2418 |
infalliblePush(type); |
| 2419 |
} else { |
| 2420 |
infalliblePush(TypeAndValue(type.asNonNullable(), *input)); |
| 2421 |
} |
| 2422 |
return true; |
| 2423 |
} |
| 2424 |
|
| 2425 |
template <typename Policy> |
| 2426 |
inline bool OpIter<Policy>::readBrOnNull(uint32_t* relativeDepth, |
| 2427 |
ResultType* type, ValueVector* values, |
| 2428 |
Value* condition) { |
| 2429 |
MOZ_ASSERT(Classify(op_) == OpKind::BrOnNull); |
| 2430 |
|
| 2431 |
if (!readVarU32(relativeDepth)) { |
| 2432 |
return fail("unable to read br_on_null depth"); |
| 2433 |
} |
| 2434 |
|
| 2435 |
StackType refType; |
| 2436 |
if (!popWithRefType(condition, &refType)) { |
| 2437 |
return false; |
| 2438 |
} |
| 2439 |
|
| 2440 |
if (!checkBranchValueAndPush(*relativeDepth, type, values, |
| 2441 |
/*rewriteStackTypes=*/true)) { |
| 2442 |
return false; |
| 2443 |
} |
| 2444 |
|
| 2445 |
if (refType.isStackBottom()) { |
| 2446 |
return push(refType); |
| 2447 |
} |
| 2448 |
return push(TypeAndValue(refType.asNonNullable(), *condition)); |
| 2449 |
} |
| 2450 |
|
| 2451 |
template <typename Policy> |
| 2452 |
inline bool OpIter<Policy>::readBrOnNonNull(uint32_t* relativeDepth, |
| 2453 |
ResultType* type, |
| 2454 |
ValueVector* values, |
| 2455 |
Value* condition) { |
| 2456 |
MOZ_ASSERT(Classify(op_) == OpKind::BrOnNonNull); |
| 2457 |
|
| 2458 |
if (!readVarU32(relativeDepth)) { |
| 2459 |
return fail("unable to read br_on_non_null depth"); |
| 2460 |
} |
| 2461 |
|
| 2462 |
Control* block = nullptr; |
| 2463 |
if (!getControl(*relativeDepth, &block)) { |
| 2464 |
return false; |
| 2465 |
} |
| 2466 |
|
| 2467 |
*type = block->branchTargetType(); |
| 2468 |
|
| 2469 |
// Check we at least have one type in the branch target type. |
| 2470 |
if (type->length() < 1) { |
| 2471 |
return fail("type mismatch: target block type expected to be [_, ref]"); |
| 2472 |
} |
| 2473 |
|
| 2474 |
// Pop the condition reference. |
| 2475 |
StackType refType; |
| 2476 |
if (!popWithRefType(condition, &refType)) { |
| 2477 |
return false; |
| 2478 |
} |
| 2479 |
|
| 2480 |
// Push non-nullable version of condition reference on the stack, prior |
| 2481 |
// checking the target type below. |
| 2482 |
if (!(refType.isStackBottom() |
| 2483 |
? push(refType) |
| 2484 |
: push(TypeAndValue(refType.asNonNullable(), *condition)))) { |
| 2485 |
return false; |
| 2486 |
} |
| 2487 |
|
| 2488 |
// Check if the type stack matches the branch target type. |
| 2489 |
if (!checkTopTypeMatches(*type, values, /*rewriteStackTypes=*/true)) { |
| 2490 |
return false; |
| 2491 |
} |
| 2492 |
|
| 2493 |
// Pop the condition reference -- the null-branch does not receive the value. |
| 2494 |
StackType unusedType; |
| 2495 |
Value unusedValue; |
| 2496 |
return popStackType(&unusedType, &unusedValue); |
| 2497 |
} |
| 2498 |
|
| 2499 |
template <typename Policy> |
| 2500 |
inline bool OpIter<Policy>::popCallArgs(const ValTypeVector& expectedTypes, |
| 2501 |
ValueVector* values) { |
| 2502 |
// Iterate through the argument types backward so that pops occur in the |
| 2503 |
// right order. |
| 2504 |
|
| 2505 |
if (!values->resize(expectedTypes.length())) { |
| 2506 |
return false; |
| 2507 |
} |
| 2508 |
|
| 2509 |
for (int32_t i = int32_t(expectedTypes.length()) - 1; i >= 0; i--) { |
| 2510 |
if (!popWithType(expectedTypes[i], &(*values)[i])) { |
| 2511 |
return false; |
| 2512 |
} |
| 2513 |
} |
| 2514 |
|
| 2515 |
return true; |
| 2516 |
} |
| 2517 |
|
| 2518 |
template <typename Policy> |
| 2519 |
inline bool OpIter<Policy>::readCall(uint32_t* funcIndex, |
| 2520 |
ValueVector* argValues) { |
| 2521 |
MOZ_ASSERT(Classify(op_) == OpKind::Call); |
| 2522 |
|
| 2523 |
if (!readVarU32(funcIndex)) { |
| 2524 |
return fail("unable to read call function index"); |
| 2525 |
} |
| 2526 |
|
| 2527 |
if (*funcIndex >= codeMeta_.funcs.length()) { |
| 2528 |
return fail("callee index out of range"); |
| 2529 |
} |
| 2530 |
|
| 2531 |
const FuncType& funcType = codeMeta_.getFuncType(*funcIndex); |
| 2532 |
|
| 2533 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2534 |
return false; |
| 2535 |
} |
| 2536 |
|
| 2537 |
return push(ResultType::Vector(funcType.results())); |
| 2538 |
} |
| 2539 |
|
| 2540 |
template <typename Policy> |
| 2541 |
inline bool OpIter<Policy>::readReturnCall(uint32_t* funcIndex, |
| 2542 |
ValueVector* argValues) { |
| 2543 |
MOZ_ASSERT(Classify(op_) == OpKind::ReturnCall); |
| 2544 |
|
| 2545 |
featureUsage_ |= FeatureUsage::ReturnCall; |
| 2546 |
|
| 2547 |
if (!readVarU32(funcIndex)) { |
| 2548 |
return fail("unable to read call function index"); |
| 2549 |
} |
| 2550 |
|
| 2551 |
if (*funcIndex >= codeMeta_.funcs.length()) { |
| 2552 |
return fail("callee index out of range"); |
| 2553 |
} |
| 2554 |
|
| 2555 |
const FuncType& funcType = codeMeta_.getFuncType(*funcIndex); |
| 2556 |
|
| 2557 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2558 |
return false; |
| 2559 |
} |
| 2560 |
|
| 2561 |
// Check if callee results are subtypes of caller's. |
| 2562 |
Control& body = controlStack_[0]; |
| 2563 |
MOZ_ASSERT(body.kind() == LabelKind::Body); |
| 2564 |
if (!checkIsSubtypeOf(ResultType::Vector(funcType.results()), |
| 2565 |
body.resultType())) { |
| 2566 |
return false; |
| 2567 |
} |
| 2568 |
|
| 2569 |
afterUnconditionalBranch(); |
| 2570 |
return true; |
| 2571 |
} |
| 2572 |
|
| 2573 |
template <typename Policy> |
| 2574 |
inline bool OpIter<Policy>::readCallIndirect(uint32_t* funcTypeIndex, |
| 2575 |
uint32_t* tableIndex, |
| 2576 |
Value* callee, |
| 2577 |
ValueVector* argValues) { |
| 2578 |
MOZ_ASSERT(Classify(op_) == OpKind::CallIndirect); |
| 2579 |
MOZ_ASSERT(funcTypeIndex != tableIndex); |
| 2580 |
|
| 2581 |
if (!readVarU32(funcTypeIndex)) { |
| 2582 |
return fail("unable to read call_indirect signature index"); |
| 2583 |
} |
| 2584 |
|
| 2585 |
if (*funcTypeIndex >= codeMeta_.numTypes()) { |
| 2586 |
return fail("signature index out of range"); |
| 2587 |
} |
| 2588 |
|
| 2589 |
if (!readVarU32(tableIndex)) { |
| 2590 |
return fail("unable to read call_indirect table index"); |
| 2591 |
} |
| 2592 |
if (*tableIndex >= codeMeta_.tables.length()) { |
| 2593 |
// Special case this for improved user experience. |
| 2594 |
if (!codeMeta_.tables.length()) { |
| 2595 |
return fail("can't call_indirect without a table"); |
| 2596 |
} |
| 2597 |
return fail("table index out of range for call_indirect"); |
| 2598 |
} |
| 2599 |
if (!codeMeta_.tables[*tableIndex].elemType.isFuncHierarchy()) { |
| 2600 |
return fail("indirect calls must go through a table of 'funcref'"); |
| 2601 |
} |
| 2602 |
|
| 2603 |
if (!popWithType(ToValType(codeMeta_.tables[*tableIndex].addressType()), |
| 2604 |
callee)) { |
| 2605 |
return false; |
| 2606 |
} |
| 2607 |
|
| 2608 |
const TypeDef& typeDef = codeMeta_.types->type(*funcTypeIndex); |
| 2609 |
if (!typeDef.isFuncType()) { |
| 2610 |
return fail("expected signature type"); |
| 2611 |
} |
| 2612 |
const FuncType& funcType = typeDef.funcType(); |
| 2613 |
|
| 2614 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2615 |
return false; |
| 2616 |
} |
| 2617 |
|
| 2618 |
return push(ResultType::Vector(funcType.results())); |
| 2619 |
} |
| 2620 |
|
| 2621 |
template <typename Policy> |
| 2622 |
inline bool OpIter<Policy>::readReturnCallIndirect(uint32_t* funcTypeIndex, |
| 2623 |
uint32_t* tableIndex, |
| 2624 |
Value* callee, |
| 2625 |
ValueVector* argValues) { |
| 2626 |
MOZ_ASSERT(Classify(op_) == OpKind::ReturnCallIndirect); |
| 2627 |
MOZ_ASSERT(funcTypeIndex != tableIndex); |
| 2628 |
|
| 2629 |
featureUsage_ |= FeatureUsage::ReturnCall; |
| 2630 |
|
| 2631 |
if (!readVarU32(funcTypeIndex)) { |
| 2632 |
return fail("unable to read return_call_indirect signature index"); |
| 2633 |
} |
| 2634 |
if (*funcTypeIndex >= codeMeta_.numTypes()) { |
| 2635 |
return fail("signature index out of range"); |
| 2636 |
} |
| 2637 |
|
| 2638 |
if (!readVarU32(tableIndex)) { |
| 2639 |
return fail("unable to read return_call_indirect table index"); |
| 2640 |
} |
| 2641 |
if (*tableIndex >= codeMeta_.tables.length()) { |
| 2642 |
// Special case this for improved user experience. |
| 2643 |
if (!codeMeta_.tables.length()) { |
| 2644 |
return fail("can't return_call_indirect without a table"); |
| 2645 |
} |
| 2646 |
return fail("table index out of range for return_call_indirect"); |
| 2647 |
} |
| 2648 |
if (!codeMeta_.tables[*tableIndex].elemType.isFuncHierarchy()) { |
| 2649 |
return fail("indirect calls must go through a table of 'funcref'"); |
| 2650 |
} |
| 2651 |
|
| 2652 |
if (!popWithType(ToValType(codeMeta_.tables[*tableIndex].addressType()), |
| 2653 |
callee)) { |
| 2654 |
return false; |
| 2655 |
} |
| 2656 |
|
| 2657 |
const TypeDef& typeDef = codeMeta_.types->type(*funcTypeIndex); |
| 2658 |
if (!typeDef.isFuncType()) { |
| 2659 |
return fail("expected signature type"); |
| 2660 |
} |
| 2661 |
const FuncType& funcType = typeDef.funcType(); |
| 2662 |
|
| 2663 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2664 |
return false; |
| 2665 |
} |
| 2666 |
|
| 2667 |
// Check if callee results are subtypes of caller's. |
| 2668 |
Control& body = controlStack_[0]; |
| 2669 |
MOZ_ASSERT(body.kind() == LabelKind::Body); |
| 2670 |
if (!checkIsSubtypeOf(ResultType::Vector(funcType.results()), |
| 2671 |
body.resultType())) { |
| 2672 |
return false; |
| 2673 |
} |
| 2674 |
|
| 2675 |
afterUnconditionalBranch(); |
| 2676 |
return true; |
| 2677 |
} |
| 2678 |
|
| 2679 |
template <typename Policy> |
| 2680 |
inline bool OpIter<Policy>::readCallRef(uint32_t* funcTypeIndex, Value* callee, |
| 2681 |
ValueVector* argValues) { |
| 2682 |
MOZ_ASSERT(Classify(op_) == OpKind::CallRef); |
| 2683 |
|
| 2684 |
if (!readFuncTypeIndex(funcTypeIndex)) { |
| 2685 |
return false; |
| 2686 |
} |
| 2687 |
|
| 2688 |
const TypeDef& typeDef = codeMeta_.types->type(*funcTypeIndex); |
| 2689 |
const FuncType& funcType = typeDef.funcType(); |
| 2690 |
|
| 2691 |
if (!popWithType(ValType(RefType::fromTypeDef(&typeDef, true)), callee)) { |
| 2692 |
return false; |
| 2693 |
} |
| 2694 |
|
| 2695 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2696 |
return false; |
| 2697 |
} |
| 2698 |
|
| 2699 |
return push(ResultType::Vector(funcType.results())); |
| 2700 |
} |
| 2701 |
|
| 2702 |
template <typename Policy> |
| 2703 |
inline bool OpIter<Policy>::readReturnCallRef(uint32_t* funcTypeIndex, |
| 2704 |
Value* callee, |
| 2705 |
ValueVector* argValues) { |
| 2706 |
MOZ_ASSERT(Classify(op_) == OpKind::ReturnCallRef); |
| 2707 |
|
| 2708 |
featureUsage_ |= FeatureUsage::ReturnCall; |
| 2709 |
|
| 2710 |
if (!readFuncTypeIndex(funcTypeIndex)) { |
| 2711 |
return false; |
| 2712 |
} |
| 2713 |
|
| 2714 |
const TypeDef& typeDef = codeMeta_.types->type(*funcTypeIndex); |
| 2715 |
const FuncType& funcType = typeDef.funcType(); |
| 2716 |
|
| 2717 |
if (!popWithType(ValType(RefType::fromTypeDef(&typeDef, true)), callee)) { |
| 2718 |
return false; |
| 2719 |
} |
| 2720 |
|
| 2721 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2722 |
return false; |
| 2723 |
} |
| 2724 |
|
| 2725 |
// Check if callee results are subtypes of caller's. |
| 2726 |
Control& body = controlStack_[0]; |
| 2727 |
MOZ_ASSERT(body.kind() == LabelKind::Body); |
| 2728 |
if (!checkIsSubtypeOf(ResultType::Vector(funcType.results()), |
| 2729 |
body.resultType())) { |
| 2730 |
return false; |
| 2731 |
} |
| 2732 |
|
| 2733 |
afterUnconditionalBranch(); |
| 2734 |
return true; |
| 2735 |
} |
| 2736 |
|
| 2737 |
template <typename Policy> |
| 2738 |
inline bool OpIter<Policy>::readOldCallDirect(uint32_t numFuncImports, |
| 2739 |
uint32_t* funcIndex, |
| 2740 |
ValueVector* argValues) { |
| 2741 |
MOZ_ASSERT(Classify(op_) == OpKind::OldCallDirect); |
| 2742 |
|
| 2743 |
uint32_t funcDefIndex; |
| 2744 |
if (!readVarU32(&funcDefIndex)) { |
| 2745 |
return fail("unable to read call function index"); |
| 2746 |
} |
| 2747 |
|
| 2748 |
if (UINT32_MAX - funcDefIndex < numFuncImports) { |
| 2749 |
return fail("callee index out of range"); |
| 2750 |
} |
| 2751 |
|
| 2752 |
*funcIndex = numFuncImports + funcDefIndex; |
| 2753 |
|
| 2754 |
if (*funcIndex >= codeMeta_.funcs.length()) { |
| 2755 |
return fail("callee index out of range"); |
| 2756 |
} |
| 2757 |
|
| 2758 |
const FuncType& funcType = codeMeta_.getFuncType(*funcIndex); |
| 2759 |
|
| 2760 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2761 |
return false; |
| 2762 |
} |
| 2763 |
|
| 2764 |
return push(ResultType::Vector(funcType.results())); |
| 2765 |
} |
| 2766 |
|
| 2767 |
template <typename Policy> |
| 2768 |
inline bool OpIter<Policy>::readOldCallIndirect(uint32_t* funcTypeIndex, |
| 2769 |
Value* callee, |
| 2770 |
ValueVector* argValues) { |
| 2771 |
MOZ_ASSERT(Classify(op_) == OpKind::OldCallIndirect); |
| 2772 |
|
| 2773 |
if (!readVarU32(funcTypeIndex)) { |
| 2774 |
return fail("unable to read call_indirect signature index"); |
| 2775 |
} |
| 2776 |
|
| 2777 |
if (*funcTypeIndex >= codeMeta_.numTypes()) { |
| 2778 |
return fail("signature index out of range"); |
| 2779 |
} |
| 2780 |
|
| 2781 |
const TypeDef& typeDef = codeMeta_.types->type(*funcTypeIndex); |
| 2782 |
if (!typeDef.isFuncType()) { |
| 2783 |
return fail("expected signature type"); |
| 2784 |
} |
| 2785 |
const FuncType& funcType = typeDef.funcType(); |
| 2786 |
|
| 2787 |
if (!popCallArgs(funcType.args(), argValues)) { |
| 2788 |
return false; |
| 2789 |
} |
| 2790 |
|
| 2791 |
if (!popWithType(ValType::I32, callee)) { |
| 2792 |
return false; |
| 2793 |
} |
| 2794 |
|
| 2795 |
return push(ResultType::Vector(funcType.results())); |
| 2796 |
} |
| 2797 |
|
| 2798 |
template <typename Policy> |
| 2799 |
inline bool OpIter<Policy>::readNotify(LinearMemoryAddress<Value>* addr, |
| 2800 |
Value* count) { |
| 2801 |
MOZ_ASSERT(Classify(op_) == OpKind::Notify); |
| 2802 |
|
| 2803 |
if (!popWithType(ValType::I32, count)) { |
| 2804 |
return false; |
| 2805 |
} |
| 2806 |
|
| 2807 |
uint32_t byteSize = 4; // Per spec; smallest WAIT is i32. |
| 2808 |
|
| 2809 |
if (!readLinearMemoryAddressAligned(byteSize, addr)) { |
| 2810 |
return false; |
| 2811 |
} |
| 2812 |
|
| 2813 |
infalliblePush(ValType::I32); |
| 2814 |
return true; |
| 2815 |
} |
| 2816 |
|
| 2817 |
template <typename Policy> |
| 2818 |
inline bool OpIter<Policy>::readWait(LinearMemoryAddress<Value>* addr, |
| 2819 |
ValType valueType, uint32_t byteSize, |
| 2820 |
Value* value, Value* timeout) { |
| 2821 |
MOZ_ASSERT(Classify(op_) == OpKind::Wait); |
| 2822 |
|
| 2823 |
if (!popWithType(ValType::I64, timeout)) { |
| 2824 |
return false; |
| 2825 |
} |
| 2826 |
|
| 2827 |
if (!popWithType(valueType, value)) { |
| 2828 |
return false; |
| 2829 |
} |
| 2830 |
|
| 2831 |
if (!readLinearMemoryAddressAligned(byteSize, addr)) { |
| 2832 |
return false; |
| 2833 |
} |
| 2834 |
|
| 2835 |
infalliblePush(ValType::I32); |
| 2836 |
return true; |
| 2837 |
} |
| 2838 |
|
| 2839 |
template <typename Policy> |
| 2840 |
inline bool OpIter<Policy>::readFence() { |
| 2841 |
MOZ_ASSERT(Classify(op_) == OpKind::Fence); |
| 2842 |
uint8_t flags; |
| 2843 |
if (!readFixedU8(&flags)) { |
| 2844 |
return fail("expected memory order after fence"); |
| 2845 |
} |
| 2846 |
if (flags != 0) { |
| 2847 |
return fail("non-zero memory order not supported yet"); |
| 2848 |
} |
| 2849 |
return true; |
| 2850 |
} |
| 2851 |
|
| 2852 |
template <typename Policy> |
| 2853 |
inline bool OpIter<Policy>::readAtomicLoad(LinearMemoryAddress<Value>* addr, |
| 2854 |
ValType resultType, |
| 2855 |
uint32_t byteSize) { |
| 2856 |
MOZ_ASSERT(Classify(op_) == OpKind::AtomicLoad); |
| 2857 |
|
| 2858 |
if (!readLinearMemoryAddressAligned(byteSize, addr)) { |
| 2859 |
return false; |
| 2860 |
} |
| 2861 |
|
| 2862 |
infalliblePush(resultType); |
| 2863 |
return true; |
| 2864 |
} |
| 2865 |
|
| 2866 |
template <typename Policy> |
| 2867 |
inline bool OpIter<Policy>::readAtomicStore(LinearMemoryAddress<Value>* addr, |
| 2868 |
ValType resultType, |
| 2869 |
uint32_t byteSize, Value* value) { |
| 2870 |
MOZ_ASSERT(Classify(op_) == OpKind::AtomicStore); |
| 2871 |
|
| 2872 |
if (!popWithType(resultType, value)) { |
| 2873 |
return false; |
| 2874 |
} |
| 2875 |
|
| 2876 |
return readLinearMemoryAddressAligned(byteSize, addr); |
| 2877 |
} |
| 2878 |
|
| 2879 |
template <typename Policy> |
| 2880 |
inline bool OpIter<Policy>::readAtomicRMW(LinearMemoryAddress<Value>* addr, |
| 2881 |
ValType resultType, uint32_t byteSize, |
| 2882 |
Value* value) { |
| 2883 |
MOZ_ASSERT(Classify(op_) == OpKind::AtomicRMW); |
| 2884 |
|
| 2885 |
if (!popWithType(resultType, value)) { |
| 2886 |
return false; |
| 2887 |
} |
| 2888 |
|
| 2889 |
if (!readLinearMemoryAddressAligned(byteSize, addr)) { |
| 2890 |
return false; |
| 2891 |
} |
| 2892 |
|
| 2893 |
infalliblePush(resultType); |
| 2894 |
return true; |
| 2895 |
} |
| 2896 |
|
| 2897 |
template <typename Policy> |
| 2898 |
inline bool OpIter<Policy>::readAtomicCmpXchg(LinearMemoryAddress<Value>* addr, |
| 2899 |
ValType resultType, |
| 2900 |
uint32_t byteSize, |
| 2901 |
Value* oldValue, |
| 2902 |
Value* newValue) { |
| 2903 |
MOZ_ASSERT(Classify(op_) == OpKind::AtomicCmpXchg); |
| 2904 |
|
| 2905 |
if (!popWithType(resultType, newValue)) { |
| 2906 |
return false; |
| 2907 |
} |
| 2908 |
|
| 2909 |
if (!popWithType(resultType, oldValue)) { |
| 2910 |
return false; |
| 2911 |
} |
| 2912 |
|
| 2913 |
if (!readLinearMemoryAddressAligned(byteSize, addr)) { |
| 2914 |
return false; |
| 2915 |
} |
| 2916 |
|
| 2917 |
infalliblePush(resultType); |
| 2918 |
return true; |
| 2919 |
} |
| 2920 |
|
| 2921 |
template <typename Policy> |
| 2922 |
inline bool OpIter<Policy>::readMemOrTableCopy(bool isMem, |
| 2923 |
uint32_t* dstMemOrTableIndex, |
| 2924 |
Value* dst, |
| 2925 |
uint32_t* srcMemOrTableIndex, |
| 2926 |
Value* src, Value* len) { |
| 2927 |
MOZ_ASSERT(Classify(op_) == OpKind::MemOrTableCopy); |
| 2928 |
MOZ_ASSERT(dstMemOrTableIndex != srcMemOrTableIndex); |
| 2929 |
|
| 2930 |
// Spec requires (dest, src) as of 2019-10-04. |
| 2931 |
if (!readVarU32(dstMemOrTableIndex)) { |
| 2932 |
return false; |
| 2933 |
} |
| 2934 |
if (!readVarU32(srcMemOrTableIndex)) { |
| 2935 |
return false; |
| 2936 |
} |
| 2937 |
|
| 2938 |
if (isMem) { |
| 2939 |
if (*srcMemOrTableIndex >= codeMeta_.memories.length() || |
| 2940 |
*dstMemOrTableIndex >= codeMeta_.memories.length()) { |
| 2941 |
return fail("memory index out of range for memory.copy"); |
| 2942 |
} |
| 2943 |
} else { |
| 2944 |
if (*dstMemOrTableIndex >= codeMeta_.tables.length() || |
| 2945 |
*srcMemOrTableIndex >= codeMeta_.tables.length()) { |
| 2946 |
return fail("table index out of range for table.copy"); |
| 2947 |
} |
| 2948 |
ValType dstElemType = codeMeta_.tables[*dstMemOrTableIndex].elemType; |
| 2949 |
ValType srcElemType = codeMeta_.tables[*srcMemOrTableIndex].elemType; |
| 2950 |
if (!checkIsSubtypeOf(srcElemType, dstElemType)) { |
| 2951 |
return false; |
| 2952 |
} |
| 2953 |
} |
| 2954 |
|
| 2955 |
ValType dstPtrType; |
| 2956 |
ValType srcPtrType; |
| 2957 |
ValType lenType; |
| 2958 |
if (isMem) { |
| 2959 |
dstPtrType = |
| 2960 |
ToValType(codeMeta_.memories[*dstMemOrTableIndex].addressType()); |
| 2961 |
srcPtrType = |
| 2962 |
ToValType(codeMeta_.memories[*srcMemOrTableIndex].addressType()); |
| 2963 |
} else { |
| 2964 |
dstPtrType = ToValType(codeMeta_.tables[*dstMemOrTableIndex].addressType()); |
| 2965 |
srcPtrType = ToValType(codeMeta_.tables[*srcMemOrTableIndex].addressType()); |
| 2966 |
} |
| 2967 |
if (dstPtrType == ValType::I64 && srcPtrType == ValType::I64) { |
| 2968 |
lenType = ValType::I64; |
| 2969 |
} else { |
| 2970 |
lenType = ValType::I32; |
| 2971 |
} |
| 2972 |
|
| 2973 |
if (!popWithType(lenType, len)) { |
| 2974 |
return false; |
| 2975 |
} |
| 2976 |
|
| 2977 |
if (!popWithType(srcPtrType, src)) { |
| 2978 |
return false; |
| 2979 |
} |
| 2980 |
|
| 2981 |
return popWithType(dstPtrType, dst); |
| 2982 |
} |
| 2983 |
|
| 2984 |
template <typename Policy> |
| 2985 |
inline bool OpIter<Policy>::readDataOrElemDrop(bool isData, |
| 2986 |
uint32_t* segIndex) { |
| 2987 |
MOZ_ASSERT(Classify(op_) == OpKind::DataOrElemDrop); |
| 2988 |
|
| 2989 |
if (!readVarU32(segIndex)) { |
| 2990 |
return fail("unable to read segment index"); |
| 2991 |
} |
| 2992 |
|
| 2993 |
if (isData) { |
| 2994 |
if (codeMeta_.dataCount.isNothing()) { |
| 2995 |
return fail("data.drop requires a DataCount section"); |
| 2996 |
} |
| 2997 |
if (*segIndex >= *codeMeta_.dataCount) { |
| 2998 |
return fail("data.drop segment index out of range"); |
| 2999 |
} |
| 3000 |
} else { |
| 3001 |
if (*segIndex >= codeMeta_.elemSegmentTypes.length()) { |
| 3002 |
return fail("element segment index out of range for elem.drop"); |
| 3003 |
} |
| 3004 |
} |
| 3005 |
|
| 3006 |
return true; |
| 3007 |
} |
| 3008 |
|
| 3009 |
template <typename Policy> |
| 3010 |
inline bool OpIter<Policy>::readMemFill(uint32_t* memoryIndex, Value* start, |
| 3011 |
Value* val, Value* len) { |
| 3012 |
MOZ_ASSERT(Classify(op_) == OpKind::MemFill); |
| 3013 |
|
| 3014 |
if (!readVarU32(memoryIndex)) { |
| 3015 |
return fail("failed to read memory index"); |
| 3016 |
} |
| 3017 |
|
| 3018 |
if (*memoryIndex >= codeMeta_.numMemories()) { |
| 3019 |
return fail("memory index out of range for memory.fill"); |
| 3020 |
} |
| 3021 |
|
| 3022 |
ValType ptrType = ToValType(codeMeta_.memories[*memoryIndex].addressType()); |
| 3023 |
|
| 3024 |
if (!popWithType(ptrType, len)) { |
| 3025 |
return false; |
| 3026 |
} |
| 3027 |
|
| 3028 |
if (!popWithType(ValType::I32, val)) { |
| 3029 |
return false; |
| 3030 |
} |
| 3031 |
|
| 3032 |
return popWithType(ptrType, start); |
| 3033 |
} |
| 3034 |
|
| 3035 |
template <typename Policy> |
| 3036 |
inline bool OpIter<Policy>::readMemOrTableInit(bool isMem, uint32_t* segIndex, |
| 3037 |
uint32_t* dstMemOrTableIndex, |
| 3038 |
Value* dst, Value* src, |
| 3039 |
Value* len) { |
| 3040 |
MOZ_ASSERT(Classify(op_) == OpKind::MemOrTableInit); |
| 3041 |
MOZ_ASSERT(segIndex != dstMemOrTableIndex); |
| 3042 |
|
| 3043 |
if (!readVarU32(segIndex)) { |
| 3044 |
return fail("unable to read segment index"); |
| 3045 |
} |
| 3046 |
|
| 3047 |
uint32_t memOrTableIndex = 0; |
| 3048 |
if (!readVarU32(&memOrTableIndex)) { |
| 3049 |
return false; |
| 3050 |
} |
| 3051 |
|
| 3052 |
if (isMem) { |
| 3053 |
if (memOrTableIndex >= codeMeta_.memories.length()) { |
| 3054 |
return fail("memory index out of range for memory.init"); |
| 3055 |
} |
| 3056 |
*dstMemOrTableIndex = memOrTableIndex; |
| 3057 |
|
| 3058 |
if (codeMeta_.dataCount.isNothing()) { |
| 3059 |
return fail("memory.init requires a DataCount section"); |
| 3060 |
} |
| 3061 |
if (*segIndex >= *codeMeta_.dataCount) { |
| 3062 |
return fail("memory.init segment index out of range"); |
| 3063 |
} |
| 3064 |
} else { |
| 3065 |
if (memOrTableIndex >= codeMeta_.tables.length()) { |
| 3066 |
return fail("table index out of range for table.init"); |
| 3067 |
} |
| 3068 |
*dstMemOrTableIndex = memOrTableIndex; |
| 3069 |
|
| 3070 |
if (*segIndex >= codeMeta_.elemSegmentTypes.length()) { |
| 3071 |
return fail("table.init segment index out of range"); |
| 3072 |
} |
| 3073 |
if (!checkIsSubtypeOf(codeMeta_.elemSegmentTypes[*segIndex], |
| 3074 |
codeMeta_.tables[*dstMemOrTableIndex].elemType)) { |
| 3075 |
return false; |
| 3076 |
} |
| 3077 |
} |
| 3078 |
|
| 3079 |
if (!popWithType(ValType::I32, len)) { |
| 3080 |
return false; |
| 3081 |
} |
| 3082 |
|
| 3083 |
if (!popWithType(ValType::I32, src)) { |
| 3084 |
return false; |
| 3085 |
} |
| 3086 |
|
| 3087 |
ValType ptrType = |
| 3088 |
isMem ? ToValType(codeMeta_.memories[*dstMemOrTableIndex].addressType()) |
| 3089 |
: ToValType(codeMeta_.tables[*dstMemOrTableIndex].addressType()); |
| 3090 |
return popWithType(ptrType, dst); |
| 3091 |
} |
| 3092 |
|
| 3093 |
template <typename Policy> |
| 3094 |
inline bool OpIter<Policy>::readTableFill(uint32_t* tableIndex, Value* start, |
| 3095 |
Value* val, Value* len) { |
| 3096 |
MOZ_ASSERT(Classify(op_) == OpKind::TableFill); |
| 3097 |
|
| 3098 |
if (!readVarU32(tableIndex)) { |
| 3099 |
return fail("unable to read table index"); |
| 3100 |
} |
| 3101 |
if (*tableIndex >= codeMeta_.tables.length()) { |
| 3102 |
return fail("table index out of range for table.fill"); |
| 3103 |
} |
| 3104 |
|
| 3105 |
const TableDesc& table = codeMeta_.tables[*tableIndex]; |
| 3106 |
|
| 3107 |
if (!popWithType(ToValType(table.addressType()), len)) { |
| 3108 |
return false; |
| 3109 |
} |
| 3110 |
if (!popWithType(table.elemType, val)) { |
| 3111 |
return false; |
| 3112 |
} |
| 3113 |
return popWithType(ToValType(table.addressType()), start); |
| 3114 |
} |
| 3115 |
|
| 3116 |
template <typename Policy> |
| 3117 |
inline bool OpIter<Policy>::readMemDiscard(uint32_t* memoryIndex, Value* start, |
| 3118 |
Value* len) { |
| 3119 |
MOZ_ASSERT(Classify(op_) == OpKind::MemDiscard); |
| 3120 |
|
| 3121 |
if (!readVarU32(memoryIndex)) { |
| 3122 |
return fail("failed to read memory index"); |
| 3123 |
} |
| 3124 |
if (*memoryIndex >= codeMeta_.memories.length()) { |
| 3125 |
return fail("memory index out of range for memory.discard"); |
| 3126 |
} |
| 3127 |
|
| 3128 |
ValType ptrType = ToValType(codeMeta_.memories[*memoryIndex].addressType()); |
| 3129 |
|
| 3130 |
if (!popWithType(ptrType, len)) { |
| 3131 |
return false; |
| 3132 |
} |
| 3133 |
|
| 3134 |
return popWithType(ptrType, start); |
| 3135 |
} |
| 3136 |
|
| 3137 |
template <typename Policy> |
| 3138 |
inline bool OpIter<Policy>::readTableGet(uint32_t* tableIndex, Value* address) { |
| 3139 |
MOZ_ASSERT(Classify(op_) == OpKind::TableGet); |
| 3140 |
|
| 3141 |
if (!readVarU32(tableIndex)) { |
| 3142 |
return fail("unable to read table index"); |
| 3143 |
} |
| 3144 |
if (*tableIndex >= codeMeta_.tables.length()) { |
| 3145 |
return fail("table index out of range for table.get"); |
| 3146 |
} |
| 3147 |
|
| 3148 |
const TableDesc& table = codeMeta_.tables[*tableIndex]; |
| 3149 |
|
| 3150 |
if (!popWithType(ToValType(table.addressType()), address)) { |
| 3151 |
return false; |
| 3152 |
} |
| 3153 |
|
| 3154 |
infalliblePush(table.elemType); |
| 3155 |
return true; |
| 3156 |
} |
| 3157 |
|
| 3158 |
template <typename Policy> |
| 3159 |
inline bool OpIter<Policy>::readTableGrow(uint32_t* tableIndex, |
| 3160 |
Value* initValue, Value* delta) { |
| 3161 |
MOZ_ASSERT(Classify(op_) == OpKind::TableGrow); |
| 3162 |
|
| 3163 |
if (!readVarU32(tableIndex)) { |
| 3164 |
return fail("unable to read table index"); |
| 3165 |
} |
| 3166 |
if (*tableIndex >= codeMeta_.tables.length()) { |
| 3167 |
return fail("table index out of range for table.grow"); |
| 3168 |
} |
| 3169 |
|
| 3170 |
const TableDesc& table = codeMeta_.tables[*tableIndex]; |
| 3171 |
|
| 3172 |
if (!popWithType(ToValType(table.addressType()), delta)) { |
| 3173 |
return false; |
| 3174 |
} |
| 3175 |
if (!popWithType(table.elemType, initValue)) { |
| 3176 |
return false; |
| 3177 |
} |
| 3178 |
|
| 3179 |
infalliblePush(ToValType(table.addressType())); |
| 3180 |
return true; |
| 3181 |
} |
| 3182 |
|
| 3183 |
template <typename Policy> |
| 3184 |
inline bool OpIter<Policy>::readTableSet(uint32_t* tableIndex, Value* address, |
| 3185 |
Value* value) { |
| 3186 |
MOZ_ASSERT(Classify(op_) == OpKind::TableSet); |
| 3187 |
|
| 3188 |
if (!readVarU32(tableIndex)) { |
| 3189 |
return fail("unable to read table index"); |
| 3190 |
} |
| 3191 |
if (*tableIndex >= codeMeta_.tables.length()) { |
| 3192 |
return fail("table index out of range for table.set"); |
| 3193 |
} |
| 3194 |
|
| 3195 |
const TableDesc& table = codeMeta_.tables[*tableIndex]; |
| 3196 |
|
| 3197 |
if (!popWithType(table.elemType, value)) { |
| 3198 |
return false; |
| 3199 |
} |
| 3200 |
|
| 3201 |
return popWithType(ToValType(table.addressType()), address); |
| 3202 |
} |
| 3203 |
|
| 3204 |
template <typename Policy> |
| 3205 |
inline bool OpIter<Policy>::readTableSize(uint32_t* tableIndex) { |
| 3206 |
MOZ_ASSERT(Classify(op_) == OpKind::TableSize); |
| 3207 |
|
| 3208 |
*tableIndex = 0; |
| 3209 |
|
| 3210 |
if (!readVarU32(tableIndex)) { |
| 3211 |
return fail("unable to read table index"); |
| 3212 |
} |
| 3213 |
if (*tableIndex >= codeMeta_.tables.length()) { |
| 3214 |
return fail("table index out of range for table.size"); |
| 3215 |
} |
| 3216 |
|
| 3217 |
return push(ToValType(codeMeta_.tables[*tableIndex].addressType())); |
| 3218 |
} |
| 3219 |
|
| 3220 |
template <typename Policy> |
| 3221 |
inline bool OpIter<Policy>::readGcTypeIndex(uint32_t* typeIndex) { |
| 3222 |
if (!d_.readTypeIndex(typeIndex)) { |
| 3223 |
return false; |
| 3224 |
} |
| 3225 |
|
| 3226 |
if (*typeIndex >= codeMeta_.types->length()) { |
| 3227 |
return fail("type index out of range"); |
| 3228 |
} |
| 3229 |
|
| 3230 |
if (!codeMeta_.types->type(*typeIndex).isStructType() && |
| 3231 |
!codeMeta_.types->type(*typeIndex).isArrayType()) { |
| 3232 |
return fail("not a gc type"); |
| 3233 |
} |
| 3234 |
|
| 3235 |
return true; |
| 3236 |
} |
| 3237 |
|
| 3238 |
template <typename Policy> |
| 3239 |
inline bool OpIter<Policy>::readStructTypeIndex(uint32_t* typeIndex) { |
| 3240 |
if (!readVarU32(typeIndex)) { |
| 3241 |
return fail("unable to read type index"); |
| 3242 |
} |
| 3243 |
|
| 3244 |
if (*typeIndex >= codeMeta_.types->length()) { |
| 3245 |
return fail("type index out of range"); |
| 3246 |
} |
| 3247 |
|
| 3248 |
if (!codeMeta_.types->type(*typeIndex).isStructType()) { |
| 3249 |
return fail("not a struct type"); |
| 3250 |
} |
| 3251 |
|
| 3252 |
return true; |
| 3253 |
} |
| 3254 |
|
| 3255 |
template <typename Policy> |
| 3256 |
inline bool OpIter<Policy>::readArrayTypeIndex(uint32_t* typeIndex) { |
| 3257 |
if (!readVarU32(typeIndex)) { |
| 3258 |
return fail("unable to read type index"); |
| 3259 |
} |
| 3260 |
|
| 3261 |
if (*typeIndex >= codeMeta_.types->length()) { |
| 3262 |
return fail("type index out of range"); |
| 3263 |
} |
| 3264 |
|
| 3265 |
if (!codeMeta_.types->type(*typeIndex).isArrayType()) { |
| 3266 |
return fail("not an array type"); |
| 3267 |
} |
| 3268 |
|
| 3269 |
return true; |
| 3270 |
} |
| 3271 |
|
| 3272 |
template <typename Policy> |
| 3273 |
inline bool OpIter<Policy>::readFuncTypeIndex(uint32_t* typeIndex) { |
| 3274 |
if (!readVarU32(typeIndex)) { |
| 3275 |
return fail("unable to read type index"); |
| 3276 |
} |
| 3277 |
|
| 3278 |
if (*typeIndex >= codeMeta_.types->length()) { |
| 3279 |
return fail("type index out of range"); |
| 3280 |
} |
| 3281 |
|
| 3282 |
if (!codeMeta_.types->type(*typeIndex).isFuncType()) { |
| 3283 |
return fail("not an func type"); |
| 3284 |
} |
| 3285 |
|
| 3286 |
return true; |
| 3287 |
} |
| 3288 |
|
| 3289 |
template <typename Policy> |
| 3290 |
inline bool OpIter<Policy>::readFieldIndex(uint32_t* fieldIndex, |
| 3291 |
const StructType& structType) { |
| 3292 |
if (!readVarU32(fieldIndex)) { |
| 3293 |
return fail("unable to read field index"); |
| 3294 |
} |
| 3295 |
|
| 3296 |
if (structType.fields_.length() <= *fieldIndex) { |
| 3297 |
return fail("field index out of range"); |
| 3298 |
} |
| 3299 |
|
| 3300 |
return true; |
| 3301 |
} |
| 3302 |
|
| 3303 |
template <typename Policy> |
| 3304 |
inline bool OpIter<Policy>::readStructNew(uint32_t* typeIndex, |
| 3305 |
ValueVector* argValues) { |
| 3306 |
MOZ_ASSERT(Classify(op_) == OpKind::StructNew); |
| 3307 |
|
| 3308 |
if (!readStructTypeIndex(typeIndex)) { |
| 3309 |
return false; |
| 3310 |
} |
| 3311 |
|
| 3312 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3313 |
const StructType& structType = typeDef.structType(); |
| 3314 |
|
| 3315 |
if (!argValues->resize(structType.fields_.length())) { |
| 3316 |
return false; |
| 3317 |
} |
| 3318 |
|
| 3319 |
static_assert(MaxStructFields <= INT32_MAX, "Or we iloop below"); |
| 3320 |
|
| 3321 |
for (int32_t i = structType.fields_.length() - 1; i >= 0; i--) { |
| 3322 |
if (!popWithType(structType.fields_[i].type.widenToValType(), |
| 3323 |
&(*argValues)[i])) { |
| 3324 |
return false; |
| 3325 |
} |
| 3326 |
} |
| 3327 |
|
| 3328 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 3329 |
} |
| 3330 |
|
| 3331 |
template <typename Policy> |
| 3332 |
inline bool OpIter<Policy>::readStructNewDefault(uint32_t* typeIndex) { |
| 3333 |
MOZ_ASSERT(Classify(op_) == OpKind::StructNewDefault); |
| 3334 |
|
| 3335 |
if (!readStructTypeIndex(typeIndex)) { |
| 3336 |
return false; |
| 3337 |
} |
| 3338 |
|
| 3339 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3340 |
const StructType& structType = typeDef.structType(); |
| 3341 |
|
| 3342 |
if (!structType.isDefaultable()) { |
| 3343 |
return fail("struct must be defaultable"); |
| 3344 |
} |
| 3345 |
|
| 3346 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 3347 |
} |
| 3348 |
|
| 3349 |
template <typename Policy> |
| 3350 |
inline bool OpIter<Policy>::readStructGet(uint32_t* typeIndex, |
| 3351 |
uint32_t* fieldIndex, |
| 3352 |
FieldWideningOp wideningOp, |
| 3353 |
Value* ptr) { |
| 3354 |
MOZ_ASSERT(typeIndex != fieldIndex); |
| 3355 |
MOZ_ASSERT(Classify(op_) == OpKind::StructGet); |
| 3356 |
|
| 3357 |
if (!readStructTypeIndex(typeIndex)) { |
| 3358 |
return false; |
| 3359 |
} |
| 3360 |
|
| 3361 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3362 |
const StructType& structType = typeDef.structType(); |
| 3363 |
|
| 3364 |
if (!readFieldIndex(fieldIndex, structType)) { |
| 3365 |
return false; |
| 3366 |
} |
| 3367 |
|
| 3368 |
if (!popWithType(RefType::fromTypeDef(&typeDef, true), ptr)) { |
| 3369 |
return false; |
| 3370 |
} |
| 3371 |
|
| 3372 |
StorageType StorageType = structType.fields_[*fieldIndex].type; |
| 3373 |
|
| 3374 |
if (StorageType.isValType() && wideningOp != FieldWideningOp::None) { |
| 3375 |
return fail("must not specify signedness for unpacked field type"); |
| 3376 |
} |
| 3377 |
|
| 3378 |
if (!StorageType.isValType() && wideningOp == FieldWideningOp::None) { |
| 3379 |
return fail("must specify signedness for packed field type"); |
| 3380 |
} |
| 3381 |
|
| 3382 |
return push(StorageType.widenToValType()); |
| 3383 |
} |
| 3384 |
|
| 3385 |
template <typename Policy> |
| 3386 |
inline bool OpIter<Policy>::readStructSet(uint32_t* typeIndex, |
| 3387 |
uint32_t* fieldIndex, Value* ptr, |
| 3388 |
Value* val) { |
| 3389 |
MOZ_ASSERT(typeIndex != fieldIndex); |
| 3390 |
MOZ_ASSERT(Classify(op_) == OpKind::StructSet); |
| 3391 |
|
| 3392 |
if (!readStructTypeIndex(typeIndex)) { |
| 3393 |
return false; |
| 3394 |
} |
| 3395 |
|
| 3396 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3397 |
const StructType& structType = typeDef.structType(); |
| 3398 |
|
| 3399 |
if (!readFieldIndex(fieldIndex, structType)) { |
| 3400 |
return false; |
| 3401 |
} |
| 3402 |
|
| 3403 |
if (!popWithType(structType.fields_[*fieldIndex].type.widenToValType(), |
| 3404 |
val)) { |
| 3405 |
return false; |
| 3406 |
} |
| 3407 |
|
| 3408 |
if (!structType.fields_[*fieldIndex].isMutable) { |
| 3409 |
return fail("field is not mutable"); |
| 3410 |
} |
| 3411 |
|
| 3412 |
return popWithType(RefType::fromTypeDef(&typeDef, true), ptr); |
| 3413 |
} |
| 3414 |
|
| 3415 |
template <typename Policy> |
| 3416 |
inline bool OpIter<Policy>::readArrayNew(uint32_t* typeIndex, |
| 3417 |
Value* numElements, Value* argValue) { |
| 3418 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayNew); |
| 3419 |
|
| 3420 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3421 |
return false; |
| 3422 |
} |
| 3423 |
|
| 3424 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3425 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3426 |
|
| 3427 |
if (!popWithType(ValType::I32, numElements)) { |
| 3428 |
return false; |
| 3429 |
} |
| 3430 |
|
| 3431 |
if (!popWithType(arrayType.elementType().widenToValType(), argValue)) { |
| 3432 |
return false; |
| 3433 |
} |
| 3434 |
|
| 3435 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 3436 |
} |
| 3437 |
|
| 3438 |
template <typename Policy> |
| 3439 |
inline bool OpIter<Policy>::readArrayNewFixed(uint32_t* typeIndex, |
| 3440 |
uint32_t* numElements, |
| 3441 |
ValueVector* values) { |
| 3442 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayNewFixed); |
| 3443 |
MOZ_ASSERT(values->length() == 0); |
| 3444 |
|
| 3445 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3446 |
return false; |
| 3447 |
} |
| 3448 |
|
| 3449 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3450 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3451 |
|
| 3452 |
if (!readVarU32(numElements)) { |
| 3453 |
return false; |
| 3454 |
} |
| 3455 |
|
| 3456 |
if (*numElements > MaxArrayNewFixedElements) { |
| 3457 |
return fail("too many array.new_fixed elements"); |
| 3458 |
} |
| 3459 |
|
| 3460 |
if (!values->reserve(*numElements)) { |
| 3461 |
return false; |
| 3462 |
} |
| 3463 |
|
| 3464 |
ValType widenedElementType = arrayType.elementType().widenToValType(); |
| 3465 |
for (uint32_t i = 0; i < *numElements; i++) { |
| 3466 |
Value v; |
| 3467 |
if (!popWithType(widenedElementType, &v)) { |
| 3468 |
return false; |
| 3469 |
} |
| 3470 |
values->infallibleAppend(v); |
| 3471 |
} |
| 3472 |
|
| 3473 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 3474 |
} |
| 3475 |
|
| 3476 |
template <typename Policy> |
| 3477 |
inline bool OpIter<Policy>::readArrayNewDefault(uint32_t* typeIndex, |
| 3478 |
Value* numElements) { |
| 3479 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayNewDefault); |
| 3480 |
|
| 3481 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3482 |
return false; |
| 3483 |
} |
| 3484 |
|
| 3485 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3486 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3487 |
|
| 3488 |
if (!popWithType(ValType::I32, numElements)) { |
| 3489 |
return false; |
| 3490 |
} |
| 3491 |
|
| 3492 |
if (!arrayType.elementType().isDefaultable()) { |
| 3493 |
return fail("array must be defaultable"); |
| 3494 |
} |
| 3495 |
|
| 3496 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 3497 |
} |
| 3498 |
|
| 3499 |
template <typename Policy> |
| 3500 |
inline bool OpIter<Policy>::readArrayNewData(uint32_t* typeIndex, |
| 3501 |
uint32_t* segIndex, Value* offset, |
| 3502 |
Value* numElements) { |
| 3503 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayNewData); |
| 3504 |
|
| 3505 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3506 |
return false; |
| 3507 |
} |
| 3508 |
|
| 3509 |
if (!readVarU32(segIndex)) { |
| 3510 |
return fail("unable to read segment index"); |
| 3511 |
} |
| 3512 |
|
| 3513 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3514 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3515 |
StorageType elemType = arrayType.elementType(); |
| 3516 |
if (!elemType.isNumber() && !elemType.isPacked() && !elemType.isVector()) { |
| 3517 |
return fail("element type must be i8/i16/i32/i64/f32/f64/v128"); |
| 3518 |
} |
| 3519 |
if (codeMeta_.dataCount.isNothing()) { |
| 3520 |
return fail("datacount section missing"); |
| 3521 |
} |
| 3522 |
if (*segIndex >= *codeMeta_.dataCount) { |
| 3523 |
return fail("segment index is out of range"); |
| 3524 |
} |
| 3525 |
|
| 3526 |
if (!popWithType(ValType::I32, numElements)) { |
| 3527 |
return false; |
| 3528 |
} |
| 3529 |
if (!popWithType(ValType::I32, offset)) { |
| 3530 |
return false; |
| 3531 |
} |
| 3532 |
|
| 3533 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 3534 |
} |
| 3535 |
|
| 3536 |
template <typename Policy> |
| 3537 |
inline bool OpIter<Policy>::readArrayNewElem(uint32_t* typeIndex, |
| 3538 |
uint32_t* segIndex, Value* offset, |
| 3539 |
Value* numElements) { |
| 3540 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayNewElem); |
| 3541 |
|
| 3542 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3543 |
return false; |
| 3544 |
} |
| 3545 |
|
| 3546 |
if (!readVarU32(segIndex)) { |
| 3547 |
return fail("unable to read segment index"); |
| 3548 |
} |
| 3549 |
|
| 3550 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3551 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3552 |
StorageType dstElemType = arrayType.elementType(); |
| 3553 |
if (!dstElemType.isRefType()) { |
| 3554 |
return fail("element type is not a reftype"); |
| 3555 |
} |
| 3556 |
if (*segIndex >= codeMeta_.elemSegmentTypes.length()) { |
| 3557 |
return fail("segment index is out of range"); |
| 3558 |
} |
| 3559 |
|
| 3560 |
RefType srcElemType = codeMeta_.elemSegmentTypes[*segIndex]; |
| 3561 |
// srcElemType needs to be a subtype (child) of dstElemType |
| 3562 |
if (!checkIsSubtypeOf(srcElemType, dstElemType.refType())) { |
| 3563 |
return fail("incompatible element types"); |
| 3564 |
} |
| 3565 |
|
| 3566 |
if (!popWithType(ValType::I32, numElements)) { |
| 3567 |
return false; |
| 3568 |
} |
| 3569 |
if (!popWithType(ValType::I32, offset)) { |
| 3570 |
return false; |
| 3571 |
} |
| 3572 |
|
| 3573 |
return push(RefType::fromTypeDef(&typeDef, false)); |
| 3574 |
} |
| 3575 |
|
| 3576 |
template <typename Policy> |
| 3577 |
inline bool OpIter<Policy>::readArrayInitData(uint32_t* typeIndex, |
| 3578 |
uint32_t* segIndex, Value* array, |
| 3579 |
Value* arrayIndex, |
| 3580 |
Value* segOffset, Value* length) { |
| 3581 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayInitData); |
| 3582 |
|
| 3583 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3584 |
return false; |
| 3585 |
} |
| 3586 |
|
| 3587 |
if (!readVarU32(segIndex)) { |
| 3588 |
return fail("unable to read segment index"); |
| 3589 |
} |
| 3590 |
|
| 3591 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3592 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3593 |
StorageType elemType = arrayType.elementType(); |
| 3594 |
if (!elemType.isNumber() && !elemType.isPacked() && !elemType.isVector()) { |
| 3595 |
return fail("element type must be i8/i16/i32/i64/f32/f64/v128"); |
| 3596 |
} |
| 3597 |
if (!arrayType.isMutable()) { |
| 3598 |
return fail("destination array is not mutable"); |
| 3599 |
} |
| 3600 |
if (codeMeta_.dataCount.isNothing()) { |
| 3601 |
return fail("datacount section missing"); |
| 3602 |
} |
| 3603 |
if (*segIndex >= *codeMeta_.dataCount) { |
| 3604 |
return fail("segment index is out of range"); |
| 3605 |
} |
| 3606 |
|
| 3607 |
if (!popWithType(ValType::I32, length)) { |
| 3608 |
return false; |
| 3609 |
} |
| 3610 |
if (!popWithType(ValType::I32, segOffset)) { |
| 3611 |
return false; |
| 3612 |
} |
| 3613 |
if (!popWithType(ValType::I32, arrayIndex)) { |
| 3614 |
return false; |
| 3615 |
} |
| 3616 |
return popWithType(RefType::fromTypeDef(&typeDef, true), array); |
| 3617 |
} |
| 3618 |
|
| 3619 |
template <typename Policy> |
| 3620 |
inline bool OpIter<Policy>::readArrayInitElem(uint32_t* typeIndex, |
| 3621 |
uint32_t* segIndex, Value* array, |
| 3622 |
Value* arrayIndex, |
| 3623 |
Value* segOffset, Value* length) { |
| 3624 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayInitElem); |
| 3625 |
|
| 3626 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3627 |
return false; |
| 3628 |
} |
| 3629 |
|
| 3630 |
if (!readVarU32(segIndex)) { |
| 3631 |
return fail("unable to read segment index"); |
| 3632 |
} |
| 3633 |
|
| 3634 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3635 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3636 |
StorageType dstElemType = arrayType.elementType(); |
| 3637 |
if (!arrayType.isMutable()) { |
| 3638 |
return fail("destination array is not mutable"); |
| 3639 |
} |
| 3640 |
if (!dstElemType.isRefType()) { |
| 3641 |
return fail("element type is not a reftype"); |
| 3642 |
} |
| 3643 |
if (*segIndex >= codeMeta_.elemSegmentTypes.length()) { |
| 3644 |
return fail("segment index is out of range"); |
| 3645 |
} |
| 3646 |
|
| 3647 |
RefType srcElemType = codeMeta_.elemSegmentTypes[*segIndex]; |
| 3648 |
// srcElemType needs to be a subtype (child) of dstElemType |
| 3649 |
if (!checkIsSubtypeOf(srcElemType, dstElemType.refType())) { |
| 3650 |
return fail("incompatible element types"); |
| 3651 |
} |
| 3652 |
|
| 3653 |
if (!popWithType(ValType::I32, length)) { |
| 3654 |
return false; |
| 3655 |
} |
| 3656 |
if (!popWithType(ValType::I32, segOffset)) { |
| 3657 |
return false; |
| 3658 |
} |
| 3659 |
if (!popWithType(ValType::I32, arrayIndex)) { |
| 3660 |
return false; |
| 3661 |
} |
| 3662 |
return popWithType(RefType::fromTypeDef(&typeDef, true), array); |
| 3663 |
} |
| 3664 |
|
| 3665 |
template <typename Policy> |
| 3666 |
inline bool OpIter<Policy>::readArrayGet(uint32_t* typeIndex, |
| 3667 |
FieldWideningOp wideningOp, |
| 3668 |
Value* index, Value* ptr) { |
| 3669 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayGet); |
| 3670 |
|
| 3671 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3672 |
return false; |
| 3673 |
} |
| 3674 |
|
| 3675 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3676 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3677 |
|
| 3678 |
if (!popWithType(ValType::I32, index)) { |
| 3679 |
return false; |
| 3680 |
} |
| 3681 |
|
| 3682 |
if (!popWithType(RefType::fromTypeDef(&typeDef, true), ptr)) { |
| 3683 |
return false; |
| 3684 |
} |
| 3685 |
|
| 3686 |
StorageType elementType = arrayType.elementType(); |
| 3687 |
|
| 3688 |
if (elementType.isValType() && wideningOp != FieldWideningOp::None) { |
| 3689 |
return fail("must not specify signedness for unpacked element type"); |
| 3690 |
} |
| 3691 |
|
| 3692 |
if (!elementType.isValType() && wideningOp == FieldWideningOp::None) { |
| 3693 |
return fail("must specify signedness for packed element type"); |
| 3694 |
} |
| 3695 |
|
| 3696 |
return push(elementType.widenToValType()); |
| 3697 |
} |
| 3698 |
|
| 3699 |
template <typename Policy> |
| 3700 |
inline bool OpIter<Policy>::readArraySet(uint32_t* typeIndex, Value* val, |
| 3701 |
Value* index, Value* ptr) { |
| 3702 |
MOZ_ASSERT(Classify(op_) == OpKind::ArraySet); |
| 3703 |
|
| 3704 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3705 |
return false; |
| 3706 |
} |
| 3707 |
|
| 3708 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3709 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3710 |
|
| 3711 |
if (!arrayType.isMutable()) { |
| 3712 |
return fail("array is not mutable"); |
| 3713 |
} |
| 3714 |
|
| 3715 |
if (!popWithType(arrayType.elementType().widenToValType(), val)) { |
| 3716 |
return false; |
| 3717 |
} |
| 3718 |
|
| 3719 |
if (!popWithType(ValType::I32, index)) { |
| 3720 |
return false; |
| 3721 |
} |
| 3722 |
|
| 3723 |
return popWithType(RefType::fromTypeDef(&typeDef, true), ptr); |
| 3724 |
} |
| 3725 |
|
| 3726 |
template <typename Policy> |
| 3727 |
inline bool OpIter<Policy>::readArrayLen(Value* ptr) { |
| 3728 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayLen); |
| 3729 |
|
| 3730 |
if (!popWithType(RefType::array(), ptr)) { |
| 3731 |
return false; |
| 3732 |
} |
| 3733 |
|
| 3734 |
return push(ValType::I32); |
| 3735 |
} |
| 3736 |
|
| 3737 |
template <typename Policy> |
| 3738 |
inline bool OpIter<Policy>::readArrayCopy(uint32_t* dstArrayTypeIndex, |
| 3739 |
uint32_t* srcArrayTypeIndex, |
| 3740 |
Value* dstArray, Value* dstIndex, |
| 3741 |
Value* srcArray, Value* srcIndex, |
| 3742 |
Value* numElements) { |
| 3743 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayCopy); |
| 3744 |
|
| 3745 |
if (!readArrayTypeIndex(dstArrayTypeIndex)) { |
| 3746 |
return false; |
| 3747 |
} |
| 3748 |
if (!readArrayTypeIndex(srcArrayTypeIndex)) { |
| 3749 |
return false; |
| 3750 |
} |
| 3751 |
|
| 3752 |
// `dstArrayTypeIndex`/`srcArrayTypeIndex` are ensured by the above to both be |
| 3753 |
// array types. Reject if: |
| 3754 |
// * the dst array is not of mutable type |
| 3755 |
// * the element types are incompatible |
| 3756 |
const TypeDef& dstTypeDef = codeMeta_.types->type(*dstArrayTypeIndex); |
| 3757 |
const ArrayType& dstArrayType = dstTypeDef.arrayType(); |
| 3758 |
const TypeDef& srcTypeDef = codeMeta_.types->type(*srcArrayTypeIndex); |
| 3759 |
const ArrayType& srcArrayType = srcTypeDef.arrayType(); |
| 3760 |
StorageType dstElemType = dstArrayType.elementType(); |
| 3761 |
StorageType srcElemType = srcArrayType.elementType(); |
| 3762 |
if (!dstArrayType.isMutable()) { |
| 3763 |
return fail("destination array is not mutable"); |
| 3764 |
} |
| 3765 |
|
| 3766 |
if (!checkIsSubtypeOf(srcElemType, dstElemType)) { |
| 3767 |
return fail("incompatible element types"); |
| 3768 |
} |
| 3769 |
MOZ_ASSERT(dstElemType.isRefType() == srcElemType.isRefType()); |
| 3770 |
|
| 3771 |
if (!popWithType(ValType::I32, numElements)) { |
| 3772 |
return false; |
| 3773 |
} |
| 3774 |
if (!popWithType(ValType::I32, srcIndex)) { |
| 3775 |
return false; |
| 3776 |
} |
| 3777 |
if (!popWithType(RefType::fromTypeDef(&srcTypeDef, true), srcArray)) { |
| 3778 |
return false; |
| 3779 |
} |
| 3780 |
if (!popWithType(ValType::I32, dstIndex)) { |
| 3781 |
return false; |
| 3782 |
} |
| 3783 |
|
| 3784 |
return popWithType(RefType::fromTypeDef(&dstTypeDef, true), dstArray); |
| 3785 |
} |
| 3786 |
|
| 3787 |
template <typename Policy> |
| 3788 |
inline bool OpIter<Policy>::readArrayFill(uint32_t* typeIndex, Value* array, |
| 3789 |
Value* index, Value* val, |
| 3790 |
Value* length) { |
| 3791 |
MOZ_ASSERT(Classify(op_) == OpKind::ArrayFill); |
| 3792 |
|
| 3793 |
if (!readArrayTypeIndex(typeIndex)) { |
| 3794 |
return false; |
| 3795 |
} |
| 3796 |
|
| 3797 |
const TypeDef& typeDef = codeMeta_.types->type(*typeIndex); |
| 3798 |
const ArrayType& arrayType = typeDef.arrayType(); |
| 3799 |
if (!arrayType.isMutable()) { |
| 3800 |
return fail("destination array is not mutable"); |
| 3801 |
} |
| 3802 |
|
| 3803 |
if (!popWithType(ValType::I32, length)) { |
| 3804 |
return false; |
| 3805 |
} |
| 3806 |
if (!popWithType(arrayType.elementType().widenToValType(), val)) { |
| 3807 |
return false; |
| 3808 |
} |
| 3809 |
if (!popWithType(ValType::I32, index)) { |
| 3810 |
return false; |
| 3811 |
} |
| 3812 |
|
| 3813 |
return popWithType(RefType::fromTypeDef(&typeDef, true), array); |
| 3814 |
} |
| 3815 |
|
| 3816 |
template <typename Policy> |
| 3817 |
inline bool OpIter<Policy>::readRefTest(bool nullable, RefType* sourceType, |
| 3818 |
RefType* destType, Value* ref) { |
| 3819 |
MOZ_ASSERT(Classify(op_) == OpKind::RefTest); |
| 3820 |
|
| 3821 |
if (!readHeapType(nullable, destType)) { |
| 3822 |
return false; |
| 3823 |
} |
| 3824 |
|
| 3825 |
StackType inputType; |
| 3826 |
if (!popWithType(destType->topType(), ref, &inputType)) { |
| 3827 |
return false; |
| 3828 |
} |
| 3829 |
*sourceType = inputType.valTypeOr(RefType::any()).refType(); |
| 3830 |
|
| 3831 |
return push(ValType(ValType::I32)); |
| 3832 |
} |
| 3833 |
|
| 3834 |
template <typename Policy> |
| 3835 |
inline bool OpIter<Policy>::readRefCast(bool nullable, RefType* sourceType, |
| 3836 |
RefType* destType, Value* ref) { |
| 3837 |
MOZ_ASSERT(Classify(op_) == OpKind::RefCast); |
| 3838 |
|
| 3839 |
if (!readHeapType(nullable, destType)) { |
| 3840 |
return false; |
| 3841 |
} |
| 3842 |
|
| 3843 |
StackType inputType; |
| 3844 |
if (!popWithType(destType->topType(), ref, &inputType)) { |
| 3845 |
return false; |
| 3846 |
} |
| 3847 |
*sourceType = inputType.valTypeOr(RefType::any()).refType(); |
| 3848 |
|
| 3849 |
return push(*destType); |
| 3850 |
} |
| 3851 |
|
| 3852 |
// `br_on_cast <flags> <labelRelativeDepth> <rt1> <rt2>` |
| 3853 |
// branches if a reference has a given heap type. |
| 3854 |
// |
| 3855 |
// V6 spec text follows - note that br_on_cast and br_on_cast_fail are both |
| 3856 |
// handled by this function (disambiguated by a flag). |
| 3857 |
// |
| 3858 |
// * `br_on_cast <labelidx> <reftype> <reftype>` branches if a reference has a |
| 3859 |
// given type |
| 3860 |
// - `br_on_cast $l rt1 rt2 : [t0* rt1] -> [t0* rt1\rt2]` |
| 3861 |
// - iff `$l : [t0* rt2]` |
| 3862 |
// - and `rt2 <: rt1` |
| 3863 |
// - passes operand along with branch under target type, plus possible extra |
| 3864 |
// args |
| 3865 |
// - if `rt2` contains `null`, branches on null, otherwise does not |
| 3866 |
// * `br_on_cast_fail <labelidx> <reftype> <reftype>` branches if a reference |
| 3867 |
// does not have a given type |
| 3868 |
// - `br_on_cast_fail $l rt1 rt2 : [t0* rt1] -> [t0* rt2]` |
| 3869 |
// - iff `$l : [t0* rt1\rt2]` |
| 3870 |
// - and `rt2 <: rt1` |
| 3871 |
// - passes operand along with branch, plus possible extra args |
| 3872 |
// - if `rt2` contains `null`, does not branch on null, otherwise does |
| 3873 |
// where: |
| 3874 |
// - `(ref null1? ht1)\(ref null ht2) = (ref ht1)` |
| 3875 |
// - `(ref null1? ht1)\(ref ht2) = (ref null1? ht1)` |
| 3876 |
// |
| 3877 |
// The `rt1\rt2` syntax is a "diff" - it is basically rt1 minus rt2, because a |
| 3878 |
// successful cast to rt2 will branch away. So if rt2 allows null, the result |
| 3879 |
// after a non-branch will be non-null; on the other hand, if rt2 is |
| 3880 |
// non-nullable, the cast will have nothing to say about nullability and the |
| 3881 |
// nullability of rt1 will be preserved. |
| 3882 |
// |
| 3883 |
// `values` will be nonempty after the call, and its last entry will be the |
| 3884 |
// type that causes a branch (rt1\rt2 or rt2, depending). |
| 3885 |
|
| 3886 |
enum class BrOnCastFlags : uint8_t { |
| 3887 |
SourceNullable = 0x1, |
| 3888 |
DestNullable = 0x1 << 1, |
| 3889 |
AllowedMask = uint8_t(SourceNullable) | uint8_t(DestNullable), |
| 3890 |
}; |
| 3891 |
|
| 3892 |
template <typename Policy> |
| 3893 |
inline bool OpIter<Policy>::readBrOnCast(bool onSuccess, |
| 3894 |
uint32_t* labelRelativeDepth, |
| 3895 |
RefType* sourceType, RefType* destType, |
| 3896 |
ResultType* labelType, |
| 3897 |
ValueVector* values) { |
| 3898 |
MOZ_ASSERT(Classify(op_) == OpKind::BrOnCast); |
| 3899 |
|
| 3900 |
uint8_t flags; |
| 3901 |
if (!readFixedU8(&flags)) { |
| 3902 |
return fail("unable to read br_on_cast flags"); |
| 3903 |
} |
| 3904 |
if ((flags & ~uint8_t(BrOnCastFlags::AllowedMask)) != 0) { |
| 3905 |
return fail("invalid br_on_cast flags"); |
| 3906 |
} |
| 3907 |
bool sourceNullable = flags & uint8_t(BrOnCastFlags::SourceNullable); |
| 3908 |
bool destNullable = flags & uint8_t(BrOnCastFlags::DestNullable); |
| 3909 |
|
| 3910 |
if (!readVarU32(labelRelativeDepth)) { |
| 3911 |
return fail("unable to read br_on_cast depth"); |
| 3912 |
} |
| 3913 |
|
| 3914 |
// This is distinct from the actual source type we pop from the stack, which |
| 3915 |
// can be more specific and allow for better optimizations. |
| 3916 |
RefType immediateSourceType; |
| 3917 |
if (!readHeapType(sourceNullable, &immediateSourceType)) { |
| 3918 |
return fail("unable to read br_on_cast source type"); |
| 3919 |
} |
| 3920 |
|
| 3921 |
if (!readHeapType(destNullable, destType)) { |
| 3922 |
return fail("unable to read br_on_cast dest type"); |
| 3923 |
} |
| 3924 |
|
| 3925 |
// Check that source and destination types are compatible |
| 3926 |
if (!checkIsSubtypeOf(*destType, immediateSourceType)) { |
| 3927 |
return fail( |
| 3928 |
"type mismatch: source and destination types for cast are " |
| 3929 |
"incompatible"); |
| 3930 |
} |
| 3931 |
|
| 3932 |
RefType typeOnSuccess = *destType; |
| 3933 |
// This is rt1\rt2 |
| 3934 |
RefType typeOnFail = |
| 3935 |
destNullable ? immediateSourceType.asNonNullable() : immediateSourceType; |
| 3936 |
RefType typeOnBranch = onSuccess ? typeOnSuccess : typeOnFail; |
| 3937 |
RefType typeOnFallthrough = onSuccess ? typeOnFail : typeOnSuccess; |
| 3938 |
|
| 3939 |
// Get the branch target type, which will also determine the type of extra |
| 3940 |
// values that are passed along on branch. |
| 3941 |
Control* block = nullptr; |
| 3942 |
if (!getControl(*labelRelativeDepth, &block)) { |
| 3943 |
return false; |
| 3944 |
} |
| 3945 |
*labelType = block->branchTargetType(); |
| 3946 |
|
| 3947 |
// Check we have at least one value slot in the branch target type, so as to |
| 3948 |
// receive the casted or non-casted type when we branch. |
| 3949 |
const size_t labelTypeNumValues = labelType->length(); |
| 3950 |
if (labelTypeNumValues < 1) { |
| 3951 |
return fail("type mismatch: branch target type has no value types"); |
| 3952 |
} |
| 3953 |
|
| 3954 |
// The last value slot in the branch target type is what is being cast. |
| 3955 |
// This slot is guaranteed to exist by the above check. |
| 3956 |
|
| 3957 |
// Check that the branch target type can accept typeOnBranch. |
| 3958 |
if (!checkIsSubtypeOf(typeOnBranch, (*labelType)[labelTypeNumValues - 1])) { |
| 3959 |
return false; |
| 3960 |
} |
| 3961 |
|
| 3962 |
// Replace the top operand with the result of falling through. Even branching |
| 3963 |
// on success can change the type on top of the stack on fallthrough. |
| 3964 |
Value inputValue; |
| 3965 |
StackType inputType; |
| 3966 |
if (!popWithType(immediateSourceType, &inputValue, &inputType)) { |
| 3967 |
return false; |
| 3968 |
} |
| 3969 |
*sourceType = inputType.valTypeOr(immediateSourceType).refType(); |
| 3970 |
infalliblePush(TypeAndValue(typeOnFallthrough, inputValue)); |
| 3971 |
|
| 3972 |
// Create a copy of the branch target type, with the relevant value slot |
| 3973 |
// replaced by typeOnFallthrough. |
| 3974 |
ValTypeVector fallthroughTypes; |
| 3975 |
if (!labelType->cloneToVector(&fallthroughTypes)) { |
| 3976 |
return false; |
| 3977 |
} |
| 3978 |
fallthroughTypes[labelTypeNumValues - 1] = typeOnFallthrough; |
| 3979 |
|
| 3980 |
return checkTopTypeMatches(ResultType::Vector(fallthroughTypes), values, |
| 3981 |
/*rewriteStackTypes=*/true); |
| 3982 |
} |
| 3983 |
|
| 3984 |
template <typename Policy> |
| 3985 |
inline bool OpIter<Policy>::readRefConversion(RefType operandType, |
| 3986 |
RefType resultType, |
| 3987 |
Value* operandValue) { |
| 3988 |
MOZ_ASSERT(Classify(op_) == OpKind::RefConversion); |
| 3989 |
|
| 3990 |
StackType actualOperandType; |
| 3991 |
if (!popWithType(ValType(operandType), operandValue, &actualOperandType)) { |
| 3992 |
return false; |
| 3993 |
} |
| 3994 |
|
| 3995 |
// The result nullability is the same as the operand nullability |
| 3996 |
bool outputNullable = actualOperandType.isNullableAsOperand(); |
| 3997 |
infalliblePush(ValType(resultType.withIsNullable(outputNullable))); |
| 3998 |
return true; |
| 3999 |
} |
| 4000 |
|
| 4001 |
#ifdef ENABLE_WASM_SIMD |
| 4002 |
|
| 4003 |
template <typename Policy> |
| 4004 |
inline bool OpIter<Policy>::readLaneIndex(uint32_t inputLanes, |
| 4005 |
uint32_t* laneIndex) { |
| 4006 |
uint8_t tmp; |
| 4007 |
if (!readFixedU8(&tmp)) { |
| 4008 |
return false; // Caller signals error |
| 4009 |
} |
| 4010 |
if (tmp >= inputLanes) { |
| 4011 |
return false; |
| 4012 |
} |
| 4013 |
*laneIndex = tmp; |
| 4014 |
return true; |
| 4015 |
} |
| 4016 |
|
| 4017 |
template <typename Policy> |
| 4018 |
inline bool OpIter<Policy>::readExtractLane(ValType resultType, |
| 4019 |
uint32_t inputLanes, |
| 4020 |
uint32_t* laneIndex, Value* input) { |
| 4021 |
MOZ_ASSERT(Classify(op_) == OpKind::ExtractLane); |
| 4022 |
|
| 4023 |
if (!readLaneIndex(inputLanes, laneIndex)) { |
| 4024 |
return fail("missing or invalid extract_lane lane index"); |
| 4025 |
} |
| 4026 |
|
| 4027 |
if (!popWithType(ValType::V128, input)) { |
| 4028 |
return false; |
| 4029 |
} |
| 4030 |
|
| 4031 |
infalliblePush(resultType); |
| 4032 |
|
| 4033 |
return true; |
| 4034 |
} |
| 4035 |
|
| 4036 |
template <typename Policy> |
| 4037 |
inline bool OpIter<Policy>::readReplaceLane(ValType operandType, |
| 4038 |
uint32_t inputLanes, |
| 4039 |
uint32_t* laneIndex, |
| 4040 |
Value* baseValue, Value* operand) { |
| 4041 |
MOZ_ASSERT(Classify(op_) == OpKind::ReplaceLane); |
| 4042 |
|
| 4043 |
if (!readLaneIndex(inputLanes, laneIndex)) { |
| 4044 |
return fail("missing or invalid replace_lane lane index"); |
| 4045 |
} |
| 4046 |
|
| 4047 |
if (!popWithType(operandType, operand)) { |
| 4048 |
return false; |
| 4049 |
} |
| 4050 |
|
| 4051 |
if (!popWithType(ValType::V128, baseValue)) { |
| 4052 |
return false; |
| 4053 |
} |
| 4054 |
|
| 4055 |
infalliblePush(ValType::V128); |
| 4056 |
|
| 4057 |
return true; |
| 4058 |
} |
| 4059 |
|
| 4060 |
template <typename Policy> |
| 4061 |
inline bool OpIter<Policy>::readVectorShift(Value* baseValue, Value* shift) { |
| 4062 |
MOZ_ASSERT(Classify(op_) == OpKind::VectorShift); |
| 4063 |
|
| 4064 |
if (!popWithType(ValType::I32, shift)) { |
| 4065 |
return false; |
| 4066 |
} |
| 4067 |
|
| 4068 |
if (!popWithType(ValType::V128, baseValue)) { |
| 4069 |
return false; |
| 4070 |
} |
| 4071 |
|
| 4072 |
infalliblePush(ValType::V128); |
| 4073 |
|
| 4074 |
return true; |
| 4075 |
} |
| 4076 |
|
| 4077 |
template <typename Policy> |
| 4078 |
inline bool OpIter<Policy>::readVectorShuffle(Value* v1, Value* v2, |
| 4079 |
V128* selectMask) { |
| 4080 |
MOZ_ASSERT(Classify(op_) == OpKind::VectorShuffle); |
| 4081 |
|
| 4082 |
for (unsigned char& byte : selectMask->bytes) { |
| 4083 |
uint8_t tmp; |
| 4084 |
if (!readFixedU8(&tmp)) { |
| 4085 |
return fail("unable to read shuffle index"); |
| 4086 |
} |
| 4087 |
if (tmp > 31) { |
| 4088 |
return fail("shuffle index out of range"); |
| 4089 |
} |
| 4090 |
byte = tmp; |
| 4091 |
} |
| 4092 |
|
| 4093 |
if (!popWithType(ValType::V128, v2)) { |
| 4094 |
return false; |
| 4095 |
} |
| 4096 |
|
| 4097 |
if (!popWithType(ValType::V128, v1)) { |
| 4098 |
return false; |
| 4099 |
} |
| 4100 |
|
| 4101 |
infalliblePush(ValType::V128); |
| 4102 |
|
| 4103 |
return true; |
| 4104 |
} |
| 4105 |
|
| 4106 |
template <typename Policy> |
| 4107 |
inline bool OpIter<Policy>::readV128Const(V128* value) { |
| 4108 |
MOZ_ASSERT(Classify(op_) == OpKind::V128Const); |
| 4109 |
|
| 4110 |
if (!d_.readV128Const(value)) { |
| 4111 |
return false; |
| 4112 |
} |
| 4113 |
|
| 4114 |
return push(ValType::V128); |
| 4115 |
} |
| 4116 |
|
| 4117 |
template <typename Policy> |
| 4118 |
inline bool OpIter<Policy>::readLoadSplat(uint32_t byteSize, |
| 4119 |
LinearMemoryAddress<Value>* addr) { |
| 4120 |
MOZ_ASSERT(Classify(op_) == OpKind::Load); |
| 4121 |
|
| 4122 |
if (!readLinearMemoryAddress(byteSize, addr)) { |
| 4123 |
return false; |
| 4124 |
} |
| 4125 |
|
| 4126 |
infalliblePush(ValType::V128); |
| 4127 |
|
| 4128 |
return true; |
| 4129 |
} |
| 4130 |
|
| 4131 |
template <typename Policy> |
| 4132 |
inline bool OpIter<Policy>::readLoadExtend(LinearMemoryAddress<Value>* addr) { |
| 4133 |
MOZ_ASSERT(Classify(op_) == OpKind::Load); |
| 4134 |
|
| 4135 |
if (!readLinearMemoryAddress(/*byteSize=*/8, addr)) { |
| 4136 |
return false; |
| 4137 |
} |
| 4138 |
|
| 4139 |
infalliblePush(ValType::V128); |
| 4140 |
|
| 4141 |
return true; |
| 4142 |
} |
| 4143 |
|
| 4144 |
template <typename Policy> |
| 4145 |
inline bool OpIter<Policy>::readLoadLane(uint32_t byteSize, |
| 4146 |
LinearMemoryAddress<Value>* addr, |
| 4147 |
uint32_t* laneIndex, Value* input) { |
| 4148 |
MOZ_ASSERT(Classify(op_) == OpKind::LoadLane); |
| 4149 |
|
| 4150 |
if (!popWithType(ValType::V128, input)) { |
| 4151 |
return false; |
| 4152 |
} |
| 4153 |
|
| 4154 |
if (!readLinearMemoryAddress(byteSize, addr)) { |
| 4155 |
return false; |
| 4156 |
} |
| 4157 |
|
| 4158 |
uint32_t inputLanes = 16 / byteSize; |
| 4159 |
if (!readLaneIndex(inputLanes, laneIndex)) { |
| 4160 |
return fail("missing or invalid load_lane lane index"); |
| 4161 |
} |
| 4162 |
|
| 4163 |
infalliblePush(ValType::V128); |
| 4164 |
|
| 4165 |
return true; |
| 4166 |
} |
| 4167 |
|
| 4168 |
template <typename Policy> |
| 4169 |
inline bool OpIter<Policy>::readStoreLane(uint32_t byteSize, |
| 4170 |
LinearMemoryAddress<Value>* addr, |
| 4171 |
uint32_t* laneIndex, Value* input) { |
| 4172 |
MOZ_ASSERT(Classify(op_) == OpKind::StoreLane); |
| 4173 |
|
| 4174 |
if (!popWithType(ValType::V128, input)) { |
| 4175 |
return false; |
| 4176 |
} |
| 4177 |
|
| 4178 |
if (!readLinearMemoryAddress(byteSize, addr)) { |
| 4179 |
return false; |
| 4180 |
} |
| 4181 |
|
| 4182 |
uint32_t inputLanes = 16 / byteSize; |
| 4183 |
if (!readLaneIndex(inputLanes, laneIndex)) { |
| 4184 |
return fail("missing or invalid store_lane lane index"); |
| 4185 |
} |
| 4186 |
|
| 4187 |
return true; |
| 4188 |
} |
| 4189 |
|
| 4190 |
#endif // ENABLE_WASM_SIMD |
| 4191 |
|
| 4192 |
#ifdef ENABLE_WASM_JSPI |
| 4193 |
template <typename Policy> |
| 4194 |
inline bool OpIter<Policy>::readStackSwitch(StackSwitchKind* kind, |
| 4195 |
Value* suspender, Value* fn, |
| 4196 |
Value* data) { |
| 4197 |
MOZ_ASSERT(Classify(op_) == OpKind::StackSwitch); |
| 4198 |
MOZ_ASSERT(codeMeta_.jsPromiseIntegrationEnabled()); |
| 4199 |
uint32_t kind_; |
| 4200 |
if (!d_.readVarU32(&kind_)) { |
| 4201 |
return false; |
| 4202 |
} |
| 4203 |
*kind = StackSwitchKind(kind_); |
| 4204 |
if (!popWithType(ValType(RefType::any()), data)) { |
| 4205 |
return false; |
| 4206 |
} |
| 4207 |
StackType stackType; |
| 4208 |
if (!popWithType(ValType(RefType::func()), fn, &stackType)) { |
| 4209 |
return false; |
| 4210 |
} |
| 4211 |
# if DEBUG |
| 4212 |
// Verify that the function takes suspender and data as parameters. |
| 4213 |
MOZ_ASSERT((*kind == StackSwitchKind::ContinueOnSuspendable) == |
| 4214 |
stackType.isNullableAsOperand()); |
| 4215 |
if (!stackType.isNullableAsOperand()) { |
| 4216 |
ValType valType = stackType.valType(); |
| 4217 |
MOZ_ASSERT(valType.isRefType() && valType.typeDef()->isFuncType()); |
| 4218 |
const FuncType& func = valType.typeDef()->funcType(); |
| 4219 |
MOZ_ASSERT(func.args().length() == 2 && func.arg(0).isExternRef() && |
| 4220 |
ValType::isSubTypeOf(func.arg(1), RefType::any())); |
| 4221 |
MOZ_ASSERT_IF(*kind != StackSwitchKind::SwitchToMain, |
| 4222 |
func.results().empty()); |
| 4223 |
MOZ_ASSERT_IF(*kind == StackSwitchKind::SwitchToMain, |
| 4224 |
func.results().length() == 1 && func.result(0).isAnyRef()); |
| 4225 |
} |
| 4226 |
# endif |
| 4227 |
if (!popWithType(ValType(RefType::extern_()), suspender)) { |
| 4228 |
return false; |
| 4229 |
} |
| 4230 |
// Returns a value only for SwitchToMain. |
| 4231 |
if (*kind == StackSwitchKind::SwitchToMain) { |
| 4232 |
return push(RefType::extern_()); |
| 4233 |
} |
| 4234 |
return true; |
| 4235 |
} |
| 4236 |
#endif |
| 4237 |
|
| 4238 |
template <typename Policy> |
| 4239 |
inline bool OpIter<Policy>::readCallBuiltinModuleFunc( |
| 4240 |
const BuiltinModuleFunc** builtinModuleFunc, ValueVector* params) { |
| 4241 |
MOZ_ASSERT(Classify(op_) == OpKind::CallBuiltinModuleFunc); |
| 4242 |
|
| 4243 |
uint32_t id; |
| 4244 |
if (!d_.readVarU32(&id)) { |
| 4245 |
return false; |
| 4246 |
} |
| 4247 |
|
| 4248 |
if (id >= uint32_t(BuiltinModuleFuncId::Limit)) { |
| 4249 |
return fail("index out of range"); |
| 4250 |
} |
| 4251 |
|
| 4252 |
*builtinModuleFunc = &BuiltinModuleFuncs::getFromId(BuiltinModuleFuncId(id)); |
| 4253 |
const FuncType& funcType = *(*builtinModuleFunc)->funcType(); |
| 4254 |
if (!popCallArgs(funcType.args(), params)) { |
| 4255 |
return false; |
| 4256 |
} |
| 4257 |
|
| 4258 |
return push(ResultType::Vector(funcType.results())); |
| 4259 |
} |
| 4260 |
|
| 4261 |
} // namespace wasm |
| 4262 |
} // namespace js |
| 4263 |
|
| 4264 |
static_assert(std::is_trivially_copyable< |
| 4265 |
js::wasm::TypeAndValueT<mozilla::Nothing>>::value, |
| 4266 |
"Must be trivially copyable"); |
| 4267 |
static_assert(std::is_trivially_destructible< |
| 4268 |
js::wasm::TypeAndValueT<mozilla::Nothing>>::value, |
| 4269 |
"Must be trivially destructible"); |
| 4270 |
|
| 4271 |
static_assert(std::is_trivially_copyable< |
| 4272 |
js::wasm::ControlStackEntry<mozilla::Nothing>>::value, |
| 4273 |
"Must be trivially copyable"); |
| 4274 |
static_assert(std::is_trivially_destructible< |
| 4275 |
js::wasm::ControlStackEntry<mozilla::Nothing>>::value, |
| 4276 |
"Must be trivially destructible"); |
| 4277 |
|
| 4278 |
#endif // wasm_op_iter_h |