最近在学习开发php扩展,创建类的时候,编译成功,但是执行测试文件的时候报错了:Segmentation fault (core dumped)
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "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)
{
#if defined(COMPILE_DL_PERSON) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
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
};
/* }}} */
#ifdef COMPILE_DL_PERSON
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(Person)
#endif
php测试代码
$obj = new Person();
$obj->setname("aaaaa");
gdb 调试截图
2 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
可以参考这个源码文件中的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
请看问题第二张图,很明显是内存越界了,无法访问的地址是高地址,在栈上,说明有地方的指针变量没加*,贴出的代码没问题
- 2 回答
- 0 关注
- 900 浏览
添加回答
举报
0/150
提交
取消