Fix double-free of closure run_time_cache in partial application#22790
Merged
Conversation
A partial over a closure whose op_array is not persistent shallow-copies the closure's op_array into the per-request PFA cache as a lifetime anchor. The copy inherited ZEND_ACC_HEAP_RT_CACHE and the run_time_cache pointer, so destroy_op_array() freed the closure's cache twice: once when the closure was released and again at PFA cache teardown. Use function_add_ref() for the copy, which resets the per-request run_time_cache and static_variables_ptr map pointers, so only the live closure owns and frees the cache.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A partial built over a closure whose op_array is not persistent (created in eval() or via the CLI -r path, or when opcache is not caching the declaring script) makes zend_accel_compile_pfa shallow-copy the closure op_array into the per-request PFA cache as a lifetime anchor. The copy inherited ZEND_ACC_HEAP_RT_CACHE and the run_time_cache pointer, so destroy_op_array freed the shared run_time_cache twice: once when the closure was released and again at PFA cache teardown, corrupting the heap. Use function_add_ref() on the copy, the canonical shallow-copy refcount helper, which resets the per-request run_time_cache and static_variables_ptr map pointers so only the live closure owns and frees the cache.
Reproducer, invalid free under valgrind (USE_ZEND_ALLOC=0) before the fix, clean after:
php -d opcache.enable_cli=1 -r 'class C { function m($x,$y){ return $x+$y; } } $cl = Closure::fromCallable([new C,"m"]); $pc = $cl(10,?); var_dump($pc(5));'