Repository navigation
Report an invalid bool constant as undefined behavior - #4975
Conversation
|
@CYJ904 @Tianshu-Huang @wodex1nhaoIeng @acearyanarun for review. At the same optimisation level, rustc can fold an invalid |
21a43eb to
efa5146
Compare
There was a problem hiding this comment.
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
The dummy-span detection only checksstart_line() == Some(0). If a dummy/unknown location is… · Newcurrent_stmt_locis a mutable field that gets overwritten for each statement/terminator, but it… · Newcurrent_stmt_locis a mutable field that gets overwritten for each statement/terminator, but it… · New This branch is only forRigidTy::Bool, so using{ty}is effectively alwaysbool. To reduce… · New
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
SafetyCheckUB assertion + nondeterministicboolwhen decoding an invalidboolconstant.
| 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
left a comment
There was a problem hiding this comment.
The approach is right: reaching an invalid bool is UB, and the reached, unreached, struct and valid cases are covered.
Before merge:
- Please rebase. The PR is dirty against
main. current_stmt_locis 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.- Handle a
Nonelocation as well as line 0. - Write
boolin the message instead of{ty}, since this branch only handlesbool. auditfails. Please confirm it also fails onmain.
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.
efa5146 to
88280bd
Compare
|
@feliperodri rebased onto main. |
feliperodri
left a comment
There was a problem hiding this comment.
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.
45db131


At
-Zmir-opt-level=2, GVN foldstransmute::<u8, bool>(2)into aboolconstant whose byte is 2, with no validity check and a dummy span. Kani's constant codegen read it withread_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 wheneveropt-levelis 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-checksuses, followed by a nondeterministicboolso 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,NonZeroor 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 invalidbooland one inside a struct (both FAILURE), one behind a branch that is never taken, and a validbool(both SUCCESSFUL), and pins the location of each failing check. It panics onmain.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.