You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: Got Segmentation Fault when use-after-free a ndarray using SharedMemory as its buffer #23305
If I visit the SharedMemory.buf(which is a memoryview of mmap) after shm is released, I'll got a ValueError: operation forbidden on released memoryview object exception instead a seg fault. But somehow this kind of guard doesn't work in ndarray. Is it possible for ndarray to check the status of memoryview before visiting?
Hi @seberg , I tried to use np.frombuffer and the seg fault disappeared. But when the SharedMemory is destroyed, the __del__method raise a BufferError that can't be caught.
Maybe you should open it as a shared memory bug. NumPy cannot do more than memoryview and that is what you are seeing there. So if that is an issue, it is a SharedMemory issue, not a NumPy one.
Maybe it is just unintended use, I guess, but either way it isn't NumPy. The only thing that may be plausible would be to have a numpy helper to create a shared memory array.
I was also struggling to classify this problem so at the beginning I reported an issue to cpython. But as @sobolevn said, I can't create a reproducable code without numpy since a normally released memoryview will through a catchable ValueError. Maybe it needs some cooperations between the two projects to determine whether this should be fixed and how?
Activity
knwng commentedon Mar 1, 2023
If I visit the SharedMemory.buf(which is a memoryview of mmap) after shm is released, I'll got a
ValueError: operation forbidden on released memoryview objectexception instead a seg fault. But somehow this kind of guard doesn't work in ndarray. Is it possible for ndarray to check the status of memoryview before visiting?seberg commentedon Mar 6, 2023
For what its worth
np.frombuffershould protect you here, maybe that should be expanded tonp.ndarray.knwng commentedon Mar 6, 2023
Hi @seberg , I tried to use
np.frombufferand the seg fault disappeared. But when the SharedMemory is destroyed, the__del__method raise a BufferError that can't be caught.The code is:
The message printed:
It's great to avoid the seg fault but the BufferError seems to be another problem. I'm not sure if it can be eliminated.
seberg commentedon Mar 6, 2023
Maybe you should open it as a shared memory bug. NumPy cannot do more than
memoryviewand that is what you are seeing there. So if that is an issue, it is aSharedMemoryissue, not a NumPy one.Maybe it is just unintended use, I guess, but either way it isn't NumPy. The only thing that may be plausible would be to have a numpy helper to create a shared memory array.
knwng commentedon Mar 7, 2023
I was also struggling to classify this problem so at the beginning I reported an issue to cpython. But as @sobolevn said, I can't create a reproducable code without numpy since a normally released memoryview will through a catchable ValueError. Maybe it needs some cooperations between the two projects to determine whether this should be fixed and how?
seberg commentedon Mar 7, 2023
The segfault is NumPy of course (there is historic context for this, but that doesn't matter much).
But you can reproduce the the same thing as
frombufferby simply replacing it withmemoryview.