[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <6cfa426b-4ffe-41bc-9de5-ba7c7c70d610@proton.me>
Date: Tue, 30 Jul 2024 18:06:20 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Wedson Almeida Filho <wedsonaf@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...sung.com>, rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] rust: implement ForeignOwnable for Pin<Box<T>>
On 30.07.24 19:50, Alice Ryhl wrote:
> On Tue, Jul 30, 2024 at 7:14 PM Benno Lossin <benno.lossin@...ton.me> wrote:
>>
>> On 30.07.24 15:06, Alice Ryhl wrote:
>>> @@ -89,6 +90,32 @@ unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
>>> }
>>> }
>>>
>>> +impl<T: 'static> ForeignOwnable for Pin<Box<T>> {
>>> + type Borrowed<'a> = Pin<&'a T>;
>>> +
>>> + fn into_foreign(self) -> *const core::ffi::c_void {
>>> + // SAFETY: We are still treating the box as pinned.
>>
>> I don't think that we have the guarantee that the pointee at the pointer
>> that is returned by `into_foreign` is not moved.
>
> That doesn't seem like the kind of thing we need a guarantee for.
> Rather, unless we give anyone a guarantee that dereferencing it is
> okay, it seems reasonable to assume that it won't be dereferenced.
> Right now, it's just an opaque pointer, so C really shouldn't be
> touching it. We already implement the trait for Arc which is also
> pinned and also doesn't even point at a value of type T.
Hmm, I don't have any rational concerns with this, but it doesn't _feel_
right...
When I replace the return type by `OpaquePtr(*const core::ffi::c_void)`
that is `repr(transparent)` I don't feel like this is a problem any
more.
So I guess your argument is fine.
I think it shouldn't hurt to add this to the docs, will send a patch for
that. Anyways,
Reviewed-by: Benno Lossin <benno.lossin@...ton.me>
>> AFAIU `ForeignOwnable` is used to store these pointers in C structures
>> and never to actually access the value behind the returned pointer. So
>> we could add the requirement to `into_foreign` (thus making it `unsafe`)
>> that the pointer should not be dereferenced/used aside from `borrow` and
>> `from_foreign`. Otherwise I don't see how the call below can be OK.
>> What do you think?
>
> Dereferencing the void pointer is unsafe in itself, so I don't see why
> `into_foreign` has to be unsafe too.
Oh yes, you are right.
---
Cheers,
Benno
Powered by blists - more mailing lists