Staging: destructor-hazard-section deferral mechanism#154
Closed
iliaal wants to merge 1 commit into
Closed
Conversation
An internal C operation that drops a refcount while holding a raw pointer into a structure a synchronous __destruct could free is a use-after-free. Add ZEND_DTOR_HAZARD_BEGIN/END sections and a deferral gate in zend_objects_store_del: while a section is open, an object that reaches refcount zero and has a destructor is pinned with GC_ADDREF and its destructor deferred until the section closes. gc_possible_root_when_full skips synchronous collection while a section is open. Apply the sections to concat (phpGH-20477), zend_hash_clean (phpGH-22061), and unserialize teardown (phpGH-21824). Fixes phpGH-20477 Fixes phpGH-22061 Fixes phpGH-21824
092b675 to
d617101
Compare
Owner
Author
|
Submitted upstream as php#22697. |
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.
Phase 2 of the destructor-reentrancy work, building on the error-handler deferral in 22515. Adds
ZEND_DTOR_HAZARD_BEGIN/ENDsections and a deferral gate inzend_objects_store_del: while a section is open, an object that hits refcount zero and has a destructor is pinned and its destructor deferred until the section closes. Applied to three reentrancy use-after-frees where an internal C loop drops a refcount while holding a raw pointer a synchronous__destructcould free: concat (phpGH-20477),zend_hash_clean(phpGH-22061), and unserialize teardown (phpGH-21824), with a regression test for the concat-assign case (phpGH-19999); that test expectsNULLbecause the deferred destructor runs at section exit, after the concat writes the result, and nulls$GLOBALS['a'].How this differs from arnaud-lb#31: that prototype deferred every destructor through a single global
EG(delayed_effects)queue, pinned the dying object withGC_SET_REFCOUNT(obj, 1), and flushed at the VM function-leave helper. It left three problems open: throwing destructors reordered against try/finally and catch (6 XFAILed tests), 67 tests needed a dummy call to create a flush point (a timing change), and GC reconciliation between the RC=1 pin and cycle collection stayed unresolved. This mechanism is scoped instead. BEGIN/END bracket only the specific internal loops that hold the raw pointer, so no user code runs inside a window. That drops the try/finally reordering question (no exception crosses a boundary), drops the timing change (flush is at section exit, not an arbitrary safepoint), usesGC_ADDREFinstead ofGC_SET_REFCOUNTso it reuses the existing store_del invariants, and closes the GC hole by havinggc_possible_root_when_fullskip synchronous collection while a section is open.Performance: the store_del gate is one branch guarded by
has_dtor, so destructor-less objects are untouched. Sections arm only on cold paths (concat arms only when an operand needed stringification or the result aliases an operand;hash_cleanonly when the table has a destructor). Release callgrind over 2M concats shows +1.2 instructions per concat, 0.45% ofconcat_function, with the common in-place concat paying one predicted-not-taken branch. No globalvm_interruptarming, no JITmay_throwchange.