/src/WasmEdge/lib/validator/validator.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | #include "validator/validator.h" |
5 | | |
6 | | #include "ast/section.h" |
7 | | #include "common/errinfo.h" |
8 | | #include "common/hash.h" |
9 | | |
10 | | #include <numeric> |
11 | | #include <string> |
12 | | #include <unordered_set> |
13 | | |
14 | | using namespace std::literals; |
15 | | |
16 | | namespace WasmEdge { |
17 | | namespace Validator { |
18 | | |
19 | | namespace { |
20 | | |
21 | | static constexpr uint32_t MaxSubtypeDepth = 63; |
22 | | |
23 | | Expect<void> calculateSubtypeDepthRecursiveHelper( |
24 | | uint32_t Index, uint32_t Depth, std::unordered_set<uint32_t> &VisitedSet, |
25 | 2 | const FormChecker &Checker, uint32_t TypeIdx) { |
26 | 2 | if (VisitedSet.count(Index)) { |
27 | 1 | spdlog::error(ErrCode::Value::InvalidSubType); |
28 | 1 | spdlog::error(" Cycle detected in subtype hierarchy for type {}."sv, |
29 | 1 | Index); |
30 | 1 | return Unexpect(ErrCode::Value::InvalidSubType); |
31 | 1 | } |
32 | | |
33 | 1 | if (Depth >= MaxSubtypeDepth) { |
34 | 0 | spdlog::error(ErrCode::Value::InvalidSubType); |
35 | 0 | spdlog::error( |
36 | 0 | " subtype depth for Type section's {}th signature exceeded " |
37 | 0 | "the limits of {}"sv, |
38 | 0 | TypeIdx, MaxSubtypeDepth); |
39 | 0 | return Unexpect(ErrCode::Value::InvalidSubType); |
40 | 0 | } |
41 | | |
42 | 1 | VisitedSet.insert(Index); |
43 | 1 | const auto &TypeVec = Checker.getTypes(); |
44 | 1 | const auto &Type = *TypeVec[Index]; |
45 | 1 | for (const auto SuperIdx : Type.getSuperTypeIndices()) { |
46 | 1 | EXPECTED_TRY(calculateSubtypeDepthRecursiveHelper( |
47 | 1 | SuperIdx, Depth + 1, VisitedSet, Checker, TypeIdx) |
48 | 1 | .map_error([&](auto E) { |
49 | 1 | spdlog::error(" When checking super type index {}."sv, |
50 | 1 | SuperIdx); |
51 | 1 | return E; |
52 | 1 | })); |
53 | 1 | } |
54 | 0 | return {}; |
55 | 1 | } |
56 | | |
57 | | Expect<void> calculateSubtypeDepth(uint32_t TypeIdx, |
58 | 1 | const FormChecker &Checker) { |
59 | 1 | std::unordered_set<uint32_t> VisitedNodes; |
60 | 1 | return calculateSubtypeDepthRecursiveHelper(TypeIdx, 0, VisitedNodes, Checker, |
61 | 1 | TypeIdx); |
62 | 1 | } |
63 | | |
64 | | } // namespace |
65 | | |
66 | | // Validate Module. See "include/validator/validator.h". |
67 | 4.32k | Expect<void> Validator::validate(const AST::Module &Mod) { |
68 | | // https://webassembly.github.io/spec/core/valid/modules.html |
69 | 4.32k | Checker.reset(true); |
70 | | |
71 | | // Validate and register type section. |
72 | 4.32k | EXPECTED_TRY(validate(Mod.getTypeSection()).map_error([](auto E) { |
73 | 4.31k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Type)); |
74 | 4.31k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
75 | 4.31k | return E; |
76 | 4.31k | })); |
77 | | |
78 | | // Validate and register import section into FormChecker. |
79 | 4.31k | EXPECTED_TRY(validate(Mod.getImportSection()).map_error([](auto E) { |
80 | 4.29k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Import)); |
81 | 4.29k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
82 | 4.29k | return E; |
83 | 4.29k | })); |
84 | | |
85 | | // Validate function section and register functions into FormChecker. |
86 | 4.29k | EXPECTED_TRY(validate(Mod.getFunctionSection()).map_error([](auto E) { |
87 | 4.28k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Function)); |
88 | 4.28k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
89 | 4.28k | return E; |
90 | 4.28k | })); |
91 | | |
92 | | // Validate table section and register tables into FormChecker. |
93 | 4.28k | EXPECTED_TRY(validate(Mod.getTableSection()).map_error([](auto E) { |
94 | 4.27k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Table)); |
95 | 4.27k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
96 | 4.27k | return E; |
97 | 4.27k | })); |
98 | | |
99 | | // Validate memory section and register memories into FormChecker. |
100 | 4.27k | EXPECTED_TRY(validate(Mod.getMemorySection()).map_error([](auto E) { |
101 | 4.24k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Memory)); |
102 | 4.24k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
103 | 4.24k | return E; |
104 | 4.24k | })); |
105 | | |
106 | | // Validate global section and register globals into FormChecker. |
107 | 4.24k | EXPECTED_TRY(validate(Mod.getGlobalSection()).map_error([](auto E) { |
108 | 4.12k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Global)); |
109 | 4.12k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
110 | 4.12k | return E; |
111 | 4.12k | })); |
112 | | |
113 | | // Validate tag section and register tags into FormChecker. |
114 | 4.12k | EXPECTED_TRY(validate(Mod.getTagSection()).map_error([](auto E) { |
115 | 4.10k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Tag)); |
116 | 4.10k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
117 | 4.10k | return E; |
118 | 4.10k | })); |
119 | | |
120 | | // Validate export section. |
121 | 4.10k | EXPECTED_TRY(validate(Mod.getExportSection()).map_error([](auto E) { |
122 | 4.05k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Export)); |
123 | 4.05k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
124 | 4.05k | return E; |
125 | 4.05k | })); |
126 | | |
127 | | // Validate start section. |
128 | 4.05k | EXPECTED_TRY(validate(Mod.getStartSection()).map_error([](auto E) { |
129 | 4.04k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Start)); |
130 | 4.04k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
131 | 4.04k | return E; |
132 | 4.04k | })); |
133 | | |
134 | | // Validate element section which initialize tables. |
135 | 4.04k | EXPECTED_TRY(validate(Mod.getElementSection()).map_error([](auto E) { |
136 | 3.99k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Element)); |
137 | 3.99k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
138 | 3.99k | return E; |
139 | 3.99k | })); |
140 | | |
141 | | // Validate data section which initialize memories. |
142 | 3.99k | EXPECTED_TRY(validate(Mod.getDataSection()).map_error([](auto E) { |
143 | 3.97k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Data)); |
144 | 3.97k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
145 | 3.97k | return E; |
146 | 3.97k | })); |
147 | | |
148 | | // Validate code section and expressions. |
149 | 3.97k | EXPECTED_TRY(validate(Mod.getCodeSection()).map_error([](auto E) { |
150 | 2.33k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Code)); |
151 | 2.33k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
152 | 2.33k | return E; |
153 | 2.33k | })); |
154 | | |
155 | | // Multiple tables is for the ReferenceTypes proposal. |
156 | 2.33k | if (Checker.getTables().size() > 1 && |
157 | 44 | !Conf.hasProposal(Proposal::ReferenceTypes)) { |
158 | 0 | spdlog::error(ErrCode::Value::MultiTables); |
159 | 0 | spdlog::error(ErrInfo::InfoProposal(Proposal::ReferenceTypes)); |
160 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
161 | 0 | return Unexpect(ErrCode::Value::MultiTables); |
162 | 0 | } |
163 | | |
164 | | // Multiple memories is for the MultiMemories proposal. |
165 | 2.33k | if (Checker.getMemories() > 1 && !Conf.hasProposal(Proposal::MultiMemories)) { |
166 | 0 | spdlog::error(ErrCode::Value::MultiMemories); |
167 | 0 | spdlog::error(ErrInfo::InfoProposal(Proposal::MultiMemories)); |
168 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module)); |
169 | 0 | return Unexpect(ErrCode::Value::MultiMemories); |
170 | 0 | } |
171 | | |
172 | | // Set the validated flag. |
173 | 2.33k | const_cast<AST::Module &>(Mod).setIsValidated(); |
174 | 2.33k | return {}; |
175 | 2.33k | } |
176 | | |
177 | | // Validate Sub type. See "include/validator/validator.h". |
178 | 7.72k | Expect<void> Validator::validate(const AST::SubType &Type) { |
179 | 7.72k | const auto &TypeVec = Checker.getTypes(); |
180 | 7.72k | const auto &CompType = Type.getCompositeType(); |
181 | | |
182 | | // Check the validation of the composite type. |
183 | 7.72k | if (CompType.isFunc()) { |
184 | 7.50k | const auto &FType = CompType.getFuncType(); |
185 | 7.50k | for (auto &PType : FType.getParamTypes()) { |
186 | 6.67k | EXPECTED_TRY(Checker.validate(PType).map_error([](auto E) { |
187 | 6.67k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function)); |
188 | 6.67k | return E; |
189 | 6.67k | })); |
190 | 6.67k | } |
191 | 7.50k | if (unlikely(!Conf.hasProposal(Proposal::MultiValue)) && |
192 | 0 | FType.getReturnTypes().size() > 1) { |
193 | 0 | spdlog::error(ErrCode::Value::InvalidResultArity); |
194 | 0 | spdlog::error(ErrInfo::InfoProposal(Proposal::MultiValue)); |
195 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function)); |
196 | 0 | return Unexpect(ErrCode::Value::InvalidResultArity); |
197 | 0 | } |
198 | 7.50k | for (auto &RType : FType.getReturnTypes()) { |
199 | 5.57k | EXPECTED_TRY(Checker.validate(RType).map_error([](auto E) { |
200 | 5.57k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function)); |
201 | 5.57k | return E; |
202 | 5.57k | })); |
203 | 5.57k | } |
204 | 7.50k | } else { |
205 | 221 | const auto &FTypes = CompType.getFieldTypes(); |
206 | 221 | for (auto &FieldType : FTypes) { |
207 | 166 | EXPECTED_TRY(Checker.validate(FieldType.getStorageType())); |
208 | 166 | } |
209 | 221 | } |
210 | | |
211 | | // In current version, the length of type index vector will be <= 1. |
212 | 7.71k | if (Type.getSuperTypeIndices().size() > 1) { |
213 | 3 | spdlog::error(ErrCode::Value::InvalidSubType); |
214 | 3 | spdlog::error(" Accepts only one super type currently."sv); |
215 | 3 | return Unexpect(ErrCode::Value::InvalidSubType); |
216 | 3 | } |
217 | | |
218 | 7.71k | for (const auto &Index : Type.getSuperTypeIndices()) { |
219 | 2 | if (unlikely(Index >= TypeVec.size())) { |
220 | 1 | spdlog::error(ErrCode::Value::InvalidSubType); |
221 | 1 | spdlog::error( |
222 | 1 | ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::DefinedType, Index, |
223 | 1 | static_cast<uint32_t>(TypeVec.size()))); |
224 | 1 | return Unexpect(ErrCode::Value::InvalidSubType); |
225 | 1 | } |
226 | | |
227 | 1 | if (auto Res = calculateSubtypeDepth(Index, Checker); !Res) { |
228 | 1 | spdlog::error(" When checking subtype hierarchy of super type {}."sv, |
229 | 1 | Index); |
230 | 1 | return Unexpect(Res.error()); |
231 | 1 | } |
232 | | |
233 | 0 | if (TypeVec[Index]->isFinal()) { |
234 | 0 | spdlog::error(ErrCode::Value::InvalidSubType); |
235 | 0 | spdlog::error(" Super type should not be final."sv); |
236 | 0 | return Unexpect(ErrCode::Value::InvalidSubType); |
237 | 0 | } |
238 | 0 | auto &SuperType = TypeVec[Index]->getCompositeType(); |
239 | 0 | if (!AST::TypeMatcher::matchType(Checker.getTypes(), SuperType, CompType)) { |
240 | 0 | spdlog::error(ErrCode::Value::InvalidSubType); |
241 | 0 | spdlog::error(" Super type not matched."sv); |
242 | 0 | return Unexpect(ErrCode::Value::InvalidSubType); |
243 | 0 | } |
244 | 0 | } |
245 | 7.71k | return {}; |
246 | 7.71k | } |
247 | | |
248 | | // Validate Limit type. See "include/validator/validator.h". |
249 | 1.95k | Expect<void> Validator::validate(const AST::Limit &Lim) { |
250 | 1.95k | if (Lim.hasMax() && Lim.getMin() > Lim.getMax()) { |
251 | 14 | spdlog::error(ErrCode::Value::InvalidLimit); |
252 | 14 | spdlog::error(ErrInfo::InfoLimit(Lim.hasMax(), Lim.getMin(), Lim.getMax())); |
253 | 14 | return Unexpect(ErrCode::Value::InvalidLimit); |
254 | 14 | } |
255 | 1.94k | if (Lim.isShared() && unlikely(!Lim.hasMax())) { |
256 | 0 | spdlog::error(ErrCode::Value::SharedMemoryNoMax); |
257 | 0 | return Unexpect(ErrCode::Value::SharedMemoryNoMax); |
258 | 0 | } |
259 | 1.94k | return {}; |
260 | 1.94k | } |
261 | | |
262 | | // Validate Table type. See "include/validator/validator.h". |
263 | 487 | Expect<void> Validator::validate(const AST::TableType &Tab) { |
264 | | // Validate value type. |
265 | 487 | EXPECTED_TRY(Checker.validate(Tab.getRefType())); |
266 | | // Validate table limits. |
267 | 485 | return validate(Tab.getLimit()).map_error([](auto E) { |
268 | 3 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Limit)); |
269 | 3 | return E; |
270 | 3 | }); |
271 | 487 | } |
272 | | |
273 | | // Validate Memory type. See "include/validator/validator.h". |
274 | 1.47k | Expect<void> Validator::validate(const AST::MemoryType &Mem) { |
275 | | // Validate memory limits. |
276 | 1.47k | const auto &Lim = Mem.getLimit(); |
277 | 1.47k | EXPECTED_TRY(validate(Lim).map_error([](auto E) { |
278 | 1.46k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Limit)); |
279 | 1.46k | return E; |
280 | 1.46k | })); |
281 | 1.46k | if (Lim.getMin() > LIMIT_MEMORYTYPE || |
282 | 1.45k | (Lim.hasMax() && Lim.getMax() > LIMIT_MEMORYTYPE)) { |
283 | | // TODO: MEMORY64 - fully support implementation. |
284 | 22 | ErrCode::Value FailCode = Conf.hasProposal(Proposal::Memory64) |
285 | 22 | ? ErrCode::Value::InvalidMemPages64 |
286 | 22 | : ErrCode::Value::InvalidMemPages; |
287 | 22 | spdlog::error(FailCode); |
288 | 22 | spdlog::error(ErrInfo::InfoLimit(Lim.hasMax(), Lim.getMin(), Lim.getMax())); |
289 | 22 | return Unexpect(FailCode); |
290 | 22 | } |
291 | 1.43k | return {}; |
292 | 1.46k | } |
293 | | |
294 | | // Validate Global type. See "include/validator/validator.h". |
295 | 283 | Expect<void> Validator::validate(const AST::GlobalType &Glob) { |
296 | | // Validate value type. |
297 | 283 | return Checker.validate(Glob.getValType()); |
298 | 283 | } |
299 | | |
300 | | // Validate Table segment. See "include/validator/validator.h". |
301 | 442 | Expect<void> Validator::validate(const AST::TableSegment &TabSeg) { |
302 | 442 | if (TabSeg.getExpr().getInstrs().size() > 0) { |
303 | | // Check ref initialization is a const expression. |
304 | 9 | EXPECTED_TRY( |
305 | 9 | validateConstExpr(TabSeg.getExpr().getInstrs(), |
306 | 9 | {ValType(TabSeg.getTableType().getRefType())}) |
307 | 9 | .map_error([](auto E) { |
308 | 9 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
309 | 9 | return E; |
310 | 9 | })); |
311 | 433 | } else { |
312 | | // No init expression. Check the reference type is nullable. |
313 | 433 | if (!TabSeg.getTableType().getRefType().isNullableRefType()) { |
314 | 3 | spdlog::error(ErrCode::Value::TypeCheckFailed); |
315 | 3 | spdlog::error(ErrInfo::InfoMismatch( |
316 | 3 | ValType(TypeCode::RefNull, |
317 | 3 | TabSeg.getTableType().getRefType().getHeapTypeCode(), |
318 | 3 | TabSeg.getTableType().getRefType().getTypeIndex()), |
319 | 3 | TabSeg.getTableType().getRefType())); |
320 | 3 | return Unexpect(ErrCode::Value::TypeCheckFailed); |
321 | 3 | } |
322 | 433 | } |
323 | | // Validate table type. |
324 | 434 | return validate(TabSeg.getTableType()).map_error([](auto E) { |
325 | 3 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Table)); |
326 | 3 | return E; |
327 | 3 | }); |
328 | 442 | } |
329 | | |
330 | | // Validate Global segment. See "include/validator/validator.h". |
331 | 348 | Expect<void> Validator::validate(const AST::GlobalSegment &GlobSeg) { |
332 | | // Check global initialization is a const expression. |
333 | 348 | EXPECTED_TRY(validateConstExpr(GlobSeg.getExpr().getInstrs(), |
334 | 229 | {GlobSeg.getGlobalType().getValType()}) |
335 | 229 | .map_error([](auto E) { |
336 | 229 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
337 | 229 | return E; |
338 | 229 | })); |
339 | | // Validate global type. |
340 | 229 | return validate(GlobSeg.getGlobalType()).map_error([](auto E) { |
341 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Global)); |
342 | 0 | return E; |
343 | 0 | }); |
344 | 348 | } |
345 | | |
346 | | // Validate Element segment. See "include/validator/validator.h". |
347 | 792 | Expect<void> Validator::validate(const AST::ElementSegment &ElemSeg) { |
348 | | // Check initialization expressions are const expressions. |
349 | 1.70k | for (auto &Expr : ElemSeg.getInitExprs()) { |
350 | 1.70k | EXPECTED_TRY( |
351 | 1.70k | validateConstExpr(Expr.getInstrs(), {ValType(ElemSeg.getRefType())}) |
352 | 1.70k | .map_error([](auto E) { |
353 | 1.70k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
354 | 1.70k | return E; |
355 | 1.70k | })); |
356 | 1.70k | } |
357 | | |
358 | | // The reference type should be valid. |
359 | 768 | EXPECTED_TRY(Checker.validate(ElemSeg.getRefType())); |
360 | | |
361 | | // Passive and declarative cases are valid with the valid reference type. |
362 | 766 | if (ElemSeg.getMode() == AST::ElementSegment::ElemMode::Active) { |
363 | | // Check table index and reference type in context. |
364 | 481 | const auto &TableVec = Checker.getTables(); |
365 | 481 | if (ElemSeg.getIdx() >= TableVec.size()) { |
366 | 10 | spdlog::error(ErrCode::Value::InvalidTableIdx); |
367 | 10 | spdlog::error(ErrInfo::InfoForbidIndex( |
368 | 10 | ErrInfo::IndexCategory::Table, ElemSeg.getIdx(), |
369 | 10 | static_cast<uint32_t>(TableVec.size()))); |
370 | 10 | return Unexpect(ErrCode::Value::InvalidTableIdx); |
371 | 10 | } |
372 | | // TODO: Use AST::TypeMatcher::matchType() to match types instead. |
373 | | // For the element segments, the RefType may not record the strict type |
374 | | // index, and should check the init exprs for the real type index to do type |
375 | | // matching. But for the table type, the type index is recorded into the |
376 | | // heap type. So it will fail here to do strict type matching. Therefore, |
377 | | // only check the FuncRef and ExternRef and the nullable here. |
378 | 471 | if (TableVec[ElemSeg.getIdx()].isFuncRefType() != |
379 | 471 | ElemSeg.getRefType().isFuncRefType() || |
380 | 469 | (!TableVec[ElemSeg.getIdx()].isNullableRefType() && |
381 | 2 | ElemSeg.getRefType().isNullableRefType())) { |
382 | | // Reference type not matched. |
383 | 2 | spdlog::error(ErrCode::Value::TypeCheckFailed); |
384 | 2 | spdlog::error(ErrInfo::InfoMismatch(TableVec[ElemSeg.getIdx()], |
385 | 2 | ElemSeg.getRefType())); |
386 | 2 | return Unexpect(ErrCode::Value::TypeCheckFailed); |
387 | 2 | } |
388 | | // Check table initialization is a const expression. |
389 | 469 | return validateConstExpr(ElemSeg.getExpr().getInstrs(), |
390 | 469 | {ValType(TypeCode::I32)}) |
391 | 469 | .map_error([](auto E) { |
392 | 14 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
393 | 14 | return E; |
394 | 14 | }); |
395 | 471 | } |
396 | 285 | return {}; |
397 | 766 | } |
398 | | |
399 | | // Validate Code segment. See "include/validator/validator.h". |
400 | | Expect<void> Validator::validate(const AST::CodeSegment &CodeSeg, |
401 | 13.8k | const uint32_t TypeIdx) { |
402 | | // Due to the validation of the function section, the type of index bust be a |
403 | | // function type. |
404 | 13.8k | const auto &FuncType = |
405 | 13.8k | Checker.getTypes()[TypeIdx]->getCompositeType().getFuncType(); |
406 | | // Reset stack in FormChecker. |
407 | 13.8k | Checker.reset(); |
408 | | // Add parameters into this frame. |
409 | 13.8k | for (auto &Type : FuncType.getParamTypes()) { |
410 | | // Local passed by function parameters must have been initialized. |
411 | 11.2k | Checker.addLocal(Type, true); |
412 | 11.2k | } |
413 | | // Add locals into this frame. |
414 | 13.8k | for (auto Val : CodeSeg.getLocals()) { |
415 | 8.41M | for (uint32_t Cnt = 0; Cnt < Val.first; ++Cnt) { |
416 | | // The local value type should be valid. |
417 | 8.41M | EXPECTED_TRY(Checker.validate(Val.second)); |
418 | 8.41M | Checker.addLocal(Val.second, false); |
419 | 8.41M | } |
420 | 2.35k | } |
421 | | // Validate function body expression. |
422 | 13.8k | return Checker |
423 | 13.8k | .validate(CodeSeg.getExpr().getInstrs(), FuncType.getReturnTypes()) |
424 | 13.8k | .map_error([](auto E) { |
425 | 1.62k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
426 | 1.62k | return E; |
427 | 1.62k | }); |
428 | 13.8k | } |
429 | | |
430 | | // Validate Data segment. See "include/validator/validator.h". |
431 | 338 | Expect<void> Validator::validate(const AST::DataSegment &DataSeg) { |
432 | 338 | switch (DataSeg.getMode()) { |
433 | 183 | case AST::DataSegment::DataMode::Active: { |
434 | | // Check memory index in context. |
435 | 183 | const auto &MemNum = Checker.getMemories(); |
436 | 183 | if (DataSeg.getIdx() >= MemNum) { |
437 | 21 | spdlog::error(ErrCode::Value::InvalidMemoryIdx); |
438 | 21 | spdlog::error(ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::Memory, |
439 | 21 | DataSeg.getIdx(), MemNum)); |
440 | 21 | return Unexpect(ErrCode::Value::InvalidMemoryIdx); |
441 | 21 | } |
442 | | // Check memory initialization is a const expression. |
443 | 162 | return validateConstExpr(DataSeg.getExpr().getInstrs(), |
444 | 162 | {ValType(TypeCode::I32)}) |
445 | 162 | .map_error([](auto E) { |
446 | 6 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
447 | 6 | return E; |
448 | 6 | }); |
449 | 183 | } |
450 | 155 | case AST::DataSegment::DataMode::Passive: |
451 | | // Passive case is always valid. |
452 | 155 | return {}; |
453 | 0 | default: |
454 | 0 | return {}; |
455 | 338 | } |
456 | 338 | } |
457 | | |
458 | | // Validate Import description. See "include/validator/validator.h". |
459 | 486 | Expect<void> Validator::validate(const AST::ImportDesc &ImpDesc) { |
460 | 486 | switch (ImpDesc.getExternalType()) { |
461 | | // External type and the external content are ensured to be matched in |
462 | | // loader phase. |
463 | 329 | case ExternalType::Function: { |
464 | 329 | const auto TId = ImpDesc.getExternalFuncTypeIdx(); |
465 | | // Function type index must exist in context and be valid. |
466 | 329 | if (TId >= Checker.getTypes().size()) { |
467 | 4 | spdlog::error(ErrCode::Value::InvalidFuncTypeIdx); |
468 | 4 | spdlog::error(ErrInfo::InfoForbidIndex( |
469 | 4 | ErrInfo::IndexCategory::FunctionType, TId, |
470 | 4 | static_cast<uint32_t>(Checker.getTypes().size()))); |
471 | 4 | return Unexpect(ErrCode::Value::InvalidFuncTypeIdx); |
472 | 4 | } |
473 | 325 | if (!Checker.getTypes()[TId]->getCompositeType().isFunc()) { |
474 | 1 | spdlog::error(ErrCode::Value::InvalidFuncTypeIdx); |
475 | 1 | spdlog::error(" Defined type index {} is not a function type."sv, TId); |
476 | 1 | return Unexpect(ErrCode::Value::InvalidFuncTypeIdx); |
477 | 1 | } |
478 | 324 | Checker.addRef(static_cast<uint32_t>(Checker.getFunctions().size())); |
479 | 324 | Checker.addFunc(TId, true); |
480 | 324 | return {}; |
481 | 325 | } |
482 | 53 | case ExternalType::Table: { |
483 | 53 | const auto &TabType = ImpDesc.getExternalTableType(); |
484 | | // Table type must be valid. |
485 | 53 | EXPECTED_TRY(validate(TabType).map_error([](auto E) { |
486 | 51 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Table)); |
487 | 51 | return E; |
488 | 51 | })); |
489 | 51 | Checker.addTable(TabType); |
490 | 51 | return {}; |
491 | 53 | } |
492 | 37 | case ExternalType::Memory: { |
493 | 37 | const auto &MemType = ImpDesc.getExternalMemoryType(); |
494 | | // Memory type must be valid. |
495 | 37 | EXPECTED_TRY(validate(MemType).map_error([](auto E) { |
496 | 35 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Memory)); |
497 | 35 | return E; |
498 | 35 | })); |
499 | 35 | Checker.addMemory(MemType); |
500 | 35 | return {}; |
501 | 37 | } |
502 | 13 | case ExternalType::Tag: { |
503 | 13 | const auto &T = ImpDesc.getExternalTagType(); |
504 | | // Tag type index must exist in context. |
505 | 13 | auto TagTypeIdx = T.getTypeIdx(); |
506 | 13 | if (TagTypeIdx >= Checker.getTypes().size()) { |
507 | 5 | spdlog::error(ErrCode::Value::InvalidTagIdx); |
508 | 5 | spdlog::error(ErrInfo::InfoForbidIndex( |
509 | 5 | ErrInfo::IndexCategory::TagType, TagTypeIdx, |
510 | 5 | static_cast<uint32_t>(Checker.getTypes().size()))); |
511 | 5 | return Unexpect(ErrCode::Value::InvalidTagIdx); |
512 | 5 | } |
513 | | // Tag type must be valid. |
514 | 8 | auto &CompType = Checker.getTypes()[TagTypeIdx]->getCompositeType(); |
515 | 8 | if (!CompType.isFunc()) { |
516 | 2 | spdlog::error(ErrCode::Value::InvalidTagIdx); |
517 | 2 | spdlog::error(" Defined type index {} is not a function type."sv, |
518 | 2 | TagTypeIdx); |
519 | 2 | return Unexpect(ErrCode::Value::InvalidTagIdx); |
520 | 2 | } |
521 | 6 | if (!CompType.getFuncType().getReturnTypes().empty()) { |
522 | 1 | spdlog::error(ErrCode::Value::InvalidTagResultType); |
523 | 1 | return Unexpect(ErrCode::Value::InvalidTagResultType); |
524 | 1 | } |
525 | 5 | Checker.addTag(TagTypeIdx); |
526 | 5 | return {}; |
527 | 6 | } |
528 | 54 | case ExternalType::Global: { |
529 | 54 | const auto &GlobType = ImpDesc.getExternalGlobalType(); |
530 | | // Global type must be valid. |
531 | 54 | EXPECTED_TRY(validate(GlobType).map_error([](auto E) { |
532 | 53 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Global)); |
533 | 53 | return E; |
534 | 53 | })); |
535 | 53 | Checker.addGlobal(GlobType, true); |
536 | 53 | return {}; |
537 | 54 | } |
538 | 0 | default: |
539 | 0 | return {}; |
540 | 486 | } |
541 | 486 | } |
542 | | |
543 | | // Validate Export description. See "include/validator/validator.h". |
544 | 11.3k | Expect<void> Validator::validate(const AST::ExportDesc &ExpDesc) { |
545 | 11.3k | auto Id = ExpDesc.getExternalIndex(); |
546 | 11.3k | switch (ExpDesc.getExternalType()) { |
547 | 11.2k | case ExternalType::Function: |
548 | 11.2k | if (Id >= Checker.getFunctions().size()) { |
549 | 17 | spdlog::error(ErrCode::Value::InvalidFuncIdx); |
550 | 17 | spdlog::error(ErrInfo::InfoForbidIndex( |
551 | 17 | ErrInfo::IndexCategory::Function, Id, |
552 | 17 | static_cast<uint32_t>(Checker.getFunctions().size()))); |
553 | 17 | return Unexpect(ErrCode::Value::InvalidFuncIdx); |
554 | 17 | } |
555 | 11.1k | Checker.addRef(Id); |
556 | 11.1k | return {}; |
557 | 30 | case ExternalType::Table: |
558 | 30 | if (Id >= Checker.getTables().size()) { |
559 | 6 | spdlog::error(ErrCode::Value::InvalidTableIdx); |
560 | 6 | spdlog::error(ErrInfo::InfoForbidIndex( |
561 | 6 | ErrInfo::IndexCategory::Table, Id, |
562 | 6 | static_cast<uint32_t>(Checker.getTables().size()))); |
563 | 6 | return Unexpect(ErrCode::Value::InvalidTableIdx); |
564 | 6 | } |
565 | 24 | return {}; |
566 | 100 | case ExternalType::Memory: |
567 | 100 | if (Id >= Checker.getMemories()) { |
568 | 16 | spdlog::error(ErrCode::Value::InvalidMemoryIdx); |
569 | 16 | spdlog::error(ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::Memory, Id, |
570 | 16 | Checker.getMemories())); |
571 | 16 | return Unexpect(ErrCode::Value::InvalidMemoryIdx); |
572 | 16 | } |
573 | 84 | return {}; |
574 | 12 | case ExternalType::Tag: |
575 | 12 | if (Id >= Checker.getTags().size()) { |
576 | 1 | spdlog::error(ErrCode::Value::InvalidTagIdx); |
577 | 1 | spdlog::error(ErrInfo::InfoForbidIndex( |
578 | 1 | ErrInfo::IndexCategory::Tag, Id, |
579 | 1 | static_cast<uint32_t>(Checker.getTags().size()))); |
580 | 1 | return Unexpect(ErrCode::Value::InvalidTagIdx); |
581 | 1 | } |
582 | 11 | return {}; |
583 | 35 | case ExternalType::Global: |
584 | 35 | if (Id >= Checker.getGlobals().size()) { |
585 | 5 | spdlog::error(ErrCode::Value::InvalidGlobalIdx); |
586 | 5 | spdlog::error(ErrInfo::InfoForbidIndex( |
587 | 5 | ErrInfo::IndexCategory::Global, Id, |
588 | 5 | static_cast<uint32_t>(Checker.getGlobals().size()))); |
589 | 5 | return Unexpect(ErrCode::Value::InvalidGlobalIdx); |
590 | 5 | } |
591 | 30 | return {}; |
592 | 0 | default: |
593 | 0 | return {}; |
594 | 11.3k | } |
595 | 11.3k | } |
596 | | |
597 | 4.32k | Expect<void> Validator::validate(const AST::TypeSection &TypeSec) { |
598 | 4.32k | const auto STypeList = TypeSec.getContent(); |
599 | 4.32k | uint32_t Idx = 0; |
600 | 12.0k | while (Idx < STypeList.size()) { |
601 | 7.72k | const auto &SType = STypeList[Idx]; |
602 | 7.72k | if (SType.getRecursiveInfo().has_value()) { |
603 | | // Recursive type case. Add types first for referring recursively. |
604 | 4 | uint32_t RecSize = SType.getRecursiveInfo()->RecTypeSize; |
605 | 9 | for (uint32_t I = Idx; I < Idx + RecSize; I++) { |
606 | 5 | Checker.addType(STypeList[I]); |
607 | 5 | } |
608 | 8 | for (uint32_t I = Idx; I < Idx + RecSize; I++) { |
609 | 5 | EXPECTED_TRY(validate(STypeList[I]).map_error([](auto E) { |
610 | 5 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Rec)); |
611 | 5 | return E; |
612 | 5 | })); |
613 | 5 | } |
614 | 3 | Idx += RecSize; |
615 | 7.71k | } else { |
616 | | // SubType case. |
617 | 7.71k | if (Conf.hasProposal(Proposal::GC)) { |
618 | | // For the GC proposal, the subtype is seemed as a self-recursive type. |
619 | | // Add types first for referring recursively. |
620 | 7.71k | Checker.addType(SType); |
621 | 7.71k | EXPECTED_TRY(validate(*Checker.getTypes().back())); |
622 | 7.71k | } else { |
623 | | // Validating first. |
624 | 0 | EXPECTED_TRY(validate(SType)); |
625 | 0 | Checker.addType(SType); |
626 | 0 | } |
627 | 7.71k | Idx++; |
628 | 7.71k | } |
629 | 7.72k | } |
630 | 4.31k | return {}; |
631 | 4.32k | } |
632 | | |
633 | | // Validate Import section. See "include/validator/validator.h". |
634 | 4.31k | Expect<void> Validator::validate(const AST::ImportSection &ImportSec) { |
635 | 4.31k | for (auto &ImportDesc : ImportSec.getContent()) { |
636 | 486 | EXPECTED_TRY(validate(ImportDesc).map_error([](auto E) { |
637 | 486 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Import)); |
638 | 486 | return E; |
639 | 486 | })); |
640 | 486 | } |
641 | 4.29k | return {}; |
642 | 4.31k | } |
643 | | |
644 | | // Validate Function section. See "include/validator/validator.h". |
645 | 4.29k | Expect<void> Validator::validate(const AST::FunctionSection &FuncSec) { |
646 | 4.29k | const auto &FuncVec = FuncSec.getContent(); |
647 | 4.29k | const auto &TypeVec = Checker.getTypes(); |
648 | | |
649 | | // Check if type id of function is valid in context. |
650 | 18.0k | for (auto &TId : FuncVec) { |
651 | 18.0k | if (TId >= TypeVec.size()) { |
652 | 11 | spdlog::error(ErrCode::Value::InvalidFuncTypeIdx); |
653 | 11 | spdlog::error( |
654 | 11 | ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::FunctionType, TId, |
655 | 11 | static_cast<uint32_t>(TypeVec.size()))); |
656 | 11 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function)); |
657 | 11 | return Unexpect(ErrCode::Value::InvalidFuncTypeIdx); |
658 | 11 | } |
659 | 18.0k | if (!TypeVec[TId]->getCompositeType().isFunc()) { |
660 | 3 | spdlog::error(ErrCode::Value::InvalidFuncTypeIdx); |
661 | 3 | spdlog::error(" Defined type index {} is not a function type."sv, TId); |
662 | 3 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function)); |
663 | 3 | return Unexpect(ErrCode::Value::InvalidFuncTypeIdx); |
664 | 3 | } |
665 | 18.0k | Checker.addFunc(TId); |
666 | 18.0k | } |
667 | 4.28k | return {}; |
668 | 4.29k | } |
669 | | |
670 | | // Validate Table section. See "include/validator/validator.h". |
671 | 4.28k | Expect<void> Validator::validate(const AST::TableSection &TabSec) { |
672 | 4.28k | for (auto &Tab : TabSec.getContent()) { |
673 | 442 | EXPECTED_TRY(validate(Tab).map_error([](auto E) { |
674 | 431 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Table)); |
675 | 431 | return E; |
676 | 431 | })); |
677 | 431 | Checker.addTable(Tab.getTableType()); |
678 | 431 | } |
679 | 4.27k | return {}; |
680 | 4.28k | } |
681 | | |
682 | | // Validate Memory section. See "include/validator/validator.h". |
683 | 4.27k | Expect<void> Validator::validate(const AST::MemorySection &MemSec) { |
684 | 4.27k | for (auto &Mem : MemSec.getContent()) { |
685 | 1.43k | EXPECTED_TRY(validate(Mem).map_error([](auto E) { |
686 | 1.40k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Memory)); |
687 | 1.40k | return E; |
688 | 1.40k | })); |
689 | 1.40k | Checker.addMemory(Mem); |
690 | 1.40k | } |
691 | 4.24k | return {}; |
692 | 4.27k | } |
693 | | |
694 | | // Validate Global section. See "include/validator/validator.h". |
695 | 4.24k | Expect<void> Validator::validate(const AST::GlobalSection &GlobSec) { |
696 | 4.24k | for (auto &GlobSeg : GlobSec.getContent()) { |
697 | 348 | EXPECTED_TRY(validate(GlobSeg).map_error([](auto E) { |
698 | 229 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Global)); |
699 | 229 | return E; |
700 | 229 | })); |
701 | 229 | Checker.addGlobal(GlobSeg.getGlobalType()); |
702 | 229 | } |
703 | 4.12k | return {}; |
704 | 4.24k | } |
705 | | |
706 | | // Validate Element section. See "include/validator/validator.h". |
707 | 4.04k | Expect<void> Validator::validate(const AST::ElementSection &ElemSec) { |
708 | 4.04k | for (auto &ElemSeg : ElemSec.getContent()) { |
709 | 792 | EXPECTED_TRY(validate(ElemSeg).map_error([](auto E) { |
710 | 740 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Element)); |
711 | 740 | return E; |
712 | 740 | })); |
713 | 740 | Checker.addElem(ElemSeg); |
714 | 740 | } |
715 | 3.99k | return {}; |
716 | 4.04k | } |
717 | | |
718 | | // Validate Code section. See "include/validator/validator.h". |
719 | 3.97k | Expect<void> Validator::validate(const AST::CodeSection &CodeSec) { |
720 | 3.97k | const auto &CodeVec = CodeSec.getContent(); |
721 | 3.97k | const auto &FuncVec = Checker.getFunctions(); |
722 | | |
723 | | // Validate function body. |
724 | 16.2k | for (uint32_t Id = 0; Id < static_cast<uint32_t>(CodeVec.size()); ++Id) { |
725 | | // Added functions contains imported functions. |
726 | 13.8k | uint32_t TId = Id + static_cast<uint32_t>(Checker.getNumImportFuncs()); |
727 | 13.8k | if (TId >= static_cast<uint32_t>(FuncVec.size())) { |
728 | 0 | spdlog::error(ErrCode::Value::InvalidFuncIdx); |
729 | 0 | spdlog::error( |
730 | 0 | ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::Function, TId, |
731 | 0 | static_cast<uint32_t>(FuncVec.size()))); |
732 | 0 | return Unexpect(ErrCode::Value::InvalidFuncIdx); |
733 | 0 | } |
734 | 13.8k | EXPECTED_TRY(validate(CodeVec[Id], FuncVec[TId]).map_error([](auto E) { |
735 | 13.8k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Code)); |
736 | 13.8k | return E; |
737 | 13.8k | })); |
738 | 13.8k | } |
739 | 2.33k | return {}; |
740 | 3.97k | } |
741 | | |
742 | | // Validate Data section. See "include/validator/validator.h". |
743 | 3.99k | Expect<void> Validator::validate(const AST::DataSection &DataSec) { |
744 | 3.99k | for (auto &DataSeg : DataSec.getContent()) { |
745 | 338 | EXPECTED_TRY(validate(DataSeg).map_error([](auto E) { |
746 | 311 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Data)); |
747 | 311 | return E; |
748 | 311 | })); |
749 | 311 | Checker.addData(DataSeg); |
750 | 311 | } |
751 | 3.97k | return {}; |
752 | 3.99k | } |
753 | | |
754 | | // Validate Start section. See "include/validator/validator.h". |
755 | 4.05k | Expect<void> Validator::validate(const AST::StartSection &StartSec) { |
756 | 4.05k | if (StartSec.getContent()) { |
757 | 13 | auto FId = *StartSec.getContent(); |
758 | 13 | if (FId >= Checker.getFunctions().size()) { |
759 | 7 | spdlog::error(ErrCode::Value::InvalidFuncIdx); |
760 | 7 | spdlog::error(ErrInfo::InfoForbidIndex( |
761 | 7 | ErrInfo::IndexCategory::Function, FId, |
762 | 7 | static_cast<uint32_t>(Checker.getFunctions().size()))); |
763 | 7 | return Unexpect(ErrCode::Value::InvalidFuncIdx); |
764 | 7 | } |
765 | 6 | auto TId = Checker.getFunctions()[FId]; |
766 | 6 | assuming(TId < Checker.getTypes().size()); |
767 | 6 | if (!Checker.getTypes()[TId]->getCompositeType().isFunc()) { |
768 | 0 | spdlog::error(ErrCode::Value::InvalidStartFunc); |
769 | 0 | spdlog::error(" Defined type index {} is not a function type."sv, TId); |
770 | 0 | return Unexpect(ErrCode::Value::InvalidStartFunc); |
771 | 0 | } |
772 | 6 | auto &Type = Checker.getTypes()[TId]->getCompositeType().getFuncType(); |
773 | 6 | if (Type.getParamTypes().size() != 0 || Type.getReturnTypes().size() != 0) { |
774 | | // Start function signature should be {}->{} |
775 | 2 | spdlog::error(ErrCode::Value::InvalidStartFunc); |
776 | 2 | spdlog::error(ErrInfo::InfoMismatch({}, {}, Type.getParamTypes(), |
777 | 2 | Type.getReturnTypes())); |
778 | 2 | return Unexpect(ErrCode::Value::InvalidStartFunc); |
779 | 2 | } |
780 | 6 | } |
781 | 4.04k | return {}; |
782 | 4.05k | } |
783 | | |
784 | | // Validate Export section. See "include/validator/validator.h". |
785 | 4.10k | Expect<void> Validator::validate(const AST::ExportSection &ExportSec) { |
786 | 4.10k | std::unordered_set<std::string_view, Hash::Hash> ExportNames; |
787 | 11.3k | for (auto &ExportDesc : ExportSec.getContent()) { |
788 | 11.3k | auto Result = ExportNames.emplace(ExportDesc.getExternalName()); |
789 | 11.3k | if (!Result.second) { |
790 | | // Duplicated export name. |
791 | 6 | spdlog::error(ErrCode::Value::DupExportName); |
792 | 6 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Export)); |
793 | 6 | return Unexpect(ErrCode::Value::DupExportName); |
794 | 6 | } |
795 | 11.3k | EXPECTED_TRY(validate(ExportDesc).map_error([](auto E) { |
796 | 11.3k | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Export)); |
797 | 11.3k | return E; |
798 | 11.3k | })); |
799 | 11.3k | } |
800 | 4.05k | return {}; |
801 | 4.10k | } |
802 | | |
803 | | // Validate Tag section. See "include/validator/validator.h". |
804 | 4.12k | Expect<void> Validator::validate(const AST::TagSection &TagSec) { |
805 | 4.12k | const auto &TagVec = TagSec.getContent(); |
806 | 4.12k | const auto &TypeVec = Checker.getTypes(); |
807 | | |
808 | | // Check if type id of tag is valid in context. |
809 | 4.12k | for (auto &TagType : TagVec) { |
810 | 53 | auto TagTypeIdx = TagType.getTypeIdx(); |
811 | 53 | if (TagTypeIdx >= TypeVec.size()) { |
812 | 8 | spdlog::error(ErrCode::Value::InvalidTagIdx); |
813 | 8 | spdlog::error( |
814 | 8 | ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::TagType, TagTypeIdx, |
815 | 8 | static_cast<uint32_t>(TypeVec.size()))); |
816 | 8 | return Unexpect(ErrCode::Value::InvalidTagIdx); |
817 | 8 | } |
818 | 45 | auto &CompType = TypeVec[TagTypeIdx]->getCompositeType(); |
819 | 45 | if (!CompType.isFunc()) { |
820 | 3 | spdlog::error(ErrCode::Value::InvalidTagIdx); |
821 | 3 | spdlog::error(" Defined type index {} is not a function type."sv, |
822 | 3 | TagTypeIdx); |
823 | 3 | return Unexpect(ErrCode::Value::InvalidTagIdx); |
824 | 3 | } |
825 | 42 | if (!CompType.getFuncType().getReturnTypes().empty()) { |
826 | 1 | spdlog::error(ErrCode::Value::InvalidTagResultType); |
827 | 1 | return Unexpect(ErrCode::Value::InvalidTagResultType); |
828 | 1 | } |
829 | 41 | Checker.addTag(TagTypeIdx); |
830 | 41 | } |
831 | 4.10k | return {}; |
832 | 4.12k | } |
833 | | |
834 | | // Validate constant expression. See "include/validator/validator.h". |
835 | | Expect<void> Validator::validateConstExpr(AST::InstrView Instrs, |
836 | 2.69k | Span<const ValType> Returns) { |
837 | 6.80k | for (auto &Instr : Instrs) { |
838 | | // Only these instructions are accepted. |
839 | 6.80k | switch (Instr.getOpCode()) { |
840 | 25 | case OpCode::Global__get: { |
841 | | // For initialization case, global indices must be imported globals. |
842 | 25 | auto GlobIdx = Instr.getTargetIndex(); |
843 | 25 | uint32_t ValidGlobalSize = Checker.getNumImportGlobals(); |
844 | 25 | if (Conf.hasProposal(Proposal::FunctionReferences)) { |
845 | 25 | ValidGlobalSize = static_cast<uint32_t>(Checker.getGlobals().size()); |
846 | 25 | } |
847 | 25 | if (GlobIdx >= ValidGlobalSize) { |
848 | 8 | spdlog::error(ErrCode::Value::InvalidGlobalIdx); |
849 | 8 | spdlog::error(ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::Global, |
850 | 8 | GlobIdx, ValidGlobalSize)); |
851 | 8 | spdlog::error( |
852 | 8 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
853 | 8 | return Unexpect(ErrCode::Value::InvalidGlobalIdx); |
854 | 8 | } |
855 | 17 | if (Checker.getGlobals()[GlobIdx].second != ValMut::Const) { |
856 | 3 | spdlog::error(ErrCode::Value::ConstExprRequired); |
857 | 3 | spdlog::error( |
858 | 3 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
859 | 3 | return Unexpect(ErrCode::Value::ConstExprRequired); |
860 | 3 | } |
861 | 14 | break; |
862 | 17 | } |
863 | 1.64k | case OpCode::Ref__func: { |
864 | | // When in const expression, add the reference into context. |
865 | 1.64k | auto FuncIdx = Instr.getTargetIndex(); |
866 | 1.64k | if (FuncIdx >= Checker.getFunctions().size()) { |
867 | | // Function index out of range. |
868 | 20 | spdlog::error(ErrCode::Value::InvalidFuncIdx); |
869 | 20 | spdlog::error(ErrInfo::InfoForbidIndex( |
870 | 20 | ErrInfo::IndexCategory::Function, FuncIdx, |
871 | 20 | static_cast<uint32_t>(Checker.getFunctions().size()))); |
872 | 20 | spdlog::error( |
873 | 20 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
874 | 20 | return Unexpect(ErrCode::Value::InvalidFuncIdx); |
875 | 20 | } |
876 | 1.62k | Checker.addRef(Instr.getTargetIndex()); |
877 | 1.62k | break; |
878 | 1.64k | } |
879 | 1.28k | case OpCode::I32__const: |
880 | 1.38k | case OpCode::I64__const: |
881 | 1.44k | case OpCode::F32__const: |
882 | 1.48k | case OpCode::F64__const: |
883 | 1.64k | case OpCode::Ref__null: |
884 | 1.66k | case OpCode::V128__const: |
885 | 4.25k | case OpCode::End: |
886 | 4.27k | case OpCode::Struct__new: |
887 | 4.28k | case OpCode::Struct__new_default: |
888 | 4.29k | case OpCode::Array__new: |
889 | 4.30k | case OpCode::Array__new_default: |
890 | 4.31k | case OpCode::Array__new_fixed: |
891 | 4.32k | case OpCode::Any__convert_extern: |
892 | 4.33k | case OpCode::Extern__convert_any: |
893 | 4.40k | case OpCode::Ref__i31: |
894 | 4.40k | break; |
895 | | |
896 | | // For the Extended-const proposal, these instructions are accepted. |
897 | 92 | case OpCode::I32__add: |
898 | 162 | case OpCode::I32__sub: |
899 | 246 | case OpCode::I32__mul: |
900 | 401 | case OpCode::I64__add: |
901 | 527 | case OpCode::I64__sub: |
902 | 659 | case OpCode::I64__mul: |
903 | 659 | if (Conf.hasProposal(Proposal::ExtendedConst)) { |
904 | 659 | break; |
905 | 659 | } |
906 | 0 | spdlog::error(ErrCode::Value::ConstExprRequired); |
907 | 0 | spdlog::error(ErrInfo::InfoProposal(Proposal::ExtendedConst)); |
908 | 0 | spdlog::error( |
909 | 0 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
910 | 0 | return Unexpect(ErrCode::Value::ConstExprRequired); |
911 | | |
912 | 75 | default: |
913 | 75 | spdlog::error(ErrCode::Value::ConstExprRequired); |
914 | 75 | spdlog::error( |
915 | 75 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
916 | 75 | return Unexpect(ErrCode::Value::ConstExprRequired); |
917 | 6.80k | } |
918 | 6.80k | } |
919 | | // Validate expression with result types. |
920 | 2.58k | Checker.reset(); |
921 | 2.58k | return Checker.validate(Instrs, Returns); |
922 | 2.69k | } |
923 | | |
924 | | } // namespace Validator |
925 | | } // namespace WasmEdge |