镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Verify the absence of overflow in checked integer arithmetic - #280

Merged
coord-e merged 7 commits into
mainfrom
claude/vibrant-brown-tjmb6h-checked-arith
Sep 28, 2026
Merged

coord-e merged 7 commits into
mainfrom
claude/vibrant-brown-tjmb6h-checked-arith

Conversation

@coord-e

@coord-e coord-e commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

This is the first stage of the fix for the arithmetic-overflow issue found while reviewing #278.

With overflow checks on, rustc lowers integer + / - / * to AddWithOverflow / SubWithOverflow / MulWithOverflow followed by an assert on the overflow flag. Thrust hit unimplemented! on these operations, which is why every UI test is compiled with -C debug-assertions=off.

Changes

  • AddWithOverflow / SubWithOverflow / MulWithOverflow now evaluate to the pair (wrapped result, overflowed), matching rustc's definition ("both the wrapped result and a bool indicating whether it overflowed"):
    • the first field is the mathematical result wrapped into the operand type's range;
    • overflowed is true exactly when the mathematical result lies outside that range.
  • The existing Assert handling then turns each overflow check into a verification condition, so a program that can overflow is rejected.
  • Operand MIR types are read from Analyzer::local_decls. body.local_decls does not include the temporaries that ReborrowVisitor adds.

Tests

int_overflow_checked, compiled with -C overflow-checks=on: an i64 x + 1 guarded against i64::MAX. The fail side has the guard off by one.

On main, the pair stops at not implemented: ty=int, op=AddWithOverflow. cargo test fails on exactly the same set of tests as main (65 PCSat tests: the local coar:main image can't handle (Seq Int)). fmt and clippy -D warnings are clean.

Not covered (next stage)

  • Integer values carry no range facts yet. For example, nothing records that a u32 parameter is non-negative, so a guard is needed on both sides of a value unless the inferred precondition already bounds it.
  • Wrapping arithmetic when overflow checks are off will be handled together with range facts.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MvvhC4sHsobSMHAn9CgLBq

With overflow checks on, rustc lowers `+`, `-` and `*` on integers to
AddWithOverflow/SubWithOverflow/MulWithOverflow followed by an assertion
on the overflow flag, and these fell through to unimplemented!. They now
evaluate to the pair of the mathematical result and whether it lies
outside the operand type's range, so the existing Assert handling turns
each overflow check into a verification condition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MvvhC4sHsobSMHAn9CgLBq
AddWithOverflow and friends evaluate to the wrapped result paired with
the overflow flag, so the first field is now wrapped into the operand
type's range instead of relying on rustc reading it only after the
overflow assertion. Also drop the SubWithOverflow test pair; one pair
covers the checked operations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MvvhC4sHsobSMHAn9CgLBq
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MvvhC4sHsobSMHAn9CgLBq
Term::pow2 is gone from main, so int_term_in_range builds its bounds
the way wrap_int_term does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MvvhC4sHsobSMHAn9CgLBq
@coord-e
coord-e marked this pull request as ready for review September 28, 2026 08:50
@coord-e
coord-e requested a balanced review from Copilot September 28, 2026 08:50
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-28T08:54:48.383515Z 2f0f5bc Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.

Copilot review overview

🟡 Changes recommended

The regression pair can succeed for the wrong reason and leaves subtraction, multiplication, and unsigned behavior untested.

Review effort: Balanced
Findings: 5 Low severity

Open (5)
What changed in this PR

Adds verification for overflow-checked integer addition, subtraction, and multiplication.

Changes:

  • Models wrapped results and overflow flags.
  • Uses analyzer-local MIR declarations.
  • Adds an i64 overflow UI-test pair.
File Description
src/​analyze/​basic_block.rs Implements checked arithmetic modeling.
tests/​ui/​pass/​int_overflow_checked.rs Adds passing overflow-check coverage.
tests/​ui/​fail/​int_overflow_checked.rs Adds expected-failure coverage.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/analyze/basic_block.rs
Comment thread src/analyze/basic_block.rs
Comment thread src/analyze/basic_block.rs
Comment thread tests/ui/fail/int_overflow_checked.rs Outdated
Comment thread tests/ui/pass/int_overflow_checked.rs Outdated
coord-e and others added 2 commits September 28, 2026 17:56
Update assertions for succ_below_max function.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Update assertions in main to test edge cases.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@coord-e
coord-e merged commit 4204213 into main Sep 28, 2026
6 checks passed
@coord-e
coord-e deleted the claude/vibrant-brown-tjmb6h-checked-arith branch September 28, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants