wasmer_wasix/os/console/
mod.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
#![allow(unused_imports)]
#![allow(dead_code)]

pub mod cconst;

use std::{
    borrow::Cow,
    collections::HashMap,
    io::Write,
    ops::{Deref, DerefMut},
    path::{Path, PathBuf},
    sync::{atomic::AtomicBool, Arc, Mutex},
};

use derivative::*;
use linked_hash_set::LinkedHashSet;
use tokio::sync::{mpsc, RwLock};
#[allow(unused_imports, dead_code)]
use tracing::{debug, error, info, trace, warn};
use virtual_fs::{
    ArcBoxFile, ArcFile, AsyncWriteExt, CombineFile, DeviceFile, DuplexPipe, FileSystem, Pipe,
    PipeRx, PipeTx, RootFileSystemBuilder, StaticFile, VirtualFile,
};
#[cfg(feature = "sys")]
use wasmer::Engine;
use wasmer_config::package::PackageSource;
use wasmer_wasix_types::{types::__WASI_STDIN_FILENO, wasi::Errno};

use super::{cconst::ConsoleConst, common::*, task::TaskJoinHandle};
use crate::{
    bin_factory::{spawn_exec, BinFactory, BinaryPackage},
    capabilities::Capabilities,
    os::task::{control_plane::WasiControlPlane, process::WasiProcess},
    runtime::task_manager::InlineWaker,
    Runtime, SpawnError, WasiEnv, WasiEnvBuilder, WasiRuntimeError,
};

#[derive(Derivative)]
#[derivative(Debug)]
pub struct Console {
    user_agent: Option<String>,
    boot_cmd: String,
    uses: LinkedHashSet<String>,
    is_mobile: bool,
    is_ssh: bool,
    whitelabel: bool,
    token: Option<String>,
    no_welcome: bool,
    prompt: String,
    env: HashMap<String, String>,
    runtime: Arc<dyn Runtime + Send + Sync>,
    stdin: ArcBoxFile,
    stdout: ArcBoxFile,
    stderr: ArcBoxFile,
    capabilities: Capabilities,
    ro_files: HashMap<String, Cow<'static, [u8]>>,
    memfs_memory_limiter: Option<virtual_fs::limiter::DynFsMemoryLimiter>,
}

impl Console {
    pub fn new(webc_boot_package: &str, runtime: Arc<dyn Runtime + Send + Sync + 'static>) -> Self {
        Self {
            boot_cmd: webc_boot_package.to_string(),
            uses: LinkedHashSet::new(),
            is_mobile: false,
            is_ssh: false,
            user_agent: None,
            whitelabel: false,
            token: None,
            no_welcome: false,
            env: HashMap::new(),
            runtime,
            prompt: "wasmer.sh".to_string(),
            stdin: ArcBoxFile::new(Box::new(Pipe::channel().0)),
            stdout: ArcBoxFile::new(Box::new(Pipe::channel().0)),
            stderr: ArcBoxFile::new(Box::new(Pipe::channel().0)),
            capabilities: Default::default(),
            memfs_memory_limiter: None,
            ro_files: Default::default(),
        }
    }

    pub fn with_prompt(mut self, prompt: String) -> Self {
        self.prompt = prompt;
        self
    }

    pub fn with_boot_cmd(mut self, cmd: String) -> Self {
        let prog = cmd.split_once(' ').map(|a| a.0).unwrap_or(cmd.as_str());
        self.uses.insert(prog.to_string());
        self.boot_cmd = cmd;
        self
    }

    pub fn with_uses(mut self, uses: Vec<String>) -> Self {
        self.uses = uses.into_iter().collect();
        self
    }

    pub fn with_env(mut self, env: HashMap<String, String>) -> Self {
        self.env = env;
        self
    }

    pub fn with_user_agent(mut self, user_agent: &str) -> Self {
        self.is_mobile = is_mobile(user_agent);
        self.is_ssh = is_ssh(user_agent);
        self.user_agent = Some(user_agent.to_string());
        self
    }

    pub fn with_no_welcome(mut self, no_welcome: bool) -> Self {
        self.no_welcome = no_welcome;
        self
    }

    pub fn with_token(mut self, token: String) -> Self {
        self.token = Some(token);
        self
    }

    pub fn with_capabilities(mut self, caps: Capabilities) -> Self {
        self.capabilities = caps;
        self
    }

    pub fn with_stdin(mut self, stdin: Box<dyn VirtualFile + Send + Sync + 'static>) -> Self {
        self.stdin = ArcBoxFile::new(stdin);
        self
    }

    pub fn with_stdout(mut self, stdout: Box<dyn VirtualFile + Send + Sync + 'static>) -> Self {
        self.stdout = ArcBoxFile::new(stdout);
        self
    }

    pub fn with_stderr(mut self, stderr: Box<dyn VirtualFile + Send + Sync + 'static>) -> Self {
        self.stderr = ArcBoxFile::new(stderr);
        self
    }

    pub fn with_ro_files(mut self, ro_files: HashMap<String, Cow<'static, [u8]>>) -> Self {
        self.ro_files = ro_files;
        self
    }

    pub fn with_mem_fs_memory_limiter(
        mut self,
        limiter: virtual_fs::limiter::DynFsMemoryLimiter,
    ) -> Self {
        self.memfs_memory_limiter = Some(limiter);
        self
    }

    pub fn run(&mut self) -> Result<(TaskJoinHandle, WasiProcess), SpawnError> {
        // Extract the program name from the arguments
        let empty_args: Vec<&str> = Vec::new();
        let (webc, prog, args) = match self.boot_cmd.split_once(' ') {
            Some((webc, args)) => (
                webc,
                webc.split_once('/').map(|a| a.1).unwrap_or(webc),
                args.split(' ').collect::<Vec<_>>(),
            ),
            None => (
                self.boot_cmd.as_str(),
                self.boot_cmd
                    .split_once('/')
                    .map(|a| a.1)
                    .unwrap_or(self.boot_cmd.as_str()),
                empty_args,
            ),
        };

        let webc_ident: PackageSource = match webc.parse() {
            Ok(ident) => ident,
            Err(e) => {
                tracing::debug!(webc, error = ?e, "Unable to parse the WEBC identifier");
                return Err(SpawnError::BadRequest);
            }
        };

        let resolved_package = InlineWaker::block_on(BinaryPackage::from_registry(
            &webc_ident,
            self.runtime.as_ref(),
        ));

        let pkg = match resolved_package {
            Ok(pkg) => pkg,
            Err(e) => {
                let mut stderr = self.stderr.clone();
                InlineWaker::block_on(async {
                    let mut buffer = Vec::new();
                    writeln!(buffer, "Error: {e}").ok();
                    let mut source = e.source();
                    while let Some(s) = source {
                        writeln!(buffer, "  Caused by: {s}").ok();
                        source = s.source();
                    }

                    virtual_fs::AsyncWriteExt::write_all(&mut stderr, &buffer)
                        .await
                        .ok();
                });
                tracing::debug!(error=?e, %webc, "failed to get webc dependency");
                return Err(SpawnError::NotFound {
                    message: e.to_string(),
                });
            }
        };

        let wasi_opts = webc::metadata::annotations::Wasi::new(prog);

        let root_fs = RootFileSystemBuilder::new()
            .with_tty(Box::new(CombineFile::new(
                Box::new(self.stdout.clone()),
                Box::new(self.stdin.clone()),
            )))
            .build();

        if let Some(limiter) = &self.memfs_memory_limiter {
            root_fs.set_memory_limiter(limiter.clone());
        }

        let builder = crate::runners::wasi::WasiRunner::new()
            .with_envs(self.env.clone().into_iter())
            .with_args(args)
            .with_capabilities(self.capabilities.clone())
            .with_stdin(Box::new(self.stdin.clone()))
            .with_stdout(Box::new(self.stdout.clone()))
            .with_stderr(Box::new(self.stderr.clone()))
            .prepare_webc_env(
                prog,
                &wasi_opts,
                Some(&pkg),
                self.runtime.clone(),
                Some(root_fs),
            )
            // TODO: better error conversion
            .map_err(|err| SpawnError::Other(err.into()))?;

        let env = builder.build()?;

        // Display the welcome message
        if !self.whitelabel && !self.no_welcome {
            InlineWaker::block_on(self.draw_welcome());
        }

        let wasi_process = env.process.clone();

        if let Err(err) = env.uses(self.uses.clone()) {
            let mut stderr = self.stderr.clone();
            InlineWaker::block_on(async {
                virtual_fs::AsyncWriteExt::write_all(
                    &mut stderr,
                    format!("{}\r\n", err).as_bytes(),
                )
                .await
                .ok();
            });
            tracing::debug!("failed to load used dependency - {}", err);
            return Err(SpawnError::BadRequest);
        }

        // The custom readonly files have to be added after the uses packages
        // otherwise they will be overriden by their attached file systems
        for (path, data) in self.ro_files.clone() {
            let path = PathBuf::from(path);
            env.fs_root().remove_file(&path).ok();
            let mut file = env
                .fs_root()
                .new_open_options()
                .create(true)
                .truncate(true)
                .write(true)
                .open(&path)
                .map_err(|err| SpawnError::Other(err.into()))?;
            InlineWaker::block_on(file.copy_reference(Box::new(StaticFile::new(data))))
                .map_err(|err| SpawnError::Other(err.into()))?;
        }

        // Build the config
        // Run the binary
        let store = self.runtime.new_store();
        let process = InlineWaker::block_on(spawn_exec(pkg, prog, store, env, &self.runtime))?;

        // Return the process
        Ok((process, wasi_process))
    }

    pub async fn draw_welcome(&self) {
        let welcome = match (self.is_mobile, self.is_ssh) {
            (true, _) => ConsoleConst::WELCOME_MEDIUM,
            (_, true) => ConsoleConst::WELCOME_SMALL,
            (_, _) => ConsoleConst::WELCOME_LARGE,
        };
        let mut data = welcome
            .replace("\\x1B", "\x1B")
            .replace("\\r", "\r")
            .replace("\\n", "\n");
        data.insert_str(0, ConsoleConst::TERM_NO_WRAPAROUND);

        let mut stderr = self.stderr.clone();
        virtual_fs::AsyncWriteExt::write_all(&mut stderr, data.as_bytes())
            .await
            .ok();
    }
}

#[cfg(all(test, not(target_family = "wasm")))]
mod tests {
    use virtual_fs::{AsyncSeekExt, BufferFile, Pipe};

    use super::*;

    use std::{io::Read, sync::Arc};

    use crate::{
        runtime::{package_loader::BuiltinPackageLoader, task_manager::tokio::TokioTaskManager},
        PluggableRuntime,
    };

    /// Test that [`Console`] correctly runs a command with arguments and
    /// specified env vars, and that the TTY correctly handles stdout output.
    ///
    /// Note that this test currently aborts the process unconditionally due
    /// to a misaligned pointer access in stack_checkpoint() triggering a panic
    /// in a function that isn't allowed to unwind.
    ///
    /// See [#4284](https://github.com/wasmerio/wasmer/issues/4284) for more.
    #[test]
    #[cfg_attr(not(feature = "host-reqwest"), ignore = "Requires a HTTP client")]
    #[ignore = "Unconditionally aborts (CC #4284)"]
    fn test_console_dash_tty_with_args_and_env() {
        let tokio_rt = tokio::runtime::Runtime::new().unwrap();
        let rt_handle = tokio_rt.handle().clone();
        let _guard = rt_handle.enter();

        let tm = TokioTaskManager::new(tokio_rt);
        let mut rt = PluggableRuntime::new(Arc::new(tm));
        let client = rt.http_client().unwrap().clone();
        rt.set_engine(Some(wasmer::Engine::default()))
            .set_package_loader(BuiltinPackageLoader::new().with_shared_http_client(client));

        let env: HashMap<String, String> = [("MYENV1".to_string(), "VAL1".to_string())]
            .into_iter()
            .collect();

        // Pass some arguments.
        let cmd = "sharrattj/dash -s stdin";

        let (mut stdin_tx, stdin_rx) = Pipe::channel();
        let (stdout_tx, mut stdout_rx) = Pipe::channel();

        let (mut handle, _proc) = Console::new(cmd, Arc::new(rt))
            .with_env(env)
            .with_stdin(Box::new(stdin_rx))
            .with_stdout(Box::new(stdout_tx))
            .run()
            .unwrap();

        let code = rt_handle
            .block_on(async move {
                virtual_fs::AsyncWriteExt::write_all(
                    &mut stdin_tx,
                    b"echo hello $MYENV1 > /dev/tty; exit\n",
                )
                .await?;

                stdin_tx.close();
                std::mem::drop(stdin_tx);

                let res = handle.wait_finished().await?;
                Ok::<_, anyhow::Error>(res)
            })
            .unwrap();

        assert_eq!(code.raw(), 78);

        let mut out = String::new();
        stdout_rx.read_to_string(&mut out).unwrap();

        assert_eq!(out, "hello VAL1\n");
    }

    /// Regression test to ensure merging of multiple packages works correctly.
    #[test]
    #[ignore = "must be re-enabled after backend is deployed"]
    fn test_console_python_merge() {
        let tokio_rt = tokio::runtime::Runtime::new().unwrap();
        let rt_handle = tokio_rt.handle().clone();
        let _guard = rt_handle.enter();

        let tm = TokioTaskManager::new(tokio_rt);
        let mut rt = PluggableRuntime::new(Arc::new(tm));
        let client = rt.http_client().unwrap().clone();
        rt.set_engine(Some(wasmer::Engine::default()))
            .set_package_loader(BuiltinPackageLoader::new().with_shared_http_client(client));

        let cmd = "wasmer-tests/python-env-dump --help";

        let (mut handle, _proc) = Console::new(cmd, Arc::new(rt)).run().unwrap();

        let code = rt_handle
            .block_on(async move {
                let res = handle.wait_finished().await?;
                Ok::<_, anyhow::Error>(res)
            })
            .unwrap();

        assert_eq!(code.raw(), 0);
    }
}