From patchwork Wed Jul 19 14:20:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 122651 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2482802vqt; Wed, 19 Jul 2023 07:35:34 -0700 (PDT) X-Google-Smtp-Source: APBJJlHRN72iLulvDw/z1ecRSPlxsC6C/t/OCBGTKmSATaTe0BQN6VWPYDpQyW/VBlKC/sJutZaU X-Received: by 2002:a50:ee0e:0:b0:51e:5ec8:d2f7 with SMTP id g14-20020a50ee0e000000b0051e5ec8d2f7mr2364447eds.30.1689777334166; Wed, 19 Jul 2023 07:35:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689777334; cv=none; d=google.com; s=arc-20160816; b=aASIp7VxddVyMGR5c/R2deR/De8OXkygiruJlJPseuH8IXIoIEQljmFNpVT/u3zQ+f b5aEQyk0UQx+fFFUiMcqfy0jYa+Ni47V9Y7VDRkwh4ci48d3xFgG6JyZWYLGDyrUyYLh ZbXH1S3AP2WDY3vRxOp9Z+0WA2j4h9kMaLKbzsqu0fwd/RLIKSVBt1ubHO5G/O/Ja4oe TkzUQzwA8vzHllX0OMBeMvnfvPGcwT0gD03i3YNqlX+BmHHo/FP07T3IUMjbVNd5e4cf T6nNQzkcIwUJ4ITj6OpN96qDGRtW9n2vOPZ2UmXCDcTx6pfBCHgiKHiWDPJCK+Jlp3+v X6cA== 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=ssgVP+xm/xUxRo5NGKU6FQiwAj0frSX0JCoESMRG9+4=; fh=qm1bOEYzVYZKL4fg86TgWb8D+QJA9mGpUhKFp2A6Xio=; b=wXbV9f7RqKhZS4JkCLAOSl7pk+uhp/exYgnSpNmyj/loSF09eFdc4civ8riXNF4zbE agAnuTR5u4JMPC79jCMjdIYRoS4Otl5daC45m2Qu05bD43WP8jlwrbgzADUlar8wvWtb 0UVcjcc9zBrS2sivrq2J9OiYLQpXncpwQWgvw5oYDm/4jCBLEPwoxay52zpT4lUUhjyn 4nlmPt5lqCGwMR6Ufahq7XG7FR+QfClM02Ffrr2GD3m32VV7AN/qA9mCD37FhfOodZvm whaJCX1JPnB9zwkWu5VZ1LQvjoe3rk74XYNzX/gaQo/kuyKGrxUENFjVhs2HDtY3/ov/ 6I3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=r5yynngpuvbuthttlghkc7g77y.protonmail header.b=g+vfPcqM; 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 o22-20020aa7d3d6000000b0051de4c29577si3010647edr.184.2023.07.19.07.35.08; Wed, 19 Jul 2023 07:35:34 -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=r5yynngpuvbuthttlghkc7g77y.protonmail header.b=g+vfPcqM; 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 S231247AbjGSOVb (ORCPT + 99 others); Wed, 19 Jul 2023 10:21:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230375AbjGSOV2 (ORCPT ); Wed, 19 Jul 2023 10:21:28 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E10F2122; Wed, 19 Jul 2023 07:21:01 -0700 (PDT) Date: Wed, 19 Jul 2023 14:20:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=r5yynngpuvbuthttlghkc7g77y.protonmail; t=1689776458; x=1690035658; bh=ssgVP+xm/xUxRo5NGKU6FQiwAj0frSX0JCoESMRG9+4=; 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=g+vfPcqM0arg3Xzi+IPsZG7G/BF/jKT9SNSx+ctTlu2zdhzxFxxhOAGG0pLGfFEPY 10fLtiyor6Ot2+c+USFvHRwDiNqwalSOlAI+X3jUMXn3uuSbfushJedmjJNmqS1dj/ XuwFeMSa//FBPmn1psngv0glJ9+r2I2lIYYU6aOcpsv7g2T9BlhUzl7tsor77HP5Y2 tVR4tv22Gkc/bnJbkrilMGfm1N5S0l2SufWT2aDkfNesWLsSZNMnDycmknTkeym24l r4OVWO8ejG6ipRs+I54mIwWe17uF7EQg8xMYncAq1oQ1FOyna0jIllEyMfO5fl6e4H ZtO/KzbmaxBwQ== 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 Subject: [PATCH v2 05/12] rust: init: make initializer values inaccessible after initializing Message-ID: <20230719141918.543938-6-benno.lossin@proton.me> In-Reply-To: <20230719141918.543938-1-benno.lossin@proton.me> References: <20230719141918.543938-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_DNSWL_BLOCKED, 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: 1771859957892632213 X-GMAIL-MSGID: 1771859957892632213 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. Signed-off-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl --- 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 160b95fc03c9..5de939e0801f 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -1073,13 +1073,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. @@ -1105,12 +1105,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. @@ -1136,11 +1136,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.