-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix GH-19653: Closure named argument unpacking between temporary closures can cause a crash #19654
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19ca448
Fix GH-19653: Closure named argument unpacking between temporary clos…
nielsdos 3afdb1f
review
nielsdos 27344ac
fixup review
nielsdos 3d32d6d
Fix inverted condition
nielsdos b7f2adf
Remove redundant check
nielsdos 061cc5d
Soften requirement
nielsdos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--TEST-- | ||
GH-19653 (Closure named argument unpacking between temporary closures can cause a crash) | ||
--CREDITS-- | ||
ivan-u7n | ||
--FILE-- | ||
<?php | ||
|
||
function func1(string $a1 = 'a1', string $a2 = 'a2', string $a3 = 'a3') { | ||
echo __FUNCTION__ . "() a1=$a1 a2=$a2 a3=$a3\n"; | ||
} | ||
|
||
function usage1(?Closure $func1 = null) { | ||
echo __FUNCTION__ . "() "; | ||
($func1 ?? func1(...))(a3: 'm3+'); | ||
} | ||
usage1(); | ||
|
||
$func1 = function (string ...$args) { | ||
echo "[function] "; | ||
func1(...$args, a2: 'm2+'); | ||
}; | ||
usage1(func1: $func1); | ||
|
||
?> | ||
--EXPECT-- | ||
usage1() func1() a1=a1 a2=a2 a3=m3+ | ||
usage1() [function] func1() a1=a1 a2=m2+ a3=m3+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
GH-19653 (Closure named argument unpacking between temporary closures can cause a crash) - eval variation | ||
--CREDITS-- | ||
arnaud-lb | ||
--FILE-- | ||
<?php | ||
|
||
function usage1(Closure $c) { | ||
$c(a: 1); | ||
} | ||
|
||
usage1(eval('return function($a) { var_dump($a); };')); | ||
usage1(eval('return function($b) { var_dump($b); };')); | ||
|
||
?> | ||
--EXPECTF-- | ||
int(1) | ||
|
||
Fatal error: Uncaught Error: Unknown named parameter $a in %s:%d | ||
Stack trace: | ||
#0 %s(%d): usage1(Object(Closure)) | ||
#1 {main} | ||
thrown in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5058,7 +5058,12 @@ static zend_never_inline zend_result ZEND_FASTCALL zend_quick_check_constant( | |
|
||
static zend_always_inline uint32_t zend_get_arg_offset_by_name( | ||
zend_function *fbc, zend_string *arg_name, void **cache_slot) { | ||
if (EXPECTED(*cache_slot == fbc)) { | ||
/* Due to closures, the `fbc` address isn't unique if the memory address is reused. | ||
* The argument info will be however and uniquely positions the arguments. | ||
* We do support NULL arg_info, so we have to distinguish that from an uninitialized cache slot. */ | ||
void *unique_id = (void *) ((uintptr_t) fbc->common.arg_info | 1); | ||
|
||
if (EXPECTED(*cache_slot == unique_id)) { | ||
return *(uintptr_t *)(cache_slot + 1); | ||
} | ||
|
||
|
@@ -5069,8 +5074,10 @@ static zend_always_inline uint32_t zend_get_arg_offset_by_name( | |
for (uint32_t i = 0; i < num_args; i++) { | ||
zend_arg_info *arg_info = &fbc->op_array.arg_info[i]; | ||
if (zend_string_equals(arg_name, arg_info->name)) { | ||
*cache_slot = fbc; | ||
*(uintptr_t *)(cache_slot + 1) = i; | ||
if (!fbc->op_array.refcount) { | ||
*cache_slot = unique_id; | ||
*(uintptr_t *)(cache_slot + 1) = i; | ||
} | ||
return i; | ||
} | ||
} | ||
|
@@ -5079,16 +5086,20 @@ static zend_always_inline uint32_t zend_get_arg_offset_by_name( | |
zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i]; | ||
size_t len = strlen(arg_info->name); | ||
if (zend_string_equals_cstr(arg_name, arg_info->name, len)) { | ||
*cache_slot = fbc; | ||
*(uintptr_t *)(cache_slot + 1) = i; | ||
if (fbc->type == ZEND_INTERNAL_FUNCTION) { | ||
|
||
*cache_slot = unique_id; | ||
*(uintptr_t *)(cache_slot + 1) = i; | ||
} | ||
return i; | ||
} | ||
} | ||
} | ||
|
||
if (fbc->common.fn_flags & ZEND_ACC_VARIADIC) { | ||
*cache_slot = fbc; | ||
*(uintptr_t *)(cache_slot + 1) = fbc->common.num_args; | ||
if (fbc->type == ZEND_INTERNAL_FUNCTION || !fbc->op_array.refcount) { | ||
*cache_slot = unique_id; | ||
*(uintptr_t *)(cache_slot + 1) = fbc->common.num_args; | ||
} | ||
return fbc->common.num_args; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 believe this to be valid, allowing this to still get the benefit of the cache slot without storage in shared memory.
It won't help for Closures in CLI scripts (which more commonly run without opcache), but might help a small bit.