From nobody Sat Nov 23 21:52:29 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1730741629; cv=none; d=zohomail.com; s=zohoarc; b=dMdCK8GUfhq8mSc3sRwMvkJFHppcjyAMNDF9YbvFaUZQgR9q3T5wmmAqf7/9bwaJrSU91yqaaydQSuAaxT5x/Mg/nBdTxSE+cxLo9Z4xLUS5eXYU3cinry9Zez/gT0eFYU5KA79Np67M4p+gBYuvsklFi2BBs2s3dO9F8wW2km4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1730741629; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eq5qbzLPKG4WD2CsQZHh3f809/48MMF72sDvSN7iEVo=; b=Auu6VrSCu6N6+Jmatad5hZzL55gsgTC2aora6RIYMvFmio2npVmOvsa5usAC1jlqmVKC+pftYjrhO7ISCsdgdFFuffRSwDPVzB3GGlEPACFDBkRvNH/h2U/5TdU7Ag2RqdpHPJb92uYV0kjA7Wfz9fdtmn03PD4bxufxlxNXyW8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1730741629569874.7933291200616; Mon, 4 Nov 2024 09:33:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t80sg-0007Br-4A; Mon, 04 Nov 2024 12:28:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t80sd-0006w0-LI for qemu-devel@nongnu.org; Mon, 04 Nov 2024 12:28:43 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t80sb-0000Qf-OT for qemu-devel@nongnu.org; Mon, 04 Nov 2024 12:28:43 -0500 Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-ln8lSviuMhiTKz45rdcugg-1; Mon, 04 Nov 2024 12:28:39 -0500 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2fb55e102b6so27509781fa.1 for ; Mon, 04 Nov 2024 09:28:39 -0800 (PST) Received: from [192.168.10.3] ([151.49.226.83]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431bd917fa7sm187323265e9.18.2024.11.04.09.28.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Nov 2024 09:28:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730741320; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eq5qbzLPKG4WD2CsQZHh3f809/48MMF72sDvSN7iEVo=; b=JaYdjpfH9poUalcBJg4/Acr+SiPaSbbMnxqWLbVvd9W8T5WQO061cczV20+yqfXz8tLUJA Ic9QUQ7eq2APaLy5WQZVw1zTuj6uLWA41JzG6+/6ZHuLXhFxAtMGJtLHwzZA8wwfNBwnnK DlNSlFzcZ3FVcPDir6nrXyMN6yi44W8= X-MC-Unique: ln8lSviuMhiTKz45rdcugg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730741317; x=1731346117; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eq5qbzLPKG4WD2CsQZHh3f809/48MMF72sDvSN7iEVo=; b=lohfIF5NJrr1X+JLf5N6f0Kgkh6FcfnfC8+YxZ/SutdXEpwguabk3pkKcRkOLgYLHl HFYmPfKEQ1+oCBWGmk+9NODOuxZEJUtviae9/Gr0fvla0LYIQK4YiE3sAzVevY5hiVt1 f1JRKWvLhdgowkmbtnEEUiisyIJJ87t/5dgOto+ib0MchAZo/VGkOABuQCw+Byipt0Tv 2tjWfdHXmpCZcGW7ZAYPgUZadrw3S8OZrTFKPYWEIHcJoX4YH1KiNAU1bHJNjCPTu4FP 8zlY/9ldZRuXMrMReJYJAtk0Bow/H2MwLo0wCGaveHaCYqAGwjDu4+8MtiFHWCSGhc/d 9jcA== X-Gm-Message-State: AOJu0YzXp4oVdCgM1q9j9jQFpuBmE0PYSTKpcBHBHsQcBH1AUYcCJshD sNKtUgenOu0zZvyQmF4YC0iTd4p/PoCeSqtgBwMGltJNE+9W36qx5xXyoAJoVVKccEuDRdsJQux J8Nazv97MwBdqhqBAfAXT/xPiOiFojTZX8lhNfVQWEPvgAgKlQ73g94x+tGj/CAz7guhNPu1Y// Ik5WD2bAu0JRkRVg1jXX4KcjX9wh/CaR7HQH3TC+s= X-Received: by 2002:a05:651c:1508:b0:2fa:ddb5:77f9 with SMTP id 38308e7fff4ca-2fedb7ec448mr66869711fa.40.1730741316623; Mon, 04 Nov 2024 09:28:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IHENFgBbFtPZcrXOhpsL/Kt0hDBbVz4KNDOgXigmEFmYVxLZcBKOeqwjS2FVGp7keKsNSehqQ== X-Received: by 2002:a05:651c:1508:b0:2fa:ddb5:77f9 with SMTP id 38308e7fff4ca-2fedb7ec448mr66869441fa.40.1730741316076; Mon, 04 Nov 2024 09:28:36 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Junjie Mao , Kevin Wolf , Manos Pitsidianakis , Pierrick Bouvier , Zhao Liu Subject: [PULL 33/40] rust: do not use MaybeUninit::zeroed() Date: Mon, 4 Nov 2024 18:27:12 +0100 Message-ID: <20241104172721.180255-34-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241104172721.180255-1-pbonzini@redhat.com> References: <20241104172721.180255-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.34, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1730741630884116600 Content-Type: text/plain; charset="utf-8" MaybeUninit::zeroed() is handy but is not available as a "const" function until Rust 1.75.0. Remove the default implementation of Zeroable::ZERO, and write by hand the definitions for those types that need it. It may be possible to add automatic implementation of the trait, via a procedural macro and/or a trick similar to offset_of!, but do it the easy way for now. Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/zeroable.rs | 91 +++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 14 deletions(-) diff --git a/rust/qemu-api/src/zeroable.rs b/rust/qemu-api/src/zeroable.rs index 45ec95c9f70..13cdb2ccba5 100644 --- a/rust/qemu-api/src/zeroable.rs +++ b/rust/qemu-api/src/zeroable.rs @@ -1,23 +1,86 @@ // SPDX-License-Identifier: GPL-2.0-or-later =20 +use std::ptr; + /// Encapsulates the requirement that -/// `MaybeUninit::::zeroed().assume_init()` does not cause -/// undefined behavior. +/// `MaybeUninit::::zeroed().assume_init()` does not cause undefined +/// behavior. This trait in principle could be implemented as just: +/// +/// ``` +/// const ZERO: Self =3D unsafe { +/// ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed= ().assume_init() +/// }, +/// ``` +/// +/// The need for a manual implementation is only because `zeroed()` cannot +/// be used as a `const fn` prior to Rust 1.75.0. Once we can assume a new +/// enough version of the compiler, we could provide a `#[derive(Zeroable)= ]` +/// macro to check at compile-time that all struct fields are Zeroable, and +/// use the above blanket implementation of the `ZERO` constant. /// /// # Safety /// -/// Do not add this trait to a type unless all-zeroes is -/// a valid value for the type. In particular, remember that raw -/// pointers can be zero, but references and `NonNull` cannot -/// unless wrapped with `Option<>`. +/// Because the implementation of `ZERO` is manual, it does not make +/// any assumption on the safety of `zeroed()`. However, other users of t= he +/// trait could use it that way. Do not add this trait to a type unless +/// all-zeroes is a valid value for the type. In particular, remember that +/// raw pointers can be zero, but references and `NonNull` cannot pub unsafe trait Zeroable: Default { - /// SAFETY: If the trait was added to a type, then by definition - /// this is safe. - const ZERO: Self =3D unsafe { ::core::mem::MaybeUninit::::zeroed= ().assume_init() }; + const ZERO: Self; } =20 -unsafe impl Zeroable for crate::bindings::Property__bindgen_ty_1 {} -unsafe impl Zeroable for crate::bindings::Property {} -unsafe impl Zeroable for crate::bindings::VMStateDescription {} -unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_1 {} -unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_2 {} +unsafe impl Zeroable for crate::bindings::Property__bindgen_ty_1 { + const ZERO: Self =3D Self { i: 0 }; +} + +unsafe impl Zeroable for crate::bindings::Property { + const ZERO: Self =3D Self { + name: ptr::null(), + info: ptr::null(), + offset: 0, + bitnr: 0, + bitmask: 0, + set_default: false, + defval: Zeroable::ZERO, + arrayoffset: 0, + arrayinfo: ptr::null(), + arrayfieldsize: 0, + link_type: ptr::null(), + }; +} + +unsafe impl Zeroable for crate::bindings::VMStateDescription { + const ZERO: Self =3D Self { + name: ptr::null(), + unmigratable: false, + early_setup: false, + version_id: 0, + minimum_version_id: 0, + priority: crate::bindings::MigrationPriority::MIG_PRI_DEFAULT, + pre_load: None, + post_load: None, + pre_save: None, + post_save: None, + needed: None, + dev_unplug_pending: None, + fields: ptr::null(), + subsections: ptr::null(), + }; +} + +unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_1 { + const ZERO: Self =3D Self { + min_access_size: 0, + max_access_size: 0, + unaligned: false, + accepts: None, + }; +} + +unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_2 { + const ZERO: Self =3D Self { + min_access_size: 0, + max_access_size: 0, + unaligned: false, + }; +} --=20 2.47.0