Coverage Report

Created: 2025-11-09 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
0
    const FormChecker &Checker, uint32_t TypeIdx) {
26
0
  if (VisitedSet.count(Index)) {
27
0
    spdlog::error(ErrCode::Value::InvalidSubType);
28
0
    spdlog::error("    Cycle detected in subtype hierarchy for type {}."sv,
29
0
                  Index);
30
0
    return Unexpect(ErrCode::Value::InvalidSubType);
31
0
  }
32
33
0
  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
0
  VisitedSet.insert(Index);
43
0
  const auto &TypeVec = Checker.getTypes();
44
0
  const auto &Type = *TypeVec[Index];
45
0
  for (const auto SuperIdx : Type.getSuperTypeIndices()) {
46
0
    EXPECTED_TRY(calculateSubtypeDepthRecursiveHelper(
47
0
                     SuperIdx, Depth + 1, VisitedSet, Checker, TypeIdx)
48
0
                     .map_error([&](auto E) {
49
0
                       spdlog::error("    When checking super type index {}."sv,
50
0
                                     SuperIdx);
51
0
                       return E;
52
0
                     }));
53
0
  }
54
0
  return {};
55
0
}
56
57
Expect<void> calculateSubtypeDepth(uint32_t TypeIdx,
58
0
                                   const FormChecker &Checker) {
59
0
  std::unordered_set<uint32_t> VisitedNodes;
60
0
  return calculateSubtypeDepthRecursiveHelper(TypeIdx, 0, VisitedNodes, Checker,
61
0
                                              TypeIdx);
62
0
}
63
64
} // namespace
65
66
// Validate Module. See "include/validator/validator.h".
67
4.20k
Expect<void> Validator::validate(const AST::Module &Mod) {
68
  // https://webassembly.github.io/spec/core/valid/modules.html
69
4.20k
  Checker.reset(true);
70
71
  // Validate and register type section.
72
4.20k
  EXPECTED_TRY(validate(Mod.getTypeSection()).map_error([](auto E) {
73
4.20k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Type));
74
4.20k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
75
4.20k
    return E;
76
4.20k
  }));
77
78
  // Validate and register import section into FormChecker.
79
4.20k
  EXPECTED_TRY(validate(Mod.getImportSection()).map_error([](auto E) {
80
4.18k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Import));
81
4.18k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
82
4.18k
    return E;
83
4.18k
  }));
84
85
  // Validate function section and register functions into FormChecker.
86
4.18k
  EXPECTED_TRY(validate(Mod.getFunctionSection()).map_error([](auto E) {
87
4.17k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Function));
88
4.17k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
89
4.17k
    return E;
90
4.17k
  }));
91
92
  // Validate table section and register tables into FormChecker.
93
4.17k
  EXPECTED_TRY(validate(Mod.getTableSection()).map_error([](auto E) {
94
4.15k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Table));
95
4.15k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
96
4.15k
    return E;
97
4.15k
  }));
98
99
  // Validate memory section and register memories into FormChecker.
100
4.15k
  EXPECTED_TRY(validate(Mod.getMemorySection()).map_error([](auto E) {
101
4.12k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Memory));
102
4.12k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
103
4.12k
    return E;
104
4.12k
  }));
105
106
  // Validate global section and register globals into FormChecker.
107
4.12k
  EXPECTED_TRY(validate(Mod.getGlobalSection()).map_error([](auto E) {
108
3.99k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Global));
109
3.99k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
110
3.99k
    return E;
111
3.99k
  }));
112
113
  // Validate tag section and register tags into FormChecker.
114
3.99k
  EXPECTED_TRY(validate(Mod.getTagSection()).map_error([](auto E) {
115
3.97k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Tag));
116
3.97k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
117
3.97k
    return E;
118
3.97k
  }));
119
120
  // Validate export section.
121
3.97k
  EXPECTED_TRY(validate(Mod.getExportSection()).map_error([](auto E) {
122
3.92k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Export));
123
3.92k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
124
3.92k
    return E;
125
3.92k
  }));
126
127
  // Validate start section.
128
3.92k
  EXPECTED_TRY(validate(Mod.getStartSection()).map_error([](auto E) {
129
3.91k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Start));
130
3.91k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
131
3.91k
    return E;
132
3.91k
  }));
133
134
  // Validate element section which initialize tables.
135
3.91k
  EXPECTED_TRY(validate(Mod.getElementSection()).map_error([](auto E) {
136
3.85k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Element));
137
3.85k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
138
3.85k
    return E;
139
3.85k
  }));
140
141
  // Validate data section which initialize memories.
142
3.85k
  EXPECTED_TRY(validate(Mod.getDataSection()).map_error([](auto E) {
143
3.82k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Data));
144
3.82k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
145
3.82k
    return E;
146
3.82k
  }));
147
148
  // Validate code section and expressions.
149
3.82k
  EXPECTED_TRY(validate(Mod.getCodeSection()).map_error([](auto E) {
150
2.19k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Code));
151
2.19k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
152
2.19k
    return E;
153
2.19k
  }));
154
155
  // Multiple tables is for the ReferenceTypes proposal.
156
2.19k
  if (Checker.getTables().size() > 1 &&
157
50
      !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.19k
  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.19k
  const_cast<AST::Module &>(Mod).setIsValidated();
174
2.19k
  return {};
175
2.19k
}
176
177
// Validate Sub type. See "include/validator/validator.h".
178
7.21k
Expect<void> Validator::validate(const AST::SubType &Type) {
179
7.21k
  const auto &TypeVec = Checker.getTypes();
180
7.21k
  const auto &CompType = Type.getCompositeType();
181
182
  // Check the validation of the composite type.
183
7.21k
  if (CompType.isFunc()) {
184
7.19k
    const auto &FType = CompType.getFuncType();
185
7.19k
    for (auto &PType : FType.getParamTypes()) {
186
6.51k
      EXPECTED_TRY(Checker.validate(PType).map_error([](auto E) {
187
6.51k
        spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function));
188
6.51k
        return E;
189
6.51k
      }));
190
6.51k
    }
191
7.19k
    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.19k
    for (auto &RType : FType.getReturnTypes()) {
199
5.36k
      EXPECTED_TRY(Checker.validate(RType).map_error([](auto E) {
200
5.36k
        spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function));
201
5.36k
        return E;
202
5.36k
      }));
203
5.36k
    }
204
7.19k
  } else {
205
23
    const auto &FTypes = CompType.getFieldTypes();
206
23
    for (auto &FieldType : FTypes) {
207
22
      EXPECTED_TRY(Checker.validate(FieldType.getStorageType()));
208
22
    }
209
23
  }
210
211
  // In current version, the length of type index vector will be <= 1.
212
7.21k
  if (Type.getSuperTypeIndices().size() > 1) {
213
0
    spdlog::error(ErrCode::Value::InvalidSubType);
214
0
    spdlog::error("    Accepts only one super type currently."sv);
215
0
    return Unexpect(ErrCode::Value::InvalidSubType);
216
0
  }
217
218
7.21k
  for (const auto &Index : Type.getSuperTypeIndices()) {
219
0
    if (unlikely(Index >= TypeVec.size())) {
220
0
      spdlog::error(ErrCode::Value::InvalidSubType);
221
0
      spdlog::error(
222
0
          ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::DefinedType, Index,
223
0
                                   static_cast<uint32_t>(TypeVec.size())));
224
0
      return Unexpect(ErrCode::Value::InvalidSubType);
225
0
    }
226
227
0
    if (auto Res = calculateSubtypeDepth(Index, Checker); !Res) {
228
0
      spdlog::error("    When checking subtype hierarchy of super type {}."sv,
229
0
                    Index);
230
0
      return Unexpect(Res.error());
231
0
    }
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.21k
  return {};
246
7.21k
}
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
15
    spdlog::error(ErrCode::Value::InvalidLimit);
252
15
    spdlog::error(ErrInfo::InfoLimit(Lim.hasMax(), Lim.getMin(), Lim.getMax()));
253
15
    return Unexpect(ErrCode::Value::InvalidLimit);
254
15
  }
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
481
Expect<void> Validator::validate(const AST::TableType &Tab) {
264
  // Validate value type.
265
481
  EXPECTED_TRY(Checker.validate(Tab.getRefType()));
266
  // Validate table limits.
267
476
  return validate(Tab.getLimit()).map_error([](auto E) {
268
4
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Limit));
269
4
    return E;
270
4
  });
271
481
}
272
273
// Validate Memory type. See "include/validator/validator.h".
274
1.48k
Expect<void> Validator::validate(const AST::MemoryType &Mem) {
275
  // Validate memory limits.
276
1.48k
  const auto &Lim = Mem.getLimit();
277
1.48k
  EXPECTED_TRY(validate(Lim).map_error([](auto E) {
278
1.47k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Limit));
279
1.47k
    return E;
280
1.47k
  }));
281
1.47k
  if (Lim.getMin() > LIMIT_MEMORYTYPE ||
282
1.46k
      (Lim.hasMax() && Lim.getMax() > LIMIT_MEMORYTYPE)) {
283
    // TODO: MEMORY64 - fully support implementation.
284
23
    ErrCode::Value FailCode = Conf.hasProposal(Proposal::Memory64)
285
23
                                  ? ErrCode::Value::InvalidMemPages64
286
23
                                  : ErrCode::Value::InvalidMemPages;
287
23
    spdlog::error(FailCode);
288
23
    spdlog::error(ErrInfo::InfoLimit(Lim.hasMax(), Lim.getMin(), Lim.getMax()));
289
23
    return Unexpect(FailCode);
290
23
  }
291
1.44k
  return {};
292
1.47k
}
293
294
// Validate Global type. See "include/validator/validator.h".
295
281
Expect<void> Validator::validate(const AST::GlobalType &Glob) {
296
  // Validate value type.
297
281
  return Checker.validate(Glob.getValType());
298
281
}
299
300
// Validate Table segment. See "include/validator/validator.h".
301
445
Expect<void> Validator::validate(const AST::TableSegment &TabSeg) {
302
445
  if (TabSeg.getExpr().getInstrs().size() > 0) {
303
    // Check ref initialization is a const expression.
304
16
    EXPECTED_TRY(
305
16
        validateConstExpr(TabSeg.getExpr().getInstrs(),
306
16
                          {ValType(TabSeg.getTableType().getRefType())})
307
16
            .map_error([](auto E) {
308
16
              spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
309
16
              return E;
310
16
            }));
311
429
  } else {
312
    // No init expression. Check the reference type is nullable.
313
429
    if (!TabSeg.getTableType().getRefType().isNullableRefType()) {
314
2
      spdlog::error(ErrCode::Value::TypeCheckFailed);
315
2
      spdlog::error(ErrInfo::InfoMismatch(
316
2
          ValType(TypeCode::RefNull,
317
2
                  TabSeg.getTableType().getRefType().getHeapTypeCode(),
318
2
                  TabSeg.getTableType().getRefType().getTypeIndex()),
319
2
          TabSeg.getTableType().getRefType()));
320
2
      return Unexpect(ErrCode::Value::TypeCheckFailed);
321
2
    }
322
429
  }
323
  // Validate table type.
324
435
  return validate(TabSeg.getTableType()).map_error([](auto E) {
325
7
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Table));
326
7
    return E;
327
7
  });
328
445
}
329
330
// Validate Global segment. See "include/validator/validator.h".
331
355
Expect<void> Validator::validate(const AST::GlobalSegment &GlobSeg) {
332
  // Check global initialization is a const expression.
333
355
  EXPECTED_TRY(validateConstExpr(GlobSeg.getExpr().getInstrs(),
334
225
                                 {GlobSeg.getGlobalType().getValType()})
335
225
                   .map_error([](auto E) {
336
225
                     spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
337
225
                     return E;
338
225
                   }));
339
  // Validate global type.
340
225
  return validate(GlobSeg.getGlobalType()).map_error([](auto E) {
341
0
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Global));
342
0
    return E;
343
0
  });
344
355
}
345
346
// Validate Element segment. See "include/validator/validator.h".
347
627
Expect<void> Validator::validate(const AST::ElementSegment &ElemSeg) {
348
  // Check initialization expressions are const expressions.
349
1.65k
  for (auto &Expr : ElemSeg.getInitExprs()) {
350
1.65k
    EXPECTED_TRY(
351
1.65k
        validateConstExpr(Expr.getInstrs(), {ValType(ElemSeg.getRefType())})
352
1.65k
            .map_error([](auto E) {
353
1.65k
              spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
354
1.65k
              return E;
355
1.65k
            }));
356
1.65k
  }
357
358
  // The reference type should be valid.
359
588
  EXPECTED_TRY(Checker.validate(ElemSeg.getRefType()));
360
361
  // Passive and declarative cases are valid with the valid reference type.
362
586
  if (ElemSeg.getMode() == AST::ElementSegment::ElemMode::Active) {
363
    // Check table index and reference type in context.
364
349
    const auto &TableVec = Checker.getTables();
365
349
    if (ElemSeg.getIdx() >= TableVec.size()) {
366
12
      spdlog::error(ErrCode::Value::InvalidTableIdx);
367
12
      spdlog::error(ErrInfo::InfoForbidIndex(
368
12
          ErrInfo::IndexCategory::Table, ElemSeg.getIdx(),
369
12
          static_cast<uint32_t>(TableVec.size())));
370
12
      return Unexpect(ErrCode::Value::InvalidTableIdx);
371
12
    }
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
337
    if (TableVec[ElemSeg.getIdx()].isFuncRefType() !=
379
337
            ElemSeg.getRefType().isFuncRefType() ||
380
335
        (!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
335
    return validateConstExpr(ElemSeg.getExpr().getInstrs(),
390
335
                             {ValType(TypeCode::I32)})
391
335
        .map_error([](auto E) {
392
9
          spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
393
9
          return E;
394
9
        });
395
337
  }
396
237
  return {};
397
586
}
398
399
// Validate Code segment. See "include/validator/validator.h".
400
Expect<void> Validator::validate(const AST::CodeSegment &CodeSeg,
401
14.0k
                                 const uint32_t TypeIdx) {
402
  // Due to the validation of the function section, the type of index bust be a
403
  // function type.
404
14.0k
  const auto &FuncType =
405
14.0k
      Checker.getTypes()[TypeIdx]->getCompositeType().getFuncType();
406
  // Reset stack in FormChecker.
407
14.0k
  Checker.reset();
408
  // Add parameters into this frame.
409
14.0k
  for (auto &Type : FuncType.getParamTypes()) {
410
    // Local passed by function parameters must have been initialized.
411
11.0k
    Checker.addLocal(Type, true);
412
11.0k
  }
413
  // Add locals into this frame.
414
14.0k
  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.40M
      EXPECTED_TRY(Checker.validate(Val.second));
418
8.40M
      Checker.addLocal(Val.second, false);
419
8.40M
    }
420
2.40k
  }
421
  // Validate function body expression.
422
14.0k
  return Checker
423
14.0k
      .validate(CodeSeg.getExpr().getInstrs(), FuncType.getReturnTypes())
424
14.0k
      .map_error([](auto E) {
425
1.62k
        spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
426
1.62k
        return E;
427
1.62k
      });
428
14.0k
}
429
430
// Validate Data segment. See "include/validator/validator.h".
431
352
Expect<void> Validator::validate(const AST::DataSegment &DataSeg) {
432
352
  switch (DataSeg.getMode()) {
433
191
  case AST::DataSegment::DataMode::Active: {
434
    // Check memory index in context.
435
191
    const auto &MemNum = Checker.getMemories();
436
191
    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
170
    return validateConstExpr(DataSeg.getExpr().getInstrs(),
444
170
                             {ValType(TypeCode::I32)})
445
170
        .map_error([](auto E) {
446
5
          spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
447
5
          return E;
448
5
        });
449
191
  }
450
161
  case AST::DataSegment::DataMode::Passive:
451
    // Passive case is always valid.
452
161
    return {};
453
0
  default:
454
0
    return {};
455
352
  }
456
352
}
457
458
// Validate Import description. See "include/validator/validator.h".
459
469
Expect<void> Validator::validate(const AST::ImportDesc &ImpDesc) {
460
469
  switch (ImpDesc.getExternalType()) {
461
  // External type and the external content are ensured to be matched in
462
  // loader phase.
463
319
  case ExternalType::Function: {
464
319
    const auto TId = ImpDesc.getExternalFuncTypeIdx();
465
    // Function type index must exist in context and be valid.
466
319
    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
315
    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
314
    Checker.addRef(static_cast<uint32_t>(Checker.getFunctions().size()));
479
314
    Checker.addFunc(TId, true);
480
314
    return {};
481
315
  }
482
46
  case ExternalType::Table: {
483
46
    const auto &TabType = ImpDesc.getExternalTableType();
484
    // Table type must be valid.
485
46
    EXPECTED_TRY(validate(TabType).map_error([](auto E) {
486
44
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Table));
487
44
      return E;
488
44
    }));
489
44
    Checker.addTable(TabType);
490
44
    return {};
491
46
  }
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
11
  case ExternalType::Tag: {
503
11
    const auto &T = ImpDesc.getExternalTagType();
504
    // Tag type index must exist in context.
505
11
    auto TagTypeIdx = T.getTypeIdx();
506
11
    if (TagTypeIdx >= Checker.getTypes().size()) {
507
3
      spdlog::error(ErrCode::Value::InvalidTagIdx);
508
3
      spdlog::error(ErrInfo::InfoForbidIndex(
509
3
          ErrInfo::IndexCategory::TagType, TagTypeIdx,
510
3
          static_cast<uint32_t>(Checker.getTypes().size())));
511
3
      return Unexpect(ErrCode::Value::InvalidTagIdx);
512
3
    }
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
56
  case ExternalType::Global: {
529
56
    const auto &GlobType = ImpDesc.getExternalGlobalType();
530
    // Global type must be valid.
531
56
    EXPECTED_TRY(validate(GlobType).map_error([](auto E) {
532
54
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Global));
533
54
      return E;
534
54
    }));
535
54
    Checker.addGlobal(GlobType, true);
536
54
    return {};
537
56
  }
538
0
  default:
539
0
    return {};
540
469
  }
541
469
}
542
543
// Validate Export description. See "include/validator/validator.h".
544
11.4k
Expect<void> Validator::validate(const AST::ExportDesc &ExpDesc) {
545
11.4k
  auto Id = ExpDesc.getExternalIndex();
546
11.4k
  switch (ExpDesc.getExternalType()) {
547
11.2k
  case ExternalType::Function:
548
11.2k
    if (Id >= Checker.getFunctions().size()) {
549
19
      spdlog::error(ErrCode::Value::InvalidFuncIdx);
550
19
      spdlog::error(ErrInfo::InfoForbidIndex(
551
19
          ErrInfo::IndexCategory::Function, Id,
552
19
          static_cast<uint32_t>(Checker.getFunctions().size())));
553
19
      return Unexpect(ErrCode::Value::InvalidFuncIdx);
554
19
    }
555
11.2k
    Checker.addRef(Id);
556
11.2k
    return {};
557
32
  case ExternalType::Table:
558
32
    if (Id >= Checker.getTables().size()) {
559
7
      spdlog::error(ErrCode::Value::InvalidTableIdx);
560
7
      spdlog::error(ErrInfo::InfoForbidIndex(
561
7
          ErrInfo::IndexCategory::Table, Id,
562
7
          static_cast<uint32_t>(Checker.getTables().size())));
563
7
      return Unexpect(ErrCode::Value::InvalidTableIdx);
564
7
    }
565
25
    return {};
566
110
  case ExternalType::Memory:
567
110
    if (Id >= Checker.getMemories()) {
568
19
      spdlog::error(ErrCode::Value::InvalidMemoryIdx);
569
19
      spdlog::error(ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::Memory, Id,
570
19
                                             Checker.getMemories()));
571
19
      return Unexpect(ErrCode::Value::InvalidMemoryIdx);
572
19
    }
573
91
    return {};
574
8
  case ExternalType::Tag:
575
8
    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
7
    return {};
583
31
  case ExternalType::Global:
584
31
    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
26
    return {};
592
0
  default:
593
0
    return {};
594
11.4k
  }
595
11.4k
}
596
597
4.20k
Expect<void> Validator::validate(const AST::TypeSection &TypeSec) {
598
4.20k
  const auto STypeList = TypeSec.getContent();
599
4.20k
  uint32_t Idx = 0;
600
11.4k
  while (Idx < STypeList.size()) {
601
7.21k
    const auto &SType = STypeList[Idx];
602
7.21k
    if (SType.getRecursiveInfo().has_value()) {
603
      // Recursive type case. Add types first for referring recursively.
604
0
      uint32_t RecSize = SType.getRecursiveInfo()->RecTypeSize;
605
0
      for (uint32_t I = Idx; I < Idx + RecSize; I++) {
606
0
        Checker.addType(STypeList[I]);
607
0
      }
608
0
      for (uint32_t I = Idx; I < Idx + RecSize; I++) {
609
0
        EXPECTED_TRY(validate(STypeList[I]).map_error([](auto E) {
610
0
          spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Rec));
611
0
          return E;
612
0
        }));
613
0
      }
614
0
      Idx += RecSize;
615
7.21k
    } else {
616
      // SubType case.
617
7.21k
      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.21k
        Checker.addType(SType);
621
7.21k
        EXPECTED_TRY(validate(*Checker.getTypes().back()));
622
7.21k
      } else {
623
        // Validating first.
624
0
        EXPECTED_TRY(validate(SType));
625
0
        Checker.addType(SType);
626
0
      }
627
7.21k
      Idx++;
628
7.21k
    }
629
7.21k
  }
630
4.20k
  return {};
631
4.20k
}
632
633
// Validate Import section. See "include/validator/validator.h".
634
4.20k
Expect<void> Validator::validate(const AST::ImportSection &ImportSec) {
635
4.20k
  for (auto &ImportDesc : ImportSec.getContent()) {
636
469
    EXPECTED_TRY(validate(ImportDesc).map_error([](auto E) {
637
469
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Import));
638
469
      return E;
639
469
    }));
640
469
  }
641
4.18k
  return {};
642
4.20k
}
643
644
// Validate Function section. See "include/validator/validator.h".
645
4.18k
Expect<void> Validator::validate(const AST::FunctionSection &FuncSec) {
646
4.18k
  const auto &FuncVec = FuncSec.getContent();
647
4.18k
  const auto &TypeVec = Checker.getTypes();
648
649
  // Check if type id of function is valid in context.
650
19.6k
  for (auto &TId : FuncVec) {
651
19.6k
    if (TId >= TypeVec.size()) {
652
10
      spdlog::error(ErrCode::Value::InvalidFuncTypeIdx);
653
10
      spdlog::error(
654
10
          ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::FunctionType, TId,
655
10
                                   static_cast<uint32_t>(TypeVec.size())));
656
10
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function));
657
10
      return Unexpect(ErrCode::Value::InvalidFuncTypeIdx);
658
10
    }
659
19.6k
    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
19.6k
    Checker.addFunc(TId);
666
19.6k
  }
667
4.17k
  return {};
668
4.18k
}
669
670
// Validate Table section. See "include/validator/validator.h".
671
4.17k
Expect<void> Validator::validate(const AST::TableSection &TabSec) {
672
4.17k
  for (auto &Tab : TabSec.getContent()) {
673
445
    EXPECTED_TRY(validate(Tab).map_error([](auto E) {
674
428
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Table));
675
428
      return E;
676
428
    }));
677
428
    Checker.addTable(Tab.getTableType());
678
428
  }
679
4.15k
  return {};
680
4.17k
}
681
682
// Validate Memory section. See "include/validator/validator.h".
683
4.15k
Expect<void> Validator::validate(const AST::MemorySection &MemSec) {
684
4.15k
  for (auto &Mem : MemSec.getContent()) {
685
1.44k
    EXPECTED_TRY(validate(Mem).map_error([](auto E) {
686
1.41k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Memory));
687
1.41k
      return E;
688
1.41k
    }));
689
1.41k
    Checker.addMemory(Mem);
690
1.41k
  }
691
4.12k
  return {};
692
4.15k
}
693
694
// Validate Global section. See "include/validator/validator.h".
695
4.12k
Expect<void> Validator::validate(const AST::GlobalSection &GlobSec) {
696
4.12k
  for (auto &GlobSeg : GlobSec.getContent()) {
697
355
    EXPECTED_TRY(validate(GlobSeg).map_error([](auto E) {
698
225
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Global));
699
225
      return E;
700
225
    }));
701
225
    Checker.addGlobal(GlobSeg.getGlobalType());
702
225
  }
703
3.99k
  return {};
704
4.12k
}
705
706
// Validate Element section. See "include/validator/validator.h".
707
3.91k
Expect<void> Validator::validate(const AST::ElementSection &ElemSec) {
708
3.91k
  for (auto &ElemSeg : ElemSec.getContent()) {
709
627
    EXPECTED_TRY(validate(ElemSeg).map_error([](auto E) {
710
563
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Element));
711
563
      return E;
712
563
    }));
713
563
    Checker.addElem(ElemSeg);
714
563
  }
715
3.85k
  return {};
716
3.91k
}
717
718
// Validate Code section. See "include/validator/validator.h".
719
3.82k
Expect<void> Validator::validate(const AST::CodeSection &CodeSec) {
720
3.82k
  const auto &CodeVec = CodeSec.getContent();
721
3.82k
  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
14.0k
    uint32_t TId = Id + static_cast<uint32_t>(Checker.getNumImportFuncs());
727
14.0k
    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
14.0k
    EXPECTED_TRY(validate(CodeVec[Id], FuncVec[TId]).map_error([](auto E) {
735
14.0k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Code));
736
14.0k
      return E;
737
14.0k
    }));
738
14.0k
  }
739
2.19k
  return {};
740
3.82k
}
741
742
// Validate Data section. See "include/validator/validator.h".
743
3.85k
Expect<void> Validator::validate(const AST::DataSection &DataSec) {
744
3.85k
  for (auto &DataSeg : DataSec.getContent()) {
745
352
    EXPECTED_TRY(validate(DataSeg).map_error([](auto E) {
746
326
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Data));
747
326
      return E;
748
326
    }));
749
326
    Checker.addData(DataSeg);
750
326
  }
751
3.82k
  return {};
752
3.85k
}
753
754
// Validate Start section. See "include/validator/validator.h".
755
3.92k
Expect<void> Validator::validate(const AST::StartSection &StartSec) {
756
3.92k
  if (StartSec.getContent()) {
757
12
    auto FId = *StartSec.getContent();
758
12
    if (FId >= Checker.getFunctions().size()) {
759
6
      spdlog::error(ErrCode::Value::InvalidFuncIdx);
760
6
      spdlog::error(ErrInfo::InfoForbidIndex(
761
6
          ErrInfo::IndexCategory::Function, FId,
762
6
          static_cast<uint32_t>(Checker.getFunctions().size())));
763
6
      return Unexpect(ErrCode::Value::InvalidFuncIdx);
764
6
    }
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
3.91k
  return {};
782
3.92k
}
783
784
// Validate Export section. See "include/validator/validator.h".
785
3.97k
Expect<void> Validator::validate(const AST::ExportSection &ExportSec) {
786
3.97k
  std::unordered_set<std::string_view, Hash::Hash> ExportNames;
787
11.4k
  for (auto &ExportDesc : ExportSec.getContent()) {
788
11.4k
    auto Result = ExportNames.emplace(ExportDesc.getExternalName());
789
11.4k
    if (!Result.second) {
790
      // Duplicated export name.
791
5
      spdlog::error(ErrCode::Value::DupExportName);
792
5
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Export));
793
5
      return Unexpect(ErrCode::Value::DupExportName);
794
5
    }
795
11.4k
    EXPECTED_TRY(validate(ExportDesc).map_error([](auto E) {
796
11.4k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Export));
797
11.4k
      return E;
798
11.4k
    }));
799
11.4k
  }
800
3.92k
  return {};
801
3.97k
}
802
803
// Validate Tag section. See "include/validator/validator.h".
804
3.99k
Expect<void> Validator::validate(const AST::TagSection &TagSec) {
805
3.99k
  const auto &TagVec = TagSec.getContent();
806
3.99k
  const auto &TypeVec = Checker.getTypes();
807
808
  // Check if type id of tag is valid in context.
809
3.99k
  for (auto &TagType : TagVec) {
810
46
    auto TagTypeIdx = TagType.getTypeIdx();
811
46
    if (TagTypeIdx >= TypeVec.size()) {
812
11
      spdlog::error(ErrCode::Value::InvalidTagIdx);
813
11
      spdlog::error(
814
11
          ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::TagType, TagTypeIdx,
815
11
                                   static_cast<uint32_t>(TypeVec.size())));
816
11
      return Unexpect(ErrCode::Value::InvalidTagIdx);
817
11
    }
818
35
    auto &CompType = TypeVec[TagTypeIdx]->getCompositeType();
819
35
    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
32
    if (!CompType.getFuncType().getReturnTypes().empty()) {
826
1
      spdlog::error(ErrCode::Value::InvalidTagResultType);
827
1
      return Unexpect(ErrCode::Value::InvalidTagResultType);
828
1
    }
829
31
    Checker.addTag(TagTypeIdx);
830
31
  }
831
3.97k
  return {};
832
3.99k
}
833
834
// Validate constant expression. See "include/validator/validator.h".
835
Expect<void> Validator::validateConstExpr(AST::InstrView Instrs,
836
2.53k
                                          Span<const ValType> Returns) {
837
6.37k
  for (auto &Instr : Instrs) {
838
    // Only these instructions are accepted.
839
6.37k
    switch (Instr.getOpCode()) {
840
26
    case OpCode::Global__get: {
841
      // For initialization case, global indices must be imported globals.
842
26
      auto GlobIdx = Instr.getTargetIndex();
843
26
      uint32_t ValidGlobalSize = Checker.getNumImportGlobals();
844
26
      if (Conf.hasProposal(Proposal::FunctionReferences)) {
845
26
        ValidGlobalSize = static_cast<uint32_t>(Checker.getGlobals().size());
846
26
      }
847
26
      if (GlobIdx >= ValidGlobalSize) {
848
7
        spdlog::error(ErrCode::Value::InvalidGlobalIdx);
849
7
        spdlog::error(ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::Global,
850
7
                                               GlobIdx, ValidGlobalSize));
851
7
        spdlog::error(
852
7
            ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
853
7
        return Unexpect(ErrCode::Value::InvalidGlobalIdx);
854
7
      }
855
19
      if (Checker.getGlobals()[GlobIdx].second != ValMut::Const) {
856
2
        spdlog::error(ErrCode::Value::ConstExprRequired);
857
2
        spdlog::error(
858
2
            ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
859
2
        return Unexpect(ErrCode::Value::ConstExprRequired);
860
2
      }
861
17
      break;
862
19
    }
863
1.61k
    case OpCode::Ref__func: {
864
      // When in const expression, add the reference into context.
865
1.61k
      auto FuncIdx = Instr.getTargetIndex();
866
1.61k
      if (FuncIdx >= Checker.getFunctions().size()) {
867
        // Function index out of range.
868
18
        spdlog::error(ErrCode::Value::InvalidFuncIdx);
869
18
        spdlog::error(ErrInfo::InfoForbidIndex(
870
18
            ErrInfo::IndexCategory::Function, FuncIdx,
871
18
            static_cast<uint32_t>(Checker.getFunctions().size())));
872
18
        spdlog::error(
873
18
            ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
874
18
        return Unexpect(ErrCode::Value::InvalidFuncIdx);
875
18
      }
876
1.59k
      Checker.addRef(Instr.getTargetIndex());
877
1.59k
      break;
878
1.61k
    }
879
1.10k
    case OpCode::I32__const:
880
1.23k
    case OpCode::I64__const:
881
1.31k
    case OpCode::F32__const:
882
1.36k
    case OpCode::F64__const:
883
1.49k
    case OpCode::Ref__null:
884
1.52k
    case OpCode::V128__const:
885
3.94k
    case OpCode::End:
886
3.97k
    case OpCode::Struct__new:
887
3.98k
    case OpCode::Struct__new_default:
888
3.99k
    case OpCode::Array__new:
889
4.00k
    case OpCode::Array__new_default:
890
4.01k
    case OpCode::Array__new_fixed:
891
4.01k
    case OpCode::Any__convert_extern:
892
4.02k
    case OpCode::Extern__convert_any:
893
4.02k
    case OpCode::Ref__i31:
894
4.02k
      break;
895
896
    // For the Extended-const proposal, these instructions are accepted.
897
98
    case OpCode::I32__add:
898
191
    case OpCode::I32__sub:
899
291
    case OpCode::I32__mul:
900
387
    case OpCode::I64__add:
901
461
    case OpCode::I64__sub:
902
621
    case OpCode::I64__mul:
903
621
      if (Conf.hasProposal(Proposal::ExtendedConst)) {
904
621
        break;
905
621
      }
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
90
    default:
913
90
      spdlog::error(ErrCode::Value::ConstExprRequired);
914
90
      spdlog::error(
915
90
          ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
916
90
      return Unexpect(ErrCode::Value::ConstExprRequired);
917
6.37k
    }
918
6.37k
  }
919
  // Validate expression with result types.
920
2.41k
  Checker.reset();
921
2.41k
  return Checker.validate(Instrs, Returns);
922
2.53k
}
923
924
} // namespace Validator
925
} // namespace WasmEdge