zend_call_method with arguments passed by reference

php.internals

Unnamed Person

6 years ago
Hello, I hope this is the appropriate mailing list, please redirect me to better please if required. I am currently rewriting our PHP extension from PHP5 to PHP7. To call PHP methods from our C/C++ code we use slightly modified zend_call_method from Zend/zend_interfaces.c (to use more arguments than 2). Now I found out that it does not work with arguments passed by reference, such as: public function FuncWithRef(array &$changeThis) if values are changed in the PHP code then zval values back in C part after zend_call_function call are not influenced. With PHP5 the value was overwritten and could be used later in C code. Previously the zend_fcall_info struct for function call was filled with params simply by params[0] = &arg1; In PHP7 this is changed to ZVAL_COPY_VALUE(&params[0], arg1); After function is executed (zend_call_function) both fci.params and arg1 contain still the original zval values, changes made in PHP code are not available. Is there any way how to solve this? I am mainly searching for and comparing code snippets in PHP/ext folder to see how things were rewritten from PHP5 to PHP7. Sorry if I missed something obvious and thank you for your help.

Nikita Popov

6 years ago
On Wed, Jan 8, 2020 at 3:59 PM mdolezal.noctuint.cz via internals < internals@lists.php.net> wrote:
> Hello, > > I hope this is the appropriate mailing list, please redirect me to better > please if required. > > > > I am currently rewriting our PHP extension from PHP5 to PHP7. > > To call PHP methods from our C/C++ code we use slightly modified > zend_call_method from Zend/zend_interfaces.c (to use more arguments than > 2). > > > Now I found out that it does not work with arguments passed by reference, > such as: > > public function FuncWithRef(array &$changeThis) > > > > if values are changed in the PHP code then zval values back in C part after > zend_call_function call are not influenced. > > With PHP5 the value was overwritten and could be used later in C code. > > Previously the zend_fcall_info struct for function call was filled with > params simply by > > > > params[0] = &arg1; > > > > In PHP7 this is changed to > > > > ZVAL_COPY_VALUE(&params[0], arg1); > > > > After function is executed (zend_call_function) both fci.params and arg1 > contain still the original zval values, > > changes made in PHP code are not available. Is there any way how to solve > this? > > I am mainly searching for and comparing code snippets in PHP/ext folder to > see how things were rewritten from PHP5 to PHP7. > > Sorry if I missed something obvious and thank you for your help. >
It looks like the implementation of zend_call_method() does not support this, as it does not copy parameters back after the call. I'd suggest to use the lower level zend_call_function() for the cases where you need this for now. Nikita

Unnamed Person

6 years ago
Thank you for your answer Nikita. Can you please give me more details what you mean? I can try that approach. zend_call_method copies the args to params array which is assigned to zend_fcall_info fci. I can pass there for example "test" zval string. Then the mentioned lower level zend_call_function() is called with fci as parameter, that's what I do. In PHP code "test" is changed to "changed". But after the call the fci.params are not changed, still "test", so I have nothing to copy back manually. -----Original Message----- From: Nikita Popov <nikita.ppv@gmail.com> Sent: Wednesday, January 8, 2020 4:06 PM To: mdolezal@noctuint.cz Cc: PHP internals <internals@lists.php.net> Subject: Re: [PHP-DEV] zend_call_method with arguments passed by reference On Wed, Jan 8, 2020 at 3:59 PM mdolezal.noctuint.cz via internals < internals@lists.php.net> wrote:
> Hello, > > I hope this is the appropriate mailing list, please redirect me to > better please if required. > > > > I am currently rewriting our PHP extension from PHP5 to PHP7. > > To call PHP methods from our C/C++ code we use slightly modified > zend_call_method from Zend/zend_interfaces.c (to use more arguments > than 2). > > > Now I found out that it does not work with arguments passed by > reference, such as: > > public function FuncWithRef(array &$changeThis) > > > > if values are changed in the PHP code then zval values back in C part > after zend_call_function call are not influenced. > > With PHP5 the value was overwritten and could be used later in C code. > > Previously the zend_fcall_info struct for function call was filled > with params simply by > > > > params[0] = &arg1; > > > > In PHP7 this is changed to > > > > ZVAL_COPY_VALUE(&params[0], arg1); > > > > After function is executed (zend_call_function) both fci.params and > arg1 contain still the original zval values, > > changes made in PHP code are not available. Is there any way how to > solve this? > > I am mainly searching for and comparing code snippets in PHP/ext > folder to see how things were rewritten from PHP5 to PHP7. > > Sorry if I missed something obvious and thank you for your help. >
It looks like the implementation of zend_call_method() does not support this, as it does not copy parameters back after the call. I'd suggest to use the lower level zend_call_function() for the cases where you need this for now. Nikita

Nikita Popov

6 years ago
On Wed, Jan 8, 2020 at 4:18 PM <mdolezal@noctuint.cz> wrote:
> Thank you for your answer Nikita. > Can you please give me more details what you mean? I can try that approach. > zend_call_method copies the args to params array which is assigned to > zend_fcall_info fci. > I can pass there for example "test" zval string. > Then the mentioned lower level zend_call_function() is called with fci as > parameter, that's what I do. > In PHP code "test" is changed to "changed". > But after the call the fci.params are not changed, still "test", so I have > nothing to copy back manually. >
fci.params *should* contain the changed parameters after the call, at least assuming no_separation=0. It's not clear to me under what circumstances that wouldn't work. Nikita

Unnamed Person

6 years ago
Thanks a lot Nikita, that’s it. I was not aware of no_separation influence, it was set to 1 in zend_call_method both in PHP5 and PHP7 so I didn't touch it during migration. I need to copy the arguments properly back now but basically it's working. Thanks again, Martin -----Original Message----- From: Nikita Popov <nikita.ppv@gmail.com> Sent: Thursday, January 9, 2020 1:17 PM To: mdolezal@noctuint.cz Cc: PHP internals <internals@lists.php.net> Subject: Re: [PHP-DEV] zend_call_method with arguments passed by reference On Wed, Jan 8, 2020 at 4:18 PM <mdolezal@noctuint.cz> wrote:
> Thank you for your answer Nikita. > Can you please give me more details what you mean? I can try that approach. > zend_call_method copies the args to params array which is assigned to > zend_fcall_info fci. > I can pass there for example "test" zval string. > Then the mentioned lower level zend_call_function() is called with fci > as parameter, that's what I do. > In PHP code "test" is changed to "changed". > But after the call the fci.params are not changed, still "test", so I > have nothing to copy back manually. >
fci.params *should* contain the changed parameters after the call, at least assuming no_separation=0. It's not clear to me under what circumstances that wouldn't work. Nikita