wasmer_wasix/journal/effector/
thread_state.rs

1use std::sync::Arc;
2
3use wasmer_wasix_types::wasix::ThreadStartType;
4
5use crate::{
6    RewindState,
7    os::task::thread::{RewindResultType, WasiMemoryLayout},
8    syscalls::thread_spawn_internal_using_layout,
9};
10
11use super::*;
12
13impl JournalEffector {
14    pub fn save_thread_state<M: MemorySize>(
15        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
16        id: WasiThreadId,
17        memory_stack: Bytes,
18        rewind_stack: Bytes,
19        store_data: Bytes,
20        start: ThreadStartType,
21        layout: WasiMemoryLayout,
22    ) -> anyhow::Result<()> {
23        Self::save_event(
24            ctx,
25            JournalEntry::SetThreadV1 {
26                id: id.raw(),
27                call_stack: Cow::Owned(rewind_stack.into()),
28                memory_stack: Cow::Owned(memory_stack.into()),
29                store_data: Cow::Owned(store_data.into()),
30                start,
31                layout,
32                is_64bit: M::is_64bit(),
33            },
34        )
35    }
36
37    /// This will take the supplied stacks and apply them to the memory region
38    /// dedicated to this thread. After that it will spawn a WASM thread and
39    // continue the thread where it left off, which may even mean it goes
40    // straight back to sleep.
41    pub fn apply_thread_state<M: MemorySize>(
42        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
43        tid: WasiThreadId,
44        memory_stack: Bytes,
45        rewind_stack: Bytes,
46        store_data: Bytes,
47        start: ThreadStartType,
48        layout: WasiMemoryLayout,
49    ) -> anyhow::Result<()> {
50        let start_ptr: M::Offset = match start {
51            ThreadStartType::MainThread => {
52                return Err(anyhow::format_err!(
53                    "unable to restore a main thread via this method"
54                ));
55            }
56            ThreadStartType::ThreadSpawn { start_ptr } => start_ptr
57                .try_into()
58                .map_err(|_| anyhow::format_err!("overflow while processing thread restoration"))?,
59        };
60
61        // Create the thread for this ID
62        let thread_handle = Arc::new(ctx.data().process.new_thread_with_id(
63            layout.clone(),
64            start,
65            tid,
66        )?);
67
68        // Now spawn the thread itself
69        thread_spawn_internal_using_layout::<M>(
70            ctx,
71            thread_handle,
72            layout.clone(),
73            start_ptr,
74            Some((
75                RewindState {
76                    memory_stack,
77                    rewind_stack,
78                    store_data,
79                    start,
80                    layout,
81                    is_64bit: M::is_64bit(),
82                },
83                RewindResultType::RewindRestart,
84            )),
85        )
86        .map_err(|err| anyhow::format_err!("failed to spawn thread - {err}"))?;
87
88        Ok(())
89    }
90}