Repository navigation
Introduce bitst and bytest to avoid bit/byte mix-up - #7413
Conversation
71ea76d to
185c77f
Compare
185c77f to
eb3bbab
Compare
Codecov Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
eb3bbab to
cb0fde1
Compare
|
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 -
|
Thank you for alerting me to this! This will require implementing more methods, but that's ok. I'll try this route. |
4279119 to
c14cb4a
Compare
c14cb4a to
b6c9a4e
Compare
117e4d6 to
7cd034d
Compare
7cd034d to
98d642f
Compare
656f15d to
2cc9ef7
Compare
2cc9ef7 to
5eb0bd3
Compare
5eb0bd3 to
72265e9
Compare
72265e9 to
82ad51e
Compare
82ad51e to
79490c5
Compare
a6df679 to
3dad36d
Compare
3dad36d to
b533d00
Compare
b533d00 to
e427ca9
Compare
There was a problem hiding this comment.
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,bytesttypes, 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 returnbytest/bitstinstead of rawmp_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%hasPRECONDITION(rhs.get() > 0)butoperator%=doesn't have this check. If someone uses%=directly, they bypass the precondition. Also, theoperator/(line 169) similarly only checks in the free function. Consider moving the preconditions intooperator%=and the (missing)operator/=or at minimum into both places.
src/util/mp_arith.h:1 - The class is missing
operator/=but hasoperator%=,operator+=, andoperator-=. Division between same-typed values is provided as a free function returningmp_integer, but the asymmetry is confusing. Additionally, there's no explicit conversion constructor fromstd::size_t— many call sites usebitst{some_size_t}which relies on implicitstd::size_t→mp_integerconversion. This works but documenting the intended construction patterns would help maintainability.
src/util/lower_byte_operators.cpp:1 bits_to_bytes_trunchasPRECONDITION(bits_per_byte > 0)but the inputoffset_bits_in_target_elementis negative at this point (guarded byif(offset_bits_in_target_element < bitst{0})). When dividing a negativebitstby a positivebits_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 tobits_to_bytes_truncwhich just doesbits.get() / bits_per_byte. For negative dividends, C++ integer division truncates toward zero, sobits_to_bytes_trunc(bitst{-5}, 8)givesbytest{0}notbytest{-1}. The original code would compute-(-5) / 8 = 0then negate to get0, 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 callingbits_to_bytes_truncto preserve the original semantics.
src/util/simplify_expr.cpp:1- Division of
bitstbybitstreturnsmp_integer(per theoperator/template), then it's wrapped inbytest{}. This works but the intent isbits_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 offsetis anexprtbeing compared to integer0(line 2141). This was not changed to usebytestbut given thatoffsetis an expression, comparing it to0checks whether the expression is the integer constant 0 (viaexprt::operator==(int)). This is consistent with the original code — but note thatoffset_int(a few lines below) isnumeric_cast<bytest>(offset), so the variableoffsetitself is not abytest. This line is fine.
src/solvers/smt2/smt2_conv.cpp:1- Shift distances are measured in bits, so using
bitstis 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_opisnumeric_cast<bitst>— but the value is later used in arithmetic withwidth_op0(astd::size_t). Ensure the downstream arithmetic still compiles and is correct with thebitsttype.
💡 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.
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.