Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/include/validator/validator.h
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
//===-- wasmedge/validator/validator.h - validator class definition -------===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the declaration of the validator class, which controls
12
/// the flow of WASM validation.
13
///
14
//===----------------------------------------------------------------------===//
15
#pragma once
16
17
#include "ast/module.h"
18
#include "common/configure.h"
19
#include "validator/component_context.h"
20
#include "validator/formchecker.h"
21
22
#include <cstdint>
23
#include <vector>
24
25
namespace WasmEdge {
26
namespace Validator {
27
28
/// Validator flow control class.
29
class Validator {
30
public:
31
  Validator(const Configure &Conf) noexcept;
32
8.55k
  ~Validator() noexcept = default;
33
34
  /// Validate AST::Module.
35
  Expect<void> validate(const AST::Module &Mod);
36
  /// Validate AST::Component.
37
  Expect<void> validate(const AST::Component::Component &Comp) noexcept;
38
39
private:
40
  /// \name Validate WASM AST nodes
41
  /// @{
42
  // Validate AST::Types
43
  Expect<void> validate(const AST::SubType &Type, uint32_t OwnTypeIdx,
44
                        std::vector<uint32_t> &SubTypeDepthMap);
45
  Expect<void> validate(const AST::Limit &Lim);
46
  Expect<void> validate(const AST::TableType &Tab);
47
  Expect<void> validate(const AST::MemoryType &Mem);
48
  Expect<void> validate(const AST::GlobalType &Glob);
49
  // Validate AST::Segments
50
  Expect<void> validate(const AST::TableSegment &TabSeg);
51
  Expect<void> validate(const AST::GlobalSegment &GlobSeg);
52
  Expect<void> validate(const AST::ElementSegment &ElemSeg);
53
  Expect<void> validate(const AST::CodeSegment &CodeSeg,
54
                        const uint32_t TypeIdx);
55
  Expect<void> validate(const AST::DataSegment &DataSeg);
56
  // Validate AST::Desc
57
  Expect<void> validate(const AST::ImportDesc &ImpDesc);
58
  Expect<void> validate(const AST::ExportDesc &ExpDesc);
59
  // Validate AST::Sections
60
  Expect<void> validate(const AST::TypeSection &TypeSec);
61
  Expect<void> validate(const AST::ImportSection &ImportSec);
62
  Expect<void> validate(const AST::FunctionSection &FuncSec);
63
  Expect<void> validate(const AST::TableSection &TabSec);
64
  Expect<void> validate(const AST::MemorySection &MemSec);
65
  Expect<void> validate(const AST::GlobalSection &GlobSec);
66
  Expect<void> validate(const AST::ElementSection &ElemSec);
67
  Expect<void> validate(const AST::CodeSection &CodeSec);
68
  Expect<void> validate(const AST::DataSection &DataSec);
69
  Expect<void> validate(const AST::StartSection &StartSec);
70
  Expect<void> validate(const AST::ExportSection &ExportSec);
71
  Expect<void> validate(const AST::TagSection &TagSec);
72
  // Validate const expression
73
  Expect<void> validateConstExpr(AST::InstrView Instrs,
74
                                 Span<const ValType> Returns);
75
  /// @}
76
77
  /// \name Validate Component Model AST nodes
78
  /// @{
79
  // Validate component
80
  Expect<void>
81
  validateComponent(const AST::Component::Component &Comp) noexcept;
82
  // Validate component sections
83
  Expect<void>
84
  validate(const AST::Component::CoreModuleSection &ModSec) noexcept;
85
  Expect<void>
86
  validate(const AST::Component::CoreInstanceSection &InstSec) noexcept;
87
  Expect<void>
88
  validate(const AST::Component::CoreTypeSection &TypeSec) noexcept;
89
  Expect<void>
90
  validate(const AST::Component::ComponentSection &CompSec) noexcept;
91
  Expect<void>
92
  validate(const AST::Component::InstanceSection &InstSec) noexcept;
93
  Expect<void> validate(const AST::Component::AliasSection &AliasSec) noexcept;
94
  Expect<void> validate(const AST::Component::TypeSection &TypeSec) noexcept;
95
  Expect<void> validate(const AST::Component::CanonSection &CanonSec) noexcept;
96
  Expect<void> validate(const AST::Component::StartSection &StartSec) noexcept;
97
  Expect<void> validate(const AST::Component::ImportSection &ImpSec) noexcept;
98
  Expect<void> validate(const AST::Component::ExportSection &ExpSec) noexcept;
99
  // Validate component core:instance and instance
100
  Expect<void> validate(const AST::Component::CoreInstance &Inst) noexcept;
101
  Expect<void> validate(const AST::Component::Instance &Inst) noexcept;
102
  // Validate component core:alias and alias
103
  Expect<void> validate(const AST::Component::CoreAlias &Alias) noexcept;
104
  Expect<void> validate(const AST::Component::Alias &Alias) noexcept;
105
  // Validate component core:deftype and deftype
106
  Expect<void> validate(const AST::Component::CoreDefType &DType) noexcept;
107
  Expect<void> validate(const AST::Component::DefType &DType) noexcept;
108
  // Validate component canonical
109
  Expect<void> validate(const AST::Component::Canonical &Canon) noexcept;
110
  Expect<void>
111
  validateCanonOptions(ComponentCanonOpCode Code,
112
                       Span<const AST::Component::CanonOpt> Opts) noexcept;
113
  // Per-opcode canonical built-in validators.
114
  Expect<void>
115
  validateCanonLift(const AST::Component::Canonical &Canon) noexcept;
116
  Expect<void>
117
  validateCanonLower(const AST::Component::Canonical &Canon) noexcept;
118
  Expect<void>
119
  validateCanonResourceNew(const AST::Component::Canonical &Canon) noexcept;
120
  Expect<void>
121
  validateCanonResourceRep(const AST::Component::Canonical &Canon) noexcept;
122
  Expect<void>
123
  validateCanonResourceDrop(const AST::Component::Canonical &Canon) noexcept;
124
  // Validate component import
125
  Expect<void> validate(const AST::Component::Import &Im) noexcept;
126
  // Validate component export
127
  Expect<void> validate(const AST::Component::Export &Ex) noexcept;
128
  // Validate component descs
129
  Expect<void> validate(const AST::Component::CoreImportDesc &Desc) noexcept;
130
  Expect<void> validate(const AST::Component::ExternDesc &Desc) noexcept;
131
  // Validate component decls
132
  Expect<void> validate(const AST::Component::CoreImportDecl &Decl) noexcept;
133
  Expect<void> validate(const AST::Component::CoreExportDecl &Decl) noexcept;
134
  Expect<void> validate(const AST::Component::CoreModuleDecl &Decl) noexcept;
135
  Expect<void> validate(const AST::Component::ImportDecl &Decl) noexcept;
136
  Expect<void> validate(const AST::Component::ExportDecl &Decl) noexcept;
137
  Expect<void> validate(const AST::Component::InstanceDecl &Decl) noexcept;
138
  Expect<void> validate(const AST::Component::ComponentDecl &Decl) noexcept;
139
  // Validate component value types and type definitions
140
  Expect<void> validate(const ComponentValType &VT) noexcept;
141
  Expect<void> validate(const AST::Component::DefValType &DVT) noexcept;
142
  Expect<void> validate(const AST::Component::FuncType &FT) noexcept;
143
  Expect<void> validate(const AST::Component::InstanceType &IT) noexcept;
144
  Expect<void> validate(const AST::Component::ComponentType &CT) noexcept;
145
  Expect<void> validate(const AST::Component::ResourceType &RT) noexcept;
146
  bool containsBorrow(const ComponentValType &VT) const noexcept;
147
  bool containsBorrow(const AST::Component::DefValType &DVT) const noexcept;
148
149
  // Populate the export table of an instance slot from an InstanceType.
150
  void
151
  populateInstanceFromType(uint32_t InstIdx,
152
                           const AST::Component::InstanceType &IT) noexcept;
153
  // Structural subtype on InstanceTypes (sort-kind for non-instance exports).
154
  bool isInstanceSubtype(const AST::Component::InstanceType &S,
155
                         const AST::Component::InstanceType &T) const noexcept;
156
  // True iff a provided export satisfies a required-decl entry.
157
  bool
158
  exportSatisfies(const AST::Component::InstanceType &RequiredCtx,
159
                  const ComponentContext::InstanceExport &Provided,
160
                  const AST::Component::ExternDesc &Required) const noexcept;
161
  // Name of the first required export of `RequiredIT` not satisfied by the
162
  // instance at `ProvidedInstIdx`; nullopt ⇒ all satisfied.
163
  std::optional<std::string> findMissingRequiredExport(
164
      uint32_t ProvidedInstIdx,
165
      const AST::Component::InstanceType &RequiredIT) const noexcept;
166
  /// @}
167
168
  /// Memory page limit for WASM32 and WASM64
169
  static inline const uint64_t LIMIT_MEMORYTYPE_LIM64 = UINT64_C(1) << 48;
170
  static inline const uint32_t LIMIT_MEMORYTYPE_LIM32 = UINT32_C(1) << 16;
171
  /// Proposal configure
172
  const Configure Conf;
173
  /// Formal checker
174
  FormChecker Checker;
175
  /// Context for Component validation
176
  ComponentContext CompCtx;
177
  /// Pre-defined core function SubTypes
178
  const AST::SubType CoreFuncType_I32_I32;  // [i32] -> [i32]
179
  const AST::SubType CoreFuncType_I32_Void; // [i32] -> []
180
};
181
182
} // namespace Validator
183
} // namespace WasmEdge