Coverage Report

Created: 2026-09-28 06:22

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
8.23k
Expect<void> Loader::loadSection(AST::Component::CoreModuleSection &Sec) {
11
8.23k
  return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> {
12
8.16k
    auto ReportError = [](auto E) {
13
118
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_CoreMod));
14
118
      return E;
15
118
    };
16
8.16k
    auto ExpectedSize = Sec.getContentSize();
17
8.16k
    auto StartOffset = FMgr.getOffset();
18
19
8.16k
    EXPECTED_TRY(auto Preamble, Loader::loadPreamble().map_error(ReportError));
20
8.13k
    auto &[WasmMagic, Ver] = Preamble;
21
8.13k
    if (unlikely(Ver != ModuleVersion)) {
22
7
      return logLoadError(ErrCode::Value::MalformedVersion,
23
7
                          FMgr.getLastOffset(), ASTNodeAttr::Comp_Sec_CoreMod);
24
7
    }
25
8.13k
    AST::Module &CoreMod = Sec.getContent();
26
8.13k
    CoreMod.getMagic() = WasmMagic;
27
8.13k
    CoreMod.getVersion() = Ver;
28
29
8.13k
    auto Offset = FMgr.getOffset();
30
8.13k
    if (unlikely(ExpectedSize < Offset - StartOffset)) {
31
5
      return logLoadError(ErrCode::Value::UnexpectedEnd, FMgr.getLastOffset(),
32
5
                          ASTNodeAttr::Comp_Sec_CoreMod);
33
5
    }
34
35
8.12k
    EXPECTED_TRY(loadModule(CoreMod, ExpectedSize - (Offset - StartOffset))
36
8.03k
                     .map_error(ReportError));
37
8.03k
    return {};
38
8.12k
  });
39
8.23k
}
40
41
// Load component core:instance section. See "include/loader/loader.h".
42
89.4k
Expect<void> Loader::loadSection(AST::Component::CoreInstanceSection &Sec) {
43
89.4k
  return loadSectionContent(Sec, [this, &Sec]() {
44
89.4k
    return loadSectionContentVec(
45
484k
        Sec, [this](AST::Component::CoreInstance &Instance) {
46
484k
          return loadInstance(Instance);
47
484k
        });
48
89.4k
  });
49
89.4k
}
50
51
// Load component core:type section. See "include/loader/loader.h".
52
180k
Expect<void> Loader::loadSection(AST::Component::CoreTypeSection &Sec) {
53
180k
  return loadSectionContent(Sec, [this, &Sec]() {
54
180k
    return loadSectionContentVec(
55
180k
        Sec, [this](AST::Component::CoreDefType &Ty) { return loadType(Ty); });
56
180k
  });
57
180k
}
58
59
// Load component nested-component section. See "include/loader/loader.h".
60
102k
Expect<void> Loader::loadSection(AST::Component::ComponentSection &Sec) {
61
102k
  return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> {
62
102k
    auto ReportError = [](auto E) {
63
1.43k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_Component));
64
1.43k
      return E;
65
1.43k
    };
66
102k
    auto ExpectedSize = Sec.getContentSize();
67
102k
    auto StartOffset = FMgr.getOffset();
68
69
102k
    EXPECTED_TRY(auto Preamble, loadPreamble().map_error(ReportError));
70
102k
    auto &[WasmMagic, Ver] = Preamble;
71
102k
    if (unlikely(Ver != ComponentVersion)) {
72
9
      return logLoadError(ErrCode::Value::MalformedVersion,
73
9
                          FMgr.getLastOffset(),
74
9
                          ASTNodeAttr::Comp_Sec_Component);
75
9
    }
76
102k
    auto NestedComp = std::make_unique<AST::Component::Component>();
77
102k
    NestedComp->getMagic() = WasmMagic;
78
102k
    NestedComp->getVersion() = {Ver[0], Ver[1]};
79
102k
    NestedComp->getLayer() = {Ver[2], Ver[3]};
80
81
102k
    auto Offset = FMgr.getOffset();
82
102k
    if (unlikely(ExpectedSize < Offset - StartOffset)) {
83
1
      return logLoadError(ErrCode::Value::UnexpectedEnd, FMgr.getLastOffset(),
84
1
                          ASTNodeAttr::Component);
85
1
    }
86
87
102k
    EXPECTED_TRY(
88
100k
        loadComponent(*NestedComp, ExpectedSize - (Offset - StartOffset))
89
100k
            .map_error(ReportError));
90
100k
    Sec.getContent() = std::move(NestedComp);
91
100k
    return {};
92
102k
  });
93
102k
}
94
95
// Load component instance section. See "include/loader/loader.h".
96
1.50M
Expect<void> Loader::loadSection(AST::Component::InstanceSection &Sec) {
97
1.50M
  return loadSectionContent(Sec, [this, &Sec]() {
98
1.50M
    return loadSectionContentVec(Sec,
99
1.50M
                                 [this](AST::Component::Instance &Instance) {
100
1.45M
                                   return loadInstance(Instance);
101
1.45M
                                 });
102
1.50M
  });
103
1.50M
}
104
105
// Load component alias section. See "include/loader/loader.h".
106
6.34k
Expect<void> Loader::loadSection(AST::Component::AliasSection &Sec) {
107
6.34k
  return loadSectionContent(Sec, [this, &Sec]() {
108
6.32k
    return loadSectionContentVec(
109
6.65k
        Sec, [this](AST::Component::Alias &Alias) { return loadAlias(Alias); });
110
6.32k
  });
111
6.34k
}
112
113
// Load component type section. See "include/loader/loader.h".
114
2.62M
Expect<void> Loader::loadSection(AST::Component::TypeSection &Sec) {
115
2.62M
  return loadSectionContent(Sec, [this, &Sec]() {
116
2.62M
    return loadSectionContentVec(
117
2.62M
        Sec, [this](AST::Component::DefType &Ty) { return loadType(Ty); });
118
2.62M
  });
119
2.62M
}
120
121
// Load component cannon section. See "include/loader/loader.h".
122
56.5k
Expect<void> Loader::loadSection(AST::Component::CanonSection &Sec) {
123
56.5k
  return loadSectionContent(Sec, [this, &Sec]() {
124
56.5k
    return loadSectionContentVec(
125
68.9k
        Sec, [this](AST::Component::Canonical &C) { return loadCanonical(C); });
126
56.5k
  });
127
56.5k
}
128
129
// Load component start section. See "include/loader/loader.h".
130
1.01k
Expect<void> Loader::loadSection(AST::Component::StartSection &Sec) {
131
1.01k
  return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> {
132
981
    return loadStart(Sec.getContent()).map_error([](auto E) {
133
71
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_Start));
134
71
      return E;
135
71
    });
136
981
  });
137
1.01k
}
138
139
// Load component import section. See "include/loader/loader.h".
140
78.8k
Expect<void> Loader::loadSection(AST::Component::ImportSection &Sec) {
141
78.8k
  return loadSectionContent(Sec, [this, &Sec]() {
142
78.8k
    return loadSectionContentVec(
143
78.8k
        Sec, [this](AST::Component::Import &C) { return loadImport(C); });
144
78.8k
  });
145
78.8k
}
146
147
// Load component export section. See "include/loader/loader.h".
148
6.57k
Expect<void> Loader::loadSection(AST::Component::ExportSection &Sec) {
149
6.57k
  return loadSectionContent(Sec, [this, &Sec]() {
150
6.53k
    return loadSectionContentVec(
151
6.53k
        Sec, [this](AST::Component::Export &C) { return loadExport(C); });
152
6.53k
  });
153
6.57k
}
154
155
// Load component value section. See "include/loader/loader.h".
156
14.4k
Expect<void> Loader::loadSection(AST::Component::ValueSection &Sec) {
157
14.4k
  return loadSectionContent(Sec, [this, &Sec]() {
158
14.4k
    return loadSectionContentVec(
159
16.4k
        Sec, [this](AST::Component::Value &C) { return loadValue(C); });
160
14.4k
  });
161
14.4k
}
162
163
} // namespace Loader
164
} // namespace WasmEdge