Skip to content

Introduce bitst and bytest to avoid bit/byte mix-up - #7413

Merged
tautschnig merged 2 commits into
diffblue:developfrom
tautschnig:feature/bits-bytes
Nov 6, 2026
Merged

tautschnig merged 2 commits into
diffblue:developfrom
tautschnig:feature/bits-bytes

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Use mp_integers with a unit to clarify when an mp_integer holds a number
of bits or a number of bytes. This makes sure that type-inconsistent
comparisons as fixed in #7411 result in compile-time errors.

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@tautschnig tautschnig self-assigned this Dec 5, 2022
@tautschnig tautschnig changed the title Introduce bitst and bytest to avoid bit/byte mix-up [depends-on: #7411, #7412] Introduce bitst and bytest to avoid bit/byte mix-up Dec 6, 2022
@tautschnig
tautschnig marked this pull request as ready for review December 6, 2022 12:40
@codecov

codecov Bot commented Dec 6, 2022 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.11409% with 47 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.01%. Comparing base (4839902) to head (2de6376).
⚠️ Report is 8 commits behind head on develop.

Files with missing lines Patch % Lines
src/cprover/bv_pointers_wide.cpp 55.55% 8 Missing ⚠️
src/goto-programs/interpreter_evaluate.cpp 33.33% 6 Missing ⚠️
src/memory-analyzer/analyze_symbol.cpp 77.27% 5 Missing ⚠️
src/solvers/flattening/bv_pointers.cpp 80.95% 4 Missing ⚠️
src/util/pointer_offset_size.cpp 93.65% 4 Missing ⚠️
src/ansi-c/expr2c.cpp 25.00% 3 Missing ⚠️
src/ansi-c/padding.cpp 93.87% 3 Missing ⚠️
src/analyses/goto_rw.cpp 77.77% 2 Missing ⚠️
src/goto-synthesizer/cegis_evaluator.cpp 60.00% 2 Missing ⚠️
src/util/config.cpp 84.61% 2 Missing ⚠️
... and 8 more
Additional details and impacted files
@@            Coverage Diff            @@
##           develop    #7413    +/-   ##
=========================================
  Coverage    81.01%   81.01%            
=========================================
  Files         1722     1723     +1     
  Lines       191139   191259   +120     
  Branches        73       73            
=========================================
+ Hits        154853   154957   +104     
- Misses       36286    36302    +16     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@thomasspriggs

Copy link
Copy Markdown
Contributor

Have you seen the approach taken to strong types here? https://www.fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces/

This approach appears to have the following advantages -

  • Templates are used to avoid needing a full cut-and-paste definition for each of the strong / unit associated types.
  • Values are stored using containership rather than inheritance. This could side-step some of the potential issues with implicit upcasting.

@tautschnig

Copy link
Copy Markdown
Collaborator Author

Have you seen the approach taken to strong types here? https://www.fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces/

This approach appears to have the following advantages -

* Templates are used to avoid needing a full cut-and-paste definition for each of the strong / unit associated types.

* Values are stored using containership rather than inheritance. This could side-step some of the potential issues with implicit upcasting.

Thank you for alerting me to this! This will require implementing more methods, but that's ok. I'll try this route.

@tautschnig tautschnig self-assigned this Dec 6, 2022
@tautschnig
tautschnig force-pushed the feature/bits-bytes branch 3 times, most recently from 4279119 to c14cb4a Compare December 14, 2022 08:47
@tautschnig tautschnig removed their assignment Dec 14, 2022
@tautschnig
tautschnig force-pushed the feature/bits-bytes branch from 117e4d6 to 7cd034d Compare June 1, 2023 18:28
@tautschnig tautschnig removed their assignment Jun 1, 2023
@tautschnig
tautschnig force-pushed the feature/bits-bytes branch from 7cd034d to 98d642f Compare July 19, 2023 05:51
@tautschnig
tautschnig force-pushed the feature/bits-bytes branch 2 times, most recently from 656f15d to 2cc9ef7 Compare November 20, 2023 21:00
@tautschnig tautschnig self-assigned this Jan 22, 2024
@tautschnig tautschnig assigned feliperodri and unassigned tautschnig Jan 22, 2024

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

@feliperodri feliperodri removed their assignment Jan 22, 2024
@tautschnig tautschnig self-assigned this Jan 22, 2024
@tautschnig tautschnig removed their assignment Jan 23, 2024
@tautschnig
tautschnig force-pushed the feature/bits-bytes branch 2 times, most recently from a6df679 to 3dad36d Compare May 8, 2024 11:32
@tautschnig
tautschnig force-pushed the feature/bits-bytes branch from 3dad36d to b533d00 Compare June 20, 2024 08:59
@tautschnig tautschnig assigned tautschnig and unassigned kroening Jul 28, 2025
@tautschnig
tautschnig force-pushed the feature/bits-bytes branch from b533d00 to e427ca9 Compare July 29, 2025 13:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Introduces bitst and bytest strong types wrapping mp_integer to prevent accidental bit/byte mix-ups at compile time.

Changes:

  • Adds named_mp_integert<Tag> template, bitst, bytest types, and conversion helpers (bytes_to_bits, bits_to_bytes_trunc, bits_to_bytes_ceil)
  • Updates all APIs (pointer_offset_size, pointer_offset_bits, member_offset, etc.) to return bytest/bitst instead of raw mp_integer
  • Updates all call sites across the codebase to use the new types

Reviewed changes

Copilot reviewed 51 out of 51 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/util/mp_arith.h Defines named_mp_integert, bitst, bytest, and conversion helpers
src/util/arith_tools.h Adds numeric_castt and from_integer overloads for named types
src/util/pointer_offset_size.h/.cpp Changes return types to bitst/bytest
src/util/simplify_expr*.cpp Updates simplification logic to use new types
src/util/lower_byte_operators.cpp Updates byte operator lowering to use new types
src/util/config.h/.cpp Changes alignment and memory_operand_size to bytest
src/ansi-c/padding.cpp Updates padding logic to use bytest
src/solvers/flattening/bv_pointers.cpp Updates pointer BV encoding
src/solvers/flattening/pointer_logic.cpp/.h Changes offset to bytest
Other files Mechanical updates to use new types
Comments suppressed due to low confidence (6)

src/util/mp_arith.h:1

  • The operator% has PRECONDITION(rhs.get() > 0) but operator%= doesn't have this check. If someone uses %= directly, they bypass the precondition. Also, the operator/ (line 169) similarly only checks in the free function. Consider moving the preconditions into operator%= and the (missing) operator/= or at minimum into both places.
    src/util/mp_arith.h:1
  • The class is missing operator/= but has operator%=, operator+=, and operator-=. Division between same-typed values is provided as a free function returning mp_integer, but the asymmetry is confusing. Additionally, there's no explicit conversion constructor from std::size_t — many call sites use bitst{some_size_t} which relies on implicit std::size_t → mp_integer conversion. This works but documenting the intended construction patterns would help maintainability.
    src/util/lower_byte_operators.cpp:1
  • bits_to_bytes_trunc has PRECONDITION(bits_per_byte > 0) but the input offset_bits_in_target_element is negative at this point (guarded by if(offset_bits_in_target_element < bitst{0})). When dividing a negative bitst by a positive bits_per_byte, the truncation direction matters. The original code did -offset_bits_in_target_element / bits_per_byte (dividing the absolute value), but the new code passes the negative value to bits_to_bytes_trunc which just does bits.get() / bits_per_byte. For negative dividends, C++ integer division truncates toward zero, so bits_to_bytes_trunc(bitst{-5}, 8) gives bytest{0} not bytest{-1}. The original code would compute -(-5) / 8 = 0 then negate to get 0, so the result happens to match for small negative values, but for e.g. -9 / 8: original gives -((-(-9))/8) = -1, new gives (-1) * ((-9)/8) = (-1)*(-1) = 1. The semantics differ. Consider using the absolute value before calling bits_to_bytes_trunc to preserve the original semantics.
    src/util/simplify_expr.cpp:1
  • Division of bitst by bitst returns mp_integer (per the operator/ template), then it's wrapped in bytest{}. This works but the intent is bits_to_bytes_trunc(m_offset_bits, expr.get_bits_per_byte()) — using the helper function would be more consistent with the rest of the PR and clearer about truncation direction.
    src/util/simplify_expr.cpp:1
  • offset is an exprt being compared to integer 0 (line 2141). This was not changed to use bytest but given that offset is an expression, comparing it to 0 checks whether the expression is the integer constant 0 (via exprt::operator==(int)). This is consistent with the original code — but note that offset_int (a few lines below) is numeric_cast<bytest>(offset), so the variable offset itself is not a bytest. This line is fine.
    src/solvers/smt2/smt2_conv.cpp:1
  • Shift distances are measured in bits, so using bitst is semantically correct here. However, numeric_cast_v<bitst> on a shift distance that may come from a non-bit-typed expression could be confusing. More importantly, at line 1893, distance_int_op is numeric_cast<bitst> — but the value is later used in arithmetic with width_op0 (a std::size_t). Ensure the downstream arithmetic still compiles and is correct with the bitst type.

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


mp_integer size = 0;
std::size_t count = 0;
std::optional<bytest> size;
The upper bound for lowering a union to a sequence of bytes is the
byte-width of the widest member, not the bit width.
Use mp_integers with a unit to clarify when an mp_integer holds a number
of bits or a number of bytes. This makes sure that type-inconsistent
comparisons as fixed in diffblue#7411 result in compile-time errors.
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.

9 participants