diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2019-10-28 10:28:24 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2019-10-28 10:28:24 +0100 |
commit | 9f98fddb7be1c4817e60a2c9f31b6c069ba6ef8a (patch) | |
tree | c4bdab1769111eaa87038799ecec5bdcb53dba38 | |
parent | addc78f379b6b13220ad2b4ebd8029b86d8b726c (diff) | |
parent | 1ddda13d846936fb625823778d14df68158d0e56 (diff) | |
download | php-git-9f98fddb7be1c4817e60a2c9f31b6c069ba6ef8a.tar.gz |
Merge branch 'PHP-7.4'
* PHP-7.4:
Fix bug #78752
-rw-r--r-- | Zend/tests/bug78752.phpt | 24 | ||||
-rw-r--r-- | Zend/zend_generators.c | 20 |
2 files changed, 36 insertions, 8 deletions
diff --git a/Zend/tests/bug78752.phpt b/Zend/tests/bug78752.phpt new file mode 100644 index 0000000000..367a44eab5 --- /dev/null +++ b/Zend/tests/bug78752.phpt @@ -0,0 +1,24 @@ +--TEST-- +Bug #78752: Segfault if GC triggered while generator stack frame is being destroyed +--FILE-- +<?php + +function gen(&$gen) { + $a = new stdClass; + $a->a = $a; + $b = new stdClass; + $b->b = $b; + yield 1; +} + +$gen = gen($gen); +var_dump($gen->current()); +for ($i = 0; $i < 9999; $i++) { + $a = new stdClass; + $a->a = $a; +} +$gen->next(); + +?> +--EXPECT-- +int(1) diff --git a/Zend/zend_generators.c b/Zend/zend_generators.c index 432c1198d9..db286d2c3d 100644 --- a/Zend/zend_generators.c +++ b/Zend/zend_generators.c @@ -94,16 +94,18 @@ ZEND_API zend_execute_data* zend_generator_freeze_call_stack(zend_execute_data * /* }}} */ static void zend_generator_cleanup_unfinished_execution( - zend_generator *generator, uint32_t catch_op_num) /* {{{ */ + zend_generator *generator, zend_execute_data *execute_data, uint32_t catch_op_num) /* {{{ */ { - zend_execute_data *execute_data = generator->execute_data; - if (execute_data->opline != execute_data->func->op_array.opcodes) { /* -1 required because we want the last run opcode, not the next to-be-run one. */ uint32_t op_num = execute_data->opline - execute_data->func->op_array.opcodes - 1; if (UNEXPECTED(generator->frozen_call_stack)) { + /* Temporarily restore generator->execute_data if it has been NULLed out already. */ + zend_execute_data *save_ex = generator->execute_data; + generator->execute_data = execute_data; zend_generator_restore_call_stack(generator); + generator->execute_data = save_ex; } zend_cleanup_unfinished_execution(execute_data, op_num, catch_op_num); } @@ -114,6 +116,9 @@ ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished { if (EXPECTED(generator->execute_data)) { zend_execute_data *execute_data = generator->execute_data; + /* Null out execute_data early, to prevent double frees if GC runs while we're + * already cleaning up execute_data. */ + generator->execute_data = NULL; if (EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE) { zend_clean_and_cache_symbol_table(execute_data->symbol_table); @@ -132,12 +137,12 @@ ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished return; } - zend_vm_stack_free_extra_args(generator->execute_data); + zend_vm_stack_free_extra_args(execute_data); /* Some cleanups are only necessary if the generator was closed * before it could finish execution (reach a return statement). */ if (UNEXPECTED(!finished_execution)) { - zend_generator_cleanup_unfinished_execution(generator, 0); + zend_generator_cleanup_unfinished_execution(generator, execute_data, 0); } /* Free closure object */ @@ -151,8 +156,7 @@ ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished generator->gc_buffer = NULL; } - efree(generator->execute_data); - generator->execute_data = NULL; + efree(execute_data); } } /* }}} */ @@ -213,7 +217,7 @@ static void zend_generator_dtor_storage(zend_object *object) /* {{{ */ if (finally_op_num) { zval *fast_call; - zend_generator_cleanup_unfinished_execution(generator, finally_op_num); + zend_generator_cleanup_unfinished_execution(generator, ex, finally_op_num); fast_call = ZEND_CALL_VAR(ex, ex->func->op_array.opcodes[finally_op_end].op1.var); Z_OBJ_P(fast_call) = EG(exception); |