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