Add test for leaking prior context with symmetric coroutines

This commit is contained in:
Aaron Piotrowski 2021-06-12 00:08:26 -05:00
parent 039f0fec58
commit f5c209989a
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
5 changed files with 91 additions and 6 deletions

View File

@ -74,6 +74,7 @@ static zend_fiber_transfer zend_test_fiber_suspend(zend_test_fiber *fiber, zval
static ZEND_STACK_ALIGNED void zend_test_fiber_execute(zend_fiber_transfer *transfer)
{
zend_test_fiber *fiber = ZT_G(active_fiber);
zval retval;
zend_execute_data *execute_data;
@ -93,10 +94,11 @@ static ZEND_STACK_ALIGNED void zend_test_fiber_execute(zend_fiber_transfer *tran
EG(current_execute_data) = execute_data;
EG(jit_trace_num) = 0;
fiber->fci.retval = &fiber->result;
fiber->fci.retval = &retval;
zend_call_function(&fiber->fci, &fiber->fci_cache);
zval_ptr_dtor(&fiber->result); // Destroy param from symmetric coroutine.
zval_ptr_dtor(&fiber->fci.function_name);
if (EG(exception)) {
@ -111,15 +113,30 @@ static ZEND_STACK_ALIGNED void zend_test_fiber_execute(zend_fiber_transfer *tran
zend_clear_exception();
} else {
ZVAL_COPY_VALUE(&fiber->result, &retval);
ZVAL_COPY(&transfer->value, &fiber->result);
}
} zend_catch {
fiber->flags |= ZEND_FIBER_FLAG_BAILOUT;
} zend_end_try();
transfer->context = fiber->caller;
zend_vm_stack_destroy();
if (fiber->target) {
zend_fiber_context *target = zend_test_fiber_get_context(fiber->target);
zend_fiber_init_context(target, zend_test_fiber_class, zend_test_fiber_execute, EG(fiber_stack_size));
transfer->context = target;
ZVAL_COPY(&fiber->target->result, &fiber->result);
fiber->target->fci.params = &fiber->target->result;
fiber->target->fci.param_count = 1;
fiber->target->caller = fiber->caller;
ZT_G(active_fiber) = fiber->target;
} else {
transfer->context = fiber->caller;
}
fiber->caller = NULL;
}
@ -179,6 +196,10 @@ static void zend_test_fiber_object_free(zend_object *object)
zval_ptr_dtor(&fiber->fci.function_name);
}
if (fiber->target) {
OBJ_RELEASE(&fiber->target->std);
}
zval_ptr_dtor(&fiber->result);
zend_object_std_dtor(&fiber->std);
@ -225,6 +246,11 @@ static ZEND_METHOD(_ZendTestFiber, start)
ZEND_ASSERT(fiber->status == ZEND_FIBER_STATUS_INIT);
if (fiber->previous != NULL) {
zend_throw_error(NULL, "Cannot start a fiber that is the target of another fiber");
RETURN_THROWS();
}
fiber->fci.params = params;
fiber->fci.param_count = param_count;
fiber->fci.named_params = named_params;
@ -287,6 +313,33 @@ static ZEND_METHOD(_ZendTestFiber, resume)
delegate_transfer_result(fiber, &transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
static ZEND_METHOD(_ZendTestFiber, pipeTo)
{
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_FUNC(fci, fci_cache)
ZEND_PARSE_PARAMETERS_END();
zend_test_fiber *fiber = (zend_test_fiber *) Z_OBJ_P(getThis());
zend_test_fiber *target = (zend_test_fiber *) zend_test_fiber_class->create_object(zend_test_fiber_class);
target->fci = fci;
target->fci_cache = fci_cache;
Z_TRY_ADDREF(target->fci.function_name);
target->previous = zend_test_fiber_get_context(fiber);
if (fiber->target) {
OBJ_RELEASE(&fiber->target->std);
}
fiber->target = target;
RETURN_OBJ_COPY(&target->std);
}
void zend_test_fiber_init(void)
{
zend_test_fiber_class = register_class__ZendTestFiber();

View File

@ -19,15 +19,18 @@
#include "zend_fibers.h"
typedef struct _zend_test_fiber {
typedef struct _zend_test_fiber zend_test_fiber;
struct _zend_test_fiber {
zend_object std;
ZEND_FIBER_CONTEXT_FIELDS;
zend_fiber_context *caller;
zend_fiber_context *previous;
zend_test_fiber *target;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
zval result;
} zend_test_fiber;
};
void zend_test_fiber_init(void);

View File

@ -10,5 +10,7 @@ final class _ZendTestFiber
public function resume(mixed $value = null): mixed {}
public function pipeTo(callable $callback): _ZendTestFiber {}
public static function suspend(mixed $value = null): mixed {}
}

View File

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 12481d4bfa2981f886a2efabc82c29cfaf14b2f0 */
* Stub hash: 72732355038392236e17ce928354667302c4c6bf */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class__ZendTestFiber___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
@ -13,12 +13,17 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class__ZendTestFiber_resume, 0,
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_MIXED, 0, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class__ZendTestFiber_pipeTo, 0, 1, _ZendTestFiber, 0)
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
ZEND_END_ARG_INFO()
#define arginfo_class__ZendTestFiber_suspend arginfo_class__ZendTestFiber_resume
static ZEND_METHOD(_ZendTestFiber, __construct);
static ZEND_METHOD(_ZendTestFiber, start);
static ZEND_METHOD(_ZendTestFiber, resume);
static ZEND_METHOD(_ZendTestFiber, pipeTo);
static ZEND_METHOD(_ZendTestFiber, suspend);
@ -26,6 +31,7 @@ static const zend_function_entry class__ZendTestFiber_methods[] = {
ZEND_ME(_ZendTestFiber, __construct, arginfo_class__ZendTestFiber___construct, ZEND_ACC_PUBLIC)
ZEND_ME(_ZendTestFiber, start, arginfo_class__ZendTestFiber_start, ZEND_ACC_PUBLIC)
ZEND_ME(_ZendTestFiber, resume, arginfo_class__ZendTestFiber_resume, ZEND_ACC_PUBLIC)
ZEND_ME(_ZendTestFiber, pipeTo, arginfo_class__ZendTestFiber_pipeTo, ZEND_ACC_PUBLIC)
ZEND_ME(_ZendTestFiber, suspend, arginfo_class__ZendTestFiber_suspend, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_FE_END
};

View File

@ -0,0 +1,21 @@
--TEST--
Symmetric coroutine does not leak prior context
--EXTENSIONS--
zend_test
--FILE--
<?php
$fiber = new _ZendTestFiber(function (): int {
return 1;
});
$fiber->pipeTo(function (int $result): void {
var_dump($result);
});
var_dump($fiber->start());
?>
--EXPECTF--
int(1)
NULL