/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/component/component.h" |
18 | | #include "ast/module.h" |
19 | | #include "common/configure.h" |
20 | | #include "validator/component_context.h" |
21 | | #include "validator/formchecker.h" |
22 | | |
23 | | #include <cstdint> |
24 | | #include <utility> |
25 | | #include <vector> |
26 | | |
27 | | namespace WasmEdge { |
28 | | namespace Validator { |
29 | | |
30 | | /// Validator flow control class. |
31 | | class Validator { |
32 | | public: |
33 | 16.1k | Validator(const Configure &Conf) noexcept : Conf(Conf) {} |
34 | 16.1k | ~Validator() noexcept = default; |
35 | | |
36 | | /// Validate AST::Module. |
37 | | Expect<void> validate(const AST::Module &Mod); |
38 | | /// Validate AST::Component. |
39 | | Expect<void> validate(const AST::Component::Component &Comp) noexcept; |
40 | | |
41 | | private: |
42 | | /// \name Validate WASM AST nodes |
43 | | /// @{ |
44 | | // Validate AST::Types |
45 | | Expect<void> validate(const AST::SubType &Type, uint32_t OwnTypeIdx, |
46 | | std::vector<uint32_t> &SubTypeDepthMap, |
47 | | const std::vector<const AST::SubType *> &TypeVec); |
48 | | Expect<void> validate(Span<const AST::SubType> RecType, uint32_t BaseIdx, |
49 | | std::vector<uint32_t> &SubTypeDepthMap, |
50 | | const std::vector<const AST::SubType *> &TypeVec); |
51 | | Expect<void> validate(const AST::Limit &Lim); |
52 | | Expect<void> validate(const AST::TableType &Tab); |
53 | | Expect<void> validate(const AST::MemoryType &Mem); |
54 | | Expect<void> validate(const AST::GlobalType &Glob); |
55 | | // Validate AST::Segments |
56 | | Expect<void> validate(const AST::TableSegment &TabSeg); |
57 | | Expect<void> validate(const AST::GlobalSegment &GlobSeg); |
58 | | Expect<void> validate(const AST::ElementSegment &ElemSeg); |
59 | | Expect<void> validate(const AST::CodeSegment &CodeSeg, |
60 | | const uint32_t TypeIdx); |
61 | | Expect<void> validate(const AST::DataSegment &DataSeg); |
62 | | // Validate AST::Desc |
63 | | Expect<void> validate(const AST::ImportDesc &ImpDesc); |
64 | | Expect<void> validate(const AST::ExportDesc &ExpDesc); |
65 | | // Validate AST::Sections |
66 | | Expect<void> validate(const AST::TypeSection &TypeSec); |
67 | | Expect<void> validate(const AST::ImportSection &ImportSec); |
68 | | Expect<void> validate(const AST::FunctionSection &FuncSec); |
69 | | Expect<void> validate(const AST::TableSection &TabSec); |
70 | | Expect<void> validate(const AST::MemorySection &MemSec); |
71 | | Expect<void> validate(const AST::GlobalSection &GlobSec); |
72 | | Expect<void> validate(const AST::ElementSection &ElemSec); |
73 | | Expect<void> validate(const AST::CodeSection &CodeSec); |
74 | | Expect<void> validate(const AST::DataSection &DataSec); |
75 | | Expect<void> validate(const AST::StartSection &StartSec); |
76 | | Expect<void> validate(const AST::ExportSection &ExportSec); |
77 | | Expect<void> validate(const AST::TagSection &TagSec); |
78 | | // Validate const expression |
79 | | Expect<void> validateConstExpr(AST::InstrView Instrs, |
80 | | Span<const ValType> Returns); |
81 | | /// @} |
82 | | |
83 | | /// \name Validate Component Model AST nodes |
84 | | /// @{ |
85 | | // Validate a component body in a fresh scope, into the caller's shape. |
86 | | Expect<void> validate(const AST::Component::Component &Comp, |
87 | | Component::Shape &Out) noexcept; |
88 | | // Validate component sections |
89 | | Expect<void> |
90 | | validate(const AST::Component::CoreModuleSection &ModSec) noexcept; |
91 | | Expect<void> |
92 | | validate(const AST::Component::CoreInstanceSection &InstSec) noexcept; |
93 | | Expect<void> |
94 | | validate(const AST::Component::CoreTypeSection &TypeSec) noexcept; |
95 | | Expect<void> |
96 | | validate(const AST::Component::ComponentSection &CompSec) noexcept; |
97 | | Expect<void> |
98 | | validate(const AST::Component::InstanceSection &InstSec) noexcept; |
99 | | Expect<void> validate(const AST::Component::AliasSection &AliasSec) noexcept; |
100 | | Expect<void> validate(const AST::Component::TypeSection &TypeSec) noexcept; |
101 | | Expect<void> validate(const AST::Component::CanonSection &CanonSec) noexcept; |
102 | | Expect<void> validate(const AST::Component::StartSection &StartSec) noexcept; |
103 | | // Imports and exports accumulate into the component's own external type. |
104 | | Expect<void> validate(const AST::Component::ImportSection &ImpSec, |
105 | | Component::Shape &Out) noexcept; |
106 | | Expect<void> validate(const AST::Component::ExportSection &ExpSec, |
107 | | Component::Shape &Out) noexcept; |
108 | | Expect<void> validate(const AST::Component::ValueSection &ValSec) noexcept; |
109 | | // Validate component core:instance and instance |
110 | | Expect<void> validate(const AST::Component::CoreInstance &Inst) noexcept; |
111 | | Expect<void> validate(const AST::Component::Instance &Inst) noexcept; |
112 | | // Validate component core:alias and alias |
113 | | Expect<void> validate(const AST::Component::CoreAlias &Alias) noexcept; |
114 | | Expect<void> validate(const AST::Component::Alias &Alias) noexcept; |
115 | | // Validate component core:deftype and deftype |
116 | | Expect<void> validate(const AST::Component::CoreDefType &DType) noexcept; |
117 | | Expect<void> validate(const AST::Component::DefType &DType) noexcept; |
118 | | // Validate component canonical |
119 | | Expect<void> validate(const AST::Component::Canonical &Canon) noexcept; |
120 | | // Validate component import/export |
121 | | Expect<void> validate(const AST::Component::Import &Im, |
122 | | Component::Shape &Out) noexcept; |
123 | | Expect<void> validate(const AST::Component::Export &Ex, |
124 | | Component::Shape &Out) noexcept; |
125 | | // Resolve descriptors into typed views; a sub-resource bound gets a fresh id. |
126 | | Expect<void> validate(const AST::Component::CoreImportDesc &Desc, |
127 | | Component::CoreExternInfo &Out) noexcept; |
128 | | Expect<void> validate(const AST::Component::ExternDesc &Desc, bool IsImport, |
129 | | Component::ExternInfo &Out) noexcept; |
130 | | // The external type of an inline core module, into the caller's shape. |
131 | | Expect<void> validate(const AST::Module &Mod, |
132 | | Component::CoreShape &Out) noexcept; |
133 | | // Validate type declaration bodies into the caller-provided shape. |
134 | | Expect<void> validate(Span<const AST::Component::CoreModuleDecl> Decls, |
135 | | Component::CoreShape &Out) noexcept; |
136 | | Expect<void> validate(const AST::Component::InstanceType &IT, |
137 | | Component::Shape &Out) noexcept; |
138 | | Expect<void> validate(const AST::Component::ComponentType &CT, |
139 | | Component::Shape &Out) noexcept; |
140 | | Expect<void> validate(const AST::Component::InstanceDecl &Decl, |
141 | | std::map<std::string, Component::ExternInfo, |
142 | | std::less<>> &Exports) noexcept; |
143 | | Expect<void> validate(const AST::Component::ComponentDecl &Decl, |
144 | | Component::Shape &Out) noexcept; |
145 | | // Validate component value types and type definitions |
146 | | Expect<void> validate(const AST::Component::DefValType &DVT) noexcept; |
147 | | Expect<void> validate(const AST::Component::FuncType &FT) noexcept; |
148 | | Expect<void> validate(const AST::Component::ResourceType &RT) noexcept; |
149 | | /// @} |
150 | | |
151 | | /// Memory page limit for WASM32 and WASM64 |
152 | | static inline const uint64_t LIMIT_MEMORYTYPE_LIM64 = UINT64_C(1) << 48; |
153 | | static inline const uint32_t LIMIT_MEMORYTYPE_LIM32 = UINT32_C(1) << 16; |
154 | | /// Proposal configure |
155 | | const Configure Conf; |
156 | | /// Formal checker |
157 | | FormChecker Checker; |
158 | | /// Core-module code sections deferred to the end of the root component. |
159 | | std::vector<std::pair<const AST::Module *, FormChecker>> DeferredModules; |
160 | | /// Checker for Component validation |
161 | | Component::Context CompCtx; |
162 | | }; |
163 | | |
164 | | } // namespace Validator |
165 | | } // namespace WasmEdge |