php-src icon indicating copy to clipboard operation
php-src copied to clipboard

FFI struct with flexible array member

Open userqq opened this issue 4 years ago • 3 comments

Description

The following code:

<?php

echo 'Creating FFI struct...' . PHP_EOL;
$value = FFI::new('struct {
    int32_t length;
    char data[]; 
}');

echo 'Our struct is:' . PHP_EOL;
var_dump($value);
echo 'Our struct->data is:' . PHP_EOL;
var_dump($value->data);

echo 'Please keep in memory following will not work:' . PHP_EOL;
try {
    $value->data = FFI::new('char[4]');
} catch (Throwable $t) {
    echo '    - ' . $t->getMessage() . PHP_EOL;
}

echo PHP_EOL . 'Let\'s try something dirty:' . PHP_EOL;
var_dump($value->data[0]); // Well, it's ok, FFI is unsafe
var_dump($value->data[1000]); // No problem at all, we should carefully check boundaries 

echo PHP_EOL . 'And even more dirtier:' . PHP_EOL;
$value->data[0] = 'a';
var_dump($value);
var_dump($value->data[0]);
echo 'I think I could broke something.' . PHP_EOL;

echo PHP_EOL . 'Let\'s fill our struct with value: ' . PHP_EOL;
FFI::memcpy($value, pack('V', 5), 4);
var_dump($value);

echo PHP_EOL . 'Will it work for array member? ' . PHP_EOL;
try {
    FFI::memcpy($value, pack('V', 5) . 'aaaa', 8);
    var_dump($value);
} catch (Throwable $t) {
    echo '    - ' . $t->getMessage() . PHP_EOL;
    echo 'Seems like it won\'t.' . PHP_EOL;
}

echo PHP_EOL . 'Maybe I can assign data directly to array member? ' . PHP_EOL;
try {
    FFI::memcpy($value->data, 'aaaa', 4);
    var_dump($value);
} catch (Throwable $t) {
    echo '    - ' . $t->getMessage() . PHP_EOL;
    echo 'Doesn\'t work either.' . PHP_EOL;
}

echo PHP_EOL . 'Or maybe I can assign to array member itself? ' . PHP_EOL;
try {
    $value->data = FFI::new('char[4]');
    var_dump($value);
} catch (Throwable) {
    echo '    - ' . $t->getMessage() . PHP_EOL;
}
echo 'Do you remember last time we did this we got different error: ' . PHP_EOL;
echo '    - It was "Incompatible types when assigning to type \'char[]\' from type \'char[4]\'"' . PHP_EOL;

Resulted in this output:

Creating FFI struct...
Our struct is:
object(FFI\CData:struct <anonymous>)#1 (2) {
  ["length"]=>
  int(0)
  ["data"]=>
  object(FFI\CData:char[])#2 (0) {
  }
}
Our struct->data is:
object(FFI\CData:char[])#2 (0) {
}
Please keep in memory following will not work:
    - Incompatible types when assigning to type 'char[]' from type 'char[4]'

Let's try something dirty:
string(1) "▒"
string(1) "▒"

And even more dirtier:
object(FFI\CData:struct <anonymous>)#1 (2) {
  ["length"]=>
  int(0)
  ["data"]=>
  object(FFI\CData:char[])#2 (0) {
  }
}
string(1) "a"
I think I could broke something.

Let's fill our struct with value:
object(FFI\CData:struct <anonymous>)#1 (2) {
  ["length"]=>
  int(5)
  ["data"]=>
  object(FFI\CData:char[])#2 (0) {
  }
}

Will it work for array member?
    - Attempt to write over data boundary
Seems like it won't.

Maybe I can assign data directly to array member?
    - Attempt to write over data boundary
Doesn't work either.

Or maybe I can assign to array member itself?
    - Attempt to write over data boundary
Do you remember last time we did this we got different error, it was:
    - Incompatible types when assigning to type 'char[]' from type 'char[4]'

I did not expect something different, but it seems strange to me - flexible array members in structs are allowed, but I can't find the way how to use them.

PHP Version

PHP 8.1.1 (cli) NTS

Operating System

No response

userqq avatar Jan 15 '22 21:01 userqq

There is actually support for flexible array members (and the struct hack char[0]), but the programmer is responsible to allocate a proper struct (FFI::new() can't do that). Assuming you have such a properly allocated struct, the following works:

$value->data[100] = "a";
var_dump($value->data[100]); // "a"

This is explicitly supported due to https://github.com/php/php-src/blob/655e578e816e6eec767b25963e93d8e044a9f392/ext/ffi/ffi.c#L1345

The fact that FFI::memcpy() does not work, looks like a bug to me. I think there should be an exception for ZEND_FFI_TYPE_ARRAY with type1->size == 0 at https://github.com/php/php-src/blob/655e578e816e6eec767b25963e93d8e044a9f392/ext/ffi/ffi.c#L4245

cmb69 avatar Jan 16 '22 18:01 cmb69

but the programmer is responsible to allocate a proper struct

I finally got it. Such struct could be allocated in foreign function, like:

$lib = FFI::cdef("
    typedef struct Data {
        int32_t size;
        char data[];
    } Data;

    Data createData();
", 'lib.so');

FFI::new() can't do that

The fact that FFI::memcpy() does not work, looks like a bug to me.

So it still could be possible allocate such structs in PHP? In some cases passing PHP string to FFI function (and receiving it as C string) will not work properly. If there is a possibility string contains \0 it will be truncated at first NULL-byte, so to handle such cases length should be passed explicitly. It would be very handy to have possibility use structs with flexible array member instead of additional length argument in foreign function.

$lib = FFI::cdef("
    typedef struct Data {
        int32_t size;
        char data[];
    } Data;

    void handleData(Data data);
", 'lib.so');

userqq avatar Jan 17 '22 00:01 userqq

The primary purpose of ext/ffi is to interface with C libraries; so if your library exposes structs with flexible array members and functions to create them, you should use these. You can't use FFI::new() to allocate a struct with a flexible array member, but you can use it to allocate a struct with a regular array member, and cast afterwards, e.g.

$value = FFI::new('struct {
    int32_t length;
    char data[10]; 
}');
foreach (range('a', 'j') as $i => $char) {
    $value->data[$i] = $char;
}
$alias = FFI::cast('struct {int32_t length; char data[];}', $value);
for ($i = 0; $i < 10; ++$i) {
    echo $alias->data[$i];
}

cmb69 avatar Jan 18 '22 15:01 cmb69