8000 bpo-44698: Fix undefined behaviour in complex exponentiation. by Yhg1s · Pull Request #27278 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44698: Fix undefined behaviour in complex exponentiation. #27278 8000

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

Merged
merged 7 commits into from
Jul 26, 2021

Conversation

Yhg1s
Copy link
Member
@Yhg1s Yhg1s commented Jul 21, 2021

The undefined behaviour is already triggered by an existing check in test_complex (test_truediv, later moved to test_pow), which was commented out until Python 3.8. Clang's UBSan, specifically -fsanitize=float-cast-overflow, caught the error. It's present in all Python versions that I have access to, but it's probably not worth fixing in 3.8 and earlier.

https://bugs.python.org/issue44698

The undefined behaviour is already triggered by an existing check in
test_complex (test_truediv), which was commented out until Python 3.8.
Clang's UBSan, specifically -fsanitize=float-cast-overflow, caught the
error.
@@ -526,7 +526,10 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
}
errno = 0;
exponent = b;
int_exponent = (long)exponent.real;
if ((double)LONG_MIN <= exponent.real && exponent.real <= (double)LONG_MAX)
Copy link
Member

Choose a reason for hiding this comment

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

if the sanitizer is happy with this, i'm happy with this. It's basically asking the compiler to do the cast from high precision 64-bit int to less precision 53-bit *2^11 int in the right direction.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, UBSan is happy with it; tested internally with all the sanitizers, and with Debian's clang 7.0 with CFLAGS=-fsanitize=float-cast-overflow LDFLAGS=-fsanitize=float-cast-overflow.

Copy link
Member

Choose a reason for hiding this comment

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

Does this condition still allow the case of exponent.real equal to 2**63 (because (double)LONG_MAX will be rounded up to 2**63, assuming 64-bit longs)?

Copy link
Member Author

Choose a reason for hiding this comment

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

If you look at the next bit of code, it's checking that int_exponent == exponent.real before using int_exponent. It's checking for a fast path for complex ** int (or float with an integer value) after creating a complex out of int. I believe the new check has exactly the same effect while avoiding undefined behaviour, but the failure mode here is taking the slightly slower path for integer values that don't round-trip through the complex's 'real' double.

Copy link
Member

Choose a reason for hiding this comment

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

Right, but that check occurs after the undefined behaviour has already taken place. If exponent.real is 0x1.0p+63, the guard in the if condition will be true, and then we invoke undefined behaviour when we do (long)exponent.real.

Copy link
Member

Choose a reason for hiding this comment

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

Whoops, sorry; wrong section reference. That should be C11 §6.3.1.4p2, not C11 §6.3.1.5 (but the same quoted text).

Copy link
Member

Choose a reason for hiding this comment

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

I think changing the second inequality to exponent.real < -(double)LONG_MIN would fix this corner case. Unlike LONG_MAX, the conversion of LONG_MIN to a double should be exact (assuming two's complement, no trap representation, etc., etc., but those are assumptions that we've been happy to make in many other places in the code).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, you're right. I didn't realise a double that's equal to LONG_MAX could end up being an invalid cast to long. I've added a test that triggered the behaviour, and was correctly caught by UBSan without the fix:

test_pow (main.ComplexTest) ... ../../cpython/Objects/complexobject.c:530:24: runtime error: 9.22337e+18 is outside the range of representable values of type 'long'

Copy link
Member

Choose a reason for hiding this comment

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

Well, technically the double 0x1.0p+63 isn't equal to LONG_MAX; it's equal to the result of converting LONG_MAX to a double - the error introduced in that conversion is exactly the cause of difficulties here.

Thanks for the fix! (BTW, I'm not Mark Shannon. :-))

Copy link
Member Author

Choose a reason for hiding this comment

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

D'oh, I'm sorry, Mark! I know you're not Mark Shannon, I have no idea what I was thinking while I wrote that commit message.

@rhettinger
Copy link
Contributor

FWIW, the only use of int_exponent is guarded by the test on the subsequent line:

 if (exponent.imag == 0. && exponent.real == int_exponent)

Presumably, that has prevented any real word errors by checking after the conversion rather than beforehand.

Copy link
Member
@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -526,7 +526,10 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
}
errno = 0;
exponent = b;
int_exponent = (long)exponent.real;
if ((double)LONG_MIN <= exponent.real && exponent.real < (double)LONG_MAX)
Copy link
Member

Choose a reason for hiding this comment

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

Just as a comment: the reason I suggested -(double)LONG_MIN instead of (double)LONG_MAX is that (a) it's easier to reason about, since LONG_MIN is (almost certainly) a power of 2 and so there's no error introduced in the long-to-double conversion, and (b) it makes a difference in the case of 32-bit longs: if exponent.real has value exactly equal to 2**31 - 1 then it can safely be converted to a long, but the if condition excludes it. So we miss one particular integer case here. It doesn't matter much - there's no undefined behaviour either way, but I do think -(double)LONG_MIN is a bit cleaner here.

Copy link
Contributor
@ambv ambv Jul 22, 2021

Choose a reason for hiding this comment

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

FWIW I find -(double)LONG_MIN looking odd but the explanation why is very clear. Since it's strictly more correct (even if the difference is academic), I'd suggest adopting it with a comment essentially copying what Mark wrote above. It'll be a gem to find for future code readers. Think of the children!

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, looking at the code a bit more, I think we should stop doing this altogether. The code is taking 'w' and converting it to a complex, and then unpacking the complex to check if w is an integer value that fits in a C long. We have a much easier way of checking for that...

Copy link
Member
@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

The new version LGTM. If I'm reading it correctly, it means that z ** 2.0 will no longer use the fast algorithm, but that seems fine to me.

@ambv ambv merged commit 1d582bb into python:main Jul 26, 2021
@miss-islington
Copy link
Contributor

Thanks @Yhg1s for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.9.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-27366 is a backport of this pull request to the 3.10 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 26, 2021
…GH-27278)

(cherry picked from commit 1d582bb)

Co-authored-by: T. Wouters <thomas@python.org>
@bedevere-bot
Copy link

GH-27367 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Jul 26, 2021
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 26, 2021
…GH-27278)

(cherry picked from commit 1d582bb)

Co-authored-by: T. Wouters <thomas@python.org>
ambv pushed a commit that referenced this pull request Jul 26, 2021
…) (#27366)

(cherry picked from commit 1d582bb)

Co-authored-by: T. Wouters <thomas@python.org>
ambv pushed a commit that referenced this pull request Jul 26, 2021
…) (GH-27367)

(cherry picked from commit 1d582bb)

Co-authored-by: T. Wouters <thomas@python.org>
@Yhg1s Yhg1s deleted the complex-pow branch October 21, 2022 23:33
@Yhg1s Yhg1s restored the complex-pow branch June 6, 2023 15:07
@Yhg1s Yhg1s deleted the complex-pow branch November 22, 2023 15:51
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.

8 participants
0