Hi! I'm compiled shared library for using inside C...
# kotlin-native
r
Hi! I'm compiled shared library for using inside C project. But received
Segmentation Fault
when use it.
gdb
shows surprising error
Copy code
Program received signal SIGSEGV, Segmentation fault.
kfun:kotlinx.cinterop.toKString@kotlinx.cinterop.CPointer<kotlinx.cinterop.ByteVarOf<kotlin.Byte>>.()kotlin.String ()
    at /opt/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt:402
402     /opt/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt: No such file or directory.
o
please prepare self-contained reproducer, we cannot say much from info you provided
r
I will try, but I think that it's almost impossible. Maybe the following will be helpful. Whole project there https://github.com/rjhdby/kotlin-native-php-extension backtrace
Copy code
#0  kfun:kotlinx.cinterop.toKString@kotlinx.cinterop.CPointer<kotlinx.cinterop.ByteVarOf<kotlin.Byte>>.()kotlin.String ()
    at /opt/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt:402
#1  0x00007fffe71c913e in kfun:php.extension.proxy.getString@kotlinx.cinterop.CPointer<php._zval_struct>.()kotlin.String ()
   from ./phpmodule/modules/example.so
#2  0x00007fffe71c9d92 in kfun:printArray(kotlinx.cinterop.CPointer<php._zval_struct>) ()
   from ./phpmodule/modules/example.so
#3  0x00007fffe71c449a in _konan_function_8_impl(void*) () from ./phpmodule/modules/example.so
#4  0x00007fffe71c2fc1 in zif_printArray (execute_data=<optimized out>, return_value=0x7fffffffcd30)
    at /root/simpleExtension/phpmodule/extension.c:183
#5  0x0000555555802e7a in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER ()
#6  0x00005555557f2cab in execute_ex ()
#7  0x0000555555846964 in zend_execute ()
#8  0x000055555579c168 in zend_eval_stringl ()
#9  0x000055555579c319 in zend_eval_stringl_ex ()
#10 0x00005555558488e3 in do_cli ()
#11 0x0000555555624bbf in main ()
Last called function
Copy code
fun zval.getString() = __zp_zval_to_string(this.ptr)?.toKString() ?: ""
function in
.def
Copy code
static inline char* __zp_zval_to_string(zval *z_value){
    return Z_STRVAL_P(z_value);
}
function in compiled
.def
Copy code
fun __zp_zval_to_string(z_value: CValuesRef<zval>?): CPointer<ByteVar>? {
    memScoped {
        return interpretCPointer<ByteVar>(kniBridge1239(z_value?.getPointer(memScope).rawValue))
    }
}
K/N version
Copy code
info: kotlinc-native 1.2.60-dev-444 (JRE 1.8.0_171-b10)
Kotlin/Native: 0.7.1
I sure that problem appear on call
toKString()
in this code
Copy code
fun zval.getString() = __zp_zval_to_string(this.ptr)?.toKString() ?: ""

static inline char* __zp_zval_to_string(zval *z_value){
    return Z_STRVAL_P(z_value);
}
When I cast same
zval
to Long, all works properly
Copy code
fun zval.getLong() = __zp_zval_to_long(this.ptr)

static inline int32_t __zp_zval_to_long(zval *z_value){
    return Z_LVAL_P(z_value);
}
Root cause is uncorrect work with
union
.
Copy code
union {
    uint32_t   intVal;
    char * strVal;
Error appears if structure defined with
intVar
, but requested as
strVal
. But error like
/opt/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt: No such file or directory.
is confusing
o
Memory overlays aka unions must be worked with carefully, and K/N couldn’t help much here. The problem with debug info you reported is known and will be fixed once we switch to intermediate representation (IR) based libraries