Re: com php-src: Fix #79177: FFI doesn't handle well PHP exceptions within callback: NEWS ext/ffi/ffi.c ext/ffi/tests/bug79177.phpt
| From: | Christoph M. Becker | Date: | Fri, 30 Oct 2020 09:51:30 +0000 |
| Subject: | Re: com php-src: Fix #79177: FFI doesn't handle well PHP exceptions within callback: NEWS ext/ffi/ffi.c ext/ffi/tests/bug79177.phpt | ||
| References: | 1 2 3 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-113991@lists.php.net to get a copy of this message | ||
On 30.10.2020 at 09:46, Nikita Popov wrote:
> On Fri, Oct 30, 2020 at 9:25 AM Dmitry Stogov <dmitrystogov@gmail.com>
> wrote:
>
>> hey,
>>
>> Have you seen this failure
>>
>>
>> https://dev.azure.com/phpazuredevops/PHP/_build/results?buildId=12673&view=ms.vss-test-web.build-test-results-tab&runId=280374&resultId=105281&paneView=debug
>>
>> Thanks. Dmitry.
>>
>
> We need to use some callback other than zend_write here, as JIT ignores it
> being overwritten at runtime...
Thanks! I'll provide a PR ASAP.
Christoph
> Nikita
>
>
>> On Wed, Oct 28, 2020 at 3:50 PM Christoph Michael Becker <cmb@php.net>
>> wrote:
>>
>>> Commit: f547412cba52b499a4a130b0de1b6af2ea746e3a
>>> Author: Christoph M. Becker <cmbecker69@gmx.de> Wed, 28 Oct
>>> 2020 12:01:28 +0100
>>> Parents: 68dcaa29d8a2d51f0fed5fde9f3543a1338bfe44
>>> Branches: PHP-7.4 PHP-8.0 master
>>>
>>> Link:
>>>
>>
>> http://git.php.net/?p=php-src.git;a=commitdiff;h=f547412cba52b499a4a130b0de1b6af2ea746e3a
>>>
>>> Log:
>>> Fix #79177: FFI doesn't handle well PHP exceptions within callback
>>>
>>> We have to error on unhandled exceptions in FFI callbacks, to avoid
>>> passing back undefined values.
>>>
>>> This has been discussed and agreed upon in a previous PR[1].
>>>
>>> [1] <https://github.com/php/php-src/pull/5120>
>>>
>>> Closes GH-6366.
>>>
>>> Bugs:
>>> https://bugs.php.net/79177
>>>
>>> Changed paths:
>>> M NEWS
>>> M ext/ffi/ffi.c
>>> A ext/ffi/tests/bug79177.phpt
>>>
>>>
>>> Diff:
>>> diff --git a/NEWS b/NEWS
>>> index 95de673b3f9..017896befac 100644
>>> --- a/NEWS
>>> +++ b/NEWS
>>> @@ -14,6 +14,10 @@ PHP
>>> NEWS
>>> - DOM:
>>> . Fixed bug #80268 (loadHTML() truncates at NUL bytes). (cmb)
>>>
>>> +- FFI:
>>> + . Fixed bug #79177 (FFI doesn't handle well PHP exceptions within
>>> callback).
>>> + (cmb, Dmitry, Nikita)
>>> +
>>> - IMAP:
>>> . Fixed bug #64076 (imap_sort() does not return FALSE on failure).
>> (cmb)
>>> . Fixed bug #76618 (segfault on imap_reopen). (girgias)
>>> diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c
>>> index f350f91d8a3..d119ea050cb 100644
>>> --- a/ext/ffi/ffi.c
>>> +++ b/ext/ffi/ffi.c
>>> @@ -886,6 +886,10 @@ static void zend_ffi_callback_trampoline(ffi_cif*
>>> cif, void* ret, void** args, v
>>> }
>>> free_alloca(fci.params, use_heap);
>>>
>>> + if (EG(exception)) {
>>> + zend_error(E_ERROR, "Throwing from FFI callbacks is not
>>> allowed");
>>> + }
>>> +
>>> ret_type = ZEND_FFI_TYPE(callback_data->type->func.ret_type);
>>> if (ret_type->kind != ZEND_FFI_TYPE_VOID) {
>>> zend_ffi_zval_to_cdata(ret, ret_type, &retval);
>>> diff --git a/ext/ffi/tests/bug79177.phpt b/ext/ffi/tests/bug79177.phpt
>>> new file mode 100644
>>> index 00000000000..d764437b2d5
>>> --- /dev/null
>>> +++ b/ext/ffi/tests/bug79177.phpt
>>> @@ -0,0 +1,47 @@
>>> +--TEST--
>>> +Bug #79177 (FFI doesn't handle well PHP exceptions within callback)
>>> +--SKIPIF--
>>> +<?php
>>> +require_once('skipif.inc');
>>> +require_once('utils.inc');
>>> +try {
>>> + ffi_cdef("extern void *zend_printf;", ffi_get_php_dll_name());
>>> +} catch (Throwable $e) {
>>> + die('skip PHP symbols not available');
>>> +}
>>> +?>
>>> +--FILE--
>>> +<?php
>>> +require_once('utils.inc');
>>> +$php = ffi_cdef("
>>> +typedef char (*zend_write_func_t)(const char *str, size_t str_length);
>>> +extern zend_write_func_t zend_write;
>>> +", ffi_get_php_dll_name());
>>> +
>>> +echo "Before\n";
>>> +
>>> +$originalHandler = clone $php->zend_write;
>>> +$php->zend_write = function($str, $len): string {
>>> + throw new \RuntimeException('Not allowed');
>>> +};
>>> +try {
>>> + echo "After\n";
>>> +} catch (\Throwable $exception) {
>>> + // Do not output anything here, as handler is overridden
>>> +} finally {
>>> + $php->zend_write = $originalHandler;
>>> +}
>>> +if (isset($exception)) {
>>> + echo $exception->getMessage(), PHP_EOL;
>>> +}
>>> +?>
>>> +--EXPECTF--
>>> +Before
>>> +
>>> +Warning: Uncaught RuntimeException: Not allowed in %s:%d
>>> +Stack trace:
>>> +#0 %s(%d): {closure}('After\n', 6)
>>> +#1 {main}
>>> + thrown in %s on line %d
>>> +
>>> +Fatal error: Throwing from FFI callbacks is not allowed in %s on line %d
>>>
>>>
>>> --
>>> PHP CVS Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>
>