-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/standard/stream: Use FCC instead of zval for notification callback #19024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -856,10 +856,7 @@ PHP_FUNCTION(stream_select) | |
| static void user_space_stream_notifier(php_stream_context *context, int notifycode, int severity, | ||
| char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr) | ||
| { | ||
| zval *callback = &context->notifier->ptr; | ||
| zval retval; | ||
| zval zvs[6]; | ||
| int i; | ||
|
|
||
| ZVAL_LONG(&zvs[0], notifycode); | ||
| ZVAL_LONG(&zvs[1], severity); | ||
|
|
@@ -872,21 +869,20 @@ static void user_space_stream_notifier(php_stream_context *context, int notifyco | |
| ZVAL_LONG(&zvs[4], bytes_sofar); | ||
| ZVAL_LONG(&zvs[5], bytes_max); | ||
|
|
||
| if (FAILURE == call_user_function(NULL, NULL, callback, &retval, 6, zvs)) { | ||
| php_error_docref(NULL, E_WARNING, "Failed to call user notifier"); | ||
| } | ||
| for (i = 0; i < 6; i++) { | ||
| zend_call_known_fcc(context->notifier->fcc, NULL, 6, zvs, NULL); | ||
| for (uint8_t i = 0; i < 6; i++) { | ||
| zval_ptr_dtor(&zvs[i]); | ||
| } | ||
| zval_ptr_dtor(&retval); | ||
| } | ||
|
|
||
| static void user_space_stream_notifier_dtor(php_stream_notifier *notifier) | ||
| { | ||
| if (notifier && Z_TYPE(notifier->ptr) != IS_UNDEF) { | ||
| zval_ptr_dtor(¬ifier->ptr); | ||
| ZVAL_UNDEF(¬ifier->ptr); | ||
| } | ||
| ZEND_ASSERT(notifier); | ||
| ZEND_ASSERT(notifier->fcc); | ||
| ZEND_ASSERT(notifier->fcc->function_handler); | ||
| zend_fcc_dtor(notifier->fcc); | ||
| efree(notifier->fcc); | ||
| notifier->fcc = NULL; | ||
| } | ||
|
|
||
| static zend_result parse_context_options(php_stream_context *context, HashTable *options) | ||
|
|
@@ -924,9 +920,19 @@ static zend_result parse_context_params(php_stream_context *context, HashTable * | |
| context->notifier = NULL; | ||
| } | ||
|
|
||
| zend_fcall_info_cache *fcc = emalloc(sizeof(*fcc)); | ||
| char *error; | ||
| if (!zend_is_callable_ex(tmp, NULL, 0, NULL, fcc, &error)) { | ||
| zend_argument_type_error(1, "must be an array with valid callbacks as values, %s", error); | ||
nielsdos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| efree(fcc); | ||
| efree(error); | ||
| return FAILURE; | ||
| } | ||
| zend_fcc_addref(fcc); | ||
|
|
||
| context->notifier = php_stream_notification_alloc(); | ||
| context->notifier->func = user_space_stream_notifier; | ||
| ZVAL_COPY(&context->notifier->ptr, tmp); | ||
| context->notifier->fcc = fcc; | ||
| context->notifier->dtor = user_space_stream_notifier_dtor; | ||
| } | ||
| if (NULL != (tmp = zend_hash_str_find(params, "options", sizeof("options")-1))) { | ||
|
|
@@ -1123,9 +1129,11 @@ PHP_FUNCTION(stream_context_get_params) | |
| } | ||
|
|
||
| array_init(return_value); | ||
| if (context->notifier && Z_TYPE(context->notifier->ptr) != IS_UNDEF && context->notifier->func == user_space_stream_notifier) { | ||
| Z_TRY_ADDREF(context->notifier->ptr); | ||
| add_assoc_zval_ex(return_value, "notification", sizeof("notification")-1, &context->notifier->ptr); | ||
| if (context->notifier && context->notifier->fcc) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can you have a notifier without a valid fcc field? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That I don't know as I'm not super familiar with the code, so I went the safe route of checking it exists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Extensions. In fact this change is an actual breaking change. The There was a reason the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix is in #19158. |
||
| ZEND_ASSERT(context->notifier->func == user_space_stream_notifier); | ||
| zval fn; | ||
| zend_get_callable_zval_from_fcc(context->notifier->fcc, &fn); | ||
| add_assoc_zval_ex(return_value, ZEND_STRL("notification"), &fn); | ||
| } | ||
| Z_TRY_ADDREF(context->options); | ||
| add_assoc_zval_ex(return_value, "options", sizeof("options")-1, &context->options); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --TEST-- | ||
| stream_context_set_params() with invalid notification option | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $ctx = stream_context_create(); | ||
| try { | ||
| var_dump(stream_context_set_params($ctx, ["notification" => "fn_not_exist"])); | ||
| } catch (\Throwable $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
| try { | ||
| var_dump(stream_context_set_params($ctx, ["notification" => ["myclass", "fn_not_exist"]])); | ||
| } catch (\Throwable $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| TypeError: stream_context_set_params(): Argument #1 ($context) must be an array with valid callbacks as values, function "fn_not_exist" not found or invalid function name | ||
| TypeError: stream_context_set_params(): Argument #1 ($context) must be an array with valid callbacks as values, class "myclass" not found |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --TEST-- | ||
| stream_context_set_params() with valid, then invalid notification option | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function foo() {} | ||
|
|
||
| $ctx = stream_context_create(); | ||
| var_dump(stream_context_set_params($ctx, ["notification" => "foo"])); | ||
|
|
||
| try { | ||
| var_dump(stream_context_set_params($ctx, ["notification" => "fn_not_exist"])); | ||
| } catch (\Throwable $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| TypeError: stream_context_set_params(): Argument #1 ($context) must be an array with valid callbacks as values, function "fn_not_exist" not found or invalid function name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun fact: If not unrolled, using an (unsigned) int would be better in the sense that the generated assembly for both x86-64 and arm64 is shorter. For x86 it saves an instruction prefix byte to switch to 8 bit mode, and for arm64 it saves an instruction.
However, the loop is likely unrolled in which case it doesn't matter, but just something to take into account that using a shorter type is not always better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, why do we even have a loop? Just call the zval_ptr_dtor(_str) on
&zvs[2]?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about that, but can't this be an issue if the notifier callback takes the arguments by-ref and changes them to strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because the conversion happens on the call frame, not on the original zvals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK, will change then