Coverage Report

Created: 2025-11-16 06:42

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
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.30k
Expect<void> Validator::validate(const AST::Module &Mod) {
68
  // https://webassembly.github.io/spec/core/valid/modules.html
69
4.30k
  Checker.reset(true);
70
71
  // Validate and register type section.
72
4.30k
  EXPECTED_TRY(validate(Mod.getTypeSection()).map_error([](auto E) {
73
4.29k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Type));
74
4.29k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
75
4.29k
    return E;
76
4.29k
  }));
77
78
  // Validate and register import section into FormChecker.
79
4.29k
  EXPECTED_TRY(validate(Mod.getImportSection()).map_error([](auto E) {
80
4.27k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Import));
81
4.27k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
82
4.27k
    return E;
83
4.27k
  }));
84
85
  // Validate function section and register functions into FormChecker.
86
4.27k
  EXPECTED_TRY(validate(Mod.getFunctionSection()).map_error([](auto E) {
87
4.26k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Function));
88
4.26k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
89
4.26k
    return E;
90
4.26k
  }));
91
92
  // Validate table section and register tables into FormChecker.
93
4.26k
  EXPECTED_TRY(validate(Mod.getTableSection()).map_error([](auto E) {
94
4.25k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Table));
95
4.25k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
96
4.25k
    return E;
97
4.25k
  }));
98
99
  // Validate memory section and register memories into FormChecker.
100
4.25k
  EXPECTED_TRY(validate(Mod.getMemorySection()).map_error([](auto E) {
101
4.22k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Memory));
102
4.22k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
103
4.22k
    return E;
104
4.22k
  }));
105
106
  // Validate global section and register globals into FormChecker.
107
4.22k
  EXPECTED_TRY(validate(Mod.getGlobalSection()).map_error([](auto E) {
108
4.09k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Global));
109
4.09k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
110
4.09k
    return E;
111
4.09k
  }));
112
113
  // Validate tag section and register tags into FormChecker.
114
4.09k
  EXPECTED_TRY(validate(Mod.getTagSection()).map_error([](auto E) {
115
4.08k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Tag));
116
4.08k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
117
4.08k
    return E;
118
4.08k
  }));
119
120
  // Validate export section.
121
4.08k
  EXPECTED_TRY(validate(Mod.getExportSection()).map_error([](auto E) {
122
4.02k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Export));
123
4.02k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
124
4.02k
    return E;
125
4.02k
  }));
126
127
  // Validate start section.
128
4.02k
  EXPECTED_TRY(validate(Mod.getStartSection()).map_error([](auto E) {
129
4.02k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Start));
130
4.02k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
131
4.02k
    return E;
132
4.02k
  }));
133
134
  // Validate element section which initialize tables.
135
4.02k
  EXPECTED_TRY(validate(Mod.getElementSection()).map_error([](auto E) {
136
3.95k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Element));
137
3.95k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
138
3.95k
    return E;
139
3.95k
  }));
140
141
  // Validate data section which initialize memories.
142
3.95k
  EXPECTED_TRY(validate(Mod.getDataSection()).map_error([](auto E) {
143
3.92k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Data));
144
3.92k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
145
3.92k
    return E;
146
3.92k
  }));
147
148
  // Validate code section and expressions.
149
3.92k
  EXPECTED_TRY(validate(Mod.getCodeSection()).map_error([](auto E) {
150
2.30k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Sec_Code));
151
2.30k
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
152
2.30k
    return E;
153
2.30k
  }));
154
155
  // Multiple tables is for the ReferenceTypes proposal.
156
2.30k
  if (Checker.getTables().size() > 1 &&
157
42
      !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.30k
  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.30k
  const_cast<AST::Module &>(Mod).setIsValidated();
174
2.30k
  return {};
175
2.30k
}
176
177
// Validate Sub type. See "include/validator/validator.h".
178
7.52k
Expect<void> Validator::validate(const AST::SubType &Type) {
179
7.52k
  const auto &TypeVec = Checker.getTypes();
180
7.52k
  const auto &CompType = Type.getCompositeType();
181
182
  // Check the validation of the composite type.
183
7.52k
  if (CompType.isFunc()) {
184
7.37k
    const auto &FType = CompType.getFuncType();
185
7.37k
    for (auto &PType : FType.getParamTypes()) {
186
6.61k
      EXPECTED_TRY(Checker.validate(PType).map_error([](auto E) {
187
6.61k
        spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function));
188
6.61k
        return E;
189
6.61k
      }));
190
6.61k
    }
191
7.37k
    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.37k
    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.37k
  } else {
205
142
    const auto &FTypes = CompType.getFieldTypes();
206
142
    for (auto &FieldType : FTypes) {
207
135
      EXPECTED_TRY(Checker.validate(FieldType.getStorageType()));
208
135
    }
209
142
  }
210
211
  // In current version, the length of type index vector will be <= 1.
212
7.51k
  if (Type.getSuperTypeIndices().size() > 1) {
213
1
    spdlog::error(ErrCode::Value::InvalidSubType);
214
1
    spdlog::error("    Accepts only one super type currently."sv);
215
1
    return Unexpect(ErrCode::Value::InvalidSubType);
216
1
  }
217
218
7.51k
  for (const auto &Index : Type.getSuperTypeIndices()) {
219
3
    if (unlikely(Index >= TypeVec.size())) {
220
2
      spdlog::error(ErrCode::Value::InvalidSubType);
221
2
      spdlog::error(
222
2
          ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::DefinedType, Index,
223
2
                                   static_cast<uint32_t>(TypeVec.size())));
224
2
      return Unexpect(ErrCode::Value::InvalidSubType);
225
2
    }
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.51k
  return {};
246
7.51k
}
247
248
// Validate Limit type. See "include/validator/validator.h".
249
1.94k
Expect<void> Validator::validate(const AST::Limit &Lim) {
250
1.94k
  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.93k
  if (Lim.isShared() && unlikely(!Lim.hasMax())) {
256
0
    spdlog::error(ErrCode::Value::SharedMemoryNoMax);
257
0
    return Unexpect(ErrCode::Value::SharedMemoryNoMax);
258
0
  }
259
1.93k
  return {};
260
1.93k
}
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
477
  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.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
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.43k
  return {};
292
1.46k
}
293
294
// Validate Global type. See "include/validator/validator.h".
295
291
Expect<void> Validator::validate(const AST::GlobalType &Glob) {
296
  // Validate value type.
297
291
  return Checker.validate(Glob.getValType());
298
291
}
299
300
// Validate Table segment. See "include/validator/validator.h".
301
433
Expect<void> Validator::validate(const AST::TableSegment &TabSeg) {
302
433
  if (TabSeg.getExpr().getInstrs().size() > 0) {
303
    // Check ref initialization is a const expression.
304
10
    EXPECTED_TRY(
305
10
        validateConstExpr(TabSeg.getExpr().getInstrs(),
306
10
                          {ValType(TabSeg.getTableType().getRefType())})
307
10
            .map_error([](auto E) {
308
10
              spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
309
10
              return E;
310
10
            }));
311
423
  } else {
312
    // No init expression. Check the reference type is nullable.
313
423
    if (!TabSeg.getTableType().getRefType().isNullableRefType()) {
314
1
      spdlog::error(ErrCode::Value::TypeCheckFailed);
315
1
      spdlog::error(ErrInfo::InfoMismatch(
316
1
          ValType(TypeCode::RefNull,
317
1
                  TabSeg.getTableType().getRefType().getHeapTypeCode(),
318
1
                  TabSeg.getTableType().getRefType().getTypeIndex()),
319
1
          TabSeg.getTableType().getRefType()));
320
1
      return Unexpect(ErrCode::Value::TypeCheckFailed);
321
1
    }
322
423
  }
323
  // Validate table type.
324
426
  return validate(TabSeg.getTableType()).map_error([](auto E) {
325
6
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Table));
326
6
    return E;
327
6
  });
328
433
}
329
330
// Validate Global segment. See "include/validator/validator.h".
331
349
Expect<void> Validator::validate(const AST::GlobalSegment &GlobSeg) {
332
  // Check global initialization is a const expression.
333
349
  EXPECTED_TRY(validateConstExpr(GlobSeg.getExpr().getInstrs(),
334
226
                                 {GlobSeg.getGlobalType().getValType()})
335
226
                   .map_error([](auto E) {
336
226
                     spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
337
226
                     return E;
338
226
                   }));
339
  // Validate global type.
340
226
  return validate(GlobSeg.getGlobalType()).map_error([](auto E) {
341
0
    spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Global));
342
0
    return E;
343
0
  });
344
349
}
345
346
// Validate Element segment. See "include/validator/validator.h".
347
807
Expect<void> Validator::validate(const AST::ElementSegment &ElemSeg) {
348
  // Check initialization expressions are const expressions.
349
1.76k
  for (auto &Expr : ElemSeg.getInitExprs()) {
350
1.76k
    EXPECTED_TRY(
351
1.76k
        validateConstExpr(Expr.getInstrs(), {ValType(ElemSeg.getRefType())})
352
1.76k
            .map_error([](auto E) {
353
1.76k
              spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
354
1.76k
              return E;
355
1.76k
            }));
356
1.76k
  }
357
358
  // The reference type should be valid.
359
766
  EXPECTED_TRY(Checker.validate(ElemSeg.getRefType()));
360
361
  // Passive and declarative cases are valid with the valid reference type.
362
765
  if (ElemSeg.getMode() == AST::ElementSegment::ElemMode::Active) {
363
    // Check table index and reference type in context.
364
485
    const auto &TableVec = Checker.getTables();
365
485
    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
473
    if (TableVec[ElemSeg.getIdx()].isFuncRefType() !=
379
473
            ElemSeg.getRefType().isFuncRefType() ||
380
471
        (!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
471
    return validateConstExpr(ElemSeg.getExpr().getInstrs(),
390
471
                             {ValType(TypeCode::I32)})
391
471
        .map_error([](auto E) {
392
12
          spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
393
12
          return E;
394
12
        });
395
473
  }
396
280
  return {};
397
765
}
398
399
// Validate Code segment. See "include/validator/validator.h".
400
Expect<void> Validator::validate(const AST::CodeSegment &CodeSeg,
401
14.3k
                                 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.3k
  const auto &FuncType =
405
14.3k
      Checker.getTypes()[TypeIdx]->getCompositeType().getFuncType();
406
  // Reset stack in FormChecker.
407
14.3k
  Checker.reset();
408
  // Add parameters into this frame.
409
14.3k
  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
14.3k
  for (auto Val : CodeSeg.getLocals()) {
415
13.0M
    for (uint32_t Cnt = 0; Cnt < Val.first; ++Cnt) {
416
      // The local value type should be valid.
417
13.0M
      EXPECTED_TRY(Checker.validate(Val.second));
418
13.0M
      Checker.addLocal(Val.second, false);
419
13.0M
    }
420
2.50k
  }
421
  // Validate function body expression.
422
14.3k
  return Checker
423
14.3k
      .validate(CodeSeg.getExpr().getInstrs(), FuncType.getReturnTypes())
424
14.3k
      .map_error([](auto E) {
425
1.61k
        spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
426
1.61k
        return E;
427
1.61k
      });
428
14.3k
}
429
430
// Validate Data segment. See "include/validator/validator.h".
431
343
Expect<void> Validator::validate(const AST::DataSegment &DataSeg) {
432
343
  switch (DataSeg.getMode()) {
433
190
  case AST::DataSegment::DataMode::Active: {
434
    // Check memory index in context.
435
190
    const auto &MemNum = Checker.getMemories();
436
190
    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
169
    return validateConstExpr(DataSeg.getExpr().getInstrs(),
444
169
                             {ValType(TypeCode::I32)})
445
169
        .map_error([](auto E) {
446
6
          spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression));
447
6
          return E;
448
6
        });
449
190
  }
450
153
  case AST::DataSegment::DataMode::Passive:
451
    // Passive case is always valid.
452
153
    return {};
453
0
  default:
454
0
    return {};
455
343
  }
456
343
}
457
458
// Validate Import description. See "include/validator/validator.h".
459
501
Expect<void> Validator::validate(const AST::ImportDesc &ImpDesc) {
460
501
  switch (ImpDesc.getExternalType()) {
461
  // External type and the external content are ensured to be matched in
462
  // loader phase.
463
333
  case ExternalType::Function: {
464
333
    const auto TId = ImpDesc.getExternalFuncTypeIdx();
465
    // Function type index must exist in context and be valid.
466
333
    if (TId >= Checker.getTypes().size()) {
467
5
      spdlog::error(ErrCode::Value::InvalidFuncTypeIdx);
468
5
      spdlog::error(ErrInfo::InfoForbidIndex(
469
5
          ErrInfo::IndexCategory::FunctionType, TId,
470
5
          static_cast<uint32_t>(Checker.getTypes().size())));
471
5
      return Unexpect(ErrCode::Value::InvalidFuncTypeIdx);
472
5
    }
473
328
    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
327
    Checker.addRef(static_cast<uint32_t>(Checker.getFunctions().size()));
479
327
    Checker.addFunc(TId, true);
480
327
    return {};
481
328
  }
482
55
  case ExternalType::Table: {
483
55
    const auto &TabType = ImpDesc.getExternalTableType();
484
    // Table type must be valid.
485
55
    EXPECTED_TRY(validate(TabType).map_error([](auto E) {
486
53
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Table));
487
53
      return E;
488
53
    }));
489
53
    Checker.addTable(TabType);
490
53
    return {};
491
55
  }
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
65
  case ExternalType::Global: {
529
65
    const auto &GlobType = ImpDesc.getExternalGlobalType();
530
    // Global type must be valid.
531
65
    EXPECTED_TRY(validate(GlobType).map_error([](auto E) {
532
63
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Global));
533
63
      return E;
534
63
    }));
535
63
    Checker.addGlobal(GlobType, true);
536
63
    return {};
537
65
  }
538
0
  default:
539
0
    return {};
540
501
  }
541
501
}
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
18
      spdlog::error(ErrCode::Value::InvalidFuncIdx);
550
18
      spdlog::error(ErrInfo::InfoForbidIndex(
551
18
          ErrInfo::IndexCategory::Function, Id,
552
18
          static_cast<uint32_t>(Checker.getFunctions().size())));
553
18
      return Unexpect(ErrCode::Value::InvalidFuncIdx);
554
18
    }
555
11.2k
    Checker.addRef(Id);
556
11.2k
    return {};
557
31
  case ExternalType::Table:
558
31
    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
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
14
  case ExternalType::Tag:
575
14
    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
13
    return {};
583
34
  case ExternalType::Global:
584
34
    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
29
    return {};
592
0
  default:
593
0
    return {};
594
11.4k
  }
595
11.4k
}
596
597
4.30k
Expect<void> Validator::validate(const AST::TypeSection &TypeSec) {
598
4.30k
  const auto STypeList = TypeSec.getContent();
599
4.30k
  uint32_t Idx = 0;
600
11.8k
  while (Idx < STypeList.size()) {
601
7.52k
    const auto &SType = STypeList[Idx];
602
7.52k
    if (SType.getRecursiveInfo().has_value()) {
603
      // Recursive type case. Add types first for referring recursively.
604
1
      uint32_t RecSize = SType.getRecursiveInfo()->RecTypeSize;
605
2
      for (uint32_t I = Idx; I < Idx + RecSize; I++) {
606
1
        Checker.addType(STypeList[I]);
607
1
      }
608
2
      for (uint32_t I = Idx; I < Idx + RecSize; I++) {
609
1
        EXPECTED_TRY(validate(STypeList[I]).map_error([](auto E) {
610
1
          spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Rec));
611
1
          return E;
612
1
        }));
613
1
      }
614
1
      Idx += RecSize;
615
7.52k
    } else {
616
      // SubType case.
617
7.52k
      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.52k
        Checker.addType(SType);
621
7.52k
        EXPECTED_TRY(validate(*Checker.getTypes().back()));
622
7.52k
      } else {
623
        // Validating first.
624
0
        EXPECTED_TRY(validate(SType));
625
0
        Checker.addType(SType);
626
0
      }
627
7.51k
      Idx++;
628
7.51k
    }
629
7.52k
  }
630
4.29k
  return {};
631
4.30k
}
632
633
// Validate Import section. See "include/validator/validator.h".
634
4.29k
Expect<void> Validator::validate(const AST::ImportSection &ImportSec) {
635
4.29k
  for (auto &ImportDesc : ImportSec.getContent()) {
636
501
    EXPECTED_TRY(validate(ImportDesc).map_error([](auto E) {
637
501
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Import));
638
501
      return E;
639
501
    }));
640
501
  }
641
4.27k
  return {};
642
4.29k
}
643
644
// Validate Function section. See "include/validator/validator.h".
645
4.27k
Expect<void> Validator::validate(const AST::FunctionSection &FuncSec) {
646
4.27k
  const auto &FuncVec = FuncSec.getContent();
647
4.27k
  const auto &TypeVec = Checker.getTypes();
648
649
  // Check if type id of function is valid in context.
650
19.0k
  for (auto &TId : FuncVec) {
651
19.0k
    if (TId >= TypeVec.size()) {
652
9
      spdlog::error(ErrCode::Value::InvalidFuncTypeIdx);
653
9
      spdlog::error(
654
9
          ErrInfo::InfoForbidIndex(ErrInfo::IndexCategory::FunctionType, TId,
655
9
                                   static_cast<uint32_t>(TypeVec.size())));
656
9
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function));
657
9
      return Unexpect(ErrCode::Value::InvalidFuncTypeIdx);
658
9
    }
659
19.0k
    if (!TypeVec[TId]->getCompositeType().isFunc()) {
660
2
      spdlog::error(ErrCode::Value::InvalidFuncTypeIdx);
661
2
      spdlog::error("    Defined type index {} is not a function type."sv, TId);
662
2
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Type_Function));
663
2
      return Unexpect(ErrCode::Value::InvalidFuncTypeIdx);
664
2
    }
665
19.0k
    Checker.addFunc(TId);
666
19.0k
  }
667
4.26k
  return {};
668
4.27k
}
669
670
// Validate Table section. See "include/validator/validator.h".
671
4.26k
Expect<void> Validator::validate(const AST::TableSection &TabSec) {
672
4.26k
  for (auto &Tab : TabSec.getContent()) {
673
433
    EXPECTED_TRY(validate(Tab).map_error([](auto E) {
674
420
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Table));
675
420
      return E;
676
420
    }));
677
420
    Checker.addTable(Tab.getTableType());
678
420
  }
679
4.25k
  return {};
680
4.26k
}
681
682
// Validate Memory section. See "include/validator/validator.h".
683
4.25k
Expect<void> Validator::validate(const AST::MemorySection &MemSec) {
684
4.25k
  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.22k
  return {};
692
4.25k
}
693
694
// Validate Global section. See "include/validator/validator.h".
695
4.22k
Expect<void> Validator::validate(const AST::GlobalSection &GlobSec) {
696
4.22k
  for (auto &GlobSeg : GlobSec.getContent()) {
697
349
    EXPECTED_TRY(validate(GlobSeg).map_error([](auto E) {
698
226
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Global));
699
226
      return E;
700
226
    }));
701
226
    Checker.addGlobal(GlobSeg.getGlobalType());
702
226
  }
703
4.09k
  return {};
704
4.22k
}
705
706
// Validate Element section. See "include/validator/validator.h".
707
4.02k
Expect<void> Validator::validate(const AST::ElementSection &ElemSec) {
708
4.02k
  for (auto &ElemSeg : ElemSec.getContent()) {
709
807
    EXPECTED_TRY(validate(ElemSeg).map_error([](auto E) {
710
739
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Element));
711
739
      return E;
712
739
    }));
713
739
    Checker.addElem(ElemSeg);
714
739
  }
715
3.95k
  return {};
716
4.02k
}
717
718
// Validate Code section. See "include/validator/validator.h".
719
3.92k
Expect<void> Validator::validate(const AST::CodeSection &CodeSec) {
720
3.92k
  const auto &CodeVec = CodeSec.getContent();
721
3.92k
  const auto &FuncVec = Checker.getFunctions();
722
723
  // Validate function body.
724
16.6k
  for (uint32_t Id = 0; Id < static_cast<uint32_t>(CodeVec.size()); ++Id) {
725
    // Added functions contains imported functions.
726
14.3k
    uint32_t TId = Id + static_cast<uint32_t>(Checker.getNumImportFuncs());
727
14.3k
    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.3k
    EXPECTED_TRY(validate(CodeVec[Id], FuncVec[TId]).map_error([](auto E) {
735
14.3k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Code));
736
14.3k
      return E;
737
14.3k
    }));
738
14.3k
  }
739
2.30k
  return {};
740
3.92k
}
741
742
// Validate Data section. See "include/validator/validator.h".
743
3.95k
Expect<void> Validator::validate(const AST::DataSection &DataSec) {
744
3.95k
  for (auto &DataSeg : DataSec.getContent()) {
745
343
    EXPECTED_TRY(validate(DataSeg).map_error([](auto E) {
746
316
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Data));
747
316
      return E;
748
316
    }));
749
316
    Checker.addData(DataSeg);
750
316
  }
751
3.92k
  return {};
752
3.95k
}
753
754
// Validate Start section. See "include/validator/validator.h".
755
4.02k
Expect<void> Validator::validate(const AST::StartSection &StartSec) {
756
4.02k
  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
4.02k
  return {};
782
4.02k
}
783
784
// Validate Export section. See "include/validator/validator.h".
785
4.08k
Expect<void> Validator::validate(const AST::ExportSection &ExportSec) {
786
4.08k
  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
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.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
4.02k
  return {};
801
4.08k
}
802
803
// Validate Tag section. See "include/validator/validator.h".
804
4.09k
Expect<void> Validator::validate(const AST::TagSection &TagSec) {
805
4.09k
  const auto &TagVec = TagSec.getContent();
806
4.09k
  const auto &TypeVec = Checker.getTypes();
807
808
  // Check if type id of tag is valid in context.
809
4.09k
  for (auto &TagType : TagVec) {
810
52
    auto TagTypeIdx = TagType.getTypeIdx();
811
52
    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
41
    auto &CompType = TypeVec[TagTypeIdx]->getCompositeType();
819
41
    if (!CompType.isFunc()) {
820
2
      spdlog::error(ErrCode::Value::InvalidTagIdx);
821
2
      spdlog::error("    Defined type index {} is not a function type."sv,
822
2
                    TagTypeIdx);
823
2
      return Unexpect(ErrCode::Value::InvalidTagIdx);
824
2
    }
825
39
    if (!CompType.getFuncType().getReturnTypes().empty()) {
826
1
      spdlog::error(ErrCode::Value::InvalidTagResultType);
827
1
      return Unexpect(ErrCode::Value::InvalidTagResultType);
828
1
    }
829
38
    Checker.addTag(TagTypeIdx);
830
38
  }
831
4.08k
  return {};
832
4.09k
}
833
834
// Validate constant expression. See "include/validator/validator.h".
835
Expect<void> Validator::validateConstExpr(AST::InstrView Instrs,
836
2.76k
                                          Span<const ValType> Returns) {
837
6.87k
  for (auto &Instr : Instrs) {
838
    // Only these instructions are accepted.
839
6.87k
    switch (Instr.getOpCode()) {
840
22
    case OpCode::Global__get: {
841
      // For initialization case, global indices must be imported globals.
842
22
      auto GlobIdx = Instr.getTargetIndex();
843
22
      uint32_t ValidGlobalSize = Checker.getNumImportGlobals();
844
22
      if (Conf.hasProposal(Proposal::FunctionReferences)) {
845
22
        ValidGlobalSize = static_cast<uint32_t>(Checker.getGlobals().size());
846
22
      }
847
22
      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
15
      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
13
      break;
862
15
    }
863
1.68k
    case OpCode::Ref__func: {
864
      // When in const expression, add the reference into context.
865
1.68k
      auto FuncIdx = Instr.getTargetIndex();
866
1.68k
      if (FuncIdx >= Checker.getFunctions().size()) {
867
        // Function index out of range.
868
21
        spdlog::error(ErrCode::Value::InvalidFuncIdx);
869
21
        spdlog::error(ErrInfo::InfoForbidIndex(
870
21
            ErrInfo::IndexCategory::Function, FuncIdx,
871
21
            static_cast<uint32_t>(Checker.getFunctions().size())));
872
21
        spdlog::error(
873
21
            ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
874
21
        return Unexpect(ErrCode::Value::InvalidFuncIdx);
875
21
      }
876
1.66k
      Checker.addRef(Instr.getTargetIndex());
877
1.66k
      break;
878
1.68k
    }
879
1.29k
    case OpCode::I32__const:
880
1.40k
    case OpCode::I64__const:
881
1.46k
    case OpCode::F32__const:
882
1.50k
    case OpCode::F64__const:
883
1.68k
    case OpCode::Ref__null:
884
1.71k
    case OpCode::V128__const:
885
4.36k
    case OpCode::End:
886
4.38k
    case OpCode::Struct__new:
887
4.39k
    case OpCode::Struct__new_default:
888
4.41k
    case OpCode::Array__new:
889
4.42k
    case OpCode::Array__new_default:
890
4.43k
    case OpCode::Array__new_fixed:
891
4.44k
    case OpCode::Any__convert_extern:
892
4.46k
    case OpCode::Extern__convert_any:
893
4.48k
    case OpCode::Ref__i31:
894
4.48k
      break;
895
896
    // For the Extended-const proposal, these instructions are accepted.
897
89
    case OpCode::I32__add:
898
184
    case OpCode::I32__sub:
899
285
    case OpCode::I32__mul:
900
377
    case OpCode::I64__add:
901
478
    case OpCode::I64__sub:
902
597
    case OpCode::I64__mul:
903
597
      if (Conf.hasProposal(Proposal::ExtendedConst)) {
904
597
        break;
905
597
      }
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
84
    default:
913
84
      spdlog::error(ErrCode::Value::ConstExprRequired);
914
84
      spdlog::error(
915
84
          ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
916
84
      return Unexpect(ErrCode::Value::ConstExprRequired);
917
6.87k
    }
918
6.87k
  }
919
  // Validate expression with result types.
920
2.65k
  Checker.reset();
921
2.65k
  return Checker.validate(Instrs, Returns);
922
2.76k
}
923
924
} // namespace Validator
925
} // namespace WasmEdge