From 00962c8d28446e213ead3b4065f83ad2d14ea96b Mon Sep 17 00:00:00 2001 From: DPDmancul Date: Fri, 27 Nov 2026 16:00:15 +0100 Subject: [PATCH] fix php#25837: FFI: struct with an array field passed by value is described to libffi with a void element --- ext/ffi/ffi.c | 73 ++++++++++++++++++++++++++++++------- ext/ffi/tests/bug25837.phpt | 61 +++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 ext/ffi/tests/bug25837.phpt diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 9977008f83da..53995dc4121e 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -354,7 +354,10 @@ static bool zend_ffi_is_compatible_type(zend_ffi_type *dst_type, zend_ffi_type * } /* }}} */ -static ffi_type* zend_ffi_face_struct_add_fields(ffi_type* t, zend_ffi_type *type, int *i, size_t size) +static ffi_type* zend_ffi_face_inner_struct_add_fields(ffi_type* t, zend_ffi_type *type, int *i, size_t *size); +static ffi_type* zend_ffi_face_array_add_elements(ffi_type* t, zend_ffi_type *type, int *i, size_t *size); + +static ffi_type* zend_ffi_face_struct_add_fields(ffi_type* t, zend_ffi_type *type, int *i, size_t *size) { zend_ffi_field *field; @@ -396,18 +399,11 @@ static ffi_type* zend_ffi_face_struct_add_fields(ffi_type* t, zend_ffi_type *typ case ZEND_FFI_TYPE_POINTER: t->elements[(*i)++] = &ffi_type_pointer; break; + case ZEND_FFI_TYPE_ARRAY: + t = zend_ffi_face_array_add_elements(t, ZEND_FFI_TYPE(field->type), i, size); + break; case ZEND_FFI_TYPE_STRUCT: { - zend_ffi_type *field_type = ZEND_FFI_TYPE(field->type); - /* for unions we use only the first field */ - uint32_t num_fields = !(field_type->attr & ZEND_FFI_ATTR_UNION) ? - zend_hash_num_elements(&field_type->record.fields) : 1; - - if (num_fields > 1) { - size += sizeof(ffi_type*) * (num_fields - 1); - t = erealloc(t, size); - t->elements = (ffi_type**)(t + 1); - } - t = zend_ffi_face_struct_add_fields(t, field_type, i, size); + t = zend_ffi_face_inner_struct_add_fields(t, ZEND_FFI_TYPE(field->type), i, size); break; } default: @@ -422,6 +418,57 @@ static ffi_type* zend_ffi_face_struct_add_fields(ffi_type* t, zend_ffi_type *typ return t; } +static ffi_type* zend_ffi_face_inner_struct_add_fields(ffi_type* t, zend_ffi_type *type, int *i, size_t *size) +{ + assert(type->kind == ZEND_FFI_TYPE_STRUCT); + /* for unions we use only the first field */ + uint32_t num_fields = !(type->attr & ZEND_FFI_ATTR_UNION) ? + zend_hash_num_elements(&type->record.fields) : 1; + + if (num_fields > 1) { + *size += sizeof(ffi_type*) * (num_fields - 1); + t = erealloc(t, *size); + t->elements = (ffi_type**)(t + 1); + } + return zend_ffi_face_struct_add_fields(t, type, i, size); +} + +static ffi_type *zend_ffi_get_type(zend_ffi_type *type); + +static ffi_type* zend_ffi_face_array_add_elements(ffi_type* t, zend_ffi_type *type, int *i, size_t *size) +{ + assert(type->kind == ZEND_FFI_TYPE_ARRAY); + zend_ffi_type *element_type = ZEND_FFI_TYPE(type->array.type); + zend_long length = type->array.length; + + if (length <= 0) { + return t; + } + + if (length > 1) { + *size += sizeof(ffi_type*) * (length - 1); + t = erealloc(t, *size); + t->elements = (ffi_type**)(t + 1); + } + + if (element_type->kind == ZEND_FFI_TYPE_STRUCT) { + for (zend_long n = 0; n < length; n++) { + t = zend_ffi_face_inner_struct_add_fields(t, element_type, i, size); + } + } else if (element_type->kind == ZEND_FFI_TYPE_ARRAY) { + for (zend_long n = 0; n < length; n++) { + t = zend_ffi_face_array_add_elements(t, element_type, i, size); + } + } else { + ffi_type *element_ffi_type = zend_ffi_get_type(element_type); + for (zend_long n = 0; n < length; n++) { + t->elements[(*i)++] = element_ffi_type; + } + } + + return t; +} + static ffi_type *zend_ffi_make_fake_struct_type(zend_ffi_type *type) /* {{{ */ { /* for unions we use only the first field */ @@ -436,7 +483,7 @@ static ffi_type *zend_ffi_make_fake_struct_type(zend_ffi_type *type) /* {{{ */ t->type = FFI_TYPE_STRUCT; t->elements = (ffi_type**)(t + 1); i = 0; - t = zend_ffi_face_struct_add_fields(t, type, &i, size); + t = zend_ffi_face_struct_add_fields(t, type, &i, &size); t->elements[i] = NULL; return t; } diff --git a/ext/ffi/tests/bug25837.phpt b/ext/ffi/tests/bug25837.phpt new file mode 100644 index 000000000000..54b35e7d002a --- /dev/null +++ b/ext/ffi/tests/bug25837.phpt @@ -0,0 +1,61 @@ +--TEST-- +GH-25837 (FFI: struct with an array field passed by value is described to libffi with a void element) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +new('functions'); + +$functions->read_first = fn ($test, $last) => $test->numbers[0]; +$functions->read_second = fn ($test, $last) => $test->numbers[1]; +$functions->read_third = fn ($test, $last) => $last; +$functions->read_first_wrapped = fn ($test, $last) => $test->inner[0][0]->numbers[0]; + +$test = $ffi->new("test"); +$test->numbers[0] = 1; +$test->numbers[1] = 2; +$test->other = 3; + +var_dump(($functions->read_first)($test, 4)); +var_dump(($functions->read_second)($test, 4)); +var_dump(($functions->read_third)($test, 4)); + +$wrapper = $ffi->new('wrapper'); +$wrapper->inner = $ffi->new("test[1][2]"); +$wrapper->inner[0][0] = $test; +$wrapper->inner[0][1] = $ffi->new("test"); +$wrapper->inner[0][1]->numbers[0] = 4; +$wrapper->inner[0][1]->numbers[1] = 5; +$wrapper->other = 5; + +var_dump(($functions->read_first_wrapped)($wrapper, 7)); + +?> +--EXPECT-- +int(1) +int(2) +int(4) +int(1)