Fix bug due to UB in conversion from python int to ZZ (python 3.11, 32 bit, gcc12)#34997
Merged
vbraun merged 2 commits intosagemath:developfrom Feb 24, 2023
Merged
Fix bug due to UB in conversion from python int to ZZ (python 3.11, 32 bit, gcc12)#34997vbraun merged 2 commits intosagemath:developfrom
vbraun merged 2 commits intosagemath:developfrom
Conversation
mkoeppe
reviewed
Feb 7, 2023
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
Member
Author
Member
|
it's to hopefully allow for CI to pass |
Member
|
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
mkoeppe
approved these changes
Feb 8, 2023
Contributor
mkoeppe
left a comment
There was a problem hiding this comment.
Thanks for making this change. LGTM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 == 31andPyLong_SHIFT == 30so the computed value is<long>1 << -29which 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: