Debian Codegraph @

js/src/wasm/WasmTable.cpp

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 *
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 #include "wasm/WasmTable.h"
20
21 #include "mozilla/CheckedInt.h"
22
23 #include "vm/JSContext.h"
24 #include "wasm/WasmInstance.h"
25 #include "wasm/WasmJS.h"
26 #include "wasm/WasmValue.h"
27
28 #include "gc/StableCellHasher-inl.h"
29 #include "vm/Realm-inl.h"
30 #include "wasm/WasmInstance-inl.h"
31
32 using namespace js;
33 using namespace js::wasm;
34 using mozilla::CheckedInt;
35
36 Table::Table(JSContext* cx, const TableDesc& desc,
37 Handle<WasmTableObject*> maybeObject, FuncRefVector&& functions)
38 : maybeObject_(maybeObject),
39 observers_(cx->zone()),
40 functions_(std::move(functions)),
41 addressType_(desc.addressType()),
42 elemType_(desc.elemType),
43 isAsmJS_(desc.isAsmJS),
44 length_(desc.initialLength()),
45 maximum_(desc.maximumLength()) {
46 // Acquire a strong reference to the type definition this table may be
47 // referencing.
48 elemType_.AddRef();
49 MOZ_ASSERT(repr() == TableRepr::Func);
50 MOZ_ASSERT(length_ <= MaxTableElemsRuntime);
51 }
52
53 Table::Table(JSContext* cx, const TableDesc& desc,
54 Handle<WasmTableObject*> maybeObject, TableAnyRefVector&& objects)
55 : maybeObject_(maybeObject),
56 observers_(cx->zone()),
57 objects_(std::move(objects)),
58 addressType_(desc.addressType()),
59 elemType_(desc.elemType),
60 isAsmJS_(desc.isAsmJS),
61 length_(desc.initialLength()),
62 maximum_(desc.maximumLength()) {
63 // Acquire a strong reference to the type definition this table may be
64 // referencing.
65 elemType_.AddRef();
66 MOZ_ASSERT(repr() == TableRepr::Ref);
67 MOZ_ASSERT(length_ <= MaxTableElemsRuntime);
68 }
69
70 Table::~Table() {
71 // Release the strong reference, if any.
72 elemType_.Release();
73 }
74
75 /* static */
76 SharedTable Table::create(JSContext* cx, const TableDesc& desc,
77 Handle<WasmTableObject*> maybeObject) {
78 // Tables are initialized with init_expr values at Instance::init or
79 // WasmTableObject::create.
80
81 switch (desc.elemType.tableRepr()) {
82 case TableRepr::Func: {
83 FuncRefVector functions;
84 if (!functions.resize(desc.initialLength())) {
85 ReportOutOfMemory(cx);
86 return nullptr;
87 }
88 return SharedTable(
89 cx->new_<Table>(cx, desc, maybeObject, std::move(functions)));
90 }
91 case TableRepr::Ref: {
92 TableAnyRefVector objects;
93 if (!objects.resize(desc.initialLength())) {
94 ReportOutOfMemory(cx);
95 return nullptr;
96 }
97 return SharedTable(
98 cx->new_<Table>(cx, desc, maybeObject, std::move(objects)));
99 }
100 }
101 MOZ_CRASH("switch is exhaustive");
102 }
103
104 void Table::tracePrivate(JSTracer* trc) {
105 // If this table has a WasmTableObject, then this method is only called by
106 // WasmTableObject's trace hook so maybeObject_ must already be marked.
107 // TraceEdge is called so that the pointer can be updated during a moving
108 // GC.
109 TraceNullableEdge(trc, &maybeObject_, "wasm table object");
110
111 switch (repr()) {
112 case TableRepr::Func: {
113 if (isAsmJS_) {
114 #ifdef DEBUG
115 for (uint32_t i = 0; i < length_; i++) {
116 MOZ_ASSERT(!functions_[i].instance);
117 }
118 #endif
119 break;
120 }
121
122 for (uint32_t i = 0; i < length_; i++) {
123 if (functions_[i].instance) {
124 wasm::TraceInstanceEdge(trc, functions_[i].instance,
125 "wasm table instance");
126 } else {
127 MOZ_ASSERT(!functions_[i].code);
128 }
129 }
130 break;
131 }
132 case TableRepr::Ref: {
133 objects_.trace(trc);
134 break;
135 }
136 }
137 }
138
139 void Table::trace(JSTracer* trc) {
140 // The trace hook of WasmTableObject will call Table::tracePrivate at
141 // which point we can mark the rest of the children. If there is no
142 // WasmTableObject, call Table::tracePrivate directly. Redirecting through
143 // the WasmTableObject avoids marking the entire Table on each incoming
144 // edge (once per dependent Instance).
145 if (maybeObject_) {
146 TraceEdge(trc, &maybeObject_, "wasm table object");
147 } else {
148 tracePrivate(trc);
149 }
150 }
151
152 uint8_t* Table::instanceElements() const {
153 if (repr() == TableRepr::Ref) {
154 return (uint8_t*)objects_.begin();
155 }
156 return (uint8_t*)functions_.begin();
157 }
158
159 const FunctionTableElem& Table::getFuncRef(uint32_t address) const {
160 MOZ_ASSERT(isFunction());
161 return functions_[address];
162 }
163
164 bool Table::getFuncRef(JSContext* cx, uint32_t address,
165 MutableHandleFunction fun) const {
166 MOZ_ASSERT(isFunction());
167
168 const FunctionTableElem& elem = getFuncRef(address);
169 if (!elem.code) {
170 fun.set(nullptr);
171 return true;
172 }
173
174 Instance& instance = *elem.instance;
175 const CodeRange& codeRange = *instance.code().lookupFuncRange(elem.code);
176 AutoRealmUnchecked ar(cx, instance.realm());
177 return instance.getExportedFunction(cx, codeRange.funcIndex(), fun);
178 }
179
180 void Table::setFuncRef(uint32_t address, JSFunction* fun) {
181 MOZ_ASSERT(isFunction());
182 MOZ_ASSERT(fun->isWasm());
183
184 // Tables can store references to wasm functions from other instances. To
185 // preserve the === function identity required by the JS embedding spec, we
186 // must set the element to the function's underlying
187 // CodeRange.funcCheckedCallEntry and Instance so that Table.get()s always
188 // produce the same function object as was imported.
189 setFuncRef(address, fun->wasmCheckedCallEntry(), &fun->wasmInstance());
190 }
191
192 void Table::setFuncRef(uint32_t address, void* code, Instance* instance) {
193 MOZ_ASSERT(isFunction());
194
195 FunctionTableElem& elem = functions_[address];
196 if (elem.instance) {
197 gc::PreWriteBarrier(elem.instance->objectUnbarriered());
198 }
199
200 if (!isAsmJS_) {
201 elem.code = code;
202 elem.instance = instance;
203 MOZ_ASSERT(elem.instance->objectUnbarriered()->isTenured(),
204 "no postWriteBarrier (Table::set)");
205 } else {
206 elem.code = code;
207 elem.instance = nullptr;
208 }
209 }
210
211 void Table::fillFuncRef(uint32_t address, uint32_t fillCount, FuncRef ref,
212 JSContext* cx) {
213 MOZ_ASSERT(isFunction());
214
215 if (ref.isNull()) {
216 for (uint32_t i = address, end = address + fillCount; i != end; i++) {
217 setNull(i);
218 }
219 return;
220 }
221
222 RootedFunction fun(cx, ref.asJSFunction());
223 void* code = fun->wasmCheckedCallEntry();
224 Instance& instance = fun->wasmInstance();
225 for (uint32_t i = address, end = address + fillCount; i != end; i++) {
226 setFuncRef(i, code, &instance);
227 }
228 }
229
230 AnyRef Table::getAnyRef(uint32_t address) const {
231 MOZ_ASSERT(!isFunction());
232 return objects_[address];
233 }
234
235 void Table::setAnyRef(uint32_t address, AnyRef ref) {
236 MOZ_ASSERT(!isFunction());
237 objects_[address] = ref;
238 }
239
240 void Table::fillAnyRef(uint32_t address, uint32_t fillCount, AnyRef ref) {
241 MOZ_ASSERT(!isFunction());
242 for (uint32_t i = address, end = address + fillCount; i != end; i++) {
243 objects_[i] = ref;
244 }
245 }
246
247 void Table::setRef(uint32_t address, AnyRef ref) {
248 if (ref.isNull()) {
249 setNull(address);
250 } else if (isFunction()) {
251 JSFunction* func = &ref.toJSObject().as<JSFunction>();
252 setFuncRef(address, func);
253 } else {
254 setAnyRef(address, ref);
255 }
256 }
257
258 bool Table::getValue(JSContext* cx, uint32_t address,
259 MutableHandleValue result) const {
260 switch (repr()) {
261 case TableRepr::Func: {
262 MOZ_RELEASE_ASSERT(!isAsmJS());
263 RootedFunction fun(cx);
264 if (!getFuncRef(cx, address, &fun)) {
265 return false;
266 }
267 result.setObjectOrNull(fun);
268 return true;
269 }
270 case TableRepr::Ref: {
271 if (!ValType(elemType_).isExposable()) {
272 JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
273 JSMSG_WASM_BAD_VAL_TYPE);
274 return false;
275 }
276 return ToJSValue(cx, &objects_[address], ValType(elemType_), result);
277 }
278 default:
279 MOZ_CRASH();
280 }
281 }
282
283 void Table::setNull(uint32_t address) {
284 switch (repr()) {
285 case TableRepr::Func: {
286 MOZ_RELEASE_ASSERT(!isAsmJS_);
287 FunctionTableElem& elem = functions_[address];
288 if (elem.instance) {
289 gc::PreWriteBarrier(elem.instance->objectUnbarriered());
290 }
291
292 elem.code = nullptr;
293 elem.instance = nullptr;
294 break;
295 }
296 case TableRepr::Ref: {
297 setAnyRef(address, AnyRef::null());
298 break;
299 }
300 }
301 }
302
303 bool Table::copy(JSContext* cx, const Table& srcTable, uint32_t dstIndex,
304 uint32_t srcIndex) {
305 MOZ_RELEASE_ASSERT(!srcTable.isAsmJS_);
306 switch (repr()) {
307 case TableRepr::Func: {
308 MOZ_RELEASE_ASSERT(elemType().isFuncHierarchy() &&
309 srcTable.elemType().isFuncHierarchy());
310 FunctionTableElem& dst = functions_[dstIndex];
311 if (dst.instance) {
312 gc::PreWriteBarrier(dst.instance->objectUnbarriered());
313 }
314
315 const FunctionTableElem& src = srcTable.functions_[srcIndex];
316 dst.code = src.code;
317 dst.instance = src.instance;
318
319 if (dst.instance) {
320 MOZ_ASSERT(dst.code);
321 MOZ_ASSERT(dst.instance->objectUnbarriered()->isTenured(),
322 "no postWriteBarrier (Table::copy)");
323 } else {
324 MOZ_ASSERT(!dst.code);
325 }
326 break;
327 }
328 case TableRepr::Ref: {
329 switch (srcTable.repr()) {
330 case TableRepr::Ref: {
331 setAnyRef(dstIndex, srcTable.getAnyRef(srcIndex));
332 break;
333 }
334 case TableRepr::Func: {
335 MOZ_RELEASE_ASSERT(srcTable.elemType().isFuncHierarchy());
336 // Upcast.
337 RootedFunction fun(cx);
338 if (!srcTable.getFuncRef(cx, srcIndex, &fun)) {
339 // OOM, so just pass it on.
340 return false;
341 }
342 setAnyRef(dstIndex, AnyRef::fromJSObject(*fun));
343 break;
344 }
345 }
346 break;
347 }
348 }
349 return true;
350 }
351
352 uint32_t Table::grow(uint32_t delta) {
353 // This isn't just an optimization: movingGrowable() assumes that
354 // onMovingGrowTable does not fire when length == maximum.
355 if (!delta) {
356 return length_;
357 }
358
359 uint32_t oldLength = length_;
360
361 CheckedInt<uint32_t> newLength = oldLength;
362 newLength += delta;
363 if (!newLength.isValid() || newLength.value() > MaxTableElemsRuntime) {
364 return -1;
365 }
366
367 if (maximum_ && newLength.value() > maximum_.value()) {
368 return -1;
369 }
370
371 MOZ_ASSERT(movingGrowable());
372
373 switch (repr()) {
374 case TableRepr::Func: {
375 MOZ_RELEASE_ASSERT(!isAsmJS_);
376 if (!functions_.resize(newLength.value())) {
377 return -1;
378 }
379 break;
380 }
381 case TableRepr::Ref: {
382 if (!objects_.resize(newLength.value())) {
383 return -1;
384 }
385 break;
386 }
387 }
388
389 if (auto* object = maybeObject_.unbarrieredGet()) {
390 RemoveCellMemory(object, gcMallocBytes(), MemoryUse::WasmTableTable);
391 }
392
393 length_ = newLength.value();
394
395 if (auto* object = maybeObject_.unbarrieredGet()) {
396 AddCellMemory(object, gcMallocBytes(), MemoryUse::WasmTableTable);
397 }
398
399 for (InstanceSet::Range r = observers_.all(); !r.empty(); r.popFront()) {
400 r.front()->instance().onMovingGrowTable(this);
401 }
402
403 return oldLength;
404 }
405
406 bool Table::movingGrowable() const {
407 return !maximum_ || length_ < maximum_.value();
408 }
409
410 bool Table::addMovingGrowObserver(JSContext* cx, WasmInstanceObject* instance) {
411 MOZ_ASSERT(movingGrowable());
412
413 // A table can be imported multiple times into an instance, but we only
414 // register the instance as an observer once.
415
416 if (!observers_.put(instance)) {
417 ReportOutOfMemory(cx);
418 return false;
419 }
420
421 return true;
422 }
423
424 void Table::fillUninitialized(uint32_t address, uint32_t fillCount,
425 HandleAnyRef ref, JSContext* cx) {
426 #ifdef DEBUG
427 assertRangeNull(address, fillCount);
428 #endif // DEBUG
429 switch (repr()) {
430 case TableRepr::Func: {
431 MOZ_RELEASE_ASSERT(!isAsmJS_);
432 fillFuncRef(address, fillCount, FuncRef::fromAnyRefUnchecked(ref), cx);
433 break;
434 }
435 case TableRepr::Ref: {
436 fillAnyRef(address, fillCount, ref);
437 break;
438 }
439 }
440 }
441
442 #ifdef DEBUG
443 void Table::assertRangeNull(uint32_t address, uint32_t length) const {
444 switch (repr()) {
445 case TableRepr::Func:
446 for (uint32_t i = address; i < address + length; i++) {
447 MOZ_ASSERT(getFuncRef(i).instance == nullptr);
448 MOZ_ASSERT(getFuncRef(i).code == nullptr);
449 }
450 break;
451 case TableRepr::Ref:
452 for (uint32_t i = address; i < address + length; i++) {
453 MOZ_ASSERT(getAnyRef(i).isNull());
454 }
455 break;
456 }
457 }
458
459 void Table::assertRangeNotNull(uint32_t address, uint32_t length) const {
460 switch (repr()) {
461 case TableRepr::Func:
462 for (uint32_t i = address; i < address + length; i++) {
463 MOZ_ASSERT_IF(!isAsmJS_, getFuncRef(i).instance != nullptr);
464 MOZ_ASSERT(getFuncRef(i).code != nullptr);
465 }
466 break;
467 case TableRepr::Ref:
468 for (uint32_t i = address; i < address + length; i++) {
469 MOZ_ASSERT(!getAnyRef(i).isNull());
470 }
471 break;
472 }
473 }
474 #endif // DEBUG
475
476 size_t Table::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
477 if (isFunction()) {
478 return functions_.sizeOfExcludingThis(mallocSizeOf);
479 }
480 return objects_.sizeOfExcludingThis(mallocSizeOf);
481 }
482
483 size_t Table::gcMallocBytes() const {
484 size_t size = sizeof(*this);
485 if (isFunction()) {
486 size += length() * sizeof(FunctionTableElem);
487 } else {
488 size += length() * sizeof(TableAnyRefVector::ElementType);
489 }
490 return size;
491 }