Coverage Report

Created: 2025-07-01 06:18

/src/WasmEdge/include/common/errinfo.h
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
//===-- wasmedge/common/errinfo.h - Error information definition ----------===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the enumerations of WasmEdge error information.
12
///
13
//===----------------------------------------------------------------------===//
14
#pragma once
15
16
#include "common/enum_ast.hpp"
17
#include "common/enum_configure.hpp"
18
#include "common/enum_errcode.hpp"
19
#include "common/enum_errinfo.hpp"
20
#include "common/enum_types.hpp"
21
#include "common/filesystem.h"
22
#include "common/spdlog.h"
23
#include "common/types.h"
24
25
#include <cstdint>
26
#include <iosfwd>
27
#include <limits>
28
#include <string>
29
#include <string_view>
30
#include <vector>
31
32
namespace WasmEdge {
33
namespace ErrInfo {
34
35
/// Information structures.
36
struct InfoFile {
37
  InfoFile() = delete;
38
0
  InfoFile(const std::filesystem::path &FName) noexcept : FileName(FName) {}
39
40
  std::filesystem::path FileName;
41
};
42
43
struct InfoLoading {
44
  InfoLoading() = delete;
45
5.47k
  InfoLoading(const uint64_t Off) noexcept : Offset(Off) {}
46
47
  uint64_t Offset;
48
};
49
50
struct InfoAST {
51
  InfoAST() = delete;
52
29.3k
  InfoAST(const ASTNodeAttr Attr) noexcept : NodeAttr(Attr) {}
53
54
  ASTNodeAttr NodeAttr;
55
};
56
57
struct InfoInstanceBound {
58
  InfoInstanceBound() = delete;
59
  InfoInstanceBound(const ExternalType Inst, const uint32_t Num,
60
                    const uint32_t Lim) noexcept
61
0
      : Instance(Inst), Number(Num), Limited(Lim) {}
62
63
  ExternalType Instance;
64
  uint32_t Number, Limited;
65
};
66
67
struct InfoForbidIndex {
68
  InfoForbidIndex() = delete;
69
  InfoForbidIndex(const IndexCategory Cate, const uint32_t Idx,
70
                  const uint32_t Bound) noexcept
71
430
      : Category(Cate), Index(Idx), Boundary(Bound) {}
72
73
  IndexCategory Category;
74
  uint32_t Index, Boundary;
75
};
76
77
struct InfoExporting {
78
  InfoExporting() = delete;
79
0
  InfoExporting(std::string_view Ext) noexcept : ExtName(Ext) {}
80
81
  std::string ExtName;
82
};
83
84
struct InfoLimit {
85
  InfoLimit() = delete;
86
  InfoLimit(const bool HasMax, const uint32_t Min,
87
            const uint32_t Max = 0) noexcept
88
37
      : LimHasMax(HasMax), LimMin(Min), LimMax(Max) {}
89
90
  bool LimHasMax;
91
  uint32_t LimMin, LimMax;
92
};
93
94
struct InfoRegistering {
95
  InfoRegistering() = delete;
96
0
  InfoRegistering(std::string_view Mod) noexcept : ModName(Mod) {}
97
98
  std::string ModName;
99
};
100
101
struct InfoLinking {
102
  InfoLinking() = delete;
103
  InfoLinking(std::string_view Mod, std::string_view Ext,
104
              const ExternalType ExtT = ExternalType::Function) noexcept
105
0
      : ModName(Mod), ExtName(Ext), ExtType(ExtT) {}
106
107
  std::string ModName;
108
  std::string ExtName;
109
  ExternalType ExtType;
110
};
111
112
struct InfoExecuting {
113
  InfoExecuting() = delete;
114
  InfoExecuting(std::string_view Mod, std::string_view Func) noexcept
115
0
      : ModName(Mod), FuncName(Func) {}
116
0
  InfoExecuting(std::string_view Func) noexcept : ModName(""), FuncName(Func) {}
117
118
  std::string ModName;
119
  std::string FuncName;
120
};
121
122
struct InfoMismatch {
123
  InfoMismatch() = delete;
124
125
  /// Case 1: unexpected alignment
126
  InfoMismatch(const uint8_t ExpAlign, const uint32_t GotAlign) noexcept
127
54
      : Category(MismatchCategory::Alignment), ExpAlignment(ExpAlign),
128
54
        GotAlignment(GotAlign) {}
129
130
  /// Case 2: unexpected value type
131
  InfoMismatch(const ValType &ExpVT, const ValType &GotVT) noexcept
132
585
      : Category(MismatchCategory::ValueType), ExpValType(ExpVT),
133
585
        GotValType(GotVT) {}
134
135
  /// Case 3: unexpected value type list
136
  InfoMismatch(const std::vector<ValType> &ExpV,
137
               const std::vector<ValType> &GotV) noexcept
138
11
      : Category(MismatchCategory::ValueTypes), ExpParams(ExpV),
139
11
        GotParams(GotV) {}
140
141
  /// Case 4: unexpected mutation settings
142
  InfoMismatch(const ValMut ExpM, const ValMut GotM) noexcept
143
0
      : Category(MismatchCategory::Mutation), ExpValMut(ExpM), GotValMut(GotM) {
144
0
  }
145
146
  /// Case 5: unexpected external types
147
  InfoMismatch(const ExternalType ExpExt, const ExternalType GotExt) noexcept
148
0
      : Category(MismatchCategory::ExternalType), ExpExtType(ExpExt),
149
0
        GotExtType(GotExt) {}
150
151
  /// Case 6: unexpected function types
152
  InfoMismatch(const std::vector<ValType> &ExpP,
153
               const std::vector<ValType> &ExpR,
154
               const std::vector<ValType> &GotP,
155
               const std::vector<ValType> &GotR) noexcept
156
2
      : Category(MismatchCategory::FunctionType), ExpParams(ExpP),
157
2
        GotParams(GotP), ExpReturns(ExpR), GotReturns(GotR) {}
158
159
  /// Case 7: unexpected table types
160
  InfoMismatch(const ValType &ExpRType,
161
               // Expected reference type
162
               const bool ExpHasMax, const uint32_t ExpMin,
163
               const uint32_t ExpMax,
164
               // Expected Limit
165
               const ValType &GotRType,
166
               // Got reference type
167
               const bool GotHasMax, const uint32_t GotMin,
168
               const uint32_t GotMax
169
               // Got limit
170
               ) noexcept
171
0
      : Category(MismatchCategory::Table), ExpValType(ExpRType),
172
0
        GotValType(GotRType), ExpLimHasMax(ExpHasMax), GotLimHasMax(GotHasMax),
173
0
        ExpLimMin(ExpMin), GotLimMin(GotMin), ExpLimMax(ExpMax),
174
0
        GotLimMax(GotMax) {}
175
176
  /// Case 8: unexpected memory limits
177
  InfoMismatch(const bool ExpHasMax, const uint32_t ExpMin,
178
               const uint32_t ExpMax,
179
               // Expect Limit
180
               const bool GotHasMax, const uint32_t GotMin,
181
               const uint32_t GotMax
182
               // Got limit
183
               ) noexcept
184
0
      : Category(MismatchCategory::Memory), ExpLimHasMax(ExpHasMax),
185
0
        GotLimHasMax(GotHasMax), ExpLimMin(ExpMin), GotLimMin(GotMin),
186
0
        ExpLimMax(ExpMax), GotLimMax(GotMax) {}
187
188
  /// Case 9: unexpected global types
189
  InfoMismatch(const ValType &ExpVType, const ValMut ExpVMut,
190
               const ValType &GotVType, const ValMut GotVMut) noexcept
191
0
      : Category(MismatchCategory::Global), ExpValType(ExpVType),
192
0
        GotValType(GotVType), ExpValMut(ExpVMut), GotValMut(GotVMut) {}
193
194
  /// Case 10: unexpected version
195
  InfoMismatch(const uint32_t ExpV, const uint32_t GotV) noexcept
196
0
      : Category(MismatchCategory::Version), ExpVersion(ExpV),
197
0
        GotVersion(GotV) {}
198
199
  /// Mismatched category
200
  MismatchCategory Category;
201
202
  /// Case 1: unexpected alignment
203
  uint8_t ExpAlignment;
204
  uint32_t GotAlignment;
205
206
  /// Case 5: unexpected external type
207
  ExternalType ExpExtType, GotExtType;
208
209
  /// Case 6: unexpected function type
210
  /// Case 3: unexpected value type list
211
  std::vector<ValType> ExpParams, GotParams;
212
  std::vector<ValType> ExpReturns, GotReturns;
213
214
  /// Case 2: unexpected value type
215
  /// Case 7: unexpected table type: reference type
216
  /// Case 9: unexpected global type: value type
217
  ValType ExpValType, GotValType;
218
  /// Case 4: unexpected mutation settings
219
  /// Case 9: unexpected global type: value mutation
220
  ValMut ExpValMut, GotValMut;
221
  /// Case 7 & 8: unexpected table or memory type: limit
222
  bool ExpLimHasMax, GotLimHasMax;
223
  uint32_t ExpLimMin, GotLimMin;
224
  uint32_t ExpLimMax, GotLimMax;
225
226
  /// Case 10: unexpected version
227
  uint32_t ExpVersion, GotVersion;
228
};
229
230
struct InfoInstruction {
231
  InfoInstruction() = delete;
232
  InfoInstruction(const OpCode Op, const uint64_t Off,
233
                  const std::vector<ValVariant> &ArgsVec = {},
234
                  const std::vector<ValType> &ArgsTypesVec = {},
235
                  const bool Signed = false) noexcept
236
1.46k
      : Code(Op), Offset(Off), Args(ArgsVec), ArgsTypes(ArgsTypesVec),
237
1.46k
        IsSigned(Signed) {}
238
239
  OpCode Code;
240
  uint64_t Offset;
241
  std::vector<ValVariant> Args;
242
  std::vector<ValType> ArgsTypes;
243
  bool IsSigned;
244
};
245
246
struct InfoBoundary {
247
  InfoBoundary() = delete;
248
  InfoBoundary(
249
      const uint64_t Off, const uint32_t Len = 0,
250
      const uint32_t Lim = std::numeric_limits<uint32_t>::max()) noexcept
251
0
      : Offset(Off), Size(Len), Limit(Lim) {}
252
253
  uint64_t Offset;
254
  uint32_t Size;
255
  uint32_t Limit;
256
};
257
258
struct InfoProposal {
259
  InfoProposal() = delete;
260
542
  InfoProposal(Proposal P) noexcept : P(P) {}
261
262
  Proposal P;
263
};
264
265
} // namespace ErrInfo
266
} // namespace WasmEdge
267
268
template <>
269
struct fmt::formatter<WasmEdge::ErrInfo::InfoFile>
270
    : fmt::formatter<std::string_view> {
271
  fmt::format_context::iterator format(const WasmEdge::ErrInfo::InfoFile &Info,
272
                                       fmt::format_context &Ctx) const noexcept;
273
};
274
template <>
275
struct fmt::formatter<WasmEdge::ErrInfo::InfoLoading>
276
    : fmt::formatter<std::string_view> {
277
  fmt::format_context::iterator
278
  format(const WasmEdge::ErrInfo::InfoLoading &Info,
279
         fmt::format_context &Ctx) const noexcept;
280
};
281
template <>
282
struct fmt::formatter<WasmEdge::ErrInfo::InfoAST>
283
    : fmt::formatter<std::string_view> {
284
  fmt::format_context::iterator format(const WasmEdge::ErrInfo::InfoAST &Info,
285
                                       fmt::format_context &Ctx) const noexcept;
286
};
287
template <>
288
struct fmt::formatter<WasmEdge::ErrInfo::InfoInstanceBound>
289
    : fmt::formatter<std::string_view> {
290
  fmt::format_context::iterator
291
  format(const WasmEdge::ErrInfo::InfoInstanceBound &Info,
292
         fmt::format_context &Ctx) const noexcept;
293
};
294
template <>
295
struct fmt::formatter<WasmEdge::ErrInfo::InfoForbidIndex>
296
    : fmt::formatter<std::string_view> {
297
  fmt::format_context::iterator
298
  format(const WasmEdge::ErrInfo::InfoForbidIndex &Info,
299
         fmt::format_context &Ctx) const noexcept;
300
};
301
template <>
302
struct fmt::formatter<WasmEdge::ErrInfo::InfoExporting>
303
    : fmt::formatter<std::string_view> {
304
  fmt::format_context::iterator
305
  format(const WasmEdge::ErrInfo::InfoExporting &Info,
306
         fmt::format_context &Ctx) const noexcept;
307
};
308
template <>
309
struct fmt::formatter<WasmEdge::ErrInfo::InfoLimit>
310
    : fmt::formatter<std::string_view> {
311
  fmt::format_context::iterator format(const WasmEdge::ErrInfo::InfoLimit &Info,
312
                                       fmt::format_context &Ctx) const noexcept;
313
};
314
template <>
315
struct fmt::formatter<WasmEdge::ErrInfo::InfoRegistering>
316
    : fmt::formatter<std::string_view> {
317
  fmt::format_context::iterator
318
  format(const WasmEdge::ErrInfo::InfoRegistering &Info,
319
         fmt::format_context &Ctx) const noexcept;
320
};
321
template <>
322
struct fmt::formatter<WasmEdge::ErrInfo::InfoLinking>
323
    : fmt::formatter<std::string_view> {
324
  fmt::format_context::iterator
325
  format(const WasmEdge::ErrInfo::InfoLinking &Info,
326
         fmt::format_context &Ctx) const noexcept;
327
};
328
template <>
329
struct fmt::formatter<WasmEdge::ErrInfo::InfoExecuting>
330
    : fmt::formatter<std::string_view> {
331
  fmt::format_context::iterator
332
  format(const WasmEdge::ErrInfo::InfoExecuting &Info,
333
         fmt::format_context &Ctx) const noexcept;
334
};
335
template <>
336
struct fmt::formatter<WasmEdge::ErrInfo::InfoMismatch>
337
    : fmt::formatter<std::string_view> {
338
  fmt::format_context::iterator
339
  format(const WasmEdge::ErrInfo::InfoMismatch &Info,
340
         fmt::format_context &Ctx) const noexcept;
341
};
342
template <>
343
struct fmt::formatter<WasmEdge::ErrInfo::InfoInstruction>
344
    : fmt::formatter<std::string_view> {
345
  fmt::format_context::iterator
346
  format(const WasmEdge::ErrInfo::InfoInstruction &Info,
347
         fmt::format_context &Ctx) const noexcept;
348
};
349
template <>
350
struct fmt::formatter<WasmEdge::ErrInfo::InfoBoundary>
351
    : fmt::formatter<std::string_view> {
352
  fmt::format_context::iterator
353
  format(const WasmEdge::ErrInfo::InfoBoundary &Info,
354
         fmt::format_context &Ctx) const noexcept;
355
};
356
template <>
357
struct fmt::formatter<WasmEdge::ErrInfo::InfoProposal>
358
    : fmt::formatter<std::string_view> {
359
  fmt::format_context::iterator
360
  format(const WasmEdge::ErrInfo::InfoProposal &Info,
361
         fmt::format_context &Ctx) const noexcept;
362
};