-
Notifications
You must be signed in to change notification settings - Fork 296
/
sandbox_manager.rs
596 lines (557 loc) · 22.9 KB
/
sandbox_manager.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! The sandbox manager provides the actual functionality of the sandbox
//! process. It allows the replica controller process to manage
//! everything required in order to execute code. It holds three
//! kinds of resources that it manages on behalf of the replica
//! controller process:
//!
//! - CanisterWasm: The (wasm) code corresponding to one canister
//! - State: The heap and other (mutable) user state associated with a canister
//! - Execution: An ongoing execution of a canister, using one wasm and state
//! object
//!
//! All of the above objects as well as the functionality provided
//! towards the controller are found in this module.
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt::{Debug, Formatter};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use crate::protocol::id::{ExecId, MemoryId, WasmId};
use crate::protocol::sbxsvc::{
CreateExecutionStateSerializedSuccessReply, CreateExecutionStateSuccessReply, OpenMemoryRequest,
};
use crate::protocol::structs::{
MemoryModifications, SandboxExecInput, SandboxExecOutput, StateModifications,
};
use crate::{controller_service::ControllerService, protocol};
use ic_config::embedders::Config as EmbeddersConfig;
use ic_embedders::{
wasm_executor::WasmStateChanges,
wasm_utils::{compile, decoding::decode_wasm, Segments},
CompilationResult, SerializedModule, SerializedModuleBytes, WasmtimeEmbedder,
};
use ic_interfaces::execution_environment::{
ExecutionMode, HypervisorError, HypervisorResult, WasmExecutionOutput,
};
use ic_logger::ReplicaLogger;
use ic_replicated_state::page_map::{PageAllocatorRegistry, PageMapSerialization};
use ic_replicated_state::{EmbedderCache, Global, Memory, PageMap};
use ic_types::CanisterId;
use crate::dts::{DeterministicTimeSlicingHandler, PausedExecution};
struct ExecutionInstantiateError;
impl Debug for ExecutionInstantiateError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to instantatiate execution.")
}
}
/// A canister execution currently in progress.
struct Execution {
/// Id of the execution. This is used in communicating back to
/// the replica (e.g. for syscalls) such that replica can associate
/// events with the correct execution.
exec_id: ExecId,
/// The canister wasm used in this execution.
embedder_cache: Arc<EmbedderCache>,
/// The sandbox manager that is responsible for
/// 1) Providing the controller to talk to the replica process.
/// 2) Creating a new execution state.
sandbox_manager: Arc<SandboxManager>,
}
impl Execution {
/// Creates new execution based on canister wasm and state. In order
/// to start the execution, the given state object will be "locked" --
/// if that cannot be done, then creation of execution will fail.
/// The actual code to be run will be scheduled to the given
/// thread pool.
///
/// This will *actually* schedule and initiate a new execution.
#[allow(clippy::too_many_arguments)]
pub(crate) fn start_on_worker_thread(
exec_id: ExecId,
embedder_cache: Arc<EmbedderCache>,
wasm_memory: Arc<Memory>,
stable_memory: Arc<Memory>,
sandbox_manager: Arc<SandboxManager>,
workers: &mut threadpool::ThreadPool,
exec_input: SandboxExecInput,
total_timer: std::time::Instant,
) {
let wasm_memory = (*wasm_memory).clone();
let stable_memory = (*stable_memory).clone();
let execution = Arc::new(Self {
exec_id,
embedder_cache,
sandbox_manager,
});
workers.execute(move || {
execution.run(exec_id, exec_input, wasm_memory, stable_memory, total_timer)
});
}
// Actual wasm code execution -- this is run on the target thread
// in the thread pool.
fn run(
&self,
exec_id: ExecId,
exec_input: SandboxExecInput,
mut wasm_memory: Memory,
mut stable_memory: Memory,
total_timer: std::time::Instant,
) {
let run_timer = std::time::Instant::now();
let message_instruction_limit =
exec_input.execution_parameters.instruction_limits.message();
let slice_instruction_limit = exec_input.execution_parameters.instruction_limits.slice();
let sandbox_manager = Arc::clone(&self.sandbox_manager);
let out_of_instructions_handler = DeterministicTimeSlicingHandler::new(
i64::try_from(message_instruction_limit.get()).unwrap_or(i64::MAX),
i64::try_from(slice_instruction_limit.get()).unwrap_or(i64::MAX),
move |slice, paused_execution| {
{
let mut guard = sandbox_manager.repr.lock().unwrap();
guard.paused_executions.insert(exec_id, paused_execution);
}
sandbox_manager
.controller
.execution_paused(protocol::ctlsvc::ExecutionPausedRequest { exec_id, slice });
},
);
let (
slice,
WasmExecutionOutput {
wasm_result,
num_instructions_left,
allocated_bytes,
allocated_message_bytes,
instance_stats,
system_api_call_counters,
canister_log_records,
},
deltas,
instance_or_system_api,
) = ic_embedders::wasm_executor::process(
exec_input.func_ref,
exec_input.api_type,
exec_input.canister_current_memory_usage,
exec_input.canister_current_message_memory_usage,
exec_input.execution_parameters,
exec_input.subnet_available_memory,
exec_input.sandbox_safe_system_state,
&self.embedder_cache,
&self.sandbox_manager.embedder,
&mut wasm_memory,
&mut stable_memory,
&exec_input.globals,
self.sandbox_manager.log.clone(),
exec_input.wasm_reserved_pages,
Rc::new(out_of_instructions_handler),
);
match wasm_result {
Ok(_) => {
let state_modifications = deltas.map(
|WasmStateChanges {
dirty_page_indices,
globals,
}| {
let system_state_changes = match instance_or_system_api {
// Here we use `store_data_mut` instead of
// `into_store_data` because the later will drop the
// wasmtime Instance which can be an expensive
// operation. Mutating the store instead allows us
// to delay the drop until after the execution
// completed message is sent back to the main
// process.
Ok(mut instance) => instance
.store_data_mut()
.system_api_mut()
.expect("System api not present in the wasmtime instance")
.take_system_state_changes(),
Err(system_api) => system_api.into_system_state_changes(),
};
StateModifications::new(
globals,
&wasm_memory,
&stable_memory,
&dirty_page_indices.wasm_memory_delta,
&dirty_page_indices.stable_memory_delta,
system_state_changes,
)
},
);
if state_modifications.is_some() {
self.sandbox_manager
.add_memory(exec_input.next_wasm_memory_id, wasm_memory);
self.sandbox_manager
.add_memory(exec_input.next_stable_memory_id, stable_memory);
}
let wasm_output = WasmExecutionOutput {
wasm_result,
allocated_bytes,
allocated_message_bytes,
num_instructions_left,
instance_stats,
system_api_call_counters,
canister_log_records,
};
self.sandbox_manager.controller.execution_finished(
protocol::ctlsvc::ExecutionFinishedRequest {
exec_id: self.exec_id,
exec_output: SandboxExecOutput {
slice,
wasm: wasm_output,
state: state_modifications,
execute_total_duration: total_timer.elapsed(),
execute_run_duration: run_timer.elapsed(),
},
},
);
}
Err(HypervisorError::Aborted) => {
// Do not send any reply to the controller because the execution
// was aborted and the controller removed `exec_id` on its side.
}
Err(err) => {
let wasm_output = WasmExecutionOutput {
wasm_result: Err(err),
num_instructions_left,
allocated_bytes,
allocated_message_bytes,
instance_stats,
system_api_call_counters,
canister_log_records,
};
self.sandbox_manager.controller.execution_finished(
protocol::ctlsvc::ExecutionFinishedRequest {
exec_id: self.exec_id,
exec_output: SandboxExecOutput {
slice,
wasm: wasm_output,
state: None,
execute_total_duration: total_timer.elapsed(),
execute_run_duration: run_timer.elapsed(),
},
},
);
}
}
}
}
/// Manages the entirety of the sandbox process. It provides the methods
/// through which the controller process (the replica) manages the
/// sandboxed execution.
pub struct SandboxManager {
repr: Mutex<SandboxManagerInt>,
controller: Arc<dyn ControllerService>,
embedder: Arc<WasmtimeEmbedder>,
page_allocator_registry: Arc<PageAllocatorRegistry>,
log: ReplicaLogger,
}
struct SandboxManagerInt {
caches: HashMap<WasmId, Arc<EmbedderCache>>,
memories: HashMap<MemoryId, Arc<Memory>>,
paused_executions: HashMap<ExecId, PausedExecution>,
workers_for_replicated_execution: threadpool::ThreadPool,
workers_for_non_replicated_execution: threadpool::ThreadPool,
workers_for_cleanup: threadpool::ThreadPool,
}
impl SandboxManager {
/// Creates new sandbox manager. In order to operate, it needs
/// an established backward RPC channel to the controller process
/// to relay e.g. syscalls and completions.
pub fn new(
controller: Arc<dyn ControllerService>,
config: EmbeddersConfig,
log: ReplicaLogger,
) -> Self {
let embedder = Arc::new(WasmtimeEmbedder::new(config.clone(), log.clone()));
SandboxManager {
repr: Mutex::new(SandboxManagerInt {
caches: HashMap::new(),
memories: HashMap::new(),
paused_executions: HashMap::new(),
workers_for_replicated_execution: threadpool::ThreadPool::new(1),
workers_for_non_replicated_execution: threadpool::ThreadPool::new(
config.query_execution_threads_per_canister,
),
workers_for_cleanup: threadpool::ThreadPool::new(1),
}),
controller,
embedder,
log,
page_allocator_registry: Arc::new(PageAllocatorRegistry::new()),
}
}
/// Compiles the given Wasm binary and registers it under the given id.
/// The function may fail if the Wasm binary is invalid.
pub fn open_wasm(
&self,
wasm_id: WasmId,
wasm_src: Vec<u8>,
) -> HypervisorResult<(Arc<EmbedderCache>, CompilationResult, SerializedModule)> {
let mut guard = self.repr.lock().unwrap();
assert!(
!guard.caches.contains_key(&wasm_id),
"Failed to open wasm session {}: id is already in use",
wasm_id,
);
let wasm = decode_wasm(Arc::new(wasm_src))?;
let (cache, result) = compile(&self.embedder, &wasm);
let embedder_cache = Arc::new(cache);
guard.caches.insert(wasm_id, Arc::clone(&embedder_cache));
// Return as much memory as possible because compiling seems to use up
// some extra memory that can be returned.
//
// SAFETY: 0 is always a valid argument to `malloc_trim`.
#[cfg(target_os = "linux")]
unsafe {
libc::malloc_trim(0);
}
let (compilation_result, serialized_module) = result?;
Ok((embedder_cache, compilation_result, serialized_module))
}
pub fn open_wasm_serialized(
&self,
wasm_id: WasmId,
serialized_module: &SerializedModuleBytes,
) -> HypervisorResult<(Arc<EmbedderCache>, Duration)> {
let mut guard = self.repr.lock().unwrap();
assert!(
!guard.caches.contains_key(&wasm_id),
"Failed to open wasm session {}: id is already in use",
wasm_id,
);
let deserialization_timer = Instant::now();
let instance_pre = self
.embedder
.deserialize_module_and_pre_instantiate(serialized_module);
let cache = Arc::new(EmbedderCache::new(instance_pre.clone()));
let deserialization_time = deserialization_timer.elapsed();
guard.caches.insert(wasm_id, Arc::clone(&cache));
match instance_pre {
Ok(_) => Ok((cache, deserialization_time)),
Err(err) => Err(err),
}
}
/// Closes previously opened wasm instance, by id.
pub fn close_wasm(&self, wasm_id: WasmId) {
let mut guard = self.repr.lock().unwrap();
let removed = guard.caches.remove(&wasm_id);
assert!(
removed.is_some(),
"Failed to close wasm session {}: id not found",
wasm_id
);
}
/// Opens a new memory requested by the replica process.
pub fn open_memory(&self, request: OpenMemoryRequest) {
let mut guard = self.repr.lock().unwrap();
guard.open_memory(request, &self.page_allocator_registry);
}
/// Adds a new memory after sandboxed execution.
fn add_memory(&self, memory_id: MemoryId, memory: Memory) {
let mut guard = self.repr.lock().unwrap();
guard.add_memory(memory_id, memory);
}
/// Closes previously opened memory instance, by id.
pub fn close_memory(&self, memory_id: MemoryId) {
let mut guard = self.repr.lock().unwrap();
let removed = guard.memories.remove(&memory_id);
assert!(
removed.is_some(),
"Failed to close state {}: id not found",
memory_id
);
// Dropping memory may be expensive. Do it on a worker thread to avoid
// blocking the main thread of the sandbox process.
guard.workers_for_cleanup.execute(move || drop(removed));
}
/// Starts Wasm execution using specific code and state, passing
/// execution input.
///
/// Note that inside here we start a transaction and the state of
/// execution can not and does not change while we are processing
/// this particular session.
pub fn start_execution(
sandbox_manager: &Arc<SandboxManager>,
exec_id: ExecId,
wasm_id: WasmId,
wasm_memory_id: MemoryId,
stable_memory_id: MemoryId,
exec_input: SandboxExecInput,
) {
let total_timer = std::time::Instant::now();
let mut guard = sandbox_manager.repr.lock().unwrap();
let wasm_runner = guard.caches.get(&wasm_id).unwrap_or_else(|| {
unreachable!(
"Failed to open exec session {}: wasm {} not found",
exec_id, wasm_id
)
});
let wasm_memory = guard.memories.get(&wasm_memory_id).unwrap_or_else(|| {
unreachable!(
"Failed to open exec session {}: wasm memory {} not found",
exec_id, wasm_memory_id,
)
});
let stable_memory = guard.memories.get(&stable_memory_id).unwrap_or_else(|| {
unreachable!(
"Failed to open exec session {}: stable memory {} not found",
exec_id, stable_memory_id,
)
});
match exec_input.execution_parameters.execution_mode {
ExecutionMode::Replicated => Execution::start_on_worker_thread(
exec_id,
Arc::clone(wasm_runner),
Arc::clone(wasm_memory),
Arc::clone(stable_memory),
Arc::clone(sandbox_manager),
&mut guard.workers_for_replicated_execution,
exec_input,
total_timer,
),
ExecutionMode::NonReplicated => Execution::start_on_worker_thread(
exec_id,
Arc::clone(wasm_runner),
Arc::clone(wasm_memory),
Arc::clone(stable_memory),
Arc::clone(sandbox_manager),
&mut guard.workers_for_non_replicated_execution,
exec_input,
total_timer,
),
};
}
/// Resume the paused Wasm execution.
pub fn resume_execution(sandbox_manager: &Arc<SandboxManager>, exec_id: ExecId) {
let paused_execution = {
let mut guard = sandbox_manager.repr.lock().unwrap();
guard
.paused_executions
.remove(&exec_id)
.unwrap_or_else(|| unreachable!("Failed to get paused execution {}", exec_id))
};
paused_execution.resume();
}
/// Abort the paused Wasm execution.
pub fn abort_execution(sandbox_manager: &Arc<SandboxManager>, exec_id: ExecId) {
let paused_execution = {
let mut guard = sandbox_manager.repr.lock().unwrap();
guard
.paused_executions
.remove(&exec_id)
.unwrap_or_else(|| unreachable!("Failed to get paused execution {}", exec_id))
};
paused_execution.abort();
}
pub fn create_execution_state(
&self,
wasm_id: WasmId,
wasm_source: Vec<u8>,
wasm_page_map: PageMapSerialization,
next_wasm_memory_id: MemoryId,
canister_id: CanisterId,
stable_memory_page_map: PageMapSerialization,
) -> HypervisorResult<CreateExecutionStateSuccessReply> {
// Validate, instrument, and compile the binary.
let (embedder_cache, compilation_result, serialized_module) =
self.open_wasm(wasm_id, wasm_source)?;
let (wasm_memory_modifications, exported_globals) = self
.create_initial_memory_and_globals(
&embedder_cache,
&serialized_module.data_segments,
wasm_page_map,
next_wasm_memory_id,
canister_id,
stable_memory_page_map,
)?;
Ok(CreateExecutionStateSuccessReply {
wasm_memory_modifications,
exported_globals,
compilation_result,
serialized_module,
})
}
pub fn create_execution_state_serialized(
&self,
wasm_id: WasmId,
serialized_module: Arc<SerializedModule>,
wasm_page_map: PageMapSerialization,
next_wasm_memory_id: MemoryId,
canister_id: CanisterId,
stable_memory_page_map: PageMapSerialization,
) -> HypervisorResult<CreateExecutionStateSerializedSuccessReply> {
let timer = Instant::now();
let (embedder_cache, deserialization_time) =
self.open_wasm_serialized(wasm_id, &serialized_module.bytes)?;
let (wasm_memory_modifications, exported_globals) = self
.create_initial_memory_and_globals(
&embedder_cache,
&serialized_module.data_segments,
wasm_page_map,
next_wasm_memory_id,
canister_id,
stable_memory_page_map,
)?;
Ok(CreateExecutionStateSerializedSuccessReply {
wasm_memory_modifications,
exported_globals,
deserialization_time,
total_sandbox_time: timer.elapsed(),
})
}
fn create_initial_memory_and_globals(
&self,
embedder_cache: &EmbedderCache,
data_segments: &Segments,
wasm_page_map: PageMapSerialization,
next_wasm_memory_id: MemoryId,
canister_id: CanisterId,
stable_memory_page_map: PageMapSerialization,
) -> HypervisorResult<(MemoryModifications, Vec<Global>)> {
let embedder = Arc::clone(&self.embedder);
let mut wasm_page_map =
PageMap::deserialize(wasm_page_map, &self.page_allocator_registry).unwrap();
let stable_mem_page_map =
PageMap::deserialize(stable_memory_page_map, &self.page_allocator_registry).unwrap();
let (exported_globals, wasm_memory_delta, wasm_memory_size) =
ic_embedders::wasm_executor::get_initial_globals_and_memory(
data_segments,
embedder_cache,
&embedder,
&mut wasm_page_map,
canister_id,
&stable_mem_page_map,
)?;
let wasm_memory = Memory::new(wasm_page_map, wasm_memory_size);
// Send all necessary data for creating the execution state to replica.
let wasm_memory_modifications = MemoryModifications {
page_delta: wasm_memory.page_map.serialize_delta(&wasm_memory_delta),
size: wasm_memory_size,
};
// Save the memory for future message executions.
self.add_memory(next_wasm_memory_id, wasm_memory);
Ok((wasm_memory_modifications, exported_globals))
}
}
impl SandboxManagerInt {
fn open_memory(
&mut self,
request: OpenMemoryRequest,
page_allocator_registry: &PageAllocatorRegistry,
) {
let page_map =
PageMap::deserialize(request.memory.page_map, page_allocator_registry).unwrap();
let memory = Memory::new(page_map, request.memory.num_wasm_pages);
self.add_memory(request.memory_id, memory);
}
fn add_memory(&mut self, memory_id: MemoryId, memory: Memory) {
assert!(
!self.memories.contains_key(&memory_id),
"Failed to open memory {}: id is already in use",
memory_id
);
let memory = Arc::new(memory);
self.memories.insert(memory_id, memory);
}
}