Repository navigation
JIT: Fix OP_DATA leak when ASSIGN_DIM fails #24581
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
Open
jorgsowa
wants to merge
3
commits into
php:PHP-8.4
Choose a base branch
from
jorgsowa:fix-jit-assign-dim-append-leak
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,6 +243,7 @@ static size_t tsrm_tls_offset = 0; | |
| _(undefined_offset, IR_FUNCTION | IR_FASTCALL_FUNC) \ | ||
| _(undefined_key, IR_FUNCTION | IR_FASTCALL_FUNC) \ | ||
| _(cannot_add_element, IR_FUNCTION | IR_FASTCALL_FUNC) \ | ||
| _(assign_dim_cannot_add_element, IR_FUNCTION | IR_FASTCALL_FUNC) \ | ||
| _(assign_const, IR_FUNCTION | IR_FASTCALL_FUNC) \ | ||
| _(assign_tmp, IR_FUNCTION | IR_FASTCALL_FUNC) \ | ||
| _(assign_var, IR_FUNCTION | IR_FASTCALL_FUNC) \ | ||
|
|
@@ -2158,6 +2159,12 @@ static int zend_jit_invalid_this_stub(zend_jit_ctx *jit) | |
| return 1; | ||
| } | ||
|
|
||
| static int zend_jit_assign_dim_cannot_add_element_stub(zend_jit_ctx *jit) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need this new stub, why do we need this indirection? |
||
| { | ||
| ir_TAILCALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_dim_cannot_add_element)); | ||
| return 1; | ||
| } | ||
|
|
||
| static int zend_jit_undefined_function_stub(zend_jit_ctx *jit) | ||
| { | ||
| // JIT: load EX(opline) | ||
|
|
@@ -3124,6 +3131,7 @@ static void zend_jit_setup_disasm(void) | |
| REGISTER_HELPER(zend_jit_fetch_dim_isset_helper); | ||
| REGISTER_HELPER(zend_jit_fetch_dim_rw_helper); | ||
| REGISTER_HELPER(zend_jit_fetch_dim_w_helper); | ||
| REGISTER_HELPER(zend_jit_assign_dim_fetch_w_helper); | ||
| REGISTER_HELPER(zend_jit_fetch_dim_str_offset_r_helper); | ||
| REGISTER_HELPER(zend_jit_fetch_dim_str_r_helper); | ||
| REGISTER_HELPER(zend_jit_fetch_dim_str_is_helper); | ||
|
|
@@ -12417,7 +12425,11 @@ static int zend_jit_fetch_dimension_address_inner(zend_jit_ctx *jit, | |
| ir_END_list(*end_inputs); | ||
| break; | ||
| case BP_VAR_W: | ||
| ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_w_helper), ht_ref, ref); | ||
| if (opline->opcode == ZEND_ASSIGN_DIM && ((opline+1)->op1_type & (IS_VAR|IS_TMP_VAR))) { | ||
| ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_assign_dim_fetch_w_helper), ht_ref, ref); | ||
| } else { | ||
| ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_w_helper), ht_ref, ref); | ||
| } | ||
| if_found = ir_IF(ref); | ||
| ir_IF_TRUE(if_found); | ||
| ir_refs_add(found_inputs, ir_END()); | ||
|
|
@@ -13352,7 +13364,11 @@ static int zend_jit_assign_dim(zend_jit_ctx *jit, | |
|
|
||
| // JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied"); | ||
| jit_SET_EX_OPLINE(jit, opline); | ||
| ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC)); | ||
| if ((opline+1)->op1_type & (IS_VAR|IS_TMP_VAR)) { | ||
| ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_assign_dim_cannot_add_element, IR_FASTCALL_FUNC)); | ||
| } else { | ||
| ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC)); | ||
| } | ||
|
|
||
| ir_END_list(end_inputs); | ||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --TEST-- | ||
| JIT ASSIGN_DIM: OP_DATA is freed when appending to a full array fails | ||
| --INI-- | ||
| opcache.enable=1 | ||
| opcache.enable_cli=1 | ||
| opcache.file_update_protection=0 | ||
| opcache.jit=function | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --FILE-- | ||
| <?php | ||
| class D { | ||
| function __destruct() { | ||
| echo "dtor\n"; | ||
| } | ||
| } | ||
|
|
||
| function test() { | ||
| $a = [PHP_INT_MAX => 1]; | ||
| $a[] = new D; | ||
| } | ||
|
|
||
| try { | ||
| test(); | ||
| } catch (Throwable $e) { | ||
| echo $e::class, ": ", $e->getMessage(), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| dtor | ||
| Error: Cannot add element to the array as the next element is already occupied |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| JIT ASSIGN_DIM: OP_DATA is freed when a float key conversion throws | ||
| --INI-- | ||
| opcache.enable=1 | ||
| opcache.enable_cli=1 | ||
| opcache.file_update_protection=0 | ||
| opcache.jit=function | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --FILE-- | ||
| <?php | ||
| class D { | ||
| function __destruct() { | ||
| echo "dtor\n"; | ||
| } | ||
| } | ||
|
|
||
| set_error_handler(function ($errno, $errstr) { | ||
| throw new Exception($errstr); | ||
| }); | ||
|
|
||
| function test() { | ||
| $a[1.5] = new D; | ||
| } | ||
|
|
||
| try { | ||
| test(); | ||
| } catch (Throwable $e) { | ||
| echo $e::class, ": ", $e->getMessage(), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| dtor | ||
| Exception: Implicit conversion from float 1.5 to int loses precision |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| JIT ASSIGN_DIM: OP_DATA is freed when a resource key conversion throws | ||
| --INI-- | ||
| opcache.enable=1 | ||
| opcache.enable_cli=1 | ||
| opcache.file_update_protection=0 | ||
| opcache.jit=function | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --FILE-- | ||
| <?php | ||
| class D { | ||
| function __destruct() { | ||
| echo "dtor\n"; | ||
| } | ||
| } | ||
|
|
||
| set_error_handler(function ($errno, $errstr) { | ||
| throw new Exception($errstr); | ||
| }); | ||
|
|
||
| function test() { | ||
| $a[STDIN] = new D; | ||
| } | ||
|
|
||
| try { | ||
| test(); | ||
| } catch (Throwable $e) { | ||
| echo $e::class, ": ", $e->getMessage(), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| dtor | ||
| Exception: Resource ID#%d used as offset, casting to integer (%d) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| JIT ASSIGN_DIM: OP_DATA is freed once when a float key conversion throws on a false container | ||
| --INI-- | ||
| opcache.enable=1 | ||
| opcache.enable_cli=1 | ||
| opcache.file_update_protection=0 | ||
| opcache.jit=function | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --FILE-- | ||
| <?php | ||
| set_error_handler(function ($errno, $errstr) { | ||
| if (!str_starts_with($errstr, "Automatic conversion of false")) { | ||
| throw new Exception($errstr); | ||
| } | ||
| }); | ||
|
|
||
| function id($x) { | ||
| return $x; | ||
| } | ||
|
|
||
| function test($o) { | ||
| $a = false; | ||
| $a[1.5] = id($o); | ||
| } | ||
|
|
||
| try { | ||
| test(new stdClass); | ||
| } catch (Exception $e) { | ||
| echo $e::class, ": ", $e->getMessage(), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| Exception: Implicit conversion from float 1.5 to int loses precision |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| JIT ASSIGN_DIM: OP_DATA is freed once when a resource key conversion throws on a false container | ||
| --INI-- | ||
| opcache.enable=1 | ||
| opcache.enable_cli=1 | ||
| opcache.file_update_protection=0 | ||
| opcache.jit=function | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --FILE-- | ||
| <?php | ||
| set_error_handler(function ($errno, $errstr) { | ||
| if (!str_starts_with($errstr, "Automatic conversion of false")) { | ||
| throw new Exception($errstr); | ||
| } | ||
| }); | ||
|
|
||
| function id($x) { | ||
| return $x; | ||
| } | ||
|
|
||
| function test($o) { | ||
| $a = false; | ||
| $a[STDIN] = id($o); | ||
| } | ||
|
|
||
| try { | ||
| test(new stdClass); | ||
| } catch (Exception $e) { | ||
| echo $e::class, ": ", $e->getMessage(), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| Exception: Resource ID#%d used as offset, casting to integer (%d) |
Oops, something went wrong.
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.
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.
it would be better to move the execute_data and opline loads to the
!retvalerror path IMO. To deal withoplinechanging, then check for an exception and useEG(opline_before_exception).Same remark for the other helper