Skip to content

[FFI] Segmentation fault when calling callback function #9214

Open
@fwflunky

Description

@fwflunky

Description

The following code:

<?php
class Test {
   public $lib;
   public function __construct() {
      $this->lib = \FFI::cdef("extern void setCloseSessionCallback(void(*fun)(unsigned long cid));
      extern void testCloseSessionCallback();
      ", "./libsomelib.so");
   }
}

$tclass = new Test();
$tclass->lib->setCloseSessionCallback(function($cid){
   echo "ok\n";
});

$tclass->lib->testCloseSessionCallback(); //this function creates new std::thread from c++ stl, sleeps 2 seconds and calls function passed to setCloseSessionCallback
while(true); //prevent php script from completion because testCloseSessionCallback running in other thread

Resulted in this output:

segmentation fault (core dumped)

But I expected this output instead:

ok

gdb backtrace:
image

PHP Version

PHP 8.0.21 (zts) with Zend OPcache v8.0.21

Operating System

Manjaro Linux

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