π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Simplify approval checks for Rust unsafe code
Manav Rathi944f35e91147
diff --git a/.github/checks/change-approval/check.mjs b/.github/checks/change-approval/check.mjsindex 5e1671e78d..f79699e6c0 100644--- a/.github/checks/change-approval/check.mjs+++ b/.github/checks/change-approval/check.mjs@@ -30,11 +30,11 @@ const cargoLints = (source) =>import json, sys, tomllibcargo = tomllib.loads(sys.stdin.read())workspace = cargo.get("workspace", {})-selection = {key: workspace.get(key) for key in ("members", "exclude", "default-members")}+selection = {key: sorted(workspace[key]) if key in workspace else None for key in ("members", "exclude", "default-members")}print(json.dumps([cargo.get("lints"), workspace.get("lints"), selection], sort_keys=True))`], { encoding: "utf8", input: source });-function rustLintScopes(source) {+function rustPolicy(source) {const lexer = /\/\/[^\n]*|\/\*|[bc]?r(#+)?"[\s\S]*?"\1|[bc]?"(?:\\[\s\S]|[^"\\])*"|b?'(?:\\(?:u\{[\da-fA-F_]+\}|x[\da-fA-F]{2}|[\s\S])|[^'\\\r\n])'|(?:r#)?[a-zA-Z_]\w*|[^\s]/gu;const tokens = [];let match;@@ -68,64 +68,39 @@ function rustLintScopes(source) {if (closing) throw new Error("Unclosed Rust delimiter");return nodes;};- const lint = (nodes, levels = /^(?:r#)?(?:allow|expect|warn|deny|forbid)$/) => nodes.some((node, i) =>- Array.isArray(node) ? lint(node, levels) : levels.test(node) && nodes[i + 1]?.[0] === "(",+ const lint = (nodes) => nodes.some((node, i) =>+ Array.isArray(node) ? lint(node) : /^(?:r#)?(?:allow|expect|warn|deny|forbid)$/.test(node) && nodes[i + 1]?.[0] === "(",);- const scopes = [];- const visit = (nodes, context) => {- let boundary = 0;- let angles = 0;+ const declarations = [];+ const visit = (nodes) => {for (let i = 0; i < nodes.length; i++) {if (nodes[i] === "#") {let end = i;let hasLint = false;- let exception = false;- let inner = false;while (nodes[end] === "#") {const bang = nodes[end + 1] === "!";const attribute = nodes[end + (bang ? 2 : 1)];if (!Array.isArray(attribute) || attribute[0] !== "[") break;hasLint ||= lint(attribute);- exception ||= lint(attribute, /^(?:r#)?(?:allow|expect)$/);- inner ||= bang;end += bang ? 3 : 2;}if (hasLint) {let stop = end;- const statement = nodes[end] === "let";- let angles = 0;- let whereClause = false;- while (stop < nodes.length) {- const node = nodes[stop++];- whereClause ||= node === "where";- if (!statement && node === "<") angles++;- if (!statement && node === ">") angles = Math.max(0, angles - 1);- if (node === ";" || (!statement && !angles && ((node === "," && !whereClause) || (Array.isArray(node) && node[0] === "{")))) break;+ if (nodes[i + 1] !== "!") {+ while (stop < nodes.length && !["{", ";", ",", "="].includes(Array.isArray(nodes[stop]) ? nodes[stop][0] : nodes[stop])) stop++;}- const scope = inner ? (exception ? nodes : nodes.slice(i, end)) : nodes.slice(i, stop);- if (!inner && !exception && scope.at(-1)?.[0] === "{") scope[scope.length - 1] = ["{", "}"];- scopes.push(JSON.stringify([context, scope]));+ declarations.push(JSON.stringify(nodes.slice(i, stop)));}if (end > i) {i = end - 1;continue;}}- if (Array.isArray(nodes[i])) {- visit(nodes[i].slice(1, -1), [...context, nodes.slice(boundary, i), nodes[i][0]]);- if (nodes[i][0] === "{" && !angles) boundary = i + 1;- } else if (nodes[i] === ";") {- boundary = i + 1;- angles = 0;- } else if (nodes[i] === "<") {- angles++;- } else if (nodes[i] === ">") {- angles = Math.max(0, angles - 1);- }+ if (Array.isArray(nodes[i])) visit(nodes[i].slice(1, -1));}};- visit(group(), []);- return JSON.stringify(scopes.sort());+ visit(group());+ return { unsafe: tokens.includes("unsafe"), declarations: JSON.stringify(declarations.sort()) };}const tomlPackages = (text) =>@@ -280,8 +255,10 @@ const lintPolicies = [...numstat(), ...additions].filter((file) => {const before = kept.includes(file) ? git("show", `${mergeBase}:${file}`) : "";const after = sizes.has(file) ? (local ? readFileSync(file, "utf8") : git("show", `HEAD:${file}`)) : "";- const policy = file.endsWith(".rs") ? rustLintScopes : cargoLints;- return policy(before) !== policy(after);+ if (!file.endsWith(".rs")) return cargoLints(before) !== cargoLints(after);+ const oldPolicy = rustPolicy(before);+ const newPolicy = rustPolicy(after);+ return oldPolicy.unsafe || newPolicy.unsafe || oldPolicy.declarations !== newPolicy.declarations;});const categories = [@@ -303,7 +280,7 @@ const detail = [.join("\n\n")}`,guardrails.length && `## Guardrail changes\n\n${list(guardrails.map(code))}`,configs.length && `## Toolchain and registry config\n\n${list(configs.map(code))}`,- lintPolicies.length && `## Rust lint policy and exceptions\n\n${list(lintPolicies.map(code))}`,+ lintPolicies.length && `## Rust lint declarations and files containing unsafe\n\n${list(lintPolicies.map(code))}`,].filter(Boolean).join("\n\n");diff --git a/.github/checks/change-approval/test.mjs b/.github/checks/change-approval/test.mjsindex b0c9e2b3a5..c0412dfe1e 100644--- a/.github/checks/change-approval/test.mjs+++ b/.github/checks/change-approval/test.mjs@@ -454,7 +454,7 @@ test("Cargo lint changes need approval across TOML layouts", (t) => {]) {const { output, summary } = scan(t, { "rust/Cargo.toml": before }, { "rust/Cargo.toml": after }, { ci: true });assert.equal(output, 'categories=["Rust lint policy files"]\n');- assert.match(summary, /## Rust lint policy and exceptions\n\n- `rust\/Cargo.toml`/);+ assert.match(summary, /## Rust lint declarations and files containing unsafe\n\n- `rust\/Cargo.toml`/);}});@@ -466,75 +466,104 @@ test("Cargo dependency edits and equivalent lint layouts need no lint approval",}), "");});-test("Rust lint attributes, conditions and scopes need approval", (t) => {- const expect = '#[expect(unsafe_code, reason = "FFI") ]';- const body = 'fn call() { unsafe { ffi(); } }';+test("reordering Cargo workspace selection lists needs no approval", (t) => {+ for (const key of ["members", "exclude", "default-members"]) {+ assert.equal(scan(t, {+ "rust/Cargo.toml": `[workspace]\n${key} = ["a", "b"]\n`,+ }, {+ "rust/Cargo.toml": `[workspace]\n${key} = ["b", "a"]\n`,+ }), "");+ }+});++test("Rust lint declarations, reasons and conditions need approval", (t) => {+ const expect = '#[expect(dead_code, reason = "Shared helper")]';+ const body = 'fn helper() {}';for (const [before, after] of [[body, `${expect}\n${body}`],[`${expect}\n${body}`, body],- [`${expect}\n${body}`, `${expect.replace("FFI", "New reason")}\n${body}`],- [`${expect}\n${body}`, `${expect}\n${body.replace("ffi();", "ffi(); more_ffi();")}`],+ [`${expect}\n${body}`, null],+ [`${expect}\n${body}`, `${expect.replace("Shared helper", "New reason")}\n${body}`],+ [`${expect}\n${body}`, `${expect.replace("dead_code", "unused_variables")}\n${body}`],[`#[cfg(unix)]\n${expect}\n${body}`, `${expect}\n${body}`],- [`#[cfg_attr(unix, expect(unsafe_code, reason = "FFI"))]\n${body}`, `#[cfg_attr(test, expect(unsafe_code, reason = "FFI"))]\n${body}`],+ [`#[cfg_attr(unix, expect(dead_code))]\n${body}`, `#[cfg_attr(test, expect(dead_code))]\n${body}`],+ [`#[cfg_attr(unix, cfg_attr(test, expect(dead_code)))]\n${body}`, body],+ [`#[path = "a.rs"]\n${expect}\nmod support;`, `#[path = "b.rs"]\n${expect}\nmod support;`],+ [body, `#![allow(clippy::allow_attributes, clippy::allow_attributes_without_reason, dead_code)]\n${body}`],+ [body, `#[allow(dead_code, reason = "Shared helper")]\n${body}`],+ [`#[deny(dead_code)]\nmod guarded {}`, 'mod guarded {}'],+ [body, `#[r#expect(dead_code, reason = "Shared helper")]\n${body}`],+ [`${expect}\nmod support {}`, `${expect.replace("#[", "#![")}\nmod support {}`],[`${expect}\n${body}\nfn other() {}`, `${body}\n${expect}\nfn other() {}`],- [body, `#![allow(clippy::allow_attributes, clippy::allow_attributes_without_reason, unsafe_code)]\n${body}`],- [body, `#[allow(unsafe_code, reason = "FFI")]\n${body}`],- [`#[deny(unsafe_code)]\nmod guarded {}`, 'mod guarded {}'],- [body, `#[r#expect(unsafe_code, reason = "FFI")]\n${body}`],]) {assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), /^1 Rust lint policy file\n/);}});-test("Rust statement exceptions cover the whole initializer and their enclosing function", (t) => {- const binding = '#[expect(unsafe_code, reason = "FFI")] let x = unsafe { ffi() };';- const before = `fn a() { ${binding} } fn b() {}`;- for (const after of [- `fn a() {} fn b() { ${binding} }`,- before.replace('ffi() };', 'ffi() } + unsafe { other() };'),+test("ordinary code under existing lint declarations needs no approval", (t) => {+ for (const before of [+ '#[expect(dead_code, reason = "Shared helper")] fn helper() { first(); }',+ '#![expect(dead_code, reason = "Shared helpers")] fn helper() { first(); }',+ '#![forbid(unsafe_code)] fn helper() { first(); }',+ 'fn helper() { #[expect(unused_variables, reason = "Temporary binding")] let value = first(); }',]) {- assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), /^1 Rust lint policy file\n/);+ assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('first()', 'second()') }), "");}});-test("Rust generic signatures do not truncate an exception's scope", (t) => {- for (const signature of ['fn f<T, U>() -> Array<{ 2 }>', 'fn f<T>() where T: A, T: B']) {- const before = `#[expect(unsafe_code, reason = "FFI")] ${signature} { unsafe { ffi() } }`;- assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('ffi()', 'other()') }), /^1 Rust lint policy file\n/);+test("any edit in a file containing unsafe needs approval", (t) => {+ for (const before of [+ 'fn call() { #[expect(unsafe_code)] if ready() {} else { unsafe { first(); } } }',+ 'unsafe impl Send for Context {}\nfn unrelated() { first(); }',+ 'unsafe fn ffi() {}\n// first',+ ]) {+ assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('first', 'second') }), /^1 Rust lint policy file\n/);}- const binding = '#[expect(unsafe_code, reason = "FFI")] let x = unsafe { ffi() };';- assert.match(scan(t, {- "src/lib.rs": `fn a<T, U>() -> Array<{ 2 }> { ${binding} } fn b<T, U>() -> Array<{ 2 }> {}`,- }, {- "src/lib.rs": `fn a<T, U>() -> Array<{ 2 }> {} fn b<T, U>() -> Array<{ 2 }> { ${binding} }`,- }), /^1 Rust lint policy file\n/);-});--test("Rust closure parameters do not truncate an exception's initializer", (t) => {- const before = 'fn f() { #[expect(unsafe_code, reason = "FFI")] let f = |p, q| unsafe { ffi(p, q) }; }';- assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('ffi(p, q)', 'other(p, q)') }), /^1 Rust lint policy file\n/);});-test("Rust comments, strings and unrelated edits do not change exceptions", (t) => {- const decoys = '/* nested /* #[allow(unsafe_code)] */ comment */\nconst EXAMPLE: &str = r##"#[expect(unsafe_code)]"##;\n';- const before = `${decoys}#[expect(unsafe_code, reason = "brackets: ] [ escaped: \\\"")] fn call() { unsafe { ffi(); } }\nfn constructor() {}`;- const after = before- .replace('fn constructor() {}', "fn constructor() { let c = ']'; }")- .replace('unsafe { ffi(); }', 'unsafe { /* reason */ ffi( ); }')- .replace('expect(unsafe_code, reason', 'expect(\n unsafe_code,\n reason');- assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), "");+test("unsafe in either revision needs approval, including new and deleted files", (t) => {+ const source = 'fn call() { unsafe { ffi(); } }';+ for (const [before, after] of [+ ['fn call() {}', source],+ [source, 'fn call() {}'],+ [source, null],+ ]) {+ assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), /^1 Rust lint policy file\n/);+ }+ assert.match(scan(t, {}, { "src/lib.rs": source }, { commit: false }), /^1 Rust lint policy file\n/);});-test("code under deny or forbid can change without changing lint policy", (t) => {- for (const attribute of ['#![forbid(unsafe_code)]', '#[deny(unsafe_code)]']) {- const before = `${attribute}\nfn a() { first(); }`;- assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('first()', 'second()') }), "");- assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('unsafe_code', 'dead_code') }), /^1 Rust lint policy file\n/);+test("external module edits need approval only when the edited file contains unsafe", (t) => {+ for (const [before, needsApproval] of [+ ['pub fn call() { unsafe {\n first();\n} }', true],+ ['fn helper() { first(); }', false],+ ]) {+ const { output } = scan(t, {+ "src/lib.rs": '#[expect(dead_code, unsafe_code, reason = "Shared helpers")] #[path = "support/mod.rs"] mod support;',+ "src/support/mod.rs": 'mod nested;',+ "src/support/nested.rs": before,+ }, {+ "src/support/nested.rs": before.replace('first()', 'second()'),+ }, { ci: true });+ assert.equal(output, needsApproval ? 'categories=["Rust lint policy files"]\n' : 'categories=[]\n');}});-test("new untracked Rust exceptions and deleted exception files are scanned", (t) => {- const source = '#[expect(dead_code, reason = "Shared helper")] fn helper() {}';- assert.match(scan(t, {}, { "src/lib.rs": source }, { commit: false }), /^1 Rust lint policy file\n/);- assert.match(scan(t, { "src/lib.rs": source }, { "src/lib.rs": null }), /^1 Rust lint policy file\n/);+test("Rust comments, literals and raw identifiers do not count as unsafe or declarations", (t) => {+ const before = String.raw`+// unsafe { #[allow(dead_code)]+/* nested /* unsafe #[allow(dead_code)] */ comment */+const EXAMPLE: &str = r##"unsafe #[expect(dead_code)]"##;+fn example() {+ let r#unsafe = "unsafe #[allow(dead_code)]";+ let _ = (b"unsafe", c"unsafe", br"unsafe", cr#"unsafe"#, ']');+}+#[expect(dead_code, reason = "brackets: ] [ escaped: \")]")]+fn helper() {}+`;+ const after = before+ .replaceAll('unsafe', 'example')+ .replace('expect(dead_code, reason', 'expect(\n dead_code, /* comment */\n reason')+ .replace('fn helper() {}', 'fn helper() { let c = \'[\'; }');+ assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), "");});