Repository navigation
Fix a failed compound assignment resetting a typed property to 0 - #25811
Open
ChrisAbner wants to merge 1 commit into
Open
ChrisAbner wants to merge 1 commit into
ChrisAbner wants to merge 1 commit into
Conversation
The typed property and typed reference versions of a compound assignment compute the result into a separate zval and then verify it against the type, without checking whether the operation failed. When it throws, for example on $o->i /= 0 or $o->i += [], the result is UNDEF. In coercive typing mode the type check turns it into 0 and assigns it, so the property loses its value even though the exception is caught. Untyped properties and plain variables keep their value, because the result is written over the operand. Only assign the result when the binary operation succeeds, as zend_binary_assign_op_obj_dim() already does. The JIT has its own copies of these helpers, so they get the same check.
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.
The problem
When a compound assignment such as
/=or+=throws on a typed property, the property is set to 0. This happens in coercive typing mode, that is, withoutstrict_types. The code can catch the exception and keep running, but the property has lost its value.With an untyped property, a variable, an array element or
strict_types=1, the same code keeps 5.The operator does not have to throw by itself. Laravel turns warnings into
ErrorException. In a Laravel 13 app on PHP 8.4.28,$s->total += "5 apples"throws "A non-numeric value encountered" and leaves$s->totalat 0. An untyped property keeps 5.The cause
For a typed property or a typed reference, the engine computes the result in a temporary copy. Then it checks the copy against the type and writes it.
zend_binary_assign_op_typed_prop()andzend_binary_assign_op_typed_ref()never check whether the operation succeeded. When it fails, the copy is left empty (UNDEF). In coercive mode the type check turns that empty value into 0 (zend_parse_arg_long_weak()treats anything belowIS_TRUEas 0), and the 0 is written to the property.Untyped properties do not use a copy. The operation writes straight into the property, so the old value stays when it fails. With
strict_types=1the type check rejects the empty value, so nothing is written either.The fix
Write the result only when
zend_binary_op()returnsSUCCESS.zend_binary_assign_op_obj_dim()in the same file already does this.The JIT has its own copies of these functions in
zend_jit_helpers.c:zend_jit_assign_op_to_typed_prop(),zend_jit_assign_op_to_typed_ref()andzend_jit_assign_op_to_typed_ref_tmp(). They get the same check. Without that part,opcache.jit=functionstill leaves 0 in 7 of the 13 cases below.I also tried checking
EG(exception)instead. It gave the same results in every case. I kept the return value check to matchzend_binary_assign_op_obj_dim().Versions and branch
Reproduced on the official images of 8.0.30, 8.1.34, 8.2.35, 8.3.36, 8.4.28, 8.5.13 and 8.6.0. The patch applies cleanly to PHP-8.5, PHP-8.6 and master.
I targeted PHP-8.4 because the value is lost silently in code that catches the exception, as in the Laravel case. 398cfb9 fixed a similar edge case of typed properties after a failed assignment, and it went into 8.0.7. If you prefer master for this one, I can retarget it.
Verification
New test
Zend/tests/type_declarations/typed_properties_failed_compound_assign.phpt. It fails on 8.4.28 and passes with the fix. That holds without OPcache, with OPcache, withopcache.jit=tracingand withopcache.jit=function.Value left after the exception, on a PHP-8.4 debug build. The results are the same in all four modes.
intwith+= [],/= 0,%= 0,<<= -1,>>= -1,-= "abc",.= new S(__toStringthrows)int(0)int(5)floatwith+= []and/= 0float(0)float(2.5)?intwith/= 0int(0)int(7)static intwith/= 0int(0)int(5)/= 0and+= []int(0)int(5)intwith+= "5 apples",%= 1.5,|= 1.5and<<= 1.5, with an error handler that throws on every warning and deprecationint(0)int(5)/= 055Test suites with the fix, on the PHP-8.4 debug build:
Zend/testswithout OPcacheZend/testswith OPcacheext/opcache/testswith OPcacheext/opcache/tests/jitwithopcache.jit=tracingThe 14 failures are the inheritance cache, path, phar and preload restart tests (
bug66338,bug78106,gh17733,gh20469×3,gh24534,gh8739,gh8846,issue0115,issue0149,revalidate_path_01,preload_restart_function_jit,preload_restart_hot_counters). They fail the same way on the base commit without the fix in my environment.On an ASan build of the same commit, the reproducers gave no reports in any of the four modes.
Zend/tests/type_declarationsandext/opcache/tests/jit/assign_op_*.phptwith--asanpassed too (465 tests, 0 failures).Not covered
No PHP code I tried reaches
zend_jit_assign_op_to_typed_ref_tmp(). With a temporary operand, the JIT falls back to the VM handler. The change there is the same line as in the other two helpers.With
opcache.jit=tracing, every form I tried also goes through the VM handler. So in tracing mode the fix inzend_execute.cis the part that applies.