/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 | | // Copy the value type here due to handling the externalized case. |
335 | 0 | auto VT = Val.get<RefVariant>().getType(); |
336 | 0 | if (VT.isExternalized()) { |
337 | 0 | VT = ValType(VT.isNullableRefType() ? TypeCode::RefNull : TypeCode::Ref, |
338 | 0 | TypeCode::ExternRef); |
339 | 0 | } |
340 | 0 | Span<const AST::SubType *const> GotTypeList = ModInst->getTypeList(); |
341 | 0 | if (!VT.isAbsHeapType()) { |
342 | 0 | auto *Inst = |
343 | 0 | Val.get<RefVariant>().getPtr<Runtime::Instance::CompositeBase>(); |
344 | | // Reference must not be nullptr here because the null references are typed |
345 | | // with the least abstract heap type. |
346 | 0 | assuming(Inst); |
347 | 0 | if (Inst->getModule()) { |
348 | 0 | GotTypeList = Inst->getModule()->getTypeList(); |
349 | 0 | } |
350 | 0 | } |
351 | | |
352 | 0 | if (AST::TypeMatcher::matchType(ModInst->getTypeList(), Instr.getValType(), |
353 | 0 | GotTypeList, VT)) { |
354 | 0 | if (!IsCast) { |
355 | 0 | Val.emplace<uint32_t>(1U); |
356 | 0 | } |
357 | 0 | } else { |
358 | 0 | if (IsCast) { |
359 | 0 | spdlog::error(ErrCode::Value::CastFailed); |
360 | 0 | spdlog::error(ErrInfo::InfoMismatch(Instr.getValType(), VT)); |
361 | 0 | spdlog::error( |
362 | 0 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
363 | 0 | return Unexpect(ErrCode::Value::CastFailed); |
364 | 0 | } else { |
365 | 0 | Val.emplace<uint32_t>(0U); |
366 | 0 | } |
367 | 0 | } |
368 | 0 | return {}; |
369 | 0 | } |
370 | | |
371 | | Expect<void> Executor::runRefConvOp(RefVariant &Ref, |
372 | 0 | TypeCode TCode) const noexcept { |
373 | 0 | if (TCode == TypeCode::AnyRef) { |
374 | | // Internalize. |
375 | 0 | if (Ref.isNull()) { |
376 | 0 | Ref = RefVariant(ValType(TypeCode::RefNull, TypeCode::NullRef)); |
377 | 0 | } else { |
378 | 0 | Ref.getType().setInternalized(); |
379 | 0 | if (Ref.getType().isExternRefType()) { |
380 | 0 | Ref.getType() = ValType(TypeCode::Ref, TypeCode::AnyRef); |
381 | 0 | } |
382 | 0 | } |
383 | 0 | } else { |
384 | | // Externalize. |
385 | 0 | if (Ref.isNull()) { |
386 | 0 | Ref = RefVariant(ValType(TypeCode::RefNull, TypeCode::NullExternRef)); |
387 | 0 | } else { |
388 | | // Use the externalize flag because the value type information should be |
389 | | // reserved when a reference being externalized and internalized. |
390 | 0 | Ref.getType().setExternalized(); |
391 | 0 | } |
392 | 0 | } |
393 | 0 | return {}; |
394 | 0 | } |
395 | | |
396 | 0 | Expect<void> Executor::runRefI31Op(ValVariant &Val) const noexcept { |
397 | 0 | uint32_t RefNum = (Val.get<uint32_t>() & 0x7FFFFFFFU) | 0x80000000U; |
398 | 0 | Val = RefVariant(ValType(TypeCode::Ref, TypeCode::I31Ref), |
399 | 0 | reinterpret_cast<void *>(static_cast<uint64_t>(RefNum))); |
400 | 0 | return {}; |
401 | 0 | } |
402 | | |
403 | | Expect<void> Executor::runI31GetOp(ValVariant &Val, |
404 | | const AST::Instruction &Instr, |
405 | 0 | const bool IsSigned) const noexcept { |
406 | 0 | uint32_t RefNum = static_cast<uint32_t>( |
407 | 0 | reinterpret_cast<uintptr_t>(Val.get<RefVariant>().getPtr<void>())); |
408 | 0 | if ((RefNum & 0x80000000U) == 0) { |
409 | 0 | return Unexpect(logError(ErrCode::Value::AccessNullI31, Instr)); |
410 | 0 | } |
411 | 0 | RefNum &= 0x7FFFFFFFU; |
412 | 0 | if (IsSigned) { |
413 | 0 | RefNum |= ((RefNum & 0x40000000U) << 1); |
414 | 0 | } |
415 | 0 | Val.emplace<uint32_t>(RefNum); |
416 | 0 | return {}; |
417 | 0 | } |
418 | | |
419 | | Expect<RefVariant> |
420 | | Executor::structNew(const Runtime::Instance::ModuleInstance *ModInstArg, |
421 | | const uint32_t TypeIdx, |
422 | 0 | Span<const ValVariant> Args) const noexcept { |
423 | | /// TODO: The array and struct instances are currently owned by the module |
424 | | /// instance because they refer to the defined types of the module instances. |
425 | | /// This may be changed after applying the garbage collection mechanism. |
426 | 0 | const auto &CompType = getCompositeTypeByIdx(ModInstArg, TypeIdx); |
427 | 0 | uint32_t N = static_cast<uint32_t>(CompType.getFieldTypes().size()); |
428 | 0 | auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg); |
429 | 0 | std::vector<ValVariant> Vals(N); |
430 | 0 | for (uint32_t I = 0; I < N; I++) { |
431 | 0 | const auto &VType = CompType.getFieldTypes()[I].getStorageType(); |
432 | 0 | if (Args.size() > 0) { |
433 | 0 | Vals[I] = packVal(VType, Args[I]); |
434 | 0 | } else { |
435 | 0 | Vals[I] = VType.isRefType() |
436 | 0 | ? ValVariant(RefVariant(toBottomType(ModInstArg, VType))) |
437 | 0 | : ValVariant(static_cast<uint128_t>(0U)); |
438 | 0 | } |
439 | 0 | } |
440 | 0 | WasmEdge::Runtime::Instance::StructInstance *Inst = |
441 | 0 | ModInst->newStruct(TypeIdx, std::move(Vals)); |
442 | 0 | return RefVariant(Inst->getDefType(), Inst); |
443 | 0 | } |
444 | | |
445 | | Expect<ValVariant> |
446 | | Executor::structGet(const Runtime::Instance::ModuleInstance *ModInst, |
447 | | const RefVariant Ref, const uint32_t TypeIdx, |
448 | 0 | const uint32_t Off, const bool IsSigned) const noexcept { |
449 | 0 | const auto *Inst = Ref.getPtr<Runtime::Instance::StructInstance>(); |
450 | 0 | if (Inst == nullptr) { |
451 | 0 | return Unexpect(ErrCode::Value::AccessNullStruct); |
452 | 0 | } |
453 | 0 | const auto &VType = getStructStorageTypeByIdx(ModInst, TypeIdx, Off); |
454 | 0 | return unpackVal(VType, Inst->getField(Off), IsSigned); |
455 | 0 | } |
456 | | |
457 | | Expect<void> |
458 | | Executor::structSet(const Runtime::Instance::ModuleInstance *ModInst, |
459 | | const RefVariant Ref, const ValVariant Val, |
460 | 0 | const uint32_t TypeIdx, const uint32_t Off) const noexcept { |
461 | 0 | auto *Inst = Ref.getPtr<Runtime::Instance::StructInstance>(); |
462 | 0 | if (Inst == nullptr) { |
463 | 0 | return Unexpect(ErrCode::Value::AccessNullStruct); |
464 | 0 | } |
465 | 0 | const auto &VType = getStructStorageTypeByIdx(ModInst, TypeIdx, Off); |
466 | 0 | Inst->getField(Off) = packVal(VType, Val); |
467 | 0 | return {}; |
468 | 0 | } |
469 | | |
470 | | Expect<RefVariant> |
471 | | Executor::arrayNew(const Runtime::Instance::ModuleInstance *ModInstArg, |
472 | | const uint32_t TypeIdx, const uint32_t Length, |
473 | 0 | Span<const ValVariant> Args) const noexcept { |
474 | | /// TODO: The array and struct instances are currently owned by the module |
475 | | /// instance because they refer to the defined types of the module instances. |
476 | | /// This may be changed after applying the garbage collection mechanism. |
477 | 0 | const auto &VType = getArrayStorageTypeByIdx(ModInstArg, TypeIdx); |
478 | 0 | WasmEdge::Runtime::Instance::ArrayInstance *Inst = nullptr; |
479 | 0 | auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg); |
480 | 0 | if (Args.size() == 0) { |
481 | | // New and fill with default values. |
482 | 0 | auto InitVal = VType.isRefType() |
483 | 0 | ? ValVariant(RefVariant(toBottomType(ModInstArg, VType))) |
484 | 0 | : ValVariant(static_cast<uint128_t>(0U)); |
485 | 0 | Inst = ModInst->newArray(TypeIdx, Length, InitVal); |
486 | 0 | } else if (Args.size() == 1) { |
487 | | // Create and fill with the argument value. |
488 | 0 | Inst = ModInst->newArray(TypeIdx, Length, packVal(VType, Args[0])); |
489 | 0 | } else { |
490 | | // Create with arguments. |
491 | 0 | Inst = ModInst->newArray( |
492 | 0 | TypeIdx, |
493 | 0 | packVals(VType, std::vector<ValVariant>(Args.begin(), Args.end()))); |
494 | 0 | } |
495 | 0 | return RefVariant(Inst->getDefType(), Inst); |
496 | 0 | } |
497 | | |
498 | | Expect<RefVariant> |
499 | | Executor::arrayNewData(const Runtime::Instance::ModuleInstance *ModInstArg, |
500 | | const uint32_t TypeIdx, const uint32_t DataIdx, |
501 | | const uint32_t Start, |
502 | 0 | const uint32_t Length) const noexcept { |
503 | 0 | const auto &VType = getArrayStorageTypeByIdx(ModInstArg, TypeIdx); |
504 | 0 | const uint32_t BSize = VType.getBitWidth() / 8; |
505 | 0 | auto *DataInst = getDataInstByIdx(ModInstArg, DataIdx); |
506 | 0 | assuming(DataInst); |
507 | 0 | if (static_cast<uint64_t>(Start) + static_cast<uint64_t>(Length) * BSize > |
508 | 0 | DataInst->getData().size()) { |
509 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
510 | 0 | } |
511 | 0 | auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg); |
512 | 0 | std::vector<ValVariant> Args; |
513 | 0 | Args.reserve(Length); |
514 | 0 | for (uint32_t Idx = 0; Idx < Length; Idx++) { |
515 | | // The value has been packed. |
516 | 0 | Args.push_back(DataInst->loadValue(Start + Idx * BSize, BSize)); |
517 | 0 | } |
518 | 0 | WasmEdge::Runtime::Instance::ArrayInstance *Inst = |
519 | 0 | ModInst->newArray(TypeIdx, std::move(Args)); |
520 | 0 | return RefVariant(Inst->getDefType(), Inst); |
521 | 0 | } |
522 | | |
523 | | Expect<RefVariant> |
524 | | Executor::arrayNewElem(const Runtime::Instance::ModuleInstance *ModInstArg, |
525 | | const uint32_t TypeIdx, const uint32_t ElemIdx, |
526 | | const uint32_t Start, |
527 | 0 | const uint32_t Length) const noexcept { |
528 | 0 | const auto &VType = getArrayStorageTypeByIdx(ModInstArg, TypeIdx); |
529 | 0 | auto *ElemInst = getElemInstByIdx(ModInstArg, ElemIdx); |
530 | 0 | assuming(ElemInst); |
531 | 0 | auto ElemSrc = ElemInst->getRefs(); |
532 | 0 | if (static_cast<uint64_t>(Start) + static_cast<uint64_t>(Length) > |
533 | 0 | ElemSrc.size()) { |
534 | 0 | return Unexpect(ErrCode::Value::TableOutOfBounds); |
535 | 0 | } |
536 | 0 | std::vector<ValVariant> Refs(ElemSrc.begin() + Start, |
537 | 0 | ElemSrc.begin() + Start + Length); |
538 | 0 | auto *ModInst = const_cast<Runtime::Instance::ModuleInstance *>(ModInstArg); |
539 | 0 | WasmEdge::Runtime::Instance::ArrayInstance *Inst = |
540 | 0 | ModInst->newArray(TypeIdx, packVals(VType, std::move(Refs))); |
541 | 0 | return RefVariant(Inst->getDefType(), Inst); |
542 | 0 | } |
543 | | |
544 | | Expect<ValVariant> |
545 | | Executor::arrayGet(const Runtime::Instance::ModuleInstance *ModInst, |
546 | | const RefVariant &Ref, const uint32_t TypeIdx, |
547 | 0 | const uint32_t Idx, const bool IsSigned) const noexcept { |
548 | 0 | const auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>(); |
549 | 0 | if (Inst == nullptr) { |
550 | 0 | return Unexpect(ErrCode::Value::AccessNullArray); |
551 | 0 | } |
552 | 0 | if (Idx >= Inst->getLength()) { |
553 | 0 | return Unexpect(ErrCode::Value::ArrayOutOfBounds); |
554 | 0 | } |
555 | 0 | const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx); |
556 | 0 | return unpackVal(VType, Inst->getData(Idx), IsSigned); |
557 | 0 | } |
558 | | |
559 | | Expect<void> |
560 | | Executor::arraySet(const Runtime::Instance::ModuleInstance *ModInst, |
561 | | const RefVariant &Ref, const ValVariant &Val, |
562 | 0 | const uint32_t TypeIdx, const uint32_t Idx) const noexcept { |
563 | 0 | auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>(); |
564 | 0 | if (Inst == nullptr) { |
565 | 0 | return Unexpect(ErrCode::Value::AccessNullArray); |
566 | 0 | } |
567 | 0 | if (Idx >= Inst->getLength()) { |
568 | 0 | return Unexpect(ErrCode::Value::ArrayOutOfBounds); |
569 | 0 | } |
570 | 0 | const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx); |
571 | 0 | Inst->getData(Idx) = packVal(VType, Val); |
572 | 0 | return {}; |
573 | 0 | } |
574 | | |
575 | | Expect<void> |
576 | | Executor::arrayFill(const Runtime::Instance::ModuleInstance *ModInst, |
577 | | const RefVariant &Ref, const ValVariant &Val, |
578 | | const uint32_t TypeIdx, const uint32_t Idx, |
579 | 0 | 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>(Idx) + 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 | auto Arr = Inst->getArray(); |
590 | 0 | std::fill(Arr.begin() + Idx, Arr.begin() + Idx + Cnt, packVal(VType, Val)); |
591 | 0 | return {}; |
592 | 0 | } |
593 | | |
594 | | Expect<void> Executor::arrayInitData( |
595 | | const Runtime::Instance::ModuleInstance *ModInst, const RefVariant &Ref, |
596 | | const uint32_t TypeIdx, const uint32_t DataIdx, const uint32_t DstIdx, |
597 | 0 | const uint32_t SrcIdx, const uint32_t Cnt) const noexcept { |
598 | 0 | auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>(); |
599 | 0 | if (Inst == nullptr) { |
600 | 0 | return Unexpect(ErrCode::Value::AccessNullArray); |
601 | 0 | } |
602 | 0 | if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) > |
603 | 0 | Inst->getLength()) { |
604 | 0 | return Unexpect(ErrCode::Value::ArrayOutOfBounds); |
605 | 0 | } |
606 | 0 | const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx); |
607 | 0 | const uint32_t BSize = VType.getBitWidth() / 8; |
608 | 0 | auto *DataInst = getDataInstByIdx(ModInst, DataIdx); |
609 | 0 | assuming(DataInst); |
610 | 0 | if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) * BSize > |
611 | 0 | DataInst->getData().size()) { |
612 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
613 | 0 | } |
614 | | |
615 | 0 | for (uint32_t Idx = 0; Idx < Cnt; Idx++) { |
616 | | // The value has been packed. |
617 | 0 | Inst->getData(DstIdx + Idx) = |
618 | 0 | DataInst->loadValue(SrcIdx + Idx * BSize, BSize); |
619 | 0 | } |
620 | 0 | return {}; |
621 | 0 | } |
622 | | |
623 | | Expect<void> Executor::arrayInitElem( |
624 | | const Runtime::Instance::ModuleInstance *ModInst, const RefVariant &Ref, |
625 | | const uint32_t TypeIdx, const uint32_t ElemIdx, const uint32_t DstIdx, |
626 | 0 | const uint32_t SrcIdx, const uint32_t Cnt) const noexcept { |
627 | 0 | auto *Inst = Ref.getPtr<Runtime::Instance::ArrayInstance>(); |
628 | 0 | if (Inst == nullptr) { |
629 | 0 | return Unexpect(ErrCode::Value::AccessNullArray); |
630 | 0 | } |
631 | 0 | if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) > |
632 | 0 | Inst->getLength()) { |
633 | 0 | return Unexpect(ErrCode::Value::ArrayOutOfBounds); |
634 | 0 | } |
635 | 0 | const auto &VType = getArrayStorageTypeByIdx(ModInst, TypeIdx); |
636 | 0 | auto *ElemInst = getElemInstByIdx(ModInst, ElemIdx); |
637 | 0 | assuming(ElemInst); |
638 | 0 | auto ElemSrc = ElemInst->getRefs(); |
639 | 0 | if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) > |
640 | 0 | ElemSrc.size()) { |
641 | 0 | return Unexpect(ErrCode::Value::TableOutOfBounds); |
642 | 0 | } |
643 | | |
644 | 0 | auto Arr = Inst->getArray(); |
645 | | // The value has been packed. |
646 | 0 | std::transform(ElemSrc.begin() + SrcIdx, ElemSrc.begin() + SrcIdx + Cnt, |
647 | 0 | Arr.begin() + DstIdx, |
648 | 0 | [&](const RefVariant &V) { return packVal(VType, V); }); |
649 | 0 | return {}; |
650 | 0 | } |
651 | | |
652 | | Expect<void> |
653 | | Executor::arrayCopy(const Runtime::Instance::ModuleInstance *ModInst, |
654 | | const RefVariant &DstRef, const uint32_t DstTypeIdx, |
655 | | const uint32_t DstIdx, const RefVariant &SrcRef, |
656 | | const uint32_t SrcTypeIdx, const uint32_t SrcIdx, |
657 | 0 | const uint32_t Cnt) const noexcept { |
658 | 0 | auto *SrcInst = SrcRef.getPtr<Runtime::Instance::ArrayInstance>(); |
659 | 0 | auto *DstInst = DstRef.getPtr<Runtime::Instance::ArrayInstance>(); |
660 | 0 | if (SrcInst == nullptr) { |
661 | 0 | return Unexpect(ErrCode::Value::AccessNullArray); |
662 | 0 | } |
663 | 0 | if (DstInst == nullptr) { |
664 | 0 | return Unexpect(ErrCode::Value::AccessNullArray); |
665 | 0 | } |
666 | 0 | if (static_cast<uint64_t>(SrcIdx) + static_cast<uint64_t>(Cnt) > |
667 | 0 | SrcInst->getLength()) { |
668 | 0 | return Unexpect(ErrCode::Value::ArrayOutOfBounds); |
669 | 0 | } |
670 | 0 | if (static_cast<uint64_t>(DstIdx) + static_cast<uint64_t>(Cnt) > |
671 | 0 | DstInst->getLength()) { |
672 | 0 | return Unexpect(ErrCode::Value::ArrayOutOfBounds); |
673 | 0 | } |
674 | | |
675 | 0 | auto SrcArr = SrcInst->getArray(); |
676 | 0 | auto DstArr = DstInst->getArray(); |
677 | 0 | const auto &SrcVType = getArrayStorageTypeByIdx(ModInst, SrcTypeIdx); |
678 | 0 | const auto &DstVType = getArrayStorageTypeByIdx(ModInst, DstTypeIdx); |
679 | 0 | if (DstIdx <= SrcIdx) { |
680 | 0 | std::transform(SrcArr.begin() + SrcIdx, SrcArr.begin() + SrcIdx + Cnt, |
681 | 0 | DstArr.begin() + DstIdx, [&](const ValVariant &V) { |
682 | 0 | return packVal(DstVType, unpackVal(SrcVType, V)); |
683 | 0 | }); |
684 | 0 | } else { |
685 | 0 | std::transform(std::make_reverse_iterator(SrcArr.begin() + SrcIdx + Cnt), |
686 | 0 | std::make_reverse_iterator(SrcArr.begin() + SrcIdx), |
687 | 0 | std::make_reverse_iterator(DstArr.begin() + DstIdx + Cnt), |
688 | 0 | [&](const ValVariant &V) { |
689 | 0 | return packVal(DstVType, unpackVal(SrcVType, V)); |
690 | 0 | }); |
691 | 0 | } |
692 | 0 | return {}; |
693 | 0 | } |
694 | | |
695 | | } // namespace Executor |
696 | | } // namespace WasmEdge |