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

FFI::cast() from pointer returns incorrect value

Open userqq opened this issue 4 years ago • 3 comments

Description

Hi! I mentioned in https://github.com/php/php-src/issues/7904 casting from pointer to integer. While https://github.com/php/php-src/issues/7904 still relevant, I found it also casts wrong data when casting from pointer to int

$val = FFI::new('char[8]');
FFI::memcpy($val, implode('', range('a', 'h')), 8);

$firstInt = FFI::cast('int32_t', $val);
var_dump($firstInt);

$secondInt = FFI::cast('int32_t', $val + 4);
var_dump($secondInt); 

var_dump(unpack('Nfirst/Nsecond', implode('', range('a', 'h'))));

Resulted in this output:

object(FFI\CData:int32_t)#2 (1) {
  ["cdata"]=>
  int(1684234849)
}
object(FFI\CData:int32_t)#4 (1) {
  ["cdata"]=>
  int(-423620580) // any random int32 actually
}
array(2) {
  ["first"]=>
  int(1633837924)
  ["second"]=>
  int(1701209960)
}

But I expected this output instead:

object(FFI\CData:int32_t)#2 (1) {
  ["cdata"]=>
  int(1684234849)
}
object(FFI\CData:int32_t)#4 (1) {
  ["cdata"]=>
  int(1701209960)
}
array(2) {
  ["first"]=>
  int(1633837924)
  ["second"]=>
  int(1701209960)
}

PHP Version

8.1.1, 8.1.2

Operating System

No response

userqq avatar Jan 29 '22 00:01 userqq

Two issues here.

First is that you're using N in the unpack, which is big-endian and unsigned, but int32_t will be machine-dependent order and signed. If I change your unpack() to use l then I get the expected output for "first".

The second is that char[] + 4 will give you a pointer, and if you cast the pointer to a int32_t then you'll get a number for the memory address. You need to work with them as pointers, meaning cast to a int32_t* and then dereference that. However casting the original array to an int32_t works.

damianwadley avatar Jan 29 '22 02:01 damianwadley

Oops, sent that a bit early.

$val = FFI::new('char[8]');
FFI::memcpy($val, 'abcdefgh', 8);
var_dump($val);

$first = FFI::cast('int32_t', $val);
var_dump($first);
var_dump(unpack('l', 'abcdefgh'));

$second = FFI::cast('int32_t*', $val + 4);
var_dump($second[0]);
var_dump(unpack('l', 'efgh'));
object(FFI\CData:char[8])#1 (8) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
  [7]=>
  string(1) "h"
}
object(FFI\CData:int32_t)#2 (1) {
  ["cdata"]=>
  int(1684234849)
}
array(1) {
  [1]=>
  int(1684234849)
}
int(1751606885)
array(1) {
  [1]=>
  int(1751606885)
}

Neither of the issues seem to be bugs to me, however the treatment of an array (automatically dereferenced) versus a pointer (not dereferenced) is surprising.

damianwadley avatar Jan 29 '22 02:01 damianwadley

Well, I should admit I'm newbie in C, even though I had idea about pointers :) Thank you @damianwadley , it works for me.

I will close this issue if you mind.

userqq avatar Jan 29 '22 12:01 userqq