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

Wrong result due to integer overflow (in pynac?) #31585

Closed
DaveWitteMorris opened this issue Mar 31, 2021 · 52 comments
Closed

Wrong result due to integer overflow (in pynac?) #31585

DaveWitteMorris opened this issue Mar 31, 2021 · 52 comments

Comments

@DaveWitteMorris
Copy link
Member

Volker Braun pointed out in #31479 comment:12 that sage gives the following wrong answer on a 32-bit machine after the bug-fix patch of #31479 is applied:

sage: a,b,c,d = var("a b c d")
sage: ((a + b + c)^30 * (3*b + d - 5/d)^3).expand().subs(a=0,b=2,c=-1)
d^3 + 18*d^2 + 1739461754973*d - 8697308774865/d + 450/d^2 - 125/d^3 + 36

This answer is congruent modulo 2^32 to d^3 + 18*d^2 + 93*d - 465/d + 450/d^2 - 125/d^3 + 36, which is the correct answer, so it seems clear that the problem comes from an integer overflow error. Perhaps the overflow error can also be produced on a 64-bit machine.

Depends on #31694

Component: symbolics

Keywords: integer overflow, pynac

Author: Dave Morris, Matthias Koeppe

Branch/Commit: eba0a9c

Reviewer: Dima Pasechnik

Issue created by migration from https://trac.sagemath.org/ticket/31585

@mkoeppe
Copy link
Contributor

mkoeppe commented May 10, 2021

comment:1

Moving to 9.4, as 9.3 has been released.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 May 10, 2021
@DaveWitteMorris
Copy link
Member Author

comment:2

I reproduced the bug by installing sage in a 32-bit debian virtual machine, and found a much simpler example of the overflow:

sage: (2*x).subs(x=-2^31)
0

More generally:

sage: for n in range(10):
....:     print(n, (n*x).subs(x=-2^31)/2^31)
0 0
1 -1
2 0
3 -1
4 0
5 -1
6 0
7 -1
8 0
9 -1

@DaveWitteMorris
Copy link
Member Author

Author: Dave Morris

@DaveWitteMorris
Copy link
Member Author

comment:3

I think the problem is an incorrect calculation of absolute value:

sage: abs(x).subs(x=-2^31)
-2147483648

To decide whether a multiplication might cause an overflow, pynac compares the absolute values of the factors to a certain bound. In the example of comment:2, the absolute value of a factor is negative, so it is less than the bound, even though the factor is actually very large.

And I think the incorrect absolute value comes from the C++ std::labs function. The documentation of that function says "If the result cannot be represented as a long int (such as labs(LONG_MIN) in an implementation with two's complement signed values), it causes undefined behavior."

I should be able to write a pynac patch soon that tests my theory. Even if it isn't the bug we are looking for, this bug in the absolute value needs to be fixed.

@DaveWitteMorris
Copy link
Member Author

comment:4

The same problem comes up in pynac's numeric::negative method, so that will need to be fixed too:

sage: (-x).subs(x=-2^31)
-2147483648

@DaveWitteMorris
Copy link
Member Author

comment:5

Here is a crash that is caused by the overflow when -2^31 is divided by -1:

sage: (1 - 2^31*x)*x
    <snip>
Unhandled SIGFPE: An unhandled floating point exception occurred. ...

Patches to fix all of these should be coming soon.

@DaveWitteMorris
Copy link
Member Author

Branch: public/31585

@DaveWitteMorris
Copy link
Member Author

comment:7

The pynac bugs on this ticket do not currently affect 64-bit sage. (This is because pynac does not store -2^63 as a long integer, even though it could fit into that space on a 64-bit machine.) However, they will affect 64-bit machines after the branch on #31986 is merged. (See the explanation and examples there.) Hence, a 64-bit user can test the PR of this ticket by first merging the branch of #31986 (and executing sage -f pynac), and replacing -2^31 with -2^63, as in the examples in the description of #31986.

Followup tickets: #31984 and #31986.

I will try to post this (and my other pynac patches) to the pynac github repository soon. My previous attempts failed because I know almost nothing about git, but now I understand that I need a fork, instead of a clone, so I hope to be successful.


New commits:

f1a99fatrac 31585 pynac overflow patch
3fcf072add doctests
fdde3d9update pynac patch level

@DaveWitteMorris
Copy link
Member Author

Commit: fdde3d9

@DaveWitteMorris
Copy link
Member Author

comment:8

I think this pynac patch also fixes #25688.

@tscrim
Copy link
Collaborator

tscrim commented Jun 20, 2021

comment:9

Merge conflict

@DaveWitteMorris
Copy link
Member Author

Changed branch from public/31585 to public/31585r1

@DaveWitteMorris
Copy link
Member Author

comment:11

Rebased on 9.4b3.


New commits:

ecb99c8trac 31585 pynac overflow patch
2d04fa4add doctests
5a340ceupdate pynac patch level

@DaveWitteMorris
Copy link
Member Author

Changed commit from fdde3d9 to 5a340ce

@mkoeppe
Copy link
Contributor

mkoeppe commented Jun 28, 2021

comment:12

PR to pynac please

@DaveWitteMorris
Copy link
Member Author

comment:13

Yes, I will try.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 28, 2021

Changed commit from 5a340ce to 627ec10

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 28, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

627ec10also fix /=

@DaveWitteMorris
Copy link
Member Author

comment:15

I previously neglected to fix the /= operator, which can also overflow.

I created pynac PR #378. It's my first PR to anywhere other than the trac server, so please let me know if I goofed anything up.

@dimpase
Copy link
Member

dimpase commented Jun 28, 2021

comment:25

running GH Actions tests on this https://github.com/sagemath/sagetrac-mirror/actions/runs/978774922
(basically to test #31694)

@mkoeppe
Copy link
Contributor

mkoeppe commented Jun 29, 2021

comment:26

PR with standard-conforming code at pynac/pynac#379

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 29, 2021

Changed commit from a081808 to 5d89a50

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 29, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

5d89a50trying pynac PR 379

@dimpase
Copy link
Member

dimpase commented Jun 29, 2021

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 29, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

3c75f53correct patch suffix

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 29, 2021

Changed commit from 5d89a50 to 3c75f53

@dimpase
Copy link
Member

dimpase commented Jun 29, 2021

comment:31

update - https://github.com/sagemath/sagetrac-mirror/actions/runs/982441436


New commits:

3c75f53correct patch suffix

@mkoeppe
Copy link
Contributor

mkoeppe commented Jun 30, 2021

Changed branch from u/dimpase/31585r1 to u/mkoeppe/31585r1

@mkoeppe
Copy link
Contributor

mkoeppe commented Jun 30, 2021

Changed commit from 3c75f53 to a8e82dd

@mkoeppe
Copy link
Contributor

mkoeppe commented Jun 30, 2021

New commits:

c9c50e3build/pkgs/pynac: Upgrade to 0.7.29
f1d83c3Merge #31694
a8e82ddadd doctests - cf #31585

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 30, 2021

Changed commit from a8e82dd to eba0a9c

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 30, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

eba0a9cbuild/bin/write-dockerfile.sh: Disable the gfan testsuite (does not terminate on 32bit)

@mkoeppe
Copy link
Contributor

mkoeppe commented Jun 30, 2021

Changed author from Dave Morris to Dave Morris, Matthias Koeppe

@dimpase
Copy link
Member

dimpase commented Jun 30, 2021

@dimpase
Copy link
Member

dimpase commented Jul 1, 2021

comment:37

lgtm

@mkoeppe
Copy link
Contributor

mkoeppe commented Jul 1, 2021

comment:38

Thanks!

@vbraun
Copy link
Member

vbraun commented Jul 6, 2021

Changed branch from u/mkoeppe/31585r1 to eba0a9c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants