FFI is one of the features that made Python and LuaJIT very useful for fast prototyping. It allows calling C functions and using C data types from pure scripting language and therefore develop “system code” more productively. For PHP, FFI opens a way to write PHP extensions and bindings to C libraries in pure PHP.
It is proposed to extend PHP with a simple FFI API designed after LuaJTI/FFI and Python/CFFI (the latter was actually based on former). This API allows loading shared libraries (.DLL or .so), calling C functions and accessing C data structures, in pure PHP, without having to have deep knowledge in the Zend extension API, and without having to learn a 3rd “intermediate” language.
The public API is implemented as a single class FFI with a few static methods (some of them may be called non-statically), and overloaded object methods, that perform actual interaction with C data. Before diving into the details of FFI API, lets take a look into few examples, demonstrating simplicity of FFI API usage for regular tasks.
<?php // create FFI object, loading libc and exporting function printf() $ffi = FFI::cdef( "int printf(const char *format, ...);", // this is regular C declaration "libc.so.6"); // call C printf() $ffi->printf("Hello %s!\n", "world");
<?php // create gettimeofday() binding $ffi = FFI::cdef(" typedef unsigned int time_t; typedef unsigned int suseconds_t; struct timeval { time_t tv_sec; suseconds_t tv_usec; }; struct timezone { int tz_minuteswest; int tz_dsttime; }; int gettimeofday(struct timeval *tv, struct timezone *tz); ", "libc.so.6"); // create C data structures $tv = $ffi->new("struct timeval"); $tz = $ffi->new("struct timezone"); // calls C gettimeofday() var_dump($ffi->gettimeofday(FFI::addr($tv), FFI::addr($tz))); // access field of C data structure var_dump($tv->tv_sec); // print the whole C data structure var_dump($tz);
<?php // create FFI object, loading libc and exporting errno variable $ffi = FFI::cdef("int errno;", // this is regular C declaration "libc.so.6"); // print C errno var_dump($ffi->errno);
<?php // create C data structure $a = FFI::new("unsigned char[1024*1024]"); // work with it like with regular PHP array for ($i = 0; $i < count($a); $i++) { $a[$i] = $i; } var_dump($a[25]); $sum = 0; foreach ($a as $n) { $sum += $n; } var_dump($sum); var_dump(FFI::sizeof($a));
Creates a new FFI object. The first optional argument is a string, containing a sequence of declarations in regular C languages (types, structures, functions, variables, etc). Actually, this string may be copy-pasted from C header files. The second optional argument is a shared library file name, to be loaded and linked with definitions. All the declared entities are going to be available to PHP through overloaded functions or other FFI API functions:
Note: At this time we don't support C preprocessor directives. #include, #define and CPP macros won't work.
Creates native data structure of given C type. $type may be any valid C string declaration or an instance of FFI\CType created before. Using the second argument, it's possible to create owned data (default), or unmanaged. In the first case, data structure is going to live together with returned FFI\CData object, and die when last reference is released by regular PHP reference counting or GC. However, in some cases, programmer may decide to keep C data even after, releasing of FFI\CData object and manually free it through FFI::free() similar to regular C. By default, the memory for the data is allocated on PHP request heap (using emalloc()), but it's also possible to use system heap, specifying true in the third argument.
This function may be called statically or as a method of previously created FFI object. In the first case, it may use only predefine C type names (e.g int, char, etc), and in the second, any type declared in the string passed to FFI::cdef() or file passed to FFI::load().
The returned FFI\CData object may be used in a number of ways as a regular PHP data
$x = FFI::new(“int”); $x = 42;
$cdata->field
$cdata[$offset]
$cdata[0]
$cdata += 5
$cdata()
$cdata2 = clone $cdata;
Manually releases a previously created “not-owned” data structure.
Performs C type cast. It creates a new FFI\CData object, that references the same C data structure, but associated with different type. The resulting object doesn't own the C data, and the source $cdata must relive the result. C type may be specified as a string with any valid C type declaration or FFI\CType object, created before.
This function may be called statically or as a method of previously created FFI object. In the first case, it may use only predefine C type names (e.g int, char, etc), and in the second, any type declared in the string passed to FFI::cdef() or file passed to FFI::load()..
Creates a not owned pointer to the C data represented by given FFI\CData. The source $data must relive the resulting pointer. This function is mainly useful to pass arguments of C functions by pointer.
This function creates and returns a FFI\CType object for the given string containing C type declaration.
This function may be called statically or as a method of previously created FFI object. In the first case, it may use only predefine C type names (e.g int, char, etc), and in the second, any type declared in the string passed to FFI::cdef() or file passed to FFI::load()..
Dynamically constructs a new C array type with elements of type defined by the first argument and dimensions specified by the second. In the following example $t1 and $t2 are equivalent array types.
$t1 = FFI::type("int[2][3]"); $t2 = FFI::arrayType(FFI::type("int"), [2, 3]);
This function returns a FFI\CType object, representing the type of the given FFI\CData object.
Returns size of the given FFI\CData or FFI\CType
Returns alignment of the given FFI\CData or FFI\CType
Copies $size bytes from memory area $src to memory area $dst. $src may be any native data structure (FFI\CData) or PHP string.
Compares $size bytes from memory area $src1 and $dst2. Both $src1 and $src2 may be any native data structures (FFI\CData) or PHP strings.
Fills the $size bytes of the memory area pointed to by $dst with the given byte $c
Creates a PHP string from $size bytes of the memory area pointed by $src. If size is omitted, $src must be a zero terminated array of C chars.
In addition to ability of embedding C declaration code into FFI::cdef(), it's also possible to load C declarations from separate C header file.
Note: C preprocessor directives are currently not supported. #include, #define and CPP macros don't work.
It's possible to specify shared libraries, that should be loaded, using special FFI_LIB define in the loaded C header file.
FFI definition parsing and shared library loading may take significant time. It's not useful to do it on each HTTP request in a Web environment. However, it's possible to preload FFI definitions and libraries at PHP startup, and instantiate FFI objects when necessary. Header files may be extended with special FFI_SCOPE #define (e.g. #define FFI_SCOPE “foo”, the default scope is “C”) and then loaded by FFI::load() during preloading. This leads to creation of persistent binding, that will be available to all the following requests through FFI::scope(). Please see the sample below for an example.
It's possible to preload more than one C header file into the same scope.
This function may be used to instantiate FFI object, containing C declarations parsed during preloading.
It's possible to assign PHP closure to native variable of function pointer type (or pass it as a function argument).
$zend = FFI::cdef(" typedef int (*zend_write_func_t)(const char *str, size_t str_length); extern zend_write_func_t zend_write; "); echo "Hello World 1!\n"; $orig_zend_write = clone $zend->zend_write; $zend->zend_write = function($str, $len) { global $orig_zend_write; $orig_zend_write("{\n\t", 3); $ret = $orig_zend_write($str, $len); $orig_zend_write("}\n", 2); return $ret; }; echo "Hello World 2!\n"; $zend->zend_write = $orig_zend_write; echo "Hello World 3!\n";
Hello World 1! { Hello World 2! } Hello World 3!
This works, but this functionality is not supported on all libffi platforms, it is not efficient and leaks resources by the end of request. It's recommended to minimize the usage of PHP callbacks.
FFI API opens all the C power, and consequently, also an enormous possibility to have something go wrong, crash PHP, or even worse. To minimize risk PHP FFI API usage may be restricted. By default FFI API may be used only in CLI scripts and preloaded PHP files. This may be changed through ffi.enable INI directive. This is INI_SYSTEM directive and it's value can't be changed at run-time.
PHP FFI API restriction makes effect only to FFI class, but not to overloaded functions of FFI\CData object. This means, it's possible to create some FFI\CData objects in preloaded files, and then use them directly in “user” code.
php.ini
ffi.enable=preload opcache.preload=preload.php
preload.php
<?php FFI::load(__DIR__ . "/dummy.h"); opcache_compile_file(__DIR__ . "/dummy.php");
dummy.h
#define FFI_SCOPE "DUMMY" #define FFI_LIB "libc.so.6" int printf(const char *format, ...);
dummy.php
<?php final class Dummy { private static $ffi = null; function __construct() { if (is_null(self::$ffi)) { self::$ffi = FFI::scope("DUMMY"); } } function printf($format, ...$args) { return (int)self::$ffi->printf($format, ...$args); } }
test.php
<?php $d = new Dummy(); $d->printf("Hello %s!\n", "world");
Accessing FFI data structures is significantly (about 2 times) slower, than accessing native PHP arrays and objects. It makes no sense to use them for speed, but may make sense to reduce memory consumption. This is true for all similar FFI implementations in interpretative mode. However, LuaJIT achieves improvement providing special support for FFI in its JIT.
The following table shows time of execution of ary3 benchmark from bench.php (in seconds, lower is better).
<?php function ary3($n, bool $use_ffi = false) { if ($use_ffi) { $X = FFI::new("int[$n]"); $Y = FFI::new("int[$n]"); } for ($i=0; $i<$n; $i++) { $X[$i] = $i + 1; $Y[$i] = 0; } for ($k=0; $k<1000; $k++) { for ($i=$n-1; $i>=0; $i--) { $Y[$i] += $X[$i]; } } $last = $n-1; print "$Y[0] $Y[$last]\n"; }
Native | FFI | |
---|---|---|
Python | 0.212 | 0.343 |
PyPy | 0.010 | 0.081 |
LuaJit -joff | 0.037 | 0.412 |
LuaJit -jon | 0.003 | 0.002 |
PHP | 0.040 | 0.093 |
PHP + jit | 0.016 | 0.087 |
None, except of introduced FFI class and namespace.
PHP 7.4
FFI is designed in conjunction with preloading (curently implemented as part of opcache). FFI C headers may be loaded during preloading by FFI::load() and become available to all the following HTTP requests without reloading overhead.
ffi.enable=false|preload|true
allows enabling or disabling FFI API usage, or restricting it only to preloaded files. The default value is preload. This is INI_SYSTEM directive and it's value can't be changed at run-time.
Make sure there are no open issues when the vote starts!
There were few other attempts to implement FFI for PHP.
The usability of this FFI extension was proved by TensorFlow binding, implemented in pure PHP.
Currently, the performance of C data structures access is worse than access of native PHP data structures (arrays and objects). This is a common problem, and both LuaJIT (in interpretator mode) and Python suffer from it as well. However, LuaJIT may also compile data access code in very efficient way (almost as C compiler), and produce highly efficient machine code. It's planned to try similar things, when we implement JIT for PHP.
Include FFI extension into PHP-7.4 (bundle) This project requires 50%+1 majority The voting started 2018-12-20 and will close on 2019-01-09
https://github.com/dstogov/php-ffi implementation on top of libffi (tested on Linux and Windows)
After the project is implemented, this section should contain
Keep this updated with features that were discussed on the mail lists.