/src/WasmEdge/include/runtime/instance/memory.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | //===-- wasmedge/runtime/instance/memory.h - Memory Instance definition ---===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the memory instance definition in store manager. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/type.h" |
17 | | #include "common/errcode.h" |
18 | | #include "common/errinfo.h" |
19 | | #include "common/spdlog.h" |
20 | | #include "system/allocator.h" |
21 | | |
22 | | #include <algorithm> |
23 | | #include <cstdint> |
24 | | #include <cstring> |
25 | | #include <fstream> |
26 | | #include <memory> |
27 | | #include <set> |
28 | | #include <utility> |
29 | | |
30 | | namespace WasmEdge { |
31 | | namespace Runtime { |
32 | | namespace Instance { |
33 | | |
34 | | class MemoryInstance { |
35 | | |
36 | | public: |
37 | | static inline constexpr const uint64_t kPageSize = UINT64_C(65536); |
38 | | static inline constexpr const uint64_t k4G = UINT64_C(0x100000000); |
39 | | MemoryInstance() = delete; |
40 | | MemoryInstance(MemoryInstance &&Inst) noexcept |
41 | | : MemType(Inst.MemType), DataPtr(Inst.DataPtr), |
42 | 0 | PageLimit(Inst.PageLimit) { |
43 | 0 | Inst.DataPtr = nullptr; |
44 | 0 | } |
45 | | MemoryInstance(const AST::MemoryType &MType, |
46 | | uint32_t PageLim = UINT32_C(65536)) noexcept |
47 | 0 | : MemType(MType), PageLimit(PageLim) { |
48 | 0 | using namespace std::literals; |
49 | 0 | if (MemType.getLimit().getMin() > PageLimit) { |
50 | 0 | spdlog::error("Memory Instance: Limited {} page in configuration."sv, |
51 | 0 | PageLimit); |
52 | 0 | MemType.getLimit().setMin(PageLimit); |
53 | 0 | } |
54 | 0 | DataPtr = Allocator::allocate(MemType.getLimit().getMin()); |
55 | 0 | if (DataPtr == nullptr) { |
56 | 0 | spdlog::error("Memory Instance: Unable to find usable memory address."sv); |
57 | 0 | MemType.getLimit().setMin(0U); |
58 | 0 | return; |
59 | 0 | } |
60 | 0 | } |
61 | 0 | ~MemoryInstance() noexcept { |
62 | 0 | Allocator::release(DataPtr, MemType.getLimit().getMin()); |
63 | 0 | } |
64 | | |
65 | 0 | bool isShared() const noexcept { return MemType.getLimit().isShared(); } |
66 | | |
67 | | /// Get page size of memory.data |
68 | 0 | uint32_t getPageSize() const noexcept { |
69 | | // The memory page size is binded with the limit in memory type. |
70 | 0 | return MemType.getLimit().getMin(); |
71 | 0 | } |
72 | | |
73 | | /// Getter of memory type. |
74 | 0 | const AST::MemoryType &getMemoryType() const noexcept { return MemType; } |
75 | | |
76 | | /// Check access size is valid. |
77 | 0 | bool checkAccessBound(uint32_t Offset, uint32_t Length) const noexcept { |
78 | 0 | const uint64_t AccessLen = |
79 | 0 | static_cast<uint64_t>(Offset) + static_cast<uint64_t>(Length); |
80 | 0 | return AccessLen <= MemType.getLimit().getMin() * kPageSize; |
81 | 0 | } |
82 | | |
83 | | /// Get boundary index. |
84 | 0 | uint32_t getBoundIdx() const noexcept { |
85 | 0 | return MemType.getLimit().getMin() > 0 |
86 | 0 | ? MemType.getLimit().getMin() * kPageSize - 1 |
87 | 0 | : 0; |
88 | 0 | } |
89 | | |
90 | | /// Grow page |
91 | 0 | bool growPage(const uint32_t Count) { |
92 | 0 | if (Count == 0) { |
93 | 0 | return true; |
94 | 0 | } |
95 | | // Maximum pages count, 65536 |
96 | 0 | uint32_t MaxPageCaped = k4G / kPageSize; |
97 | 0 | uint32_t Min = MemType.getLimit().getMin(); |
98 | 0 | assuming(MaxPageCaped >= Min); |
99 | 0 | if (MemType.getLimit().hasMax()) { |
100 | 0 | uint32_t Max = MemType.getLimit().getMax(); |
101 | 0 | assuming(Max >= Min); |
102 | 0 | MaxPageCaped = std::min(Max, MaxPageCaped); |
103 | 0 | } |
104 | 0 | if (Count > MaxPageCaped - Min) { |
105 | 0 | return false; |
106 | 0 | } |
107 | 0 | assuming(PageLimit >= Min); |
108 | 0 | if (Count > PageLimit - Min) { |
109 | 0 | spdlog::error("Memory Instance: Memory grow page failed, exceeded " |
110 | 0 | "limited {} page size in configuration.", |
111 | 0 | PageLimit); |
112 | 0 | return false; |
113 | 0 | } |
114 | 0 | if (auto NewPtr = Allocator::resize(DataPtr, Min, Min + Count); |
115 | 0 | NewPtr == nullptr) { |
116 | 0 | return false; |
117 | 0 | } else { |
118 | 0 | DataPtr = NewPtr; |
119 | 0 | } |
120 | 0 | MemType.getLimit().setMin(Min + Count); |
121 | 0 | return true; |
122 | 0 | } |
123 | | |
124 | | /// Get slice of Data[Offset : Offset + Length - 1] |
125 | 0 | Expect<Span<Byte>> getBytes(uint32_t Offset, uint32_t Length) const noexcept { |
126 | | // Check the memory boundary. |
127 | 0 | if (unlikely(!checkAccessBound(Offset, Length))) { |
128 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
129 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
130 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
131 | 0 | } |
132 | 0 | return Span<Byte>(&DataPtr[Offset], Length); |
133 | 0 | } |
134 | | |
135 | | /// Replace the bytes of Data[Offset :] by Slice[Start : Start + Length - 1] |
136 | | Expect<void> setBytes(Span<const Byte> Slice, uint32_t Offset, uint32_t Start, |
137 | 0 | uint32_t Length) noexcept { |
138 | | // Check the memory boundary. |
139 | 0 | if (unlikely(!checkAccessBound(Offset, Length))) { |
140 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
141 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
142 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
143 | 0 | } |
144 | | |
145 | | // Check the input data validation. |
146 | 0 | if (unlikely(static_cast<uint64_t>(Start) + static_cast<uint64_t>(Length) > |
147 | 0 | Slice.size())) { |
148 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
149 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
150 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
151 | 0 | } |
152 | | |
153 | | // Copy the data. |
154 | 0 | if (likely(Length > 0)) { |
155 | 0 | std::copy(Slice.begin() + Start, Slice.begin() + Start + Length, |
156 | 0 | DataPtr + Offset); |
157 | 0 | } |
158 | 0 | return {}; |
159 | 0 | } |
160 | | |
161 | | /// Fill the bytes of Data[Offset : Offset + Length - 1] by Val. |
162 | | Expect<void> fillBytes(uint8_t Val, uint32_t Offset, |
163 | 0 | uint32_t Length) noexcept { |
164 | | // Check the memory boundary. |
165 | 0 | if (unlikely(!checkAccessBound(Offset, Length))) { |
166 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
167 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
168 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
169 | 0 | } |
170 | | |
171 | | // Copy the data. |
172 | 0 | if (likely(Length > 0)) { |
173 | 0 | std::fill(DataPtr + Offset, DataPtr + Offset + Length, Val); |
174 | 0 | } |
175 | 0 | return {}; |
176 | 0 | } |
177 | | |
178 | | /// Get an uint8 array from Data[Offset : Offset + Length - 1] |
179 | | Expect<void> getArray(uint8_t *Arr, uint32_t Offset, uint32_t Length, |
180 | 0 | bool IsReverse = false) const noexcept { |
181 | 0 | // Check the memory boundary. |
182 | 0 | if (unlikely(!checkAccessBound(Offset, Length))) { |
183 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
184 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
185 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
186 | 0 | } |
187 | 0 | if (likely(Length > 0)) { |
188 | 0 | // Copy the data. |
189 | 0 | if (IsReverse) { |
190 | 0 | std::reverse_copy(DataPtr + Offset, DataPtr + Offset + Length, Arr); |
191 | 0 | } else { |
192 | 0 | std::copy(DataPtr + Offset, DataPtr + Offset + Length, Arr); |
193 | 0 | } |
194 | 0 | } |
195 | 0 | return {}; |
196 | 0 | } |
197 | | |
198 | | /// Replace Data[Offset : Offset + Length - 1] to an uint8 array |
199 | | Expect<void> setArray(const uint8_t *Arr, uint32_t Offset, uint32_t Length, |
200 | 0 | bool IsReverse = false) noexcept { |
201 | 0 | // Check the memory boundary. |
202 | 0 | if (unlikely(!checkAccessBound(Offset, Length))) { |
203 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
204 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
205 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
206 | 0 | } |
207 | 0 | if (likely(Length > 0)) { |
208 | 0 | // Copy the data. |
209 | 0 | if (IsReverse) { |
210 | 0 | std::reverse_copy(Arr, Arr + Length, DataPtr + Offset); |
211 | 0 | } else { |
212 | 0 | std::copy(Arr, Arr + Length, DataPtr + Offset); |
213 | 0 | } |
214 | 0 | } |
215 | 0 | return {}; |
216 | 0 | } |
217 | | |
218 | | /// Get pointer to specific offset of memory or null. |
219 | | template <typename T> |
220 | | typename std::enable_if_t<std::is_pointer_v<T>, T> |
221 | | getPointerOrNull(uint32_t Offset) const noexcept { |
222 | | if (Offset == 0 || |
223 | | unlikely(!checkAccessBound(Offset, sizeof(std::remove_pointer_t<T>)))) { |
224 | | return nullptr; |
225 | | } |
226 | | return reinterpret_cast<T>(&DataPtr[Offset]); |
227 | | } |
228 | | |
229 | | /// Get pointer to specific offset of memory. |
230 | | template <typename T> |
231 | | typename std::enable_if_t<std::is_pointer_v<T>, T> |
232 | 0 | getPointer(uint32_t Offset) const noexcept { |
233 | 0 | using Type = std::remove_pointer_t<T>; |
234 | 0 | uint32_t ByteSize = static_cast<uint32_t>(sizeof(Type)); |
235 | 0 | if (unlikely(!checkAccessBound(Offset, ByteSize))) { |
236 | 0 | return nullptr; |
237 | 0 | } |
238 | 0 | return reinterpret_cast<T>(&DataPtr[Offset]); |
239 | 0 | } Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPcEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES7_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPNSt3__16atomicImEEEENS4_9enable_ifIXsr3stdE12is_pointer_vIT_EES9_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPNSt3__16atomicIjEEEENS4_9enable_ifIXsr3stdE12is_pointer_vIT_EES9_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPNSt3__16atomicIiEEEENS4_9enable_ifIXsr3stdE12is_pointer_vIT_EES9_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPNSt3__16atomicIlEEEENS4_9enable_ifIXsr3stdE12is_pointer_vIT_EES9_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPNSt3__16atomicIhEEEENS4_9enable_ifIXsr3stdE12is_pointer_vIT_EES9_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPNSt3__16atomicItEEEENS4_9enable_ifIXsr3stdE12is_pointer_vIT_EES9_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIP17__wasi_addrinfo_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIP17__wasi_sockaddr_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPjEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES7_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPmEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES7_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIP15__wasi_fdstat_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIP17__wasi_filestat_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIP16__wasi_prestat_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPiEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES7_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIP16__wasi_address_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIP16__wasi_roflags_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPK17__wasi_addrinfo_tEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES9_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPKjEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES8_E4typeEj Unexecuted instantiation: _ZNK8WasmEdge7Runtime8Instance14MemoryInstance10getPointerIPtEENSt3__19enable_ifIXsr3stdE12is_pointer_vIT_EES7_E4typeEj |
240 | | |
241 | | /// Get array of object at specific offset of memory. |
242 | | template <typename T> |
243 | 0 | Span<T> getSpan(uint32_t Offset, uint32_t Size) const noexcept { |
244 | 0 | uint32_t ByteSize = static_cast<uint32_t>(sizeof(T) * Size); |
245 | 0 | if (unlikely(!checkAccessBound(Offset, ByteSize))) { |
246 | 0 | return Span<T>(); |
247 | 0 | } |
248 | 0 | return Span<T>(reinterpret_cast<T *>(&DataPtr[Offset]), Size); |
249 | 0 | } Unexecuted instantiation: cxx20::span<unsigned char, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<unsigned char>(unsigned int, unsigned int) const Unexecuted instantiation: cxx20::span<unsigned char const, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<unsigned char const>(unsigned int, unsigned int) const Unexecuted instantiation: cxx20::span<unsigned int, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<unsigned int>(unsigned int, unsigned int) const Unexecuted instantiation: cxx20::span<__wasi_iovec_t, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<__wasi_iovec_t>(unsigned int, unsigned int) const Unexecuted instantiation: cxx20::span<__wasi_ciovec_t, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<__wasi_ciovec_t>(unsigned int, unsigned int) const Unexecuted instantiation: cxx20::span<char, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<char>(unsigned int, unsigned int) const Unexecuted instantiation: cxx20::span<__wasi_subscription_t const, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<__wasi_subscription_t const>(unsigned int, unsigned int) const Unexecuted instantiation: cxx20::span<__wasi_event_t, 18446744073709551615ul> WasmEdge::Runtime::Instance::MemoryInstance::getSpan<__wasi_event_t>(unsigned int, unsigned int) const |
250 | | |
251 | | /// Get array of object at specific offset of memory. |
252 | | std::string_view getStringView(uint32_t Offset, |
253 | 0 | uint32_t Size) const noexcept { |
254 | 0 | if (unlikely(!checkAccessBound(Offset, Size))) { |
255 | 0 | return {}; |
256 | 0 | } |
257 | 0 | return {reinterpret_cast<const char *>(&DataPtr[Offset]), Size}; |
258 | 0 | } |
259 | | |
260 | | /// Template of loading bytes and convert to a value. |
261 | | /// |
262 | | /// Load the length of vector and construct into a value. |
263 | | /// Only output value of int32, uint32, int64, uint64, float, and double are |
264 | | /// allowed. |
265 | | /// |
266 | | /// \param Value the constructed output value. |
267 | | /// \param Offset the start offset in data array. |
268 | | /// |
269 | | /// \returns void when success, ErrCode when failed. |
270 | | template <typename T, uint32_t Length = sizeof(T)> |
271 | | typename std::enable_if_t<IsWasmNumV<T>, Expect<void>> |
272 | 0 | loadValue(T &Value, uint32_t Offset) const noexcept { |
273 | | // Check the data boundary. |
274 | 0 | static_assert(Length <= sizeof(T)); |
275 | | // Check the memory boundary. |
276 | 0 | if (unlikely(!checkAccessBound(Offset, Length))) { |
277 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
278 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
279 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
280 | 0 | } |
281 | | // Load the data to the value. |
282 | 0 | if (likely(Length > 0)) { |
283 | 0 | if constexpr (std::is_floating_point_v<T>) { |
284 | | // Floating case. Do the memory copy. |
285 | 0 | std::memcpy(&Value, &DataPtr[Offset], sizeof(T)); |
286 | 0 | } else { |
287 | 0 | if constexpr (sizeof(T) > 8) { |
288 | 0 | assuming(sizeof(T) == 16); |
289 | 0 | Value = 0U; |
290 | 0 | std::memcpy(&Value, &DataPtr[Offset], Length); |
291 | 0 | } else { |
292 | 0 | uint64_t LoadVal = 0; |
293 | | // Integer case. Extends to the result type. |
294 | 0 | std::memcpy(&LoadVal, &DataPtr[Offset], Length); |
295 | 0 | if (std::is_signed_v<T> && (LoadVal >> (Length * 8 - 1))) { |
296 | | // Signed extension. |
297 | 0 | for (unsigned int I = Length; I < 8; I++) { |
298 | 0 | LoadVal |= 0xFFULL << (I * 8); |
299 | 0 | } |
300 | 0 | } |
301 | 0 | Value = static_cast<T>(LoadVal); |
302 | 0 | } |
303 | 0 | } |
304 | 0 | } |
305 | 0 | return {}; |
306 | 0 | } Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned int, 4u>(unsigned int&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned long, 8u>(unsigned long&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<float>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<float, 4u>(float&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<double>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<double, 8u>(double&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<int, 1u>(int&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned int, 1u>(unsigned int&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<int, 2u>(int&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned int, 2u>(unsigned int&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<long, 1u>(long&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned long, 1u>(unsigned long&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<long, 2u>(long&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned long, 2u>(unsigned long&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<long, 4u>(long&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned long, 4u>(unsigned long&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned __int128>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned __int128, 16u>(unsigned __int128&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned __int128>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned __int128, 4u>(unsigned __int128&, unsigned int) const Unexecuted instantiation: std::__1::enable_if<IsWasmNumV<unsigned __int128>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::loadValue<unsigned __int128, 8u>(unsigned __int128&, unsigned int) const |
307 | | |
308 | | /// Template of loading bytes and convert to a value. |
309 | | /// |
310 | | /// Destruct and Store the value to length of vector. |
311 | | /// Only input value of uint32, uint64, float, and double are allowed. |
312 | | /// |
313 | | /// \param Value the value want to store into data array. |
314 | | /// \param Offset the start offset in data array. |
315 | | /// |
316 | | /// \returns void when success, ErrCode when failed. |
317 | | template <typename T, uint32_t Length = sizeof(T)> |
318 | | typename std::enable_if_t<IsWasmNativeNumV<T>, Expect<void>> |
319 | 0 | storeValue(const T &Value, uint32_t Offset) noexcept { |
320 | | // Check the data boundary. |
321 | 0 | static_assert(Length <= sizeof(T)); |
322 | | // Check the memory boundary. |
323 | 0 | if (unlikely(!checkAccessBound(Offset, Length))) { |
324 | 0 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
325 | 0 | spdlog::error(ErrInfo::InfoBoundary(Offset, Length, getBoundIdx())); |
326 | 0 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
327 | 0 | } |
328 | | // Copy the stored data to the value. |
329 | 0 | if (likely(Length > 0)) { |
330 | 0 | std::memcpy(&DataPtr[Offset], &Value, Length); |
331 | 0 | } |
332 | 0 | return {}; |
333 | 0 | } Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned int, 4u>(unsigned int const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned long, 8u>(unsigned long const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<float>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<float, 4u>(float const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<double>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<double, 8u>(double const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned int, 1u>(unsigned int const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned int>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned int, 2u>(unsigned int const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned long, 1u>(unsigned long const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned long, 2u>(unsigned long const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned long>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned long, 4u>(unsigned long const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned __int128>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned __int128, 16u>(unsigned __int128 const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned int const>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned int const, 1u>(unsigned int const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned int const>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned int const, 2u>(unsigned int const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned int const>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned int const, 4u>(unsigned int const&, unsigned int) Unexecuted instantiation: std::__1::enable_if<IsWasmNativeNumV<unsigned long const>, cxx20::expected<void, WasmEdge::ErrCode> >::type WasmEdge::Runtime::Instance::MemoryInstance::storeValue<unsigned long const, 8u>(unsigned long const&, unsigned int) |
334 | | |
335 | 0 | uint8_t *const &getDataPtr() const noexcept { return DataPtr; } |
336 | 0 | uint8_t *&getDataPtr() noexcept { return DataPtr; } |
337 | | |
338 | | private: |
339 | | /// \name Data of memory instance. |
340 | | /// @{ |
341 | | AST::MemoryType MemType; |
342 | | uint8_t *DataPtr = nullptr; |
343 | | const uint32_t PageLimit; |
344 | | /// @} |
345 | | }; |
346 | | |
347 | | } // namespace Instance |
348 | | } // namespace Runtime |
349 | | } // namespace WasmEdge |