Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5680290rwd; Wed, 24 May 2023 05:30:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5mueWqknZ0xZQdjxrToF5ZOBRz40sHq1U7RZwGR3n8C2xAVSyh3BNFSYbLS3MwbykUeAqM X-Received: by 2002:a05:6a20:a5a8:b0:104:b7b4:e03b with SMTP id bc40-20020a056a20a5a800b00104b7b4e03bmr18698065pzb.48.1684931428938; Wed, 24 May 2023 05:30:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684931428; cv=none; d=google.com; s=arc-20160816; b=Fs3YGxz1ZCNhC4o2EUL4mMmrep//rZJglAlZEG++OGIVemgaNLHWsICmdALpXS/pwB 3U4kgb3OFSni1yoj70f3+LW8zbGoZcczYKUIig2YWfMqlSUwrSwitop6gzIBHXyGzZwt AeZ3H+QXDF2n0oaLngLbDoWz0UvoT05VrEJYFeXieZv5cP0UFDqCYJADM53xofgb5wpZ mRZ/wcu1RAz78O5akYl8aXHBHLRAseA1FTW4JWn0M2GeSCziiWa7DuAhTUhhySlP1Ra4 8cFVazlz2wfAvu1aM8zTsN0ADSxJqzCvhYPaAO2nY2t6XOw9D+mnq2+8K45YRgjjkubA kyvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=tIkJCVY+tMqNuU9Hdw7jNNdVzKzit4T0GD2WdhoZSXQ=; b=Pk00t2e1tn2hNyACartUQ++KfRl//jQduK34OylkHvUyk79JIOHl4SlZ7o1EZXCtCa JujEzChAn/jGXcHnSer2HVchTnCk/q+M9mCHrC4p7f6u2P27jCwW6/6Zt7xi6MjH6URu nBJDFhjG65jmgH8qMk/Fn2s/GSXYVCbZA+oQBzYz5FFvP3xWLQC4N4FmlCf8LRNZwVVR gdJJslHvBCQsZv/Jj/rbq0iWx4uyMFH5HaLsS1lCdhMDxtsDYBvgOTncb+2ch8NElXwf EVU8MrFRZaA9gXty6h+eHpkoJ+CsChtnU8unHRAf0cYypJ3XP2NbbtrK54q/IdbYlvi1 iK2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Rf0miXKt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p20-20020a639514000000b0051b13a071a8si2334554pgd.575.2023.05.24.05.30.14; Wed, 24 May 2023 05:30:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Rf0miXKt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232974AbjEXM0q (ORCPT + 99 others); Wed, 24 May 2023 08:26:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232939AbjEXM0o (ORCPT ); Wed, 24 May 2023 08:26:44 -0400 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08150139 for ; Wed, 24 May 2023 05:26:43 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-513f8141094so941816a12.1 for ; Wed, 24 May 2023 05:26:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684931201; x=1687523201; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tIkJCVY+tMqNuU9Hdw7jNNdVzKzit4T0GD2WdhoZSXQ=; b=Rf0miXKtnbI4iEIXnusCA82/4rOsQIVvu9oayiSk1I7Dqc9En3wSLFxnjUErf0qLVa 5KAMme6jheRdEI8gnBPwkCmhQVED/QGDHTgZLbzMB5ZR4iUnlN/djzTMiy0JZO/TF2s1 nJzbPCf3eo3CYZdvYUnOuLyl/r03i2DjvnXQ3lunbiMziTZ6KXC93Jrz6kEUbg3r+eY3 6Qb0Mzv2vDX1y4Ut2U5vyMmQcwLtKMbzG874bgg1SOn6cwo7hhWFWsaWl+Cc97/FrTN6 ZYndWmGkM+enrawaHU4L38v9HejkFTTP3gjfqq9l7YY9WhVBau7Kt4lMFE49Rt6tgjGu v2NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684931201; x=1687523201; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tIkJCVY+tMqNuU9Hdw7jNNdVzKzit4T0GD2WdhoZSXQ=; b=M61R43OulVGwpU+K0tn6cPwUT1KkfXIZRpITQaMGZOiOdb0g3NhR2SNMqFW+Fcxr3F yYEcu2ccuI+wJ3EsbqXHQcrMZlv6XGrUQSxpQjjZrM+/4n48LLIF46A2PGwMs47aJmLP g4R/uVldNv4G6BFDOrgC7RRgxqSv2/wKlQNtZ853m2JZ5mctcybLEMmgX89cfrFpHKA/ 1i9JI7eeJei3Sv+G5xKQa5OcbYwIJ/PYsie4tiETxtUuvU+rOBIlKfVSXOg5TJgRx30i KiXJ+BLWXNvy9NDKAl8yWLpksPpiUgMWZNVf/Vm59+6qtNiCpJ71mYXS03TROTw3Oxn+ I0eQ== X-Gm-Message-State: AC+VfDx7V/pvwyZTPgvy8mqbjQJrHz9NvmJxkdLjkLZI1pgy8UKZNl+a RTEue74NJv+9FRXy+PDdfBqXLQ4iOTbi78g= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a50:bb64:0:b0:50c:d8e:5f4b with SMTP id y91-20020a50bb64000000b0050c0d8e5f4bmr820708ede.8.1684931201472; Wed, 24 May 2023 05:26:41 -0700 (PDT) Date: Wed, 24 May 2023 12:26:39 +0000 In-Reply-To: <20230523164818.0a44fec8.gary@garyguo.net> Mime-Version: 1.0 References: <20230523164818.0a44fec8.gary@garyguo.net> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Message-ID: <20230524122639.2500848-1-aliceryhl@google.com> Subject: Re: [PATCH v1 2/7] rust: add offset_of! macro From: Alice Ryhl To: gary@garyguo.net Cc: alex.gaynor@gmail.com, aliceryhl@google.com, benno.lossin@proton.me, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, jiangshanlai@gmail.com, linux-kernel@vger.kernel.org, ojeda@kernel.org, patches@lists.linux.dev, rust-for-linux@vger.kernel.org, tj@kernel.org, wedsonaf@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Gary Guo writes: > On Wed, 17 May 2023 20:31:14 +0000 > Alice Ryhl wrote: >> +#[macro_export] >> +macro_rules! offset_of { >> + ($type:ty, $field:ident) => {{ >> + let tmp = ::core::mem::MaybeUninit::<$type>::uninit(); >> + let outer = tmp.as_ptr(); >> + // To avoid warnings when nesting `unsafe` blocks. >> + #[allow(unused_unsafe)] >> + // SAFETY: The pointer is valid and aligned, just not initialised; `addr_of` ensures that >> + // we don't actually read from `outer` (which would be UB) nor create an intermediate >> + // reference. >> + let inner = unsafe { ::core::ptr::addr_of!((*outer).$field) } as *const u8; >> + // To avoid warnings when nesting `unsafe` blocks. >> + #[allow(unused_unsafe)] >> + // SAFETY: The two pointers are within the same allocation block. >> + unsafe { >> + inner.offset_from(outer as *const u8) as usize >> + } > > This has no protection against using `Deref`. The memoffset crate has a > > let $type { $field: _, .. }; > > line to ensure that the field is a direct member of type and deref is > not happening. Added. I had to change `$type:ty` to `$type:path` to get that to compile, since otherwise you can't use the token in a pattern. However, I think it's fine - this is temporary anyway until the standard library gets the macro. >> + }}; >> +} >> diff --git a/scripts/Makefile.build b/scripts/Makefile.build >> index 9f94fc83f086..ec583d13dde2 100644 >> --- a/scripts/Makefile.build >> +++ b/scripts/Makefile.build >> @@ -277,7 +277,7 @@ $(obj)/%.lst: $(src)/%.c FORCE >> # Compile Rust sources (.rs) >> # --------------------------------------------------------------------------- >> >> -rust_allowed_features := core_ffi_c,explicit_generic_args_with_impl_trait,new_uninit,pin_macro >> +rust_allowed_features := const_ptr_offset_from,const_refs_to_cell,core_ffi_c,explicit_generic_args_with_impl_trait,new_uninit,pin_macro > > Side note: once we bump our compiler to 1.71, we should switch to the > built-in `offset_of!` macro and get rid of these unstable features. Agreed. I mentioned that in the commit message. Alice