Coverage Report

Created: 2025-12-31 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/include/validator/component_context.h
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
#pragma once
5
6
#include "ast/component/component.h"
7
#include "ast/module.h"
8
9
#include <deque>
10
#include <optional>
11
#include <unordered_map>
12
#include <vector>
13
14
namespace WasmEdge {
15
namespace Validator {
16
17
class ComponentContext {
18
public:
19
  struct Context {
20
    const AST::Component::Component *Component;
21
    std::vector<const AST::Module *> CoreModules;
22
    std::vector<const AST::Component::Component *> ChildComponents;
23
    const Context *Parent;
24
    std::vector<uint32_t> SortIndexSizes;
25
    std::vector<uint32_t> CoreSortIndexSizes;
26
    std::unordered_map<std::string, uint32_t> TypeSubstitutions;
27
    std::unordered_map<uint32_t,
28
                       std::unordered_map<std::string_view,
29
                                          const AST::Component::ExternDesc *>>
30
        ComponentInstanceExports;
31
    std::unordered_map<uint32_t,
32
                       std::unordered_map<std::string_view, ExternalType>>
33
        CoreInstanceExports;
34
    std::unordered_map<uint32_t, const AST::Component::InstanceType *>
35
        ComponentInstanceTypes;
36
    std::unordered_map<uint32_t, const AST::Component::ResourceType *>
37
        ComponentResourceTypes;
38
39
    Context(const AST::Component::Component *C,
40
            const Context *P = nullptr) noexcept
41
0
        : Component(C), Parent(P), SortIndexSizes(static_cast<uint32_t>(
42
0
                                       AST::Component::Sort::SortType::Max)),
43
0
          CoreSortIndexSizes(
44
0
              static_cast<uint32_t>(AST::Component::Sort::CoreSortType::Max)) {}
45
46
    uint32_t
47
0
    getSortIndexSize(const AST::Component::Sort::SortType ST) const noexcept {
48
0
      return SortIndexSizes[static_cast<uint32_t>(ST)];
49
0
    }
50
51
    uint32_t getCoreSortIndexSize(
52
0
        const AST::Component::Sort::CoreSortType ST) const noexcept {
53
0
      return CoreSortIndexSizes[static_cast<uint32_t>(ST)];
54
0
    }
55
  };
56
57
0
  void reset() noexcept { CompCtxs.clear(); }
58
59
0
  void enterComponent(const AST::Component::Component &C) noexcept {
60
0
    const Context *Parent = nullptr;
61
0
    if (!CompCtxs.empty()) {
62
0
      CompCtxs.back().ChildComponents.emplace_back(&C);
63
0
      Parent = &CompCtxs.back();
64
0
    }
65
0
    CompCtxs.emplace_back(&C, Parent);
66
0
  }
67
68
0
  void exitComponent() noexcept {
69
0
    assuming(!CompCtxs.empty());
70
0
    CompCtxs.pop_back();
71
0
  }
72
73
0
  Context &getCurrentContext() noexcept {
74
0
    assuming(!CompCtxs.empty());
75
0
    return CompCtxs.back();
76
0
  }
77
78
0
  const Context &getCurrentContext() const noexcept {
79
0
    assuming(!CompCtxs.empty());
80
0
    return CompCtxs.back();
81
0
  }
82
83
0
  void addCoreModule(const AST::Module &M) noexcept {
84
0
    getCurrentContext().CoreModules.emplace_back(&M);
85
0
  }
86
87
0
  const AST::Module &getCoreModule(const uint32_t Index) const noexcept {
88
0
    return *getCurrentContext().CoreModules.at(Index);
89
0
  }
90
91
0
  uint32_t getCoreModuleCount() const noexcept {
92
0
    return static_cast<uint32_t>(getCurrentContext().CoreModules.size());
93
0
  }
94
95
  const AST::Component::Component &
96
0
  getComponent(const uint32_t Index) const noexcept {
97
0
    return *getCurrentContext().ChildComponents.at(Index);
98
0
  }
99
100
0
  uint32_t getComponentCount() const noexcept {
101
0
    return static_cast<uint32_t>(getCurrentContext().ChildComponents.size());
102
0
  }
103
104
0
  const Context *getParentContext() const noexcept {
105
0
    return getCurrentContext().Parent;
106
0
  }
107
108
  uint32_t
109
0
  getSortIndexSize(const AST::Component::Sort::SortType ST) const noexcept {
110
0
    return getCurrentContext().getSortIndexSize(ST);
111
0
  }
112
113
  uint32_t getCoreSortIndexSize(
114
0
      const AST::Component::Sort::CoreSortType ST) const noexcept {
115
0
    return getCurrentContext().getCoreSortIndexSize(ST);
116
0
  }
117
118
0
  void incSortIndexSize(const AST::Component::Sort::SortType ST) noexcept {
119
0
    getCurrentContext().SortIndexSizes[static_cast<uint32_t>(ST)]++;
120
0
  }
121
122
  void
123
0
  incCoreSortIndexSize(const AST::Component::Sort::CoreSortType ST) noexcept {
124
0
    getCurrentContext().CoreSortIndexSizes[static_cast<uint32_t>(ST)]++;
125
0
  }
126
127
  void substituteTypeImport(const std::string &ImportName,
128
0
                            const uint32_t TypeIdx) noexcept {
129
0
    getCurrentContext().TypeSubstitutions[ImportName] = TypeIdx;
130
0
  }
131
132
  std::optional<uint32_t>
133
0
  getSubstitutedType(const std::string &ImportName) const {
134
0
    const auto &Ctx = getCurrentContext();
135
0
    auto It = Ctx.TypeSubstitutions.find(ImportName);
136
0
    if (It != Ctx.TypeSubstitutions.end()) {
137
0
      return It->second;
138
0
    }
139
0
    return std::nullopt;
140
0
  }
141
142
  void addComponentInstanceExport(uint32_t InstanceIdx,
143
                                  const std::string_view &ExportName,
144
0
                                  const AST::Component::ExternDesc &ED) {
145
0
    auto &Ctx = getCurrentContext();
146
0
    Ctx.ComponentInstanceExports[InstanceIdx][ExportName] = &ED;
147
0
  }
148
149
  std::unordered_map<std::string_view, const AST::Component::ExternDesc *>
150
0
  getComponentInstanceExports(uint32_t InstanceIdx) const {
151
0
    const auto &Ctx = getCurrentContext();
152
0
    auto It = Ctx.ComponentInstanceExports.find(InstanceIdx);
153
0
    if (It != Ctx.ComponentInstanceExports.end()) {
154
0
      return It->second;
155
0
    }
156
0
    return {};
157
0
  }
158
159
0
  uint32_t getComponentInstanceExportsSize() const {
160
0
    const auto &Exports = getCurrentContext().ComponentInstanceExports;
161
0
    if (Exports.empty()) {
162
0
      return 0;
163
0
    }
164
165
0
    uint32_t MaxIdx = 0;
166
0
    for (const auto &Pair : Exports) {
167
0
      if (Pair.first > MaxIdx) {
168
0
        MaxIdx = Pair.first;
169
0
      }
170
0
    }
171
0
    return MaxIdx + 1;
172
0
  }
173
174
  void addCoreInstanceExport(uint32_t InstanceIdx,
175
                             const std::string_view &ExportName,
176
0
                             const ExternalType &ET) {
177
0
    auto &Ctx = getCurrentContext();
178
0
    Ctx.CoreInstanceExports[InstanceIdx][ExportName] = ET;
179
0
  }
180
181
  std::unordered_map<std::string_view, ExternalType>
182
0
  getCoreInstanceExports(uint32_t InstanceIdx) const {
183
0
    const auto &Ctx = getCurrentContext();
184
0
    auto It = Ctx.CoreInstanceExports.find(InstanceIdx);
185
0
    if (It != Ctx.CoreInstanceExports.end()) {
186
0
      return It->second;
187
0
    }
188
0
    return {};
189
0
  }
190
191
0
  uint32_t getCoreInstanceExportsSize() const {
192
0
    const auto &Exports = getCurrentContext().CoreInstanceExports;
193
0
    if (Exports.empty()) {
194
0
      return 0;
195
0
    }
196
197
0
    uint32_t MaxIdx = 0;
198
0
    for (const auto &Pair : Exports) {
199
0
      if (Pair.first > MaxIdx) {
200
0
        MaxIdx = Pair.first;
201
0
      }
202
0
    }
203
0
    return MaxIdx + 1;
204
0
  }
205
206
  void addComponentInstanceType(uint32_t Idx,
207
0
                                const AST::Component::InstanceType &IT) {
208
0
    auto &Ctx = getCurrentContext();
209
0
    Ctx.ComponentInstanceTypes[Idx] = &IT;
210
0
  }
211
212
  const AST::Component::InstanceType *
213
0
  getComponentInstanceType(uint32_t Idx) const {
214
0
    const auto &Ctx = getCurrentContext();
215
0
    auto It = Ctx.ComponentInstanceTypes.find(Idx);
216
0
    if (It != Ctx.ComponentInstanceTypes.end()) {
217
0
      return It->second;
218
0
    } else {
219
0
      return nullptr;
220
0
    }
221
0
  }
222
223
  void addComponentResourceType(uint32_t Idx,
224
0
                                const AST::Component::ResourceType &IT) {
225
0
    auto &Ctx = getCurrentContext();
226
0
    Ctx.ComponentResourceTypes[Idx] = &IT;
227
0
  }
228
229
  const AST::Component::ResourceType *
230
0
  getComponentResourceType(uint32_t Idx) const {
231
0
    const auto &Ctx = getCurrentContext();
232
0
    auto It = Ctx.ComponentResourceTypes.find(Idx);
233
0
234
0
    if (It != Ctx.ComponentResourceTypes.end()) {
235
0
      return It->second;
236
0
    } else {
237
0
      return nullptr;
238
0
    }
239
0
  }
240
241
private:
242
  std::deque<Context> CompCtxs;
243
};
244
245
} // namespace Validator
246
} // namespace WasmEdge