Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1627706rdh; Mon, 25 Sep 2023 20:45:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELAsH5bgJqACPNEWCBFbKmcWQ+IMsDTszQFeBxdQM3f1aTP/TqMxNwMTrYvE5LGsT4kQsM X-Received: by 2002:a17:902:c3d1:b0:1c3:8464:cabd with SMTP id j17-20020a170902c3d100b001c38464cabdmr6223481plj.12.1695699950682; Mon, 25 Sep 2023 20:45:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695699950; cv=none; d=google.com; s=arc-20160816; b=NoSDxkHqANSx0P2spUfoCOuGGZtPPIWDc91foTo3bFVeh4NsxvwCM67EUn4lO6Rc1v FSElK/rcWSER0732nWgfWvPkL6DjFk95atHAyQNBVzd3sIDMTSZRJ9C4vbWxePEiber5 HWpXDLz3YZ6AK4mRCJ/XXmEFfI/1saM1Ej5gI8IL1BtyHNp+3yuZTPUpRJrunQVgSXVV VTMi+dYkbeC/9RfEwa0J1+BVNtSpKYAgGxo5TS1J3ZDK8vZl1zlQUXD5/U2Muz+pj5Oi 6I105gI2M6AB+AULoJLTij9H1cTTOx7+xmHrExI+3ohSwUzMGI58MKswe4GJNQWzxCoX T6xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :date:dkim-signature; bh=EUihDlu4xMCW4pJXMQT++Wi78DVs4G1Y5sE4xVXS7LI=; fh=NzxGoz2C8fG311LKiw1sBH0veILnB/F87TKwH1iLN4k=; b=p06sOYDnO4BNa+CGv2S9eJoftqYtAY6IJihHkpG4p+HPocPQt6SgzcHY27UNbEHF2d yqNuKFN3I0pDC87xvxvn5+D6R4OPnvtmlQxQ5Yx/xI1FITQPxZoj4oTfl4ZvzNfOLoyO mX1OpxqtiK7VFLJ/Kxq8wnZrs0qPUfEOwFeFfOc7tYztwr6j5PRCmIhD11CMteDHeZ0n fePmKPzNZeNS2yXjG8vzgijhBXIrQaI+vWHdyDb2dWppquKvDDhMv0xbP8YPjTiihyHo qy6galFx+SQ82zOtsnUSbI8b1D+V38OUXKugYCoLjjh2XQJrwaEYK8BZfvU0v1L3MpNb 73Lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=CRf33zD4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id a15-20020a170902710f00b001c381bd030csi11010996pll.172.2023.09.25.20.45.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 20:45:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=CRf33zD4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 8159181C5249; Mon, 25 Sep 2023 14:04:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232278AbjIYVEL (ORCPT + 99 others); Mon, 25 Sep 2023 17:04:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229513AbjIYVEK (ORCPT ); Mon, 25 Sep 2023 17:04:10 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFD5010F; Mon, 25 Sep 2023 14:04:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1695675840; x=1695935040; bh=EUihDlu4xMCW4pJXMQT++Wi78DVs4G1Y5sE4xVXS7LI=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=CRf33zD4C8pjNv/GD/F85p0HUQw3v8OTbTEU2ZF+jvfUOqycYXwfLtBYCfUhjsx/t Ope6Pc881DenM1TOSpCxO3DsOe8vWXuBTx6itY0H2NK2G87xCAu2aTb7vTwnmbL35W 6z6CeoA5qwIBewfSTSgA6QjRVbSmcfLotvTUeNcZZBT3VqgqTvdPlIXv9tkIvSc3JX DHq6JRsrUTayn0/nto3uhKAD/4UX2ZHFAsysERo/lkqHIGY9v4JNy/c2wn4NXU/5YX fatZfNnSHRtr472RziosIsSQBnZZS2pVUHYO7zzAmK97VQgOHHOIlPUuumBK+tz8sX zttnQSU55HqZw== Date: Mon, 25 Sep 2023 21:03:52 +0000 To: Boqun Feng From: Benno Lossin Cc: Alice Ryhl , Wedson Almeida Filho , rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Message-ID: In-Reply-To: References: <20230923144938.219517-1-wedsonaf@gmail.com> <20230923144938.219517-3-wedsonaf@gmail.com> <969eab7f-ad40-0dfb-18b9-6002fc54e12b@proton.me> <14513589-cc31-8985-8ff6-a97d2882f593@proton.me> <9d6d6c94-5da6-a56d-4e85-fbf8da26a0b0@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 25 Sep 2023 14:04:11 -0700 (PDT) On 25.09.23 20:51, Boqun Feng wrote: > On Mon, Sep 25, 2023 at 05:00:45PM +0000, Benno Lossin wrote: >> 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 =3D 0; >>>> } >>>> } >>>> >>>> let arc =3D Arc::new(MutatingDrop { value: 42 }); >>>> let wr =3D arc.as_with_ref(); // this creates a shared `&` reference t= o the MutatingDrop >>>> let arc2: Arc =3D wr.into(); // increments the reference= count to 2 >>> >>> More precisely, here we did a >>> >>> =09&WithRef<_> -> NonNull> >>> >>> conversion, and later on, we may use the `NonNull>` in >>> `drop` to get a `Box>`. >> >> Indeed. >> >=20 > Can we workaround this issue by (ab)using the `UnsafeCell` inside > `WithRef`? >=20 > impl From<&WithRef> for Arc { > fn from(b: &WithRef) -> Self { > // SAFETY: The existence of the references proves that > =09// `b.refcount.get()` is a valid pointer to `WithRef`. > =09let ptr =3D unsafe { NonNull::new_unchecked(b.refcount.get().cast::>()) }; >=20 > =09// SAFETY: see the SAFETY above `let ptr =3D ..` line. > ManuallyDrop::new(unsafe { Arc::from_inner(ptr) }) > .deref() > .clone() > } > } >=20 > This way, the raw pointer in the new Arc no longer derives from the > reference of `WithRef`. No, the code above only obtains a pointer that has provenance valid for a `bindings::refcount_t` (or type with the same layout, such as `Opaque`). But not the whole `WithRef`, so accessi= ng it by reading/writing will still be UB. --=20 Cheers, Benno