diff --git a/CLAUDE.md b/CLAUDE.md index 3c1753e2..8c9d5a07 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -16,7 +16,16 @@ to trust the agents. Read `docs/VERIFICATION.md` and `docs/ROADMAP.md` first. a `// SAFETY:` comment directly above it citing the man page and the `libc` declaration (on Windows, the Microsoft Learn page, written `docs `, and the hand-written declaration); there is no raw pointer arithmetic and every `pub fn` - is safe. It makes no policy decisions: it takes data that safe, verified + is safe. The one exception is the **code entry** (jit.md J.0(a), + `xtask/src/sys.rs` rules 10 and 11): in `src/enter.rs`, once per ABI, a + block that calls the code region's fixed enter stub, as the whole body + of a private `run` that only `enter` calls. `enter` takes a `Validated` + entry, which only `clazel-codecheck`'s validator is meant to build, and + only `clazel-codecheck` and `clazel-jit` may name the code-region API. + Its `// SAFETY:` comment cites the W^X man page (or `docs `) + and the ABI document. The checker enforces the shape; the audit checks + the stub's bytes and that the entry is checked against the region. + It makes no policy decisions: it takes data that safe, verified code built and hands it to the OS. `cargo xtask check` enforces all of this, and the README states the measured `unsafe` count. **Every change to `clazel-sys` is audited** by a different LLM session diff --git a/xtask/src/sys.rs b/xtask/src/sys.rs index f94d2f93..abd09bbc 100644 --- a/xtask/src/sys.rs +++ b/xtask/src/sys.rs @@ -72,7 +72,16 @@ //! 11. only the crates of [`CODE_USERS`] (`clazel-codecheck`, `clazel-jit`) may //! name the code-region API of `clazel-sys` ([`CODE_NAMES`] and `enter`), so //! only they can build a `Validated` entry, write code or run it -//! ([`code_use_violations`]). +//! ([`code_use_violations`]). The names unique to the API are refused in +//! every file; `Validated` and `enter`, which other crates also define, in +//! every file that names `clazel_sys` (raw identifiers included); and the +//! ways of hiding the crate from that check (an alias, a `pub use` or `pub +//! extern crate`, a brace-wrapped or macro-passed name, a re-exported +//! platform module, any path through a `code` submodule) are refused. +//! Rule 10 also requires exactly one `fn enter` in the code-entry file, at +//! module level, whose one call of `run` passes `stub, raw, entry, +//! region.len()` bound by the four statements the checker pins +//! ([`check_enter_fn`]). //! //! And the README's memory-safety sentence states the measured `unsafe` count //! ([`readme_problems`]). @@ -434,9 +443,28 @@ pub const CODE_ENTRY_FILES: &[&str] = &["crates/clazel-sys/src/enter.rs"]; /// Neither exists as a user yet. pub const CODE_USERS: &[&str] = &["clazel-codecheck", "clazel-jit"]; -/// The types of the code-region API (`enter` is the function; see +/// The names of the code-region API of `clazel-sys` that no other crate uses, +/// refused in every file of a crate that is not in [`CODE_USERS`] (see /// [`code_use_violations`]). -const CODE_NAMES: &[&str] = &["Validated", "CodeWriter", "CodeRegion", "SharedCode"]; +const CODE_NAMES: &[&str] = &[ + "CodeWriter", + "CodeRegion", + "SharedCode", + "CodeError", + "RemoteView", + "CODE_START", + "CONTEXT_BYTES", + "CONTEXT_WORDS", + "EXIT_CODE_WORD", +]; + +/// The names of the API that other crates define for themselves (`Validated` +/// is `clazel-x509`'s and `clazel-crypto`'s own type; `enter` is a common +/// function name), judged only in a file that names `clazel_sys`. +const SHARED_CODE_NAMES: &[&str] = &["Validated"]; + +/// The platform modules of `clazel-sys`, each with a `code` submodule. +const PLATFORM_MODULES: &[&str] = &["macos", "linux", "windows"]; /// The ABIs a code entry may be written with: x86-64 System V, x86-64 Windows, /// and AArch64 (`"C"`, which is AAPCS64 there). The enter stub of @@ -867,6 +895,7 @@ fn check_with( walk(&tokens, None, &lines, &mut file); if file.code_entry_allowed { check_run_use(&tokens, false, &mut file); + check_enter_fn(&tokens, &mut file); } file.violations.sort(); Ok(file) @@ -1362,6 +1391,222 @@ fn count_run_calls(tokens: &[TokenTree]) -> usize { .fold(0usize, usize::saturating_add) } +/// The statements of `fn enter` that make `run`'s arguments what rule 10 says +/// they are (whitespace removed): the base address of the region, the entry +/// checked against the region and then added to that base, the context as a +/// byte pointer, and the entry's offset. +const ENTER_LETS: &[&str] = &[ + "letoffset=entry.from_start();", + "letstub=region.base();", + "letentry=stub.checked_add(offset).ok_or(CodeError::OutOfRange)?;", + "letraw:*mutu8=std::ptr::from_mut(&mut*ctx).cast();", +]; + +/// What `fn enter` passes to `run`: the region's base, the context, the checked +/// entry and the region's length. +const ENTER_RUN_ARGS: &str = "stub,raw,entry,region.len()"; + +/// Rule 10, the other half: in a code-entry file there is exactly one +/// `fn enter`, at module level (not in a nested module, `impl` or function), with +/// the fixed parameters, and its body binds exactly the four names of +/// [`ENTER_LETS`], so the one call of `run` in it passes +/// `stub, raw, entry, region.len()` with the meanings those statements give, +/// and nothing rebinds them (no other `let`, `for`, `match`, loop or closure). +fn check_enter_fn(tokens: &[TokenTree], file: &mut File) { + let mut all = Vec::new(); + collect_fn_enters(tokens, 0, &mut all); + let top: Vec<&(usize, usize, Vec)> = + all.iter().filter(|(depth, _, _)| *depth == 0).collect(); + if all.len() != 1 || top.len() != 1 { + let line = all.first().map_or(1, |(_, l, _)| *l); + flag( + file, + line, + format!( + "a code-entry file must have exactly one `fn enter`, at module level; found {} \ + ({} at module level)", + all.len(), + top.len() + ), + ); + return; + } + let Some((_, line, rest)) = top.first().copied() else { + return; + }; + let squash = |ts: TokenStream| -> String { + ts.to_string() + .chars() + .filter(|c| !c.is_whitespace()) + .collect() + }; + let (params, body) = match ( + rest.first(), + rest.iter().find_map(|t| match t { + TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => Some(g), + _ => None, + }), + ) { + (Some(TokenTree::Group(p)), Some(b)) if p.delimiter() == Delimiter::Parenthesis => { + (squash(p.stream()), b) + } + _ => { + flag(file, *line, "`fn enter` must have parameters and a body"); + return; + } + }; + if params != "region:&CodeRegion,entry:Validated,ctx:&mutContext" { + flag( + file, + *line, + "`fn enter` must take exactly `region: &CodeRegion, entry: Validated, ctx: &mut Context`", + ); + } + let body_text = squash(body.stream()); + for stmt in ENTER_LETS { + if body_text.matches(stmt).count() != 1 { + flag( + file, + *line, + format!( + "`fn enter` must contain exactly one statement `{stmt}` (whitespace aside)" + ), + ); + } + } + let body_tokens: Vec = body.stream().into_iter().collect(); + let lets = count_idents(&body_tokens, &["let"]); + if lets != ENTER_LETS.len() { + flag( + file, + *line, + format!( + "`fn enter` must have exactly {} `let` statements, found {lets}", + ENTER_LETS.len() + ), + ); + } + if count_idents(&body_tokens, &["for", "while", "loop", "match", "move"]) != 0 + || count_lone_pipes(&body_tokens) != 0 + { + flag( + file, + *line, + "`fn enter` must not contain a loop, `match` or closure that could rebind its names", + ); + } + let mut args = Vec::new(); + run_call_args(&body_tokens, &mut args); + if args.len() != 1 || args.first().map(String::as_str) != Some(ENTER_RUN_ARGS) { + flag( + file, + *line, + format!("`fn enter` must call `run({ENTER_RUN_ARGS})` and only that"), + ); + } +} + +/// Every `fn enter` in `tokens` at any depth: (depth, line, the tokens after +/// the name). +fn collect_fn_enters( + tokens: &[TokenTree], + depth: usize, + out: &mut Vec<(usize, usize, Vec)>, +) { + for (i, tt) in tokens.iter().enumerate() { + match tt { + TokenTree::Ident(id) + if *id == "enter" + && ident_is(i.checked_sub(1).and_then(|j| tokens.get(j)), "fn") => + { + // The signature and the body: up to the first brace group. + let mut rest: Vec = Vec::new(); + for t in tokens.iter().skip(i.saturating_add(1)) { + rest.push(t.clone()); + if matches!(t, TokenTree::Group(g) if g.delimiter() == Delimiter::Brace) { + break; + } + } + out.push((depth, line_of(tt), rest)); + } + TokenTree::Group(g) => { + let body: Vec = g.stream().into_iter().collect(); + collect_fn_enters(&body, depth.saturating_add(1), out); + } + _ => {} + } + } +} + +/// How many of the identifiers `names` occur in `tokens`, at any depth. +fn count_idents(tokens: &[TokenTree], names: &[&str]) -> usize { + tokens + .iter() + .map(|tt| match tt { + TokenTree::Ident(id) if names.iter().any(|n| *id == n) => 1usize, + TokenTree::Group(g) => count_idents(&g.stream().into_iter().collect::>(), names), + _ => 0, + }) + .fold(0usize, usize::saturating_add) +} + +/// How many single `|` tokens (a closure's parameter bars, or a bit-or; not the +/// `||` of a condition) occur in `tokens`, at any depth. +fn count_lone_pipes(tokens: &[TokenTree]) -> usize { + let mut n = 0usize; + for (i, tt) in tokens.iter().enumerate() { + match tt { + TokenTree::Punct(p) if p.as_char() == '|' => { + let joined_before = matches!( + i.checked_sub(1).and_then(|j| tokens.get(j)), + Some(TokenTree::Punct(q)) + if q.as_char() == '|' && q.spacing() == proc_macro2::Spacing::Joint + ); + let joined_after = p.spacing() == proc_macro2::Spacing::Joint + && is_punct(tokens.get(i.saturating_add(1)), '|'); + if !joined_before && !joined_after { + n = n.saturating_add(1); + } + } + TokenTree::Group(g) => { + n = n.saturating_add(count_lone_pipes( + &g.stream().into_iter().collect::>(), + )); + } + _ => {} + } + } + n +} + +/// The argument lists (whitespace removed, a trailing comma dropped) of each +/// `run(..)` call in `tokens`, at any depth. +fn run_call_args(tokens: &[TokenTree], out: &mut Vec) { + for (i, tt) in tokens.iter().enumerate() { + match tt { + TokenTree::Ident(id) if *id == "run" => { + if let Some(TokenTree::Group(g)) = tokens.get(i.saturating_add(1)) + && g.delimiter() == Delimiter::Parenthesis + { + let text: String = g + .stream() + .to_string() + .chars() + .filter(|c| !c.is_whitespace()) + .collect(); + out.push(text.strip_suffix(',').unwrap_or(&text).to_owned()); + } else { + out.push(String::new()); + } + } + TokenTree::Group(g) => { + run_call_args(&g.stream().into_iter().collect::>(), out); + } + _ => {} + } + } +} + /// In a code-entry file, `fn run` at `k` must be exactly /// `fn run(stub: usize, ctx: *mut u8, entry: usize, len: usize) { .. }`: no /// return type, so the four names the code entry uses are its parameters. @@ -1408,33 +1653,88 @@ fn check_run_signature(tokens: &[TokenTree], k: usize, file: &mut File) { } } +/// An identifier's name with a raw prefix removed: `r#clazel_sys` is +/// `clazel_sys`. Keywords are compared with `to_string`, which keeps `r#use` +/// from being the keyword. +fn ident_name(id: &proc_macro2::Ident) -> String { + let s = id.to_string(); + s.strip_prefix("r#") + .map_or_else(|| s.clone(), str::to_owned) +} + +/// What rule 11 knows about the file it is judging. +struct CodeUse { + /// `clazel_sys` and the names it is imported as. + names: Vec, + /// Whether the file names `clazel_sys` (raw identifier or not): only + /// then are the names other crates also use ([`SHARED_CODE_NAMES`], and + /// `enter`) judged. + mentions: bool, +} + +/// What a `use` statement, and the macro or group around it, is so far. +#[derive(Clone, Copy)] +struct UseState { + /// A `use` statement is open. + in_use: bool, + /// It starts at `clazel_sys` or an alias. + sys_use: bool, + /// It has a visibility: it re-exports. + pub_use: bool, + /// The tokens are the arguments or body of a macro. + in_macro: bool, +} + /// Rule 11: in a source file of a crate that is not in [`CODE_USERS`], the -/// violations as (1-based line, what). A file is judged only if it names -/// `clazel_sys`. It must not name a type of [`CODE_NAMES`]; name `enter` -/// through a path that starts at `clazel_sys` or at a name it was imported as -/// (`use clazel_sys as sys;`, then `sys::enter`); import `enter` in a `use` -/// statement that starts there; or glob import `clazel_sys` (`use -/// clazel_sys::*`), which would bring `enter` in by a bare name. Another -/// crate's `enter` (`clazel_renderer::sandbox::enter`) is not the API. +/// violations as (1-based line, what). +/// +/// - The names unique to `clazel-sys` ([`CODE_NAMES`]) are refused in **every** +/// file, whether or not it names `clazel_sys`, so a file that reaches the +/// API through a path made elsewhere is judged too. +/// - A file that names `clazel_sys` (raw identifiers compared with `r#` +/// removed) must not name `Validated` or `enter` either: `enter` through a +/// path that starts at `clazel_sys`, an import of it from there, or a glob +/// import of `clazel_sys` (`use clazel_sys::*`), which would bring it in by +/// a bare name. Another crate's `enter` (`clazel_renderer::sandbox::enter`) +/// and `Validated` (`clazel-x509`) are not the API, so a file that never +/// names `clazel_sys` may use them. +/// - Nothing hands the crate on under another name, which would hide a path +/// from the check: no `as` alias, no `pub use`, no `pub extern crate`, no +/// `clazel_sys` that is not the first segment of a `use` path (a brace +/// wrapper), none passed to a macro as a bare name, no `use $x`. +/// - The code-region submodules (`code`) of the platform modules are not +/// named from the crate's root, and a platform module (`macos`, `linux`, +/// `windows`) is not aliased or re-exported, so `use clazel_sys::macos::code +/// as m` and `pub use clazel_sys::macos as p` are refused. A submodule that +/// is not code (`clazel_sys::macos::window::{self as sys}`) may be aliased. pub fn code_use_violations(source: &str) -> Result, String> { let stream: TokenStream = source .parse() .map_err(|e: proc_macro2::LexError| format!("cannot tokenize: {e}"))?; let tokens: Vec = stream.into_iter().collect(); let mut out = Vec::new(); - if mentions_ident(&tokens, "clazel_sys") { - let mut names = vec!["clazel_sys".to_owned()]; - collect_sys_aliases(&tokens, &mut names); - scan_code_use(&tokens, (false, false, false), &names, &mut out); - } + let mut names = vec!["clazel_sys".to_owned()]; + collect_sys_aliases(&tokens, &mut names); + let ctx = CodeUse { + names, + mentions: mentions_ident(&tokens, "clazel_sys"), + }; + let state = UseState { + in_use: false, + sys_use: false, + pub_use: false, + in_macro: false, + }; + scan_code_use(&tokens, state, &ctx, &mut out); out.sort(); + out.dedup(); Ok(out) } -/// Whether the identifier `name` occurs anywhere in `tokens`. +/// Whether the identifier `name` (raw prefix removed) occurs anywhere in `tokens`. fn mentions_ident(tokens: &[TokenTree], name: &str) -> bool { tokens.iter().any(|tt| match tt { - TokenTree::Ident(i) => *i == name, + TokenTree::Ident(i) => ident_name(i) == name, TokenTree::Group(g) => mentions_ident(&g.stream().into_iter().collect::>(), name), _ => false, }) @@ -1445,14 +1745,36 @@ fn collect_sys_aliases(tokens: &[TokenTree], names: &mut Vec) { for (i, tt) in tokens.iter().enumerate() { match tt { TokenTree::Ident(id) - if *id == "clazel_sys" + if ident_name(id) == "clazel_sys" && ident_is(tokens.get(i.saturating_add(1)), "as") && let Some(TokenTree::Ident(alias)) = tokens.get(i.saturating_add(2)) => { - names.push(alias.to_string()); + names.push(ident_name(alias)); + } + // A private alias of a platform module (`clazel_sys::macos as os`) is a + // name for the crate's code too: paths from it are judged. + TokenTree::Ident(id) + if PLATFORM_MODULES.contains(&ident_name(id).as_str()) + && ident_is(tokens.get(i.saturating_add(1)), "as") + && let Some(TokenTree::Ident(alias)) = tokens.get(i.saturating_add(2)) => + { + names.push(ident_name(alias)); } TokenTree::Group(g) => { let body: Vec = g.stream().into_iter().collect(); + // `clazel_sys::macos::{self as os}`. + if g.delimiter() == Delimiter::Brace + && is_platform(i.checked_sub(3).and_then(|j| tokens.get(j))) + { + for (j, t) in body.iter().enumerate() { + if ident_is(Some(t), "self") + && ident_is(body.get(j.saturating_add(1)), "as") + && let Some(TokenTree::Ident(alias)) = body.get(j.saturating_add(2)) + { + names.push(ident_name(alias)); + } + } + } collect_sys_aliases(&body, names); } _ => {} @@ -1473,7 +1795,7 @@ fn path_starts_at(tokens: &[TokenTree], i: usize, names: &[String]) -> bool { { match at.checked_sub(3).and_then(|j| tokens.get(j)) { Some(TokenTree::Ident(seg)) => { - root = Some(seg.to_string()); + root = Some(ident_name(seg)); at = at.saturating_sub(3); } _ => break, @@ -1482,41 +1804,62 @@ fn path_starts_at(tokens: &[TokenTree], i: usize, names: &[String]) -> bool { root.is_some_and(|r| names.contains(&r)) } -/// [`code_use_violations`]'s walk. `state` is (a `use` statement is open, it -/// starts at `clazel_sys` or an alias, it has a visibility: it re-exports); -/// `names` is `clazel_sys` and its aliases. +/// Whether `tt` is a platform module of `clazel-sys` (an identifier). +fn is_platform(tt: Option<&TokenTree>) -> bool { + matches!(tt, Some(TokenTree::Ident(p)) if PLATFORM_MODULES.contains(&ident_name(p).as_str())) +} + +/// `pub`, `pub(crate)` and the like, ending at the token `n` back from `i` +/// (`n` counts the visibility's last token). +fn visibility_ends_at(tokens: &[TokenTree], i: usize, n: usize) -> bool { + let at = |k: usize| i.checked_sub(k).and_then(|j| tokens.get(j)); + ident_is(at(n), "pub") + || (matches!(at(n), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis) + && ident_is(at(n.saturating_add(1)), "pub")) +} + +/// [`code_use_violations`]'s walk. fn scan_code_use( tokens: &[TokenTree], - state: (bool, bool, bool), - names: &[String], + state: UseState, + ctx: &CodeUse, out: &mut Vec<(usize, String)>, ) { - let (mut in_use, mut sys_use, mut pub_use) = state; + let UseState { + mut in_use, + mut sys_use, + mut pub_use, + in_macro, + } = state; + let names = &ctx.names; for (i, tt) in tokens.iter().enumerate() { let before = |n: usize| i.checked_sub(n).and_then(|j| tokens.get(j)); + let next = tokens.get(i.saturating_add(1)); match tt { TokenTree::Ident(id) => { - let name = id.to_string(); - if name == "use" { + // Keywords are compared as written; everything else with `r#` removed. + let name = ident_name(id); + if *id == "use" { in_use = true; - let first = match tokens.get(i.saturating_add(1)) { + let first = match next { Some(TokenTree::Punct(p)) if p.as_char() == ':' => { tokens.get(i.saturating_add(3)) } other => other, }; - sys_use = matches!(first, Some(TokenTree::Ident(f)) if names.contains(&f.to_string())); + sys_use = matches!(first, Some(TokenTree::Ident(f)) if names.contains(&ident_name(f))); + if is_punct(first, '$') { + out.push(( + line_of(tt), + "a macro-passed crate name in a `use` (`use $c`) hides what a path \ + reaches" + .to_owned(), + )); + } // `pub use`, `pub(crate) use`, `pub(in path) use`. - pub_use = ident_is(before(1), "pub") - || (matches!(before(1), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis) - && ident_is(before(2), "pub")); + pub_use = visibility_ends_at(tokens, i, 1); // `pub use clazel_sys;` hands the crate itself on. - let after_root = if matches!(tokens.get(i.saturating_add(1)), Some(TokenTree::Punct(p)) if p.as_char() == ':') - { - 4 - } else { - 2 - }; + let after_root = if is_punct(next, ':') { 4 } else { 2 }; if sys_use && pub_use && is_punct(tokens.get(i.saturating_add(after_root)), ';') { out.push(( @@ -1526,25 +1869,89 @@ fn scan_code_use( )); } } - if name == "clazel_sys" && ident_is(tokens.get(i.saturating_add(1)), "as") { - out.push(( - line_of(tt), - "an alias of `clazel_sys` hides what a path reaches (`clazel_sys as x`)" + if name == "clazel_sys" { + let root_of_use = in_use + && (ident_is(before(1), "use") + || (is_punct(before(1), ':') + && is_punct(before(2), ':') + && ident_is(before(3), "use"))); + let extern_crate = + ident_is(before(1), "crate") && ident_is(before(2), "extern"); + if ident_is(next, "as") { + out.push(( + line_of(tt), + "an alias of `clazel_sys` hides what a path reaches (`clazel_sys as \ + x`)" .to_owned(), - )); + )); + } + if in_use && !root_of_use { + out.push(( + line_of(tt), + "`clazel_sys` must be the first segment of a `use` path, not inside \ + a brace group, which hides what it reaches" + .to_owned(), + )); + } + if extern_crate && visibility_ends_at(tokens, i, 3) { + out.push(( + line_of(tt), + "`pub extern crate clazel_sys` hands its code-region API on".to_owned(), + )); + } + if in_macro && !root_of_use && !extern_crate && !is_punct(next, ':') { + out.push(( + line_of(tt), + "`clazel_sys` passed to a macro as a bare name hides what a path \ + reaches" + .to_owned(), + )); + } } - if CODE_NAMES.contains(&name.as_str()) { + if CODE_NAMES.contains(&name.as_str()) + || (ctx.mentions && SHARED_CODE_NAMES.contains(&name.as_str())) + { out.push(( line_of(tt), format!("`{name}` is the code-region API of `clazel-sys`"), )); } - if name == "enter" && ((in_use && sys_use) || path_starts_at(tokens, i, names)) { + if name == "enter" + && ctx.mentions + && ((in_use && sys_use) || path_starts_at(tokens, i, names)) + { out.push(( line_of(tt), "`enter` is the code-region API of `clazel-sys`".to_owned(), )); } + // The submodule that holds the regions and writers. + if name == "code" + && ((in_use && sys_use) + || path_starts_at(tokens, i, names) + || (is_punct(before(1), ':') + && is_punct(before(2), ':') + && is_platform(before(3)))) + { + out.push(( + line_of(tt), + "`code` is the submodule of `clazel-sys` that holds the code-region API" + .to_owned(), + )); + } + // A platform module handed on would let another file reach its `code`. + // (A private alias is a name that [`collect_sys_aliases`] follows.) + if PLATFORM_MODULES.contains(&name.as_str()) + && in_use + && sys_use + && pub_use + && !is_punct(next, ':') + { + out.push(( + line_of(tt), + format!("the platform module `{name}` of `clazel-sys` re-exported"), + )); + } } TokenTree::Punct(p) if p.as_char() == ';' => { in_use = false; @@ -1562,30 +1969,44 @@ fn scan_code_use( } TokenTree::Group(g) => { let body: Vec = g.stream().into_iter().collect(); - // `use clazel_sys::{self as s}` or `pub use clazel_sys::{self}`: - // the crate itself, aliased or re-exported. (`self` of a - // submodule, `clazel_sys::macos::window::{self as sys}`, is - // not the crate.) + // `use clazel_sys::{self as s}` or `pub use clazel_sys::{self}` + // (the crate itself, aliased or re-exported), and the same for + // a platform module re-exported: `pub use clazel_sys::macos::{self}`. + // (`self` of another submodule, + // `clazel_sys::macos::window::{self as sys}`, is not either.) let at_root = in_use && sys_use && g.delimiter() == Delimiter::Brace && is_punct(before(1), ':') && is_punct(before(2), ':') - && matches!(before(3), Some(TokenTree::Ident(r)) if names.contains(&r.to_string())); + && (matches!(before(3), Some(TokenTree::Ident(r)) if names.contains(&ident_name(r))) + || is_platform(before(3))); if at_root { for (j, t) in body.iter().enumerate() { + let platform = is_platform(before(3)); if ident_is(Some(t), "self") - && (pub_use || ident_is(body.get(j.saturating_add(1)), "as")) + && (pub_use + || (!platform && ident_is(body.get(j.saturating_add(1)), "as"))) { out.push(( line_of(t), - "`self` of `clazel_sys` re-exported or aliased in a `use` group" + "`self` of `clazel_sys` or a platform module re-exported or \ + aliased in a `use` group" .to_owned(), )); } } } - scan_code_use(&body, (in_use, sys_use, pub_use), names, out); + // A macro invocation or definition: `name! (..)`. + let macro_group = + is_punct(before(1), '!') && matches!(before(2), Some(TokenTree::Ident(_))); + let inner = UseState { + in_use, + sys_use, + pub_use, + in_macro: in_macro || macro_group, + }; + scan_code_use(&body, inner, ctx, out); } _ => {} } @@ -2985,6 +3406,16 @@ mod tests { )) } + /// The violations of `f` other than that the file has no `fn enter`, which + /// these fragments of it do not. + fn no_enter_complaint(f: &super::File) -> Vec<(usize, String)> { + f.violations + .iter() + .filter(|(_, m)| !m.contains("exactly one `fn enter`")) + .cloned() + .collect() + } + fn entry(abi: &str) -> String { format!( "(std::mem::transmute::(stub))(ctx, entry, len)" @@ -2995,12 +3426,12 @@ mod tests { fn accepts_the_code_entry_for_each_abi() -> Result<(), String> { for abi in ["sysv64", "win64", "C"] { let f = check_source(ENTER_FILE, &entry_src(&entry(abi)))?; - assert_eq!(f.violations, Vec::new(), "{abi}"); + assert_eq!(no_enter_complaint(&f), Vec::new(), "{abi}"); assert_eq!(f.code_entries, 1, "{abi}"); } // A closing semicolon is the same statement. let f = check_source(ENTER_FILE, &entry_src(&format!("{};", entry("C"))))?; - assert_eq!(f.violations, Vec::new()); + assert_eq!(no_enter_complaint(&f), Vec::new()); assert_eq!(f.code_entries, 1); Ok(()) } @@ -3012,7 +3443,7 @@ mod tests { .map(|abi| entry_src(&entry(abi))) .collect(); let f = check_source(ENTER_FILE, &all)?; - assert_eq!(f.violations, Vec::new()); + assert_eq!(no_enter_complaint(&f), Vec::new()); assert_eq!(f.code_entries, 3); // The same ABI twice is rejected. let twice = format!( @@ -3157,12 +3588,90 @@ mod tests { Ok(()) } + /// `fn enter` as `crates/clazel-sys/src/enter.rs` has it. + const GOOD_ENTER: &str = "pub fn enter(region: &CodeRegion, entry: Validated, ctx: &mut Context) -> Result { + let offset = entry.from_start(); + if offset < CODE_START || offset >= region.len() { + return Err(CodeError::OutOfRange); + } + #[cfg(target_arch = \"aarch64\")] + if !offset.is_multiple_of(4) { + return Err(CodeError::Misaligned); + } + let stub = region.base(); + let entry = stub.checked_add(offset).ok_or(CodeError::OutOfRange)?; + let raw: *mut u8 = std::ptr::from_mut(&mut *ctx).cast(); + run(stub, raw, entry, region.len()); + Ok(Exit(ctx.exit_code())) +} +"; + /// The accepted file: `run` private, called once, by `enter`. fn enter_file(extra: &str) -> String { - format!( - "{}pub fn enter() {{\n run(1, std::ptr::null_mut(), 2, 3);\n}}\n{extra}", + format!("{}{GOOD_ENTER}{extra}", entry_src(&entry("sysv64"))) + } + + /// Audit of PR 277: a second `enter`, in a nested module or an `impl`, that + /// calls `run` once with other arguments, and an `enter` whose arguments to + /// `run` or whose bindings differ from the real one. + #[test] + fn rule_10_pins_enter_and_its_arguments_to_run() -> Result<(), String> { + assert_eq!( + violations_in(ENTER_FILE, &enter_file(""))?, + Vec::::new() + ); + let nested_run = "run(stub, raw, entry, region.len());"; + let bad = [ + // A second `enter`, at module level, nested, or a method. + enter_file("pub fn enter() { }"), + enter_file("mod m { pub fn enter() { super::run(1, std::ptr::null_mut(), 2, 3); } }"), + enter_file("struct S; impl S { fn enter(&self) { } }"), + // The only `enter` is not at module level: it is a method or nested. entry_src(&entry("sysv64")) - ) + + &format!( + "impl S {{ {} }}", + GOOD_ENTER.replace("pub fn enter", "fn enter") + ), + entry_src(&entry("sysv64")) + &format!("mod m {{ {GOOD_ENTER} }}"), + // No `enter` at all. + entry_src(&entry("sysv64")), + // Another signature. + enter_file("").replace("region: &CodeRegion", "region: &Other"), + enter_file("").replace("ctx: &mut Context", "ctx: &mut Other"), + // Other arguments to `run`. + enter_file("").replace(nested_run, "run(stub, raw, entry + 1, region.len());"), + enter_file("").replace(nested_run, "run(stub, raw, entry, 0);"), + enter_file("").replace(nested_run, "run(0, raw, entry, region.len());"), + enter_file("").replace(nested_run, "run(stub, raw, offset, region.len());"), + enter_file("").replace(nested_run, "run(stub, raw, entry, region.len(), 1);"), + // The bindings changed or added. + enter_file("").replace("region.base()", "other.base()"), + enter_file("").replace("stub.checked_add(offset)", "stub.wrapping_add(offset)"), + enter_file("").replace(" let raw", " let entry = entry + 1;\n let raw"), + enter_file("").replace( + nested_run, + "let stub = 0; run(stub, raw, entry, region.len());", + ), + enter_file("").replace( + nested_run, + "for entry in 0..1 { run(stub, raw, entry, region.len()); }", + ), + enter_file("").replace( + nested_run, + "let f = |entry: usize| run(stub, raw, entry, region.len()); f(0);", + ), + enter_file("").replace( + nested_run, + "match 0 { _ => run(stub, raw, entry, region.len()) }", + ), + ]; + for src in bad { + assert!( + !violations_in(ENTER_FILE, &src)?.is_empty(), + "accepted: {src}" + ); + } + Ok(()) } #[test] @@ -3177,18 +3686,18 @@ mod tests { enter_file("").replace("fn run(", "pub(crate) fn run("), enter_file("").replace("fn run(", "pub(super) fn run("), // Called or named elsewhere. - enter_file("pub fn other() { run(1, std::ptr::null_mut(), 2, 3); }"), + enter_file("pub fn other() { run(stub, raw, entry, region.len()); }"), enter_file("fn other() { let f = run; }"), - enter_file("mod tests { fn t() { super::run(1, std::ptr::null_mut(), 2, 3); } }"), + enter_file("mod tests { fn t() { super::run(stub, raw, entry, region.len()); } }"), enter_file("use self::run as go;"), enter_file("pub use self::run;"), // Named as a value, not called, in `enter`. - enter_file("").replace("run(1, std::ptr::null_mut(), 2, 3);", "let _f = run;"), + enter_file("").replace("run(stub, raw, entry, region.len());", "let _f = run;"), // Not called, or called twice, in `enter`. - enter_file("").replace("run(1, std::ptr::null_mut(), 2, 3);", ""), + enter_file("").replace("run(stub, raw, entry, region.len());", ""), enter_file("").replace( - "run(1, std::ptr::null_mut(), 2, 3);", - "run(1, std::ptr::null_mut(), 2, 3); run(1, std::ptr::null_mut(), 2, 3);", + "run(stub, raw, entry, region.len());", + "run(stub, raw, entry, region.len()); run(stub, raw, entry, region.len());", ), ]; for src in bad { @@ -3267,6 +3776,123 @@ mod tests { Ok(()) } + /// Audit of PR 277: ways to name the crate, or a path to its code-region + /// API, that the first version of the rule did not see. + #[test] + fn rule_11_refuses_the_evasions_found_in_the_277_audit() -> Result<(), String> { + let bad = [ + // A raw identifier is the same name. + "use r#clazel_sys as s;", + "pub use r#clazel_sys as s;", + "fn f() { let _ = r#clazel_sys::enter(&r, e, &mut c); }", + "use r#clazel_sys::enter;", + "use r#clazel_sys::Validated;", + "pub use r#clazel_sys;", + "extern crate r#clazel_sys as s;", + // The crate name passed through a macro. + "macro_rules! m { ($c:ident) => { pub use $c as s; } }\nm!(clazel_sys);", + "macro_rules! m { ($c:ident) => { use $c; } }", + "macro_rules! m { ($c:ident) => { pub use $c as s; } }\nm!(r#clazel_sys);", + "m!{ clazel_sys }", + "m![clazel_sys, 1];", + "m!(pub use clazel_sys as s;);", + // Brace-wrapped and nested roots. + "use {clazel_sys::{self as s}};", + "pub use {clazel_sys};", + "use {clazel_sys::enter};", + "pub use {clazel_sys::Errno};", + "use a::{clazel_sys as s};", + "use {r#clazel_sys::{self as s}};", + // `pub extern crate`. + "pub extern crate clazel_sys;", + "pub(crate) extern crate clazel_sys;", + // The code-region submodules and the platform modules. + "pub use clazel_sys::macos::code as m;", + "pub use clazel_sys::linux::code as m;", + "pub use clazel_sys::windows::code as m;", + "use clazel_sys::macos::code as m;", + "use clazel_sys::macos::code;", + "use clazel_sys::macos::{code};", + "use clazel_sys::macos::{code as m};", + "use clazel_sys::{macos::code};", + "use clazel_sys::{macos::{code as m}};", + "pub use clazel_sys::macos::code::*;", + "pub use clazel_sys::macos as p;", + "use clazel_sys::linux as p;\nfn f() { p::code::other(); }", + "pub use clazel_sys::macos;", + "pub use clazel_sys::{macos};", + "use clazel_sys::macos::{self as p};\nfn f() { p::code::other(); }", + "pub use clazel_sys::macos::{self};", + "use clazel_sys::{macos as p};\nfn f() { p::code::other(); }", + "use clazel_sys::macos as p;\nfn f() { let _ = p::enter(&r, e, &mut c); }", + "use clazel_sys::{windows::{self as p}};\nfn f() { p::code::other(); }", + "pub use clazel_sys::macos::{self as p};", + "use clazel_sys::macos;\nfn f() { let _ = macos::code::CodeRegion::new(1); }", + "use clazel_sys::macos;\nfn f() { macos::code::other(); }", + "fn f() { clazel_sys::macos::code::other(); }", + "fn f() { clazel_sys::windows::code::other(); }", + // The names unique to the API, in a file that never names the crate: + // what a second file reaches through a path the first one made. + "fn f(r: &crate::s::CodeRegion) {}", + "fn f(w: crate::s::CodeWriter) {}", + "fn f(s: crate::a::SharedCode) {}", + "use crate::m::CodeRegion;", + "use crate::m::{CodeWriter, Other};", + "fn f() -> Result<(), crate::s::CodeError> { Ok(()) }", + "const X: usize = crate::s::CODE_START;", + "const X: usize = crate::s::CONTEXT_BYTES;", + "const X: usize = crate::s::EXIT_CODE_WORD;", + "fn f(r: crate::s::RemoteView) {}", + "fn f(r: &crate::s::r#CodeRegion) {}", + ]; + for src in bad { + assert!( + !super::code_use_violations(src)?.is_empty(), + "accepted: {src}" + ); + } + Ok(()) + } + + /// Code that must stay legal: what `clazel-window` and the rest of the + /// tree write, and other crates' own `enter` and `Validated`. + #[test] + fn rule_11_still_allows_the_legitimate_uses() -> Result<(), String> { + let fine = [ + // `clazel-window`. + "use clazel_sys::macos::window::{self as sys};", + "use clazel_sys::macos::window::{self as sys, App};", + "use clazel_sys::windows::window::{self as sys};", + "use clazel_sys::linux::window::{self as sys};", + "use clazel_sys::macos::metal;", + "use clazel_sys::macos::{metal, window};", + "use clazel_sys::macos::window as w;", + // `clazel-renderer`: a private alias of a platform module. + "#[cfg(target_os = \"linux\")]\nuse clazel_sys::linux as os;\n#[cfg(target_os = \"macos\")]\nuse clazel_sys::macos as os;\nfn f() { os::attempt_fork(); }", + "use clazel_sys::macos::{self as os};\nfn f() { os::attempt_fork(); }", + "use clazel_sys::macos;\nfn f() { let _ = macos::window::open(); }", + "use clazel_sys::Errno;\nfn f() { let _ = clazel_sys::fill_random(&mut b); }", + "fn f() { assert_eq!(clazel_sys::fill_random(&mut b), Ok(())); }", + "fn f() { println!(\"{:?}\", clazel_sys::Errno::UNKNOWN); }", + "extern crate clazel_sys;", + "use clazel_sys;", + "#[cfg(target_os = \"macos\")]\nuse clazel_sys::macos::window::{self as sys};", + // Other crates' own names, in files that never name `clazel_sys`. + "use clazel_renderer::sandbox::enter;\nfn f() { enter(); }", + "fn f() { clazel_renderer::sandbox::enter().unwrap(); }", + "use crate::flow::margins::{close, enter, exit};", + "fn f(c: Clips) { let (n, d) = c.enter(rect); }", + "use clazel_x509::path::Validated;\nfn f(v: Validated) {}", + "struct Validated; fn enter() {}", + "fn f() { let macos = 1; let code = 2; let _ = macos + code; }", + ]; + for src in fine { + let v = super::code_use_violations(src)?; + assert!(v.is_empty(), "flagged: {src}: {v:?}"); + } + Ok(()) + } + #[test] fn rule_11_leaves_a_plain_reexport_of_a_harmless_item_alone() -> Result<(), String> { for src in [ diff --git a/xtask/src/workspace.rs b/xtask/src/workspace.rs index 6aa57a07..7b94904f 100644 --- a/xtask/src/workspace.rs +++ b/xtask/src/workspace.rs @@ -532,6 +532,7 @@ mod tests { } #[test] + #[cfg_attr(miri, ignore = "writes files")] fn rule_11_rejects_a_renamed_dependency_outside_the_listed_crates() -> Result<(), String> { let bad = scratch_crate("planted-renamer", &["s"])?; let scan = scan_crate(&bad)?;