forked from dfinity/ic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstable_memory.rs
228 lines (204 loc) · 7.67 KB
/
stable_memory.rs
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
224
225
226
227
228
use std::convert::TryInto;
use ic_interfaces::execution_environment::{
HypervisorError, HypervisorResult,
TrapCode::{HeapOutOfBounds, StableMemoryOutOfBounds, StableMemoryTooBigFor32Bit},
};
use ic_replicated_state::{canister_state::WASM_PAGE_SIZE_IN_BYTES, page_map, NumWasmPages};
use ic_types::NumOsPages;
const MAX_32_BIT_STABLE_MEMORY_IN_PAGES: usize = 64 * 1024; // 4GiB
/// Essentially the same as a `page_map::Memory`, but we use a `Buffer` instead
/// of a `PageMap`.
pub struct StableMemory {
/// We could update the PageMap stored in the system state directly, but it
/// will be not efficient if the canister issues many write requests to
/// nearby memory locations, which is a common case (serializing Candid
/// directly to stable memory, for example). Using a long-lived buffer
/// allows us to allocate a dirty page once and modify it in-place in
/// subsequent calls.
pub stable_memory_buffer: page_map::Buffer,
/// The size of the canister's stable memory.
pub stable_memory_size: NumWasmPages,
}
impl StableMemory {
pub fn new(stable_memory: ic_replicated_state::Memory) -> Self {
Self {
stable_memory_buffer: page_map::Buffer::new(stable_memory.page_map),
stable_memory_size: stable_memory.size,
}
}
/// Returns the stable memory size in Wasm pages after checking that it fits
/// into an unsigned 32-bit integer.
pub(super) fn stable_size(&self) -> HypervisorResult<u32> {
let size = self.stable_memory_size.get();
if size > MAX_32_BIT_STABLE_MEMORY_IN_PAGES {
return Err(HypervisorError::Trapped {
trap_code: StableMemoryTooBigFor32Bit,
backtrace: None,
});
}
// Safe as we confirmed above the value is small enough to fit into 32-bits.
Ok(size.try_into().unwrap())
}
/// Reads from stable memory back to heap.
pub(super) fn stable_read(
&self,
dst: u32,
offset: u32,
size: u32,
heap: &mut [u8],
) -> HypervisorResult<()> {
let (dst, offset, size) = (dst as usize, offset as usize, size as usize);
if offset + size > (self.stable_size()? as usize * WASM_PAGE_SIZE_IN_BYTES) {
return Err(HypervisorError::Trapped {
trap_code: StableMemoryOutOfBounds,
backtrace: None,
});
}
if dst + size > heap.len() {
return Err(HypervisorError::Trapped {
trap_code: HeapOutOfBounds,
backtrace: None,
});
}
self.stable_memory_buffer
.read(&mut heap[dst..dst + size], offset);
Ok(())
}
/// Writes from heap to stable memory.
/// Returns the number of **new** dirty pages created by the write.
pub(super) fn stable_write(
&mut self,
offset: u32,
src: u32,
size: u32,
heap: &[u8],
) -> HypervisorResult<()> {
let (src, offset, size) = (src as usize, offset as usize, size as usize);
if offset + size > (self.stable_size()? as usize * WASM_PAGE_SIZE_IN_BYTES) {
return Err(HypervisorError::Trapped {
trap_code: StableMemoryOutOfBounds,
backtrace: None,
});
}
if src + size > heap.len() {
return Err(HypervisorError::Trapped {
trap_code: HeapOutOfBounds,
backtrace: None,
});
}
self.stable_memory_buffer
.write(&heap[src..src + size], offset);
Ok(())
}
/// Same as `stable64_read`, but doesn't do any bounds checks on the stable
/// memory. This should only be called through instrumented code that does
/// its own bounds checking (although it is still safe to call without
/// bounds checking - the result will just be that zeros are read from
/// beyond the end of stable memory).
pub(super) fn stable_read_without_bounds_checks(
&self,
dst: u64,
offset: u64,
size: u64,
heap: &mut [u8],
) -> HypervisorResult<()> {
let (heap_end, overflow) = dst.overflowing_add(size);
if overflow || heap_end as usize > heap.len() {
return Err(HypervisorError::Trapped {
trap_code: HeapOutOfBounds,
backtrace: None,
});
}
self.stable_memory_buffer
.read(&mut heap[dst as usize..heap_end as usize], offset as usize);
Ok(())
}
/// Reads from stable memory back to heap.
///
/// Supports bigger stable memory indexed by 64 bit pointers.
pub(super) fn stable64_read(
&self,
dst: u64,
offset: u64,
size: u64,
heap: &mut [u8],
) -> HypervisorResult<()> {
let (dst, offset, size) = (dst as usize, offset as usize, size as usize);
let (stable_memory_size_in_bytes, overflow) = self
.stable_memory_size
.get()
.overflowing_mul(WASM_PAGE_SIZE_IN_BYTES);
if overflow {
return Err(HypervisorError::Trapped {
trap_code: StableMemoryOutOfBounds,
backtrace: None,
});
}
let (stable_memory_end, overflow) = offset.overflowing_add(size);
if overflow || stable_memory_end > stable_memory_size_in_bytes {
return Err(HypervisorError::Trapped {
trap_code: StableMemoryOutOfBounds,
backtrace: None,
});
}
let (heap_end, overflow) = dst.overflowing_add(size);
if overflow || heap_end > heap.len() {
return Err(HypervisorError::Trapped {
trap_code: HeapOutOfBounds,
backtrace: None,
});
}
self.stable_memory_buffer
.read(&mut heap[dst..heap_end], offset);
Ok(())
}
/// Writes from heap to stable memory.
///
/// Supports bigger stable memory indexed by 64 bit pointers.
/// Returns the number of **new** dirty pages created by the write.
pub(super) fn stable64_write(
&mut self,
offset: u64,
src: u64,
size: u64,
heap: &[u8],
) -> HypervisorResult<()> {
let (src, offset, size) = (src as usize, offset as usize, size as usize);
let (stable_memory_size_in_bytes, overflow) = self
.stable_memory_size
.get()
.overflowing_mul(WASM_PAGE_SIZE_IN_BYTES);
if overflow {
return Err(HypervisorError::Trapped {
trap_code: StableMemoryOutOfBounds,
backtrace: None,
});
}
let (stable_memory_end, overflow) = offset.overflowing_add(size);
if overflow || stable_memory_end > stable_memory_size_in_bytes {
return Err(HypervisorError::Trapped {
trap_code: StableMemoryOutOfBounds,
backtrace: None,
});
}
let (heap_end, overflow) = src.overflowing_add(size);
if overflow || heap_end > heap.len() {
return Err(HypervisorError::Trapped {
trap_code: HeapOutOfBounds,
backtrace: None,
});
}
self.stable_memory_buffer
.write(&heap[src..heap_end], offset);
Ok(())
}
/// Calculates the number of new dirty pages that a given write would
/// create.
///
/// No guarantee is made that such a write would succeed though (e.g. it
/// could exceed the current stable memory size).
pub(super) fn dirty_pages_from_write(&self, offset: u64, size: u64) -> NumOsPages {
self.stable_memory_buffer
.dirty_pages_from_write(offset, size)
}
}