Skip to content

Fix double-free of closure run_time_cache in partial application#180

Closed
iliaal wants to merge 5 commits into
masterfrom
fix/pfa-closure-rtcache-double-free
Closed

Fix double-free of closure run_time_cache in partial application#180
iliaal wants to merge 5 commits into
masterfrom
fix/pfa-closure-rtcache-double-free

Conversation

@iliaal

@iliaal iliaal commented Jul 17, 2026

Copy link
Copy Markdown
Owner

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));'

Sjord and others added 5 commits July 17, 2026 14:30
Assert and take advantage of that size is always 1. This is also documented in [the curl docs](https://curl.se/libcurl/c/CURLOPT_READFUNCTION.html). So there is no need to multiply with size, so this can be removed.

I started this to see whether size * nmemb can overflow. This makes it clear that it cannot.
And instead let the normal docref error handling code handle arguments
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.
@iliaal
iliaal force-pushed the fix/pfa-closure-rtcache-double-free branch from 7c3c420 to f8e0ffb Compare July 17, 2026 15:14
@iliaal

iliaal commented Jul 17, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream: php#22790.

@iliaal iliaal closed this Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants