Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand All @@ -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 */
Expand All @@ -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;
}
Expand Down
61 changes: 61 additions & 0 deletions ext/ffi/tests/bug25837.phpt
Original file line number Diff line number Diff line change
@@ -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--
<?php

$ffi = FFI::cdef(<<<EOF
typedef struct {
uint8_t numbers[2];
uint8_t other;
} test;

typedef struct {
test inner[1][2];
uint8_t other;
} wrapper;

typedef struct {
uint8_t (*read_first)(test x, uint8_t last);
uint8_t (*read_second)(test x, uint8_t last);
uint8_t (*read_third)(test x, uint8_t last);
uint8_t (*read_first_wrapped)(wrapper x, uint8_t last);
} functions;
EOF,
);

$functions = $ffi->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)
Loading