-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
Conversation
Codecov ReportBase: 88.59% // Head: 88.60% // Increases project coverage by
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
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. |
c1632f4
to
51e596f
Compare
it's to hopefully allow for CI to pass |
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`.
51e596f
to
15f37f7
Compare
There was a problem hiding this 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.
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
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
In our case we have
BITS_IN_LONG == 31
andPyLong_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: