Skip to content

Segmentation fault when calling getbuffer() on Packer object #479

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
thibaudmartinez opened this issue Jun 15, 2021 · 9 comments · Fixed by #602
Closed

Segmentation fault when calling getbuffer() on Packer object #479

thibaudmartinez opened this issue Jun 15, 2021 · 9 comments · Fixed by #602
Milestone

Comments

@thibaudmartinez
Copy link

thibaudmartinez commented Jun 15, 2021

Hi there,

I'm trying to get the internal data of the Packer object in order to avoid unneeded copying, as documented here.

The code is as follow :

import msgpack

def do_the_job():
  packer = msgpack.Packer(autoreset=False)
  packer.pack(1)
  return packer.getbuffer()

bytes(do_the_job())

When running this snippet, I get the following error :

[1]    9018 segmentation fault (core dumped)  python script.py

I am using Ubuntu 18.04.5 LTS together with msgpack 1.0.2.

Thanks in advance for your help and for your work on this package !

@methane
Copy link
Member

methane commented Jun 16, 2021

Thank you for report with snippets. But I can not reproduce it.

@thibaudmartinez
Copy link
Author

thibaudmartinez commented Jun 16, 2021

Thank you for your reply.

I was able to reproduce the bug using the following Debian Docker image.

FROM python:3.9.5-slim-buster

RUN pip install --no-input msgpack==1.0.2

RUN echo "\
import msgpack\n\
\n\
def do_the_job():\n\
    packer = msgpack.Packer(autoreset=False)\n\
    packer.pack(1)\n\
    return packer.getbuffer()\n\
\n\
bytes(do_the_job())\
" > script.py

Can you try to reproduce the bug with the above setup?

docker build . -t test-msgpack
docker run -it test-msgpack bash
python script.py

@methane
Copy link
Member

methane commented Jun 16, 2021

Thank you, I can reproduce it now! I will investigate it in tomorrow.

@methane
Copy link
Member

methane commented 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
Copy link
Author

Great! Do you think it will be easy to fix in an upcoming release?

@methane
Copy link
Member

methane commented Jun 17, 2021 via email

@jfolz
Copy link
Contributor

jfolz commented Jun 17, 2021

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.

In particular, PyBuffer_FillInfo() states:

On success, set view->obj to a new reference to exporter and return 0. Otherwise, raise PyExc_BufferError, set view->obj to NULL and return -1;

So keeping a reference to the exporting object is exactly the use case for this field.

@jfolz
Copy link
Contributor

jfolz commented Jun 18, 2021

Oh wow... that's really convoluted. So you need to implement __getbuffer__ and __releasebuffer__ magic methods, and getbuffer can then do memoryview(self).

@methane methane added this to the 1.1 milestone May 21, 2023
methane added a commit that referenced this issue May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants