lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 157 forks source link

SIGSEGV in LPython #2498

Closed rebcabin closed 5 months ago

rebcabin commented 7 months ago

This code is incorrect, but should produce an error message instead of a SIGSEGV. The error is in the return type of spot_print.... Changing the return type to None makes the problem go away. Also removing a call of main makes the problem go away because the code does not run.

from numpy import array, empty, int16
from lpython import (i16, i32, ccallback, c_p_pointer, Pointer, u64, CPtr, i64,
                     ccall, sizeof, Array, Allocatable, TypeVar, Const)

rows = TypeVar("rows")
cols = TypeVar("cols")

def spot_print_lpython_array(a: i16[:], rows: i32, cols: i32) -> i16[rows, cols]:
    pass

def main() -> i32:
    print ("hello, world!")
    return 0

if __name__ == "__main__":
    main()
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)──────────────────────────────────────────────────────────────────────────────(brian@MacBook-Pro:s001)─┐
└─(20:01:55 on vector-backend ✹ ✭)──> lpython ../ISSUES/SIGSEGV/Issue2498.py                                                             ──(Tue,Feb06)─┘
Traceback (most recent call last):
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 1872
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 824
    res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_evaluator.cpp", line 71
    run_fn, infile);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 9276
    pass_manager.apply_passes(al, &asr, co.po, diagnostics);
  File "/Users/brian/Documents/GitHub/lpython/src/libasr/pass/pass_manager.h", line 299
    apply_passes(al, asr, _passes, pass_options, diagnostics);
  File "/Users/brian/Documents/GitHub/lpython/src/libasr/pass/pass_manager.h", line 160
    _passes_db[passes[i]](al, *asr, pass_options);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/libasr/pass/subroutine_from_function.cpp", line 389
    CreateFunctionFromSubroutine v(al);
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/libasr/pass/subroutine_from_function.cpp", line 44
    ASR::symbol_t *mod = x.m_symtab->get_symbol(item);
  File "../libasr/asr.h", line 5060
  File "../libasr/asr.h", line 4774
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/libasr/pass/subroutine_from_function.cpp", line 59
    PassUtils::handle_fn_return_var(al,
  File "../libasr/pass/pass_utils.h", line 838
  File "../libasr/pass/pass_utils.h", line 838
  Binary file "/usr/lib/system/libsystem_platform.dylib", local address: 0x18046da23
Segfault: Signal SIGSEGV (segmentation fault) received
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)──────────────────────────────────────────────────────────────────────────────(brian@MacBook-Pro:s001)─┐
└─(20:03:08 on vector-backend ✹ ✭)──> PYTHONPATH=".:../src/runtime/lpython:.." LPYTHON_PY_MOD_NAME="" LPYTHON_PY_MOD_PATH="_lpython-tmp-test-cpython" python ../ISSUES/SIGSEGV/Issue2498.py
hello, world!
kmr-srbh commented 5 months ago

@rebcabin I am unable to reproduce this bug. I tried reproducing it by calling the function with the required arguments, and got the following different error:

(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
ASR verify pass error: ASR verify: The variable in ArrayItem must be an array, not a scalar
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython", in _start()
  File "./csu/../csu/libc-start.c", line 360, in __libc_start_main_impl()
  File "./csu/../sysdeps/x86/libc-start.c", line 58, in __libc_start_call_main()
  File "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython.cpp", line 1932, in main()
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython.cpp", line 838, in compile_python_to_object_file()
    !(arg_c && compiler_options.po.disable_main), "__main__", infile);
LCompilersException: Verify failed
Shaikh-Ubaid commented 5 months ago

This code is incorrect, but should produce an error message instead of a SIGSEGV. The error is in the return type of spot_print...

I think the return type is fine. We have some examples in integration tests that use a return type as used in your example (for example see integration_tests/bindpy_04.py.

As per the latest main, the example shared in the description works fine for me.

% cat examples/expr2.py         
from numpy import array, empty, int16
from lpython import (i16, i32, ccallback, c_p_pointer, Pointer, u64, CPtr, i64,
                     ccall, sizeof, Array, Allocatable, TypeVar, Const)

rows = TypeVar("rows")
cols = TypeVar("cols")

def spot_print_lpython_array(a: i16[:], rows: i32, cols: i32) -> i16[rows, cols]:
    pass

def main() -> i32:
    print ("hello, world!")
    return 0

if __name__ == "__main__":
    main()
% python examples/expr2.py      
hello, world!
% lpython_in_main examples/expr2.py
hello, world!
% echo $?
0
Shaikh-Ubaid commented 5 months ago

The following modified example also works for me.

% cat examples/expr2.py         
from numpy import array, empty, int16
from lpython import (i16, i32, ccallback, c_p_pointer, Pointer, u64, CPtr, i64,
                     ccall, sizeof, Array, Allocatable, TypeVar, Const)

rows = TypeVar("rows")
cols = TypeVar("cols")

def spot_print_lpython_array(a: i16[:], rows: i32, cols: i32) -> i16[rows, cols]:
    res: i16[rows, cols] = empty((rows, cols), dtype=int16)
    res[0, 2] = i16(-20)
    res[1, 2] = i16(102)
    return res

def main() -> i32:
    a: i16[5] = empty(5, dtype=int16)
    b: i16[2, 3]
    b = spot_print_lpython_array(a, 2, 3)
    print(b[0, 2], b[1, 2])
    print ("hello, world!")
    return 0

if __name__ == "__main__":
    main()
% python examples/expr2.py         
-20 102
hello, world!
% lpython_in_main examples/expr2.py
-20 102
hello, world!
Shaikh-Ubaid commented 5 months ago

@rebcabin and @kmr-srbh please check if the example in the issue description works for you with the latest main and we can close this issue.

kmr-srbh commented 5 months ago

The above example provided by you works for me. :+1: I noticed that I had passed the wrong input, which yielded the above error.

Shaikh-Ubaid commented 5 months ago

Thanks for trying the example. I am closing this issue as it seems fixed. We can open open it later if there is any issue in future.