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"))
{
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:?}");
}
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()),
}
}
pub struct FallbackCommand<F>
where
F: FnOnce(&[String]) -> i32 + Send + 'static,
{
pub bin: String,
pub args: Vec<String>,
pub func: Option<F>,
}
const DISABLE_FALLBACK: bool = false;
pub struct CommandLock {
locks: Vec<(File, String)>,
}
impl<F> FallbackCommand<F>
where
F: FnOnce(&[String]) -> i32 + Send + 'static,
{
pub fn new(bin: impl AsRef<str>, func: F) -> Self {
Self {
bin: bin.as_ref().to_string(),
args: Vec::new(),
func: Some(func),
}
}
pub fn arg(&mut self, arg: impl AsRef<str>) -> &mut Self {
self.args.push(arg.as_ref().to_string());
self
}
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
}
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 => {
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),
}
}
}
pub enum FallbackChild {
Process(std::process::Child),
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)
}
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)
}
}
}
}
pub struct FallbackOutput {
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
pub success: bool,
}
impl CommandLock {
pub fn acquire(ids: &[String]) -> eyre::Result<Self> {
let mut sorted_ids = ids.to_vec();
sorted_ids.sort();
sorted_ids.dedup();
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")?;
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();
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);
lock_file
.lock_exclusive()
.wrap_err_with(|| format!("Failed to lock command file: {lock_path}"))?;
}
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));
}
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()
}
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]
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);
}
}