[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <8a5d6cf1-17eb-6cb9-fb45-0a4d454d385e@protonmail.com>
Date: Thu, 30 Mar 2023 21:10:18 +0000
From: Benno Lossin <y86-dev@...tonmail.com>
To: Wedson Almeida Filho <wedsonaf@...il.com>
Cc: rust-for-linux@...r.kernel.org, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
linux-kernel@...r.kernel.org,
Wedson Almeida Filho <walmeida@...rosoft.com>
Subject: Re: [PATCH 13/13] rust: sync: introduce `LockedBy`
On 30.03.23 23:04, Wedson Almeida Filho wrote:
> On Thu, 30 Mar 2023 at 08:45, Benno Lossin <y86-dev@...tonmail.com> wrote:
>>
>> On 30.03.23 13:28, Benno Lossin wrote:
>> struct Outer {
>> mtx1: Mutex<()>,
>> mtx2: Mutex<()>,
>> inners: Vec<Inner>,
>> }
>>
>> struct Inner {
>> count: LockedBy<usize, ()>,
>> }
>>
>> fn new_inner(outer: &Outer) -> Inner {
>> Inner { count: LockedBy::new(&outer.mtx1, 0) }
>> }
>>
>> fn evil(outer: &Outer) {
>> let inner = outer.inners.get(0).unwrap();
>> let mut guard1 = outer.mtx1.lock();
>> let mut guard2 = outer.mtx2.lock();
>> // The pointee of `guard1` and `guard2` have the same address.
>> let ref1 = inner.count.access_mut(&mut *guard1);
>> let ref2 = inner.count.access_mut(&mut *guard2);
>> mem::swap(ref1, ref2);
>> }
>
> This doesn't reproduce the issue because `mtx2` itself is not a ZST
> (it contains a `struct mutex` before the data it protects).
>
> Something like the following should reproduce it though:
>
> struct Outer {
> mtx1: Mutex<()>,
> zst: (),
> }
>
> fn evil(outer: &Outer) {
> let lb = LockedBy::new(&outer.mtx1, 0u8);
> let value = lb.access(&outer.zst);
> // Accessing "value" without holding `mtx1`.
> pr_info!("{}", *value);
> }
You are correct, but in your example you also cannot be sure that it
works, since the layout of the `Mutex` and `Outer` is `repr(Rust)`.
And so you cannot be sure that `zst` has the same address as `value`
inside of the `Mutex` (since the `struct mutex` could be in between).
But regardless, lets just deny ZSTs in `LockedBy` since the fix is
easy and it would be weird to put a ZST in a lock in the first place.
(Not that you have argued against it)
--
Cheers,
Benno
Powered by blists - more mailing lists