Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odeint with empty derivative vector causes segmentation fault #13276

Open
roryyorke opened this issue Dec 21, 2020 · 2 comments
Open

odeint with empty derivative vector causes segmentation fault #13276

roryyorke opened this issue Dec 21, 2020 · 2 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.integrate

Comments

@roryyorke
Copy link
Contributor

The crash seems to happen in xerrwv, called from lsoda (see backtrace below).

solve_ivp handles the empty derivative vector case fine (see output below).

I found this while investigating the behaviour of signal.lsim2 for feedthrough (empty ABC matrix) systems; calling lsim2 with such a feedthrough system results in this crash.

Reproducing code example:

import numpy as np
import scipy
from scipy import integrate

print(f'{np.__version__ = }')
print(f'{scipy.__version__ = }')

def fprime(x, t):
    return -x

def fprime2(t, x):
    return fprime(x, t)

t_eval = np.linspace(0,1)

for x0 in [[1,1], [1], []]:
    print(f'{x0 = }')
    sivp = integrate.solve_ivp(fprime, [t_eval[0],t_eval[-1]], x0, t_eval=t_eval)
    print(f'  {sivp.y.shape = }')
    print(f'  {sivp.t.shape = }')
    
    xout = integrate.odeint(fprime, x0, np.linspace(0, 1))
    print(f'  {xout.shape = }')

Error message:

Results of above, including a stack trace:

(scipydev) rory@rory-latitude:~/projects/scipy/odeint-crash$ python -X dev odeint_crash.py
np.__version__ = '1.19.2'
scipy.__version__ = '1.7.0.dev0+ec8912c'
x0 = [1, 1]
  sivp.y.shape = (2, 50)
  sivp.t.shape = (50,)
  xout.shape = (50, 2)
x0 = [1]
  sivp.y.shape = (1, 50)
  sivp.t.shape = (50,)
  xout.shape = (50, 1)
x0 = []
  sivp.y.shape = (0, 50)
  sivp.t.shape = (50,)
Fatal Python error: Segmentation fault

Current thread 0x00007fa1d76fe740 (most recent call first):
  File "/home/rory/src/scipy/scipy/integrate/odepack.py", line 241 in odeint
  File "odeint_crash.py", line 22 in <module>
Segmentation fault (core dumped)
(scipydev) rory@rory-latitude:~/projects/scipy/odeint-crash$ gdb $(which python) core 
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/rory/.miniconda3/envs/scipydev/bin/python...

warning: core file may not match specified executable file.
[New LWP 194964]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `python odeint_crash.py'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f79e5262e30 in ?? () from /usr/lib/x86_64-linux-gnu/libgfortran.so.5
(gdb) bt
#0  0x00007f79e5262e30 in ?? () from /usr/lib/x86_64-linux-gnu/libgfortran.so.5
#1  0x00007f79e5273d98 in ?? () from /usr/lib/x86_64-linux-gnu/libgfortran.so.5
#2  0x00007f79e41dcbd0 in xerrwv (msg=..., nmes=<optimized out>, nerr=<optimized out>, level=0, ni=1, i1=0, i2=0, nr=0, r1=0, 
    r2=0) at scipy/integrate/odepack/xerrwv.f:84
#3  0x00007f79e41d1b69 in lsoda (f=<optimized out>, neq=..., y=..., t=<optimized out>, tout=0.020408163265306121, itol=1, 
    rtol=..., atol=..., itask=1, istate=1, iopt=1, rwork=..., lrw=22, iwork=..., liw=20, 
    jac=0x7f79e41cf6b0 <ode_jacobian_function>, jt=2) at scipy/integrate/odepack/lsoda.f:1554
#4  0x00007f79e41d08da in odepack_odeint (dummy=<optimized out>, args=<optimized out>, kwdict=<optimized out>)
    at scipy/integrate/_odepackmodule.c:713
#5  0x000055b0ab622f76 in cfunction_call_varargs (kwargs=<optimized out>, args=<optimized out>, func=0x7f79e43bfd10)
    at /tmp/build/80754af9/python_1599203911753/work/Objects/call.c:742
#6  PyCFunction_Call (func=0x7f79e43bfd10, args=<optimized out>, kwargs=<optimized out>)
    at /tmp/build/80754af9/python_1599203911753/work/Objects/call.c:772
#7  0x000055b0ab5e085f in _PyObject_MakeTpCall (callable=0x7f79e43bfd10, args=<optimized out>, nargs=<optimized out>, 
    keywords=<optimized out>) at /tmp/build/80754af9/python_1599203911753/work/Objects/call.c:159
#8  0x000055b0ab667f56 in _PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x55b0ad65ab10, 
    callable=0x7f79e43bfd10) at /tmp/build/80754af9/python_1599203911753/work/Include/cpython/abstract.h:125
#9  call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=0x55b0ace19070)
    at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:4963
#10 _PyEval_EvalFrameDefault (f=<optimized out>, throwflag=<optimized out>)
    at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:3469
#11 0x000055b0ab62da92 in _PyEval_EvalCodeWithName (_co=0x7f79e43c3c90, globals=<optimized out>, locals=<optimized out>, 
    args=<optimized out>, argcount=<optimized out>, kwnames=0x0, kwargs=0x7f79f39495d8, kwcount=<optimized out>, kwstep=1, 
    defs=0x7f79e44a57d8, defcount=19, kwdefs=0x0, closure=0x0, name=0x7f79f38429f0, qualname=0x7f79f38429f0)
    at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:4298
#12 0x000055b0ab62e943 in _PyFunction_Vectorcall (func=<optimized out>, stack=0x7f79f39495c0, nargsf=<optimized out>, 
    kwnames=<optimized out>) at /tmp/build/80754af9/python_1599203911753/work/Objects/call.c:435
#13 0x000055b0ab5a377f in _PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7f79f39495c0, 
    callable=0x7f79e43cd5e0) at /tmp/build/80754af9/python_1599203911753/work/Include/cpython/abstract.h:127
#14 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=0x55b0ace19070)
    at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:4963
#15 _PyEval_EvalFrameDefault (f=<optimized out>, throwflag=<optimized out>)
    at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:3469
#16 0x000055b0ab62da92 in _PyEval_EvalCodeWithName (_co=0x7f79f38cc450, globals=<optimized out>, locals=<optimized out>, 
    args=<optimized out>, argcount=<optimized out>, kwnames=0x0, kwargs=0x0, kwcount=<optimized out>, kwstep=2, defs=0x0, 
    defcount=0, kwdefs=0x0, closure=0x0, name=0x0, qualname=0x0)
    at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:4298
#17 0x000055b0ab62e754 in PyEval_EvalCodeEx (_co=<optimized out>, globals=<optimized out>, locals=<optimized out>, 
    args=<optimized out>, argcount=<optimized out>, kws=<optimized out>, kwcount=0, defs=0x0, defcount=0, kwdefs=0x0, 
    closure=0x0) at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:4327
#18 0x000055b0ab6bcedc in PyEval_EvalCode (co=<optimized out>, globals=<optimized out>, locals=<optimized out>)
    at /tmp/build/80754af9/python_1599203911753/work/Python/ceval.c:718
#19 0x000055b0ab6bcf84 in run_eval_code_obj (co=0x7f79f38cc450, globals=0x7f79f39abdc0, locals=0x7f79f39abdc0)
    at /tmp/build/80754af9/python_1599203911753/work/Python/pythonrun.c:1125
#20 0x000055b0ab6ef1f4 in run_mod (mod=<optimized out>, filename=<optimized out>, globals=0x7f79f39abdc0, 
    locals=0x7f79f39abdc0, flags=<optimized out>, arena=<optimized out>)
    at /tmp/build/80754af9/python_1599203911753/work/Python/pythonrun.c:1147
#21 0x000055b0ab5b76e1 in PyRun_FileExFlags (fp=0x55b0ace176d0, filename_str=<optimized out>, start=<optimized out>, 
    globals=0x7f79f39abdc0, locals=0x7f79f39abdc0, closeit=1, flags=0x7ffec460fc88)
    at /tmp/build/80754af9/python_1599203911753/work/Python/pythonrun.c:1063
#22 0x000055b0ab5b7ac6 in PyRun_SimpleFileExFlags (fp=0x55b0ace176d0, filename=<optimized out>, closeit=1, flags=0x7ffec460fc88)
    at /tmp/build/80754af9/python_1599203911753/work/Python/pythonrun.c:428
#23 0x000055b0ab5b898b in pymain_run_file (cf=0x7ffec460fc88, config=0x55b0ace180c0)
    at /tmp/build/80754af9/python_1599203911753/work/Modules/main.c:387
#24 pymain_run_python (exitcode=0x7ffec460fc80) at /tmp/build/80754af9/python_1599203911753/work/Modules/main.c:612
#25 Py_RunMain () at /tmp/build/80754af9/python_1599203911753/work/Modules/main.c:691
#26 0x000055b0ab6f1d19 in Py_BytesMain (argc=<optimized out>, argv=<optimized out>)
    at /tmp/build/80754af9/python_1599203911753/work/Modules/main.c:1137
#27 0x00007f79f413e0b3 in __libc_start_main (main=0x55b0ab5b9460 <main>, argc=2, argv=0x7ffec460fe88, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffec460fe78) at ../csu/libc-start.c:308
#28 0x000055b0ab681e93 in _start () at ../sysdeps/x86_64/elf/start.S:103

Scipy/Numpy/Python version information:

1.7.0.dev0+ec8912c 1.19.2 sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)
@WarrenWeckesser WarrenWeckesser added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.integrate labels Dec 21, 2020
@WarrenWeckesser
Copy link
Member

WarrenWeckesser commented Dec 21, 2020

@roryyorke, thanks for reporting the issue. When I give an empty sequence for y0 (on a Mac and on Linux, both with Python 3.7), I get a warning, not a crash, but even a warning is a bug.

I'll have a pull request that fixes this ready soon.

WarrenWeckesser added a commit to WarrenWeckesser/scipy that referenced this issue Dec 21, 2020
WarrenWeckesser added a commit to WarrenWeckesser/scipy that referenced this issue Dec 21, 2020
@roryyorke
Copy link
Contributor Author

I'm testing on Ubuntu 20.04, with the commit noted in the output. Given that the segfault happens in the error-reporting function, I suppose this could be an ABI mismatch? I can run the unit tests successfully, so whatever is causing this problem doesn't obviously affect LAPACK and BLAS calls.

I've tested the 1.6.0rc2 wheel (scipy-1.6.0rc2-cp38-cp38-manylinux1_x86_64.whl), and it produces the warning, as you said, and not a segfault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.integrate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants