Coverage Report

Created: 2026-11-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/executor/engine/refInstr.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
#include "executor/executor.h"
5
6
namespace WasmEdge {
7
namespace Executor {
8
9
namespace {
10
11
template <typename... T>
12
ErrCode logError(const ErrCode &Code, const AST::Instruction &Instr,
13
0
                 T &&...F) noexcept {
14
0
  spdlog::error(Code);
15
0
  (F(), ...);
16
0
  spdlog::error(ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
17
0
  return Code;
18
0
}
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArrayNewDataOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArrayNewDataOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&)
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArrayNewElemOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArrayNewElemOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&)
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArrayGetOp(WasmEdge::Runtime::StackManager&, unsigned int, WasmEdge::AST::Instruction const&, bool) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArrayGetOp(WasmEdge::Runtime::StackManager&, unsigned int, WasmEdge::AST::Instruction const&, bool) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&)
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArraySetOp(WasmEdge::Runtime::StackManager&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const&, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArraySetOp(WasmEdge::Runtime::StackManager&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const&, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&)
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArrayFillOp(WasmEdge::Runtime::StackManager&, unsigned int, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const&, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArrayFillOp(WasmEdge::Runtime::StackManager&, unsigned int, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const&, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&)
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArrayCopyOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArrayCopyOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&)
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArrayInitDataOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}, WasmEdge::Executor::Executor::runArrayInitDataOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#2}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArrayInitDataOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&, WasmEdge::Executor::Executor::runArrayInitDataOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#2}&&)
Unexecuted instantiation: refInstr.cpp:WasmEdge::ErrCode WasmEdge::Executor::(anonymous namespace)::logError<WasmEdge::Executor::Executor::runArrayInitElemOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}, WasmEdge::Executor::Executor::runArrayInitElemOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#2}>(WasmEdge::ErrCode const&, WasmEdge::AST::Instruction const&, WasmEdge::Executor::Executor::runArrayInitElemOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#1}&&, WasmEdge::Executor::Executor::runArrayInitElemOp(WasmEdge::Runtime::StackManager&, unsigned int, unsigned int, unsigned int, WasmEdge::AST::Instruction const&) const::$_0::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const::{lambda()#2}&&)
19
20
0
ErrCode logError(const ErrCode &Code, const AST::Instruction &Instr) noexcept {
21
0
  spdlog::error(Code);
22
0
  spdlog::error(ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
23
0
  return Code;
24
0
}
25
26
void logArrayOOB(const ErrCode &Code, const uint32_t Idx, const uint32_t Cnt,
27
0
                 const RefVariant &Ref) noexcept {
28
0
  if (Code == ErrCode::Value::ArrayOutOfBounds) {
29
0
    const auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
30
0
    spdlog::error(ErrInfo::InfoBoundary(static_cast<uint64_t>(Idx), Cnt,
31
0
                                        Inst->getLength()));
32
0
  }
33
0
}
34
35
void logDoubleArrayOOB(const ErrCode &Code, const uint32_t Idx1,
36
                       const uint32_t Cnt1, const RefVariant &Ref1,
37
                       const uint32_t Idx2, const uint32_t Cnt2,
38
0
                       const RefVariant &Ref2) noexcept {
39
0
  if (Code == ErrCode::Value::ArrayOutOfBounds) {
40
0
    const auto *Inst1 = Ref1.getPtr<Runtime::Instance::ArrayInstance>();
41
0
    const auto *Inst2 = Ref2.getPtr<Runtime::Instance::ArrayInstance>();
42
0
    if (static_cast<uint64_t>(Idx1) + static_cast<uint64_t>(Cnt1) >
43
0
        Inst1->getLength()) {
44
0
      spdlog::error(ErrInfo::InfoBoundary(static_cast<uint64_t>(Idx1), Cnt1,
45
0
                                          Inst1->getLength()));
46
0
    } else if (static_cast<uint64_t>(Idx2) + static_cast<uint64_t>(Cnt2) >
47
0
               Inst2->getLength()) {
48
0
      spdlog::error(ErrInfo::InfoBoundary(static_cast<uint64_t>(Idx2), Cnt2,
49
0
                                          Inst2->getLength()));
50
0
    }
51
0
  }
52
0
}
53
54
void logMemoryOOB(const ErrCode &Code,
55
                  const Runtime::Instance::DataInstance &DataInst,
56
0
                  const uint32_t Idx, const uint32_t Length) noexcept {
57
0
  if (Code == ErrCode::Value::MemoryOutOfBounds) {
58
0
    spdlog::error(ErrInfo::InfoBoundary(
59
0
        static_cast<uint64_t>(Idx), Length,
60
0
        static_cast<uint32_t>(DataInst.getData().size())));
61
0
  }
62
0
}
63
64
void logTableOOB(const ErrCode &Code,
65
                 const Runtime::Instance::ElementInstance &ElemInst,
66
0
                 const uint32_t Idx, const uint32_t Length) noexcept {
67
0
  if (Code == ErrCode::Value::TableOutOfBounds) {
68
0
    auto ElemSrc = ElemInst.getRefs();
69
0
    spdlog::error(ErrInfo::InfoBoundary(static_cast<uint64_t>(Idx), Length,
70
0
                                        static_cast<uint32_t>(ElemSrc.size())));
71
0
  }
72
0
}
73
74
} // namespace
75
76
Expect<void> Executor::runRefNullOp(Runtime::StackManager &StackMgr,
77
0
                                    const ValType &Type) const noexcept {
78
  // A null reference is typed with the least type in its respective hierarchy.
79
0
  StackMgr.push(RefVariant(toBottomType(StackMgr.getModule(), Type)));
80
0
  return {};
81
0
}
82
83
0
Expect<void> Executor::runRefIsNullOp(ValVariant &Val) const noexcept {
84
0
  Val.emplace<uint32_t>(Val.get<RefVariant>().isNull() ? 1U : 0U);
85
0
  return {};
86
0
}
87
88
Expect<void> Executor::runRefFuncOp(Runtime::StackManager &StackMgr,
89
0
                                    uint32_t Idx) const noexcept {
90
0
  const auto *FuncInst = getFuncInstByIdx(StackMgr.getModule(), Idx);
91
0
  StackMgr.push(RefVariant(FuncInst->getDefType(), FuncInst));
92
0
  return {};
93
0
}
94
95
Expect<void> Executor::runRefEqOp(ValVariant &Val1,
96
0
                                  const ValVariant &Val2) const noexcept {
97
0
  Val1.emplace<uint32_t>(Val1.get<RefVariant>().getPtr<void>() ==
98
0
                                 Val2.get<RefVariant>().getPtr<void>()
99
0
                             ? 1U
100
0
                             : 0U);
101
0
  return {};
102
0
}
103
104
Expect<void>
105
Executor::runRefAsNonNullOp(RefVariant &Ref,
106
0
                            const AST::Instruction &Instr) const noexcept {
107
0
  if (Ref.isNull()) {
108
0
    return Unexpect(logError(ErrCode::Value::CastNullToNonNull, Instr));
109
0
  }
110
0
  Ref.getType().toNonNullableRef();
111
0
  return {};
112
0
}
113
114
Expect<void> Executor::runStructNewOp(Runtime::StackManager &StackMgr,
115
                                      const uint32_t TypeIdx,
116
0
                                      const bool IsDefault) const noexcept {
117
0
  if (IsDefault) {
118
0
    StackMgr.push(*structNew(StackMgr.getModule(), TypeIdx));
119
0
  } else {
120
0
    const auto &CompType = getCompositeTypeByIdx(StackMgr.getModule(), TypeIdx);
121
0
    const uint32_t N = static_cast<uint32_t>(CompType.getFieldTypes().size());
122
0
    std::vector<ValVariant> Vals = StackMgr.pop(N);
123
0
    StackMgr.push(*structNew(StackMgr.getModule(), TypeIdx, Vals));
124
0
  }
125
0
  return {};
126
0
}
127
128
Expect<void> Executor::runStructGetOp(Runtime::StackManager &StackMgr,
129
                                      const uint32_t TypeIdx,
130
                                      const uint32_t Off,
131
                                      const AST::Instruction &Instr,
132
0
                                      const bool IsSigned) const noexcept {
133
0
  const RefVariant Ref = StackMgr.getTop().get<RefVariant>();
134
0
  EXPECTED_TRY(auto Val,
135
0
               structGet(StackMgr.getModule(), Ref, TypeIdx, Off, IsSigned)
136
0
                   .map_error([&](auto E) { return logError(E, Instr); }));
137
0
  StackMgr.getTop() = Val;
138
0
  return {};
139
0
}
140
141
Expect<void>
142
Executor::runStructSetOp(Runtime::StackManager &StackMgr, const ValVariant &Val,
143
                         const uint32_t TypeIdx, const uint32_t Off,
144
0
                         const AST::Instruction &Instr) const noexcept {
145
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
146
0
  EXPECTED_TRY(structSet(StackMgr.getModule(), Ref, Val, TypeIdx, Off)
147
0
                   .map_error([&](auto E) { return logError(E, Instr); }));
148
0
  return {};
149
0
}
150
151
Expect<void> Executor::runArrayNewOp(Runtime::StackManager &StackMgr,
152
                                     const uint32_t TypeIdx,
153
                                     const uint32_t InitCnt,
154
0
                                     uint32_t Length) const noexcept {
155
0
  assuming(InitCnt == 0 || InitCnt == 1 || InitCnt == Length);
156
0
  if (InitCnt == 0) {
157
0
    StackMgr.push(*arrayNew(StackMgr.getModule(), TypeIdx, Length));
158
0
  } else if (InitCnt == 1) {
159
0
    StackMgr.getTop().emplace<RefVariant>(
160
0
        *arrayNew(StackMgr.getModule(), TypeIdx, Length, {StackMgr.getTop()}));
161
0
  } else {
162
0
    StackMgr.push(
163
0
        *arrayNew(StackMgr.getModule(), TypeIdx, Length, StackMgr.pop(Length)));
164
0
  }
165
0
  return {};
166
0
}
167
168
Expect<void>
169
Executor::runArrayNewDataOp(Runtime::StackManager &StackMgr,
170
                            const uint32_t TypeIdx, const uint32_t DataIdx,
171
0
                            const AST::Instruction &Instr) const noexcept {
172
0
  const uint32_t Length = StackMgr.pop().get<uint32_t>();
173
0
  const uint32_t Start = StackMgr.getTop().get<uint32_t>();
174
0
  EXPECTED_TRY(
175
0
      auto InstRef,
176
0
      arrayNewData(StackMgr.getModule(), TypeIdx, DataIdx, Start, Length)
177
0
          .map_error([&](auto E) {
178
0
            auto *DataInst = getDataInstByIdx(StackMgr.getModule(), DataIdx);
179
0
            const uint32_t BSize =
180
0
                getArrayStorageTypeByIdx(StackMgr.getModule(), TypeIdx)
181
0
                    .getBitWidth() /
182
0
                8;
183
0
            return logError(E, Instr, [&]() {
184
0
              return logMemoryOOB(E, *DataInst, Start, BSize * Length);
185
0
            });
186
0
          }));
187
0
  StackMgr.getTop().emplace<RefVariant>(InstRef);
188
0
  return {};
189
0
}
190
191
Expect<void>
192
Executor::runArrayNewElemOp(Runtime::StackManager &StackMgr,
193
                            const uint32_t TypeIdx, const uint32_t ElemIdx,
194
0
                            const AST::Instruction &Instr) const noexcept {
195
0
  const uint32_t Length = StackMgr.pop().get<uint32_t>();
196
0
  const uint32_t Start = StackMgr.getTop().get<uint32_t>();
197
0
  EXPECTED_TRY(
198
0
      auto InstRef,
199
0
      arrayNewElem(StackMgr.getModule(), TypeIdx, ElemIdx, Start, Length)
200
0
          .map_error([&](auto E) {
201
0
            auto *ElemInst = getElemInstByIdx(StackMgr.getModule(), ElemIdx);
202
0
            return logError(E, Instr, [&]() {
203
0
              return logTableOOB(E, *ElemInst, Start, Length);
204
0
            });
205
0
          }));
206
0
  StackMgr.getTop().emplace<RefVariant>(InstRef);
207
0
  return {};
208
0
}
209
210
Expect<void> Executor::runArrayGetOp(Runtime::StackManager &StackMgr,
211
                                     const uint32_t TypeIdx,
212
                                     const AST::Instruction &Instr,
213
0
                                     const bool IsSigned) const noexcept {
214
0
  const uint32_t Idx = StackMgr.pop().get<uint32_t>();
215
0
  const RefVariant Ref = StackMgr.getTop().get<RefVariant>();
216
0
  EXPECTED_TRY(auto Val,
217
0
               arrayGet(StackMgr.getModule(), Ref, TypeIdx, Idx, IsSigned)
218
0
                   .map_error([&](auto E) {
219
0
                     return logError(E, Instr, [&]() {
220
0
                       return logArrayOOB(E, Idx, 1, Ref);
221
0
                     });
222
0
                   }));
223
0
  StackMgr.getTop() = Val;
224
0
  return {};
225
0
}
226
227
Expect<void>
228
Executor::runArraySetOp(Runtime::StackManager &StackMgr, const ValVariant &Val,
229
                        const uint32_t TypeIdx,
230
0
                        const AST::Instruction &Instr) const noexcept {
231
0
  const uint32_t Idx = StackMgr.pop().get<uint32_t>();
232
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
233
0
  EXPECTED_TRY(arraySet(StackMgr.getModule(), Ref, Val, TypeIdx, Idx)
234
0
                   .map_error([&](auto E) {
235
0
                     return logError(E, Instr, [&]() {
236
0
                       return logArrayOOB(E, Idx, 1, Ref);
237
0
                     });
238
0
                   }));
239
0
  return {};
240
0
}
241
242
Expect<void>
243
Executor::runArrayLenOp(ValVariant &Val,
244
0
                        const AST::Instruction &Instr) const noexcept {
245
0
  const auto *Inst =
246
0
      Val.get<RefVariant>().getPtr<Runtime::Instance::ArrayInstance>();
247
0
  if (Inst == nullptr) {
248
0
    return Unexpect(logError(ErrCode::Value::AccessNullArray, Instr));
249
0
  }
250
0
  Val.emplace<uint32_t>(Inst->getLength());
251
0
  return {};
252
0
}
253
254
Expect<void>
255
Executor::runArrayFillOp(Runtime::StackManager &StackMgr, const uint32_t Cnt,
256
                         const ValVariant &Val, const uint32_t TypeIdx,
257
0
                         const AST::Instruction &Instr) const noexcept {
258
0
  const uint32_t Idx = StackMgr.pop().get<uint32_t>();
259
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
260
0
  EXPECTED_TRY(arrayFill(StackMgr.getModule(), Ref, Val, TypeIdx, Idx, Cnt)
261
0
                   .map_error([&](auto E) {
262
0
                     return logError(E, Instr, [&]() {
263
0
                       return logArrayOOB(E, Idx, Cnt, Ref);
264
0
                     });
265
0
                   }));
266
0
  return {};
267
0
}
268
269
Expect<void>
270
Executor::runArrayCopyOp(Runtime::StackManager &StackMgr, const uint32_t Cnt,
271
                         const uint32_t DstTypeIdx, const uint32_t SrcTypeIdx,
272
0
                         const AST::Instruction &Instr) const noexcept {
273
0
  const uint32_t SrcIdx = StackMgr.pop().get<uint32_t>();
274
0
  const RefVariant SrcRef = StackMgr.pop().get<RefVariant>();
275
0
  const uint32_t DstIdx = StackMgr.pop().get<uint32_t>();
276
0
  const RefVariant DstRef = StackMgr.pop().get<RefVariant>();
277
0
  EXPECTED_TRY(arrayCopy(StackMgr.getModule(), DstRef, DstTypeIdx, DstIdx,
278
0
                         SrcRef, SrcTypeIdx, SrcIdx, Cnt)
279
0
                   .map_error([&](auto E) {
280
0
                     return logError(E, Instr, [&]() {
281
0
                       return logDoubleArrayOOB(E, SrcIdx, Cnt, SrcRef, DstIdx,
282
0
                                                Cnt, DstRef);
283
0
                     });
284
0
                   }));
285
0
  return {};
286
0
}
287
288
Expect<void> Executor::runArrayInitDataOp(
289
    Runtime::StackManager &StackMgr, const uint32_t Cnt, const uint32_t TypeIdx,
290
0
    const uint32_t DataIdx, const AST::Instruction &Instr) const noexcept {
291
0
  const uint32_t SrcIdx = StackMgr.pop().get<uint32_t>();
292
0
  const uint32_t DstIdx = StackMgr.pop().get<uint32_t>();
293
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
294
0
  EXPECTED_TRY(
295
0
      arrayInitData(StackMgr.getModule(), Ref, TypeIdx, DataIdx, DstIdx, SrcIdx,
296
0
                    Cnt)
297
0
          .map_error([&](auto E) {
298
0
            auto *DataInst = getDataInstByIdx(StackMgr.getModule(), DataIdx);
299
0
            const uint32_t BSize =
300
0
                getArrayStorageTypeByIdx(StackMgr.getModule(), TypeIdx)
301
0
                    .getBitWidth() /
302
0
                8;
303
0
            return logError(
304
0
                E, Instr, [&]() { return logArrayOOB(E, DstIdx, Cnt, Ref); },
305
0
                [&]() {
306
0
                  return logMemoryOOB(E, *DataInst, SrcIdx, Cnt * BSize);
307
0
                });
308
0
          }));
309
0
  return {};
310
0
}
311
312
Expect<void> Executor::runArrayInitElemOp(
313
    Runtime::StackManager &StackMgr, const uint32_t Cnt, const uint32_t TypeIdx,
314
0
    const uint32_t ElemIdx, const AST::Instruction &Instr) const noexcept {
315
0
  const uint32_t SrcIdx = StackMgr.pop().get<uint32_t>();
316
0
  const uint32_t DstIdx = StackMgr.pop().get<uint32_t>();
317
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
318
0
  EXPECTED_TRY(
319
0
      arrayInitElem(StackMgr.getModule(), Ref, TypeIdx, ElemIdx, DstIdx, SrcIdx,
320
0
                    Cnt)
321
0
          .map_error([&](auto E) {
322
0
            auto *ElemInst = getElemInstByIdx(StackMgr.getModule(), ElemIdx);
323
0
            return logError(
324
0
                E, Instr, [&]() { return logArrayOOB(E, DstIdx, Cnt, Ref); },
325
0
                [&]() { return logTableOOB(E, *ElemInst, SrcIdx, Cnt); });
326
0
          }));
327
0
  return {};
328
0
}
329
330
Expect<void>
331
Executor::runRefTestOp(const Runtime::Instance::ModuleInstance *ModInst,
332
                       ValVariant &Val, const AST::Instruction &Instr,
333
0
                       const bool IsCast) const noexcept {
334
0
  if (matchRef(ModInst, Instr.getValType(), Val.get<RefVariant>())) {
335
0
    if (!IsCast) {
336
0
      Val.emplace<uint32_t>(1U);
337
0
    }
338
0
  } else {
339
0
    if (IsCast) {
340
0
      spdlog::error(ErrCode::Value::CastFailed);
341
0
      spdlog::error(ErrInfo::InfoMismatch(Instr.getValType(),
342
0
                                          Val.get<RefVariant>().getType()));
343
0
      spdlog::error(
344
0
          ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
345
0
      return Unexpect(ErrCode::Value::CastFailed);
346
0
    } else {
347
0
      Val.emplace<uint32_t>(0U);
348
0
    }
349
0
  }
350
0
  return {};
351
0
}
352
353
Expect<void> Executor::runRefConvOp(RefVariant &Ref,
354
0
                                    TypeCode TCode) const noexcept {
355
0
  if (TCode == TypeCode::AnyRef) {
356
    // Internalize.
357
0
    if (Ref.isNull()) {
358
0
      Ref = RefVariant(ValType(TypeCode::RefNull, TypeCode::NullRef));
359
0
    } else {
360
0
      Ref.getType().setInternalized();
361
0
      if (Ref.getType().isExternRefType()) {
362
0
        Ref.getType() = ValType(TypeCode::Ref, TypeCode::AnyRef);
363
0
      }
364
0
    }
365
0
  } else {
366
    // Externalize.
367
0
    if (Ref.isNull()) {
368
0
      Ref = RefVariant(ValType(TypeCode::RefNull, TypeCode::NullExternRef));
369
0
    } else {
370
      // Use the externalize flag because the value type information should be
371
      // reserved when a reference being externalized and internalized.
372
0
      Ref.getType().setExternalized();
373
0
    }
374
0
  }
375
0
  return {};
376
0
}
377
378
0
Expect<void> Executor::runRefI31Op(ValVariant &Val) const noexcept {
379
0
  uint32_t RefNum = (Val.get<uint32_t>() & 0x7FFFFFFFU) | 0x80000000U;
380
0
  Val = RefVariant(ValType(TypeCode::Ref, TypeCode::I31Ref),
381
0
                   reinterpret_cast<void *>(static_cast<uint64_t>(RefNum)));
382
0
  return {};
383
0
}
384
385
Expect<void> Executor::runI31GetOp(ValVariant &Val,
386
                                   const AST::Instruction &Instr,
387
0
                                   const bool IsSigned) const noexcept {
388
0
  uint32_t RefNum = static_cast<uint32_t>(
389
0
      reinterpret_cast<uintptr_t>(Val.get<RefVariant>().getPtr<void>()));
390
0
  if ((RefNum & 0x80000000U) == 0) {
391
0
    return Unexpect(logError(ErrCode::Value::AccessNullI31, Instr));
392
0
  }
393
0
  RefNum &= 0x7FFFFFFFU;
394
0
  if (IsSigned) {
395
0
    RefNum |= ((RefNum & 0x40000000U) << 1);
396
0
  }
397
0
  Val.emplace<uint32_t>(RefNum);
398
0
  return {};
399
0
}
400
401
Expect<RefVariant>
402
Executor::structNew(const Runtime::Instance::ModuleInstance *ModInstArg,
403
                    const uint32_t TypeIdx,
404
0
                    Span<const ValVariant> Args) const noexcept {
405
  /// TODO: The array and struct instances are currently owned by the module
406
  /// instance because they refer to the defined types of the module instances.
407
  /// This may be changed after applying the garbage collection mechanism.
408
0
  const auto &CompType = getCompositeTypeByIdx(ModInstArg, TypeIdx);
409
0
  uint32_t N = static_cast<uint32_t>(CompType.getFieldTypes().size());
410
0
  auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg);
411
0
  std::vector<ValVariant> Vals(N);
412
0
  for (uint32_t I = 0; I < N; I++) {
413
0
    const auto &VType = CompType.getFieldTypes()[I].getStorageType();
414
0
    if (Args.size() > 0) {
415
0
      Vals[I] = packVal(VType, Args[I]);
416
0
    } else {
417
0
      Vals[I] = VType.isRefType()
418
0
                    ? ValVariant(RefVariant(toBottomType(ModInstArg, VType)))
419
0
                    : ValVariant(static_cast<uint128_t>(0U));
420
0
    }
421
0
  }
422
0
  WasmEdge::Runtime::Instance::StructInstance *Inst =
423
0
      ModInst->newStruct(TypeIdx, std::move(Vals));
424
0
  return RefVariant(Inst->getDefType(), Inst);
425
0
}
426
427
Expect<ValVariant>
428
Executor::structGet(const Runtime::Instance::ModuleInstance *ModInst,
429
                    const RefVariant Ref, const uint32_t TypeIdx,
430
0
                    const uint32_t Off, const bool IsSigned) const noexcept {
431
0
  const auto *Inst = Ref.getPtr<Runtime::Instance::StructInstance>();
432
0
  if (Inst == nullptr) {
433
0
    return Unexpect(ErrCode::Value::AccessNullStruct);
434
0
  }
435
0
  const auto &VType = getStructStorageTypeByIdx(ModInst, TypeIdx, Off);
436
0
  return unpackVal(VType, Inst->getField(Off), IsSigned);
437
0
}
438
439
Expect<void>
440
Executor::structSet(const Runtime::Instance::ModuleInstance *ModInst,
441
                    const RefVariant Ref, const ValVariant Val,
442
0
                    const uint32_t TypeIdx, const uint32_t Off) const noexcept {
443
0
  auto *Inst = Ref.getPtr<Runtime::Instance::StructInstance>();
444
0
  if (Inst == nullptr) {
445
0
    return Unexpect(ErrCode::Value::AccessNullStruct);
446
0
  }
447
0
  const auto &VType = getStructStorageTypeByIdx(ModInst, TypeIdx, Off);
448
0
  Inst->getField(Off) = packVal(VType, Val);
449
0
  return {};
450
0
}
451
452
Expect<RefVariant>
453
Executor::arrayNew(const Runtime::Instance::ModuleInstance *ModInstArg,
454
                   const uint32_t TypeIdx, const uint32_t Length,
455
0
                   Span<const ValVariant> Args) const noexcept {
456
  /// TODO: The array and struct instances are currently owned by the module
457
  /// instance because they refer to the defined types of the module instances.
458
  /// This may be changed after applying the garbage collection mechanism.
459
0
  const auto &VType = getArrayStorageTypeByIdx(ModInstArg, TypeIdx);
460
0
  WasmEdge::Runtime::Instance::ArrayInstance *Inst = nullptr;
461
0
  auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg);
462
0
  if (Args.size() == 0) {
463
    // New and fill with default values.
464
0
    auto InitVal = VType.isRefType()
465
0
                       ? ValVariant(RefVariant(toBottomType(ModInstArg, VType)))
466
0
                       : ValVariant(static_cast<uint128_t>(0U));
467
0
    Inst = ModInst->newArray(TypeIdx, Length, InitVal);
468
0
  } else if (Args.size() == 1) {
469
    // Create and fill with the argument value.
470
0
    Inst = ModInst->newArray(TypeIdx, Length, packVal(VType, Args[0]));
471
0
  } else {
472
    // Create with arguments.
473
0
    Inst = ModInst->newArray(
474
0
        TypeIdx,
475
0
        packVals(VType, std::vector<ValVariant>(Args.begin(), Args.end())));
476
0
  }
477
0
  return RefVariant(Inst->getDefType(), Inst);
478
0
}
479
480
Expect<RefVariant>
481
Executor::arrayNewData(const Runtime::Instance::ModuleInstance *ModInstArg,
482
                       const uint32_t TypeIdx, const uint32_t DataIdx,
483
                       const uint32_t Start,
484
0
                       const uint32_t Length) const noexcept {
485
0
  const auto &VType = getArrayStorageTypeByIdx(ModInstArg, TypeIdx);
486
0
  const uint32_t BSize = VType.getBitWidth() / 8;
487
0
  auto *DataInst = getDataInstByIdx(ModInstArg, DataIdx);
488
0
  assuming(DataInst);
489
0
  if (static_cast<uint64_t>(Start) + static_cast<uint64_t>(Length) * BSize >
490
0
      DataInst->getData().size()) {
491
0
    return Unexpect(ErrCode::Value::MemoryOutOfBounds);
492
0
  }
493
0
  auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg);
494
0
  std::vector<ValVariant> Args;
495
0
  Args.reserve(Length);
496
0
  for (uint32_t Idx = 0; Idx < Length; Idx++) {
497
    // The value has been packed.
498
0
    Args.push_back(DataInst->loadValue(Start + Idx * BSize, BSize));
499
0
  }
500
0
  WasmEdge::Runtime::Instance::ArrayInstance *Inst =
501
0
      ModInst->newArray(TypeIdx, std::move(Args));
502
0
  return RefVariant(Inst->getDefType(), Inst);
503
0
}
504
505
Expect<RefVariant>
506
Executor::arrayNewElem(const Runtime::Instance::ModuleInstance *ModInstArg,
507
                       const uint32_t TypeIdx, const uint32_t ElemIdx,
508
                       const uint32_t Start,
509
0
                       const uint32_t Length) const noexcept {
510
0
  const auto &VType = getArrayStorageTypeByIdx(ModInstArg, TypeIdx);
511
0
  auto *ElemInst = getElemInstByIdx(ModInstArg, ElemIdx);
512
0
  assuming(ElemInst);
513
0
  auto ElemSrc = ElemInst->getRefs();
514
0
  if (static_cast<uint64_t>(Start) + static_cast<uint64_t>(Length) >
515
0
      ElemSrc.size()) {
516
0
    return Unexpect(ErrCode::Value::TableOutOfBounds);
517
0
  }
518
0
  std::vector<ValVariant> Refs(ElemSrc.begin() + Start,
519
0
                               ElemSrc.begin() + Start + Length);
520
0
  auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg);
521
0
  WasmEdge::Runtime::Instance::ArrayInstance *Inst =
522
0
      ModInst->newArray(TypeIdx, packVals(VType, std::move(Refs)));
523
0
  return RefVariant(Inst->getDefType(), Inst);
524
0
}
525
526
Expect<ValVariant>
527
Executor::arrayGet(const Runtime::Instance::ModuleInstance *ModInst,
528
                   const RefVariant &Ref, const uint32_t TypeIdx,
529
0
                   const uint32_t Idx, const bool IsSigned) const noexcept {
530
0
  const auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
531
0
  if (Inst == nullptr) {
532
0
    return Unexpect(ErrCode::Value::AccessNullArray);
533
0
  }
534
0
  if (Idx >= Inst->getLength()) {
535
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
536
0
  }
537
0
  const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx);
538
0
  return unpackVal(VType, Inst->getData(Idx), IsSigned);
539
0
}
540
541
Expect<void>
542
Executor::arraySet(const Runtime::Instance::ModuleInstance *ModInst,
543
                   const RefVariant &Ref, const ValVariant &Val,
544
0
                   const uint32_t TypeIdx, const uint32_t Idx) const noexcept {
545
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
546
0
  if (Inst == nullptr) {
547
0
    return Unexpect(ErrCode::Value::AccessNullArray);
548
0
  }
549
0
  if (Idx >= Inst->getLength()) {
550
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
551
0
  }
552
0
  const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx);
553
0
  Inst->getData(Idx) = packVal(VType, Val);
554
0
  return {};
555
0
}
556
557
Expect<void>
558
Executor::arrayFill(const Runtime::Instance::ModuleInstance *ModInst,
559
                    const RefVariant &Ref, const ValVariant &Val,
560
                    const uint32_t TypeIdx, const uint32_t Idx,
561
0
                    const uint32_t Cnt) const noexcept {
562
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
563
0
  if (Inst == nullptr) {
564
0
    return Unexpect(ErrCode::Value::AccessNullArray);
565
0
  }
566
0
  if (static_cast<uint64_t>(Idx) + static_cast<uint64_t>(Cnt) >
567
0
      Inst->getLength()) {
568
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
569
0
  }
570
0
  const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx);
571
0
  auto Arr = Inst->getArray();
572
0
  std::fill(Arr.begin() + Idx, Arr.begin() + Idx + Cnt, packVal(VType, Val));
573
0
  return {};
574
0
}
575
576
Expect<void> Executor::arrayInitData(
577
    const Runtime::Instance::ModuleInstance *ModInst, const RefVariant &Ref,
578
    const uint32_t TypeIdx, const uint32_t DataIdx, const uint32_t DstIdx,
579
0
    const uint32_t SrcIdx, const uint32_t Cnt) const noexcept {
580
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
581
0
  if (Inst == nullptr) {
582
0
    return Unexpect(ErrCode::Value::AccessNullArray);
583
0
  }
584
0
  if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) >
585
0
      Inst->getLength()) {
586
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
587
0
  }
588
0
  const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx);
589
0
  const uint32_t BSize = VType.getBitWidth() / 8;
590
0
  auto *DataInst = getDataInstByIdx(ModInst, DataIdx);
591
0
  assuming(DataInst);
592
0
  if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) * BSize >
593
0
      DataInst->getData().size()) {
594
0
    return Unexpect(ErrCode::Value::MemoryOutOfBounds);
595
0
  }
596
597
0
  for (uint32_t Idx = 0; Idx < Cnt; Idx++) {
598
    // The value has been packed.
599
0
    Inst->getData(DstIdx + Idx) =
600
0
        DataInst->loadValue(SrcIdx + Idx * BSize, BSize);
601
0
  }
602
0
  return {};
603
0
}
604
605
Expect<void> Executor::arrayInitElem(
606
    const Runtime::Instance::ModuleInstance *ModInst, const RefVariant &Ref,
607
    const uint32_t TypeIdx, const uint32_t ElemIdx, const uint32_t DstIdx,
608
0
    const uint32_t SrcIdx, const uint32_t Cnt) const noexcept {
609
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
610
0
  if (Inst == nullptr) {
611
0
    return Unexpect(ErrCode::Value::AccessNullArray);
612
0
  }
613
0
  if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) >
614
0
      Inst->getLength()) {
615
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
616
0
  }
617
0
  const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx);
618
0
  auto *ElemInst = getElemInstByIdx(ModInst, ElemIdx);
619
0
  assuming(ElemInst);
620
0
  auto ElemSrc = ElemInst->getRefs();
621
0
  if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) >
622
0
      ElemSrc.size()) {
623
0
    return Unexpect(ErrCode::Value::TableOutOfBounds);
624
0
  }
625
626
0
  auto Arr = Inst->getArray();
627
  // The value has been packed.
628
0
  std::transform(ElemSrc.begin() + SrcIdx, ElemSrc.begin() + SrcIdx + Cnt,
629
0
                 Arr.begin() + DstIdx,
630
0
                 [&](const RefVariant &V) { return packVal(VType, V); });
631
0
  return {};
632
0
}
633
634
Expect<void>
635
Executor::arrayCopy(const Runtime::Instance::ModuleInstance *ModInst,
636
                    const RefVariant &DstRef, const uint32_t DstTypeIdx,
637
                    const uint32_t DstIdx, const RefVariant &SrcRef,
638
                    const uint32_t SrcTypeIdx, const uint32_t SrcIdx,
639
0
                    const uint32_t Cnt) const noexcept {
640
0
  auto *SrcInst = SrcRef.getPtr<Runtime::Instance::ArrayInstance>();
641
0
  auto *DstInst = DstRef.getPtr<Runtime::Instance::ArrayInstance>();
642
0
  if (SrcInst == nullptr) {
643
0
    return Unexpect(ErrCode::Value::AccessNullArray);
644
0
  }
645
0
  if (DstInst == nullptr) {
646
0
    return Unexpect(ErrCode::Value::AccessNullArray);
647
0
  }
648
0
  if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) >
649
0
      SrcInst->getLength()) {
650
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
651
0
  }
652
0
  if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) >
653
0
      DstInst->getLength()) {
654
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
655
0
  }
656
657
0
  auto SrcArr = SrcInst->getArray();
658
0
  auto DstArr = DstInst->getArray();
659
0
  const auto &SrcVType = getArrayStorageTypeByIdx(ModInst, SrcTypeIdx);
660
0
  const auto &DstVType = getArrayStorageTypeByIdx(ModInst, DstTypeIdx);
661
0
  if (DstIdx <= SrcIdx) {
662
0
    std::transform(SrcArr.begin() + SrcIdx, SrcArr.begin() + SrcIdx + Cnt,
663
0
                   DstArr.begin() + DstIdx, [&](const ValVariant &V) {
664
0
                     return packVal(DstVType, unpackVal(SrcVType, V));
665
0
                   });
666
0
  } else {
667
0
    std::transform(std::make_reverse_iterator(SrcArr.begin() + SrcIdx + Cnt),
668
0
                   std::make_reverse_iterator(SrcArr.begin() + SrcIdx),
669
0
                   std::make_reverse_iterator(DstArr.begin() + DstIdx + Cnt),
670
0
                   [&](const ValVariant &V) {
671
0
                     return packVal(DstVType, unpackVal(SrcVType, V));
672
0
                   });
673
0
  }
674
0
  return {};
675
0
}
676
677
} // namespace Executor
678
} // namespace WasmEdge