Repository navigation
php: every multiplication over 2**32 was silently a float - #732
Merged
Merged
Conversation
exit() is fixed -- the smoke test runs to completion and its exit status is usable again. The two cases left were both `-9223372036854775807 - 1' coming back a float while PHP_INT_MIN itself was a clean int. The subtraction was innocent, and the compiler said so: the host kencc compiled php's real zend_operators.c and fast_long_sub_function is SUBQ, ANDQ with the sign mask in a register, CMPQ -- 64 bits throughout, and tracing it with 0x8000000000000001 and 1 takes the integer arm. Reading the generated code of the real function beat reasoning about which #elif arm the config selects. PHP compiles unary minus as (-1) * x, so the float came from the multiply. zend_multiply.h:132's fallback -- the arm taken with no __builtin_*_overflow, no __GNUC__ asm and no Win32, which is exactly this port -- opens long __lres = (a) * (b); and every other arm in that file is guarded by SIZEOF_LONG == SIZEOF_ZEND_LONG or SIZEOF_ZEND_LONG == 4. This one assumes it. long is 32 bits here and zend_long is 64, so __lres held the product's low half, the delta test against __dres reported an overflow that had not happened, and the result was a double. So every integer multiplication whose product did not fit in 32 bits silently returned a float: 2000000 * 2000000 was float(4.0E+12). A wrong answer in the commonest arithmetic there is, with no diagnostic, reaching every php program run on this port. zend_long __lres now. The before/after -S diff is four lines: IMULQ into a register that was then MOVQL'd to 32 bits and CVTSL2SD'd, against IMULQ kept whole and CVTSQ2SD. 515 TUs still compile. The test needed correcting too. Its first version covered +, -, max, strlen and str_repeat and not `*', so it caught this only sideways through unary minus, and nothing in the failing output said the word multiply. Section 3b asks directly now and includes two cases that a fix which simply never reports an overflow would fail: PHP_INT_MAX * 2 and PHP_INT_MIN * 2 must still be floats. 0 failures on the host. Second round running in which the tree's most-cited invariant -- long is 32 bits on amd64 -- arrives inside upstream code that is correct on every LP64 platform. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs
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.
exit() is fixed -- the smoke test runs to completion and its exit status is usable again. The two cases left were both `-9223372036854775807 - 1' coming back a float while PHP_INT_MIN itself was a clean int.
The subtraction was innocent, and the compiler said so: the host kencc compiled php's real zend_operators.c and fast_long_sub_function is SUBQ, ANDQ with the sign mask in a register, CMPQ -- 64 bits throughout, and tracing it with 0x8000000000000001 and 1 takes the integer arm. Reading the generated code of the real function beat reasoning about which #elif arm the config selects.
PHP compiles unary minus as (-1) * x, so the float came from the multiply. zend_multiply.h:132's fallback -- the arm taken with no _builtin*_overflow, no GNUC asm and no Win32, which is exactly this port -- opens
and every other arm in that file is guarded by
SIZEOF_LONG == SIZEOF_ZEND_LONG or SIZEOF_ZEND_LONG == 4. This one assumes it. long is 32 bits here and zend_long is 64, so __lres held the product's low half, the delta test against __dres reported an overflow that had not happened, and the result was a double.
So every integer multiplication whose product did not fit in 32 bits silently returned a float: 2000000 * 2000000 was float(4.0E+12). A wrong answer in the commonest arithmetic there is, with no diagnostic, reaching every php program run on this port.
zend_long __lres now. The before/after -S diff is four lines: IMULQ into a register that was then MOVQL'd to 32 bits and CVTSL2SD'd, against IMULQ kept whole and CVTSQ2SD. 515 TUs still compile.
The test needed correcting too. Its first version covered +, -, max, strlen and str_repeat and not `*', so it caught this only sideways through unary minus, and nothing in the failing output said the word multiply. Section 3b asks directly now and includes two cases that a fix which simply never reports an overflow would fail: PHP_INT_MAX * 2 and PHP_INT_MIN * 2 must still be floats. 0 failures on the host.
Second round running in which the tree's most-cited invariant -- long is 32 bits on amd64 -- arrives inside upstream code that is correct on every LP64 platform.
Claude-Session: https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs