Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

3.4 KB
Raw Blame
// ©AngelaMos | 2026
// tokenize_differential.rs

use std::process::Command;

use lisdex_build::replay::tokenize;

const SHELL: &str = "/bin/sh";

// The oracle is the shell itself, because a hand-written table of expected splits only ever
// encodes what its author already believed. This one is valid ONLY for inputs whose parsing
// is pure word-splitting and quote removal, so the corpus carries no expansion, no glob and
// no operator: `tokenize` splits the words of a captured compile command and is deliberately
// not a shell. Four divergences were found this way, all four of them real.
fn shell_words(input: &str) -> Option<Vec<String>> {
    let run = Command::new(SHELL)
        .arg("-c")
        .arg(format!("printf '%s\\n' {input}"))
        .output()
        .ok()?;
    if !run.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&run.stdout);
    let mut words: Vec<String> = text.lines().map(str::to_owned).collect();
    // `printf '%s\n'` with no arguments still writes one empty line.
    if words.len() == 1 && words[0].is_empty() {
        words.clear();
    }
    Some(words)
}

const CORPUS: &[&str] = &[
    "cc -c foo.c",
    "cc  -c   foo.c",
    "cc -o 'my file.o' foo.c",
    "cc -o \"my file.o\" foo.c",
    "cc -DNAME=value",
    "cc -DNAME='va lue'",
    "cc -DNAME=\"va lue\"",
    "cc -DP=\"C:\\dir\\file\"",
    "cc -DP='C:\\dir\\file'",
    "cc -DP=C:\\dir\\file",
    "cc -DQ=\"a\\\"b\"",
    "cc -DQ='a\"b'",
    "cc -DR=\"a\\\\b\"",
    "cc -DS=a\\ b",
    "cc ''",
    "cc \"\"",
    "cc a''b",
    "cc a\"\"b",
    "cc 'a'\"b\"c",
    "cc -I/usr/include -I'/opt/my lib/include'",
    "cc\t-c\tfoo.c",
    "cc -c foo.c\n",
    "cc -Wl,-rpath,'/opt/a b'",
    "cc -DT=\"it's\"",
    "cc -DU='say \"hi\"'",
    "cc -DV=\"tab\there\"",
    "cc --param=x=y",
    "cc -DW=\"\\n\"",
    "cc -DX='\\n'",
    "cc -DY=one\u{00a0}two",
    "cc -DZ=\"one\u{00a0}two\"",
];

#[test]
fn tokenize_agrees_with_the_shell_on_every_corpus_command() {
    let mut divergences = Vec::new();
    let mut checked = 0;
    for input in CORPUS {
        let Some(expected) = shell_words(input) else {
            // The shell refused to parse it, so it says nothing about word splitting.
            continue;
        };
        checked += 1;
        match tokenize(input) {
            Some(actual) if actual == expected => {}
            Some(actual) => {
                divergences.push(format!("{input:?}\n  sh={expected:?}\n  us={actual:?}"));
            }
            None => divergences.push(format!("{input:?} refused, but sh accepted it")),
        }
    }
    assert!(
        checked >= CORPUS.len() - 1,
        "the oracle went quiet: only {checked} of {} inputs were graded, so agreement would \
         be vacuous",
        CORPUS.len()
    );
    assert!(
        divergences.is_empty(),
        "tokenize disagrees with {SHELL} on {} of {checked} commands:\n{}",
        divergences.len(),
        divergences.join("\n")
    );
}

#[test]
fn the_shell_also_refuses_the_commands_tokenize_refuses() {
    for input in ["cc -o 'unterminated", "cc -o \"unterminated"] {
        assert_eq!(tokenize(input), None, "{input:?}");
        assert_eq!(
            shell_words(input),
            None,
            "{input:?} must be a syntax error for the shell too, or refusing it is our own \
             invention rather than the shell's rule"
        );
    }
}