Repository navigation
ruff_charon: no '?' dispatch targets; record witnessed effects with exact assert kind #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c9f9b6
ruff_charon: extract may_dispatch_object; register red test for unnam…
claude 46ab97b
ruff_charon: never emit a '?' placeholder as a MAY_DISPATCH target (B…
claude 5e53b37
ruff_charon: register red witnessed-effects fixture (B1)
claude bcf1cab
ruff_charon: record witnessed effects with their exact kind (B1 produ…
claude a36cdb9
ruff_charon: effects test discriminates per-occurrence rows (cg_app::…
claude 482a3dd
ruff_charon: cover DEREF_WRITE effects with an argument/local/plain f…
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //! Writes through dereferenced places, for `tests/effects.rs`. | ||
| //! `through_arg` writes twice through argument-rooted derefs, `through_local` | ||
| //! writes once through a local reference, and `plain` assigns only to locals. | ||
|
|
||
| pub struct S { | ||
| pub f: i32, | ||
| } | ||
|
|
||
| pub fn through_arg(p: &mut i32, s: &mut S) { | ||
| *p = 1; | ||
| s.f = 2; | ||
| } | ||
|
|
||
| pub fn through_local(v: i32) -> i32 { | ||
| let mut x = v; | ||
| let r = &mut x; | ||
| *r = 3; | ||
| x | ||
| } | ||
|
|
||
| pub fn plain(a: i32) -> i32 { | ||
| let y = a; | ||
| y | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| //! Witnessed effects: `<crate>.effects.tsv` records each effect the compiler's | ||
| //! LLBC shows in a function body (a built-in or user assert, with its exact | ||
| //! kind; a write through a dereferenced place). It is a list of observations | ||
| //! only: a function with no row is NOT proven pure (asserts removed by | ||
| //! Charon's own passes, callees' effects and unharvested bodies are not | ||
| //! here), so nothing may read the absence of a row as purity. | ||
| #![expect( | ||
| clippy::disallowed_methods, | ||
| reason = "tests write and read their own temp files; there is no ty System here" | ||
| )] | ||
|
|
||
| use std::path::PathBuf; | ||
|
|
||
| use ruff_charon::load; | ||
| use ruff_charon::store::Store; | ||
|
|
||
| fn data(name: &str) -> PathBuf { | ||
| PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
| .join("tests/data") | ||
| .join(name) | ||
| } | ||
|
|
||
| fn import(name: &str) -> (PathBuf, String) { | ||
| import_crate(name, "cg_lib") | ||
| } | ||
|
|
||
| fn import_crate(name: &str, krate: &str) -> (PathBuf, String) { | ||
| let dir = std::env::temp_dir().join(format!("ruff_charon_fx_{name}_{}", std::process::id())); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| let llbc = load(&data(&format!("{krate}.llbc"))).expect("llbc"); | ||
| Store::new(&dir).import(&llbc, krate).expect("import"); | ||
| let text = | ||
| std::fs::read_to_string(dir.join(format!("{krate}.effects.tsv"))).expect("effects file"); | ||
| (dir, text) | ||
| } | ||
|
|
||
| /// `(effect, kind)` of every row of `subject`, in file order. | ||
| fn effects_of<'a>(text: &'a str, subject: &str) -> Vec<(&'a str, &'a str)> { | ||
| text.lines() | ||
| .filter(|l| !l.starts_with('#')) | ||
| .filter_map(|l| { | ||
| let f: Vec<&str> = l.split('\t').collect(); | ||
| (f[0] == subject).then(|| (f[1], f[2])) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Fails if the file is missing or its header changes without a version bump. | ||
| #[test] | ||
| fn the_effects_file_has_a_versioned_header() { | ||
| let (dir, text) = import("header"); | ||
| let mut lines = text.lines(); | ||
| assert_eq!(lines.next(), Some("#version 1")); | ||
| assert_eq!( | ||
| lines.next(), | ||
| Some("#schema subject\teffect\tkind\tfile\tstart\tend") | ||
| ); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
|
|
||
| /// The N7 witness pair. Today the relation rows of `double` (`x * 2`) and | ||
| /// `unit` are indistinguishable although Charon saw an overflow check in | ||
| /// `double`. The check must be recorded with its exact kind. | ||
| #[test] | ||
| fn a_witnessed_overflow_check_is_recorded_with_its_kind() { | ||
| let (dir, text) = import("witness"); | ||
| assert_eq!( | ||
| effects_of(&text, "cg_lib::util::double"), | ||
| [("ASSERT", "Overflow:Mul")] | ||
| ); | ||
| assert_eq!( | ||
| effects_of(&text, "cg_lib::util::triple"), | ||
| [("ASSERT", "Overflow:Mul")] | ||
| ); | ||
| // `double(self.0) / 2 * self.0`: two different kinds, in body order. | ||
| assert_eq!( | ||
| effects_of(&text, "cg_lib::shapes::impl_Shape_for_Square::area"), | ||
| [("ASSERT", "DivisionByZero"), ("ASSERT", "Overflow:Mul")] | ||
| ); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
|
|
||
| /// The silence twin: a function with no witnessed effect has no row. This is | ||
| /// the absence of an observation, never a purity verdict. | ||
| #[test] | ||
| fn a_function_without_a_witnessed_effect_has_no_row() { | ||
| let (dir, text) = import("silent"); | ||
| assert!(effects_of(&text, "cg_lib::util::unit").is_empty()); | ||
| assert!(effects_of(&text, "cg_lib::util::private_unused").is_empty()); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
|
|
||
| /// Fails if repeated effects of one kind are collapsed: one row per | ||
| /// occurrence, so a count survives. `cg_app::run` has three additions, each | ||
| /// with its own overflow check; a kind-level dedup would leave one. | ||
| #[test] | ||
| fn every_occurrence_is_its_own_row() { | ||
| let (dir, text) = import_crate("multi", "cg_app"); | ||
| assert_eq!( | ||
| effects_of(&text, "cg_app::run"), | ||
| [("ASSERT", "Overflow:Add"); 3] | ||
| ); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
|
|
||
| /// Fails if the effect rows disagree with the independent reference output | ||
| /// (`cg_lib.llbc_calls.txt`, written by another tool), which counts the | ||
| /// asserts per function. | ||
| #[test] | ||
| fn the_assert_count_matches_the_reference() { | ||
| let (dir, text) = import("reference"); | ||
| let total_asserts = text | ||
| .lines() | ||
| .filter(|l| !l.starts_with('#')) | ||
| .filter(|l| l.split('\t').nth(1) == Some("ASSERT")) | ||
| .count(); | ||
| let reference: usize = std::fs::read_to_string(data("cg_lib.llbc_calls.txt")) | ||
| .expect("reference") | ||
| .lines() | ||
| .filter_map(|l| l.rsplit("assert=").next()?.parse::<usize>().ok()) | ||
| .sum(); | ||
| assert_eq!(total_asserts, reference); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
|
|
||
| /// Fails if writes through a dereferenced place are dropped or their root is | ||
| /// misclassified: `through_arg` writes twice through argument-rooted derefs | ||
| /// (`*p` and `s.f`, which is `(*s).f`), `through_local` once through a local | ||
| /// reference, and `plain` only assigns locals, so it has no row (the twin). | ||
| #[test] | ||
| fn deref_writes_are_classified_by_their_root() { | ||
| let (dir, text) = import_crate("deref", "derefw"); | ||
| assert_eq!( | ||
| effects_of(&text, "derefw::through_arg"), | ||
| vec![("DEREF_WRITE", "Arg"), ("DEREF_WRITE", "Arg")] | ||
| ); | ||
| assert_eq!( | ||
| effects_of(&text, "derefw::through_local"), | ||
| vec![("DEREF_WRITE", "Local")] | ||
| ); | ||
| assert!(effects_of(&text, "derefw::plain").is_empty(), "{text}"); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.