Php extension development Segmentation fault (core dumped)

recently, when I was learning to develop a php extension and create a class, I compiled successfully, but I reported an error: Segmentation fault (core dumped) when executing the test file


/* $Id$ */

-sharpifdef HAVE_CONFIG_H
-sharpinclude "config.h"
-sharpendif

-sharpinclude "php.h"
-sharpinclude "php_ini.h"
-sharpinclude "ext/standard/info.h"
-sharpinclude "php_Person.h"

/* If you declare any globals in php_Person.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(Person)
*/

/* True global resources - no need for thread safety here */
static int le_Person;

zend_class_entry *person_ce;

ZEND_BEGIN_ARG_INFO(arg_person_setname, 0)
    ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("Person.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_Person_globals, Person_globals)
    STD_PHP_INI_ENTRY("Person.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_Person_globals, Person_globals)
PHP_INI_END()
*/
/* }}} */

/* Remove the following function when you have successfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_Person_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_Person_compiled)
{
    char *arg = NULL;
    size_t arg_len, len;
    zend_string *strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
        return;
    }

    strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "Person", arg);

    RETURN_STR(strg);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
   unfold functions in source code. See the corresponding marks just before
   function definition, where the functions purpose is also documented. Please
   follow this convention for the convenience of others editing your code.
*/

PHP_METHOD(Person, __construct)
{
    php_printf("begin");
}

PHP_METHOD(Person, __destruct)
{
    php_printf("end");
}

PHP_METHOD(Person, getname)
{
    zval *self, *name, rv;
    self = getThis();
    name = zend_read_property(Z_OBJCE_P(self), self, "name", sizeof("name") - 1, 0, &rv);
    //int type;
    //type = Z_TYPE_P(name);
    //RETURN_LONG(type);
    RETURN_STRING(Z_STRVAL_P(name));
}

PHP_METHOD(Person, setname)
{
    char *name;
    int arg_len;
    zval *value ,*self;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &arg_len) == FAILURE) {
        return;
    }
    self = getThis();
    zend_update_property_string(Z_OBJCE_P(self), self, "name", sizeof("name")-1, name);
    //RETURN_TRUE;
}

/* {{{ php_Person_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_Person_init_globals(zend_Person_globals *Person_globals)
{
    Person_globals->global_value = 0;
    Person_globals->global_string = NULL;
}
*/
/* }}} */

/* {{{ Person_functions[]
 *
 * Every user visible function must have an entry in Person_functions[].
 */
const zend_function_entry Person_functions[] = {
    PHP_ME(Person, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(Person, __destruct,  NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
    PHP_ME(Person, getname,     NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Person, setname, arg_person_setname, ZEND_ACC_PUBLIC)
    PHP_FE(confirm_Person_compiled,    NULL)        /* For testing, remove later. */
    PHP_FE_END    /* Must be the last line in Person_functions[] */
};
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(Person)
{
    /* If you have INI entries, uncomment these lines
    REGISTER_INI_ENTRIES();
    */
    zend_class_entry person;
    INIT_CLASS_ENTRY(person, "Person", Person_functions);
    person_ce = zend_register_internal_class_ex(&person, NULL);
    zend_declare_property_string(person_ce, ZEND_STRL("name"),"test", ZEND_ACC_PUBLIC);
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(Person)
{
    /* uncomment this line if you have INI entries
    UNREGISTER_INI_ENTRIES();
    */
    return SUCCESS;
}
/* }}} */

/* Remove if there"s nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(Person)
{
-sharpif defined(COMPILE_DL_PERSON) && defined(ZTS)
    ZEND_TSRMLS_CACHE_UPDATE();
-sharpendif
    return SUCCESS;
}
/* }}} */

/* Remove if there"s nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(Person)
{
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(Person)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "Person support", "enabled");
    php_info_print_table_end();

    /* Remove comments if you have entries in php.ini
    DISPLAY_INI_ENTRIES();
    */
}
/* }}} */


/* {{{ Person_module_entry
 */
zend_module_entry Person_module_entry = {
    STANDARD_MODULE_HEADER,
    "Person",
    Person_functions,
    PHP_MINIT(Person),
    PHP_MSHUTDOWN(Person),
    PHP_RINIT(Person),        /* Replace with NULL if there"s nothing to do at request start */
    PHP_RSHUTDOWN(Person),    /* Replace with NULL if there"s nothing to do at request end */
    PHP_MINFO(Person),
    PHP_PERSON_VERSION,
    STANDARD_MODULE_PROPERTIES
};
/* }}} */

-sharpifdef COMPILE_DL_PERSON
-sharpifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
-sharpendif
ZEND_GET_MODULE(Person)
-sharpendif

php test code

$obj = new Person();
$obj->setname("aaaaa");

gdb debug screenshot


the error message says that your access to memory is out of bounds. There should be a problem with this method:

zend_update_property_string(Z_OBJCE_P(self), self, "name", sizeof("name")-1, name);
The first method of

this function is that the instance of the class is usually a pointer type of zend_class_entry . What you provide is obviously not an instance of the class.

try this:

zend_update_property_string(person_ce,  getThis(), "name", sizeof("name")-1, name);

Exception
\php-7.0.27\Zend\zend_exceptions.c 256 line

ZEND_METHOD(exception, __construct)//Exception,

zend_string *message;
zend_update_property_str(base_ce, object, "message", sizeof("message")-1, message);
:
ZEND_API void zend_update_property_str(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_string *value)

char *
ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value)

api
\php-7.0.27\Zend\zend_API.c line 3900



Please take a look at the second picture of the problem. It is obvious that the memory is out of bounds, and the address that cannot be accessed is the high address. On the stack, it shows that there are some pointer variables that have not been added *, and the posted code is fine

.

@ lifei6671 this is adapted to warning, but the test file can be executed

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e54952-43f7c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e54952-43f7c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?