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
Segmentation fault when calling getbuffer() on Packer object #479
I got it. Memoryview object don't have reference to Packer.
When do_the_job() return, Packer object is released and Memoryview references "after free" memory.
It's not easy to fix. I can not release the fix anytime soon.
You shouldn't use the buffer after the Packer is released. It is unintended
usage.
2021年6月17日(木) 23:04 Thibaud Martinez ***@***.***>:
I think this can be fixed relatively easily. Replace PyMemoryView_FromMemory (here) with PyMemoryView_FromBuffer(Py_buffer *view) where view.obj points to self and incref self, though I am not entirely clear on this bit in the docs:
As a special case, for temporary buffers that are wrapped by PyMemoryView_FromBuffer() or PyBuffer_FillInfo() this field is NULL. In general, exporting objects MUST NOT use this scheme.
Oh wow... that's really convoluted. So you need to implement __getbuffer__ and __releasebuffer__ magic methods, and getbuffer can then do memoryview(self).
Activity
methane commentedon Jun 16, 2021
Thank you for report with snippets. But I can not reproduce it.
thibaudmartinez commentedon Jun 16, 2021
Thank you for your reply.
I was able to reproduce the bug using the following Debian Docker image.
Can you try to reproduce the bug with the above setup?
docker build . -t test-msgpack docker run -it test-msgpack bash python script.pymethane commentedon Jun 16, 2021
Thank you, I can reproduce it now! I will investigate it in tomorrow.
methane commentedon Jun 16, 2021
I got it. Memoryview object don't have reference to Packer.
When do_the_job() return, Packer object is released and Memoryview references "after free" memory.
thibaudmartinez commentedon Jun 17, 2021
Great! Do you think it will be easy to fix in an upcoming release?
methane commentedon Jun 17, 2021
jfolz commentedon Jun 17, 2021
I think this can be fixed relatively easily. Replace
PyMemoryView_FromMemory(here) withPyMemoryView_FromBuffer(Py_buffer *view)whereview.objpoints to self andincrefself, though I am not entirely clear on this bit in the docs:In particular,
PyBuffer_FillInfo()states:So keeping a reference to the exporting object is exactly the use case for this field.
methane commentedon Jun 18, 2021
See here.
https://github.com/python/cpython/blob/7f01f77f8fabcfd7ddb5d99f12d6fc99af9af384/Objects/memoryobject.c#L775-L776
The best way is implementing the buffer protocol.
https://cython.readthedocs.io/en/latest/src/userguide/buffer.html
jfolz commentedon Jun 18, 2021
Oh wow... that's really convoluted. So you need to implement
__getbuffer__and__releasebuffer__magic methods, andgetbuffercan then domemoryview(self).implement buffer protocol (#602)