Coverage Report

Created: 2025-07-01 06:18

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