Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffi enum type (when enum has no name) make memory leak #14286

Open
avriltank opened this issue May 21, 2024 · 2 comments
Open

ffi enum type (when enum has no name) make memory leak #14286

avriltank opened this issue May 21, 2024 · 2 comments

Comments

@avriltank
Copy link

avriltank commented May 21, 2024

Description

The following code:

<?php
$ffi = FFI::cdef("
    enum{
  //enum  TestEnum{  //if enum has a name there is no memory leak
        TEST_ONE=0,
        TEST_TWO=1,
    };
");
echo $ffi->TEST_TWO; //echo 1 
//the result is right but memory leak

i found the c ffi code when enum has no name make memory leak

void zend_ffi_make_enum_type(zend_ffi_dcl *dcl) /* {{{ */
{
	zend_ffi_type *type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
	type->kind = ZEND_FFI_TYPE_ENUM;
	type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_ENUM_ATTRS);
	type->enumeration.tag_name = NULL;
	if (type->attr & ZEND_FFI_ATTR_PACKED) {
		type->size = zend_ffi_type_uint8.size;
		type->align = zend_ffi_type_uint8.align;
		type->enumeration.kind = ZEND_FFI_TYPE_UINT8;
	} else {
		type->size = zend_ffi_type_uint32.size;
		type->align = zend_ffi_type_uint32.align;
		type->enumeration.kind = ZEND_FFI_TYPE_UINT32;
	}
	dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
	dcl->attr &= ~ZEND_FFI_ENUM_ATTRS;
}

ffi_bad

PHP Version

php 8.3

Operating System

windows,linux

@avriltank avriltank changed the title ffi enum type make memory leak ffi enum type (when enm has no name) make memory leak May 21, 2024
@avriltank avriltank changed the title ffi enum type (when enm has no name) make memory leak ffi enum type (when enum has no name) make memory leak May 21, 2024
@nielsdos
Copy link
Member

Problem seems to be that for top-level anonymous type definition we never store the declaration anywhere else nor the type anywhere else. The declaration keeps owning the type and it goes out of scope. For anonymous fields this gets handled by the add_anonymous_field code that removes the type from the declaration. I think we should do something similar.
I tried this here, which seems to work, but it feels hacky: https://gist.github.com/nielsdos/62016be38fff00f97bfbee4a430cc009

@avriltank
Copy link
Author

Problem seems to be that for top-level anonymous type definition we never store the declaration anywhere else nor the type anywhere else. The declaration keeps owning the type and it goes out of scope. For anonymous fields this gets handled by the add_anonymous_field code that removes the type from the declaration. I think we should do something similar. I tried this here, which seems to work, but it feels hacky: https://gist.github.com/nielsdos/62016be38fff00f97bfbee4a430cc009

thanks,patch this patch(https://gist.github.com/nielsdos/62016be38fff00f97bfbee4a430cc009) correct this Problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants