Skip to content

Warn about bitwise shift with negative shift amount (UB in C) - #1989

Merged
sim642 merged 8 commits into
masterfrom
copilot/warn-bitwise-shift-misuse
May 11, 2026
Merged

sim642 merged 8 commits into
masterfrom
copilot/warn-bitwise-shift-misuse

Conversation

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown
Contributor
  • Refactor shift_amount_negcheckcheck_shift_neg dir y that directly performs the M.error/M.warn/Checks.* actions
  • Shiftlt and Shiftrt cases now call check_shift_neg "left" y and check_shift_neg "right" y respectively
  • Fix test: use bounded non-negative interval [0,5] instead of unsigned int top
  • Use ID.ge/ID.lt instead of ID.minimal/ID.maximal in check_shift_neg for potentially more precise results

Copilot AI and others added 3 commits April 12, 2026 07:19
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/c81d432d-7783-4469-9c48-85d6bb3d1531

Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/c81d432d-7783-4469-9c48-85d6bb3d1531

Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/c81d432d-7783-4469-9c48-85d6bb3d1531

Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add warning for bitwise shift misuse with negative arguments Warn about bitwise shift with negative shift amount (UB in C) Apr 12, 2026
Copilot AI requested a review from michael-schwarz April 12, 2026 07:25
Comment thread src/analyses/base.ml Outdated
@michael-schwarz

Copy link
Copy Markdown
Member

@copilot: Address Simmo's comment, check why the tests don't pass and fix this.

…d int

Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/1dec7b36-67b9-4835-abab-afb1ff9c852e

Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>

Copilot AI commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

@copilot: Address Simmo's comment, check why the tests don't pass and fix this.

Done in commit 67be8c5:

  1. Addressed the code deduplication (Simmo's comment): refactored to check_shift_neg dir y that directly performs the checks, eliminating the identical match expressions in both Shiftlt and Shiftrt.

  2. Fixed the test failure: the int nonneg case expected the interval domain to determine nonneg ≥ 0 after if (nonneg < 0) { nonneg = 0; }, but the combined tuple's minimal was returning negative values in CI. Replaced it with unsigned int u_shift (always ≥ 0 by type in both def_exc and interval domains).

@michael-schwarz

Copy link
Copy Markdown
Member

@copilot: One of the tests still fails!

Expected nowarn, but registered warn on shift-neg:31
  res = 8 << u_shift; //NOWARN
98/09 failed!

1 test(s) failed: ["98/09 shift-neg"]

… int top

Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/daf39d5f-96e8-4887-9ade-7004d5fcc994

Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>

Copilot AI commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

@copilot: One of the tests still fails!

...

Fixed in commit 5dd9a2e. The unsigned int u_shift approach failed because an uninitialized unsigned int has the full range [0, UINT_MAX], and the pre-existing shift-overflow check fires when the shift amount might exceed the integer width (UINT_MAX >> 31 bits). Replaced with int pos_shift = 0; if (top) { pos_shift = 5; } which gives a bounded interval [0, 5] — provably non-negative (my new check passes) and within the valid shift range (no overflow warning).

Comment thread src/analyses/base.ml Outdated
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/fd400683-08ae-4a0a-b4ff-458d87ba80f8

Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
@michael-schwarz
michael-schwarz marked this pull request as ready for review May 7, 2026 10:18
Copilot AI review requested due to automatic review settings May 7, 2026 10:18

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.

Pull request overview

Adds explicit diagnostics for undefined behavior when bit-shifting by a negative amount, improving the base analysis’ reporting precision and extending regression coverage.

Changes:

  • Refactors shift-negative checking into check_shift_neg and integrates it into Shiftlt/Shiftrt handling in src/analyses/base.ml.
  • Emits safe / warn / error results for Checks.Category.InvalidShift based on interval bounds of the shift amount.
  • Adds a dedicated regression test for negative/possibly-negative and provably non-negative shift amounts.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/regression/98-bitwise-operations/09-shift-neg.c Adds regression coverage for left/right shifts with negative, possibly-negative, and non-negative shift amounts.
src/analyses/base.ml Introduces check_shift_neg and invokes it for both << and >> to diagnose negative shift counts as UB.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sim642 sim642 added the feature label May 11, 2026
@sim642 sim642 added this to the v2.8.0 Clumsy Clurichaun milestone May 11, 2026
@sim642
sim642 merged commit ef751d0 into master May 11, 2026
23 checks passed
@sim642
sim642 deleted the copilot/warn-bitwise-shift-misuse branch May 11, 2026 07:36
avsm pushed a commit to ocaml/opam-repository that referenced this pull request Jun 15, 2026
CHANGES:

* Add new may-happen-in-parallel analyses (goblint/analyzer#1805, goblint/analyzer#1865, goblint/analyzer#1913, goblint/analyzer#1928).
* Add Open Verification Dashboard checks output (goblint/analyzer#1838, goblint/analyzer#1929).
* Add negative bitwise shift warnings (goblint/analyzer#1637, goblint/analyzer#1989).
* Add missing function declaration warnings (goblint/analyzer#1911).
* Improve overflow warnings (goblint/analyzer#1894, goblint/analyzer#1895, goblint/analyzer#1896, goblint/analyzer#1905).
* Fix spurious overflow checks (goblint/analyzer#1767, goblint/analyzer#1909, goblint/analyzer#1910, goblint/analyzer#1932, goblint/analyzer#2022).
* Fix missing overflow and out-of-bounds checks (goblint/analyzer#1935, goblint/analyzer#2017, goblint/analyzer#2029).
* Optimize base analysis domain using Patricia trees (goblint/analyzer#2002, goblint/analyzer#2015).
* Optimize field offset calculations (goblint/analyzer#1964, goblint/analyzer#1973, goblint/analyzer#1974).
* Optimize non-incremental top-down solver (goblint/analyzer#1566, goblint/analyzer#1972).
* Add OCaml 5.5 support (goblint/analyzer#2006, goblint/analyzer#2010).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn about bitwise shift misuse with negative arguments

4 participants