rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: Boqun Feng <boqun.feng@gmail.com>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	rust-for-linux@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	linux-kernel@vger.kernel.org,
	"Wedson Almeida Filho" <walmeida@microsoft.com>
Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef`
Date: Mon, 25 Sep 2023 17:00:45 +0000	[thread overview]
Message-ID: <9d6d6c94-5da6-a56d-4e85-fbf8da26a0b0@proton.me> (raw)
In-Reply-To: <ZRGyRQuBcWvgtdNR@Boquns-Mac-mini.home>

On 25.09.23 18:16, Boqun Feng wrote:
> On Mon, Sep 25, 2023 at 03:07:44PM +0000, Benno Lossin wrote:
>> ```rust
>> struct MutatingDrop {
>>       value: i32,
>> }
>>
>> impl Drop for MutatingDrop {
>>       fn drop(&mut self) {
>>           self.value = 0;
>>       }
>> }
>>
>> let arc = Arc::new(MutatingDrop { value: 42 });
>> let wr = arc.as_with_ref(); // this creates a shared `&` reference to the MutatingDrop
>> let arc2: Arc<MutatingDrop> = wr.into(); // increments the reference count to 2
> 
> More precisely, here we did a
> 
> 	&WithRef<_> -> NonNull<WithRef<_>>
> 
> conversion, and later on, we may use the `NonNull<WithRef<_>>` in
> `drop` to get a `Box<WithRef<_>>`.

Indeed.

> 
>> drop(arc); // this decrements the reference count to 1
>> drop(arc2); // this decrements the reference count to 0, so it will drop it
>> ```
>> When dropping `arc2` it will run the destructor for `MutatingDrop`,
>> which mutates `value`. This is a problem, because the mutable reference
>> supplied was derived from a `&`, that is not allowed in Rust.
>>
> 
> Is this an UB? I kinda wonder what's the real damage we can get, because
> in this case, we just use a reference to carry a value of a pointer,
> i.e.
> 
> 	ptr -> reference -> ptr
> 
> I cannot think of any real damage compiler can make, but I'm happy to be
> surprised ;-)

This is UB, so anything can happen :)

-- 
Cheers,
Benno



  reply	other threads:[~2023-09-25 17:01 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-23 14:49 [PATCH v2 0/2] Remove `ArcBorrow` Wedson Almeida Filho
2023-09-23 14:49 ` [PATCH v2 1/2] rust: arc: rename `ArcInner` to `WithRef` Wedson Almeida Filho
2023-09-23 19:31   ` Martin Rodriguez Reboredo
2023-09-24 11:59   ` Benno Lossin
2023-09-24 13:41   ` Jianguo Bao
2023-09-25  6:21   ` Alice Ryhl
2023-09-23 14:49 ` [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Wedson Almeida Filho
2023-09-23 19:32   ` Martin Rodriguez Reboredo
2023-09-24 11:59   ` Benno Lossin
2023-09-24 13:36   ` Jianguo Bao
2023-09-25  6:29   ` Alice Ryhl
2023-09-25  9:14     ` Benno Lossin
2023-09-25 14:49       ` Boqun Feng
2023-09-25 15:00         ` Alice Ryhl
2023-09-25 15:17           ` Boqun Feng
2023-09-25 15:30             ` Alice Ryhl
2023-09-25 16:02               ` Boqun Feng
2023-09-25 16:11                 ` Benno Lossin
2023-09-25 15:07         ` Benno Lossin
2023-09-25 16:16           ` Boqun Feng
2023-09-25 17:00             ` Benno Lossin [this message]
2023-09-25 18:51               ` Boqun Feng
2023-09-25 21:03                 ` Benno Lossin
2023-09-25 21:55                   ` Boqun Feng
2023-09-25 21:58                     ` Alice Ryhl
2023-09-25 22:02                       ` Boqun Feng
2023-09-25 22:06                         ` Boqun Feng
2023-09-25 22:26                         ` Benno Lossin
2023-09-25 22:34                           ` Boqun Feng
2023-09-25 23:24                             ` Boqun Feng
2023-09-26  8:26                           ` Gary Guo
2023-09-26 15:24                             ` Boqun Feng
2023-09-26 15:41                               ` Alice Ryhl
2023-09-26 16:35                                 ` Boqun Feng
2023-09-26 17:15                                   ` Benno Lossin
2023-09-26 17:43                                     ` Boqun Feng
2023-09-26 18:26                                       ` Benno Lossin
2023-09-26 21:31                                       ` Alice Ryhl
2023-09-26 18:20                                     ` Boqun Feng
2023-09-26 21:27                                       ` Alice Ryhl
2023-09-25 15:04       ` Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9d6d6c94-5da6-a56d-4e85-fbf8da26a0b0@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=walmeida@microsoft.com \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).