From patchwork Sat Jul 29 09:09:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 128097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp953662vqg; Sat, 29 Jul 2023 03:27:04 -0700 (PDT) X-Google-Smtp-Source: APBJJlF8uf+Uz7irirRhpKQRcfo666gqhPgn1u0DXiSvXO+vnXCjFWmEXPNh9CfjIl5y8qp75sJK X-Received: by 2002:aa7:d616:0:b0:522:3081:ddb4 with SMTP id c22-20020aa7d616000000b005223081ddb4mr3877342edr.20.1690626424683; Sat, 29 Jul 2023 03:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690626424; cv=none; d=google.com; s=arc-20160816; b=MaGv7EjdwL4XFnPG/QHNhQnsceQqHgHjZovTO9GJoPtFJsJiC4hwhqyYNSGxUx13Qe BYc+nATB0RP6kYdzzYnbmrwkAl67xlSDgOlsO+eZ9dZqgTCE79X8VD0VFZqZFKo6qRC4 Dgp0ZnA7VbKKrLKE5ao3U44OU4qymJSpKxMOqGKI4kBDV4nWUnngRSCZqi90tYLEGKDK vW1Cmi1IBwWhVLeK3x2OWCiVmup100iB5ivmAdH4S9AuWcXBraCHHsNw7pteoYKDA3pw o4QGr4MWhkA9oxw7oDy0McrV7WW1fGFdNjF0yEXRwEe1y+yj25FUV+drtJrJbQMAHicU M1gw== 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 :dkim-signature:date; bh=rixSAlF8H68fR6m3THBV7cCNL7So1w86OXDiq2xYBjU=; fh=Kxkq5MAR/ee06c4zNa7/ow9QmWBIyeCoT7WYYnSSoIA=; b=rHAp7m6ABuBos5uezhgQzVCNnr3EQtcmyBymcNDMkr2dZBIpaO+YGv7ObZBN57KwLP phzt7e1xzWDWBxhBo9XzJifqVxQwvW3Fhn5EIntETsBqt0RRzJ4caAdVA3MYn3zlf2Hb eUUvW7P5XySUZ3PwmwQayHT/HOELdjITKPeLbIU3JxIECGqShCa4aYDOwwR3YAsMAJWX KHVEuOhmZ9evX1CxMgpXMCCe2KDxgl/DXb6jp0MxvSVHxiOaZpryZEMreymHoDYp2ipB z6ZeKeA4LJfOSLKtE/Dol33QqLAcR4QKwDWoVBQUBZKALZvT8w/2JXbSfHv45zv0Fm2E x48A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=fnB7DKQr; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m21-20020aa7d355000000b0051e065bbc3csi163243edr.8.2023.07.29.03.26.41; Sat, 29 Jul 2023 03:27:04 -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=@proton.me header.s=protonmail header.b=fnB7DKQr; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231590AbjG2JKl (ORCPT + 99 others); Sat, 29 Jul 2023 05:10:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231527AbjG2JKb (ORCPT ); Sat, 29 Jul 2023 05:10:31 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBD7449C4 for ; Sat, 29 Jul 2023 02:10:03 -0700 (PDT) Date: Sat, 29 Jul 2023 09:09:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1690621802; x=1690881002; bh=rixSAlF8H68fR6m3THBV7cCNL7So1w86OXDiq2xYBjU=; 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=fnB7DKQrA5Mk3rcM0qdH1bUwWWwZruoJbh//pq9ObBMiIuInGNNV8FSd6v4xXHd6Z eBJ8TiauGbt8+31hQ3QoQ4JHTlkaaW9jl4S7EYP6FyrdW2wF6YFPFlEN2cUxt82UP3 5qF43KJejvkdGm5PTJ/fF3eQsjH4LrYxXW3FYBTe9isFjDp/E3SuvmKGEPbIUkocf0 Q+Haw7xBMFQ8sy0zpMg+NIiVNOO+2IJ1plEaaZtZB8OEGBH+r+OdwO7EoV8EhsexrD UPZ7X097/SV6kA4GbIurWboVWrBdHSy36+FRBLCVOV/XPPOhsdeNGcyaSOCqnyQUGf RAEFQxJXWLkyg== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Martin Rodriguez Reboredo Subject: [PATCH v3 06/13] rust: init: make initializer values inaccessible after initializing Message-ID: <20230729090838.225225-7-benno.lossin@proton.me> In-Reply-To: <20230729090838.225225-1-benno.lossin@proton.me> References: <20230729090838.225225-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772750293729172836 X-GMAIL-MSGID: 1772750293729172836 Previously the init macros would create a local variable with the name and hygiene of the field that is being initialized to store the value of the field. This would override any user defined variables. For example: ``` struct Foo { a: usize, b: usize, } let a = 10; let foo = init!(Foo{ a: a + 1, // This creates a local variable named `a`. b: a, // This refers to that variable! }); let foo = Box::init!(foo)?; assert_eq!(foo.a, 11); assert_eq!(foo.b, 11); ``` This patch changes this behavior, so the above code would panic at the last assertion, since `b` would have value 10. Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl Signed-off-by: Benno Lossin Reviewed-by: Gary Guo --- v2 -> v3: - added Reviewed-by's from Martin and Alice. rust/kernel/init/macros.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index 2bad086cda0a..cfeacc4b3f7d 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -1075,13 +1075,13 @@ macro_rules! __init_internal { // In-place initialization syntax. @munch_fields($field:ident <- $val:expr, $($rest:tt)*), ) => { - let $field = $val; + let init = $val; // Call the initializer. // // SAFETY: `slot` is valid, because we are inside of an initializer closure, we // return when an error/panic occurs. // We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`. - unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), $field)? }; + unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? }; // Create the drop guard: // // We rely on macro hygiene to make it impossible for users to access this local variable. @@ -1107,12 +1107,12 @@ macro_rules! __init_internal { // In-place initialization syntax. @munch_fields($field:ident <- $val:expr, $($rest:tt)*), ) => { - let $field = $val; + let init = $val; // Call the initializer. // // SAFETY: `slot` is valid, because we are inside of an initializer closure, we // return when an error/panic occurs. - unsafe { $crate::init::Init::__init($field, ::core::ptr::addr_of_mut!((*$slot).$field))? }; + unsafe { $crate::init::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? }; // Create the drop guard: // // We rely on macro hygiene to make it impossible for users to access this local variable. @@ -1138,11 +1138,13 @@ macro_rules! __init_internal { // Init by-value. @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*), ) => { - $(let $field = $val;)? - // Initialize the field. - // - // SAFETY: The memory at `slot` is uninitialized. - unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) }; + { + $(let $field = $val;)? + // Initialize the field. + // + // SAFETY: The memory at `slot` is uninitialized. + unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) }; + } // Create the drop guard: // // We rely on macro hygiene to make it impossible for users to access this local variable.