php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78745 Allow to define FFI callbacks with CData return type
Submitted: 2019-10-23 16:31 UTC Modified: 2019-11-07 10:32 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: lisachenko dot it at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *Extensibility Functions
PHP Version: 7.4Git-2019-10-23 (Git) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: lisachenko dot it at gmail dot com
New email:
PHP Version: OS:

 

 [2019-10-23 16:31 UTC] lisachenko dot it at gmail dot com
Description:
------------
FFI can be used to define a native PHP callback via Closure instance for traditional C function pointers (hooks) as described in https://www.php.net/manual/en/ffi.examples-callback.php

This works well for simple return types, like void, double, int, etc. But if a C callback has a complex C return-type/struct, then FFI can't assign a callback to it, failing with a message:

  Uncaught FFI\Exception: FFI internal error. Unsupported return type

It would be nice to have an additional CData type check for a PHP callback and perform some run-time checks to allow PHP hook even for that complex callback types.

Test script:
---------------
Here is a pseudo-code for the PHP zend_compile_string callback defined as following:

ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename);

        $php->zend_compile_string = function (CData $sourceString, CData $filename): CData {
            echo 'In the hook';
            $code = Core::new('zend_op_array[10]');
            // ... some opcode initialization logic

            return FFI::addr($code);
        };

Expected result:
----------------
No errors about unsupported return type

Actual result:
--------------
Fatal error: Uncaught FFI\Exception: FFI internal error. Unsupported return type

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-24 07:31 UTC] lisachenko dot it at gmail dot com
This issue was resolved by Dmitry in https://github.com/php/php-src/commit/c744531fff9ee03c027ca3c18b21f3382023ff7e

Should be closed after recheck.
 [2019-11-07 10:32 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 02:01:28 2024 UTC