Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug due to UB in conversion from python int to ZZ (python 3.11, 32 bit, gcc12) #34997

Merged
merged 2 commits into from
Feb 24, 2023

Conversation

tornaria
Copy link
Contributor

@tornaria tornaria commented Feb 7, 2023

This affects 32 bit architectures, where the representation of python integers changed in cpython 3.11, when compiled with gcc12.

As part of #33842, the function sage.arith.long.integer_check_long_py() was rewritten to support the new representation. Unfortunately a bug remained that triggers UB for the conversion of integers between 2^60 and 2^63-1. Alas, the undesired behaviour does not happen with gcc10; it only started when I switched to gcc12.

The bug manifests in lots of doctests failing, but a quick way to demonstrate the issue is

sage: ZZ ( int(1152921504606847018) )  # 2^60 + 42
42

The function integer_check_long_py() has good unit testing, checking values around the word size, but this range was missing.

This commit adds a simple fix and new test cases for a few integers in this range.

Technical explanation:

The UB is in the line

cdef long lead_3_overflow = (<long>1) << (BITS_IN_LONG - 2 * PyLong_SHIFT)

In our case we have BITS_IN_LONG == 31 and PyLong_SHIFT == 30 so the computed value is <long>1 << -29 which is UB and it happens to evaluate to 0 with gcc10 but 8 with gcc12.

The solution is to set the value to 0 when BITS_IN_LONG < 2 * PyLong_SHIFT (which only happens for 32 bit python 3.11)


TESTING:

@codecov-commenter
Copy link

codecov-commenter commented Feb 7, 2023

Codecov Report

Base: 88.59% // Head: 88.60% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (15f37f7) compared to base (6a4667b).
Patch has no changes to coverable lines.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #34997   +/-   ##
========================================
  Coverage    88.59%   88.60%           
========================================
  Files         2136     2136           
  Lines       396141   396141           
========================================
+ Hits        350977   350985    +8     
+ Misses       45164    45156    -8     
Impacted Files Coverage Δ
src/sage/cpython/_py2_random.py 74.38% <0.00%> (-1.66%) ⬇️
src/sage/doctest/forker.py 80.24% <0.00%> (-1.48%) ⬇️
src/sage/schemes/elliptic_curves/cardinality.py 86.61% <0.00%> (-0.79%) ⬇️
src/sage/sets/integer_range.py 91.41% <0.00%> (-0.51%) ⬇️
src/sage/combinat/constellation.py 91.18% <0.00%> (-0.41%) ⬇️
src/sage/modular/overconvergent/hecke_series.py 98.76% <0.00%> (-0.31%) ⬇️
src/sage/combinat/posets/poset_examples.py 88.39% <0.00%> (-0.29%) ⬇️
src/sage/rings/function_field/ideal.py 89.47% <0.00%> (-0.17%) ⬇️
...e/combinat/cluster_algebra_quiver/mutation_type.py 52.73% <0.00%> (-0.12%) ⬇️
src/sage/sandpiles/sandpile.py 90.92% <0.00%> (-0.07%) ⬇️
... and 8 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tornaria
Copy link
Contributor Author

tornaria commented Feb 7, 2023

@dimpase I'm not sure what is the expectation here.

Is 51e596f just a rebase of c1632f4 on top of the new develop? How do I grab 51e596f from github into my local git repo?

If I want to add a small change (say, to comply with @mkoeppe request), how would I do it?

@dimpase
Copy link
Member

dimpase commented Feb 7, 2023

it's to hopefully allow for CI to pass

@dimpase
Copy link
Member

dimpase commented Feb 7, 2023

you can of course rebase the branch in your fork over the new develop, too. then they will be identical

….11, 32 bit)

This affects 32 bit architectures, where the representation of python
integers changed in cpython 3.11, when compiled with gcc12.

As part of sagemath#33842, the function `sage.arith.long.integer_check_long_py()`
was rewritten to support the new representation.  Unfortunately a bug
remained that triggers UB for the conversion of integers between 2^60
and 2^63-1. Alas, the undesired behaviour does not happen with gcc10;
it only started when I switched to gcc12.

The bug manifests in lots of doctests failing, but a quick way to
demonstrate the issue is

    sage: ZZ ( int(1152921504606847018) )  # 2^60 + 42
    42

The function `integer_check_long_py()` has good unit testing, checking
values around the word size, but this range was missing.

This commit adds a simple fix and new test cases for a few integers in
this range.

Technical explanation:

The UB is in the line

    cdef long lead_3_overflow = (<long>1) << (BITS_IN_LONG - 2 * PyLong_SHIFT)

In our case we have `BITS_IN_LONG == 31` and `PyLong_SHIFT == 30` so the
computed value is `<long>1 << -29` which is UB and it happens to
evaluate to 0 with gcc10 but 8 with gcc12.

The solution is to set the value to 0 when `BITS_IN_LONG < 2 * PyLong_SHIFT`.
Copy link
Contributor

@mkoeppe mkoeppe left a comment

Choose a reason for hiding this comment

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

Thanks for making this change. LGTM.

@mkoeppe mkoeppe modified the milestones: sage-9.8, sage-9.9 Feb 11, 2023
@vbraun vbraun merged commit 3380d29 into sagemath:develop Feb 24, 2023
@tornaria tornaria deleted the integer_check branch March 6, 2023 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants