Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Segmentation fault when creating a nargs>32 ufunc using frompyfunc in numpy 1.9.2 #5672

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

Closed
mhauru opened this issue Mar 12, 2015 · 1 comment · Fixed by #5673
Closed

Segmentation fault when creating a nargs>32 ufunc using frompyfunc in numpy 1.9.2 #5672

mhauru opened this issue Mar 12, 2015 · 1 comment · Fixed by #5673

Comments

@mhauru
Copy link

mhauru commented Mar 12, 2015

Title says most of it, here's a minimal example:

faulter.py reads

import numpy as np
print('numpy version:', np.version.version)

def passer(*args):
    pass

for i in (31,32):
    print('\ni =',i)
    vctrzd = np.frompyfunc(passer, i, 1)
    print('about to do it')
    vctrzd(*(np.random.rand(1),)*i)
    print('done')

In 1.8.2 this is handled nicely, as when I run this with my /usr/bin/python3 I just get a ValueError:

numpy version: 1.8.2

i = 31
about to do it
done

i = 32
about to do it
Traceback (most recent call last):
  File "faulter.py", line 11, in <module>
    vctrzd(*(np.random.rand(1),)*i)
ValueError: Cannot construct an iterator with more than 32 operands (33 were requested)

However, I have a virtualenv with the numpy 1.9.2. and there this happens:

numpy version: 1.9.2

i = 31
about to do it
done

i = 32
about to do it
Segmentation fault (core dumped)

If I run this in an interactive gdb session using

gdb env/bin/python3
(gdb) r faulter.py

I get

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff3b90700 (LWP 9139)]
[New Thread 0x7ffff338f700 (LWP 9140)]
[New Thread 0x7ffff0b8e700 (LWP 9141)]
numpy version: 1.9.2

i = 31
about to do it
done

i = 32
about to do it

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff63e3189 in PyUFunc_GenericFunction (ufunc=0x0, args=0x12, args@entry=0x7fffeb2f6c28, 
    kwds=kwds@entry=0x0, op=0x7fffffffd5d0) at numpy/core/src/umath/ufunc_object.c:2535
2535    numpy/core/src/umath/ufunc_object.c: No such file or directory.

I'm running Ubuntu 14.10 and python 3.4.2. The system-wide installation that has numpy 1.8.2. came from the Ubuntu binary repositories where as the virtualenv one with numpy 1.9.2. was installed with pip.

Let me know if more details are needed/helpful.

@jaimefrio
Copy link
Member

The surprising thing is that it appears to work on 1.8.2, we are just getting lucky and the out of bounds memory that we are accessing happens to be in bounds for some other variable, so nothing awfully wrong seems to happen for i= 32, but if you keep going:

In [1]: import numpy as np

In [2]: print('numpy version:', np.version.version)
('numpy version:', '1.8.0')

In [3]: def passer(*args):
   ...:         pass
   ...:

In [4]: for i in range(31, 256):
   ...:     vctrzd = np.frompyfunc(passer, i, 1)
   ...:     print 'About to do it for {}'.format(i)
   ...:     try:
   ...:         vctrzd(*(np.random.rand(1),)*i)
   ...:     except ValueError:
   ...:         print 'Doing it with {} failed'.format(i)
   ...:
About to do it for 31
About to do it for 32
Doing it with 32 failed
About to do it for 33
Doing it with 33 failed
About to do it for 34
Doing it with 34 failed
About to do it for 35
Doing it with 35 failed
About to do it for 36
Doing it with 36 failed
About to do it for 37
*** Reference count error detected:
an attempt was made to deallocate 17 (O) ***

Wherever we instantiate a PyUfuncObject, which I believe is in:

  • PyUfunc_FromFuncAndDataAndSignature in ufunc_object.c
  • ufunc_frompyfunc in umathmodule.c

we need to explicitly check for their number of arguments not exceeding NPY_MAXARGS.

Having to do this twice illustrates the need to at some point refactor this code to have a proper PyUFuncObject constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants