Debian Codegraph @

js/src/wasm/WasmTable.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 *
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_table_h
20 #define wasm_table_h
21
22 #include "gc/Policy.h"
23 #include "wasm/WasmCode.h"
24
25 namespace js {
26 namespace wasm {
27
28 // A Table is an indexable array of opaque values. Tables are first-class
29 // stateful objects exposed to WebAssembly. asm.js also uses Tables to represent
30 // its homogeneous function-pointer tables.
31 //
32 // A table of FuncRef holds FunctionTableElems, which are (code*,instance*)
33 // pairs, where the instance must be traced.
34 //
35 // A table of AnyRef holds pointers, which must be traced.
36
37 using TableAnyRefVector = GCVector<HeapPtr<AnyRef>, 0, SystemAllocPolicy>;
38
39 class Table : public ShareableBase<Table> {
40 using InstanceSet = JS::WeakCache<GCHashSet<
41 WeakHeapPtr<WasmInstanceObject*>,
42 StableCellHasher<WeakHeapPtr<WasmInstanceObject*>>, SystemAllocPolicy>>;
43 using FuncRefVector = Vector<FunctionTableElem, 0, SystemAllocPolicy>;
44
45 WeakHeapPtr<WasmTableObject*> maybeObject_;
46 InstanceSet observers_;
47 FuncRefVector functions_; // either functions_ has data
48 TableAnyRefVector objects_; // or objects_, but not both
49 const AddressType addressType_;
50 const RefType elemType_;
51 const bool isAsmJS_;
52 uint32_t length_;
53 const mozilla::Maybe<uint64_t> maximum_;
54
55 template <class>
56 friend struct js::MallocProvider;
57 Table(JSContext* cx, const TableDesc& desc,
58 Handle<WasmTableObject*> maybeObject, FuncRefVector&& functions);
59 Table(JSContext* cx, const TableDesc& desc,
60 Handle<WasmTableObject*> maybeObject, TableAnyRefVector&& objects);
61
62 void tracePrivate(JSTracer* trc);
63 friend class js::WasmTableObject;
64
65 public:
66 static RefPtr<Table> create(JSContext* cx, const TableDesc& desc,
67 Handle<WasmTableObject*> maybeObject);
68 ~Table();
69 void trace(JSTracer* trc);
70
71 AddressType addressType() const { return addressType_; }
72 RefType elemType() const { return elemType_; }
73 TableRepr repr() const { return elemType_.tableRepr(); }
74
75 bool isAsmJS() const {
76 MOZ_ASSERT(elemType_.isFuncHierarchy());
77 return isAsmJS_;
78 }
79
80 bool isFunction() const { return elemType().isFuncHierarchy(); }
81 uint32_t length() const { return length_; }
82 mozilla::Maybe<uint64_t> maximum() const { return maximum_; }
83
84 // Raw pointer to the table for use in TableInstanceData.
85 uint8_t* instanceElements() const;
86
87 // set/get/fillFuncRef is allowed only on table-of-funcref.
88 // get/fillAnyRef is allowed only on table-of-anyref.
89 // setNull is allowed on either.
90
91 const FunctionTableElem& getFuncRef(uint32_t address) const;
92 [[nodiscard]] bool getFuncRef(JSContext* cx, uint32_t address,
93 MutableHandleFunction fun) const;
94 void setFuncRef(uint32_t address, JSFunction* func);
95 void setFuncRef(uint32_t address, void* code, Instance* instance);
96 void fillFuncRef(uint32_t address, uint32_t fillCount, FuncRef ref,
97 JSContext* cx);
98
99 AnyRef getAnyRef(uint32_t address) const;
100 void setAnyRef(uint32_t address, AnyRef ref);
101 void fillAnyRef(uint32_t address, uint32_t fillCount, AnyRef ref);
102
103 // Sets ref automatically using the correct setter depending on the ref and
104 // table type (setNull, setFuncRef, or setAnyRef)
105 void setRef(uint32_t address, AnyRef ref);
106
107 // Get the element at address and convert it to a JS value.
108 [[nodiscard]] bool getValue(JSContext* cx, uint32_t address,
109 MutableHandleValue result) const;
110
111 void setNull(uint32_t address);
112
113 // Copy entry from |srcTable| at |srcIndex| to this table at |dstIndex|. Used
114 // by table.copy. May OOM if it needs to box up a function during an upcast.
115 [[nodiscard]] bool copy(JSContext* cx, const Table& srcTable,
116 uint32_t dstIndex, uint32_t srcIndex);
117
118 // grow() returns (uint32_t)-1 if it could not grow.
119 [[nodiscard]] uint32_t grow(uint32_t delta);
120 [[nodiscard]] bool movingGrowable() const;
121 [[nodiscard]] bool addMovingGrowObserver(JSContext* cx,
122 WasmInstanceObject* instance);
123
124 void fillUninitialized(uint32_t address, uint32_t fillCount, HandleAnyRef ref,
125 JSContext* cx);
126 #ifdef DEBUG
127 void assertRangeNull(uint32_t address, uint32_t length) const;
128 void assertRangeNotNull(uint32_t address, uint32_t length) const;
129 #endif // DEBUG
130
131 // about:memory reporting:
132
133 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
134
135 size_t gcMallocBytes() const;
136 };
137
138 using SharedTable = RefPtr<Table>;
139 using SharedTableVector = Vector<SharedTable, 0, SystemAllocPolicy>;
140
141 } // namespace wasm
142 } // namespace js
143
144 #endif // wasm_table_h