| 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 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef wasm_WasmGcObject_h |
| 8 |
#define wasm_WasmGcObject_h |
| 9 |
|
| 10 |
#include "mozilla/Attributes.h" |
| 11 |
#include "mozilla/CheckedInt.h" |
| 12 |
#include "mozilla/Maybe.h" |
| 13 |
|
| 14 |
#include "gc/GCProbes.h" |
| 15 |
#include "gc/Pretenuring.h" |
| 16 |
#include "gc/ZoneAllocator.h" // AddCellMemory |
| 17 |
#include "vm/JSContext.h" |
| 18 |
#include "vm/JSObject.h" |
| 19 |
#include "vm/Probes.h" |
| 20 |
#include "wasm/WasmInstanceData.h" |
| 21 |
#include "wasm/WasmMemory.h" |
| 22 |
#include "wasm/WasmTypeDef.h" |
| 23 |
#include "wasm/WasmValType.h" |
| 24 |
|
| 25 |
namespace js::wasm { |
| 26 |
|
| 27 |
// For trailer blocks whose owning Wasm{Struct,Array}Objects make it into the |
| 28 |
// tenured heap, we have to tell the tenured heap how big those trailers are |
| 29 |
// in order to get major GCs to happen sufficiently frequently. In an attempt |
| 30 |
// to make the numbers more accurate, for each block we overstate the size by |
| 31 |
// the following amount, on the assumption that: |
| 32 |
// |
| 33 |
// * mozjemalloc has an overhead of at least one word per block |
| 34 |
// |
| 35 |
// * the malloc-cache mechanism rounds up small block sizes to the nearest 16; |
| 36 |
// hence the average increase is 16 / 2. |
| 37 |
static const size_t TrailerBlockOverhead = (16 / 2) + (1 * sizeof(void*)); |
| 38 |
|
| 39 |
} // namespace js::wasm |
| 40 |
|
| 41 |
namespace js { |
| 42 |
|
| 43 |
//========================================================================= |
| 44 |
// WasmGcObject |
| 45 |
|
| 46 |
class WasmGcObject : public JSObject { |
| 47 |
protected: |
| 48 |
const wasm::SuperTypeVector* superTypeVector_; |
| 49 |
|
| 50 |
static const ObjectOps objectOps_; |
| 51 |
|
| 52 |
[[nodiscard]] static bool obj_lookupProperty(JSContext* cx, HandleObject obj, |
| 53 |
HandleId id, |
| 54 |
MutableHandleObject objp, |
| 55 |
PropertyResult* propp); |
| 56 |
|
| 57 |
[[nodiscard]] static bool obj_defineProperty(JSContext* cx, HandleObject obj, |
| 58 |
HandleId id, |
| 59 |
Handle<PropertyDescriptor> desc, |
| 60 |
ObjectOpResult& result); |
| 61 |
|
| 62 |
[[nodiscard]] static bool obj_hasProperty(JSContext* cx, HandleObject obj, |
| 63 |
HandleId id, bool* foundp); |
| 64 |
|
| 65 |
[[nodiscard]] static bool obj_getProperty(JSContext* cx, HandleObject obj, |
| 66 |
HandleValue receiver, HandleId id, |
| 67 |
MutableHandleValue vp); |
| 68 |
|
| 69 |
[[nodiscard]] static bool obj_setProperty(JSContext* cx, HandleObject obj, |
| 70 |
HandleId id, HandleValue v, |
| 71 |
HandleValue receiver, |
| 72 |
ObjectOpResult& result); |
| 73 |
|
| 74 |
[[nodiscard]] static bool obj_getOwnPropertyDescriptor( |
| 75 |
JSContext* cx, HandleObject obj, HandleId id, |
| 76 |
MutableHandle<mozilla::Maybe<PropertyDescriptor>> desc); |
| 77 |
|
| 78 |
[[nodiscard]] static bool obj_deleteProperty(JSContext* cx, HandleObject obj, |
| 79 |
HandleId id, |
| 80 |
ObjectOpResult& result); |
| 81 |
|
| 82 |
// PropOffset is a uint32_t that is used to carry information about the |
| 83 |
// location of an value from WasmGcObject::lookupProperty to |
| 84 |
// WasmGcObject::loadValue. It is distinct from a normal uint32_t to |
| 85 |
// emphasise the fact that it cannot be interpreted as an offset in any |
| 86 |
// single contiguous area of memory: |
| 87 |
// |
| 88 |
// * If the object in question is a WasmStructObject, it is the value of |
| 89 |
// `wasm::StructType::fieldOffset()` for the relevant field, without regard |
| 90 |
// to the inline/outline split. |
| 91 |
// |
| 92 |
// * If the object in question is a WasmArrayObject, then |
| 93 |
// - u32 == UINT32_MAX (0xFFFF'FFFF) means the "length" property |
| 94 |
// is requested |
| 95 |
// - u32 < UINT32_MAX means the array element starting at that byte |
| 96 |
// offset in WasmArrayObject::data_. It is not an array index value. |
| 97 |
// See WasmGcObject::lookupProperty for details. |
| 98 |
class PropOffset { |
| 99 |
uint32_t u32_; |
| 100 |
|
| 101 |
public: |
| 102 |
PropOffset() : u32_(0) {} |
| 103 |
uint32_t get() const { return u32_; } |
| 104 |
void set(uint32_t u32) { u32_ = u32; } |
| 105 |
}; |
| 106 |
|
| 107 |
[[nodiscard]] static bool lookUpProperty(JSContext* cx, |
| 108 |
Handle<WasmGcObject*> obj, jsid id, |
| 109 |
PropOffset* offset, |
| 110 |
wasm::StorageType* type); |
| 111 |
|
| 112 |
public: |
| 113 |
[[nodiscard]] static bool loadValue(JSContext* cx, Handle<WasmGcObject*> obj, |
| 114 |
jsid id, MutableHandleValue vp); |
| 115 |
|
| 116 |
const wasm::SuperTypeVector& superTypeVector() const { |
| 117 |
return *superTypeVector_; |
| 118 |
} |
| 119 |
|
| 120 |
static constexpr size_t offsetOfSuperTypeVector() { |
| 121 |
return offsetof(WasmGcObject, superTypeVector_); |
| 122 |
} |
| 123 |
|
| 124 |
// These are both expensive in that they involve a double indirection. |
| 125 |
// Avoid them if possible. |
| 126 |
const wasm::TypeDef& typeDef() const { return *superTypeVector().typeDef(); } |
| 127 |
wasm::TypeDefKind kind() const { return superTypeVector().typeDef()->kind(); } |
| 128 |
|
| 129 |
[[nodiscard]] bool isRuntimeSubtypeOf( |
| 130 |
const wasm::TypeDef* parentTypeDef) const; |
| 131 |
|
| 132 |
[[nodiscard]] static bool obj_newEnumerate(JSContext* cx, HandleObject obj, |
| 133 |
MutableHandleIdVector properties, |
| 134 |
bool enumerableOnly); |
| 135 |
}; |
| 136 |
|
| 137 |
//========================================================================= |
| 138 |
// WasmArrayObject |
| 139 |
|
| 140 |
// Class for a wasm array. It contains a pointer to the array contents and |
| 141 |
// possibly inline data. Array data is allocated with a DataHeader that tracks |
| 142 |
// whether the array data is stored inline in a trailing array, or out of line |
| 143 |
// in heap memory. The array's data pointer will always point at the start of |
| 144 |
// the array data, and the data header can always be read by subtracting |
| 145 |
// sizeof(DataHeader). |
| 146 |
class WasmArrayObject : public WasmGcObject, |
| 147 |
public TrailingArray<WasmArrayObject> { |
| 148 |
public: |
| 149 |
static const JSClass class_; |
| 150 |
|
| 151 |
// The number of elements in the array. |
| 152 |
uint32_t numElements_; |
| 153 |
|
| 154 |
// Owned data pointer, holding `numElements_` entries. This may point to |
| 155 |
// `inlineStorage` or to an externally-allocated block of memory. It points |
| 156 |
// to the start of the array data, after the data header. |
| 157 |
// |
| 158 |
// This pointer is never null. An empty array will be stored like any other |
| 159 |
// inline-storage array. |
| 160 |
uint8_t* data_; |
| 161 |
|
| 162 |
// The inline (wasm-array-level) data fields, stored as a trailing array. We |
| 163 |
// request this field to begin at an 8-aligned offset relative to the start of |
| 164 |
// the object, so as to guarantee that `double` typed fields are not subject |
| 165 |
// to misaligned-access penalties on any target, whilst wasting at maximum 4 |
| 166 |
// bytes of space. (v128 fields are possible, but we have opted to favor |
| 167 |
// slightly smaller objects over requiring a 16-byte alignment.) |
| 168 |
// |
| 169 |
// If used, the inline storage area will begin with the data header, followed |
| 170 |
// by the actual array data. See the main comment on WasmArrayObject. |
| 171 |
// |
| 172 |
// Remember that `inlineStorage` is in reality a variable length block with |
| 173 |
// maximum size WasmArrayObject_MaxInlineBytes bytes. Do not add any |
| 174 |
// (C++-level) fields after this point! |
| 175 |
uint8_t* inlineStorage() { |
| 176 |
return offsetToPointer<uint8_t>(offsetOfInlineStorage()); |
| 177 |
} |
| 178 |
|
| 179 |
// Actual array data that follows DataHeader. The array data is a part of the |
| 180 |
// `inlineStorage`. |
| 181 |
template <typename T> |
| 182 |
T* inlineArrayElements() { |
| 183 |
return offsetToPointer<T>(offsetOfInlineArrayData()); |
| 184 |
} |
| 185 |
|
| 186 |
// This tells us how big the object is if we know the number of inline bytes |
| 187 |
// it was created with. |
| 188 |
static inline constexpr size_t sizeOfIncludingInlineStorage( |
| 189 |
size_t sizeOfInlineStorage) { |
| 190 |
size_t n = sizeof(WasmArrayObject) + sizeOfInlineStorage; |
| 191 |
MOZ_ASSERT(n <= JSObject::MAX_BYTE_SIZE); |
| 192 |
return n; |
| 193 |
} |
| 194 |
|
| 195 |
// This tells us how big the object is if we know the number of inline bytes |
| 196 |
// it was created with. |
| 197 |
static inline constexpr size_t sizeOfIncludingInlineData( |
| 198 |
size_t sizeOfInlineData) { |
| 199 |
size_t n = sizeof(WasmArrayObject) + sizeOfInlineData; |
| 200 |
MOZ_ASSERT(n <= JSObject::MAX_BYTE_SIZE); |
| 201 |
return n; |
| 202 |
} |
| 203 |
|
| 204 |
// AllocKind for object creation |
| 205 |
static inline gc::AllocKind allocKindForOOL(); |
| 206 |
static inline gc::AllocKind allocKindForIL(uint32_t storageBytes); |
| 207 |
inline gc::AllocKind allocKind() const; |
| 208 |
|
| 209 |
// Calculate the byte length of the array's data storage, being careful to |
| 210 |
// check for overflow. This includes the data header, data, and any extra |
| 211 |
// space for alignment with GC sizes. Note this logic assumes that |
| 212 |
// MaxArrayPayloadBytes is within uint32_t range. |
| 213 |
// |
| 214 |
// This logic is mirrored in WasmArrayObject::maxInlineElementsForElemSize and |
| 215 |
// MacroAssembler::wasmNewArrayObject. |
| 216 |
static constexpr mozilla::CheckedUint32 calcStorageBytesChecked( |
| 217 |
uint32_t elemSize, uint32_t numElements) { |
| 218 |
static_assert(sizeof(WasmArrayObject) % gc::CellAlignBytes == 0); |
| 219 |
mozilla::CheckedUint32 storageBytes = elemSize; |
| 220 |
storageBytes *= numElements; |
| 221 |
storageBytes += sizeof(WasmArrayObject::DataHeader); |
| 222 |
// Round total allocation up to gc::CellAlignBytes |
| 223 |
storageBytes -= 1; |
| 224 |
storageBytes += gc::CellAlignBytes - (storageBytes % gc::CellAlignBytes); |
| 225 |
return storageBytes; |
| 226 |
} |
| 227 |
// Calculate the byte length of the array's data storage, without checking for |
| 228 |
// overflow. This includes the data header, data, and any extra space for |
| 229 |
// alignment with GC sizes. |
| 230 |
static uint32_t calcStorageBytesUnchecked(uint32_t elemSize, |
| 231 |
uint32_t numElements) { |
| 232 |
mozilla::CheckedUint32 storageBytes = |
| 233 |
calcStorageBytesChecked(elemSize, numElements); |
| 234 |
MOZ_ASSERT(storageBytes.isValid()); |
| 235 |
return storageBytes.value(); |
| 236 |
} |
| 237 |
// Compute the maximum number of elements that can be stored inline for the |
| 238 |
// given element size. |
| 239 |
static inline constexpr uint32_t maxInlineElementsForElemSize( |
| 240 |
uint32_t elemSize); |
| 241 |
|
| 242 |
using DataHeader = uintptr_t; |
| 243 |
static const DataHeader DataIsIL = 0; |
| 244 |
static const DataHeader DataIsOOL = 1; |
| 245 |
|
| 246 |
// Creates a new array object with out-of-line storage. Reports an error on |
| 247 |
// OOM. The element type, shape, class pointer, alloc site and alloc kind are |
| 248 |
// taken from `typeDefData`; the initial heap must be specified separately. |
| 249 |
// The size of storage is debug-asserted to be larger than |
| 250 |
// WasmArrayObject_MaxInlineBytes - generally, C++ code should use |
| 251 |
// WasmArrayObject::createArray. |
| 252 |
template <bool ZeroFields> |
| 253 |
static MOZ_ALWAYS_INLINE WasmArrayObject* createArrayOOL( |
| 254 |
JSContext* cx, wasm::TypeDefInstanceData* typeDefData, |
| 255 |
js::gc::AllocSite* allocSite, js::gc::Heap initialHeap, |
| 256 |
uint32_t numElements, uint32_t storageBytes); |
| 257 |
|
| 258 |
// Creates a new array object with inline storage. Reports an error on OOM. |
| 259 |
// The element type, shape, class pointer, alloc site and alloc kind are taken |
| 260 |
// from `typeDefData`; the initial heap must be specified separately. The size |
| 261 |
// of storage is debug-asserted to be within WasmArrayObject_MaxInlineBytes - |
| 262 |
// generally, C++ code should use WasmArrayObject::createArray. |
| 263 |
template <bool ZeroFields> |
| 264 |
static MOZ_ALWAYS_INLINE WasmArrayObject* createArrayIL( |
| 265 |
JSContext* cx, wasm::TypeDefInstanceData* typeDefData, |
| 266 |
js::gc::AllocSite* allocSite, js::gc::Heap initialHeap, |
| 267 |
uint32_t numElements, uint32_t storageBytes); |
| 268 |
|
| 269 |
// This selects one of the above two routines, depending on how much storage |
| 270 |
// is required for the given type and number of elements. |
| 271 |
template <bool ZeroFields> |
| 272 |
static MOZ_ALWAYS_INLINE WasmArrayObject* createArray( |
| 273 |
JSContext* cx, wasm::TypeDefInstanceData* typeDefData, |
| 274 |
js::gc::AllocSite* allocSite, js::gc::Heap initialHeap, |
| 275 |
uint32_t numElements); |
| 276 |
|
| 277 |
// JIT accessors |
| 278 |
static constexpr size_t offsetOfNumElements() { |
| 279 |
return offsetof(WasmArrayObject, numElements_); |
| 280 |
} |
| 281 |
static constexpr size_t offsetOfData() { |
| 282 |
return offsetof(WasmArrayObject, data_); |
| 283 |
} |
| 284 |
static const uint32_t inlineStorageAlignment = 8; |
| 285 |
static constexpr size_t offsetOfInlineStorage() { |
| 286 |
return AlignBytes(sizeof(WasmArrayObject), inlineStorageAlignment); |
| 287 |
} |
| 288 |
static constexpr size_t offsetOfInlineArrayData() { |
| 289 |
return offsetOfInlineStorage() + sizeof(DataHeader); |
| 290 |
} |
| 291 |
|
| 292 |
// Tracing and finalization |
| 293 |
static void obj_trace(JSTracer* trc, JSObject* object); |
| 294 |
static void obj_finalize(JS::GCContext* gcx, JSObject* object); |
| 295 |
static size_t obj_moved(JSObject* obj, JSObject* old); |
| 296 |
|
| 297 |
void storeVal(const wasm::Val& val, uint32_t itemIndex); |
| 298 |
void fillVal(const wasm::Val& val, uint32_t itemIndex, uint32_t len); |
| 299 |
|
| 300 |
static DataHeader* dataHeaderFromDataPointer(const uint8_t* data) { |
| 301 |
MOZ_ASSERT(data); |
| 302 |
return (DataHeader*)data - 1; |
| 303 |
} |
| 304 |
DataHeader* dataHeader() const { |
| 305 |
return WasmArrayObject::dataHeaderFromDataPointer(data_); |
| 306 |
} |
| 307 |
|
| 308 |
static bool isDataInline(uint8_t* data) { |
| 309 |
const DataHeader* header = dataHeaderFromDataPointer(data); |
| 310 |
MOZ_ASSERT(*header == DataIsIL || *header == DataIsOOL); |
| 311 |
return *header == DataIsIL; |
| 312 |
} |
| 313 |
bool isDataInline() const { return WasmArrayObject::isDataInline(data_); } |
| 314 |
|
| 315 |
static WasmArrayObject* fromInlineDataPointer(uint8_t* data) { |
| 316 |
MOZ_ASSERT(isDataInline(data)); |
| 317 |
return (WasmArrayObject*)(data - |
| 318 |
WasmArrayObject::offsetOfInlineArrayData()); |
| 319 |
} |
| 320 |
|
| 321 |
static DataHeader* addressOfInlineDataHeader(WasmArrayObject* base) { |
| 322 |
return base->offsetToPointer<DataHeader>(offsetOfInlineStorage()); |
| 323 |
} |
| 324 |
static uint8_t* addressOfInlineData(WasmArrayObject* base) { |
| 325 |
return base->offsetToPointer<uint8_t>(offsetOfInlineArrayData()); |
| 326 |
} |
| 327 |
}; |
| 328 |
|
| 329 |
static_assert((WasmArrayObject::offsetOfInlineStorage() % 8) == 0); |
| 330 |
|
| 331 |
// Helper to mark all locations that assume that the type of |
| 332 |
// WasmArrayObject::numElements is uint32_t. |
| 333 |
#define STATIC_ASSERT_WASMARRAYELEMENTS_NUMELEMENTS_IS_U32 \ |
| 334 |
static_assert(sizeof(js::WasmArrayObject::numElements_) == sizeof(uint32_t)) |
| 335 |
|
| 336 |
//========================================================================= |
| 337 |
// WasmStructObject |
| 338 |
|
| 339 |
// Class for a wasm struct. It has inline data and, if the inline area is |
| 340 |
// insufficient, a pointer to outline data that lives in the C++ heap. |
| 341 |
// Computing the field offsets is somewhat tricky; see block comment on `class |
| 342 |
// StructLayout` for background. |
| 343 |
|
| 344 |
class WasmStructObject : public WasmGcObject, |
| 345 |
public TrailingArray<WasmStructObject> { |
| 346 |
public: |
| 347 |
static const JSClass classInline_; |
| 348 |
static const JSClass classOutline_; |
| 349 |
|
| 350 |
// Owned pointer to a malloc'd block containing out-of-line fields, or |
| 351 |
// nullptr if none. Note that MIR alias analysis assumes this is readonly |
| 352 |
// for the life of the object; do not change it once the object is created. |
| 353 |
// See MWasmLoadObjectField::congruentTo. |
| 354 |
uint8_t* outlineData_; |
| 355 |
|
| 356 |
// The inline (wasm-struct-level) data fields, stored as a trailing array. |
| 357 |
// This must be a multiple of 16 bytes long in order to ensure that no field |
| 358 |
// gets split across the inline-outline boundary. As a refinement, we request |
| 359 |
// this field to begin at an 8-aligned offset relative to the start of the |
| 360 |
// object, so as to guarantee that `double` typed fields are not subject to |
| 361 |
// misaligned-access penalties on any target, whilst wasting at maximum 4 |
| 362 |
// bytes of space. |
| 363 |
// |
| 364 |
// Remember that `inlineData` is in reality a variable length block with |
| 365 |
// maximum size WasmStructObject_MaxInlineBytes bytes. Do not add any |
| 366 |
// (C++-level) fields after this point! |
| 367 |
uint8_t* inlineData() { |
| 368 |
return offsetToPointer<uint8_t>(offsetOfInlineData()); |
| 369 |
} |
| 370 |
|
| 371 |
// This tells us how big the object is if we know the number of inline bytes |
| 372 |
// it was created with. |
| 373 |
static inline constexpr size_t sizeOfIncludingInlineData( |
| 374 |
size_t sizeOfInlineData) { |
| 375 |
size_t n = sizeof(WasmStructObject) + sizeOfInlineData; |
| 376 |
MOZ_ASSERT(n <= JSObject::MAX_BYTE_SIZE); |
| 377 |
return n; |
| 378 |
} |
| 379 |
|
| 380 |
static const JSClass* classForTypeDef(const wasm::TypeDef* typeDef); |
| 381 |
static js::gc::AllocKind allocKindForTypeDef(const wasm::TypeDef* typeDef); |
| 382 |
|
| 383 |
// Creates a new struct typed object, optionally initialized to zero. |
| 384 |
// Reports if there is an out of memory error. The structure's type, shape, |
| 385 |
// class pointer, alloc site and alloc kind are taken from `typeDefData`; |
| 386 |
// the initial heap must be specified separately. It is assumed and debug- |
| 387 |
// asserted that `typeDefData` refers to a type that does not need OOL |
| 388 |
// storage. |
| 389 |
template <bool ZeroFields> |
| 390 |
static MOZ_ALWAYS_INLINE WasmStructObject* createStructIL( |
| 391 |
JSContext* cx, wasm::TypeDefInstanceData* typeDefData, |
| 392 |
gc::AllocSite* allocSite, js::gc::Heap initialHeap); |
| 393 |
|
| 394 |
// Same as ::createStructIL, except it is assumed and debug-asserted that |
| 395 |
// `typeDefData` refers to a type that does need OOL storage. |
| 396 |
template <bool ZeroFields> |
| 397 |
static MOZ_ALWAYS_INLINE WasmStructObject* createStructOOL( |
| 398 |
JSContext* cx, wasm::TypeDefInstanceData* typeDefData, |
| 399 |
gc::AllocSite* allocSite, js::gc::Heap initialHeap); |
| 400 |
|
| 401 |
// Given the total number of data bytes required (including alignment |
| 402 |
// holes), return the number of inline and outline bytes required. |
| 403 |
static inline void getDataByteSizes(uint32_t totalBytes, |
| 404 |
uint32_t* inlineBytes, |
| 405 |
uint32_t* outlineBytes); |
| 406 |
|
| 407 |
// Convenience function; returns true iff ::getDataByteSizes would set |
| 408 |
// *outlineBytes to a non-zero value. |
| 409 |
static inline bool requiresOutlineBytes(uint32_t totalBytes); |
| 410 |
|
| 411 |
// Given the offset of a field, produce the offset in `inlineData` or |
| 412 |
// `*outlineData_` to use, plus a bool indicating which area it is. |
| 413 |
// `fieldType` is for assertional purposes only. |
| 414 |
static inline void fieldOffsetToAreaAndOffset(wasm::StorageType fieldType, |
| 415 |
uint32_t fieldOffset, |
| 416 |
bool* areaIsOutline, |
| 417 |
uint32_t* areaOffset); |
| 418 |
|
| 419 |
// Given the offset of a field, return its actual address. `fieldType` is |
| 420 |
// for assertional purposes only. |
| 421 |
inline uint8_t* fieldOffsetToAddress(wasm::StorageType fieldType, |
| 422 |
uint32_t fieldOffset); |
| 423 |
|
| 424 |
// Gets JS Value of the structure field. |
| 425 |
bool getField(JSContext* cx, uint32_t index, MutableHandle<Value> val); |
| 426 |
|
| 427 |
// JIT accessors |
| 428 |
static const uint32_t inlineDataAlignment = 8; |
| 429 |
static constexpr size_t offsetOfOutlineData() { |
| 430 |
return offsetof(WasmStructObject, outlineData_); |
| 431 |
} |
| 432 |
static constexpr size_t offsetOfInlineData() { |
| 433 |
return AlignBytes(sizeof(WasmStructObject), inlineDataAlignment); |
| 434 |
} |
| 435 |
|
| 436 |
// Tracing and finalization |
| 437 |
static void obj_trace(JSTracer* trc, JSObject* object); |
| 438 |
static void obj_finalize(JS::GCContext* gcx, JSObject* object); |
| 439 |
static size_t obj_moved(JSObject* obj, JSObject* old); |
| 440 |
|
| 441 |
void storeVal(const wasm::Val& val, uint32_t fieldIndex); |
| 442 |
}; |
| 443 |
|
| 444 |
static_assert((WasmStructObject::offsetOfInlineData() % 8) == 0); |
| 445 |
|
| 446 |
// MaxInlineBytes must be a multiple of 16 for reasons described in the |
| 447 |
// comment on `class StructLayout`. This unfortunately can't be defined |
| 448 |
// inside the class definition itself because the sizeof(..) expression isn't |
| 449 |
// valid until after the end of the class definition. |
| 450 |
const size_t WasmStructObject_MaxInlineBytes = |
| 451 |
((JSObject::MAX_BYTE_SIZE - sizeof(WasmStructObject)) / 16) * 16; |
| 452 |
const size_t WasmArrayObject_MaxInlineBytes = |
| 453 |
((JSObject::MAX_BYTE_SIZE - sizeof(WasmArrayObject)) / 16) * 16; |
| 454 |
|
| 455 |
static_assert((WasmStructObject_MaxInlineBytes % 16) == 0); |
| 456 |
static_assert((WasmArrayObject_MaxInlineBytes % 16) == 0); |
| 457 |
|
| 458 |
/* static */ |
| 459 |
inline constexpr uint32_t WasmArrayObject::maxInlineElementsForElemSize( |
| 460 |
uint32_t elemSize) { |
| 461 |
// This implementation inverts the logic of WasmArrayObject::calcStorageBytes |
| 462 |
// to compute numElements. |
| 463 |
MOZ_RELEASE_ASSERT(elemSize > 0); |
| 464 |
uint32_t result = WasmArrayObject_MaxInlineBytes; |
| 465 |
static_assert(WasmArrayObject_MaxInlineBytes % gc::CellAlignBytes == 0); |
| 466 |
result -= sizeof(WasmArrayObject::DataHeader); |
| 467 |
result /= elemSize; |
| 468 |
|
| 469 |
MOZ_RELEASE_ASSERT(calcStorageBytesChecked(elemSize, result).isValid()); |
| 470 |
return result; |
| 471 |
} |
| 472 |
|
| 473 |
/*static*/ |
| 474 |
inline void WasmStructObject::getDataByteSizes(uint32_t totalBytes, |
| 475 |
uint32_t* inlineBytes, |
| 476 |
uint32_t* outlineBytes) { |
| 477 |
if (MOZ_UNLIKELY(totalBytes > WasmStructObject_MaxInlineBytes)) { |
| 478 |
*inlineBytes = WasmStructObject_MaxInlineBytes; |
| 479 |
*outlineBytes = totalBytes - WasmStructObject_MaxInlineBytes; |
| 480 |
} else { |
| 481 |
*inlineBytes = totalBytes; |
| 482 |
*outlineBytes = 0; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
/* static */ |
| 487 |
inline bool WasmStructObject::requiresOutlineBytes(uint32_t totalBytes) { |
| 488 |
uint32_t inlineBytes, outlineBytes; |
| 489 |
WasmStructObject::getDataByteSizes(totalBytes, &inlineBytes, &outlineBytes); |
| 490 |
return outlineBytes > 0; |
| 491 |
} |
| 492 |
|
| 493 |
/*static*/ |
| 494 |
inline void WasmStructObject::fieldOffsetToAreaAndOffset( |
| 495 |
wasm::StorageType fieldType, uint32_t fieldOffset, bool* areaIsOutline, |
| 496 |
uint32_t* areaOffset) { |
| 497 |
if (fieldOffset < WasmStructObject_MaxInlineBytes) { |
| 498 |
*areaIsOutline = false; |
| 499 |
*areaOffset = fieldOffset; |
| 500 |
} else { |
| 501 |
*areaIsOutline = true; |
| 502 |
*areaOffset = fieldOffset - WasmStructObject_MaxInlineBytes; |
| 503 |
} |
| 504 |
// Assert that the first and last bytes for the field agree on which side of |
| 505 |
// the inline/outline boundary they live. |
| 506 |
MOZ_RELEASE_ASSERT( |
| 507 |
(fieldOffset < WasmStructObject_MaxInlineBytes) == |
| 508 |
((fieldOffset + fieldType.size() - 1) < WasmStructObject_MaxInlineBytes)); |
| 509 |
} |
| 510 |
|
| 511 |
inline uint8_t* WasmStructObject::fieldOffsetToAddress( |
| 512 |
wasm::StorageType fieldType, uint32_t fieldOffset) { |
| 513 |
bool areaIsOutline; |
| 514 |
uint32_t areaOffset; |
| 515 |
fieldOffsetToAreaAndOffset(fieldType, fieldOffset, &areaIsOutline, |
| 516 |
&areaOffset); |
| 517 |
return (areaIsOutline ? outlineData_ : inlineData()) + areaOffset; |
| 518 |
} |
| 519 |
|
| 520 |
// Ensure that faulting loads/stores for WasmStructObject and WasmArrayObject |
| 521 |
// are in the NULL pointer guard page. |
| 522 |
static_assert(WasmStructObject_MaxInlineBytes <= wasm::NullPtrGuardSize); |
| 523 |
static_assert(sizeof(WasmArrayObject) <= wasm::NullPtrGuardSize); |
| 524 |
|
| 525 |
// Template to acquire a stable pointer to the elements of a WasmArrayObject |
| 526 |
// that will not move even if there is a GC. This will create a copy of the |
| 527 |
// array onto the stack when the array has inline data, and can be expensive. |
| 528 |
template <typename T> |
| 529 |
class MOZ_RAII StableWasmArrayObjectElements { |
| 530 |
static constexpr size_t MaxInlineElements = |
| 531 |
WasmArrayObject::maxInlineElementsForElemSize(sizeof(T)); |
| 532 |
Rooted<WasmArrayObject*> array_; |
| 533 |
T* elements_; |
| 534 |
mozilla::Maybe<mozilla::Vector<T, MaxInlineElements, SystemAllocPolicy>> |
| 535 |
ownElements_; |
| 536 |
|
| 537 |
public: |
| 538 |
StableWasmArrayObjectElements(JSContext* cx, Handle<WasmArrayObject*> array) |
| 539 |
: array_(cx, array), elements_(nullptr) { |
| 540 |
if (array->isDataInline()) { |
| 541 |
ownElements_.emplace(); |
| 542 |
if (!ownElements_->resize(array->numElements_)) { |
| 543 |
// Should not happen as we have inline storage for the maximum needed |
| 544 |
// elements. |
| 545 |
MOZ_CRASH(); |
| 546 |
} |
| 547 |
const T* src = array->inlineArrayElements<T>(); |
| 548 |
std::copy(src, src + array->numElements_, ownElements_->begin()); |
| 549 |
elements_ = ownElements_->begin(); |
| 550 |
} else { |
| 551 |
elements_ = reinterpret_cast<T*>(array->data_); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
T* elements() { return elements_; } |
| 556 |
size_t length() const { return array_->numElements_; } |
| 557 |
}; |
| 558 |
|
| 559 |
} // namespace js |
| 560 |
|
| 561 |
//========================================================================= |
| 562 |
// misc |
| 563 |
|
| 564 |
namespace js { |
| 565 |
|
| 566 |
inline bool IsWasmGcObjectClass(const JSClass* class_) { |
| 567 |
return class_ == &WasmArrayObject::class_ || |
| 568 |
class_ == &WasmStructObject::classInline_ || |
| 569 |
class_ == &WasmStructObject::classOutline_; |
| 570 |
} |
| 571 |
|
| 572 |
} // namespace js |
| 573 |
|
| 574 |
template <> |
| 575 |
inline bool JSObject::is<js::WasmGcObject>() const { |
| 576 |
return js::IsWasmGcObjectClass(getClass()); |
| 577 |
} |
| 578 |
|
| 579 |
template <> |
| 580 |
inline bool JSObject::is<js::WasmStructObject>() const { |
| 581 |
const JSClass* class_ = getClass(); |
| 582 |
return class_ == &js::WasmStructObject::classInline_ || |
| 583 |
class_ == &js::WasmStructObject::classOutline_; |
| 584 |
} |
| 585 |
|
| 586 |
#endif /* wasm_WasmGcObject_h */ |