Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1681065rdh; Mon, 25 Sep 2023 23:25:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGCOS/2OWf6RQPgmA9MZOGsYx5DyyJ5PCOPNA8syxz4c9HB8CA5VaJ6cxagG2Gt+aJddS5r X-Received: by 2002:a05:6358:2921:b0:134:d282:92e9 with SMTP id y33-20020a056358292100b00134d28292e9mr9305295rwb.29.1695709504325; Mon, 25 Sep 2023 23:25:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695709504; cv=none; d=google.com; s=arc-20160816; b=c174+SlvP01QLORCVWgt8kDbFMEHcTGjX3POMJlYxAdZy2Ioi8hoe+IashOnaKYgAj Vwe4YZ+R5VYtxMC2pQ21ZjY0fZadxR3w1D721Ft0f5sO/M9DcO2PnnN5VNBuSCTiBsDp sS3+YXKOkJXXEONkFR2Px2BpULDeHW1v+EwToXZzif0N0qgQy/MgCZAMyVnQxpcizuKG eLvVZygTUkmI812zYeZIauCHW3YoB71CdBO5AasLzvJi9iKH9nVxNqTajAYTkEFRLYgu UyrG/dsNTKuSDUWpe6yx28/n+7O3ZQpf4xqt3dZR4DTwEqsF2uLwh2WuhBdlsqed9ILA tjFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature; bh=JUIG4xw/fGQG/BKrffhAQGfYP16ndxdkwQiaa0ZhJiA=; fh=yyFQeWuEgZWbQkLdOhiO0MZE60DnSuga/bGIiTZKxQQ=; b=RgL5KLPZFxG6Mzh7xhvZuGy12RjqPyCR1Af1MWNZ1gnLdkqE4/exJ2jVJpABpR6tBu JE41sxVxUZMGeInAD01ux0iSSBpCdw18ArdISL3lHgzCerPGUJjk44ibJnr9dTkLcdyb tolsdzbeVA9YHwEaLs2CGm9KTVpCzgKEw8rNvqGMaiuAM4KXNBGr8iFM9pWU9QmCCHik T8Pz7VEKEP5XAGnr9+t3kpLQ0CX5kTUdfk4jXdxo3VmltrtvFN3NYv1Ecj65D0pSKHsK eHy0TDU+VkmUpCMkULmeGRlsYG8XT9bKfTYX5FuXnmIInLb/fAj600Hp6fjQ6/JL0dhs Os2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OnX11QsM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id d19-20020a637353000000b00578e98dec9csi11823069pgn.715.2023.09.25.23.25.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 23:25:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OnX11QsM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id B7EC881E781F; Mon, 25 Sep 2023 15:07:20 -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 S230486AbjIYWHO (ORCPT + 99 others); Mon, 25 Sep 2023 18:07:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229921AbjIYWHJ (ORCPT ); Mon, 25 Sep 2023 18:07:09 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5553AF; Mon, 25 Sep 2023 15:07:02 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-9ad8bba8125so980944166b.3; Mon, 25 Sep 2023 15:07:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695679621; x=1696284421; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=JUIG4xw/fGQG/BKrffhAQGfYP16ndxdkwQiaa0ZhJiA=; b=OnX11QsMkXOM8IHe+AN98YC7Em3pHTS1n8J8borSDHLxrbo821PzdfRT+VwlHy/6JJ Fr5yZkXptGg3syJ9lrdnA4HM0guXxcRhIZ0VzteL1TpWcNxT8MNqdUxgtyxZGUZD2TM6 YCk3hrZ3DFD5VRB/3GcR86j6NA1s3TM71KsJ9Zi5yjhmlKT9zR8tN09gI+QKGICchXIy 5XoRyXZctgE5gyJnt9MYM+/QnRBRKJh02LwH8Px2cmIRbrwQROCKKDkyjjp9Lz0eAxWX nFLQ1YjEcequdc57/7ZpZ8aYO62ayf2zY35iipgLjhPp4IAPq4H8JOxzXeBYjOIHF3/K ESkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695679621; x=1696284421; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JUIG4xw/fGQG/BKrffhAQGfYP16ndxdkwQiaa0ZhJiA=; b=iuLN1byJsjD1oLcXD55mjyOL+ODGSM6IfeGNOz+6BuS0taT8i+rT+ZODUQOSE29BAZ SS0boCp1rnGcDLZ15gTlJcbJhJx8eulJMhcQ5aEjF0a0SMhiB6vdYwDcd3J75aEQbb+c KiLAgOKjieWUpgWKnI/iu2HZ6eiEgshPnlyOKKT9bwlB9p9qFzfZYfIkb7huK9xxqcfC ksPjqQNRnVSnln/meQwybV3aRgaN6notyeziNa5kR/5pLKR0kcPguDFIx51JF6z82QQS MDWh6ippv8bBz9KKR38G0qiMa75pmnEduVfDJGbWv9s7I2C31RTwwRRjT8UzuaYttQC+ nSEw== X-Gm-Message-State: AOJu0Yw6Rj00VfqgluvPxWehHIfusco3MSWrCbkGFuzvm7KiqTH4HLqX K/jsmzC7qIBciPn6nucLs8U= X-Received: by 2002:a17:906:8a45:b0:9ae:4eb7:ae8b with SMTP id gx5-20020a1709068a4500b009ae4eb7ae8bmr7308493ejc.7.1695679620896; Mon, 25 Sep 2023 15:07:00 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id qk8-20020a170906d9c800b009ad89697c86sm6912868ejb.144.2023.09.25.15.06.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 15:07:00 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id 9484527C0054; Mon, 25 Sep 2023 18:06:58 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 25 Sep 2023 18:06:58 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudelhedgtdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueeviedu ffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 25 Sep 2023 18:06:57 -0400 (EDT) Date: Mon, 25 Sep 2023 15:06:26 -0700 From: Boqun Feng To: Alice Ryhl Cc: Benno Lossin , Alice Ryhl , Wedson Almeida Filho , rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= 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: References: <969eab7f-ad40-0dfb-18b9-6002fc54e12b@proton.me> <14513589-cc31-8985-8ff6-a97d2882f593@proton.me> <9d6d6c94-5da6-a56d-4e85-fbf8da26a0b0@proton.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 15:07:20 -0700 (PDT) On Mon, Sep 25, 2023 at 03:02:27PM -0700, Boqun Feng wrote: > On Mon, Sep 25, 2023 at 11:58:46PM +0200, Alice Ryhl wrote: > > On 9/25/23 23:55, Boqun Feng wrote: > > > On Mon, Sep 25, 2023 at 09:03:52PM +0000, Benno Lossin wrote: > > > > 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 = 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 = wr.into(); // increments the reference count to 2 > > > > > > > > > > > > > > More precisely, here we did a > > > > > > > > > > > > > > &WithRef<_> -> NonNull> > > > > > > > > > > > > > > conversion, and later on, we may use the `NonNull>` in > > > > > > > `drop` to get a `Box>`. > > > > > > > > > > > > Indeed. > > > > > > > > > > > > > > > > Can we workaround this issue by (ab)using the `UnsafeCell` inside > > > > > `WithRef`? > > > > > > > > > > impl From<&WithRef> for Arc { > > > > > fn from(b: &WithRef) -> Self { > > > > > // SAFETY: The existence of the references proves that > > > > > // `b.refcount.get()` is a valid pointer to `WithRef`. > > > > > let ptr = unsafe { NonNull::new_unchecked(b.refcount.get().cast::>()) }; > > > > > > > > > > // SAFETY: see the SAFETY above `let ptr = ..` line. > > > > > ManuallyDrop::new(unsafe { Arc::from_inner(ptr) }) > > > > > .deref() > > > > > .clone() > > > > > } > > > > > } > > > > > > > > > > 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 accessing > > > > it by reading/writing will still be UB. > > > > > > > > > > Hmm... but we do the similar thing in `Arc::from_raw()`, right? > > > > > > pub unsafe fn from_raw(ptr: *const T) -> Self { > > > .. > > > } > > > > > > , what we have is a pointer to T, and we construct a pointer to > > > `ArcInner/WithRef`, in that function. Because the `sub` on pointer > > > gets away from provenance? If so, we can also do a sub(0) in the above > > > code. > > > > Not sure what you mean. Operations on raw pointers leave provenance > > unchanged. > > Let's look at the function from_raw(), the input is a pointer to T, > right? So you only have the provenance to T, but in that function, the > pointer is casted to a pointer to WithRef/ArcInner, that means you > have the provenance to the whole WithRef/ArcInner, right? My > question is: why isn't that a UB? > Or how does it get away from provenance checkings? Regards, Boqun > Regards, > Boqun > > > > > Alice > >