Skip to content

Commit 2397e76

Browse files
committed
Fix GH-8433: Assigning function pointers to structs in FFI leaks memory
1 parent f5d9e7c commit 2397e76

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ PHP NEWS
1010
. Fixed bug GH-7979 (DatePeriod iterator advances when checking if valid).
1111
(Derick, Cody Mann)
1212

13+
- FFI:
14+
. Fixed bug GH-8433 (Assigning function pointers to structs in FFI leaks).
15+
(Bob)
16+
1317
- FPM:
1418
. Fixed bug #76003 (FPM /status reports wrong number of active processe).
1519
(Jakub Zelenka)

ext/ffi/ffi.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,14 @@ static void zend_ffi_callback_hash_dtor(zval *zv) /* {{{ */
864864
if (callback_data->fcc.function_handler->common.fn_flags & ZEND_ACC_CLOSURE) {
865865
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(callback_data->fcc.function_handler));
866866
}
867+
for (int i = 0; i < callback_data->arg_count; ++i) {
868+
if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
869+
efree(callback_data->arg_types[i]);
870+
}
871+
}
872+
if (callback_data->ret_type->type == FFI_TYPE_STRUCT) {
873+
efree(callback_data->ret_type);
874+
}
867875
efree(callback_data);
868876
}
869877
/* }}} */
@@ -917,6 +925,8 @@ static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, v
917925
if (ret_type->kind != ZEND_FFI_TYPE_VOID) {
918926
zend_ffi_zval_to_cdata(ret, ret_type, &retval);
919927
}
928+
929+
zval_ptr_dtor(&retval);
920930
}
921931
/* }}} */
922932

@@ -967,6 +977,11 @@ static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value) /* {{{ *
967977
callback_data->arg_types[n] = zend_ffi_get_type(arg_type);
968978
if (!callback_data->arg_types[n]) {
969979
zend_ffi_pass_unsupported(arg_type);
980+
for (int i = 0; i < n; ++i) {
981+
if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
982+
efree(callback_data->arg_types[i]);
983+
}
984+
}
970985
efree(callback_data);
971986
ffi_closure_free(callback);
972987
return NULL;
@@ -977,20 +992,32 @@ static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value) /* {{{ *
977992
callback_data->ret_type = zend_ffi_get_type(ZEND_FFI_TYPE(type->func.ret_type));
978993
if (!callback_data->ret_type) {
979994
zend_ffi_return_unsupported(type->func.ret_type);
995+
for (int i = 0; i < callback_data->arg_count; ++i) {
996+
if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
997+
efree(callback_data->arg_types[i]);
998+
}
999+
}
9801000
efree(callback_data);
9811001
ffi_closure_free(callback);
9821002
return NULL;
9831003
}
9841004

9851005
if (ffi_prep_cif(&callback_data->cif, type->func.abi, callback_data->arg_count, callback_data->ret_type, callback_data->arg_types) != FFI_OK) {
9861006
zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback CIF");
987-
efree(callback_data);
988-
ffi_closure_free(callback);
989-
return NULL;
1007+
goto free_on_failure;
9901008
}
9911009

9921010
if (ffi_prep_closure_loc(callback, &callback_data->cif, zend_ffi_callback_trampoline, callback_data, code) != FFI_OK) {
9931011
zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback");
1012+
free_on_failure: ;
1013+
for (int i = 0; i < callback_data->arg_count; ++i) {
1014+
if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
1015+
efree(callback_data->arg_types[i]);
1016+
}
1017+
}
1018+
if (callback_data->ret_type->type == FFI_TYPE_STRUCT) {
1019+
efree(callback_data->ret_type);
1020+
}
9941021
efree(callback_data);
9951022
ffi_closure_free(callback);
9961023
return NULL;

ext/ffi/tests/gh8433.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-8433 (Assigning function pointers to structs in FFI leaks memory)
3+
--FILE--
4+
<?php
5+
6+
$ffi = FFI::cdef("typedef struct { int a; } bar;");
7+
$x = $ffi->new("bar(*)(void)");
8+
FFI::addr($x)[0] = function() use ($ffi) {
9+
$bar = $ffi->new("bar");
10+
$bar->a = 2;
11+
return $bar;
12+
};
13+
var_dump($x());
14+
15+
?>
16+
--EXPECTF--
17+
object(FFI\CData:struct <anonymous>)#%d (1) {
18+
["a"]=>
19+
int(2)
20+
}

0 commit comments

Comments
 (0)