Coverage Report

Created: 2026-06-30 06:10

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