Coverage Report

Created: 2026-06-30 06:10

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, 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, 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, TypeIdx));
119
0
  } else {
120
0
    const auto &CompType = getCompositeTypeByIdx(StackMgr, 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, 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(
135
0
      auto Val,
136
0
      structGet(StackMgr, Ref, TypeIdx, Off, IsSigned).map_error([&](auto E) {
137
0
        return logError(E, Instr);
138
0
      }));
139
0
  StackMgr.getTop() = Val;
140
0
  return {};
141
0
}
142
143
Expect<void>
144
Executor::runStructSetOp(Runtime::StackManager &StackMgr, const ValVariant &Val,
145
                         const uint32_t TypeIdx, const uint32_t Off,
146
0
                         const AST::Instruction &Instr) const noexcept {
147
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
148
0
  EXPECTED_TRY(
149
0
      structSet(StackMgr, Ref, Val, TypeIdx, Off).map_error([&](auto E) {
150
0
        return logError(E, Instr);
151
0
      }));
152
0
  return {};
153
0
}
154
155
Expect<void> Executor::runArrayNewOp(Runtime::StackManager &StackMgr,
156
                                     const uint32_t TypeIdx,
157
                                     const uint32_t InitCnt,
158
0
                                     uint32_t Length) const noexcept {
159
0
  assuming(InitCnt == 0 || InitCnt == 1 || InitCnt == Length);
160
0
  if (InitCnt == 0) {
161
0
    StackMgr.push(*arrayNew(StackMgr, TypeIdx, Length));
162
0
  } else if (InitCnt == 1) {
163
0
    StackMgr.getTop().emplace<RefVariant>(
164
0
        *arrayNew(StackMgr, TypeIdx, Length, {StackMgr.getTop()}));
165
0
  } else {
166
0
    StackMgr.push(*arrayNew(StackMgr, TypeIdx, Length, StackMgr.pop(Length)));
167
0
  }
168
0
  return {};
169
0
}
170
171
Expect<void>
172
Executor::runArrayNewDataOp(Runtime::StackManager &StackMgr,
173
                            const uint32_t TypeIdx, const uint32_t DataIdx,
174
0
                            const AST::Instruction &Instr) const noexcept {
175
0
  const uint32_t Length = StackMgr.pop().get<uint32_t>();
176
0
  const uint32_t Start = StackMgr.getTop().get<uint32_t>();
177
0
  EXPECTED_TRY(
178
0
      auto InstRef,
179
0
      arrayNewData(StackMgr, TypeIdx, DataIdx, Start, Length)
180
0
          .map_error([&](auto E) {
181
0
            auto *DataInst = getDataInstByIdx(StackMgr, DataIdx);
182
0
            const uint32_t BSize =
183
0
                getArrayStorageTypeByIdx(StackMgr, TypeIdx).getBitWidth() / 8;
184
0
            return logError(E, Instr, [&]() {
185
0
              return logMemoryOOB(E, *DataInst, Start, BSize * Length);
186
0
            });
187
0
          }));
188
0
  StackMgr.getTop().emplace<RefVariant>(InstRef);
189
0
  return {};
190
0
}
191
192
Expect<void>
193
Executor::runArrayNewElemOp(Runtime::StackManager &StackMgr,
194
                            const uint32_t TypeIdx, const uint32_t ElemIdx,
195
0
                            const AST::Instruction &Instr) const noexcept {
196
0
  const uint32_t Length = StackMgr.pop().get<uint32_t>();
197
0
  const uint32_t Start = StackMgr.getTop().get<uint32_t>();
198
0
  EXPECTED_TRY(auto InstRef,
199
0
               arrayNewElem(StackMgr, TypeIdx, ElemIdx, Start, Length)
200
0
                   .map_error([&](auto E) {
201
0
                     auto *ElemInst = getElemInstByIdx(StackMgr, 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(
217
0
      auto Val,
218
0
      arrayGet(StackMgr, Ref, TypeIdx, Idx, IsSigned).map_error([&](auto E) {
219
0
        return logError(E, Instr,
220
0
                        [&]() { return logArrayOOB(E, Idx, 1, Ref); });
221
0
      }));
222
0
  StackMgr.getTop() = Val;
223
0
  return {};
224
0
}
225
226
Expect<void>
227
Executor::runArraySetOp(Runtime::StackManager &StackMgr, const ValVariant &Val,
228
                        const uint32_t TypeIdx,
229
0
                        const AST::Instruction &Instr) const noexcept {
230
0
  const uint32_t Idx = StackMgr.pop().get<uint32_t>();
231
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
232
0
  EXPECTED_TRY(
233
0
      arraySet(StackMgr, Ref, Val, TypeIdx, Idx).map_error([&](auto E) {
234
0
        return logError(E, Instr,
235
0
                        [&]() { return logArrayOOB(E, Idx, 1, Ref); });
236
0
      }));
237
0
  return {};
238
0
}
239
240
Expect<void>
241
Executor::runArrayLenOp(ValVariant &Val,
242
0
                        const AST::Instruction &Instr) const noexcept {
243
0
  const auto *Inst =
244
0
      Val.get<RefVariant>().getPtr<Runtime::Instance::ArrayInstance>();
245
0
  if (Inst == nullptr) {
246
0
    return Unexpect(logError(ErrCode::Value::AccessNullArray, Instr));
247
0
  }
248
0
  Val.emplace<uint32_t>(Inst->getLength());
249
0
  return {};
250
0
}
251
252
Expect<void>
253
Executor::runArrayFillOp(Runtime::StackManager &StackMgr, const uint32_t Cnt,
254
                         const ValVariant &Val, const uint32_t TypeIdx,
255
0
                         const AST::Instruction &Instr) const noexcept {
256
0
  const uint32_t Idx = StackMgr.pop().get<uint32_t>();
257
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
258
0
  EXPECTED_TRY(
259
0
      arrayFill(StackMgr, Ref, Val, TypeIdx, Idx, Cnt).map_error([&](auto E) {
260
0
        return logError(E, Instr,
261
0
                        [&]() { return logArrayOOB(E, Idx, Cnt, Ref); });
262
0
      }));
263
0
  return {};
264
0
}
265
266
Expect<void>
267
Executor::runArrayCopyOp(Runtime::StackManager &StackMgr, const uint32_t Cnt,
268
                         const uint32_t DstTypeIdx, const uint32_t SrcTypeIdx,
269
0
                         const AST::Instruction &Instr) const noexcept {
270
0
  const uint32_t SrcIdx = StackMgr.pop().get<uint32_t>();
271
0
  const RefVariant SrcRef = StackMgr.pop().get<RefVariant>();
272
0
  const uint32_t DstIdx = StackMgr.pop().get<uint32_t>();
273
0
  const RefVariant DstRef = StackMgr.pop().get<RefVariant>();
274
0
  EXPECTED_TRY(arrayCopy(StackMgr, DstRef, DstTypeIdx, DstIdx, SrcRef,
275
0
                         SrcTypeIdx, SrcIdx, Cnt)
276
0
                   .map_error([&](auto E) {
277
0
                     return logError(E, Instr, [&]() {
278
0
                       return logDoubleArrayOOB(E, SrcIdx, Cnt, SrcRef, DstIdx,
279
0
                                                Cnt, DstRef);
280
0
                     });
281
0
                   }));
282
0
  return {};
283
0
}
284
285
Expect<void> Executor::runArrayInitDataOp(
286
    Runtime::StackManager &StackMgr, const uint32_t Cnt, const uint32_t TypeIdx,
287
0
    const uint32_t DataIdx, const AST::Instruction &Instr) const noexcept {
288
0
  const uint32_t SrcIdx = StackMgr.pop().get<uint32_t>();
289
0
  const uint32_t DstIdx = StackMgr.pop().get<uint32_t>();
290
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
291
0
  EXPECTED_TRY(
292
0
      arrayInitData(StackMgr, Ref, TypeIdx, DataIdx, DstIdx, SrcIdx, Cnt)
293
0
          .map_error([&](auto E) {
294
0
            auto *DataInst = getDataInstByIdx(StackMgr, DataIdx);
295
0
            const uint32_t BSize =
296
0
                getArrayStorageTypeByIdx(StackMgr, TypeIdx).getBitWidth() / 8;
297
0
            return logError(
298
0
                E, Instr, [&]() { return logArrayOOB(E, DstIdx, Cnt, Ref); },
299
0
                [&]() {
300
0
                  return logMemoryOOB(E, *DataInst, SrcIdx, Cnt * BSize);
301
0
                });
302
0
          }));
303
0
  return {};
304
0
}
305
306
Expect<void> Executor::runArrayInitElemOp(
307
    Runtime::StackManager &StackMgr, const uint32_t Cnt, const uint32_t TypeIdx,
308
0
    const uint32_t ElemIdx, const AST::Instruction &Instr) const noexcept {
309
0
  const uint32_t SrcIdx = StackMgr.pop().get<uint32_t>();
310
0
  const uint32_t DstIdx = StackMgr.pop().get<uint32_t>();
311
0
  const RefVariant Ref = StackMgr.pop().get<RefVariant>();
312
0
  EXPECTED_TRY(
313
0
      arrayInitElem(StackMgr, Ref, TypeIdx, ElemIdx, DstIdx, SrcIdx, Cnt)
314
0
          .map_error([&](auto E) {
315
0
            auto *ElemInst = getElemInstByIdx(StackMgr, ElemIdx);
316
0
            return logError(
317
0
                E, Instr, [&]() { return logArrayOOB(E, DstIdx, Cnt, Ref); },
318
0
                [&]() { return logTableOOB(E, *ElemInst, SrcIdx, Cnt); });
319
0
          }));
320
0
  return {};
321
0
}
322
323
Expect<void>
324
Executor::runRefTestOp(const Runtime::Instance::ModuleInstance *ModInst,
325
                       ValVariant &Val, const AST::Instruction &Instr,
326
0
                       const bool IsCast) const noexcept {
327
  // Copy the value type here due to handling the externalized case.
328
0
  auto VT = Val.get<RefVariant>().getType();
329
0
  if (VT.isExternalized()) {
330
0
    VT = ValType(VT.isNullableRefType() ? TypeCode::RefNull : TypeCode::Ref,
331
0
                 TypeCode::ExternRef);
332
0
  }
333
0
  Span<const AST::SubType *const> GotTypeList = ModInst->getTypeList();
334
0
  if (!VT.isAbsHeapType()) {
335
0
    auto *Inst =
336
0
        Val.get<RefVariant>().getPtr<Runtime::Instance::CompositeBase>();
337
    // Reference must not be nullptr here because the null references are typed
338
    // with the least abstract heap type.
339
0
    assuming(Inst);
340
0
    if (Inst->getModule()) {
341
0
      GotTypeList = Inst->getModule()->getTypeList();
342
0
    }
343
0
  }
344
345
0
  if (AST::TypeMatcher::matchType(ModInst->getTypeList(), Instr.getValType(),
346
0
                                  GotTypeList, VT)) {
347
0
    if (!IsCast) {
348
0
      Val.emplace<uint32_t>(1U);
349
0
    }
350
0
  } else {
351
0
    if (IsCast) {
352
0
      spdlog::error(ErrCode::Value::CastFailed);
353
0
      spdlog::error(ErrInfo::InfoMismatch(Instr.getValType(), VT));
354
0
      spdlog::error(
355
0
          ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset()));
356
0
      return Unexpect(ErrCode::Value::CastFailed);
357
0
    } else {
358
0
      Val.emplace<uint32_t>(0U);
359
0
    }
360
0
  }
361
0
  return {};
362
0
}
363
364
Expect<void> Executor::runRefConvOp(RefVariant &Ref,
365
0
                                    TypeCode TCode) const noexcept {
366
0
  if (TCode == TypeCode::AnyRef) {
367
    // Internalize.
368
0
    if (Ref.isNull()) {
369
0
      Ref = RefVariant(ValType(TypeCode::RefNull, TypeCode::NullRef));
370
0
    } else {
371
0
      Ref.getType().setInternalized();
372
0
      if (Ref.getType().isExternRefType()) {
373
0
        Ref.getType() = ValType(TypeCode::Ref, TypeCode::AnyRef);
374
0
      }
375
0
    }
376
0
  } else {
377
    // Externalize.
378
0
    if (Ref.isNull()) {
379
0
      Ref = RefVariant(ValType(TypeCode::RefNull, TypeCode::NullExternRef));
380
0
    } else {
381
      // Use the externalize flag because the value type information should be
382
      // reserved when a reference being externalized and internalized.
383
0
      Ref.getType().setExternalized();
384
0
    }
385
0
  }
386
0
  return {};
387
0
}
388
389
0
Expect<void> Executor::runRefI31Op(ValVariant &Val) const noexcept {
390
0
  uint32_t RefNum = (Val.get<uint32_t>() & 0x7FFFFFFFU) | 0x80000000U;
391
0
  Val = RefVariant(ValType(TypeCode::Ref, TypeCode::I31Ref),
392
0
                   reinterpret_cast<void *>(static_cast<uint64_t>(RefNum)));
393
0
  return {};
394
0
}
395
396
Expect<void> Executor::runI31GetOp(ValVariant &Val,
397
                                   const AST::Instruction &Instr,
398
0
                                   const bool IsSigned) const noexcept {
399
0
  uint32_t RefNum = static_cast<uint32_t>(
400
0
      reinterpret_cast<uintptr_t>(Val.get<RefVariant>().getPtr<void>()));
401
0
  if ((RefNum & 0x80000000U) == 0) {
402
0
    return Unexpect(logError(ErrCode::Value::AccessNullI31, Instr));
403
0
  }
404
0
  RefNum &= 0x7FFFFFFFU;
405
0
  if (IsSigned) {
406
0
    RefNum |= ((RefNum & 0x40000000U) << 1);
407
0
  }
408
0
  Val.emplace<uint32_t>(RefNum);
409
0
  return {};
410
0
}
411
412
Expect<RefVariant>
413
Executor::structNew(Runtime::StackManager &StackMgr, const uint32_t TypeIdx,
414
0
                    Span<const ValVariant> Args) const noexcept {
415
  /// TODO: The array and struct instances are currently owned by the module
416
  /// instance because they refer to the defined types of the module instances.
417
  /// This may be changed after applying the garbage collection mechanism.
418
0
  const auto &CompType = getCompositeTypeByIdx(StackMgr, TypeIdx);
419
0
  uint32_t N = static_cast<uint32_t>(CompType.getFieldTypes().size());
420
0
  Runtime::Instance::ModuleInstance *ModInst =
421
0
      const_cast<Runtime::Instance::ModuleInstance *>(StackMgr.getModule());
422
0
  std::vector<ValVariant> Vals(N);
423
0
  for (uint32_t I = 0; I < N; I++) {
424
0
    const auto &VType = CompType.getFieldTypes()[I].getStorageType();
425
0
    if (Args.size() > 0) {
426
0
      Vals[I] = packVal(VType, Args[I]);
427
0
    } else {
428
0
      Vals[I] = VType.isRefType()
429
0
                    ? ValVariant(RefVariant(toBottomType(StackMgr, VType)))
430
0
                    : ValVariant(static_cast<uint128_t>(0U));
431
0
    }
432
0
  }
433
0
  WasmEdge::Runtime::Instance::StructInstance *Inst =
434
0
      ModInst->newStruct(TypeIdx, std::move(Vals));
435
0
  return RefVariant(Inst->getDefType(), Inst);
436
0
}
437
438
Expect<ValVariant> Executor::structGet(Runtime::StackManager &StackMgr,
439
                                       const RefVariant Ref,
440
                                       const uint32_t TypeIdx,
441
                                       const uint32_t Off,
442
0
                                       const bool IsSigned) const noexcept {
443
0
  const 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(StackMgr, TypeIdx, Off);
448
0
  return unpackVal(VType, Inst->getField(Off), IsSigned);
449
0
}
450
451
Expect<void> Executor::structSet(Runtime::StackManager &StackMgr,
452
                                 const RefVariant Ref, const ValVariant Val,
453
                                 const uint32_t TypeIdx,
454
0
                                 const uint32_t Off) const noexcept {
455
0
  auto *Inst = Ref.getPtr<Runtime::Instance::StructInstance>();
456
0
  if (Inst == nullptr) {
457
0
    return Unexpect(ErrCode::Value::AccessNullStruct);
458
0
  }
459
0
  const auto &VType = getStructStorageTypeByIdx(StackMgr, TypeIdx, Off);
460
0
  Inst->getField(Off) = packVal(VType, Val);
461
0
  return {};
462
0
}
463
464
Expect<RefVariant>
465
Executor::arrayNew(Runtime::StackManager &StackMgr, const uint32_t TypeIdx,
466
                   const uint32_t Length,
467
0
                   Span<const ValVariant> Args) const noexcept {
468
  /// TODO: The array and struct instances are currently owned by the module
469
  /// instance because they refer to the defined types of the module instances.
470
  /// This may be changed after applying the garbage collection mechanism.
471
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
472
0
  WasmEdge::Runtime::Instance::ArrayInstance *Inst = nullptr;
473
0
  Runtime::Instance::ModuleInstance *ModInst =
474
0
      const_cast<Runtime::Instance::ModuleInstance *>(StackMgr.getModule());
475
0
  if (Args.size() == 0) {
476
    // New and fill with default values.
477
0
    auto InitVal = VType.isRefType()
478
0
                       ? ValVariant(RefVariant(toBottomType(StackMgr, VType)))
479
0
                       : ValVariant(static_cast<uint128_t>(0U));
480
0
    Inst = ModInst->newArray(TypeIdx, Length, InitVal);
481
0
  } else if (Args.size() == 1) {
482
    // Create and fill with the argument value.
483
0
    Inst = ModInst->newArray(TypeIdx, Length, packVal(VType, Args[0]));
484
0
  } else {
485
    // Create with arguments.
486
0
    Inst = ModInst->newArray(
487
0
        TypeIdx,
488
0
        packVals(VType, std::vector<ValVariant>(Args.begin(), Args.end())));
489
0
  }
490
0
  return RefVariant(Inst->getDefType(), Inst);
491
0
}
492
493
Expect<RefVariant>
494
Executor::arrayNewData(Runtime::StackManager &StackMgr, const uint32_t TypeIdx,
495
                       const uint32_t DataIdx, const uint32_t Start,
496
0
                       const uint32_t Length) const noexcept {
497
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
498
0
  const uint32_t BSize = VType.getBitWidth() / 8;
499
0
  auto *DataInst = getDataInstByIdx(StackMgr, DataIdx);
500
0
  assuming(DataInst);
501
0
  if (static_cast<uint64_t>(Start) + static_cast<uint64_t>(Length) * BSize >
502
0
      DataInst->getData().size()) {
503
0
    return Unexpect(ErrCode::Value::MemoryOutOfBounds);
504
0
  }
505
0
  Runtime::Instance::ModuleInstance *ModInst =
506
0
      const_cast<Runtime::Instance::ModuleInstance *>(StackMgr.getModule());
507
0
  std::vector<ValVariant> Args;
508
0
  Args.reserve(Length);
509
0
  for (uint32_t Idx = 0; Idx < Length; Idx++) {
510
    // The value has been packed.
511
0
    Args.push_back(DataInst->loadValue(Start + Idx * BSize, BSize));
512
0
  }
513
0
  WasmEdge::Runtime::Instance::ArrayInstance *Inst =
514
0
      ModInst->newArray(TypeIdx, std::move(Args));
515
0
  return RefVariant(Inst->getDefType(), Inst);
516
0
}
517
518
Expect<RefVariant>
519
Executor::arrayNewElem(Runtime::StackManager &StackMgr, const uint32_t TypeIdx,
520
                       const uint32_t ElemIdx, const uint32_t Start,
521
0
                       const uint32_t Length) const noexcept {
522
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
523
0
  auto *ElemInst = getElemInstByIdx(StackMgr, ElemIdx);
524
0
  assuming(ElemInst);
525
0
  auto ElemSrc = ElemInst->getRefs();
526
0
  if (static_cast<uint64_t>(Start) + static_cast<uint64_t>(Length) >
527
0
      ElemSrc.size()) {
528
0
    return Unexpect(ErrCode::Value::TableOutOfBounds);
529
0
  }
530
0
  std::vector<ValVariant> Refs(ElemSrc.begin() + Start,
531
0
                               ElemSrc.begin() + Start + Length);
532
0
  Runtime::Instance::ModuleInstance *ModInst =
533
0
      const_cast<Runtime::Instance::ModuleInstance *>(StackMgr.getModule());
534
0
  WasmEdge::Runtime::Instance::ArrayInstance *Inst =
535
0
      ModInst->newArray(TypeIdx, packVals(VType, std::move(Refs)));
536
0
  return RefVariant(Inst->getDefType(), Inst);
537
0
}
538
539
Expect<ValVariant> Executor::arrayGet(Runtime::StackManager &StackMgr,
540
                                      const RefVariant &Ref,
541
                                      const uint32_t TypeIdx,
542
                                      const uint32_t Idx,
543
0
                                      const bool IsSigned) const noexcept {
544
0
  const auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
545
0
  if (Inst == nullptr) {
546
0
    return Unexpect(ErrCode::Value::AccessNullArray);
547
0
  }
548
0
  if (Idx >= Inst->getLength()) {
549
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
550
0
  }
551
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
552
0
  return unpackVal(VType, Inst->getData(Idx), IsSigned);
553
0
}
554
555
Expect<void> Executor::arraySet(Runtime::StackManager &StackMgr,
556
                                const RefVariant &Ref, const ValVariant &Val,
557
                                const uint32_t TypeIdx,
558
0
                                const uint32_t Idx) const noexcept {
559
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
560
0
  if (Inst == nullptr) {
561
0
    return Unexpect(ErrCode::Value::AccessNullArray);
562
0
  }
563
0
  if (Idx >= Inst->getLength()) {
564
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
565
0
  }
566
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
567
0
  Inst->getData(Idx) = packVal(VType, Val);
568
0
  return {};
569
0
}
570
571
Expect<void> Executor::arrayFill(Runtime::StackManager &StackMgr,
572
                                 const RefVariant &Ref, const ValVariant &Val,
573
                                 const uint32_t TypeIdx, const uint32_t Idx,
574
0
                                 const uint32_t Cnt) const noexcept {
575
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
576
0
  if (Inst == nullptr) {
577
0
    return Unexpect(ErrCode::Value::AccessNullArray);
578
0
  }
579
0
  if (static_cast<uint64_t>(Idx) + static_cast<uint64_t>(Cnt) >
580
0
      Inst->getLength()) {
581
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
582
0
  }
583
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
584
0
  auto Arr = Inst->getArray();
585
0
  std::fill(Arr.begin() + Idx, Arr.begin() + Idx + Cnt, packVal(VType, Val));
586
0
  return {};
587
0
}
588
589
Expect<void>
590
Executor::arrayInitData(Runtime::StackManager &StackMgr, const RefVariant &Ref,
591
                        const uint32_t TypeIdx, const uint32_t DataIdx,
592
                        const uint32_t DstIdx, const uint32_t SrcIdx,
593
0
                        const uint32_t Cnt) const noexcept {
594
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
595
0
  if (Inst == nullptr) {
596
0
    return Unexpect(ErrCode::Value::AccessNullArray);
597
0
  }
598
0
  if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) >
599
0
      Inst->getLength()) {
600
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
601
0
  }
602
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
603
0
  const uint32_t BSize = VType.getBitWidth() / 8;
604
0
  auto *DataInst = getDataInstByIdx(StackMgr, DataIdx);
605
0
  assuming(DataInst);
606
0
  if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) * BSize >
607
0
      DataInst->getData().size()) {
608
0
    return Unexpect(ErrCode::Value::MemoryOutOfBounds);
609
0
  }
610
611
0
  for (uint32_t Idx = 0; Idx < Cnt; Idx++) {
612
    // The value has been packed.
613
0
    Inst->getData(DstIdx + Idx) =
614
0
        DataInst->loadValue(SrcIdx + Idx * BSize, BSize);
615
0
  }
616
0
  return {};
617
0
}
618
619
Expect<void>
620
Executor::arrayInitElem(Runtime::StackManager &StackMgr, const RefVariant &Ref,
621
                        const uint32_t TypeIdx, const uint32_t ElemIdx,
622
                        const uint32_t DstIdx, const uint32_t SrcIdx,
623
0
                        const uint32_t Cnt) const noexcept {
624
0
  auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>();
625
0
  if (Inst == nullptr) {
626
0
    return Unexpect(ErrCode::Value::AccessNullArray);
627
0
  }
628
0
  if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) >
629
0
      Inst->getLength()) {
630
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
631
0
  }
632
0
  const auto &VType = getArrayStorageTypeByIdx(StackMgr, TypeIdx);
633
0
  auto *ElemInst = getElemInstByIdx(StackMgr, ElemIdx);
634
0
  assuming(ElemInst);
635
0
  auto ElemSrc = ElemInst->getRefs();
636
0
  if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) >
637
0
      ElemSrc.size()) {
638
0
    return Unexpect(ErrCode::Value::TableOutOfBounds);
639
0
  }
640
641
0
  auto Arr = Inst->getArray();
642
  // The value has been packed.
643
0
  std::transform(ElemSrc.begin() + SrcIdx, ElemSrc.begin() + SrcIdx + Cnt,
644
0
                 Arr.begin() + DstIdx,
645
0
                 [&](const RefVariant &V) { return packVal(VType, V); });
646
0
  return {};
647
0
}
648
649
Expect<void>
650
Executor::arrayCopy(Runtime::StackManager &StackMgr, const RefVariant &DstRef,
651
                    const uint32_t DstTypeIdx, const uint32_t DstIdx,
652
                    const RefVariant &SrcRef, const uint32_t SrcTypeIdx,
653
0
                    const uint32_t SrcIdx, const uint32_t Cnt) const noexcept {
654
0
  auto *SrcInst = SrcRef.getPtr<Runtime::Instance::ArrayInstance>();
655
0
  auto *DstInst = DstRef.getPtr<Runtime::Instance::ArrayInstance>();
656
0
  if (SrcInst == nullptr) {
657
0
    return Unexpect(ErrCode::Value::AccessNullArray);
658
0
  }
659
0
  if (DstInst == nullptr) {
660
0
    return Unexpect(ErrCode::Value::AccessNullArray);
661
0
  }
662
0
  if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) >
663
0
      SrcInst->getLength()) {
664
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
665
0
  }
666
0
  if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) >
667
0
      DstInst->getLength()) {
668
0
    return Unexpect(ErrCode::Value::ArrayOutOfBounds);
669
0
  }
670
671
0
  auto SrcArr = SrcInst->getArray();
672
0
  auto DstArr = DstInst->getArray();
673
0
  const auto &SrcVType = getArrayStorageTypeByIdx(StackMgr, SrcTypeIdx);
674
0
  const auto &DstVType = getArrayStorageTypeByIdx(StackMgr, DstTypeIdx);
675
0
  if (DstIdx <= SrcIdx) {
676
0
    std::transform(SrcArr.begin() + SrcIdx, SrcArr.begin() + SrcIdx + Cnt,
677
0
                   DstArr.begin() + DstIdx, [&](const ValVariant &V) {
678
0
                     return packVal(DstVType, unpackVal(SrcVType, V));
679
0
                   });
680
0
  } else {
681
0
    std::transform(std::make_reverse_iterator(SrcArr.begin() + SrcIdx + Cnt),
682
0
                   std::make_reverse_iterator(SrcArr.begin() + SrcIdx),
683
0
                   std::make_reverse_iterator(DstArr.begin() + DstIdx + Cnt),
684
0
                   [&](const ValVariant &V) {
685
0
                     return packVal(DstVType, unpackVal(SrcVType, V));
686
0
                   });
687
0
  }
688
0
  return {};
689
0
}
690
691
} // namespace Executor
692
} // namespace WasmEdge