184 lines
6.5 KiB
Rust
184 lines
6.5 KiB
Rust
//! Runs the decode child from the keyed parent (review Part IV.1): empty environment,
|
||
//! bounded output, wall-clock deadline, and the mapping of every way the child can end to
|
||
//! a closed outcome. The parent reads the container header of the output, never the pixels.
|
||
|
||
use std::io::{Read, Write};
|
||
use std::os::unix::process::ExitStatusExt;
|
||
use std::path::PathBuf;
|
||
use std::process::{Command, ExitStatus, Stdio};
|
||
use std::sync::mpsc;
|
||
use std::time::{Duration, Instant};
|
||
|
||
use crate::limits::{CHILD_DEADLINE, MAX_OUTPUT_BYTES, OUTPUT_SIDE};
|
||
use crate::{exit_codes, webp_check};
|
||
|
||
/// How a decode child ended.
|
||
#[derive(Debug)]
|
||
pub enum ChildOutcome {
|
||
/// A 512 × 512 WebP whose container passed the parent's check.
|
||
Produced(Vec<u8>),
|
||
/// The child refused the input with one of its content exit codes
|
||
/// (`exit_codes::is_content_rejection`).
|
||
Rejected { code: i32, name: &'static str },
|
||
/// The child could not run properly (usage, non-empty environment, sandbox failure,
|
||
/// encode or output failure, internal error): the confinement environment is broken,
|
||
/// not the upload. No content can produce this outcome (every decode-time error maps
|
||
/// to a content code). R1 alerts and stops on it instead of rejecting the job.
|
||
Broken { code: i32, name: &'static str },
|
||
/// The child died from a signal (SIGSYS 31 = seccomp, SIGABRT 6 = allocation failure,
|
||
/// SIGXCPU 24 = CPU limit, SIGKILL 9 = the parent).
|
||
Signalled { signal: i32 },
|
||
/// The child wrote more than the output cap and was killed.
|
||
OutputOverflow,
|
||
/// The child outlived the deadline and was killed.
|
||
Timeout,
|
||
/// The child exited 0 but its output failed the container check, or it could not run.
|
||
Failed(String),
|
||
}
|
||
|
||
/// A finished child run. `stderr` is text the child controls (up to 4 KiB): never logged
|
||
/// raw by R1, control characters escaped and the field allowlist applied (LRN-044).
|
||
#[derive(Debug)]
|
||
pub struct ChildRun {
|
||
pub outcome: ChildOutcome,
|
||
pub stderr: String,
|
||
}
|
||
|
||
/// Spawns `decode` children of one worker binary.
|
||
#[derive(Debug, Clone)]
|
||
pub struct Spawner {
|
||
pub exe: PathBuf,
|
||
pub deadline: Duration,
|
||
pub max_output: usize,
|
||
pub extra_args: Vec<String>,
|
||
}
|
||
|
||
impl Spawner {
|
||
pub fn new(exe: impl Into<PathBuf>) -> Self {
|
||
Self {
|
||
exe: exe.into(),
|
||
deadline: CHILD_DEADLINE,
|
||
max_output: MAX_OUTPUT_BYTES,
|
||
extra_args: Vec::new(),
|
||
}
|
||
}
|
||
|
||
/// Feeds `input` to a fresh child and classifies what comes back.
|
||
pub fn decode(&self, input: &[u8]) -> ChildRun {
|
||
let spawned = Command::new(&self.exe)
|
||
.arg("decode")
|
||
.args(&self.extra_args)
|
||
.env_clear()
|
||
.stdin(Stdio::piped())
|
||
.stdout(Stdio::piped())
|
||
.stderr(Stdio::piped())
|
||
.spawn();
|
||
let mut child = match spawned {
|
||
Ok(child) => child,
|
||
Err(error) => return failed(format!("spawn: {error}")),
|
||
};
|
||
let (Some(mut stdin), Some(stdout), Some(stderr)) =
|
||
(child.stdin.take(), child.stdout.take(), child.stderr.take())
|
||
else {
|
||
let _ = child.kill();
|
||
let _ = child.wait();
|
||
return failed("child pipes unavailable".to_owned());
|
||
};
|
||
|
||
let input = input.to_vec();
|
||
let writer = std::thread::spawn(move || {
|
||
let _ = stdin.write_all(&input);
|
||
});
|
||
let cap = self.max_output;
|
||
let (tx, rx) = mpsc::channel();
|
||
std::thread::spawn(move || {
|
||
let mut buffer = Vec::new();
|
||
let mut limited = stdout.take(cap as u64 + 1);
|
||
let _ = limited.read_to_end(&mut buffer);
|
||
let _ = tx.send(buffer);
|
||
});
|
||
let err_reader = std::thread::spawn(move || {
|
||
let mut buffer = Vec::new();
|
||
let _ = stderr.take(4096).read_to_end(&mut buffer);
|
||
String::from_utf8_lossy(&buffer).into_owned()
|
||
});
|
||
|
||
let started = Instant::now();
|
||
let mut output: Option<Vec<u8>> = None;
|
||
let status: Option<ExitStatus> = loop {
|
||
if output.is_none()
|
||
&& let Ok(buffer) = rx.try_recv()
|
||
{
|
||
if buffer.len() > cap {
|
||
let _ = child.kill();
|
||
let _ = child.wait();
|
||
let _ = writer.join();
|
||
let stderr = err_reader.join().unwrap_or_default();
|
||
return ChildRun {
|
||
outcome: ChildOutcome::OutputOverflow,
|
||
stderr,
|
||
};
|
||
}
|
||
output = Some(buffer);
|
||
}
|
||
match child.try_wait() {
|
||
Ok(Some(status)) => break Some(status),
|
||
Ok(None) => {
|
||
if started.elapsed() > self.deadline {
|
||
let _ = child.kill();
|
||
let _ = child.wait();
|
||
break None;
|
||
}
|
||
std::thread::sleep(Duration::from_millis(20));
|
||
}
|
||
Err(error) => {
|
||
let _ = child.kill();
|
||
let _ = child.wait();
|
||
return failed(format!("wait: {error}"));
|
||
}
|
||
}
|
||
};
|
||
let _ = writer.join();
|
||
let output = output.unwrap_or_else(|| rx.recv().unwrap_or_default());
|
||
let stderr = err_reader.join().unwrap_or_default();
|
||
if output.len() > cap {
|
||
return ChildRun {
|
||
outcome: ChildOutcome::OutputOverflow,
|
||
stderr,
|
||
};
|
||
}
|
||
let outcome = match status {
|
||
None => ChildOutcome::Timeout,
|
||
Some(status) => classify(status, output),
|
||
};
|
||
ChildRun { outcome, stderr }
|
||
}
|
||
}
|
||
|
||
fn classify(status: ExitStatus, output: Vec<u8>) -> ChildOutcome {
|
||
match status.code() {
|
||
Some(0) => match webp_check::expect_square(&output, OUTPUT_SIDE) {
|
||
Ok(_) => ChildOutcome::Produced(output),
|
||
Err(issue) => ChildOutcome::Failed(format!("output refused: {issue}")),
|
||
},
|
||
Some(code) if exit_codes::is_content_rejection(code) => ChildOutcome::Rejected {
|
||
code,
|
||
name: exit_codes::name(code),
|
||
},
|
||
Some(code) => ChildOutcome::Broken {
|
||
code,
|
||
name: exit_codes::name(code),
|
||
},
|
||
None => ChildOutcome::Signalled {
|
||
signal: status.signal().unwrap_or(-1),
|
||
},
|
||
}
|
||
}
|
||
|
||
fn failed(detail: String) -> ChildRun {
|
||
ChildRun {
|
||
outcome: ChildOutcome::Failed(detail),
|
||
stderr: String::new(),
|
||
}
|
||
}
|