php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79125 FFI lifetime
Submitted: 2020-01-15 19:11 UTC Modified: 2020-01-16 11:51 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: php at tim dot ainfach dot de Assigned:
Status: Verified Package: *Extensibility Functions
PHP Version: 7.4.2RC1 OS: OSX 10.14.6
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at tim dot ainfach dot de
New email:
PHP Version: OS:

 

 [2020-01-15 19:11 UTC] php at tim dot ainfach dot de
Description:
------------
returning and directly using the result of a \FFI::cdef() function ends up in a void pointer.


php-src/ext/ffi/ffi.c:1990: zend_ffi_cdata_get_debug_info: Assertion `0' failed.
https://github.com/php/php-src/blob/9d7e03c325473024e54c864f0379efc1bbf03e72/ext/ffi/ffi.c#L1990


it's easy to reproduct with the same structs used here:

https://github.com/php/php-src/commit/05f3cd23ed61d800a861f2dd057ed56e783ea6f1#diff-3ea95b4dc57e34c882215bcc36f84551


i am not sure if this is a bug or a very consusing api limitation related to the livetime of zvals.




Test script:
---------------
// php code that SEGFAULTS
function ffi()
{
    return \FFI::cdef(
        file_get_contents(__DIR__ . '/example.h'),
        __DIR__ . '/lib.so'
    );
}

$res = ffi()->bug79096();
var_dump($res);


// php code that WORKS

// same as above
$ffi = ffi();
$res = $ffi->bug79096();

// c code
struct bug79096 bug79096(void)
{
  struct bug79096 b;

  b.a = 1;
  b.b = 1;
  return b;
}

// header 
struct bug79096 {
	uint64_t a;
	uint64_t b;
};

struct bug79096 bug79096(void);


Expected result:
----------------
object(FFI\CData:struct bug79096)#9 (2) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(1)
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-15 19:27 UTC] php at tim dot ainfach dot de
-Summary: FFI Struct Segfault +Summary: FFI lifetime
 [2020-01-15 19:27 UTC] php at tim dot ainfach dot de
sorry for the misleading title, seems that i cant change it? "FFI lifetime" would be much better.
 [2020-01-16 11:51 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2020-01-16 11:51 UTC] cmb@php.net
Indeed, after `::bug79096()` has been called, the FFI instance is
destroyed including all its types, although the FFI\Cdata instance
still holds a pointer to the `struct bug79096` type.  This results
in a use-after-free.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Oct 01 05:01:24 2020 UTC