Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix GH-17868: Cannot allocate memory with tracing JIT on 8.4.4 #17869

Closed
wants to merge 3 commits into from

Conversation

nielsdos
Copy link
Member

The generated code tries to initialize the run time cache for even internal closures, but it should only initialize the run time cache for user closures. This results in an out of memory error because op_array.cache_size is a nonsense value.
We fix this by adding a check for the function type. If func is known, then we can check the type at code generation time.

This was already an issue before 28b448a, but became more visible after that commit.

This also needs fixing on PHP-8.3, I can backport this after this is handled for 8.4.

The generated code tries to initialize the run time cache for even
internal closures, but it should only initialize the run time cache for
user closures. We fix this by adding a check for the function type.
If `func` is known, then we can check the type at code generation time.
@nielsdos nielsdos linked an issue Feb 20, 2025 that may be closed by this pull request
@nielsdos nielsdos marked this pull request as ready for review February 20, 2025 21:45
@nielsdos nielsdos requested a review from dstogov as a code owner February 20, 2025 21:45
Comment on lines 8725 to 8727
// JIT: if (closure->func.op_array.run_time_cache__ptr)
if_cond = ir_IF(ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.run_time_cache__ptr))));
ir_IF_FALSE(if_cond);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this additional check?
We checked !RUN_TIME_CACHE(op_array) in zend_jit_init_func_run_time_cache_helper().

Copy link
Member Author

@nielsdos nielsdos Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The check for !RUN_TIME_CACHE(op_array) already existed and I forgot that zend_jit_init_func_run_time_cache_helper() already made this check. We can remove the !RUN_TIME_CACHE check from the generated code. I'll commit that change.

On master, I see that zend_jit_init_func_run_time_cache_helper() also checks function type (not on PHP-8.4), but I think it's preferable to check it in the generated code so that we can sometimes skip generating a call to zend_jit_init_func_run_time_cache_helper() when func is known to be an internal function.

Copy link
Member Author

@nielsdos nielsdos Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function type check on master was introduced in 097edc8.
The only other place that calls zend_jit_init_func_run_time_cache_helper() already knows that this is a user function.
Perhaps it's even beneficial to create 2 helper variants of zend_jit_init_func_run_time_cache_helper(): one with a function type check and one without? Then we could use the one with the check in zend_jit_push_call_frame if !func, otherwise call the helper that skips the check if it's a user function, and generate no call if it's an internal function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check, how this affects the generated JIT code size. E.g. function/tracing JIT on Symfony demo.
I suppose, this shouldn't make a big difference.

Copy link
Member Author

@nielsdos nielsdos Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symfony-demo:

On function JIT:

tracing with check in IR code: 21268045
tracing with check in helper (2 helper variants): 21268045

So no difference somehow.

On tracing JIT the difference is very very small:

tracing with check in IR code: 628824
tracing with check in helper (2 helper variants): 628808

Only a difference of 16! So probably not worth creating helper variants.

@nielsdos nielsdos closed this in 5ede541 Feb 24, 2025
nielsdos added a commit to nielsdos/php-src that referenced this pull request Feb 24, 2025
nielsdos added a commit that referenced this pull request Feb 24, 2025
nielsdos added a commit that referenced this pull request Feb 24, 2025
* PHP-8.3:
  Backport GH-17869 to PHP 8.3 JIT
nielsdos added a commit that referenced this pull request Feb 24, 2025
* PHP-8.4:
  Backport GH-17869 to PHP 8.3 JIT
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot allocate memory with tracing JIT on 8.4.4
2 participants