wasmer_wasix/journal/effector/
process_exit.rs

1use virtual_mio::InlineWaker;
2
3use super::*;
4
5impl JournalEffector {
6    pub fn save_process_exit(env: &WasiEnv, exit_code: Option<ExitCode>) -> anyhow::Result<()> {
7        env.active_journal()?
8            .write(JournalEntry::ProcessExitV1 { exit_code })
9            .map_err(map_snapshot_err)?;
10        Ok(())
11    }
12
13    /// # Safety
14    ///
15    /// This function manipulates the memory of the process and thus must be executed
16    /// by the WASM process thread itself.
17    ///
18    pub unsafe fn apply_process_exit(
19        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
20        exit_code: Option<ExitCode>,
21    ) -> anyhow::Result<()> {
22        let env = ctx.data();
23        // If we are in the phase of replaying journals then we
24        // close all the file descriptors but we don't actually send
25        // any signals
26        if env.replaying_journal {
27            let state = env.state.clone();
28            InlineWaker::block_on(state.fs.close_all());
29        } else {
30            env.blocking_on_exit(exit_code);
31        }
32
33        // Reset the memory back to a zero size
34        let memory = ctx
35            .data()
36            .inner()
37            .main_module_instance_handles()
38            .memory()
39            .clone();
40        memory.reset(ctx)?;
41        Ok(())
42    }
43}