wasmer_wasix/bin_factory/
exec.rs

1#![allow(clippy::result_large_err)]
2use super::{BinaryPackage, BinaryPackageCommand};
3use crate::{
4    RewindState, SpawnError, WasiError, WasiRuntimeError,
5    os::task::{
6        TaskJoinHandle,
7        thread::{RewindResultType, WasiThreadRunGuard},
8    },
9    runtime::{
10        ModuleInput, TaintReason,
11        module_cache::HashedModuleData,
12        task_manager::{
13            TaskWasm, TaskWasmRecycle, TaskWasmRecycleProperties, TaskWasmRunProperties,
14        },
15    },
16    state::context_switching::ContextSwitchingEnvironment,
17    syscalls::rewind_ext,
18};
19use crate::{Runtime, WasiEnv, WasiFunctionEnv};
20use std::{borrow::Cow, sync::Arc};
21use tracing::*;
22use virtual_mio::block_on;
23use wasmer::{Function, Memory32, Memory64, Module, RuntimeError, Store, Value};
24use wasmer_wasix_types::wasi::Errno;
25
26#[tracing::instrument(level = "trace", skip_all, fields(%name, package_id=%binary.id))]
27pub async fn spawn_exec(
28    binary: BinaryPackage,
29    name: &str,
30    env: WasiEnv,
31    runtime: &Arc<dyn Runtime + Send + Sync + 'static>,
32) -> Result<TaskJoinHandle, SpawnError> {
33    import_package_mounts(&env, &binary).await?;
34
35    let cmd = package_command_by_name(&binary, name)?;
36    let input = ModuleInput::Command(Cow::Borrowed(cmd));
37    let module = runtime.resolve_module(input, None, None).await?;
38
39    // Free the space used by the binary, since we don't need it
40    // any longer
41    drop(binary);
42
43    spawn_exec_module(module, env, runtime)
44}
45
46#[tracing::instrument(level = "trace", skip_all, fields(%name))]
47pub async fn spawn_exec_wasm(
48    wasm: HashedModuleData,
49    name: &str,
50    env: WasiEnv,
51    runtime: &Arc<dyn Runtime + Send + Sync + 'static>,
52) -> Result<TaskJoinHandle, SpawnError> {
53    let module = spawn_load_module(name, wasm, runtime).await?;
54
55    spawn_exec_module(module, env, runtime)
56}
57
58pub fn package_command_by_name<'a>(
59    pkg: &'a BinaryPackage,
60    name: &str,
61) -> Result<&'a BinaryPackageCommand, SpawnError> {
62    // If an explicit command is provided, use it.
63    // Otherwise, use the entrypoint.
64    // If no entrypoint exists, and the package has a single
65    // command, then use it. This is done for backwards
66    // compatibility.
67    let cmd = if let Some(cmd) = pkg.get_command(name) {
68        cmd
69    } else if let Some(cmd) = pkg.get_entrypoint_command() {
70        cmd
71    } else {
72        match pkg.commands.as_slice() {
73            // Package only has a single command, so use it.
74            [first] => first,
75            // Package either has no command, or has multiple commands, which
76            // would make the choice ambiguous, so fail.
77            _ => {
78                return Err(SpawnError::MissingEntrypoint {
79                    package_id: pkg.id.clone(),
80                });
81            }
82        }
83    };
84
85    Ok(cmd)
86}
87
88pub async fn spawn_load_module(
89    name: &str,
90    wasm: HashedModuleData,
91    runtime: &Arc<dyn Runtime + Send + Sync + 'static>,
92) -> Result<Module, SpawnError> {
93    match runtime.load_hashed_module(wasm, None).await {
94        Ok(module) => Ok(module),
95        Err(err) => {
96            tracing::error!(
97                command = name,
98                error = &err as &dyn std::error::Error,
99                "Failed to compile the module",
100            );
101            Err(err)
102        }
103    }
104}
105
106pub async fn import_package_mounts(
107    env: &WasiEnv,
108    binary: &BinaryPackage,
109) -> Result<(), SpawnError> {
110    // If the package mounts have not already been imported then do so.
111    env.state
112        .fs
113        .conditional_union(binary)
114        .await
115        .map_err(|err| {
116            tracing::warn!("failed to import package mounts - {err}");
117            SpawnError::FileSystemError(crate::ExtendedFsError::with_msg(
118                err,
119                "could not import package mounts",
120            ))
121        })?;
122    tracing::debug!("{:?}", env.state.fs);
123    Ok(())
124}
125
126pub fn spawn_exec_module(
127    module: Module,
128    env: WasiEnv,
129    runtime: &Arc<dyn Runtime + Send + Sync + 'static>,
130) -> Result<TaskJoinHandle, SpawnError> {
131    // Create a new task manager
132    let tasks = runtime.task_manager();
133
134    // Create the signaler
135    let pid = env.pid();
136
137    let join_handle = env.thread.join_handle();
138    {
139        // Create a thread that will run this process
140        let tasks_outer = tasks.clone();
141
142        tasks_outer
143            .task_wasm(
144                TaskWasm::new(Box::new(run_exec), env, module, true, true).with_pre_run(Box::new(
145                    |ctx, store| {
146                        Box::pin(async move {
147                            ctx.data(store).state.fs.close_cloexec_fds().await;
148                        })
149                    },
150                )),
151            )
152            .map_err(|err| {
153                error!("wasi[{}]::failed to launch module - {}", pid, err);
154                SpawnError::Other(Box::new(err))
155            })?
156    };
157
158    Ok(join_handle)
159}
160
161/// # SAFETY
162/// This must be executed from the same thread that owns the instance as
163/// otherwise it will cause a panic
164unsafe fn run_recycle(
165    callback: Option<Box<TaskWasmRecycle>>,
166    ctx: WasiFunctionEnv,
167    mut store: Store,
168) {
169    if let Some(callback) = callback {
170        let env = ctx.data_mut(&mut store);
171        let memory = unsafe { env.memory() }.clone();
172
173        let props = TaskWasmRecycleProperties {
174            env: env.clone(),
175            memory,
176            store,
177        };
178        callback(props);
179    }
180}
181
182pub fn run_exec(props: TaskWasmRunProperties) {
183    let ctx = props.ctx;
184    let mut store = props.store;
185
186    // Create the WasiFunctionEnv
187    let thread = WasiThreadRunGuard::new(ctx.data(&store).thread.clone());
188    let recycle = props.recycle;
189
190    // Perform the initialization
191    // If this module exports an _initialize function, run that first.
192    if let Ok(initialize) = ctx
193        .data(&store)
194        .inner()
195        .main_module_instance_handles()
196        .instance
197        .exports
198        .get_function("_initialize")
199        .cloned()
200    {
201        // This does not need a context switching environment as the documentation
202        // states that that is only available after the first call to main
203        let result = initialize.call(&mut store, &[]);
204
205        if let Err(err) = result {
206            thread.thread.set_status_finished(Err(err.into()));
207            ctx.data(&store)
208                .blocking_on_exit(Some(Errno::Noexec.into()));
209            unsafe { run_recycle(recycle, ctx, store) };
210            return;
211        }
212    }
213
214    // Bootstrap the process
215    // Unsafe: The bootstrap must be executed in the same thread that runs the
216    //         actual WASM code
217    let rewind_state = match unsafe { ctx.bootstrap(&mut store) } {
218        Ok(r) => r,
219        Err(err) => {
220            tracing::warn!("failed to bootstrap - {}", err);
221            thread.thread.set_status_finished(Err(err));
222            ctx.data(&store)
223                .blocking_on_exit(Some(Errno::Noexec.into()));
224            unsafe { run_recycle(recycle, ctx, store) };
225            return;
226        }
227    };
228
229    // If there is a start function
230    debug!("wasi[{}]::called main()", ctx.data(&store).pid());
231    // TODO: rewrite to use crate::run_wasi_func
232
233    // Call the module
234    call_module(ctx, store, thread, rewind_state, recycle);
235}
236
237fn get_start(ctx: &WasiFunctionEnv, store: &Store) -> Option<Function> {
238    ctx.data(store)
239        .inner()
240        .main_module_instance_handles()
241        .instance
242        .exports
243        .get_function("_start")
244        .cloned()
245        .ok()
246}
247
248/// Calls the module
249fn call_module(
250    ctx: WasiFunctionEnv,
251    mut store: Store,
252    handle: WasiThreadRunGuard,
253    rewind_state: Option<(RewindState, RewindResultType)>,
254    recycle: Option<Box<TaskWasmRecycle>>,
255) {
256    let env = ctx.data(&store);
257    let pid = env.pid();
258    let tasks = env.tasks().clone();
259    handle.thread.set_status_running();
260    let runtime = env.runtime.clone();
261
262    // If we need to rewind then do so
263    if let Some((rewind_state, rewind_result)) = rewind_state {
264        let mut ctx = ctx.env.clone().into_mut(&mut store);
265        if rewind_state.is_64bit {
266            let res = rewind_ext::<Memory64>(
267                &mut ctx,
268                Some(rewind_state.memory_stack),
269                rewind_state.rewind_stack,
270                rewind_state.store_data,
271                rewind_result,
272            );
273            if res != Errno::Success {
274                ctx.data().blocking_on_exit(Some(res.into()));
275                unsafe { run_recycle(recycle, WasiFunctionEnv { env: ctx.as_ref() }, store) };
276                return;
277            }
278        } else {
279            let res = rewind_ext::<Memory32>(
280                &mut ctx,
281                Some(rewind_state.memory_stack),
282                rewind_state.rewind_stack,
283                rewind_state.store_data,
284                rewind_result,
285            );
286            if res != Errno::Success {
287                ctx.data().blocking_on_exit(Some(res.into()));
288                unsafe { run_recycle(recycle, WasiFunctionEnv { env: ctx.as_ref() }, store) };
289                return;
290            }
291        };
292    }
293
294    // Invoke the start function
295    // Call the module
296    let Some(start) = get_start(&ctx, &store) else {
297        debug!("wasi[{}]::exec-failed: missing _start function", pid);
298        ctx.data(&store)
299            .blocking_on_exit(Some(Errno::Noexec.into()));
300        unsafe { run_recycle(recycle, ctx, store) };
301        return;
302    };
303
304    let (mut store, mut call_ret) =
305        ContextSwitchingEnvironment::run_main_context(&ctx, store, start.clone(), vec![]);
306
307    let mut store = loop {
308        // Technically, it's an error for a vfork to return from main, but anyway...
309        store = match resume_vfork(&ctx, store, &start, &call_ret) {
310            // A vfork was resumed, there may be another, so loop back
311            (store, Ok(Some(ret))) => {
312                call_ret = ret;
313                store
314            }
315
316            // An error was encountered when restoring from the vfork, report it
317            (store, Err(e)) => {
318                call_ret = Err(RuntimeError::user(Box::new(WasiError::Exit(e.into()))));
319                break store;
320            }
321
322            // No vfork, keep the call_ret value
323            (store, Ok(None)) => break store,
324        };
325    };
326
327    let ret = if let Err(err) = call_ret {
328        match err.downcast::<WasiError>() {
329            Ok(WasiError::Exit(code)) if code.is_success() => Ok(Errno::Success),
330            Ok(WasiError::ThreadExit) => Ok(Errno::Success),
331            Ok(WasiError::Exit(code)) => {
332                runtime.on_taint(TaintReason::NonZeroExitCode(code));
333                Err(WasiError::Exit(code).into())
334            }
335            Ok(WasiError::DeepSleep(deep)) => {
336                // Create the callback that will be invoked when the thread respawns after a deep sleep
337                let rewind = deep.rewind;
338                let respawn = {
339                    move |ctx, store, rewind_result| {
340                        // Call the thread
341                        call_module(
342                            ctx,
343                            store,
344                            handle,
345                            Some((rewind, RewindResultType::RewindWithResult(rewind_result))),
346                            recycle,
347                        );
348                    }
349                };
350
351                // Spawns the WASM process after a trigger
352                if let Err(err) = unsafe {
353                    tasks.resume_wasm_after_poller(Box::new(respawn), ctx, store, deep.trigger)
354                } {
355                    debug!("failed to go into deep sleep - {}", err);
356                }
357                return;
358            }
359            Ok(WasiError::UnknownWasiVersion) => {
360                debug!("failed as wasi version is unknown");
361                runtime.on_taint(TaintReason::UnknownWasiVersion);
362                Ok(Errno::Noexec)
363            }
364            Ok(WasiError::DlSymbolResolutionFailed(symbol)) => {
365                debug!("failed as a needed DL symbol could not be resolved");
366                runtime.on_taint(TaintReason::DlSymbolResolutionFailed(symbol.clone()));
367                Err(WasiError::DlSymbolResolutionFailed(symbol).into())
368            }
369            Err(err) => {
370                runtime.on_taint(TaintReason::RuntimeError(err.clone()));
371                Err(WasiRuntimeError::from(err))
372            }
373        }
374    } else {
375        Ok(Errno::Success)
376    };
377
378    let code = if let Err(err) = &ret {
379        match err.as_exit_code() {
380            Some(s) => s,
381            None => {
382                let err_display = err.display(&mut store);
383                error!("{err_display}");
384                eprintln!("{err_display}");
385                Errno::Noexec.into()
386            }
387        }
388    } else {
389        Errno::Success.into()
390    };
391
392    // Cleanup the environment
393    ctx.data(&store).blocking_on_exit(Some(code));
394    unsafe { run_recycle(recycle, ctx, store) };
395
396    debug!("wasi[{pid}]::main() has exited with {code}");
397    handle.thread.set_status_finished(ret.map(|a| a.into()));
398}
399
400#[allow(clippy::type_complexity)]
401fn resume_vfork(
402    ctx: &WasiFunctionEnv,
403    mut store: Store,
404    start: &Function,
405    call_ret: &Result<Box<[Value]>, RuntimeError>,
406) -> (
407    Store,
408    Result<Option<Result<Box<[Value]>, RuntimeError>>, Errno>,
409) {
410    let (err, code) = match call_ret {
411        Ok(_) => (None, wasmer_wasix_types::wasi::ExitCode::from(0u16)),
412        Err(err) => match err.downcast_ref::<WasiError>() {
413            // If the child process is just deep sleeping, we don't restore the vfork
414            Some(WasiError::DeepSleep(..)) => return (store, Ok(None)),
415
416            Some(WasiError::Exit(code)) => (None, *code),
417            Some(WasiError::ThreadExit) => (None, wasmer_wasix_types::wasi::ExitCode::from(0u16)),
418            Some(WasiError::UnknownWasiVersion) => (None, Errno::Noexec.into()),
419            Some(WasiError::DlSymbolResolutionFailed(_)) => (None, Errno::Nolink.into()),
420            None => (
421                Some(WasiRuntimeError::from(err.clone())),
422                Errno::Unknown.into(),
423            ),
424        },
425    };
426
427    if let Some(mut vfork) = ctx.data_mut(&mut store).vfork.take() {
428        if let Some(err) = err {
429            error!(%err, "Error from child process");
430            eprintln!("{err}");
431        }
432
433        block_on(
434            unsafe { ctx.data(&store).get_memory_and_wasi_state(&store, 0) }
435                .1
436                .fs
437                .close_all(),
438        );
439
440        tracing::debug!(
441            pid = %ctx.data_mut(&mut store).process.pid(),
442            vfork_pid = %vfork.env.process.pid(),
443            "Resuming from vfork after child process was terminated"
444        );
445
446        // Restore the WasiEnv to the point when we vforked
447        vfork.env.swap_inner(ctx.data_mut(&mut store));
448        std::mem::swap(vfork.env.as_mut(), ctx.data_mut(&mut store));
449        let mut child_env = *vfork.env;
450        child_env.owned_handles.push(vfork.handle);
451
452        // Terminate the child process
453        child_env.process.terminate(code);
454
455        // If the vfork contained a context-switching environment, exit now
456        if ctx.data(&store).context_switching_environment.is_some() {
457            // We cannot recover from this situation when using context switching
458            tracing::error!(
459                "Terminated a vfork in another way than exit or exec which is undefined behaviour. In this case the parent process will be terminated."
460            );
461            return (store, Err(code.into()));
462        }
463        let Some(asyncify_info) = vfork.asyncify else {
464            // We can only recover from this situation when using asyncify-based vforking; since asyncify is not in use here, we cannot recover and must terminate the parent process
465            tracing::error!(
466                "Terminated a vfork in another way than exit or exec which is undefined behaviour. In this case the parent process will be terminated."
467            );
468            return (store, Err(code.into()));
469        };
470        // TODO: We can also only safely recover if we are not using nested calling
471        // TODO: Just delete this branch
472
473        // Jump back to the vfork point and continue execution
474        let child_pid = child_env.process.pid();
475        let rewind_stack = asyncify_info.rewind_stack.freeze();
476        let store_data = asyncify_info.store_data;
477
478        let ctx_cloned = ctx.env.clone().into_mut(&mut store);
479        // Now rewind the previous stack and carry on from where we did the vfork
480        let rewind_result = if asyncify_info.is_64bit {
481            crate::syscalls::rewind::<Memory64, _>(
482                ctx_cloned,
483                None,
484                rewind_stack,
485                store_data,
486                crate::syscalls::ForkResult {
487                    pid: child_pid.raw() as wasmer_wasix_types::wasi::Pid,
488                    ret: Errno::Success,
489                },
490            )
491        } else {
492            crate::syscalls::rewind::<Memory32, _>(
493                ctx_cloned,
494                None,
495                rewind_stack,
496                store_data,
497                crate::syscalls::ForkResult {
498                    pid: child_pid.raw() as wasmer_wasix_types::wasi::Pid,
499                    ret: Errno::Success,
500                },
501            )
502        };
503
504        match rewind_result {
505            Errno::Success => {
506                // We should only get here, if the engine does not support context switching
507                // If the engine supports it, we should exit in the check a few lines above
508                let (store, result) = ContextSwitchingEnvironment::run_main_context(
509                    ctx,
510                    store,
511                    start.clone(),
512                    vec![],
513                );
514                (store, Ok(Some(result)))
515            }
516            err => {
517                warn!("fork failed - could not rewind the stack - errno={}", err);
518                (store, Err(err))
519            }
520        }
521    } else {
522        (store, Ok(None))
523    }
524}