sim-lib-exec 0.1.0

Capability-gated bounded process execution for SIM.
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use std::{
    io::{self, Read, Write},
    path::PathBuf,
    process::{Child, Command, ExitStatus, Stdio},
    sync::{
        Arc, Mutex,
        mpsc::{self, Receiver, RecvTimeoutError, Sender},
    },
    thread,
    time::{Duration, Instant},
};

use sim_kernel::{CapabilityName, Cx, Error, Expr, NumberLiteral, Result, Symbol};

/// Returns the capability required by [`exec`].
pub fn exec_capability() -> CapabilityName {
    CapabilityName::new("exec")
}

/// Returns the constructor symbol used by [`ProcResult::to_constructor_expr`].
pub fn proc_result_symbol() -> Symbol {
    Symbol::new("ProcResult")
}

/// Bounded process execution options.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExecOptions {
    /// Directory the child runs in.
    ///
    /// When set, the directory must canonicalize inside [`root`](Self::root).
    pub cwd: Option<PathBuf>,
    /// Root that confines [`cwd`](Self::cwd).
    ///
    /// When `cwd` is set and this is absent, the current process directory is
    /// the confinement root. When this is set and `cwd` is absent, the child
    /// runs in this root directory.
    pub root: Option<PathBuf>,
    /// Mandatory timeout in milliseconds. Zero is rejected before spawning.
    pub timeout_ms: u64,
    /// Maximum total captured stdout plus stderr bytes.
    pub max_output_bytes: usize,
    /// Optional stdin bytes written to the child.
    pub stdin: Option<Vec<u8>>,
}

impl ExecOptions {
    /// Builds process options with a timeout and output cap.
    pub fn new(timeout_ms: u64, max_output_bytes: usize) -> Self {
        Self {
            cwd: None,
            root: None,
            timeout_ms,
            max_output_bytes,
            stdin: None,
        }
    }

    /// Returns options with a confined working directory.
    pub fn with_cwd(mut self, cwd: impl Into<PathBuf>, root: impl Into<PathBuf>) -> Self {
        self.cwd = Some(cwd.into());
        self.root = Some(root.into());
        self
    }

    /// Returns options with stdin bytes.
    pub fn with_stdin(mut self, stdin: impl Into<Vec<u8>>) -> Self {
        self.stdin = Some(stdin.into());
        self
    }
}

/// Result of a bounded process run.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProcResult {
    /// Captured stdout, lossily decoded as UTF-8 after byte capping.
    pub stdout: String,
    /// Captured stderr, lossily decoded as UTF-8 after byte capping.
    pub stderr: String,
    /// Process exit code, or `-1` when the host reports no numeric code.
    pub exit_code: i32,
    /// Whether stdout or stderr exceeded the shared output byte cap.
    pub truncated: bool,
}

impl ProcResult {
    /// Encodes this result as `#(ProcResult stdout stderr exit_code truncated)`.
    pub fn to_constructor_expr(&self) -> Expr {
        Expr::Call {
            operator: Box::new(Expr::Symbol(proc_result_symbol())),
            args: vec![
                Expr::String(self.stdout.clone()),
                Expr::String(self.stderr.clone()),
                Expr::Number(NumberLiteral {
                    domain: Symbol::qualified("numbers", "i64"),
                    canonical: self.exit_code.to_string(),
                }),
                Expr::Bool(self.truncated),
            ],
        }
    }
}

/// Runs one host process with explicit argv and bounded output.
///
/// The caller must hold [`exec_capability`]. The argv list must be non-empty;
/// the first element is the program and the remaining elements are passed
/// verbatim as arguments. No shell is inserted by this function.
pub fn exec(cx: &mut Cx, argv: &[String], options: &ExecOptions) -> Result<ProcResult> {
    cx.require(&exec_capability())?;
    validate_request(argv, options)?;

    let mut command = Command::new(&argv[0]);
    command.args(&argv[1..]);
    command.stdin(Stdio::piped());
    command.stdout(Stdio::piped());
    command.stderr(Stdio::piped());
    if let Some(cwd) = confined_cwd(options)? {
        command.current_dir(cwd);
    }

    let mut child = command
        .spawn()
        .map_err(|err| Error::HostError(format!("exec spawn {}: {err}", argv[0])))?;
    run_child(&mut child, options)
}

fn validate_request(argv: &[String], options: &ExecOptions) -> Result<()> {
    if argv.is_empty() {
        return Err(Error::Eval(
            "exec requires a non-empty argv list".to_owned(),
        ));
    }
    if options.timeout_ms == 0 {
        return Err(Error::Eval(
            "exec requires a non-zero timeout_ms".to_owned(),
        ));
    }
    Ok(())
}

fn confined_cwd(options: &ExecOptions) -> Result<Option<PathBuf>> {
    if options.cwd.is_none() && options.root.is_none() {
        return Ok(None);
    }

    let root = match &options.root {
        Some(root) => root.clone(),
        None => std::env::current_dir()
            .map_err(|err| Error::HostError(format!("exec current dir: {err}")))?,
    };
    let cwd = options.cwd.clone().unwrap_or_else(|| root.clone());
    let root = canonicalize_path(root, "exec root")?;
    let cwd = canonicalize_path(cwd, "exec cwd")?;
    if !cwd.starts_with(&root) {
        return Err(Error::HostError(format!(
            "exec cwd {} escapes root {}",
            cwd.display(),
            root.display()
        )));
    }
    Ok(Some(cwd))
}

fn canonicalize_path(path: PathBuf, label: &'static str) -> Result<PathBuf> {
    path.canonicalize()
        .map_err(|err| Error::HostError(format!("{label} {}: {err}", path.display())))
}

fn run_child(child: &mut Child, options: &ExecOptions) -> Result<ProcResult> {
    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| Error::HostError("exec stdout pipe missing".to_owned()))?;
    let stderr = child
        .stderr
        .take()
        .ok_or_else(|| Error::HostError("exec stderr pipe missing".to_owned()))?;
    let stdin = child.stdin.take();

    let budget = Arc::new(Mutex::new(CaptureBudget::new(options.max_output_bytes)));
    let deadline = Instant::now()
        .checked_add(Duration::from_millis(options.timeout_ms))
        .ok_or_else(|| Error::Eval("exec timeout is too large".to_owned()))?;

    let (tx, rx) = mpsc::channel();
    spawn_reader(
        stdout,
        Arc::clone(&budget),
        CaptureStream::Stdout,
        tx.clone(),
    );
    spawn_reader(
        stderr,
        Arc::clone(&budget),
        CaptureStream::Stderr,
        tx.clone(),
    );
    let stdin_pending = if let Some(stdin) = stdin {
        spawn_writer(stdin, options.stdin.clone(), tx.clone());
        true
    } else {
        false
    };
    drop(tx);

    let completion = wait_for_completion(child, &rx, deadline, stdin_pending);
    match completion {
        Ok(ChildCompletion {
            status,
            stdout,
            stderr,
            stdin,
        }) => {
            stdin?;
            let stdout = stdout?;
            let stderr = stderr?;
            let truncated = budget
                .lock()
                .map_err(|_| Error::PoisonedLock("exec output budget"))?
                .truncated;
            Ok(ProcResult {
                stdout: String::from_utf8_lossy(&stdout).into_owned(),
                stderr: String::from_utf8_lossy(&stderr).into_owned(),
                exit_code: exit_code(status),
                truncated,
            })
        }
        Err(WaitError::Timeout { child_exited }) => {
            Err(timeout_error(child, options.timeout_ms, child_exited))
        }
        Err(WaitError::Host(err)) => Err(err),
    }
}

fn spawn_reader<R>(
    reader: R,
    budget: Arc<Mutex<CaptureBudget>>,
    stream: CaptureStream,
    tx: Sender<ChildEvent>,
) where
    R: Read + Send + 'static,
{
    thread::spawn(move || {
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            read_capped(reader, budget, stream.name())
        }))
        .unwrap_or_else(|_| {
            Err(Error::HostError(format!(
                "exec {} thread panicked",
                stream.name()
            )))
        });
        let _ = tx.send(ChildEvent::Capture { stream, result });
    });
}

fn spawn_writer(
    mut stdin: std::process::ChildStdin,
    input: Option<Vec<u8>>,
    tx: Sender<ChildEvent>,
) {
    thread::spawn(move || {
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            write_stdin(&mut stdin, input)
        }))
        .unwrap_or_else(|_| Err(Error::HostError("exec stdin thread panicked".to_owned())));
        let _ = tx.send(ChildEvent::Stdin(result));
    });
}

fn write_stdin(stdin: &mut std::process::ChildStdin, input: Option<Vec<u8>>) -> Result<()> {
    let Some(input) = input else {
        return Ok(());
    };
    match stdin.write_all(&input) {
        Ok(()) => Ok(()),
        Err(err) if err.kind() == io::ErrorKind::BrokenPipe => Ok(()),
        Err(err) => Err(Error::HostError(format!("exec stdin write: {err}"))),
    }
}

fn wait_for_completion(
    child: &mut Child,
    rx: &Receiver<ChildEvent>,
    deadline: Instant,
    stdin_pending: bool,
) -> std::result::Result<ChildCompletion, WaitError> {
    let mut status = None;
    let mut stdout = None;
    let mut stderr = None;
    let mut stdin = if stdin_pending { None } else { Some(Ok(())) };

    loop {
        poll_child_status(child, &mut status)?;
        drain_child_events(rx, &mut stdout, &mut stderr, &mut stdin)?;
        if status.is_some() && stdout.is_some() && stderr.is_some() && stdin.is_some() {
            return Ok(ChildCompletion {
                status: status.take().expect("status checked above"),
                stdout: stdout.take().expect("stdout checked above"),
                stderr: stderr.take().expect("stderr checked above"),
                stdin: stdin.take().expect("stdin checked above"),
            });
        }

        let now = Instant::now();
        if now >= deadline {
            return Err(WaitError::Timeout {
                child_exited: status.is_some(),
            });
        }

        match rx.recv_timeout((deadline - now).min(Duration::from_millis(10))) {
            Ok(event) => record_child_event(event, &mut stdout, &mut stderr, &mut stdin),
            Err(RecvTimeoutError::Timeout) => {}
            Err(RecvTimeoutError::Disconnected) if status.is_some() => {
                return Err(WaitError::Host(Error::HostError(
                    "exec capture thread ended without result".to_owned(),
                )));
            }
            Err(RecvTimeoutError::Disconnected) => {
                thread::sleep((deadline - now).min(Duration::from_millis(10)));
            }
        }
    }
}

fn poll_child_status(
    child: &mut Child,
    status: &mut Option<ExitStatus>,
) -> std::result::Result<(), WaitError> {
    if status.is_some() {
        return Ok(());
    }
    *status = child
        .try_wait()
        .map_err(|err| WaitError::Host(Error::HostError(format!("exec wait: {err}"))))?;
    Ok(())
}

fn drain_child_events(
    rx: &Receiver<ChildEvent>,
    stdout: &mut Option<Result<Vec<u8>>>,
    stderr: &mut Option<Result<Vec<u8>>>,
    stdin: &mut Option<Result<()>>,
) -> std::result::Result<(), WaitError> {
    loop {
        match rx.try_recv() {
            Ok(event) => record_child_event(event, stdout, stderr, stdin),
            Err(mpsc::TryRecvError::Empty) => return Ok(()),
            Err(mpsc::TryRecvError::Disconnected) => return Ok(()),
        }
    }
}

fn record_child_event(
    event: ChildEvent,
    stdout: &mut Option<Result<Vec<u8>>>,
    stderr: &mut Option<Result<Vec<u8>>>,
    stdin: &mut Option<Result<()>>,
) {
    match event {
        ChildEvent::Capture {
            stream: CaptureStream::Stdout,
            result,
        } => *stdout = Some(result),
        ChildEvent::Capture {
            stream: CaptureStream::Stderr,
            result,
        } => *stderr = Some(result),
        ChildEvent::Stdin(result) => *stdin = Some(result),
    }
}

fn timeout_error(child: &mut Child, timeout_ms: u64, child_exited: bool) -> Error {
    let kill_result = if child_exited { Ok(()) } else { child.kill() };
    let wait_result = child.wait();
    let mut message = format!("exec timed out after {timeout_ms} ms");
    if let Err(err) = kill_result {
        message.push_str(&format!("; kill failed: {err}"));
    }
    if let Err(err) = wait_result {
        message.push_str(&format!("; wait failed: {err}"));
    }
    Error::HostError(message)
}

struct ChildCompletion {
    status: ExitStatus,
    stdout: Result<Vec<u8>>,
    stderr: Result<Vec<u8>>,
    stdin: Result<()>,
}

enum WaitError {
    Timeout { child_exited: bool },
    Host(Error),
}

#[derive(Clone, Copy)]
enum CaptureStream {
    Stdout,
    Stderr,
}

impl CaptureStream {
    fn name(self) -> &'static str {
        match self {
            Self::Stdout => "stdout",
            Self::Stderr => "stderr",
        }
    }
}

enum ChildEvent {
    Capture {
        stream: CaptureStream,
        result: Result<Vec<u8>>,
    },
    Stdin(Result<()>),
}

fn read_capped<R>(
    mut reader: R,
    budget: Arc<Mutex<CaptureBudget>>,
    name: &'static str,
) -> Result<Vec<u8>>
where
    R: Read,
{
    let mut captured = Vec::new();
    let mut chunk = [0_u8; 4096];
    loop {
        let read = reader
            .read(&mut chunk)
            .map_err(|err| Error::HostError(format!("exec read {name}: {err}")))?;
        if read == 0 {
            return Ok(captured);
        }
        let keep = {
            let mut budget = budget
                .lock()
                .map_err(|_| Error::PoisonedLock("exec output budget"))?;
            budget.claim(read)
        };
        captured.extend_from_slice(&chunk[..keep]);
    }
}

fn exit_code(status: ExitStatus) -> i32 {
    status.code().unwrap_or(-1)
}

#[derive(Debug)]
struct CaptureBudget {
    remaining: usize,
    truncated: bool,
}

impl CaptureBudget {
    fn new(max_output_bytes: usize) -> Self {
        Self {
            remaining: max_output_bytes,
            truncated: false,
        }
    }

    fn claim(&mut self, read: usize) -> usize {
        let keep = read.min(self.remaining);
        self.remaining -= keep;
        if keep < read {
            self.truncated = true;
        }
        keep
    }
}