-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-14643 ext/standard: segfault on user shutdown function release. #14656
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,24 @@ | ||
--TEST-- | ||
GH-14643: Segfault on empty user function. | ||
--FILE-- | ||
<?php | ||
$script1_dataflow=5000000; | ||
class Logger { | ||
public function __construct() { | ||
register_shutdown_function(function () { | ||
$this->flush(); | ||
register_shutdown_function([$this, 'flush'], true); | ||
}); | ||
} | ||
public function flush($final = false) { | ||
} | ||
} | ||
for ($i = 0; $i < 200; $script1_dataflow++) { | ||
$a = new Logger(); | ||
} | ||
var_fusion($script1_connect, $script2_connect, $random_var); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Allowed memory size of %d bytes exhausted %s | ||
|
||
Fatal error: Allowed memory size of %d bytes exhausted %s |
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.
This seems to point to a bug elsewhere it should not be possible to not have an FCI here.
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.
Furthermore, the patch also doesn't really make sense: fci is address-taken from a struct member of shutdown_function_entry, so it can only be NULL if shutdown_function_entry is NULL, but in that case it's undefined behaviour because you aren't allowed to do pointer math on NULL...
I think we should all run our development tools with ASAN + UBSAN enabled.
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.
Agreed that we should check
shutdown_function_entry
here, and notshutdown_function_entry->fci
.My understanding of the issue is that we OOM here when registering a new shutdown function:
php-src/Zend/zend_hash.h
Line 858 in 909d331
as a consequence,
Z_PTR_P(zv)
remainsNULL
(it is initialized toNULL
above), soshutdown_function_entry
isNULL
here:php-src/ext/standard/basic_functions.c
Line 1584 in 909d331
So the root cause is
zend_hash_next_index_insert_mem()
. We should probably change this function so that it doesn't leave NULL entries in case of allocation failure. We could move the malloc/memcpy before the insert, initialize tmp to that, and free when insert fails. WDYT?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 think moving the allocation before the insert is the right approach.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks all for your feedback, just trying to take care of "day to day" stuff as much as I can :) but at least I get better understanding of the internals.