exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
15.13k stars 520 forks source link

Debugging Segmentation Faults in Codon #576

Closed gyuro closed 1 day ago

gyuro commented 3 months ago

In C:

#ifdef __cplusplus
extern "C" {
#endif

__attribute__((visibility("default"))) std::array<double, 4> foo() {
    std::array<double, 4> bar;
    ....
    return bar;
}

#ifdef __cplusplus
}
#endif

In Codon:

LIBPATH = "foo.so"
from C import LIBPATH.foo() -> Ptr[float]

def run():
    while True:
        bar = foo()

In the above scenarios, when running the Codon script, the bar value is initially set to null or a dummy value. However, subsequent iterations seem to work fine. I'm not sure what the problem is at this point.

Additionally, printing the bar variable during the first iteration results in a segmentation fault. It appears this issue is related to the garbage collector (GC) in Codon. This information might be useful for debugging.

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff76ec712 in GC_find_limit_with_bound () from /home/user/.codon/lib/codon/libcodonrt.so
(gdb) bt
#0  0x00007ffff76ec712 in GC_find_limit_with_bound () from /home/user/.codon/lib/codon/libcodonrt.so
#1  0x00007ffff76ec530 in GC_init_linux_data_start () from /home/user/.codon/lib/codon/libcodonrt.so
#2  0x00007ffff76ea71d in GC_init () from /home/user/.codon/lib/codon/libcodonrt.so
#3  0x00007ffff76a7638 in seq_init () from /home/user/.codon/lib/codon/libcodonrt.so
#4  0x00007ffff7fb7548 in main[unclash] () from foo.so
#5  0x00007ffff7fc947e in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffd808, env=env@entry=0x7fffffffd818) at ./elf/dl-init.c:70
#6  0x00007ffff7fc9568 in call_init (env=0x7fffffffd818, argv=0x7fffffffd808, argc=1, l=<optimized out>) at ./elf/dl-init.c:33
#7  _dl_init (main_map=0x7ffff7ffe2e0, argc=1, argv=0x7fffffffd808, env=0x7fffffffd818) at ./elf/dl-init.c:117
#8  0x00007ffff7fe32ca in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#9  0x0000000000000001 in ?? ()
#10 0x00007fffffffdc5d in ?? ()
#11 0x0000000000000000 in ?? ()

Any comments would be welcomed.

arshajii commented 3 months ago

Hi @gyuro -- there are a couple potential issues here:

Probably best to return a raw pointer (e.g. bar.data()) and copy it on the Codon side. Does it work if you do this?

gyuro commented 3 months ago

IMO, it's more effective to pass the arguments that need to be returned.

Thanks for your feedback!