Skip to content

Report an invalid bool constant as undefined behavior - #4975

Merged
feliperodri merged 4 commits into
model-checking:mainfrom
srivatsansamraj:invalid-bool-constant
Nov 7, 2026
Merged

feliperodri merged 4 commits into
model-checking:mainfrom
srivatsansamraj:invalid-bool-constant

Conversation

@srivatsansamraj

@srivatsansamraj srivatsansamraj commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

At -Zmir-opt-level=2, GVN folds transmute::<u8, bool>(2) into a bool constant whose byte is 2, with no validity check and a dummy span. Kani's constant codegen read it with read_bool().unwrap(), which panics on any byte other than 0 or 1, so the whole crate failed to compile, including harnesses that never reach the constant (#3020). rustc uses mir-opt-level 2 whenever opt-level is above 0.

Kani now codegens such a constant as a failing check, Undefined Behavior: Invalid value of type `bool` , with the message and property class -Z valid-value-checks uses, followed by a nondeterministic bool so the rest of the program stays well formed. The check is emitted whether or not that flag is set; without it, the program in #3020 would verify. Only harnesses that reach the constant fail. Since GVN gives the constant a dummy span, the check reports the location of the statement that uses the constant.

Other scalar types do not crash, since their constants are read as integers. At mir-opt-level 2 an invalid char, NonZero or null reference also escapes -Z valid-value-checks, which catches it at the default level; that is a separate gap, not changed here.

The new test expected/issue-3020 (-Zmir-opt-level=2) has an invalid bool and one inside a struct (both FAILURE), one behind a branch that is never taken, and a valid bool (both SUCCESSFUL), and pins the location of each failing check. It panics on main.

Resolves #3020

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

@srivatsansamraj

Copy link
Copy Markdown
Contributor Author

@CYJ904 @Tianshu-Huang @wodex1nhaoIeng @acearyanarun for review. At the same optimisation level, rustc can fold an invalid bool (a byte other than 0 or 1, made with transmute) into a constant, and Kani crashed reading it, failing every harness in the crate. Kani now reports it as undefined behaviour, only in the harnesses that reach it. Whether that report should depend on -Z valid-value-checks is left to the maintainers. The standard-library runs never hit this, so their numbers don't change.

@srivatsansamraj
srivatsansamraj requested review from a team as code owners October 5, 2026 21:10
@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 Oct 5, 2026
@feliperodri feliperodri self-assigned this Oct 22, 2026
@feliperodri feliperodri added [C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed Z-Autoharness Issue related to autoharness subcommand labels Nov 6, 2026
@feliperodri feliperodri added this to the Autoharness milestone Nov 6, 2026
@feliperodri feliperodri removed the Z-Autoharness Issue related to autoharness subcommand label Nov 6, 2026
@feliperodri feliperodri removed this from the Autoharness milestone Nov 6, 2026
@feliperodri
feliperodri requested a balanced review from Copilot November 6, 2026 18:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

4 open issues
What changed in this PR

Adds a regression test and codegen changes so that invalid bool constants (e.g., from MIR GVN folding transmute::<u8, bool>(2) at -Zmir-opt-level=2) are reported as undefined behavior at the point of use, rather than panicking during constant decoding.

Changes:

  • Add new expected-test case for issue #3020 that validates failures only when the invalid constant is reachable.
  • Track the current MIR statement/terminator source location during codegen.
  • Emit a SafetyCheck UB assertion + nondeterministic bool when decoding an invalid bool constant.
File Description
tests/​expected/​issue-3020/​issue_3020.rs New regression test exercising reachable/unreachable invalid bool constants at -Zmir-opt-level=2.
tests/​expected/​issue-3020/​issue_3020.expected Expected output asserting only the reachable harnesses fail with the UB message and correct locations.
kani-compiler/​src/​codegen_cprover_gotoc/​context/​goto_ctx.rs Adds current_stmt_loc to carry statement/terminator location into deeper codegen layers.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​statement.rs Sets current_stmt_loc when codegen’ing a statement or terminator.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​operand.rs Replaces read_bool().unwrap() with UB check + nondet value when decoding invalid bool constants.

Review effort: Lite

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach is right: reaching an invalid bool is UB, and the reached, unreached, struct and valid cases are covered.

Before merge:

  1. Please rebase. The PR is dirty against main.
  2. current_stmt_loc is mutable state that is never restored. Pass the location to the constant codegen, or document that it's only valid inside a statement. Copilot's threads on this are fair.
  3. Handle a None location as well as line 0.
  4. Write bool in the message instead of {ty}, since this branch only handles bool.
  5. audit fails. Please confirm it also fails on main.

At `-Zmir-opt-level=2`, GVN can fold `transmute::<u8, bool>(2)` into a
`bool` constant whose byte is 2, and `try_codegen_constant` panicked on it
in `read_bool().unwrap()`, which aborted the whole crate (model-checking#3020). Such a
constant now becomes a failing safety check with the message and property
class `-Z valid-value-checks` uses, followed by a nondeterministic `bool`,
so only harnesses that reach it fail.
A constant folded by GVN has a dummy span, so the check for an invalid
`bool` constant was reported at line 0. `GotocCtx` now records the
location of the statement or terminator being codegen'd, and the check
uses it when the constant's own location is on line 0. The test pins
the location of both failing checks.
Code generated outside a statement could otherwise report an invalid
`bool` at the last statement of an unrelated block. The field's
documentation now says that it is valid only inside a statement or
terminator. A location without a line also takes the statement's
location, and the message names `bool` directly.
@srivatsansamraj

Copy link
Copy Markdown
Contributor Author

@feliperodri rebased onto main. current_stmt_loc is cleared after each basic block and documented as valid only inside a statement, a None location is handled, and the message names bool (88280bdb3). Cargo Deny passes on main; the failure came from the old base.

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The location state is cleared per block and documented, a None location is handled, and the message names bool. audit passes after the rebase.

compile-timer-short fails in the analyzer step with normal timings, and the macos-14 jobs are cancelled by the runner. Neither comes from this change. Approving.

@feliperodri
feliperodri added this pull request to the merge queue Nov 7, 2026
Merged via the queue into model-checking:main with commit 45db131 Nov 7, 2026
22 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed 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.

panic Error("Unexpected value for bool: 2") on transmute shenanigans

4 participants