zephir-lang / zephir

Zephir is a compiled high-level language aimed to ease the creation of C-extensions for PHP
https://zephir-lang.com
MIT License
3.31k stars 466 forks source link

Segfault with Mvc\Models #2003

Closed zsilbi closed 3 years ago

zsilbi commented 5 years ago

I tried a slightly customized version of EagerLoading from the incubator with the current Phalcon 4.0.x branch. Everything works very well, but in some cases I got SEGFAULT errors.

I noticed, that there are strange = null lines before passing the actual values in some cases.

I thought that this must have been done like this to avoid bugs in the earlier versions, but in the end, I had to add another of this (= null) to avoid the SEGFAULT error.

if ($isSingle) {
    $record->{$alias} = null; // I had to add this line
    $record->{$alias} = empty($records) ? null : $records[0]; // The SEGFAULT happens here
} else {
    if (empty($records)) {
        $record->{$alias} = null; // This was already here
        $record->{$alias} = [];
    } else {
        $record->{$alias} = $records;

        if (static::$isPhalcon2) {
            $record->{$alias} = null; // This was already here also
            $record->{$alias} = $records;
        }
    }
}

https://github.com/phalcon/incubator/blob/4883d9009a9d651308bfc201a0e9440c0ff692e2/Library/Phalcon/Mvc/Model/EagerLoading/EagerLoad.php#L195-L209

This is the backtrace I got.

Before the error occurs, these are the values:

$alias = 'language';
$records = new Languages(); // extends Mvc\Model

I actually changed $records[0] to new Languages() and the error remains. I tried removing everything from the initialization of that model, didn't help. But if I change new Languages to any other model, it works well.

#0  zend_mm_alloc_small (bin_num=6, size=56, heap=0x7fad6a400040) at ./Zend/zend_alloc.c:1289
#1  _emalloc_56 () at ./Zend/zend_alloc.c:2428
#2  0x00005619f351e532 in zend_array_dup (source=0x7fad379b9e6f) at ./Zend/zend_hash.c:1924
#3  0x00007fad4eec9a31 in zephir_update_property_zval (object=0x7fff453537b0, property_name=0x7fad50d794a3 "related", property_length=7, value=0x7fff45353780) at /cphalcon/ext/kernel/object.c:622
#4  0x00007fad4f59fe7b in zephir_init_properties_Phalcon_Mvc_Model (class_type=0x7fad38fa8440) at /cphalcon/ext/phalcon/mvc/model.zep.c:10452
#5  0x00005619f350ebdb in object_and_properties_init (arg=arg@entry=0x7fad6a421870, class_type=class_type@entry=0x7fad38fa8440, properties=properties@entry=0x0) at ./Zend/zend_API.c:1335
#6  0x00005619f350ecd7 in object_init_ex (arg=arg@entry=0x7fad6a421870, class_type=class_type@entry=0x7fad38fa8440) at ./Zend/zend_API.c:1343
#7  0x00005619f358c554 in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER () at ./Zend/zend_vm_execute.h:8829
#8  0x00005619f35959e4 in execute_ex (ex=0x7fad6a400040) at ./Zend/zend_vm_execute.h:56311
#9  0x00005619f34ff4e3 in zend_call_function (fci=0x7fff45353b90, fci_cache=<optimized out>) at ./Zend/zend_execute_API.c:756
#10 0x00007fad4eedbc51 in zephir_call_user_function (object_pp=0x7fff453541b0, obj_ce=0x7fad37b3d380, type=zephir_fcall_method, function_name=0x7fff45353e10, retval_ptr=0x7fff45354160, cache_entry=0x0, cache_slot=0,
    param_count=3, params=0x7fff453541f0) at /cphalcon/ext/kernel/fcall.c:378
#11 0x00007fad4eedc5fe in zephir_call_class_method_aparams (return_value=0x7fff45354160, ce=0x7fad37b3d380, type=zephir_fcall_method, object=0x7fff453541b0, method_name=0x7fad6a5fa2b8 "afterexecuteroute", method_len=17,
    cache_entry=0x0, cache_slot=0, param_count=3, params=0x7fff453541f0) at /cphalcon/ext/kernel/fcall.c:516
#12 0x00007fad4f2cc271 in zim_Phalcon_Events_Manager_fireQueue (execute_data=0x7fad6a421540, return_value=0x7fff45354ac0) at /cphalcon/ext/phalcon/events/manager.zep.c:539
#13 0x00005619f34ff3ad in zend_call_function (fci=0x7fff453544c0, fci_cache=<optimized out>) at ./Zend/zend_execute_API.c:770
#14 0x00007fad4eedbc51 in zephir_call_user_function (object_pp=0x7fad6a4214d0, obj_ce=0x7fad6a41e240, type=zephir_fcall_method, function_name=0x7fff45354740, retval_ptr=0x7fff45354ac0, cache_entry=0x7fff45354390, cache_slot=231,
    param_count=2, params=0x7fff45354b40) at /cphalcon/ext/kernel/fcall.c:378
#15 0x00007fad4eedc5fe in zephir_call_class_method_aparams (return_value=0x7fff45354ac0, ce=0x7fad6a41e240, type=zephir_fcall_method, object=0x7fad6a4214d0, method_name=0x7fad50626c92 "firequeue", method_len=9, cache_entry=0x0,
    cache_slot=231, param_count=2, params=0x7fff45354b40) at /cphalcon/ext/kernel/fcall.c:516
#16 0x00007fad4f2cab09 in zim_Phalcon_Events_Manager_fire (execute_data=0x7fad6a4214b0, return_value=0x7fad6a421490) at /cphalcon/ext/phalcon/events/manager.zep.c:441
#17 0x00005619f359bc4e in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at ./Zend/zend_vm_execute.h:1106
#18 execute_ex (ex=0x7fad6a400040) at ./Zend/zend_vm_execute.h:55497
#19 0x00005619f34ff4e3 in zend_call_function (fci=0x7fff45354ed0, fci_cache=<optimized out>) at ./Zend/zend_execute_API.c:756
#20 0x00007fad4eedbc51 in zephir_call_user_function (object_pp=0x7fff45356b80, obj_ce=0x7fad6a41e240, type=zephir_fcall_method, function_name=0x7fff45355150, retval_ptr=0x7fff45356ff0, cache_entry=0x7fff45354da0, cache_slot=0,
    param_count=3, params=0x7fff45357110) at /cphalcon/ext/kernel/fcall.c:378
#21 0x00007fad4eedc5fe in zephir_call_class_method_aparams (return_value=0x7fff45356ff0, ce=0x7fad6a41e240, type=zephir_fcall_method, object=0x7fff45356b80, method_name=0x7fad4fc8bd41 "fire", method_len=4,
    cache_entry=0x7fff453553a0, cache_slot=0, param_count=3, params=0x7fff45357110) at /cphalcon/ext/kernel/fcall.c:516
#22 0x00007fad4f016d2b in zim_Phalcon_Dispatcher_AbstractDispatcher_dispatch (execute_data=0x7fad6a421390, return_value=0x7fff45358a20) at /cphalcon/ext/phalcon/dispatcher/abstractdispatcher.zep.c:791
#23 0x00005619f34ff3ad in zend_call_function (fci=0x7fff453573d0, fci_cache=<optimized out>) at ./Zend/zend_execute_API.c:770
#24 0x00007fad4eedbc51 in zephir_call_user_function (object_pp=0x7fff45358990, obj_ce=0x7fad37b3d980, type=zephir_fcall_method, function_name=0x7fff45357650, retval_ptr=0x7fff45358a20, cache_entry=0x0, cache_slot=0,
    param_count=0, params=0x7fff45358d00) at /cphalcon/ext/kernel/fcall.c:378
#25 0x00007fad4eedc5fe in zephir_call_class_method_aparams (return_value=0x7fff45358a20, ce=0x7fad37b3d980, type=zephir_fcall_method, object=0x7fff45358990, method_name=0x7fad50ccb4bf "dispatch", method_len=8, cache_entry=0x0,
    cache_slot=0, param_count=0, params=0x7fff45358d00) at /cphalcon/ext/kernel/fcall.c:516
#26 0x00007fad4f4c87f3 in zim_Phalcon_Mvc_Application_handle (execute_data=0x7fad6a421330, return_value=0x7fad6a421320) at /cphalcon/ext/phalcon/mvc/application.zep.c:385
#27 0x00005619f359bc4e in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at ./Zend/zend_vm_execute.h:1106
#28 execute_ex (ex=0x7fad6a400040) at ./Zend/zend_vm_execute.h:55497
#29 0x00005619f359c3f3 in zend_execute (op_array=op_array@entry=0x7fad6a480000, return_value=0x0, return_value@entry=0x7fad6a476e00) at ./Zend/zend_vm_execute.h:60889
#30 0x00005619f350d232 in zend_execute_scripts (type=type@entry=8, retval=0x7fad6a476e00, retval@entry=0x0, file_count=1782714752, file_count@entry=3) at ./Zend/zend.c:1568
#31 0x00005619f34ad2c0 in php_execute_script (primary_file=0x7fff4535b390) at ./main/main.c:2639
#32 0x00005619f336692b in main (argc=<optimized out>, argv=<optimized out>) at ./sapi/fpm/fpm/fpm_main.c:1950

If this doesn't help, I'll try to reproduce this issue in a clean example, but it seems like a pretty time consuming task to do so.

PHP 7.3.11-1+ubuntu18.04.1 Phalcon current 4.0.x branch Zephir 0.12.12

sergeyklay commented 5 years ago

@zsilbi Yes, it is doesn't help :( We'll need a code to reproduce as small as possible.

Jurigag commented 5 years ago

Don't add backtrace without debug version and symbols, it doesn't tell anything. Also reproduce it without and dependencies.

zsilbi commented 5 years ago

Thanks, @Jurigag. I modified the backtrace (debug mode and symbols)

Jeckerson commented 3 years ago

@zsilbi Does it still occur on latest Zephir?