Skip to content

Fix fallback paths in fast_long_{add,sub}_function #17666

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

Closed
wants to merge 3 commits into from

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Feb 1, 2025

This was asked to be checked in #17472 (comment)

There are 2 issues:

  1. The add/sub in the if can cause integer overflow UB warning, and can be fixed by using zend_ulong
    for the add/sub.
  2. fast_long_sub_function() has a problem when result aliases.
    This is fixed in the same way as fast_long_add_function() works.

This was asked to be checked in php#17472 (comment)

There are 3 issues:
1) The constant LONG_SIGN_MASK is wrong. Commit 98df5c9 changed this to
   avoid UB but did this incorrectly. The right fix was using
   zend_ulong.
2) The UB in the if can overflow, and can be fixed by using zend_ulong
   for the sum/sub.
3) fast_long_sub_function() has a problem when result aliases.
   This is fixed in the same way as fast_long_add_function() works.
@nielsdos nielsdos marked this pull request as ready for review February 2, 2025 10:06
@nielsdos
Copy link
Member Author

nielsdos commented Feb 2, 2025

Test failures in LINUX_X32_DEBUG_ZTS and ci/circleci: arm are unrelated: this is because of a libicu update that changes one ext/intl's test output.

@nielsdos nielsdos requested review from iluuu1994 and dstogov February 2, 2025 10:07
@cmb69
Copy link
Member

cmb69 commented Feb 2, 2025

Test failures in LINUX_X32_DEBUG_ZTS and ci/circleci: arm are unrelated: this is because of a libicu update that changes one ext/intl's test output.

See #17669.

@cmb69
Copy link
Member

cmb69 commented Feb 2, 2025

No ABI break here.

@cmb69 cmb69 removed the ABI break label Feb 2, 2025
Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

The changes look correct to me. Thanks @nielsdos!

@@ -49,7 +49,7 @@
#include "zend_multiply.h"
#include "zend_object_handlers.h"

#define LONG_SIGN_MASK ZEND_LONG_MIN
#define LONG_SIGN_MASK (((zend_ulong)1) << (8*sizeof(zend_long)-1))
Copy link
Member

Choose a reason for hiding this comment

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

The spec is complicated, but I'm not sure this is actually undefined behavior. FWIU, & is well defined for both signed and unsigned integers. In the case of two integers with the same conversion rank, the signed integer will be converted to an unsigned one.

In any case, I find the new and explicit solution a bit more obvious.

Copy link
Member Author

Choose a reason for hiding this comment

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

The old code (((zend_long)1) << (8*sizeof(zend_long)-1)) (note zend_ulong->zend_long next to the 1) was UB. UBSAN complained at this computation.

Copy link
Member

@iluuu1994 iluuu1994 Feb 2, 2025

Choose a reason for hiding this comment

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

The old yes, but the new doesn't. ZEND_LONG_MIN should be equivalent to only the signed bit being set. The only difference between the old and new solution is the signedness, if I understand correctly. Anyway, this looks fine, this wasn't meant as an argument, just trying to understand. 🙂

Edit: I guess ZEND_LONG_MIN is only correct for 2's complement, which is the only binary representation in practice. I'm not sure if this is the only case where we rely on 2's complement, but as mentioned, the new solution is fine too.

Copy link
Member Author

@nielsdos nielsdos Feb 2, 2025

Choose a reason for hiding this comment

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

Ah right, I guess I mixed up ZEND_LONG_MIN with 32/64 bit longs... I'll change that back (to keep the diff as small as possible).

Copy link
Member

@iluuu1994 iluuu1994 Feb 2, 2025

Choose a reason for hiding this comment

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

In theory, your new solution is better because it supports all integer representations supported by C. In practice, it likely doesn't matter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe so. However, we rely on 2s complement in a lot of other places, can you even get a 1s complement system somewhere nowadays?

Copy link
Member

Choose a reason for hiding this comment

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

Absolutely no clue. 🙂

Copy link
Member

Choose a reason for hiding this comment

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

We could add a respective check to configure.ac.

ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
} else {
ZVAL_LONG(result, Z_LVAL_P(op1) - Z_LVAL_P(op2));
Copy link
Member

Choose a reason for hiding this comment

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

I guess we're not actually testing this branch? Probably would only be used on some pretty esoteric platforms.

Copy link
Member

Choose a reason for hiding this comment

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

I guess we're not actually testing this branch?

My bad (#17472) ;)

Probably would only be used on some pretty esoteric platforms.

Possibly not even that. Would require a compiler without ASM goto support, no builtins (SADDL etc.) and non Windows system. Plus, that compiler would need to support C11 (or at least the subset we're using).

Copy link
Member

Choose a reason for hiding this comment

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

It's better to perform the integer math once, keeping the result in a temporary variable.
Otherwise C compiler may generate longer fast path code.

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks Niels!

ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
} else {
ZVAL_LONG(result, Z_LVAL_P(op1) - Z_LVAL_P(op2));
Copy link
Member

Choose a reason for hiding this comment

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

It's better to perform the integer math once, keeping the result in a temporary variable.
Otherwise C compiler may generate longer fast path code.

@nielsdos nielsdos closed this in 7e06a81 Feb 3, 2025
charmitro pushed a commit to wasix-org/php that referenced this pull request Mar 13, 2025
This was asked to be checked in php#17472 (comment)

There are 2 issues:
1) The UB in the if can overflow, and can be fixed by using zend_ulong
   for the sum/sub.
2) fast_long_sub_function() has a problem when result aliases.
   This is fixed in the same way as fast_long_add_function() works.

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

Successfully merging this pull request may close these issues.

4 participants