Coverage Report

Created: 2026-11-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/loader/ast/component/component_section.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
#include "loader/loader.h"
5
6
namespace WasmEdge {
7
namespace Loader {
8
9
// Load component core:module section. See "include/loader/loader.h".
10
12.1k
Expect<void> Loader::loadSection(AST::Component::CoreModuleSection &Sec) {
11
12.1k
  return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> {
12
12.0k
    auto ReportError = [](auto E) {
13
110
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_CoreMod));
14
110
      return E;
15
110
    };
16
12.0k
    auto ExpectedSize = Sec.getContentSize();
17
12.0k
    auto StartOffset = FMgr.getOffset();
18
19
12.0k
    EXPECTED_TRY(auto Preamble, Loader::loadPreamble().map_error(ReportError));
20
12.0k
    auto &[WasmMagic, Ver] = Preamble;
21
12.0k
    if (unlikely(Ver != ModuleVersion)) {
22
3
      return logLoadError(ErrCode::Value::MalformedVersion,
23
3
                          FMgr.getLastOffset(), ASTNodeAttr::Comp_Sec_CoreMod);
24
3
    }
25
12.0k
    AST::Module &CoreMod = Sec.getContent();
26
12.0k
    CoreMod.getMagic() = WasmMagic;
27
12.0k
    CoreMod.getVersion() = Ver;
28
29
12.0k
    auto Offset = FMgr.getOffset();
30
12.0k
    if (unlikely(ExpectedSize < Offset - StartOffset)) {
31
4
      return logLoadError(ErrCode::Value::UnexpectedEnd, FMgr.getLastOffset(),
32
4
                          ASTNodeAttr::Comp_Sec_CoreMod);
33
4
    }
34
35
12.0k
    EXPECTED_TRY(loadModule(CoreMod, ExpectedSize - (Offset - StartOffset))
36
11.9k
                     .map_error(ReportError));
37
11.9k
    return {};
38
12.0k
  });
39
12.1k
}
40
41
// Load component core:instance section. See "include/loader/loader.h".
42
133k
Expect<void> Loader::loadSection(AST::Component::CoreInstanceSection &Sec) {
43
133k
  return loadSectionContent(Sec, [this, &Sec]() {
44
133k
    return loadSectionContentVec(
45
273k
        Sec, [this](AST::Component::CoreInstance &Instance) {
46
273k
          return loadInstance(Instance);
47
273k
        });
48
133k
  });
49
133k
}
50
51
// Load component core:type section. See "include/loader/loader.h".
52
269k
Expect<void> Loader::loadSection(AST::Component::CoreTypeSection &Sec) {
53
269k
  return loadSectionContent(Sec, [this, &Sec]() {
54
268k
    return loadSectionContentVec(
55
268k
        Sec, [this](AST::Component::CoreDefType &Ty) { return loadType(Ty); });
56
268k
  });
57
269k
}
58
59
// Load component nested-component section. See "include/loader/loader.h".
60
83.8k
Expect<void> Loader::loadSection(AST::Component::ComponentSection &Sec) {
61
83.8k
  return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> {
62
83.7k
    auto ReportError = [](auto E) {
63
1.14k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_Component));
64
1.14k
      return E;
65
1.14k
    };
66
83.7k
    auto ExpectedSize = Sec.getContentSize();
67
83.7k
    auto StartOffset = FMgr.getOffset();
68
69
83.7k
    EXPECTED_TRY(auto Preamble, loadPreamble().map_error(ReportError));
70
83.7k
    auto &[WasmMagic, Ver] = Preamble;
71
83.7k
    if (unlikely(Ver != ComponentVersion)) {
72
6
      return logLoadError(ErrCode::Value::MalformedVersion,
73
6
                          FMgr.getLastOffset(),
74
6
                          ASTNodeAttr::Comp_Sec_Component);
75
6
    }
76
83.7k
    auto NestedComp = std::make_unique<AST::Component::Component>();
77
83.7k
    NestedComp->getMagic() = WasmMagic;
78
83.7k
    NestedComp->getVersion() = {Ver[0], Ver[1]};
79
83.7k
    NestedComp->getLayer() = {Ver[2], Ver[3]};
80
81
83.7k
    auto Offset = FMgr.getOffset();
82
83.7k
    if (unlikely(ExpectedSize < Offset - StartOffset)) {
83
1
      return logLoadError(ErrCode::Value::UnexpectedEnd, FMgr.getLastOffset(),
84
1
                          ASTNodeAttr::Component);
85
1
    }
86
87
83.7k
    EXPECTED_TRY(
88
82.6k
        loadComponent(*NestedComp, ExpectedSize - (Offset - StartOffset))
89
82.6k
            .map_error(ReportError));
90
82.6k
    Sec.getContent() = std::move(NestedComp);
91
82.6k
    return {};
92
83.7k
  });
93
83.8k
}
94
95
// Load component instance section. See "include/loader/loader.h".
96
2.28M
Expect<void> Loader::loadSection(AST::Component::InstanceSection &Sec) {
97
2.28M
  return loadSectionContent(Sec, [this, &Sec]() {
98
2.28M
    return loadSectionContentVec(Sec,
99
2.28M
                                 [this](AST::Component::Instance &Instance) {
100
2.22M
                                   return loadInstance(Instance);
101
2.22M
                                 });
102
2.28M
  });
103
2.28M
}
104
105
// Load component alias section. See "include/loader/loader.h".
106
85.2k
Expect<void> Loader::loadSection(AST::Component::AliasSection &Sec) {
107
85.2k
  return loadSectionContent(Sec, [this, &Sec]() {
108
85.2k
    return loadSectionContentVec(
109
87.8k
        Sec, [this](AST::Component::Alias &Alias) { return loadAlias(Alias); });
110
85.2k
  });
111
85.2k
}
112
113
// Load component type section. See "include/loader/loader.h".
114
6.59M
Expect<void> Loader::loadSection(AST::Component::TypeSection &Sec) {
115
6.59M
  return loadSectionContent(Sec, [this, &Sec]() {
116
6.59M
    return loadSectionContentVec(
117
6.59M
        Sec, [this](AST::Component::DefType &Ty) { return loadType(Ty); });
118
6.59M
  });
119
6.59M
}
120
121
// Load component cannon section. See "include/loader/loader.h".
122
287k
Expect<void> Loader::loadSection(AST::Component::CanonSection &Sec) {
123
287k
  return loadSectionContent(Sec, [this, &Sec]() {
124
287k
    return loadSectionContentVec(
125
301k
        Sec, [this](AST::Component::Canonical &C) { return loadCanonical(C); });
126
287k
  });
127
287k
}
128
129
// Load component start section. See "include/loader/loader.h".
130
1.22k
Expect<void> Loader::loadSection(AST::Component::StartSection &Sec) {
131
1.22k
  return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> {
132
1.19k
    return loadStart(Sec.getContent()).map_error([](auto E) {
133
59
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_Start));
134
59
      return E;
135
59
    });
136
1.19k
  });
137
1.22k
}
138
139
// Load component import section. See "include/loader/loader.h".
140
115k
Expect<void> Loader::loadSection(AST::Component::ImportSection &Sec) {
141
115k
  return loadSectionContent(Sec, [this, &Sec]() {
142
115k
    return loadSectionContentVec(
143
115k
        Sec, [this](AST::Component::Import &C) { return loadImport(C); });
144
115k
  });
145
115k
}
146
147
// Load component export section. See "include/loader/loader.h".
148
17.5k
Expect<void> Loader::loadSection(AST::Component::ExportSection &Sec) {
149
17.5k
  return loadSectionContent(Sec, [this, &Sec]() {
150
17.5k
    return loadSectionContentVec(
151
17.5k
        Sec, [this](AST::Component::Export &C) { return loadExport(C); });
152
17.5k
  });
153
17.5k
}
154
155
// Load component value section. See "include/loader/loader.h".
156
16.0k
Expect<void> Loader::loadSection(AST::Component::ValueSection &Sec) {
157
16.0k
  return loadSectionContent(Sec, [this, &Sec]() {
158
16.0k
    return loadSectionContentVec(
159
52.3k
        Sec, [this](AST::Component::Value &C) { return loadValue(C); });
160
16.0k
  });
161
16.0k
}
162
163
} // namespace Loader
164
} // namespace WasmEdge