Debian Codegraph @

js/src/wasm/WasmGcObject-inl.h

raw
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_inl_h
8 #define wasm_WasmGcObject_inl_h
9
10 #include "wasm/WasmGcObject.h"
11
12 #include "mozilla/Attributes.h"
13 #include "mozilla/DebugOnly.h"
14 #include "util/Memory.h"
15
16 #include "gc/Nursery-inl.h"
17 #include "gc/ObjectKind-inl.h"
18 #include "vm/JSContext-inl.h"
19
20 //=========================================================================
21 // WasmStructObject inlineable allocation methods
22
23 namespace js {
24
25 /* static */
26 template <bool ZeroFields>
27 MOZ_ALWAYS_INLINE WasmStructObject* WasmStructObject::createStructIL(
28 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
29 gc::AllocSite* allocSite, js::gc::Heap initialHeap) {
30 // It is up to our caller to ensure that `typeDefData` refers to a type that
31 // doesn't need OOL storage.
32
33 MOZ_ASSERT(IsWasmGcObjectClass(typeDefData->clasp));
34 MOZ_ASSERT(!typeDefData->clasp->isNativeObject());
35 debugCheckNewObject(typeDefData->shape, typeDefData->allocKind, initialHeap);
36
37 mozilla::DebugOnly<const wasm::TypeDef*> typeDef = typeDefData->typeDef;
38 MOZ_ASSERT(typeDef->kind() == wasm::TypeDefKind::Struct);
39
40 // This doesn't need to be rooted, since all we do with it prior to
41 // return is to zero out the fields (and then only if ZeroFields is true).
42 WasmStructObject* structObj = (WasmStructObject*)cx->newCell<WasmGcObject>(
43 typeDefData->allocKind, initialHeap, typeDefData->clasp, allocSite);
44 if (MOZ_UNLIKELY(!structObj)) {
45 ReportOutOfMemory(cx);
46 return nullptr;
47 }
48
49 MOZ_ASSERT((uintptr_t(structObj->inlineData()) % sizeof(uintptr_t)) == 0);
50 structObj->initShape(typeDefData->shape);
51 structObj->superTypeVector_ = typeDefData->superTypeVector;
52 structObj->outlineData_ = nullptr;
53 if constexpr (ZeroFields) {
54 uint32_t totalBytes = typeDefData->structTypeSize;
55 MOZ_ASSERT(totalBytes == typeDef->structType().size_);
56 MOZ_ASSERT(totalBytes <= WasmStructObject_MaxInlineBytes);
57 MOZ_ASSERT((totalBytes % sizeof(uintptr_t)) == 0);
58 memset(structObj->inlineData(), 0, totalBytes);
59 }
60
61 js::gc::gcprobes::CreateObject(structObj);
62 probes::CreateObject(cx, structObj);
63
64 return structObj;
65 }
66
67 /* static */
68 template <bool ZeroFields>
69 MOZ_ALWAYS_INLINE WasmStructObject* WasmStructObject::createStructOOL(
70 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
71 gc::AllocSite* allocSite, js::gc::Heap initialHeap) {
72 // It is up to our caller to ensure that `typeDefData` refers to a type that
73 // needs OOL storage.
74
75 MOZ_ASSERT(IsWasmGcObjectClass(typeDefData->clasp));
76 MOZ_ASSERT(!typeDefData->clasp->isNativeObject());
77 debugCheckNewObject(typeDefData->shape, typeDefData->allocKind, initialHeap);
78
79 mozilla::DebugOnly<const wasm::TypeDef*> typeDef = typeDefData->typeDef;
80 MOZ_ASSERT(typeDef->kind() == wasm::TypeDefKind::Struct);
81
82 uint32_t totalBytes = typeDefData->structTypeSize;
83 MOZ_ASSERT(totalBytes == typeDef->structType().size_);
84 MOZ_ASSERT(totalBytes > WasmStructObject_MaxInlineBytes);
85 MOZ_ASSERT((totalBytes % sizeof(uintptr_t)) == 0);
86
87 uint32_t inlineBytes, outlineBytes;
88 WasmStructObject::getDataByteSizes(totalBytes, &inlineBytes, &outlineBytes);
89 MOZ_ASSERT(inlineBytes == WasmStructObject_MaxInlineBytes);
90 MOZ_ASSERT(outlineBytes > 0);
91
92 // Allocate the outline data area before allocating the object so that we can
93 // infallibly initialize the outline data area.
94 Nursery& nursery = cx->nursery();
95 PointerAndUint7 outlineData =
96 nursery.mallocedBlockCache().alloc(outlineBytes);
97 if (MOZ_UNLIKELY(!outlineData.pointer())) {
98 ReportOutOfMemory(cx);
99 return nullptr;
100 }
101
102 // See corresponding comment in WasmArrayObject::createArray.
103 Rooted<WasmStructObject*> structObj(cx);
104 structObj = (WasmStructObject*)cx->newCell<WasmGcObject>(
105 typeDefData->allocKind, initialHeap, typeDefData->clasp, allocSite);
106 if (MOZ_UNLIKELY(!structObj)) {
107 ReportOutOfMemory(cx);
108 if (outlineData.pointer()) {
109 nursery.mallocedBlockCache().free(outlineData);
110 }
111 return nullptr;
112 }
113
114 MOZ_ASSERT((uintptr_t(structObj->inlineData()) % sizeof(uintptr_t)) == 0);
115 structObj->initShape(typeDefData->shape);
116 structObj->superTypeVector_ = typeDefData->superTypeVector;
117
118 // Initialize the outline data fields
119 structObj->outlineData_ = (uint8_t*)outlineData.pointer();
120 if constexpr (ZeroFields) {
121 memset(structObj->inlineData(), 0, inlineBytes);
122 memset(outlineData.pointer(), 0, outlineBytes);
123 }
124
125 if (MOZ_LIKELY(js::gc::IsInsideNursery(structObj))) {
126 // See corresponding comment in WasmArrayObject::createArrayNonEmpty.
127 if (MOZ_UNLIKELY(!nursery.registerTrailer(outlineData, outlineBytes))) {
128 nursery.mallocedBlockCache().free(outlineData);
129 ReportOutOfMemory(cx);
130 return nullptr;
131 }
132 } else {
133 // See corresponding comment in WasmArrayObject::createArrayNonEmpty.
134 MOZ_ASSERT(structObj->isTenured());
135 AddCellMemory(structObj, outlineBytes + wasm::TrailerBlockOverhead,
136 MemoryUse::WasmTrailerBlock);
137 }
138
139 js::gc::gcprobes::CreateObject(structObj);
140 probes::CreateObject(cx, structObj);
141
142 return structObj;
143 }
144
145 //=========================================================================
146 // WasmArrayObject inlineable allocation methods
147
148 /* static */
149 inline gc::AllocKind WasmArrayObject::allocKindForOOL() {
150 gc::AllocKind allocKind =
151 gc::GetGCObjectKindForBytes(sizeof(WasmArrayObject));
152 return gc::GetFinalizedAllocKindForClass(allocKind, &WasmArrayObject::class_);
153 }
154
155 /* static */
156 inline gc::AllocKind WasmArrayObject::allocKindForIL(uint32_t storageBytes) {
157 gc::AllocKind allocKind =
158 gc::GetGCObjectKindForBytes(sizeof(WasmArrayObject) + storageBytes);
159 return gc::GetFinalizedAllocKindForClass(allocKind, &WasmArrayObject::class_);
160 }
161
162 inline gc::AllocKind WasmArrayObject::allocKind() const {
163 if (isDataInline()) {
164 // numElements_ was validated to not overflow when constructing this object
165 uint32_t storageBytes = calcStorageBytesUnchecked(
166 typeDef().arrayType().elementType().size(), numElements_);
167 return allocKindForIL(storageBytes);
168 }
169
170 return allocKindForOOL();
171 }
172
173 /* static */
174 template <bool ZeroFields>
175 MOZ_ALWAYS_INLINE WasmArrayObject* WasmArrayObject::createArrayOOL(
176 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
177 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
178 uint32_t numElements, uint32_t storageBytes) {
179 STATIC_ASSERT_WASMARRAYELEMENTS_NUMELEMENTS_IS_U32;
180
181 MOZ_ASSERT(IsWasmGcObjectClass(typeDefData->clasp));
182 MOZ_ASSERT(!typeDefData->clasp->isNativeObject());
183 MOZ_ASSERT(typeDefData->allocKind == gc::AllocKind::INVALID);
184 gc::AllocKind allocKind = allocKindForOOL();
185 debugCheckNewObject(typeDefData->shape, allocKind, initialHeap);
186
187 mozilla::DebugOnly<const wasm::TypeDef*> typeDef = typeDefData->typeDef;
188 MOZ_ASSERT(typeDef->kind() == wasm::TypeDefKind::Array);
189
190 // This routine is for large arrays with out-of-line data only. For small
191 // arrays use createArrayIL.
192 MOZ_ASSERT(storageBytes > WasmArrayObject_MaxInlineBytes);
193
194 // Allocate the outline data before allocating the object so that we can
195 // infallibly initialize the pointer on the array object after it is
196 // allocated.
197 Nursery& nursery = cx->nursery();
198 PointerAndUint7 outlineAlloc(nullptr, 0);
199 outlineAlloc = nursery.mallocedBlockCache().alloc(storageBytes);
200 if (MOZ_UNLIKELY(!outlineAlloc.pointer())) {
201 ReportOutOfMemory(cx);
202 return nullptr;
203 }
204
205 // It's unfortunate that `arrayObj` has to be rooted, since this is a hot
206 // path and rooting costs around 15 instructions. It is the call to
207 // registerTrailer that makes it necessary.
208 Rooted<WasmArrayObject*> arrayObj(cx);
209 arrayObj = (WasmArrayObject*)cx->newCell<WasmGcObject>(
210 allocKind, initialHeap, typeDefData->clasp, allocSite);
211 if (MOZ_UNLIKELY(!arrayObj)) {
212 ReportOutOfMemory(cx);
213 if (outlineAlloc.pointer()) {
214 nursery.mallocedBlockCache().free(outlineAlloc);
215 }
216 return nullptr;
217 }
218
219 DataHeader* outlineHeader = (DataHeader*)outlineAlloc.pointer();
220 uint8_t* outlineData = (uint8_t*)(outlineHeader + 1);
221 *outlineHeader = DataIsOOL;
222
223 arrayObj->initShape(typeDefData->shape);
224 arrayObj->superTypeVector_ = typeDefData->superTypeVector;
225 arrayObj->numElements_ = numElements;
226 arrayObj->data_ = outlineData;
227 if constexpr (ZeroFields) {
228 uint32_t dataBytes = storageBytes - sizeof(DataHeader);
229 MOZ_ASSERT(dataBytes >= numElements * typeDefData->arrayElemSize);
230 memset(arrayObj->data_, 0, dataBytes);
231 }
232
233 MOZ_ASSERT(!arrayObj->isDataInline());
234
235 if (MOZ_LIKELY(js::gc::IsInsideNursery(arrayObj))) {
236 // We need to register the OOL area with the nursery, so it will be freed
237 // after GCing of the nursery if `arrayObj_` doesn't make it into the
238 // tenured heap. Note, the nursery will keep a running total of the
239 // current trailer block sizes, so it can decide to do a (minor)
240 // collection if that becomes excessive.
241 if (MOZ_UNLIKELY(!nursery.registerTrailer(outlineAlloc, storageBytes))) {
242 nursery.mallocedBlockCache().free(outlineAlloc);
243 ReportOutOfMemory(cx);
244 return nullptr;
245 }
246 } else {
247 MOZ_ASSERT(arrayObj->isTenured());
248 // Register the trailer size with the major GC mechanism, so that can also
249 // is able to decide if that space use warrants a (major) collection.
250 AddCellMemory(arrayObj, storageBytes + wasm::TrailerBlockOverhead,
251 MemoryUse::WasmTrailerBlock);
252 }
253
254 js::gc::gcprobes::CreateObject(arrayObj);
255 probes::CreateObject(cx, arrayObj);
256
257 return arrayObj;
258 }
259
260 template WasmArrayObject* WasmArrayObject::createArrayOOL<true>(
261 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
262 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
263 uint32_t numElements, uint32_t storageBytes);
264 template WasmArrayObject* WasmArrayObject::createArrayOOL<false>(
265 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
266 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
267 uint32_t numElements, uint32_t storageBytes);
268
269 /* static */
270 template <bool ZeroFields>
271 MOZ_ALWAYS_INLINE WasmArrayObject* WasmArrayObject::createArrayIL(
272 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
273 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
274 uint32_t numElements, uint32_t storageBytes) {
275 STATIC_ASSERT_WASMARRAYELEMENTS_NUMELEMENTS_IS_U32;
276
277 MOZ_ASSERT(IsWasmGcObjectClass(typeDefData->clasp));
278 MOZ_ASSERT(!typeDefData->clasp->isNativeObject());
279 MOZ_ASSERT(typeDefData->allocKind == gc::AllocKind::INVALID);
280 gc::AllocKind allocKind = allocKindForIL(storageBytes);
281 debugCheckNewObject(typeDefData->shape, allocKind, initialHeap);
282
283 mozilla::DebugOnly<const wasm::TypeDef*> typeDef = typeDefData->typeDef;
284 MOZ_ASSERT(typeDef->kind() == wasm::TypeDefKind::Array);
285
286 MOZ_ASSERT(storageBytes <= WasmArrayObject_MaxInlineBytes);
287
288 // There's no need for `arrayObj` to be rooted, since the only thing we're
289 // going to do is fill in some bits of it, then return it.
290 WasmArrayObject* arrayObj = (WasmArrayObject*)cx->newCell<WasmGcObject>(
291 allocKind, initialHeap, typeDefData->clasp, allocSite);
292 if (MOZ_UNLIKELY(!arrayObj)) {
293 ReportOutOfMemory(cx);
294 return nullptr;
295 }
296
297 arrayObj->initShape(typeDefData->shape);
298 arrayObj->superTypeVector_ = typeDefData->superTypeVector;
299 arrayObj->numElements_ = numElements;
300
301 DataHeader* inlineHeader =
302 WasmArrayObject::addressOfInlineDataHeader(arrayObj);
303 uint8_t* inlineData = WasmArrayObject::addressOfInlineData(arrayObj);
304 *inlineHeader = DataIsIL;
305 arrayObj->data_ = inlineData;
306
307 if constexpr (ZeroFields) {
308 uint32_t dataBytes = storageBytes - sizeof(DataHeader);
309 MOZ_ASSERT(dataBytes >= numElements * typeDefData->arrayElemSize);
310
311 if (numElements > 0) {
312 memset(arrayObj->data_, 0, dataBytes);
313 }
314 }
315
316 MOZ_ASSERT(arrayObj->isDataInline());
317
318 js::gc::gcprobes::CreateObject(arrayObj);
319 probes::CreateObject(cx, arrayObj);
320
321 return arrayObj;
322 }
323
324 template WasmArrayObject* WasmArrayObject::createArrayIL<true>(
325 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
326 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
327 uint32_t numElements, uint32_t storageBytes);
328 template WasmArrayObject* WasmArrayObject::createArrayIL<false>(
329 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
330 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
331 uint32_t numElements, uint32_t storageBytes);
332
333 /* static */
334 template <bool ZeroFields>
335 MOZ_ALWAYS_INLINE WasmArrayObject* WasmArrayObject::createArray(
336 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
337 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
338 uint32_t numElements) {
339 MOZ_ASSERT(typeDefData->arrayElemSize ==
340 typeDefData->typeDef->arrayType().elementType().size());
341 mozilla::CheckedUint32 storageBytes =
342 calcStorageBytesChecked(typeDefData->arrayElemSize, numElements);
343 if (!storageBytes.isValid() ||
344 storageBytes.value() > uint32_t(wasm::MaxArrayPayloadBytes)) {
345 js::ReportOversizedAllocation(cx, JSMSG_WASM_ARRAY_IMP_LIMIT);
346 wasm::MarkPendingExceptionAsTrap(cx);
347 return nullptr;
348 }
349
350 if (storageBytes.value() <= WasmArrayObject_MaxInlineBytes) {
351 return createArrayIL<ZeroFields>(cx, typeDefData, allocSite, initialHeap,
352 numElements, storageBytes.value());
353 }
354
355 return createArrayOOL<ZeroFields>(cx, typeDefData, allocSite, initialHeap,
356 numElements, storageBytes.value());
357 }
358
359 template WasmArrayObject* WasmArrayObject::createArray<true>(
360 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
361 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
362 uint32_t numElements);
363 template WasmArrayObject* WasmArrayObject::createArray<false>(
364 JSContext* cx, wasm::TypeDefInstanceData* typeDefData,
365 js::gc::AllocSite* allocSite, js::gc::Heap initialHeap,
366 uint32_t numElements);
367
368 } // namespace js
369
370 #endif /* wasm_WasmGcObject_inl_h */