wasi_virt_layer-cli 0.10.1

CLI for the wasi_virt_layer crate
Documentation
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
use std::{
    fs::File,
    io::{Read as _, Seek as _, Write as _},
};

use eyre::Context as _;
use fs2::FileExt as _;
use tempfile::Builder as TempFileBuilder;

pub(crate) const COMMAND_ALTERNATE_ENV_VAR: &str = "WASI_VIRT_LAYER_FALLBACK_ALTERNATE_COMMAND";

#[allow(unused)]
pub(crate) fn wasm_opt(args: &[String]) -> i32 {
    #[cfg(feature = "fallback")]
    {
        if args
            .iter()
            .any(|arg| arg.starts_with("--no-inline=__wvl_reactor_initialize"))
        {
            // The 0.116 backend cannot parse pass arguments or schedule
            // no-inline before its default optimization passes. Never silently
            // inline the reactor when the native optimizer is missing.
            eprintln!(
                "threaded reactor optimization requires native wasm-opt with --no-inline support; install wasm-opt or use --dev"
            );
            return 1;
        }

        if args.iter().any(|arg| arg == "--version") {
            println!("wasm-opt fallback");
            return 0;
        }

        if args.iter().any(|arg| {
            matches!(
                arg.as_str(),
                "--enable-shared-everything" | "--enable-multimemory"
            )
        }) {
            if let Some(output_index) = args.iter().position(|arg| arg == "--output")
                && let Some(input) = args[..output_index]
                    .iter()
                    .rev()
                    .find(|arg| !arg.starts_with('-'))
                && let Some(output) = args.get(output_index + 1)
            {
                return match std::fs::copy(input, output) {
                    Ok(_) => 0,
                    Err(err) => {
                        eprintln!("wasm-opt fallback failed: {err}");
                        1
                    }
                };
            }
        }

        let mut command = wasm_opt::integration::Command::new("wasm-opt");
        command.args(args.iter().skip(1));

        match wasm_opt::integration::run_from_command_args(command) {
            Ok(()) => 0,
            Err(err) => {
                eprintln!("wasm-opt fallback failed: {err}");
                1
            }
        }
    }
    #[cfg(not(feature = "fallback"))]
    {
        eprintln!("wasm-opt fallback is not enabled");
        1
    }
}

fn fake_fallback(args: &[String]) -> i32 {
    unimplemented!("This is a fake fallback function. It should never be called. Args: {args:?}");
}

/// Gets a `FallbackCommand` for the specified binary, with a fallback function that will be called if the binary is not found.
pub fn get_fallback_command(
    bin: impl AsRef<str>,
) -> FallbackCommand<impl FnOnce(&[String]) -> i32 + Send + 'static> {
    match bin.as_ref() {
        "wasm-opt" => FallbackCommand::new("wasm-opt", fake_fallback),
        _ => panic!("Unsupported fallback command specified: {}", bin.as_ref()),
    }
}

/// A command that can fall back to a Rust function if the binary is not found.
pub struct FallbackCommand<F>
where
    F: FnOnce(&[String]) -> i32 + Send + 'static,
{
    /// The binary name.
    pub bin: String,
    /// The arguments to the command.
    pub args: Vec<String>,
    /// The optional fallback function.
    pub func: Option<F>,
}

const DISABLE_FALLBACK: bool = false;

/// A file-based lock to prevent concurrent execution of commands.
pub struct CommandLock {
    locks: Vec<(File, String)>,
}

impl<F> FallbackCommand<F>
where
    F: FnOnce(&[String]) -> i32 + Send + 'static,
{
    /// Creates a new `FallbackCommand` with the specified binary and fallback function.
    pub fn new(bin: impl AsRef<str>, func: F) -> Self {
        Self {
            bin: bin.as_ref().to_string(),
            args: Vec::new(),
            func: Some(func),
        }
    }

    /// Adds an argument to the command.
    pub fn arg(&mut self, arg: impl AsRef<str>) -> &mut Self {
        self.args.push(arg.as_ref().to_string());
        self
    }

    /// Adds multiple arguments to the command.
    pub fn args<I, S>(&mut self, args: I) -> &mut Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        for arg in args {
            self.arg(arg.as_ref());
        }
        self
    }

    /// Spawns the command, either as a child process or as a fallback thread.
    pub fn spawn(&mut self) -> std::io::Result<FallbackChild> {
        let mut cmd = std::process::Command::new(&self.bin);
        let setter = |cmd: &mut std::process::Command| {
            cmd.args(&self.args);
            let piped_out = std::process::Stdio::piped();
            let piped_err = std::process::Stdio::piped();
            cmd.stdout(piped_out);
            cmd.stderr(piped_err);
        };
        setter(&mut cmd);
        match cmd.spawn() {
            Ok(child) => Ok(FallbackChild::new_process(child)),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound && DISABLE_FALLBACK => {
                let _ = self.func.take();
                Err(e)
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // // Fallback to the provided function
                // let args = self.args.clone();
                // let func = self.func.take().expect("Function already taken");
                // let handle = std::thread::spawn(move || {
                //     let log = std::fs::OpenOptions::new()
                //         .truncate(true)
                //         .read(true)
                //         .create(true)
                //         .write(true)
                //         .open(get_temp_filepath())
                //         .unwrap();

                //     let print_redirect = gag::Redirect::stdout(log).unwrap();

                //     let result = (func)(&args);

                //     // Extract redirect
                //     let mut log = print_redirect.into_inner();

                //     let mut buf = String::new();
                //     log.seek(std::io::SeekFrom::Start(0)).unwrap();
                //     log.read_to_string(&mut buf).unwrap();

                //     FallbackOutput {
                //         stdout: buf.into_bytes(),
                //         stderr: Vec::new(),
                //         success: result == 0,
                //     }
                // });
                // Ok(FallbackChild::new_thread(handle))

                // Fallback to self call with env var
                let mut cmd = std::process::Command::new(std::env::current_exe().unwrap());
                setter(&mut cmd);
                cmd.env(COMMAND_ALTERNATE_ENV_VAR, &self.bin);
                let child = cmd.spawn()?;
                Ok(FallbackChild::new_process(child))
            }
            Err(e) => Err(e),
        }
    }
}

/// A handle to a spawned fallback command, which could be a process or a thread.
pub enum FallbackChild {
    /// A child process handle.
    Process(std::process::Child),
    /// A handle to a thread running the fallback function.
    Thread(std::thread::JoinHandle<FallbackOutput>),
}

impl FallbackChild {
    fn new_process(child: std::process::Child) -> Self {
        FallbackChild::Process(child)
    }

    #[allow(unused)]
    fn new_thread(handle: std::thread::JoinHandle<FallbackOutput>) -> Self {
        FallbackChild::Thread(handle)
    }

    /// Waits for the command to finish and returns its output.
    pub fn wait_with_output(self) -> std::io::Result<FallbackOutput> {
        match self {
            FallbackChild::Process(child) => {
                let output = child.wait_with_output()?;
                Ok(FallbackOutput {
                    stdout: output.stdout,
                    stderr: output.stderr,
                    success: output.status.success(),
                })
            }
            FallbackChild::Thread(handle) => {
                let out = handle.join().expect("Thread panicked");
                Ok(out)
            }
        }
    }
}

/// The output of a finished fallback command.
pub struct FallbackOutput {
    /// The standard output of the command.
    pub stdout: Vec<u8>,
    /// The standard error of the command.
    pub stderr: Vec<u8>,
    /// Whether the command exited successfully.
    pub success: bool,
}

impl CommandLock {
    /// Acquires the command locks for the specified identifiers using a master-lock strategy to prevent deadlocks.
    pub fn acquire(ids: &[String]) -> eyre::Result<Self> {
        let mut sorted_ids = ids.to_vec();
        sorted_ids.sort();
        sorted_ids.dedup();

        // 1. Ensure temp dir exists
        let temp_dir = std::env::temp_dir().join(env!("CARGO_PKG_NAME"));
        std::fs::create_dir_all(&temp_dir).wrap_err("Failed to create temp lock directory")?;

        // 2. Acquire Master Lock to serialize acquisition phase
        let master_path = temp_dir.join("master.lock");
        let master_file = File::options()
            .read(true)
            .write(true)
            .create(true)
            .open(&master_path)
            .wrap_err_with(|| {
                format!("Failed to open master lock file: {}", master_path.display())
            })?;

        master_file
            .lock_exclusive()
            .wrap_err("Failed to acquire master lock")?;

        let mut locks = Vec::new();

        // 3. Acquire individual locks while holding the master lock
        for id in sorted_ids {
            let hash = {
                use std::hash::{Hash, Hasher};
                let mut hasher = std::collections::hash_map::DefaultHasher::new();
                id.hash(&mut hasher);
                format!("{:x}", hasher.finish())
            };

            let lock_path = get_temp_lock_filepath(&hash);
            let lock_file = File::options()
                .read(true)
                .write(true)
                .create(true)
                .open(&lock_path)
                .wrap_err_with(|| format!("Failed to open lock file: {lock_path}"))?;

            if let Err(_) = lock_file.try_lock_exclusive() {
                let mut pid_str = String::new();
                let mut file = &lock_file;
                let _ = file.read_to_string(&mut pid_str);
                let pid_info = if pid_str.is_empty() {
                    String::new()
                } else {
                    format!(" (held by PID {})", pid_str.trim())
                };

                eprintln!("Waiting for command lock for {} {}...", id, pid_info);

                // We are holding the master lock, so we serialize all acquisitions.
                // If we block here, other processes waiting for DIFFERENT locks will also be blocked
                // at the master lock. This is acceptable for a CLI tool and prevents deadlock.
                lock_file
                    .lock_exclusive()
                    .wrap_err_with(|| format!("Failed to lock command file: {lock_path}"))?;
            }

            // Write current PID to the lock file
            let mut file = &lock_file;
            let _ = file.set_len(0);
            let _ = file.seek(std::io::SeekFrom::Start(0));
            let _ = write!(file, "{}", std::process::id());
            let _ = file.flush();

            locks.push((lock_file, lock_path));
        }

        // 4. Release master lock
        let _ = master_file.unlock();

        Ok(Self { locks })
    }
}

impl Drop for CommandLock {
    fn drop(&mut self) {
        for (file, _) in &mut self.locks {
            let _ = file.unlock();
        }
    }
}

fn get_temp_filepath() -> String {
    let mut builder = TempFileBuilder::new();
    let prefix = format!("tmp_{}_", env!("CARGO_PKG_NAME"));
    builder.prefix(&prefix);
    builder.suffix(".log");

    let builder = builder.tempfile_in(std::env::temp_dir());

    let file = builder.expect("Failed to create temp log file");
    let (_file, path) = file.keep().expect("Failed to persist temp log file");

    path.to_string_lossy().into_owned()
}

fn get_temp_lock_filepath(hash: &str) -> String {
    std::env::temp_dir()
        .join(env!("CARGO_PKG_NAME"))
        .join(format!("command_{}.lock", hash))
        .to_string_lossy()
        .into_owned()
}

/// require mutex
pub fn check_gag() -> bool {
    pub fn check_gag() -> Option<()> {
        let gag = gag::Redirect::stdout(
            std::fs::OpenOptions::new()
                .truncate(true)
                .read(true)
                .create(true)
                .write(true)
                .open(get_temp_filepath())
                .ok()?,
        )
        .ok()?;

        const WHITE_SPACE: &str = " \t\n\r";

        print!("{WHITE_SPACE}");
        std::io::Write::flush(&mut std::io::stdout()).ok()?;

        let mut stdout = gag.into_inner();
        let mut buf = String::new();
        stdout.seek(std::io::SeekFrom::Start(0)).ok()?;
        stdout.read_to_string(&mut buf).ok()?;

        Some(if buf.contains(WHITE_SPACE) {
            ()
        } else {
            return None;
        })
    }

    check_gag().is_some()
}

#[cfg(test)]
mod tests {
    use super::*;

    static MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());

    #[ignore = "self-call fallback cannot execute an arbitrary test closure"]
    #[test]
    /// if not use nocapture arg, skip test.
    /// because gag crate require it.
    fn test_fallback_command() {
        if DISABLE_FALLBACK {
            return;
        }

        let _lock = MUTEX.lock().unwrap();
        if !check_gag() {
            return;
        }

        let mut cmd = FallbackCommand::new("non_existent_command", |args: &[String]| {
            println!("Fallback function called with args: {:?}", args);
            0
        });
        cmd.arg("arg1").arg("arg2");

        let child = cmd.spawn().expect("Failed to spawn command");
        let output = child.wait_with_output().expect("Failed to get output");

        assert!(output.success);
        let stdout_str = String::from_utf8_lossy(&output.stdout);
        assert!(stdout_str.contains("Fallback function called with args: [\"arg1\", \"arg2\"]"));

        drop(_lock);
    }
}