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