Coverage Report

Created: 2025-12-31 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/loader/ast/segment.cpp
Line
Count
Source
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 binary of TableSegment node. See "include/loader/loader.h".
10
1.76k
Expect<void> Loader::loadSegment(AST::TableSegment &TabSeg) {
11
  // Check the first byte is the reftype in table type or not.
12
1.76k
  EXPECTED_TRY(uint8_t CheckByte, FMgr.peekByte().map_error([this](auto E) {
13
1.75k
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Seg_Table);
14
1.75k
  }));
15
16
1.75k
  if (CheckByte == 0x40U) {
17
    // Table segment case is for FunctionReferences proposal.
18
42
    if (!Conf.hasProposal(Proposal::FunctionReferences)) {
19
0
      return logNeedProposal(ErrCode::Value::MalformedTable,
20
0
                             Proposal::FunctionReferences, FMgr.getLastOffset(),
21
0
                             ASTNodeAttr::Seg_Table);
22
0
    }
23
42
    FMgr.readByte();
24
25
    // Check the second byte.
26
42
    EXPECTED_TRY(uint8_t B, FMgr.readByte().map_error([this](auto E) {
27
41
      return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Seg_Table);
28
41
    }));
29
41
    if (B != 0x00U) {
30
9
      return logLoadError(ErrCode::Value::MalformedTable, FMgr.getLastOffset(),
31
9
                          ASTNodeAttr::Seg_Table);
32
9
    }
33
34
    // Read the table type.
35
32
    EXPECTED_TRY(loadType(TabSeg.getTableType()).map_error([](auto E) {
36
29
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Table));
37
29
      return E;
38
29
    }));
39
40
    // Read the expression.
41
29
    EXPECTED_TRY(loadExpression(TabSeg.getExpr()).map_error([](auto E) {
42
29
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Table));
43
29
      return E;
44
29
    }));
45
1.71k
  } else {
46
    // The table type case.
47
1.71k
    EXPECTED_TRY(loadType(TabSeg.getTableType()).map_error([](auto E) {
48
1.71k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Table));
49
1.71k
      return E;
50
1.71k
    }));
51
1.71k
  }
52
53
1.70k
  return {};
54
1.75k
}
55
56
// Load binary of GlobalSegment node. See "include/loader/loader.h".
57
1.08k
Expect<void> Loader::loadSegment(AST::GlobalSegment &GlobSeg) {
58
1.08k
  return Expect<void>{}
59
1.08k
      .and_then([this, &GlobSeg]() {
60
        // Read global type node.
61
1.08k
        return loadType(GlobSeg.getGlobalType());
62
1.08k
      })
63
1.08k
      .and_then([this, &GlobSeg]() {
64
        // Read the expression.
65
1.05k
        return loadExpression(GlobSeg.getExpr());
66
1.05k
      })
67
1.08k
      .map_error([](auto E) {
68
298
        spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Global));
69
298
        return E;
70
298
      });
71
1.08k
}
72
73
// Load binary of ElementSegment node. See "include/loader/loader.h".
74
9.89k
Expect<void> Loader::loadSegment(AST::ElementSegment &ElemSeg) {
75
9.89k
  auto ReportError = [this](auto E) {
76
64
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Seg_Element);
77
64
  };
78
79
  // Element segment binary format:
80
  // ---------------------------------------------------------------------------
81
  //  Mode | TableIdx | OffExpr | ElemKind | RefType | vec(FuncIdx) | vec(expr)
82
  // ------|----------|---------|----------|---------|--------------|-----------
83
  //    0  |          |    v    |          |         |       v      |
84
  //    1  |          |         |    v     |         |       v      |
85
  //    2  |    v     |    v    |    v     |         |       v      |
86
  //    3  |          |         |    v     |         |       v      |
87
  //    4  |          |    v    |          |         |              |     v
88
  //    5  |          |         |          |    v    |              |     v
89
  //    6  |    v     |    v    |          |    v    |              |     v
90
  //    7  |          |         |          |    v    |              |     v
91
  // ---------------------------------------------------------------------------
92
  // Mode: element initial integer, u32
93
  // TableIdx: target table index, u32
94
  // OffExpr: init offset expression, expr
95
  // ElemKind: byte 0x00, ref.func
96
  // RefType: reference type, RefType
97
  // vec(FuncIdx): function index vector, vec(u32)
98
  // vec(expr): reference init list, vec(expr)
99
100
  // Read the checking byte.
101
9.89k
  uint32_t Check = 0;
102
9.89k
  if (unlikely(!Conf.hasProposal(Proposal::BulkMemoryOperations) &&
103
0
               !Conf.hasProposal(Proposal::ReferenceTypes))) {
104
    // Legacy for BulkMemoryOperations and ReferenceTypes proposals turned off.
105
    // Element segment binary format: TableIdx + OffExpr + vec(FuncIdx)
106
0
    EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](auto Idx) {
107
0
      ElemSeg.setIdx(Idx);
108
0
    }));
109
9.89k
  } else {
110
9.89k
    EXPECTED_TRY(Check, FMgr.readU32().map_error(ReportError));
111
9.87k
  }
112
113
  // Check the prefix byte.
114
9.87k
  switch (Check) {
115
4.47k
  case 0x00:
116
6.72k
  case 0x02:
117
8.61k
  case 0x04:
118
8.79k
  case 0x06:
119
8.79k
    ElemSeg.setMode(AST::ElementSegment::ElemMode::Active);
120
8.79k
    break;
121
122
583
  case 0x01:
123
674
  case 0x05:
124
674
    ElemSeg.setMode(AST::ElementSegment::ElemMode::Passive);
125
674
    break;
126
127
236
  case 0x03:
128
370
  case 0x07:
129
370
    ElemSeg.setMode(AST::ElementSegment::ElemMode::Declarative);
130
370
    break;
131
132
41
  default:
133
    // TODO: Correctness the error code once there's spec test.
134
41
    return logLoadError(ErrCode::Value::IllegalGrammar, FMgr.getLastOffset(),
135
41
                        ASTNodeAttr::Seg_Element);
136
9.87k
  }
137
138
  // Read the table index.
139
9.83k
  ElemSeg.setIdx(0);
140
9.83k
  switch (Check) {
141
2.25k
  case 0x02:
142
2.43k
  case 0x06:
143
2.43k
    EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](auto Idx) {
144
2.43k
      ElemSeg.setIdx(Idx);
145
2.43k
    }));
146
2.43k
    break;
147
148
7.40k
  default:
149
7.40k
    break;
150
9.83k
  }
151
152
  // Read the expression.
153
9.83k
  switch (Check) {
154
4.47k
  case 0x00:
155
6.72k
  case 0x02:
156
8.60k
  case 0x04:
157
8.79k
  case 0x06:
158
8.79k
    EXPECTED_TRY(loadExpression(ElemSeg.getExpr()).map_error([](auto E) {
159
7.95k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Element));
160
7.95k
      return E;
161
7.95k
    }));
162
7.95k
    break;
163
164
7.95k
  default:
165
1.04k
    break;
166
9.83k
  }
167
168
  // Read element kind and init function indices.
169
8.99k
  switch (Check) {
170
583
  case 0x01:
171
2.81k
  case 0x02:
172
3.05k
  case 0x03:
173
3.05k
    EXPECTED_TRY(FMgr.readByte()
174
3.04k
                     .and_then([&](auto B) -> Expect<void> {
175
3.04k
                       if (B != 0x00U) {
176
3.04k
                         return Unexpect(ErrCode::Value::ExpectedZeroByte);
177
3.04k
                       };
178
3.04k
                       return {};
179
3.04k
                     })
180
3.04k
                     .map_error(ReportError));
181
3.04k
    [[fallthrough]];
182
183
6.95k
  case 0x00: {
184
6.95k
    EXPECTED_TRY(uint32_t VecCnt, loadVecCnt().map_error(ReportError));
185
17.0k
    for (uint32_t I = 0; I < VecCnt; ++I) {
186
      // For each element in vec(funcidx), make expr(ref.func idx end).
187
10.1k
      ElemSeg.getInitExprs().emplace_back();
188
10.1k
      AST::Instruction RefFunc(OpCode::Ref__func);
189
10.1k
      AST::Instruction End(OpCode::End);
190
10.1k
      EXPECTED_TRY(loadInstruction(RefFunc).map_error([](auto E) {
191
10.1k
        spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Element));
192
10.1k
        return E;
193
10.1k
      }));
194
10.1k
      ElemSeg.getInitExprs().back().getInstrs().emplace_back(
195
10.1k
          std::move(RefFunc));
196
10.1k
      ElemSeg.getInitExprs().back().getInstrs().emplace_back(std::move(End));
197
10.1k
    }
198
6.90k
    break;
199
6.91k
  }
200
6.90k
  default:
201
2.03k
    break;
202
8.99k
  }
203
204
  // Set the default reference type.
205
8.93k
  if (Check == 0x04) {
206
1.66k
    ElemSeg.setRefType(TypeCode::FuncRef);
207
7.27k
  } else {
208
7.27k
    ElemSeg.setRefType(ValType(TypeCode::Ref, TypeCode::FuncRef));
209
7.27k
  }
210
211
  // Read the reference type and init expressions.
212
8.93k
  switch (Check) {
213
91
  case 0x05:
214
236
  case 0x06:
215
370
  case 0x07: {
216
    // The AST node information is handled.
217
370
    EXPECTED_TRY(auto Type, loadRefType(ASTNodeAttr::Seg_Element));
218
366
    ElemSeg.setRefType(Type);
219
366
    [[fallthrough]];
220
366
  }
221
2.03k
  case 0x04: {
222
2.03k
    return loadVec<AST::ElementSegment>(
223
4.60k
        ElemSeg.getInitExprs(), [this](AST::Expression &Expr) -> Expect<void> {
224
4.60k
          return loadExpression(Expr);
225
4.60k
        });
226
366
  }
227
228
6.90k
  default:
229
6.90k
    break;
230
8.93k
  }
231
232
6.90k
  return {};
233
8.93k
}
234
235
// Load binary of CodeSegment node. See "include/loader/loader.h".
236
20.7k
Expect<void> Loader::loadSegment(AST::CodeSegment &CodeSeg) {
237
20.7k
  auto ReportError = [this](auto E) {
238
31
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Seg_Code);
239
31
  };
240
241
  // Read the code segment size.
242
20.7k
  EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](auto S) {
243
20.7k
    CodeSeg.setSegSize(S);
244
20.7k
  }));
245
20.7k
  auto ExprSizeBound = FMgr.getOffset() + CodeSeg.getSegSize();
246
247
  // Read the vector of local variable counts and types.
248
20.7k
  EXPECTED_TRY(uint32_t VecCnt, loadVecCnt().map_error(ReportError));
249
20.7k
  CodeSeg.getLocals().clear();
250
20.7k
  CodeSeg.getLocals().reserve(VecCnt);
251
20.7k
  uint32_t TotalLocalCnt = 0;
252
24.9k
  for (uint32_t I = 0; I < VecCnt; ++I) {
253
4.25k
    EXPECTED_TRY(uint32_t LocalCnt, FMgr.readU32().map_error(ReportError));
254
    // Total local variables should not more than 2^32. Capped at 2^26.
255
4.24k
    if (UINT32_C(67108864) - TotalLocalCnt < LocalCnt) {
256
7
      return logLoadError(ErrCode::Value::TooManyLocals, FMgr.getLastOffset(),
257
7
                          ASTNodeAttr::Seg_Code);
258
7
    }
259
4.23k
    TotalLocalCnt += LocalCnt;
260
    // Read the value type.
261
    // The AST node information is handled.
262
4.23k
    EXPECTED_TRY(ValType LocalType, loadValType(ASTNodeAttr::Seg_Code));
263
4.20k
    CodeSeg.getLocals().push_back(std::make_pair(LocalCnt, LocalType));
264
4.20k
  }
265
266
20.7k
  if (!Conf.getRuntimeConfigure().isForceInterpreter() &&
267
0
      WASMType != InputType::WASM) {
268
    // For the AOT mode and not force interpreter in configure, skip the
269
    // function body.
270
0
    FMgr.seek(ExprSizeBound);
271
20.7k
  } else {
272
    // Read function body with expected expression size.
273
20.7k
    EXPECTED_TRY(
274
20.7k
        loadExpression(CodeSeg.getExpr(), ExprSizeBound).map_error([](auto E) {
275
20.7k
          spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Code));
276
20.7k
          return E;
277
20.7k
        }));
278
20.7k
  }
279
280
19.7k
  return {};
281
20.7k
}
282
283
// Load binary of DataSegment node. See "include/loader/loader.h".
284
4.47k
Expect<void> Loader::loadSegment(AST::DataSegment &DataSeg) {
285
4.47k
  auto ReportError = [this](auto E) {
286
28
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Seg_Data);
287
28
  };
288
4.47k
  DataSeg.setMode(AST::DataSegment::DataMode::Passive);
289
4.47k
  DataSeg.setIdx(0);
290
291
  // Data segment binary format:
292
  // ----------------------------------------
293
  //  Mode | MemoryIdx | OffExpr | vec(byte)
294
  // ------|-----------|---------|-----------
295
  //    0  |           |    v    |     v
296
  //    1  |           |         |     v
297
  //    2  |     v     |    v    |     v
298
  // ----------------------------------------
299
  // Mode: data initial integer, u32
300
  // MemoryIdx: target memory index, u32
301
  // OffExpr: init offset expression, expr
302
  // vec(byte): init data, vec(u8)
303
304
  // Read the checking byte.
305
4.47k
  EXPECTED_TRY(uint32_t Check, FMgr.readU32().map_error(ReportError));
306
  // Check > 0 cases are for BulkMemoryOperations or ReferenceTypes proposal.
307
4.46k
  if (Check > 0 && !Conf.hasProposal(Proposal::BulkMemoryOperations) &&
308
0
      !Conf.hasProposal(Proposal::ReferenceTypes)) {
309
0
    return logNeedProposal(ErrCode::Value::ExpectedZeroByte,
310
0
                           Proposal::BulkMemoryOperations, FMgr.getLastOffset(),
311
0
                           ASTNodeAttr::Seg_Data);
312
0
  }
313
314
4.46k
  switch (Check) {
315
891
  case 0x02: // 0x02 memidx expr vec(byte) , Active
316
    // Read target memory index.
317
891
    EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](auto Idx) {
318
890
      DataSeg.setIdx(Idx);
319
890
    }));
320
890
    [[fallthrough]];
321
322
3.11k
  case 0x00: // 0x00 expr vec(byte) , Active
323
    // Read the offset expression.
324
3.11k
    EXPECTED_TRY(loadExpression(DataSeg.getExpr()).map_error([](auto E) {
325
1.85k
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Data));
326
1.85k
      return E;
327
1.85k
    }));
328
1.85k
    DataSeg.setMode(AST::DataSegment::DataMode::Active);
329
1.85k
    [[fallthrough]];
330
331
3.17k
  case 0x01: // 0x01 vec(byte) , Passive
332
3.17k
  {
333
    // Read initialization data.
334
3.17k
    EXPECTED_TRY(uint32_t VecCnt, loadVecCnt().map_error(ReportError));
335
3.16k
    EXPECTED_TRY(FMgr.readBytes(VecCnt).map_error(ReportError).map([&](auto V) {
336
3.15k
      DataSeg.getData() = std::move(V);
337
3.15k
    }));
338
3.15k
    break;
339
3.16k
  }
340
3.15k
  default:
341
    // TODO: Correctness the error code once there's spec test.
342
31
    return logLoadError(ErrCode::Value::IllegalGrammar, FMgr.getLastOffset(),
343
31
                        ASTNodeAttr::Seg_Data);
344
4.46k
  }
345
3.15k
  return {};
346
4.46k
}
347
348
} // namespace Loader
349
} // namespace WasmEdge