Debian Codegraph @

js/src/wasm/WasmValidate.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_validate_h
20 #define wasm_validate_h
21
22 #include <type_traits>
23
24 #include "js/Utility.h"
25 #include "js/WasmFeatures.h"
26
27 #include "wasm/WasmBinary.h"
28 #include "wasm/WasmCompile.h"
29 #include "wasm/WasmCompileArgs.h"
30 #include "wasm/WasmModuleTypes.h"
31 #include "wasm/WasmOpIter.h"
32 #include "wasm/WasmProcess.h"
33 #include "wasm/WasmTypeDef.h"
34
35 namespace js {
36 namespace wasm {
37
38 // ElemSegmentFlags provides methods for decoding and encoding the flags field
39 // of an element segment. This is needed as the flags field has a non-trivial
40 // encoding that is effectively split into independent `kind` and `payload`
41 // enums.
42 class ElemSegmentFlags {
43 enum class Flags : uint32_t {
44 // 0 means active. 1 means (passive or declared), disambiguated by the next
45 // bit.
46 Passive = 0x1,
47 // For active segments, 1 means a table index is present. Otherwise, 0 means
48 // passive and 1 means declared.
49 TableIndexOrDeclared = 0x2,
50 // 0 means element kind / index (currently only func indexes). 1 means
51 // element ref type and initializer expressions.
52 ElemExpressions = 0x4,
53
54 // Below this line are convenient combinations of flags
55 KindMask = Passive | TableIndexOrDeclared,
56 PayloadMask = ElemExpressions,
57 AllFlags = Passive | TableIndexOrDeclared | ElemExpressions,
58 };
59 uint32_t encoded_;
60
61 explicit ElemSegmentFlags(uint32_t encoded) : encoded_(encoded) {}
62
63 public:
64 ElemSegmentFlags(ElemSegmentKind kind, ElemSegmentPayload payload) {
65 encoded_ = uint32_t(kind) | uint32_t(payload);
66 }
67
68 static mozilla::Maybe<ElemSegmentFlags> construct(uint32_t encoded) {
69 if (encoded > uint32_t(Flags::AllFlags)) {
70 return mozilla::Nothing();
71 }
72 return mozilla::Some(ElemSegmentFlags(encoded));
73 }
74
75 uint32_t encoded() const { return encoded_; }
76
77 ElemSegmentKind kind() const {
78 return static_cast<ElemSegmentKind>(encoded_ & uint32_t(Flags::KindMask));
79 }
80 ElemSegmentPayload payload() const {
81 return static_cast<ElemSegmentPayload>(encoded_ &
82 uint32_t(Flags::PayloadMask));
83 }
84 };
85
86 // OpIter specialized for validation.
87
88 class NothingVector {
89 mozilla::Nothing unused_;
90
91 public:
92 bool reserve(size_t size) { return true; }
93 bool resize(size_t length) { return true; }
94 mozilla::Nothing& operator[](size_t) { return unused_; }
95 mozilla::Nothing& back() { return unused_; }
96 size_t length() const { return 0; }
97 bool append(mozilla::Nothing& nothing) { return true; }
98 void infallibleAppend(mozilla::Nothing& nothing) {}
99 };
100
101 struct ValidatingPolicy {
102 using Value = mozilla::Nothing;
103 using ValueVector = NothingVector;
104 using ControlItem = mozilla::Nothing;
105 };
106
107 template <typename Policy>
108 class OpIter;
109
110 using ValidatingOpIter = OpIter<ValidatingPolicy>;
111
112 // Shared subtyping function across validation.
113
114 [[nodiscard]] bool CheckIsSubtypeOf(Decoder& d, const CodeMetadata& codeMeta,
115 size_t opcodeOffset, ResultType subType,
116 ResultType superType);
117 [[nodiscard]] bool CheckIsSubtypeOf(Decoder& d, const CodeMetadata& codeMeta,
118 size_t opcodeOffset, StorageType subType,
119 StorageType superType);
120
121 // The local entries are part of function bodies and thus serialized by both
122 // wasm and asm.js and decoded as part of both validation and compilation.
123
124 [[nodiscard]] bool EncodeLocalEntries(Encoder& e, const ValTypeVector& locals);
125
126 // This performs no validation; the local entries must already have been
127 // validated by an earlier pass.
128
129 [[nodiscard]] bool DecodeValidatedLocalEntries(const TypeContext& types,
130 Decoder& d,
131 ValTypeVector* locals);
132
133 // This validates the entries. Function params are inserted before the locals
134 // to generate the full local entries for use in validation
135
136 [[nodiscard]] bool DecodeLocalEntriesWithParams(Decoder& d,
137 const CodeMetadata& codeMeta,
138 uint32_t funcIndex,
139 ValTypeVector* locals);
140
141 // Returns whether the given [begin, end) prefix of a module's bytecode starts a
142 // code section and, if so, returns the BytecodeRange of that code section.
143 // Note that, even if this function returns 'false', [begin, end) may actually
144 // be a valid module in the special case when there are no function defs and the
145 // code section is not present. Such modules can be valid so the caller must
146 // handle this special case.
147
148 [[nodiscard]] bool StartsCodeSection(const uint8_t* begin, const uint8_t* end,
149 BytecodeRange* codeSection);
150
151 // Calling DecodeModuleEnvironment decodes all sections up to the code section
152 // and performs full validation of all those sections. The client must then
153 // decode the code section itself, reusing ValidateFunctionBody if necessary,
154 // and finally call DecodeModuleTail to decode all remaining sections after the
155 // code section (again, performing full validation).
156
157 [[nodiscard]] bool DecodeModuleEnvironment(Decoder& d, CodeMetadata* codeMeta,
158 ModuleMetadata* moduleMeta);
159
160 [[nodiscard]] bool ValidateFunctionBody(const CodeMetadata& codeMeta,
161 uint32_t funcIndex, uint32_t bodySize,
162 Decoder& d);
163
164 [[nodiscard]] bool DecodeModuleTail(Decoder& d, CodeMetadata* codeMeta,
165 ModuleMetadata* meta);
166
167 // Validate an entire module, returning true if the module was validated
168 // successfully. If Validate returns false:
169 // - if *error is null, the caller should report out-of-memory
170 // - otherwise, there was a legitimate error described by *error
171
172 [[nodiscard]] bool Validate(JSContext* cx, const BytecodeSource& bytecode,
173 const FeatureOptions& options, UniqueChars* error);
174
175 struct NopOpDumper {
176 void dumpOpBegin(OpBytes op) {}
177 void dumpOpEnd() {}
178 void dumpTypeIndex(uint32_t typeIndex, bool asTypeUse = false) {}
179 void dumpFuncIndex(uint32_t funcIndex) {}
180 void dumpTableIndex(uint32_t tableIndex) {}
181 void dumpGlobalIndex(uint32_t globalIndex) {}
182 void dumpMemoryIndex(uint32_t memoryIndex) {}
183 void dumpElemIndex(uint32_t elemIndex) {}
184 void dumpDataIndex(uint32_t dataIndex) {}
185 void dumpTagIndex(uint32_t tagIndex) {}
186 void dumpLocalIndex(uint32_t localIndex) {}
187 void dumpBlockType(BlockType type) {}
188 void dumpI32Const(int32_t constant) {}
189 void dumpI64Const(int64_t constant) {}
190 void dumpF32Const(float constant) {}
191 void dumpF64Const(double constant) {}
192 void dumpV128Const(V128 constant) {}
193 void dumpVectorMask(V128 mask) {}
194 void dumpRefType(RefType type) {}
195 void dumpHeapType(RefType type) {}
196 void dumpValType(ValType type) {}
197 void dumpTryTableCatches(const TryTableCatchVector& catches) {}
198 void dumpLinearMemoryAddress(LinearMemoryAddress<mozilla::Nothing> addr) {}
199 void dumpBlockDepth(uint32_t relativeDepth) {}
200 void dumpBlockDepths(const Uint32Vector& relativeDepths) {}
201 void dumpFieldIndex(uint32_t fieldIndex) {}
202 void dumpNumElements(uint32_t numElements) {}
203 void dumpLaneIndex(uint32_t laneIndex) {}
204
205 void startScope() {};
206 void endScope() {};
207 };
208
209 template <class T>
210 [[nodiscard]] bool ValidateOps(ValidatingOpIter& iter, T& dumper,
211 const CodeMetadata& codeMeta);
212
213 } // namespace wasm
214 } // namespace js
215
216 #endif // namespace wasm_validate_h