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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
21 - 3 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Mar 29 15:01:28 2024 UTC