krakjoe / parallel

A succinct parallel concurrency API for PHP8
Other
1.45k stars 95 forks source link

Segmentation fault for special syntax #219

Open ziaratban opened 2 years ago

ziaratban commented 2 years ago

Hi

a.php

<?php
require 'b.php'; // comment this line for fix
require 'c.php';

b.php

<?php
function t(){
    strpos('',''); // or comment this line for fix
}

c.php

$r = new \parallel\Runtime();
$r->run(static function(){
    require 'b.php';
    t();
});
root# php a.php
Sep 16 12:11:47 server1 kernel: php[1744]: segfault at 10 ip 0000000000000010 sp 000014a4d679ea78 error 14
Sep 16 12:11:47 server1 kernel: Code: Unable to access opcode bytes at RIP 0xffffffffffffffe6.
root# php -v
PHP 8.0.10 (cli) (built: Sep 16 2021 12:16:25) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies
ziaratban commented 2 years ago

Other behavior : edit c.php

$r = new \parallel\Runtime();
$r->run(static function(){
    substr('',''); // <--- added
    require 'b.php';
    t();
});

output :

Fatal error: Uncaught TypeError: substr(): Argument #2 ($offset) must be of type int, string given in /root/b.php:3
Stack trace:
#0 /root/b.php(3): substr('', '')
#1 /root/c.php(6): t()
#2 {main}
  thrown in /root/b.php on line 3
ziaratban commented 2 years ago

Fixed

edit b.php


$r = new \parallel\Runtime();
$r->run(static function(){
    opcache_invalidate('b.php'); // <--- added
    require 'b.php';
    t();
});
realFlowControl commented 1 month ago

Hey @ziaratban,

sorry for radio silence. First of all: thanks for this nice and easy reproducer! I gave it a quick try and was able to reproduce this with OPcache, but not without OPcache. I am pretty new to parallel, but I am not sure if require/include are actually supported, another way to fix this is to include b.php not using require/include, but using the bootstrap mechanism in b.php like this:

<?php
$r = new \parallel\Runtime('b.php');
$r->run(static function(){
    t();
});

This fixes it at leas for me on:

$ php -v
PHP 8.3.11-dev (cli) (built: Jul 23 2024 20:50:22) (ZTS)
Copyright (c) The PHP Group
Zend Engine v4.3.11-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.11-dev, Copyright (c), by Zend Technologies