Coverage Report

Created: 2026-09-28 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/loader/serialize/serial_section.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
#include "loader/serialize.h"
5
6
namespace WasmEdge {
7
namespace Loader {
8
9
// Serialize custom section. See "include/loader/serialize.h".
10
void Serializer::serializeSection(const AST::CustomSection &Sec,
11
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
12
  // Custom section: 0x00 + size:u32 + name:vec(byte) + content:byte*.
13
  // Section ID.
14
0
  OutVec.push_back(0x00U);
15
0
  auto OrgSize = OutVec.size();
16
  // Name: vec(byte).
17
0
  serializeU32(static_cast<uint32_t>(Sec.getName().size()), OutVec);
18
0
  OutVec.insert(OutVec.end(), Sec.getName().begin(), Sec.getName().end());
19
  // Content: byte*.
20
0
  OutVec.insert(OutVec.end(), Sec.getContent().begin(), Sec.getContent().end());
21
  // Backward insert the section size.
22
0
  serializeU32(static_cast<uint32_t>(OutVec.size() - OrgSize), OutVec,
23
0
               std::next(OutVec.begin(), static_cast<ptrdiff_t>(OrgSize)));
24
0
}
25
26
// Serialize type section. See "include/loader/serialize.h".
27
void Serializer::serializeSection(const AST::TypeSection &Sec,
28
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
29
  // Type section: 0x01 + size:u32 + content:vec(rectype).
30
0
  auto STypes = Sec.getContent();
31
32
0
  if (STypes.size() > 0) {
33
    // Section ID.
34
0
    OutVec.push_back(0x01U);
35
0
    auto OrgSize = OutVec.size();
36
0
    uint32_t RecCnt = 0;
37
    // Content: vec(rectype).
38
0
    for (uint32_t I = 0; I < STypes.size(); I++) {
39
0
      auto RecInfo = STypes[I].getRecursiveInfo();
40
0
      if (!RecInfo.has_value()) {
41
0
        RecCnt++;
42
0
      } else if (RecInfo->Index == 0) {
43
        // First element of a recursive type group.
44
0
        OutVec.push_back(static_cast<uint8_t>(TypeCode::Rec));
45
0
        serializeU32(RecInfo->RecTypeSize, OutVec);
46
0
        RecCnt++;
47
0
      }
48
0
      serializeType(STypes[I], OutVec);
49
0
    }
50
    // Backward insert the recursive type vector size, then the section size in
51
    // front of it.
52
0
    serializeU32(RecCnt, OutVec,
53
0
                 std::next(OutVec.begin(), static_cast<ptrdiff_t>(OrgSize)));
54
0
    serializeU32(static_cast<uint32_t>(OutVec.size() - OrgSize), OutVec,
55
0
                 std::next(OutVec.begin(), static_cast<ptrdiff_t>(OrgSize)));
56
0
  }
57
0
}
58
59
// Serialize import section. See "include/loader/serialize.h".
60
void Serializer::serializeSection(const AST::ImportSection &Sec,
61
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
62
  // Import section: 0x02 + size:u32 + content:vec(importdesc).
63
0
  serializeSectionContent(
64
0
      Sec, 0x02U, OutVec,
65
0
      [this](const AST::ImportDesc &R, std::vector<uint8_t> &V) {
66
0
        serializeDesc(R, V);
67
0
      });
68
0
}
69
70
// Serialize function section. See "include/loader/serialize.h".
71
void Serializer::serializeSection(const AST::FunctionSection &Sec,
72
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
73
  // Function section: 0x03 + size:u32 + content:vec(u32).
74
0
  serializeSectionContent(Sec, 0x03U, OutVec,
75
0
                          [this](const uint32_t &R, std::vector<uint8_t> &V) {
76
0
                            serializeU32(R, V);
77
0
                          });
78
0
}
79
80
// Serialize table section. See "include/loader/serialize.h".
81
void Serializer::serializeSection(const AST::TableSection &Sec,
82
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
83
  // Table section: 0x04 + size:u32 + content:vec(table).
84
0
  serializeSectionContent(
85
0
      Sec, 0x04U, OutVec,
86
0
      [this](const AST::TableSegment &R, std::vector<uint8_t> &V) {
87
0
        serializeSegment(R, V);
88
0
      });
89
0
}
90
91
// Serialize memory section. See "include/loader/serialize.h".
92
void Serializer::serializeSection(const AST::MemorySection &Sec,
93
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
94
  // Memory section: 0x05 + size:u32 + content:vec(memorytype).
95
0
  serializeSectionContent(
96
0
      Sec, 0x05U, OutVec,
97
0
      [this](const AST::MemoryType &R, std::vector<uint8_t> &V) {
98
0
        serializeType(R, V);
99
0
      });
100
0
}
101
102
// Serialize global section. See "include/loader/serialize.h".
103
void Serializer::serializeSection(const AST::GlobalSection &Sec,
104
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
105
  // Global section: 0x06 + size:u32 + content:vec(global).
106
0
  serializeSectionContent(
107
0
      Sec, 0x06U, OutVec,
108
0
      [this](const AST::GlobalSegment &R, std::vector<uint8_t> &V) {
109
0
        serializeSegment(R, V);
110
0
      });
111
0
}
112
113
// Serialize export section. See "include/loader/serialize.h".
114
void Serializer::serializeSection(const AST::ExportSection &Sec,
115
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
116
  // Export section: 0x07 + size:u32 + content:vec(exportdesc).
117
0
  serializeSectionContent(
118
0
      Sec, 0x07U, OutVec,
119
0
      [this](const AST::ExportDesc &R, std::vector<uint8_t> &V) {
120
0
        serializeDesc(R, V);
121
0
      });
122
0
}
123
124
// Serialize start section. See "include/loader/serialize.h".
125
void Serializer::serializeSection(const AST::StartSection &Sec,
126
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
127
  // Start section: 0x08 + size:u32 + idx:u32.
128
0
  if (Sec.getContent()) {
129
    // Section ID.
130
0
    OutVec.push_back(0x08U);
131
0
    auto OrgSize = OutVec.size();
132
    // Idx: u32.
133
0
    serializeU32(*Sec.getContent(), OutVec);
134
    // Backward insert the section size.
135
0
    serializeU32(static_cast<uint32_t>(OutVec.size() - OrgSize), OutVec,
136
0
                 std::next(OutVec.begin(), static_cast<ptrdiff_t>(OrgSize)));
137
0
  }
138
0
}
139
140
// Serialize element section. See "include/loader/serialize.h".
141
void Serializer::serializeSection(const AST::ElementSection &Sec,
142
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
143
  // Element section: 0x09 + size:u32 + content:vec(elemseg).
144
0
  serializeSectionContent(
145
0
      Sec, 0x09U, OutVec,
146
0
      [this](const AST::ElementSegment &R, std::vector<uint8_t> &V) {
147
0
        serializeSegment(R, V);
148
0
      });
149
0
}
150
151
// Serialize code section. See "include/loader/serialize.h".
152
void Serializer::serializeSection(const AST::CodeSection &Sec,
153
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
154
  // Code section: 0x0A + size:u32 + content:vec(codeseg).
155
0
  serializeSectionContent(
156
0
      Sec, 0x0AU, OutVec,
157
0
      [this](const AST::CodeSegment &R, std::vector<uint8_t> &V) {
158
0
        serializeSegment(R, V);
159
0
      });
160
0
}
161
162
// Serialize data section. See "include/loader/serialize.h".
163
void Serializer::serializeSection(const AST::DataSection &Sec,
164
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
165
  // Data section: 0x0B + size:u32 + content:vec(dataseg).
166
0
  serializeSectionContent(
167
0
      Sec, 0x0BU, OutVec,
168
0
      [this](const AST::DataSegment &R, std::vector<uint8_t> &V) {
169
0
        serializeSegment(R, V);
170
0
      });
171
0
}
172
173
// Serialize datacount section. See "include/loader/serialize.h".
174
void Serializer::serializeSection(const AST::DataCountSection &Sec,
175
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
176
  // Datacount section: 0x0C + size:u32 + idx:u32.
177
0
  if (Sec.getContent()) {
178
    // Section ID.
179
0
    OutVec.push_back(0x0CU);
180
0
    auto OrgSize = OutVec.size();
181
    // Idx: u32.
182
0
    serializeU32(*Sec.getContent(), OutVec);
183
    // Backward insert the section size.
184
0
    serializeU32(static_cast<uint32_t>(OutVec.size() - OrgSize), OutVec,
185
0
                 std::next(OutVec.begin(), static_cast<ptrdiff_t>(OrgSize)));
186
0
  }
187
0
}
188
189
// Serialize tag section. See "include/loader/serialize.h".
190
void Serializer::serializeSection(const AST::TagSection &Sec,
191
0
                                  std::vector<uint8_t> &OutVec) const noexcept {
192
  // Tag section: 0x0D + size:u32 + content:vec(tagtype)
193
0
  serializeSectionContent(
194
0
      Sec, 0x0DU, OutVec,
195
0
      [this](const AST::TagType &R, std::vector<uint8_t> &V) {
196
0
        serializeType(R, V);
197
0
      });
198
0
}
199
200
} // namespace Loader
201
} // namespace WasmEdge