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

BUG: matmul.at segfaults #21301

Closed
dbeutel opened this issue Apr 6, 2022 · 4 comments
Closed

BUG: matmul.at segfaults #21301

dbeutel opened this issue Apr 6, 2022 · 4 comments
Labels
00 - Bug sprintable Issue fits the time-frame and setting of a sprint

Comments

@dbeutel
Copy link

dbeutel commented Apr 6, 2022

Describe the issue:

Calling matmul.at on arrays that pass all checks on shape, indices, etc, leads to a segmentation fault.

Expected result
Calling other methods of ufuncs with a non-trivial signature raises either a RuntimeError: Reduction not defined on ufunc with signature or, in the case of outer, a TypeError: method outer is not allowed in ufunc with non-trivial signature. Because at is not a reduction, my expectation is getting a TypeError: method at is not allowed in ufunc with non-trivial signature. I took the liberty to make the tiny adaption that implements the expected behavior here: dbeutel@6db8d96

Reproduce the code example:

import numpy as np

arr = np.ones((2, 2, 2))
np.matmul.at(arr, [0], np.ones((1, 2, 2)))

Error message:

$ gdb --args python-dbg segfault_matmul_at.py
GNU gdb (GDB) 11.2
Copyright (C) 2022 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-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://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 python-dbg...
(gdb) run
Starting program: /usr/bin/python-dbg segfault_matmul_at.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7ffff45ff640 (LWP 5504)]
[New Thread 0x7ffff3dfe640 (LWP 5505)]
[New Thread 0x7ffff15fd640 (LWP 5506)]
[New Thread 0x7fffecdfc640 (LWP 5507)]
[New Thread 0x7fffea5fb640 (LWP 5508)]
[New Thread 0x7fffe7dfa640 (LWP 5509)]
[New Thread 0x7fffe55f9640 (LWP 5510)]

Thread 1 "python-dbg" received signal SIGSEGV, Segmentation fault.
0x00007ffff707fb00 in DOUBLE_matmul () from /home/dbeutel/.local/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so
(gdb) bt
#0  0x00007ffff707fb00 in DOUBLE_matmul () from /home/dbeutel/.local/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so
#1  0x00007ffff70842e5 in generic_wrapped_legacy_loop () from /home/dbeutel/.local/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so
#2  0x00007ffff7087548 in ufunc_at () from /home/dbeutel/.local/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so
#3  0x00007ffff7cfa81e in method_vectorcall_VARARGS (func=func@entry=0x7ffff7330dd0,  args=args@entry=0x5555555f86d8, nargsf=<optimized out>, kwnames=0x0) at Objects/descrobject.c:311
#4  0x00007ffff7d08f4b in _PyObject_VectorcallTstate (kwnames=<optimized out>, nargsf=<optimized out>, args=<optimized out>, callable=<optimized out>, tstate=<optimized out>) at ./Include/cpython/abstract.h:114
#5  PyObject_Vectorcall (kwnames=<optimized out>, nargsf=<optimized out>, args=<optimized out>, callable=<optimized out>) at ./Include/cpython/abstract.h:123
#6  call_function (tstate=0x55555555e7a0, trace_info=<optimized out>, pp_stack=0x7fffffffe1f0, oparg=<optimized out>, kwnames=<optimized out>) at Python/ceval.c:5867
#7  0x00007ffff7cfec4e in _PyEval_EvalFrameDefault (tstate=<optimized out>, f=0x5555555f8570, throwflag=<optimized out>) at Python/ceval.c:4198
#8  0x00007ffff7cfd2eb in _PyEval_EvalFrame (throwflag=0, f=0x5555555f8570, tstate=0x55555555e7a0) at ./Include/internal/pycore_ceval.h:46
#9  _PyEval_Vector (tstate=<optimized out>, con=<optimized out>, locals=<optimized out>, args=<optimized out>, argcount=0, kwnames=<optimized out>) at Python/ceval.c:5065
#10 0x00007ffff7d7a1b4 in PyEval_EvalCode (co=0x7ffff739f780, globals=0x7ffff73c15b0, locals=<optimized out>) at Python/ceval.c:1134
#11 0x00007ffff7d948b4 in run_eval_code_obj (tstate=0x55555555e7a0, co=0x7ffff739f780, globals=0x7ffff73c15b0, locals=0x7ffff73c15b0) at Python/pythonrun.c:1291
#12 0x00007ffff7d8c316 in run_mod (mod=<optimized out>, filename=<optimized out>, globals=0x7ffff73c15b0, locals=0x7ffff73c15b0, flags=<optimized out>, arena=<optimized out>) at Python/pythonrun.c:1312
#13 0x00007ffff7c4024f in pyrun_file (fp=fp@entry=0x55555555a4f0, filename=filename@entry=0x7ffff73dc0b0, start=start@entry=257, globals=globals@entry=0x7ffff73c15b0, locals=locals@entry=0x7ffff73c15b0, closeit=closeit@entry=1, flags=0x7fffffffe5c8) at Python/pythonrun.c:1208
#14 0x00007ffff7c3f9fc in _PyRun_SimpleFileObject (fp=0x55555555a4f0, filename=0x7ffff73dc0b0, closeit=1, flags=0x7fffffffe5c8) at Python/pythonrun.c:456
#15 0x00007ffff7c40b2b in _PyRun_AnyFileObject (fp=0x55555555a4f0, filename=0x7ffff73dc0b0, closeit=1, flags=0x7fffffffe5c8) at Python/pythonrun.c:90
#16 0x00007ffff7da4e9c in pymain_run_file_obj (skip_source_first_line=0, filename=0x7ffff73dc0b0, program_name=0x7ffff73c1960) at Modules/main.c:353
#17 pymain_run_file (config=0x555555585550) at Modules/main.c:372
#18 pymain_run_python (exitcode=0x7fffffffe5c4) at Modules/main.c:587
#19 Py_RunMain () at Modules/main.c:666
#20 0x00007ffff7d67efd in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at Modules/main.c:720
#21 0x00007ffff7999310 in __libc_start_call_main () from /usr/lib/libc.so.6
#22 0x00007ffff79993c1 in __libc_start_main_impl () from /usr/lib/libc.so.6
#23 0x0000555555555045 in _start ()

NumPy/Python version information:

  • numpy: 1.22.3
  • python: 3.10.2 (main, Apr 5 2022, 14:03:41) [GCC 11.2.0]
@seberg
Copy link
Member

seberg commented Apr 6, 2022

Thanks for reporting. That fix looks right modulo nitpicking (no following \ necessar mainly ;)). If you are up for it, do you want to make a PR that includes a test? (Probably somewhere in numpy/core/tests/test_ufunc.py).

We could probably make the error message a bit easier to read, but I guess it is not really important here.

@alphabravo2001
Copy link

Can I take on this pr? My plan is to check the signature of the ufunc and if it doesn't equal None, throw an error as a ufunc with an invalid signature shouldn't support the at function.

@seberg
Copy link
Member

seberg commented Apr 11, 2022

There is a ufunc->core_enabled and a similar error message already given in another place. But yes, please go ahead, @alphabravo2001.

@dbeutel
Copy link
Author

dbeutel commented Apr 12, 2022

Sorry for not responding in a more timely manner. I removed the line continuation and indentation. Also, I added a simple test in numpy/core/tests/test_ufunc.py for the raised error. I made it a part of the function test_inplace_fancy_indexing where most of the properties of ufunc.at are tested: dbeutel@54b6f7b .

I can make a PR for this stuff but, of course, I'm also happy if @alphabravo2001 further improves it.

@seberg seberg added the sprintable Issue fits the time-frame and setting of a sprint label Apr 29, 2022
JohnnyOu pushed a commit to JohnnyOu/numpy that referenced this issue May 3, 2022
@seberg seberg closed this as completed in ba54f56 May 6, 2022
charris pushed a commit to charris/numpy that referenced this issue May 8, 2022
* Fixed numpy#21301

* Fixed numpy#21436, better error message, organized test cases as requested

* Update numpy/core/src/umath/ufunc_object.c

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug sprintable Issue fits the time-frame and setting of a sprint
Projects
None yet
Development

No branches or pull requests

3 participants