Coverage Report

Created: 2025-10-10 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/loader/ast/description.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 Import description. See "include/loader/loader.h".
10
11.6k
Expect<void> Loader::loadDesc(AST::ImportDesc &ImpDesc) {
11
11.6k
  auto ReportError = [this](auto E) {
12
78
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
13
78
  };
14
15
  // Read the module name.
16
11.6k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
17
11.6k
    ImpDesc.setModuleName(S);
18
11.6k
  }));
19
20
  // Read the external name.
21
11.6k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
22
11.6k
    ImpDesc.setExternalName(S);
23
11.6k
  }));
24
25
  // Read the external type.
26
11.6k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
27
11.6k
    ImpDesc.setExternalType(static_cast<ExternalType>(B));
28
11.6k
  }));
29
30
  // Make content node according to external type.
31
11.6k
  switch (ImpDesc.getExternalType()) {
32
2.45k
  case ExternalType::Function: {
33
    // Read the function type index.
34
2.45k
    EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
35
2.44k
      ImpDesc.setExternalFuncTypeIdx(Idx);
36
2.44k
    }));
37
2.44k
    return {};
38
2.45k
  }
39
170
  case ExternalType::Table: {
40
    // Read the table type node.
41
170
    return loadType(ImpDesc.getExternalTableType());
42
2.45k
  }
43
337
  case ExternalType::Memory: {
44
    // Read the memory type node.
45
337
    return loadType(ImpDesc.getExternalMemoryType());
46
2.45k
  }
47
8.64k
  case ExternalType::Global: {
48
    // Read the global type node.
49
8.64k
    EXPECTED_TRY(loadType(ImpDesc.getExternalGlobalType()));
50
    // Import the mutable globals are for ImportExportMutGlobals proposal.
51
8.64k
    if (ImpDesc.getExternalGlobalType().getValMut() == ValMut::Var &&
52
629
        unlikely(!Conf.hasProposal(Proposal::ImportExportMutGlobals))) {
53
0
      return logNeedProposal(ErrCode::Value::InvalidMut,
54
0
                             Proposal::ImportExportMutGlobals,
55
0
                             FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
56
0
    }
57
8.64k
    return {};
58
8.64k
  }
59
2
  case ExternalType::Tag: {
60
2
    if (!Conf.hasProposal(Proposal::ExceptionHandling)) {
61
2
      return logNeedProposal(ErrCode::Value::MalformedImportKind,
62
2
                             Proposal::ExceptionHandling, FMgr.getLastOffset(),
63
2
                             ASTNodeAttr::Module);
64
2
    }
65
    // Read the Tag type node.
66
0
    return loadType(ImpDesc.getExternalTagType());
67
2
  }
68
7
  default:
69
7
    return logLoadError(ErrCode::Value::MalformedImportKind,
70
7
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
71
11.6k
  }
72
11.6k
}
73
74
// Load binary of Export description. See "include/loader/loader.h".
75
15.2k
Expect<void> Loader::loadDesc(AST::ExportDesc &ExpDesc) {
76
15.2k
  auto ReportError = [this](auto E) {
77
41
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
78
41
  };
79
80
  // Read external name to export.
81
15.2k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
82
15.2k
    ExpDesc.setExternalName(S);
83
15.2k
  }));
84
85
  // Read external type.
86
15.2k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
87
15.2k
    ExpDesc.setExternalType(static_cast<ExternalType>(B));
88
15.2k
  }));
89
15.2k
  switch (ExpDesc.getExternalType()) {
90
13.8k
  case ExternalType::Function:
91
14.2k
  case ExternalType::Table:
92
14.9k
  case ExternalType::Memory:
93
15.2k
  case ExternalType::Global:
94
15.2k
    break;
95
3
  case ExternalType::Tag:
96
3
    if (!Conf.hasProposal(Proposal::ExceptionHandling)) {
97
3
      return logNeedProposal(ErrCode::Value::MalformedImportKind,
98
3
                             Proposal::ExceptionHandling, FMgr.getLastOffset(),
99
3
                             ASTNodeAttr::Module);
100
3
    }
101
0
    break;
102
10
  default:
103
10
    return logLoadError(ErrCode::Value::MalformedExportKind,
104
10
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
105
15.2k
  }
106
107
  // Read external index to export.
108
15.2k
  EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
109
15.2k
    ExpDesc.setExternalIndex(Idx);
110
15.2k
  }));
111
15.2k
  return {};
112
15.2k
}
113
114
} // namespace Loader
115
} // namespace WasmEdge