//! The verify child (V1-2.3, contract W2): one framed input in (the round, the drawing rows, //! the document, the base, the drawing bytes), the core's verdict JSON out. Same binary, //! same confinement as `decode`; the verdict itself is `faunosteo_core`'s `compute_verdict`. use std::io::{Read, Write}; use faunosteo_core::domain::sheet::verdict::compute_verdict; use crate::decode::TestHooks; use crate::limits::{VERIFY_ADDRESS_SPACE_BYTES, VERIFY_CPU_SECONDS, VERIFY_MAX_INPUT_BYTES}; use crate::verify_frame::{self, FrameError}; use crate::{exit_codes, sandbox}; /// Why the child produced no verdict (one exit code each). #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum VerifyFailure { /// The parent framed the input wrongly: a parent bug, never the content's. Frame(FrameError), /// The verdict could not be serialised: a fault of the child. Encode, } impl VerifyFailure { pub fn exit_code(self) -> i32 { match self { Self::Frame(_) => exit_codes::FRAME_INVALID, Self::Encode => exit_codes::INTERNAL, } } } /// Entry of `faunosteo-worker verify`: returns the process exit code. /// /// Same order as `decode::main`: the environment check and the sandbox come before the /// first byte of input is read. A failed verdict is exit 0: it is content, judged by the core. pub fn main( args: &[String], stdin: &mut dyn Read, stdout: &mut dyn Write, stderr: &mut dyn Write, ) -> i32 { if std::env::vars_os().next().is_some() { let _ = writeln!(stderr, "verify: the environment must be empty"); return exit_codes::ENV_NOT_EMPTY; } let hooks = match TestHooks::parse(args) { Ok(hooks) => hooks, Err(message) => { let _ = writeln!(stderr, "verify: {message}"); return exit_codes::USAGE; } }; let limits = sandbox::Limits { address_space_bytes: VERIFY_ADDRESS_SPACE_BYTES, cpu_seconds: VERIFY_CPU_SECONDS, }; if let Err(error) = sandbox::apply(&limits, hooks.seccomp) { let _ = writeln!(stderr, "verify: sandbox: {error}"); return exit_codes::SANDBOX_FAILED; } if let Some(code) = hooks.run_after_sandbox(stdout, stderr) { return code; } let mut input = Vec::new(); let read = stdin .take(VERIFY_MAX_INPUT_BYTES as u64 + 1) .read_to_end(&mut input); if read.is_err() { return exit_codes::INTERNAL; } if input.len() > VERIFY_MAX_INPUT_BYTES { let _ = writeln!(stderr, "verify: input over {VERIFY_MAX_INPUT_BYTES} bytes"); return exit_codes::INPUT_TOO_LARGE; } match process(&input) { Ok(verdict) => match stdout.write_all(&verdict).and_then(|()| stdout.flush()) { Ok(()) => exit_codes::OK, Err(_) => exit_codes::OUTPUT_FAILED, }, Err(failure) => { let _ = match failure { VerifyFailure::Frame(error) => writeln!(stderr, "verify: frame: {error}"), VerifyFailure::Encode => writeln!(stderr, "verify: verdict not serialised"), }; failure.exit_code() } } } /// Reads one frame and returns the verdict JSON; pure function of the bytes. pub fn process(frame: &[u8]) -> Result, VerifyFailure> { let frame = verify_frame::decode(frame).map_err(VerifyFailure::Frame)?; let verdict = compute_verdict(&frame.input()); serde_json::to_vec(&verdict).map_err(|_| VerifyFailure::Encode) }