-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix GH-17747: Exception on reading property in register-based FETCH_OBJ_R breaks JIT #17749
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
Conversation
…H_OBJ_R breaks JIT When read_property fails, it may return `&EG(uninitialized_zval)`, and the exception is handled in the VM. The VM will try to `zval_ptr_dtor_nogc` the result, but the result was never set, resulting in dtor'ing garbage data. To solve this, we check when a different zval* was returned and initialize the result with UNDEF. We don't need to copy as the slow_ex handler return values are used directly in a register.
ext/opcache/jit/zend_jit_helpers.c
Outdated
|
|
||
| retval = zobj->handlers->read_property(zobj, name, BP_VAR_IS, cache_slot, result); | ||
| if (retval == result && UNEXPECTED(Z_ISREF_P(retval))) { | ||
| if (retval != result) { |
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 could also check for &EG(uninitialized_zval) but that check may be more expensive and we need to compare retval and result anyway for the zend_unwrap_reference below.
On the other hand, the current approach may cause more memory writes.
ext/opcache/jit/zend_jit_helpers.c
Outdated
| if (retval == result && UNEXPECTED(Z_ISREF_P(retval))) { | ||
| if (retval != result) { | ||
| /* On exception, we may free the result in the ZEND_HANDLE_EXCEPTION VM handler | ||
| * so we need to initialize the result. | ||
| * We don't need to copy because we use the return value directly as a register. */ | ||
| ZVAL_UNDEF(result); | ||
| } else if (UNEXPECTED(Z_ISREF_P(retval))) { |
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 may cause problems for case without exceptions, because the type in memory is changed and it may be inconsistent with inferred type.
I think, it's better to use zend_jit_check_exception_undef_result() instead of zend_jit_check_exception() when Z_MODE(res_addr) == IS_REG.
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 may cause problems for case without exceptions, because the type in memory is changed and it may be inconsistent with inferred type.
I thought this didn't matter as the value isn't actually used (the bytes are uninitialized anyway in this case).
I think, it's better to use zend_jit_check_exception_undef_result() instead of zend_jit_check_exception() when Z_MODE(res_addr) == IS_REG.
I didn't know this helper existed, I should've looked better. This idea is actually something I briefly thought of doing myself but didn't know it existed already. Thanks for pointing out.
When read_property fails, it may return
&EG(uninitialized_zval), and the exception is handled in the VM. The VM will try tozval_ptr_dtor_nogcthe result, but the result was never set, resulting in dtor'ing garbage data. To solve this, we check when a different zval* was returned and initialize the result with UNDEF. We don't need to copy as the slow_ex handler return values are used directly in a register.