Skip to content

Conversation

DoeringChristian
Copy link
Contributor

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.

@LegNeato
Copy link
Contributor

Hello @DoeringChristian ! We are rebooting this project. Sorry for your PR not getting merged!

Is this still relevant?

@DoeringChristian
Copy link
Contributor Author

Hey, cool to hear! I haven't checked if it is in a long time. You can close it if you want to.

@LegNeato
Copy link
Contributor

LegNeato commented Mar 7, 2025

I think we should take this, as it look localized and was debugged and fixed in the past. Thanks again!

@LegNeato LegNeato merged commit 9db073b into Rust-GPU:main Mar 7, 2025
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.

2 participants