Skip to content

Run the compiler front end on one thread - #5207

Closed
feliperodri wants to merge 3 commits into
model-checking:mainfrom
feliperodri:frontend-threads-1
Closed

feliperodri wants to merge 3 commits into
model-checking:mainfrom
feliperodri:frontend-threads-1

Conversation

@feliperodri

@feliperodri feliperodri commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

tests/script-based-pre/codegen_cache has failed intermittently since the upgrade to nightly-2026-11-18 (#5188) with "FAIL: no cache statistics were printed". It has hit all five regression platforms, and through fail-fast it cancels the other regression jobs of the run.

Cause

Since nightly-2026-11-17, rustc is built with a default of two front-end threads (CFG_DEFAULT_FRONTEND_JOBS). With that default, rustc runs the front end on a worker of its own thread pool instead of on a thread it spawns and joins. Kani keeps per-compilation state in thread-locals, which assumes a single compiler thread:

  • The codegen CACHE. It prints its CACHE_STATS report from its destructor. Pool workers are not joined before the process exits, so that destructor sometimes never runs, and the report is lost while kani still exits successfully.
  • QUERY_DB, where the backend stores Kani's arguments. A query running on the other worker would see empty state. I haven't seen this happen, but it relies on the same assumption.

Change

Add -Zthreads=1 to KANI_REQUIRED_RUSTC_ARGS, the arguments kani-compiler appends to every Kani-mode invocation. That restores the sequential front end Kani was written for, and the doc comment explains why it's required. Like the other scalar options there, it's appended, so a caller can't override it. script-based-pre/compiler_defaults checks the full argument list, so its expected value is updated.

The Cargo lint fixes this PR also carried were merged as part of #5214.

Testing

  • With the change, kani-compiler runs with two fewer threads on the codegen_cache fixture (5 instead of 7), and the report is printed.
  • codegen_cache passed 32 times out of 32 with eight copies running in parallel. The failure is a timing race and never reproduced on my machine before the change either (80 runs), so CI is the real check.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

Since nightly-2026-11-17, rustc is built with a default of two front-end
threads (`CFG_DEFAULT_FRONTEND_JOBS`). The front end then runs on a worker of
rustc's thread pool, and queries may run on either worker. Kani keeps
per-compilation state in thread-locals (`QUERY_DB`, the codegen `CACHE`), which
assumes one compiler thread, and pool workers are not joined before the process
exits, so a thread-local destructor may never run. The codegen cache prints its
`CACHE_STATS` report from one, so `tests/script-based-pre/codegen_cache`
failed intermittently on every platform after the upgrade to
nightly-2026-11-18. Add `-Zthreads=1` to the arguments kani-compiler always
passes, which restores the sequential front end.

Also address the new Cargo lints that the same toolchain reports on every
`cargo` invocation: have the six remaining manifests inherit the workspace
lints, like the other members already do, and drop `homepage` where it repeats
`repository`.

Signed-off-by: Felipe R. Monteiro <[email protected]>
@feliperodri
feliperodri requested review from a team as code owners November 19, 2026 20:26
@feliperodri feliperodri added [C] Internal Tracks some internal work. I.e.: Users should not be affected. [F] Spurious Failure Issues that cause Kani verification to fail despite the code being correct. [I] CI / Infrastructure Work done to CI, tests and infrastructure. labels Nov 19, 2026
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Nov 19, 2026
feliperodri pushed a commit to tautschnig/kani that referenced this pull request Nov 20, 2026
On main, combining `#[kani::stub(f, g)]` with
`#[kani::proof_for_contract(f)]` or `#[kani::stub_verified(f)]` in the
same harness fails with `Failed to find contract closure
__kani_recursion_check_f`. The ordinary stub replaces the body
containing `f`'s contract instrumentation.

Kani now rejects these combinations while resolving the harness's stubs,
with an error naming the function and conflicting attributes. Ordinary
stubs of other functions and reverse-direction stubs remain supported.

Resolves model-checking#5206.

Testing: `tests/ui/function-contracts/ordinary_stub_target_conflict`
checks both errors, and
`tests/expected/function-contract/ordinary_stub_different_target` covers
a stub of another function and a reverse-direction stub under each
attribute. [CI in my
fork](https://github.com/Tiramisu-Cake/kani/actions/runs/44616139446/attempts/2):
regression on Ubuntu 22.04, Ubuntu 24.04, Ubuntu 24.04 ARM and macOS 15
Intel, documentation, perf, benchcomp, LLBC, format, Clippy and Cargo
Deny passed. The macOS 15 job did not get a runner, and Ubuntu 22.04
passed on a rerun after the intermittent `codegen_cache` failure
addressed by model-checking#5207.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.

*Disclosure: this change and description were prepared with the help of
AI assistants.*
Remove kani_macros' unused strum dependency (only strum_macros is used), mark std's kani dependency as used (its macro overrides expand to kani paths in the crates that use them, and concrete_playback is forwarded to kani), and drop the layout_for_ptr feature gates, since the feature is stable on the current toolchain.

Signed-off-by: Felipe R. Monteiro <[email protected]>
feliperodri added a commit that referenced this pull request Nov 21, 2026
The toolchain's Cargo warns on every invocation about manifests that do not inherit [workspace.lints] (cargo::missing_lints_inheritance) and about homepage keys that repeat repository (cargo::redundant_homepage). The root package, kani-driver, library/std, build-kani, kani-cov and scanner now inherit the workspace lints, like the other members already do; the workspace table only allows clippy::too_long_first_doc_paragraph, so this enables no new lints. Drop homepage from the root package and kani-driver.

(cherry picked from the Cargo manifest changes of commit ae4ce58, #5207)

Signed-off-by: Felipe R. Monteiro <[email protected]>
feliperodri added a commit that referenced this pull request Nov 21, 2026
Remove the unused dependencies that cargo::unused_dependencies reports: strum in kani_macros (only strum_macros is used) and lazy_static in cprover_bindings. Mark std's kani dependency as used: its macro overrides expand to kani paths in the crates that use them, and concrete_playback is forwarded to kani. Drop the layout_for_ptr feature gates, since the feature is stable on the current toolchain.

(cherry picked from commit 0893740, #5207, plus the lazy_static removal)

Signed-off-by: Felipe R. Monteiro <[email protected]>
feliperodri added a commit to tautschnig/kani that referenced this pull request Nov 21, 2026
Update Rust toolchain from nightly-2026-11-19 to nightly-2026-11-20.

This PR also carries two fixes needed to get CI green on this toolchain:
- **Install CBMC from the Sonoma bottle on Intel macOS** (from model-checking#5213).
Homebrew moved Intel Macs to Tier 3, and the CBMC formula's `openjdk@21`
build dependency now requires arm64. Without a macOS 15 Intel bottle,
brew built CBMC from source and failed, so every `macos-15-intel` job
failed, and fail-fast cancelled the other regression jobs.
- **Fix the Cargo and rustc warnings printed on every build** (from
model-checking#5207):
  - inherit `[workspace.lints]` in the six manifests that did not;
  - drop the `homepage` keys that repeat `repository`;
- remove the unused `strum` (`kani_macros`) and `lazy_static`
(`cprover_bindings`) dependencies;
  - mark `std`'s `kani` dependency as used;
  - drop the `layout_for_ptr` feature gates, which are stable now.

The only warning left is the `generic_const_exprs` note tracked in
model-checking#4832.

---------

Signed-off-by: Felipe R. Monteiro <[email protected]>
Co-authored-by: celinval <[email protected]>
Co-authored-by: Felipe R. Monteiro <[email protected]>
@feliperodri

Copy link
Copy Markdown
Member Author

The Cargo lint and warning fixes from this PR were merged as part of #5214. I merged main into this branch, so it now contains only the -Zthreads=1 change for the codegen_cache flake, and I updated the description to match.

feliperodri added a commit to tautschnig/kani that referenced this pull request Nov 21, 2026
Since nightly-2026-11-17, rustc defaults to two front-end threads and runs the front end on a worker of its thread pool. Kani keeps per-compilation state in thread-locals (the codegen cache, whose statistics are printed from its destructor, and the query database), which assumes a single compiler thread, and the codegen_cache test fails intermittently because pool workers are not joined before the process exits. Add -Zthreads=1 to the arguments kani-compiler appends to every Kani-mode invocation.

(cherry picked from model-checking#5207)

Signed-off-by: Felipe R. Monteiro <[email protected]>
@feliperodri

Copy link
Copy Markdown
Member Author

Closing as completed: the -Zthreads=1 change was merged as part of #4230, and the Cargo warning fixes as part of #5214.

auto-merge was automatically disabled November 21, 2026 21:42

Pull request was closed

calef pushed a commit to calef/kani that referenced this pull request Nov 23, 2026
This PR implements RFC model-checking#3893: two function contract clauses that
describe when a function panics.
- `#[kani::panics_only_if(cond)]`: the function may panic only if `cond`
held on entry.
- `#[kani::panics_if(cond)]`: the function must panic if `cond` held on
entry. Alone, it states that the function panics if and only if `cond`
holds.

```rust
#[kani::panics_if(index > self.len())]
#[kani::ensures(|_| self.len() == old(self.len()) + 1)]
pub fn insert(&mut self, index: usize, element: T) { /* ... */ }
```

A `proof_for_contract` harness checks four things:
- no undefined behavior on any path, including paths that panic;
- the function panics only when the clauses permit it;
- it does not return when `panics_if` requires a panic;
- postconditions hold whenever it returns.

When `stub_verified` replaces the function, the replacement panics as
the clauses say, so callers have to account for the panic. With both
clauses, an input where `panics_if` holds but `panics_only_if` does not
can neither panic nor return, so an inconsistent specification is
reported. This follows remi-delmas-3000's suggestion in the review
thread.

This replaces the earlier prototype of `panics_if`. That prototype
assumed `!cond` when checking the function, so the panicking inputs,
including any undefined behavior on them, were never verified.

## Changes

1. **Give Rust panics their own property class** (resolves model-checking#5212).
- Panics, the panicking MIR assertions, aborts, and Kani's
`assert!`/`assert_eq!`/`assert_ne!` overrides now emit `panic` checks.
The overrides go through a new hidden `kani::panic_assert`.
- `kani::assert`, `kani::check` and contract conditions keep
`assertion`.
- `#[kani::should_panic]`, `--export-json`'s `failure_kind` and
`--prove-safety-only` use the new class. So a failed postcondition or
`kani::assert` is no longer accepted as an expected panic, and
`--prove-safety-only` no longer drops them.
   - Autoharness's mining of `assert!` guards recognizes both functions.
2. **Update tests for the panic property class.**
- Expected files that name panic checks change from `.assertion.` to
`.panic.`.
- Two harnesses in
`tests/kani/FunctionContracts/generic_trait_method.rs` used
`should_panic` to accept a failed postcondition and a failed
precondition of a replaced contract. They move to an expected test that
checks those failures directly.
3. **Add the clauses.**
- The check closure evaluates each condition after the preconditions and
records it in verifier-internal state in `kani_core`. Like the
clause-depth counter, the state uses `__CPROVER_`/`__VERIFIER` symbol
names, so DFCC does not treat its updates as side effects, and it is
reset at harness entry.
- In a codegen unit where that state is recorded, every `panic` check
becomes `assert(cond || permitted); assume(cond)`. Other units are
unchanged.
- The replace closure panics when `panics_if` holds, and panics
nondeterministically when `panics_only_if` holds.
   - A function may have at most one clause of each kind.
4. **Tests** in `tests/expected/function-contract/panics/` and
`tests/expected/panic-class/`. They cover:
   - permitted and unexpected panics;
   - undefined behavior before a permitted panic;
   - wrong postconditions, and panics in the harness;
- must-panic checks, both clauses together, and an inconsistent
specification;
   - contract replacement;
   - `should_panic`, `--prove-safety-only` and duplicate clauses.
5. **Docs**: the `contracts` module, the contracts reference page, and
the `should_panic` and `--prove-safety-only` docs.

## Also in this PR

- `-Zthreads=1` for the compiler front end, cherry-picked from model-checking#5207.
This fixes the intermittent `codegen_cache` failure that kept failing
this PR's CI.
- Fixes from Copilot's review:
  - a panic while a contract clause is evaluated is never permitted;
  - assert mode treats only leading assertions as preconditions;
  - duplicate panic clauses are rejected even through renamed imports;
  - the `cbmc_truncated_results` fixture filters on `.panic.`.
- Known limitation, tracked in model-checking#5221: destructors of by-value arguments
that the body does not move out run after the panic permission is
restored. That can only cause spurious failures.

## Testing

- `kani-regression.sh` suites all pass, except
`script-based-pre/cbmc_truncated_results` and `compiler_defaults`, which
fail the same way on `main` in my environment (`jq` is missing).
`kani-compiler`, `kani-driver` and `kani_macros` unit tests pass. Clippy
with `-D warnings` and `kani-fmt --check` pass.
- I also probed `modifies`, `#[kani::recursion]`, `-Z uninit-checks`,
methods, and contracted callees that are not replaced. All behave as the
RFC describes.
- **verify-rust-std.** I backported these commits to the pinned Kani
(c5e65fd) and converted three std functions:
- `Duration::new`, `NonZero::abs` and `copy_from_slice` (whose
`requires(self.len() == src.len())` becomes `panics_if(self.len() !=
src.len())`) now verify on unrestricted inputs. Each proof takes under a
second.
  - The existing restricted harnesses still pass.

## Impact on verify-rust-std

- **`should_fail_*` harnesses.** The 8 `should_fail_*` harnesses in
`library/core/src/intrinsics/mod.rs` rely on model-checking#5212: they expect
`should_panic` to accept a failed `stub_verified` precondition. With
this PR they fail. I confirmed this for `should_fail_u32_to_char` and
`should_fail_u8_to_bool`.
- **`should_panic` partner harnesses.** Once a function has a
`panics_if` clause, its `should_panic` partner harness fails, because a
permitted panic is not a failed check. For example,
`duration_new_panics` fails while `duration_new` passes. These harnesses
can be deleted, since the unrestricted `proof_for_contract` harness
checks the panic condition directly.

---------

Signed-off-by: Felipe R. Monteiro <[email protected]>
Co-authored-by: Felipe R. Monteiro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Internal Tracks some internal work. I.e.: Users should not be affected. [F] Spurious Failure Issues that cause Kani verification to fail despite the code being correct. [I] CI / Infrastructure Work done to CI, tests and infrastructure. Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants