Coverage Report

Created: 2026-08-08 06:32

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