Coverage Report

Created: 2025-07-11 06:21

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