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