| 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 2023 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_anyref_h |
| 20 |
#define wasm_anyref_h |
| 21 |
|
| 22 |
#include "mozilla/FloatingPoint.h" |
| 23 |
|
| 24 |
#include <utility> |
| 25 |
|
| 26 |
#include "js/HeapAPI.h" |
| 27 |
#include "js/RootingAPI.h" |
| 28 |
#include "js/TypeDecls.h" |
| 29 |
#include "js/Value.h" |
| 30 |
|
| 31 |
// #include "NamespaceImports.h" |
| 32 |
|
| 33 |
class JSObject; |
| 34 |
class JSString; |
| 35 |
|
| 36 |
namespace js { |
| 37 |
namespace gc { |
| 38 |
struct Cell; |
| 39 |
}; // namespace gc |
| 40 |
|
| 41 |
namespace wasm { |
| 42 |
|
| 43 |
// [SMDOC] AnyRef |
| 44 |
// |
| 45 |
// An AnyRef is a boxed value that can represent any wasm reference type and any |
| 46 |
// host type that the host system allows to flow into and out of wasm |
| 47 |
// transparently. It is a pointer-sized datum that has the same representation |
| 48 |
// as all its subtypes (funcref, externref, eqref, (ref T), et al) due to the |
| 49 |
// non-coercive subtyping of the wasm type system. |
| 50 |
// |
| 51 |
// The C++/wasm boundary always uses a 'void*' type to express AnyRef values, to |
| 52 |
// emphasize the pointer-ness of the value. The C++ code must transform the |
| 53 |
// void* into an AnyRef by calling AnyRef::fromCompiledCode(), and transform an |
| 54 |
// AnyRef into a void* by calling AnyRef::toCompiledCode(). Once in C++, we use |
| 55 |
// AnyRef everywhere. A JS Value is transformed into an AnyRef by calling |
| 56 |
// AnyRef::fromJSValue(), and the AnyRef is transformed into a JS Value by |
| 57 |
// calling AnyRef::toJSValue(). |
| 58 |
// |
| 59 |
// NOTE that AnyRef values may point to GC'd storage and as such need to be |
| 60 |
// rooted if they are kept live in boxed form across code that may cause GC! |
| 61 |
// Use RootedAnyRef / HandleAnyRef / MutableHandleAnyRef where necessary. |
| 62 |
// |
| 63 |
// The lowest bits of the pointer value are used for tagging, to allow for some |
| 64 |
// representation optimizations and to distinguish various types. |
| 65 |
// |
| 66 |
// The current tagging scheme is: |
| 67 |
// if (pointer == 0) then 'null' |
| 68 |
// if (pointer & 0x1) then 'i31' |
| 69 |
// if (pointer & 0x2) then 'string' |
| 70 |
// else 'object' |
| 71 |
// |
| 72 |
// NOTE: there is sequencing required when checking tags. If bit 0x1 is set, |
| 73 |
// then bit 0x2 is part of the i31 value and does not imply string. |
| 74 |
// |
| 75 |
// An i31ref value has no sign interpretation within wasm, where instructions |
| 76 |
// specify the signedness. When converting to/from a JS value, an i31ref value |
| 77 |
// is treated as a signed 31-bit value. |
| 78 |
|
| 79 |
// The kind of value stored in an AnyRef. This is not 1:1 with the pointer tag |
| 80 |
// of AnyRef as this separates the 'Null' and 'Object' cases which are |
| 81 |
// collapsed in the pointer tag. |
| 82 |
enum class AnyRefKind : uint8_t { |
| 83 |
Null, |
| 84 |
Object, |
| 85 |
String, |
| 86 |
I31, |
| 87 |
}; |
| 88 |
|
| 89 |
// The pointer tag of an AnyRef. |
| 90 |
enum class AnyRefTag : uint8_t { |
| 91 |
// This value is either a JSObject& or a null pointer. |
| 92 |
ObjectOrNull = 0x0, |
| 93 |
// This value is a 31-bit integer. |
| 94 |
I31 = 0x1, |
| 95 |
// This value is a JSString*. |
| 96 |
String = 0x2, |
| 97 |
}; |
| 98 |
|
| 99 |
// A reference to any wasm reference type or host (JS) value. AnyRef is |
| 100 |
// optimized for efficient access to objects, strings, and 31-bit integers. |
| 101 |
// |
| 102 |
// See the above documentation comment for more details. |
| 103 |
class AnyRef { |
| 104 |
uintptr_t value_; |
| 105 |
|
| 106 |
// Get the pointer tag stored in value_. |
| 107 |
AnyRefTag pointerTag() const { return GetUintptrTag(value_); } |
| 108 |
|
| 109 |
explicit AnyRef(uintptr_t value) : value_(value) {} |
| 110 |
|
| 111 |
static constexpr uintptr_t TagUintptr(uintptr_t value, AnyRefTag tag) { |
| 112 |
MOZ_ASSERT(!(value & TagMask)); |
| 113 |
return value | uintptr_t(tag); |
| 114 |
} |
| 115 |
static constexpr uintptr_t UntagUintptr(uintptr_t value) { |
| 116 |
return value & ~TagMask; |
| 117 |
} |
| 118 |
static constexpr AnyRefTag GetUintptrTag(uintptr_t value) { |
| 119 |
// Mask off all but the lowest two-bits (the tag) |
| 120 |
uintptr_t rawTag = value & TagMask; |
| 121 |
// If the lowest bit is set, we want to normalize and only return |
| 122 |
// AnyRefTag::I31. Mask off the high-bit iff the low-bit was set. |
| 123 |
uintptr_t normalizedI31 = rawTag & ~(value << 1); |
| 124 |
return AnyRefTag(normalizedI31); |
| 125 |
} |
| 126 |
|
| 127 |
// Given a 32-bit signed integer within 31-bit signed bounds, turn it into |
| 128 |
// an AnyRef. |
| 129 |
static AnyRef fromInt32(int32_t value) { |
| 130 |
MOZ_ASSERT(!int32NeedsBoxing(value)); |
| 131 |
return AnyRef::fromUint32Truncate(uint32_t(value)); |
| 132 |
} |
| 133 |
|
| 134 |
public: |
| 135 |
static constexpr uintptr_t TagMask = 0x3; |
| 136 |
static constexpr uintptr_t TagShift = 2; |
| 137 |
static_assert(TagShift <= gc::CellAlignShift, "not enough free bits"); |
| 138 |
// A mask for getting the GC thing an AnyRef represents. |
| 139 |
static constexpr uintptr_t GCThingMask = ~TagMask; |
| 140 |
// A combined mask for getting the gc::Chunk for an AnyRef that is a GC |
| 141 |
// thing. |
| 142 |
static constexpr uintptr_t GCThingChunkMask = |
| 143 |
GCThingMask & ~js::gc::ChunkMask; |
| 144 |
|
| 145 |
// The representation of a null reference value throughout the compiler for |
| 146 |
// when we need an integer constant. This is asserted to be equivalent to |
| 147 |
// nullptr in wasm::Init. |
| 148 |
static constexpr uintptr_t NullRefValue = 0; |
| 149 |
static constexpr uintptr_t InvalidRefValue = UINTPTR_MAX << TagShift; |
| 150 |
|
| 151 |
// The inclusive maximum 31-bit signed integer, 2^30 - 1. |
| 152 |
static constexpr int32_t MaxI31Value = (2 << 29) - 1; |
| 153 |
// The inclusive minimum 31-bit signed integer, -2^30. |
| 154 |
static constexpr int32_t MinI31Value = -(2 << 29); |
| 155 |
|
| 156 |
explicit AnyRef() : value_(NullRefValue) {} |
| 157 |
MOZ_IMPLICIT AnyRef(std::nullptr_t) : value_(NullRefValue) {} |
| 158 |
|
| 159 |
// The null AnyRef value. |
| 160 |
static AnyRef null() { return AnyRef(NullRefValue); } |
| 161 |
|
| 162 |
// An invalid AnyRef cannot arise naturally from wasm and so can be used as |
| 163 |
// a sentinel value to indicate failure from an AnyRef-returning function. |
| 164 |
static AnyRef invalid() { return AnyRef(InvalidRefValue); } |
| 165 |
|
| 166 |
// Given a JSObject* that comes from JS, turn it into AnyRef. |
| 167 |
static AnyRef fromJSObjectOrNull(JSObject* objectOrNull) { |
| 168 |
MOZ_ASSERT(GetUintptrTag((uintptr_t)objectOrNull) == |
| 169 |
AnyRefTag::ObjectOrNull); |
| 170 |
return AnyRef((uintptr_t)objectOrNull); |
| 171 |
} |
| 172 |
|
| 173 |
// Given a JSObject& that comes from JS, turn it into AnyRef. |
| 174 |
static AnyRef fromJSObject(JSObject& object) { |
| 175 |
MOZ_ASSERT(GetUintptrTag((uintptr_t)&object) == AnyRefTag::ObjectOrNull); |
| 176 |
return AnyRef((uintptr_t)&object); |
| 177 |
} |
| 178 |
|
| 179 |
// Given a JSString* that comes from JS, turn it into AnyRef. |
| 180 |
static AnyRef fromJSString(JSString* string) { |
| 181 |
return AnyRef(TagUintptr((uintptr_t)string, AnyRefTag::String)); |
| 182 |
} |
| 183 |
|
| 184 |
// Given a void* that comes from compiled wasm code, turn it into AnyRef. |
| 185 |
static AnyRef fromCompiledCode(void* pointer) { |
| 186 |
return AnyRef((uintptr_t)pointer); |
| 187 |
} |
| 188 |
|
| 189 |
// Given a JS value, turn it into AnyRef. This returns false if boxing the |
| 190 |
// value failed due to an OOM. |
| 191 |
static bool fromJSValue(JSContext* cx, JS::HandleValue value, |
| 192 |
JS::MutableHandle<AnyRef> result); |
| 193 |
|
| 194 |
// fromUint32Truncate will produce an i31 from an int32 by truncating the |
| 195 |
// highest bit. For values in the 31-bit range, this losslessly preserves the |
| 196 |
// value. For values outside the 31-bit range, this performs 31-bit |
| 197 |
// wraparound. |
| 198 |
// |
| 199 |
// There are four cases here based on the two high bits: |
| 200 |
// 00 - [0, MaxI31Value] |
| 201 |
// 01 - (MaxI31Value, INT32_MAX] |
| 202 |
// 10 - [INT32_MIN, MinI31Value) |
| 203 |
// 11 - [MinI31Value, -1] |
| 204 |
// |
| 205 |
// The middle two cases can be ruled out if the value is guaranteed to be |
| 206 |
// within the i31 range. Therefore if we truncate the high bit upon converting |
| 207 |
// to i31 and perform a signed widening upon converting back to i32, we can |
| 208 |
// losslessly represent all i31 values. |
| 209 |
static AnyRef fromUint32Truncate(uint32_t value) { |
| 210 |
// See 64-bit GPRs carrying 32-bit values invariants in MacroAssember.h |
| 211 |
#if defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_X64) || \ |
| 212 |
defined(JS_CODEGEN_ARM64) |
| 213 |
// Truncate the value to the 31-bit value size. |
| 214 |
uintptr_t wideValue = uintptr_t(value & 0x7FFFFFFF); |
| 215 |
#elif defined(JS_CODEGEN_LOONG64) || defined(JS_CODEGEN_MIPS64) || \ |
| 216 |
defined(JS_CODEGEN_RISCV64) |
| 217 |
// Sign extend the value to the native pointer size. |
| 218 |
uintptr_t wideValue = uintptr_t(int64_t((uint64_t(value) << 33)) >> 33); |
| 219 |
#elif !defined(JS_64BIT) |
| 220 |
// Transfer 32-bit value as is. |
| 221 |
uintptr_t wideValue = (uintptr_t)value; |
| 222 |
#else |
| 223 |
# error "unknown architecture" |
| 224 |
#endif |
| 225 |
|
| 226 |
// Left shift the value by 1, truncating the high bit. |
| 227 |
uintptr_t shiftedValue = wideValue << 1; |
| 228 |
uintptr_t taggedValue = shiftedValue | (uintptr_t)AnyRefTag::I31; |
| 229 |
#ifdef JS_64BIT |
| 230 |
debugAssertCanonicalInt32(taggedValue); |
| 231 |
#endif |
| 232 |
return AnyRef(taggedValue); |
| 233 |
} |
| 234 |
|
| 235 |
#ifdef JS_64BIT |
| 236 |
// Ensure the value fits into a 32-bits integer on 64-bits platforms. |
| 237 |
static void debugAssertCanonicalInt32(uintptr_t value) { |
| 238 |
# ifdef DEBUG |
| 239 |
# if defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_ARM64) |
| 240 |
MOZ_ASSERT(value <= UINT32_MAX); |
| 241 |
# endif |
| 242 |
# endif |
| 243 |
} |
| 244 |
#endif |
| 245 |
|
| 246 |
static bool int32NeedsBoxing(int32_t value) { |
| 247 |
// We can represent every signed 31-bit number without boxing |
| 248 |
return value < MinI31Value || value > MaxI31Value; |
| 249 |
} |
| 250 |
|
| 251 |
static bool doubleNeedsBoxing(double value) { |
| 252 |
int32_t intValue; |
| 253 |
if (!mozilla::NumberIsInt32(value, &intValue)) { |
| 254 |
return true; |
| 255 |
} |
| 256 |
return int32NeedsBoxing(value); |
| 257 |
} |
| 258 |
|
| 259 |
// Returns whether a JS value will need to be boxed. |
| 260 |
static bool valueNeedsBoxing(JS::HandleValue value) { |
| 261 |
if (value.isObjectOrNull() || value.isString()) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
if (value.isInt32()) { |
| 265 |
return int32NeedsBoxing(value.toInt32()); |
| 266 |
} |
| 267 |
if (value.isDouble()) { |
| 268 |
return doubleNeedsBoxing(value.toDouble()); |
| 269 |
} |
| 270 |
return true; |
| 271 |
} |
| 272 |
|
| 273 |
// Box a JS Value that needs boxing. |
| 274 |
static JSObject* boxValue(JSContext* cx, JS::HandleValue value); |
| 275 |
|
| 276 |
bool operator==(const AnyRef& rhs) const { |
| 277 |
return this->value_ == rhs.value_; |
| 278 |
} |
| 279 |
bool operator!=(const AnyRef& rhs) const { return !(*this == rhs); } |
| 280 |
|
| 281 |
// Check if this AnyRef is the invalid value. |
| 282 |
bool isInvalid() const { return *this == AnyRef::invalid(); } |
| 283 |
|
| 284 |
AnyRefKind kind() const { |
| 285 |
if (value_ == NullRefValue) { |
| 286 |
return AnyRefKind::Null; |
| 287 |
} |
| 288 |
switch (pointerTag()) { |
| 289 |
case AnyRefTag::ObjectOrNull: { |
| 290 |
// The invalid pattern uses the ObjectOrNull tag, check for it here. |
| 291 |
MOZ_ASSERT(!isInvalid()); |
| 292 |
// We ruled out the null case above |
| 293 |
return AnyRefKind::Object; |
| 294 |
} |
| 295 |
case AnyRefTag::String: { |
| 296 |
return AnyRefKind::String; |
| 297 |
} |
| 298 |
case AnyRefTag::I31: { |
| 299 |
return AnyRefKind::I31; |
| 300 |
} |
| 301 |
default: { |
| 302 |
MOZ_CRASH("unknown AnyRef tag"); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
bool isNull() const { return value_ == NullRefValue; } |
| 308 |
bool isGCThing() const { return !isNull() && !isI31(); } |
| 309 |
bool isJSObject() const { return kind() == AnyRefKind::Object; } |
| 310 |
bool isJSString() const { return kind() == AnyRefKind::String; } |
| 311 |
bool isI31() const { return kind() == AnyRefKind::I31; } |
| 312 |
|
| 313 |
gc::Cell* toGCThing() const { |
| 314 |
MOZ_ASSERT(isGCThing()); |
| 315 |
return (gc::Cell*)UntagUintptr(value_); |
| 316 |
} |
| 317 |
JSObject& toJSObject() const { |
| 318 |
MOZ_ASSERT(isJSObject()); |
| 319 |
return *(JSObject*)value_; |
| 320 |
} |
| 321 |
JSObject* toJSObjectOrNull() const { |
| 322 |
MOZ_ASSERT(!isInvalid()); |
| 323 |
MOZ_ASSERT(pointerTag() == AnyRefTag::ObjectOrNull); |
| 324 |
return (JSObject*)value_; |
| 325 |
} |
| 326 |
JSString* toJSString() const { |
| 327 |
MOZ_ASSERT(isJSString()); |
| 328 |
return (JSString*)UntagUintptr(value_); |
| 329 |
} |
| 330 |
// Unpack an i31, interpreting the integer as signed. |
| 331 |
int32_t toI31() const { |
| 332 |
MOZ_ASSERT(isI31()); |
| 333 |
#ifdef JS_64BIT |
| 334 |
debugAssertCanonicalInt32(value_); |
| 335 |
#endif |
| 336 |
// On 64-bit targets, we only care about the low 4-bytes. |
| 337 |
uint32_t truncatedValue; |
| 338 |
memcpy(&truncatedValue, &value_, sizeof(uint32_t)); |
| 339 |
// Perform a right arithmetic shift (see AnyRef::fromI31 for more details), |
| 340 |
// avoiding undefined behavior by using an unsigned type. |
| 341 |
uint32_t shiftedValue = value_ >> 1; |
| 342 |
if ((truncatedValue & (1 << 31)) != 0) { |
| 343 |
shiftedValue |= (1 << 31); |
| 344 |
} |
| 345 |
// Perform a bitwise cast to see the result as a signed value. |
| 346 |
return mozilla::BitwiseCast<int32_t>(shiftedValue); |
| 347 |
} |
| 348 |
|
| 349 |
// Convert from AnyRef to a JS Value. This currently does not require any |
| 350 |
// allocation. If this changes in the future, this function will become |
| 351 |
// more complicated. |
| 352 |
JS::Value toJSValue() const; |
| 353 |
|
| 354 |
// Get the raw value for returning to wasm code. |
| 355 |
void* forCompiledCode() const { return (void*)value_; } |
| 356 |
|
| 357 |
// Get the raw value for diagnostics. |
| 358 |
uintptr_t rawValue() const { return value_; } |
| 359 |
|
| 360 |
// Internal details of the boxing format used by WasmStubs.cpp |
| 361 |
static const JSClass* valueBoxClass(); |
| 362 |
static size_t valueBoxOffsetOfValue(); |
| 363 |
}; |
| 364 |
|
| 365 |
using RootedAnyRef = JS::Rooted<AnyRef>; |
| 366 |
using HandleAnyRef = JS::Handle<AnyRef>; |
| 367 |
using MutableHandleAnyRef = JS::MutableHandle<AnyRef>; |
| 368 |
|
| 369 |
} // namespace wasm |
| 370 |
|
| 371 |
template <class Wrapper> |
| 372 |
class WrappedPtrOperations<wasm::AnyRef, Wrapper> { |
| 373 |
const wasm::AnyRef& value() const { |
| 374 |
return static_cast<const Wrapper*>(this)->get(); |
| 375 |
} |
| 376 |
|
| 377 |
public: |
| 378 |
bool isNull() const { return value().isNull(); } |
| 379 |
bool isI31() const { return value().isI31(); } |
| 380 |
bool isJSObject() const { return value().isJSObject(); } |
| 381 |
bool isJSString() const { return value().isJSString(); } |
| 382 |
JSObject& toJSObject() const { return value().toJSObject(); } |
| 383 |
JSString* toJSString() const { return value().toJSString(); } |
| 384 |
}; |
| 385 |
|
| 386 |
// If the Value is a GC pointer type, call |f| with the pointer cast to that |
| 387 |
// type and return the result wrapped in a Maybe, otherwise return None(). |
| 388 |
template <typename F> |
| 389 |
auto MapGCThingTyped(const wasm::AnyRef& val, F&& f) { |
| 390 |
switch (val.kind()) { |
| 391 |
case wasm::AnyRefKind::Object: |
| 392 |
return mozilla::Some(f(&val.toJSObject())); |
| 393 |
case wasm::AnyRefKind::String: |
| 394 |
return mozilla::Some(f(val.toJSString())); |
| 395 |
case wasm::AnyRefKind::I31: |
| 396 |
case wasm::AnyRefKind::Null: { |
| 397 |
using ReturnType = decltype(f(static_cast<JSObject*>(nullptr))); |
| 398 |
return mozilla::Maybe<ReturnType>(); |
| 399 |
} |
| 400 |
} |
| 401 |
MOZ_CRASH(); |
| 402 |
} |
| 403 |
|
| 404 |
template <typename F> |
| 405 |
bool ApplyGCThingTyped(const wasm::AnyRef& val, F&& f) { |
| 406 |
return MapGCThingTyped(val, |
| 407 |
[&f](auto t) { |
| 408 |
f(t); |
| 409 |
return true; |
| 410 |
}) |
| 411 |
.isSome(); |
| 412 |
} |
| 413 |
|
| 414 |
} // namespace js |
| 415 |
|
| 416 |
namespace JS { |
| 417 |
|
| 418 |
template <> |
| 419 |
struct GCPolicy<js::wasm::AnyRef> { |
| 420 |
static void trace(JSTracer* trc, js::wasm::AnyRef* v, const char* name) { |
| 421 |
// This should only be called as part of root marking since that's the only |
| 422 |
// time we should trace unbarriered GC thing pointers. This will assert if |
| 423 |
// called at other times. |
| 424 |
TraceRoot(trc, v, name); |
| 425 |
} |
| 426 |
static bool isValid(const js::wasm::AnyRef& v) { |
| 427 |
return !v.isGCThing() || js::gc::IsCellPointerValid(v.toGCThing()); |
| 428 |
} |
| 429 |
}; |
| 430 |
|
| 431 |
} // namespace JS |
| 432 |
|
| 433 |
#endif // wasm_anyref_h |