Req #77707 [NEW]: FFI loses type information for opaque types
| From: | ircmaxell@php.net | Date: | Thu, 07 Mar 2019 14:00:52 +0000 |
| Subject: | Req #77707 [NEW]: FFI loses type information for opaque types | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-219882@lists.php.net to get a copy of this message | ||
From: ircmaxell
Operating system: any
PHP version: Next Minor Version
Package: Unknown/Other Function
Bug Type: Feature/Change Request
Bug description:FFI loses type information for opaque types
Description:
------------
Currently, opaque types lose their name information in FFI. So when we
use an opaque pointer, such as provided by sqlite3 in the example below,
there's no way to know what pointer mismatch occurred, only that one
occurred.
If you look carefully at the actual result, see that the type name
"sqlite3_mutex" was stripped from the FFI\CData type information. So it
only shows an abstract <struct>* description.
Additionally, there's currently no way to go from a FFI\CType instance
back to the literal type definition used (in this case either "sqlite3*"
or "sqlite3_mutex*"). A method such as FFI\CType->typedef() which
returned the string type definition used (in this case, "sqlite3_mutex*"
or "sqlite3*").
Instead, name information should be retained to greatly aid in debugging
when using complex FFI code without needing to generate a typesafe
wrapper for FFI.
Test script:
---------------
<?php
$header = '
typedef struct sqlite3 sqlite3;
int sqlite3_open(const char*, sqlite3 **);
int sqlite3_close(sqlite3*);
const char* sqlite3_errmsg(sqlite3*);
typedef struct sqlite3_mutex sqlite3_mutex;
sqlite3_mutex *sqlite3_mutex_alloc(int);
void sqlite3_mutex_free(sqlite3_mutex*);
void sqlite3_mutex_enter(sqlite3_mutex*);
int sqlite3_mutex_try(sqlite3_mutex*);
void sqlite3_mutex_leave(sqlite3_mutex*);
';
$ffi = FFI::cdef($header, 'libsqlite3.so.0.8.6');
$db = $ffi->new('sqlite3*');
$result = $ffi->sqlite3_open("test.db", FFI::addr($db));
if ($result > 0) {
die("Can't open database: " . $ffi->sqlite3_errmsg($db));
}
$mutex = $ffi->sqlite3_mutex_alloc(2);
$ffi->sqlite3_close($mutex);
echo "Done\n";
Expected result:
----------------
Fatal error: Uncaught FFI\Exception: Passing incompatible pointer in
/.../test.php:33
Stack trace:
#0 /.../test.php(33):
FFI->sqlite3_close(Object(FFI\CData:sqlite3_mutex*))
#1 {main}
thrown in /.../test.php on line 33
Actual result:
--------------
Fatal error: Uncaught FFI\Exception: Passing incompatible pointer in
/.../test.php:33
Stack trace:
#0 /.../test.php(33): FFI->sqlite3_close(Object(FFI\CData:<struct>*))
#1 {main}
thrown in /.../test.php on line 33
--
Edit bug report at https://bugs.php.net/bug.php?id=77707&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77707&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77707&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77707&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=77707&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=77707&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=77707&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=77707&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=77707&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=77707&r=support
Expected behavior: https://bugs.php.net/fix.php?id=77707&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=77707&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=77707&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=77707&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77707&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=77707&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=77707&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=77707&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77707&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=77707&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=77707&r=mysqlcfg