Skip to content

primitives seem to be passed via reference instead of by value under some conditions when JIT is enabled on windows #11917

Closed

Description

@terrafrost

Description

The following code:

$a = [2147483647,2147483647,2147483647,3,0,0,32,2147483584,127];
echo crc32(json_encode(bitwise_small_split($a))) . "\n";
echo crc32(json_encode(bitwise_small_split($a))) . "\n";
echo crc32(json_encode(bitwise_small_split($a))) . "\n";
echo crc32(json_encode(bitwise_small_split($a))) . "\n";

function bitwise_small_split($val)
{
    $split = 8;
    $vals = [];

    $mask = (1 << $split) - 1;

    $i = $overflow = 0;
    $len = count($val);
    $val[] = 0;
    $remaining = 31;

    while ($i != $len) {
        $digit = $val[$i] & $mask;

        $val[$i] >>= $split;
        if (!$overflow) {
            $remaining -= $split;
            $overflow = $split <= $remaining ? 0 : $split - $remaining;

            if (!$remaining) {
                $i++;
                $remaining = 31;
                $overflow = 0;
            }
        } elseif (++$i != $len) {
            $tempmask = (1 << $overflow) - 1;
            $digit |= ($val[$i] & $tempmask) << $remaining;
            $val[$i] >>= $overflow;
            $remaining = 31 - $overflow;
            $overflow = $split <= $remaining ? 0 : $split - $remaining;
        }

        $vals[] = $digit;
    }

    while ($vals[count($vals) - 1] == 0) {
        unset($vals[count($vals) - 1]);
    }

    return array_reverse($vals);
}

With this php.ini:

zend_extension = C:/php/ext/php_opcache.dll

[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=100000
opcache.jit_buffer_size=128M
opcache.jit=tracing

Resulted in this output:

48207660
48207660
1497456976
1497456976

But I expected this output instead:

48207660
48207660
48207660
48207660

I get the expected output when I set opcache.enable to 0 in the php.ini but not when it's set to 1.

Note that sometimes I need to run the PHP file twice before the issue starts appearing. Also, this appears to only be a problem on Windows as I was unable to duplicate the issue with https://github.com/phpseclib/docker-php/tree/8.2jit

PHP Version

PHP 8.2.9

Operating System

Windows 11

Activity

  1. changed the title [-]primitives seem to be passed via reference instead of by value under some conditions when opcache is enabled[/-] [+]primitives seem to be passed via reference instead of by value under some conditions when opcache is enabled on windows[/+] on Aug 9, 2023
  2. ndossche commented on Aug 9, 2023

    @ndossche
    Member

    What happens if you enable opcache and only disable JIT? I suspect the real issue might be in the JIT instead of opcache.

  3. changed the title [-]primitives seem to be passed via reference instead of by value under some conditions when opcache is enabled on windows[/-] [+]primitives seem to be passed via reference instead of by value under some conditions when JIT is enabled on windows[/+] on Aug 9, 2023
  4. terrafrost commented on Aug 9, 2023

    @terrafrost
    Author

    @nielsdos - good call. The following php.ini does not demonstrate the problem:

    [opcache]
    opcache.enable=1
    opcache.enable_cli=1
    

    I've updated my post accordingly.

  5. gmutinel commented on Sep 19, 2023

    @gmutinel

    Same thing with 8.1.23 on Win11, had to force jit off with

    opcache.jit=off

  6. parallels999 commented on Sep 19, 2023

    @parallels999

    Same thing with 8.1.23 on Win11, had to force jit off with

    opcache.jit=off

    Me too, on Win 10 with 8.2

  7. added 2 commits that reference this issue on Sep 20, 2023
  8. dstogov commented on Oct 16, 2023

    @dstogov
    Member

    I can't reproduce the problem on 8.1.26-dev
    @nielsdos can you reproduce?

  9. danog commented on Oct 16, 2023

    @danog
    Contributor

    @dstogov the issue is still present on windows after rebasing on the latest PHP-8.1: https://github.com/php/php-src/actions/runs/6535551162/job/17745266902?pr=12269

  10. ndossche commented on Oct 16, 2023

    @ndossche
    Member

    @dstogov I can only reproduce it on Windows, not Linux. Reproduces with danog@d788e4f reliably. Bug only occurs when using tracing JIT with optimization level >= 2. Register allocation settings in JIT don't have an influence.

  11. added 4 commits that reference this issue on Oct 16, 2023
    0edc918
    89eb1c6
    ef91794
    a831cbc
  12. added a commit that references this issue on Oct 16, 2023
    221b4fe
  13. added a commit that references this issue on Oct 17, 2023
    4dcf063
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions