use std::path::{Path, PathBuf};
const SPLICES: [&str; 8] = [
"include!",
"include_str!",
"include_bytes!",
"path=\"",
"path=r\"",
"path=r#",
"macro_use",
"externcrate",
];
const LOCAL_MODULES: [&str; 4] = ["error_model", "margin", "oracle", "tests"];
const PRIMITIVES: [&str; 15] = [
"f32", "f64", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128",
"isize", "bool",
];
fn code_of(line: &str) -> &str {
let mut in_str = false;
let mut prev = ' ';
for (i, c) in line.char_indices() {
match c {
'"' if prev != '\\' => in_str = !in_str,
'/' if prev == '/' && !in_str => return &line[..i - 1],
_ => {},
}
prev = c;
}
line
}
fn escapes(src: &str) -> Vec<String> {
let lines: Vec<&str> = src.lines().map(code_of).collect();
let code = lines.join("\n");
let line_at = |i: usize| code[..i].matches('\n').count();
let mut hits = vec![false; lines.len()];
for line in splice_lines(&code) {
hits[line] = true;
}
let mut pos = 0;
while let Some(off) = code[pos..].find("::") {
let i = pos + off;
let head = code[..i].trim_end();
let root_len = head
.chars()
.rev()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.map(char::len_utf8)
.sum::<usize>();
let root = &head[head.len() - root_len..];
let before = head[..head.len() - root_len].trim_end();
let after = code[i + 2..].trim_start();
let bad = match root {
"crate" => !after.starts_with("kernel_oracle::"),
"super" => after.starts_with("super"),
"" => !head.ends_with('>'),
"std" | "core" | "alloc" | "self" | "Self" | "clippy" | "rustfmt" => false,
r => r.starts_with(|c: char| c.is_ascii_lowercase()) && is_crate_root(r, before),
};
if bad {
hits[line_at(head.len() - root_len)] = true;
}
pos = i + 2;
}
lines
.iter()
.zip(hits)
.filter(|(_, hit)| *hit)
.map(|(line, _)| line.trim().to_string())
.collect()
}
fn splice_lines(code: &str) -> Vec<usize> {
let mut squeezed = String::new();
let mut line_of = Vec::new();
let mut line = 0;
for c in code.chars() {
if c == '\n' {
line += 1;
}
if !c.is_whitespace() {
squeezed.push(c);
line_of.extend(std::iter::repeat_n(line, c.len_utf8()));
}
}
let mut found: Vec<usize> = SPLICES
.iter()
.flat_map(|s| squeezed.match_indices(s).map(|(i, _)| line_of[i]))
.collect();
found.sort_unstable();
found.dedup();
found
}
fn is_crate_root(root: &str, before: &str) -> bool {
!(before.ends_with("::")
|| before.ends_with('.')
|| LOCAL_MODULES.contains(&root)
|| PRIMITIVES.contains(&root))
}
fn rust_files(dir: &Path) -> Vec<PathBuf> {
let mut files = Vec::new();
let mut dirs = vec![dir.to_path_buf()];
while let Some(d) = dirs.pop() {
let entries = std::fs::read_dir(&d).unwrap_or_else(|e| panic!("{}: {e}", d.display()));
for entry in entries {
let path = entry.expect("directory entry").path();
if path.is_dir() {
dirs.push(path);
} else if path.extension().is_some_and(|x| x == "rs") {
files.push(path);
}
}
}
files.sort();
files
}
fn escapes_under(dir: &Path) -> Vec<String> {
let mut found = Vec::new();
for file in rust_files(dir) {
let src =
std::fs::read_to_string(&file).unwrap_or_else(|e| panic!("{}: {e}", file.display()));
let name = file
.strip_prefix(dir)
.unwrap_or(&file)
.display()
.to_string();
found.extend(
escapes(&src)
.into_iter()
.map(|line| format!("{name}: {line}")),
);
}
found
}
fn oracle_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("src/kernel_oracle")
}
#[test]
fn oracle_reaches_nothing_outside_itself() {
let files = rust_files(&oracle_dir());
let names: Vec<String> = files
.iter()
.filter_map(|f| f.file_name().map(|n| n.to_string_lossy().into_owned()))
.collect();
for known in ["mod.rs", "error_model.rs", "margin.rs", "oracle.rs"] {
assert!(
names.iter().any(|n| n == known),
"the scan must find {known}: {names:?}"
);
}
let found = escapes_under(&oracle_dir());
assert!(
found.is_empty(),
"KTEST-02 §0.3: the oracle must stay std-only, it reaches out: {found:?}"
);
}
#[test]
fn escape_scan_sees_a_planted_path() {
for planted in [
"use crate::quantize::fused_q4k_parallel_matvec;",
"let y = crate::gguf::GGUFConfig::default();",
"use super::super::kernel_registry;",
"use trueno::Vector;",
"let v = realizar::quantize::dequantize(x);",
"use ::trueno::Vector;",
"let y = crate :: gguf::GGUFConfig::default();",
"let u = \"http://x\"; let v = trueno::Vector::new();",
"extern crate trueno;",
"include!(\"../quantize/fused.rs\");",
"const K: &str = include_str!(\"../gguf/mod.rs\");",
"const B: &[u8] = include_bytes!(\"../gguf/table.bin\");",
"#[path = \"../quantize/mod.rs\"]",
"#[cfg_attr(test, path = \"../gguf/mod.rs\")]",
"#[macro_use]",
"trueno::dispatch!(x);",
"#[path = r\"../quantize/mod.rs\"]",
"#[path = r#\"../quantize/mod.rs\"#]",
] {
assert_eq!(escapes(planted), vec![planted.to_string()], "{planted}");
}
for (split, first) in [
(
"use crate\n ::quantize::fused_q4k_parallel_matvec;",
"use crate",
),
("include\n!(\"../quantize/fused.rs\");", "include"),
(
"const K: &str = include_str\n !(\"../gguf/mod.rs\");",
"const K: &str = include_str",
),
("#[path\n= \"../quantize/mod.rs\"]", "#[path"),
("#[path =\n r\"../quantize/mod.rs\"]", "#[path ="),
("extern\ncrate trueno;", "extern"),
("#[macro\n_use]", "#[macro"),
] {
assert_eq!(escapes(split), vec![first.to_string()], "{split}");
}
let one = "é\ninclude!(\"x\");";
assert_eq!(escapes(one), vec!["include!(\"x\");".to_string()], "{one}");
let many = format!("{}\ninclude!(\nx);\nlet y = 1;", "é".repeat(50));
assert_eq!(escapes(&many), vec!["include!(".to_string()], "{many}");
for clean in [
"use super::error_model::{ErrorModel, Refusal};",
"use crate::kernel_oracle::oracle;",
"use super::*;",
"let n = std::f64::consts::PI + f64::EPSILON;",
"let v: Vec<f64> = x.iter().map(|v| f64::from(*v)).collect();",
"let v = Vec::<f64>::with_capacity(n);",
"let e = ErrorModel::Dot { k, acc: Dtype::F32, out: Dtype::F32, ftz: false };",
"let r = oracle::dot(&a, &b); // crate::gguf in a comment",
"#[allow(clippy::cast_precision_loss)]",
"impl std::fmt::Display for Refusal {",
"fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {",
"impl std::error::Error for Refusal {}",
] {
assert!(escapes(clean).is_empty(), "{clean}: {:?}", escapes(clean));
}
}
#[test]
fn escape_scan_finds_a_new_file() {
let dir = std::env::temp_dir().join(format!("kernel-oracle-scan-{}", std::process::id()));
let nested = dir.join("nested");
std::fs::create_dir_all(&nested).expect("create planted dir");
std::fs::write(dir.join("mod.rs"), "pub mod nested;\n").expect("write planted mod.rs");
std::fs::write(nested.join("fast.rs"), "use trueno::Vector;\n").expect("write planted file");
let found = escapes_under(&dir);
std::fs::remove_dir_all(&dir).expect("remove planted dir");
let want = format!(
"{}: use trueno::Vector;",
Path::new("nested").join("fast.rs").display()
);
assert_eq!(found, vec![want]);
}