Files
faunosteo/crates/worker/src/spawn.rs
T

241 lines
8.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Runs a 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 a `decode` output, never the pixels,
//! and checks only that a `verify` output is a JSON object (V1-2.3).
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 serde_json::{Map, Value};
use crate::limits::{
CHILD_DEADLINE, MAX_OUTPUT_BYTES, OUTPUT_SIDE, VERIFY_DEADLINE, VERIFY_MAX_OUTPUT_BYTES,
};
use crate::{exit_codes, webp_check};
/// The subcommand a spawner runs; the output check depends on it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChildCommand {
Decode,
Verify,
}
impl ChildCommand {
fn arg(self) -> &'static str {
match self {
Self::Decode => "decode",
Self::Verify => "verify",
}
}
}
/// How a child ended.
#[derive(Debug)]
pub enum ChildOutcome {
/// A 512 × 512 WebP whose container passed the parent's check (`decode`), or a JSON
/// object (`verify`).
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 children of one worker binary, one command with its caps.
#[derive(Debug, Clone)]
pub struct Spawner {
pub exe: PathBuf,
pub command: ChildCommand,
pub deadline: Duration,
pub max_output: usize,
pub extra_args: Vec<String>,
}
impl Spawner {
/// A `decode` spawner (the R0 name, kept for its callers).
pub fn new(exe: impl Into<PathBuf>) -> Self {
Self::for_decode(exe)
}
pub fn for_decode(exe: impl Into<PathBuf>) -> Self {
Self {
exe: exe.into(),
command: ChildCommand::Decode,
deadline: CHILD_DEADLINE,
max_output: MAX_OUTPUT_BYTES,
extra_args: Vec::new(),
}
}
pub fn for_verify(exe: impl Into<PathBuf>) -> Self {
Self {
exe: exe.into(),
command: ChildCommand::Verify,
deadline: VERIFY_DEADLINE,
max_output: VERIFY_MAX_OUTPUT_BYTES,
extra_args: Vec::new(),
}
}
/// Runs the spawner's command (`decode` for a spawner built by `new`); the R0 name.
pub fn decode(&self, input: &[u8]) -> ChildRun {
self.run(input)
}
/// Feeds `input` to a fresh child and classifies what comes back.
pub fn run(&self, input: &[u8]) -> ChildRun {
let spawned = Command::new(&self.exe)
.arg(self.command.arg())
.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(self.command, status, output),
};
ChildRun { outcome, stderr }
}
}
fn classify(command: ChildCommand, status: ExitStatus, output: Vec<u8>) -> ChildOutcome {
match status.code() {
Some(0) => check_output(command, output),
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),
},
}
}
/// The output of a child that exited 0: a square WebP container, or a JSON object.
fn check_output(command: ChildCommand, output: Vec<u8>) -> ChildOutcome {
let checked = match command {
ChildCommand::Decode => webp_check::expect_square(&output, OUTPUT_SIDE)
.map(|_| ())
.map_err(|issue| issue.to_string()),
ChildCommand::Verify => serde_json::from_slice::<Map<String, Value>>(&output)
.map(|_| ())
.map_err(|_| "not a JSON object".to_owned()),
};
match checked {
Ok(()) => ChildOutcome::Produced(output),
Err(issue) => ChildOutcome::Failed(format!("output refused: {issue}")),
}
}
fn failed(detail: String) -> ChildRun {
ChildRun {
outcome: ChildOutcome::Failed(detail),
stderr: String::new(),
}
}