Skip to content

Got Segmentation Fault when use-after-free a ndarray using SharedMemory as its buffer #102349

Closed as not planned

Description

@knwng

Hi, I encountered a seg fault using SharedMemory, as discussed in https://discuss.python.org/t/should-sharedmemory-close-be-called-explicitly-or-we-should-just-rely-on-del/24281/1

You can reproduce it by

import numpy as np
from multiprocessing.shared_memory import SharedMemory

class Block(object):
    def __init__(self, nbytes, shape, dtype):
        self._nbytes = nbytes
        self._shape = shape
        self._dtype = dtype

    def get_ndarray(self):
        shm = SharedMemory(name=self._shm_name)
        return np.ndarray(self._shape, dtype=self._dtype, buffer=shm.buf)

    def set_ndarray(self, arr):
        shm = SharedMemory(create=True, size=self._nbytes)
        shm_arr = np.ndarray(self._shape, dtype=self._dtype, buffer=shm.buf)
        shm_arr[:] = arr[:]
        self._shm_name = shm.name

arr = np.random.rand(1, 100)
block = Block(arr.nbytes, arr.shape, arr.dtype)
block.set_ndarray(arr)
arr_on_shm = block.get_ndarray()
print(f'arr_on_shm: {arr_on_shm}')

After going through memoryview's document, I noticed that it seems to be infeasible for numpy to check if its buffer is valid, so I created this issue to see if python community has any solution. Thank you!

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Mar 1, 2023
  2. sobolevn commented on Mar 1, 2023

    @sobolevn
    Member

    Sorry, are you sure that this is a CPython issue and not a numpy issue?
    Can you reproduce the same without the use of numpy?

  3. knwng commented on Mar 1, 2023

    @knwng
    Author

    Hi @sobolevn, I just tried to use the SharedMemory.buf after free and it threw a ValueError instead of seg fault. So I can't give an example without numpy. I'll report this to the numpy community. Sorry for bothering.

  4. sobolevn commented on Mar 1, 2023

    @sobolevn
    Member

    No worries, happy to help! :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions