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

Fix: cast caused double free (use mem::forget) #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DoeringChristian
Copy link

Hi,

i found a double free in the cast function of the DeviceBuffer class.
The following code would not work:

        let x = vec![0f32; 100].as_slice().as_dbuf().unwrap();
        dbg!(&x);
        println!("{:#x?}", x.as_device_ptr().as_raw());
        // dbg!(x.as_host_vec());
        let x = x.cast::<u8>();
        dbg!(&x);
        let x = x.cast::<f32>();
        dbg!(&x);
        dbg!(x.as_host_vec());

This returns with CUDA_ERROR_INVALID_VALUE.
Looking at the trace using Nvidia Nsight Compute it can be seen that there occurs a double free.

Screenshot from 2023-03-20 09-12-56
It turns out that when the cast function returns it drops self and frees the buffer.
The fix is to use std::mem::forget on self.

            let new_len = (size_of::<A>() * self.len) / size_of::<B>();
            let ret = Ok(DeviceBuffer {
                buf: self.buf.cast(),
                len: new_len,
            });
            unsafe{std::mem::forget(self);} // this is necessary to prevent double free
            ret

However I don't know if there is a better way than using std::mem::forget.

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 this pull request may close these issues.

1 participant