1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
mod data;
mod ty;
pub use self::{
data::{DataSegment, DataSegmentEntity},
ty::{MemoryType, MemoryTypeBuilder},
};
use crate::{
AsContext,
AsContextMut,
Error,
StoreContext,
StoreContextMut,
core::CoreMemory,
errors::MemoryError,
store::Stored,
};
define_handle! {
/// A Wasm linear memory reference.
struct Memory(u32, Stored) => CoreMemory;
}
impl Memory {
/// Creates a new linear memory to the store.
///
/// # Errors
///
/// If more than [`u32::MAX`] much linear memory is allocated.
pub fn new(mut ctx: impl AsContextMut, ty: MemoryType) -> Result<Self, Error> {
let (inner, mut resource_limiter) = ctx
.as_context_mut()
.store
.store_inner_and_resource_limiter_ref();
let entity = CoreMemory::new(ty.core, &mut resource_limiter)?;
let memory = inner.alloc_memory(entity);
Ok(memory)
}
/// Creates a new linear memory to the store.
///
/// # Errors
///
/// If more than [`u32::MAX`] much linear memory is allocated.
/// - If static buffer is invalid
pub fn new_static(
mut ctx: impl AsContextMut,
ty: MemoryType,
buf: &'static mut [u8],
) -> Result<Self, Error> {
let (inner, mut resource_limiter) = ctx
.as_context_mut()
.store
.store_inner_and_resource_limiter_ref();
let entity = CoreMemory::new_static(ty.core, &mut resource_limiter, buf)?;
let memory = inner.alloc_memory(entity);
Ok(memory)
}
/// Returns the memory type of the linear memory.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn ty(&self, ctx: impl AsContext) -> MemoryType {
let core = ctx.as_context().store.inner.resolve_memory(self).ty();
MemoryType { core }
}
/// Returns the dynamic [`MemoryType`] of the [`Memory`].
///
/// # Note
///
/// This respects the current size of the [`Memory`] as
/// its minimum size and is useful for import subtyping checks.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub(crate) fn dynamic_ty(&self, ctx: impl AsContext) -> MemoryType {
let core = ctx
.as_context()
.store
.inner
.resolve_memory(self)
.dynamic_ty();
MemoryType { core }
}
/// Returns the size, in WebAssembly pages, of this Wasm linear memory.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn size(&self, ctx: impl AsContext) -> u64 {
ctx.as_context().store.inner.resolve_memory(self).size()
}
/// Grows the linear memory by the given amount of new pages.
///
/// Returns the amount of pages before the operation upon success.
///
/// # Errors
///
/// If the linear memory would grow beyond its maximum limit after
/// the grow operation.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn grow(&self, mut ctx: impl AsContextMut, additional: u64) -> Result<u64, MemoryError> {
let (inner, mut limiter) = ctx
.as_context_mut()
.store
.store_inner_and_resource_limiter_ref();
inner
.resolve_memory_mut(self)
.grow(additional, None, &mut limiter)
.map_err(|_| MemoryError::OutOfBoundsGrowth)
}
/// Returns a shared slice to the bytes underlying the [`Memory`].
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn data<'a, T: 'a>(&self, ctx: impl Into<StoreContext<'a, T>>) -> &'a [u8] {
ctx.into().store.inner.resolve_memory(self).data()
}
/// Returns an exclusive slice to the bytes underlying the [`Memory`].
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn data_mut<'a, T: 'a>(&self, ctx: impl Into<StoreContextMut<'a, T>>) -> &'a mut [u8] {
ctx.into().store.inner.resolve_memory_mut(self).data_mut()
}
/// Returns an exclusive slice to the bytes underlying the [`Memory`], and an exclusive
/// reference to the user provided state.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn data_and_store_mut<'a, T: 'a>(
&self,
ctx: impl Into<StoreContextMut<'a, T>>,
) -> (&'a mut [u8], &'a mut T) {
let (memory, store) = ctx.into().store.resolve_memory_and_state_mut(self);
(memory.data_mut(), store)
}
/// Returns the base pointer, in the host’s address space, that the [`Memory`] is located at.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn data_ptr(&self, ctx: impl AsContext) -> *mut u8 {
ctx.as_context().store.inner.resolve_memory(self).data_ptr()
}
/// Returns the byte length of this [`Memory`].
///
/// The returned value will be a multiple of the wasm page size, 64k.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn data_size(&self, ctx: impl AsContext) -> usize {
ctx.as_context()
.store
.inner
.resolve_memory(self)
.data_size()
}
/// Reads `n` bytes from `memory[offset..offset+n]` into `buffer`
/// where `n` is the length of `buffer`.
///
/// # Errors
///
/// If this operation accesses out of bounds linear memory.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn read(
&self,
ctx: impl AsContext,
offset: usize,
buffer: &mut [u8],
) -> Result<(), MemoryError> {
ctx.as_context()
.store
.inner
.resolve_memory(self)
.read(offset, buffer)
}
/// Writes `n` bytes to `memory[offset..offset+n]` from `buffer`
/// where `n` if the length of `buffer`.
///
/// # Errors
///
/// If this operation accesses out of bounds linear memory.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn write(
&self,
mut ctx: impl AsContextMut,
offset: usize,
buffer: &[u8],
) -> Result<(), MemoryError> {
ctx.as_context_mut()
.store
.inner
.resolve_memory_mut(self)
.write(offset, buffer)
}
}