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

FFI::cast() from pointer to array is broken

Open userqq opened this issue 4 years ago • 1 comments

Hi! Not sure its incorrect behaviour, but as you can see FFI::cast when applied to FFI\CData with offset returns data different from copy result.

The following code:

$value = FFI::new('char[26]');
FFI::memcpy($value, implode('', range('a', 'z')), 26);

$slice = FFI::new('char[4]');

echo 'cast from start' . PHP_EOL;
FFI::memcpy($slice, $value, 4);
var_dump($value + 0, $slice, FFI::cast('char[4]', $value));
echo PHP_EOL;

echo 'cast with offset' . PHP_EOL;
FFI::memcpy($slice, $value + 4, 4);
var_dump($value + 4, $slice, FFI::cast('char[4]', $value + 4));
echo PHP_EOL;

Resulted in this output:

cast from start
object(FFI\CData:char*)#3 (1) {
  [0]=>
  string(1) "a"
}
object(FFI\CData:char[4])#2 (4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
}
object(FFI\CData:char[4])#4 (4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
}

cast with offset
object(FFI\CData:char*)#4 (1) {
  [0]=>
  string(1) "e"
}
object(FFI\CData:char[4])#2 (4) {
  [0]=>
  string(1) "e"
  [1]=>
  string(1) "f"
  [2]=>
  string(1) "g"
  [3]=>
  string(1) "h"
}
object(FFI\CData:char[4])#5 (4) {
  [0]=>
  string(1) "▒"
  [1]=>
  string(1) "C"
  [2]=>
  string(1) "▒"
  [3]=>
  string(1) "x"
}

But I expected this output instead:

cast from start
object(FFI\CData:char*)#3 (1) {
  [0]=>
  string(1) "a"
}
object(FFI\CData:char[4])#2 (4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
}
object(FFI\CData:char[4])#4 (4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
}

cast with offset
object(FFI\CData:char*)#4 (1) {
  [0]=>
  string(1) "e"
}
object(FFI\CData:char[4])#2 (4) {
  [0]=>
  string(1) "e"
  [1]=>
  string(1) "f"
  [2]=>
  string(1) "g"
  [3]=>
  string(1) "h"
}
object(FFI\CData:char[4])#5 (4) {
  [0]=>
  string(1) "e"
  [1]=>
  string(1) "f"
  [2]=>
  string(1) "g"
  [3]=>
  string(1) "h"
}

PHP Version

PHP 8.1.1

Operating System

No response

userqq avatar Jan 01 '22 21:01 userqq

There are issues regarding arrays and pointers of a certain type. While pointer arithmetic mostly works, it looses the alignment, e.g.

$value = FFI::new('char[26]');
var_dump(
    FFI::alignof($value),    // int(1)
    FFI::alignof($value + 0) // int(8)
);

But then there is the more serious issue, that casting from a pointer type to an array doesn't properly check the size of the types, e.g.

$value = FFI::new('char[26]');
var_dump(FFI::cast('char[4]', $value + 0));
var_dump(FFI::cast('char[9]', $value + 0));

outputs:

object(FFI\CData:char[4])#3 (4) {
  [0]=>
  string(1) ""
  [1]=>
  string(1) ""
  [2]=>
  string(1) "E"
  [3]=>
  string(1) ""
}

Fatal error: Uncaught FFI\Exception: attempt to cast to larger type in %s:5

cmb69 avatar Jan 03 '22 13:01 cmb69