From patchwork Fri Mar 21 11:25:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 875232 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f4c:0:b0:38f:210b:807b with SMTP id cm12csp807193wrb; Fri, 21 Mar 2025 04:25:56 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXmjtTXdmtFkFdWfGYm8inuWViXXXuBnGjax82+rhxfbr+ci4jaty23bhU5Z+gvSVhhMh2RTg==@linaro.org X-Google-Smtp-Source: AGHT+IFKotAIgvTBEn1qDqboAG1GP7pC43JVwzyeIynllUre2QRaJMeFBBs6oBtXvuOyKWBylKhb X-Received: by 2002:a05:620a:390a:b0:7c5:464b:671a with SMTP id af79cd13be357-7c5ba1ea62fmr323047685a.42.1742556355924; Fri, 21 Mar 2025 04:25:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742556355; cv=none; d=google.com; s=arc-20240605; b=Id5Bc9xVrlUDzNat+LcVbBndXHib2Tewv3pouvK+thlgrJGfVBk9buSpLjKEIFVktC WmeR28sr+UQ8F1zx6M9HoJrdZzjvVqpTGNspq43g3ZxAvDW2IfZ8ULx2pHtjJn/qnFWK 2so70lL8A2R/jnKVsd7mJXplE9XkBwYtfNfyubkG0q1ViAiwwLPdOuPgPD55KvCs1ba3 q7T19iEgAhk8IJiAir0cPaCHGQCPLF0zlb0SKaPHouEsq8q3VAixNX+MGF5IoVPQNH9v lhyw9Jm/BQ+er8M0ZdICd2cstficsEFtA6/ci0YIngIxgHFQdYSgxnFiQarqj+M3QfIJ GdfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9vH8UmsTVl9pFvTYnHstPZudBd2C6Qn5erg4QfltUDw=; fh=M3/0CyqwDeCaH7k2MEIotI/UI6VUSoTVPtiMb3m6vdA=; b=Tu0GRBx7jxi+osrqFazoXAGz91ov0kDWDdT2iOyy7Y8w4kyKS78I+8F3lVsDtzSCLJ l74QuyAUi/9quRlUWC1zCXUssm8dXWLhsxMJWx/+kWh8Yy08Y5ALzOOzSmR6/CGIS9os KHnW+xghOOf26jda4URJgOwyQmYKDCvxmE/0tdb9Oa7vbRIXdngGCQzNJ6ho4zGm1Otx 5lZR/5Jbn892X4HOrakx+xf1E/BYzNkN9q6dioj9k+2wzEkXHbfBRwGzlLcR+SiXFpzS ufTJseV7019I9nbn/x1qLoVxLf1ERY+qTakxeF3xsloWRMSsHU3xQIoXKNqtwH/2a5Lp S+NA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PgMx0PEs; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 6a1803df08f44-6eb3f04d207si16830146d6.476.2025.03.21.04.25.55 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 21 Mar 2025 04:25:55 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PgMx0PEs; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tvaVU-00042c-Pe; Fri, 21 Mar 2025 07:25:47 -0400 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 1tvaVK-0003w0-0j for qemu-devel@nongnu.org; Fri, 21 Mar 2025 07:25:34 -0400 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tvaVE-0004OM-30 for qemu-devel@nongnu.org; Fri, 21 Mar 2025 07:25:33 -0400 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-43cef035a3bso13390075e9.1 for ; Fri, 21 Mar 2025 04:25:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1742556327; x=1743161127; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9vH8UmsTVl9pFvTYnHstPZudBd2C6Qn5erg4QfltUDw=; b=PgMx0PEs61XRjPmZ2ULXErS8IL9F08LY1PhBHHvaJAfksmgFE12RCD7PTzu84SMh9a j9LsYRIkxvKcjuhreejmM0otf+J4rjIIliIq5/1RvSMmCG35kCAwskyVWG66GANGF1V4 eF2bisBvb7nL+VuyVF5T6WaaUTE+Q58qK88k/7aX7VmQqNmM6RNhiuUY5N1LfKZlzjTR AbLr/5rcG6I37CSFs8nlM7NcHbQXFfcuGMkukZ3Xvq1J2OpjoYIBK7W9NPmr2VjRvgwc VesnX4wyo5LnX3SiuQf8Xg7ys0Cv8ddDVbbyxmNtfaZRRHGTccRHdKyUNkUg2FDGSzhN +Ixw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742556327; x=1743161127; 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=9vH8UmsTVl9pFvTYnHstPZudBd2C6Qn5erg4QfltUDw=; b=oa5H2gJh3UW6aQeYOzgdnrWoPd73j4RUsD3Kn31dJ22M8dNUmTWK8VEB/pNX2pjyYS Ky7TuKNTx9YHD1hyD5iMClYRYMOku6sIjREKZU99/jQIk1QjoSnAU9vdq+NlObTSYsmB R2J7fLOqvKqO2XRNc9nmDdZJhwKYt5JMDW9FebYh3pvYb7VNpHYluX/Un0y/dQGZIG0B YXkDnX6JbzGxh92DiSXkjKJmnXBc621yCkZMRERwAXKeZ2oAlm21nP74nLmphutBBd5Z An1TBZCWedOeUHSPpMu259fnj8HAZyq4HOB5fUAzbrwUqdbbAFmvZfeShB8LMlLoeyxf Aa4w== X-Forwarded-Encrypted: i=1; AJvYcCW5n3/in09DFw+5nbn/guNcTtHQVl3TWluk1a3APT9BYnvTuJ/Vti41CkRuHOWlDpH55nB5fUgXrsR+@nongnu.org X-Gm-Message-State: AOJu0YyTWmk6FUY6N1uOFDConhqhvM31c+gMc+aUY5JIm2WHsVXJafDK ULXc9SuyXDuksGqmQ0+XiPQniYt31BIGOBUM/UCcDZLZyVkVMwnDr09LnrLMMRk= X-Gm-Gg: ASbGncuJjlZpfB1HkcJJmIa6lQKavVGQLnIc/YZSmxs4vLGnIH2rAyCqGR/2wneMGJh ZCKJNJO29sUcQCubtEWAP7QFvPLpXC00/2CfKSUJST0q0gmzhQ/WbMscIRxSE0CJ4iJQR2GCMUA mpkCtr1lYorx6FGEJxZ/VXNg7/ATWGgSnfmcIQ3kQMv9xCJBSb1Rr0Am6LEOngRbg91Y1Ohq+Rp udrvNZtQQOkzDtU5OQJ08mwJjQ/n1Mz614ssbOoMjujRwq40dtCgQO7x8rnR7A6d7Fbv57oKIs2 EGpZjGZRigS+fmeKo/DLzRJtyuPA/HlYfsBfPrys6ikBch2yeOI= X-Received: by 2002:a05:600c:138a:b0:43d:224:86b5 with SMTP id 5b1f17b1804b1-43d509e38eemr24686085e9.4.1742556326636; Fri, 21 Mar 2025 04:25:26 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9e6767sm2132614f8f.66.2025.03.21.04.25.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 04:25:26 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, Paolo Bonzini , Zhao Liu Subject: [PATCH v2 1/3] rust: assertions: add static_assert Date: Fri, 21 Mar 2025 11:25:21 +0000 Message-ID: <20250321112523.1774131-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250321112523.1774131-1-peter.maydell@linaro.org> References: <20250321112523.1774131-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::336; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x336.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Paolo Bonzini Add a new assertion that is similar to "const { assert!(...) }" but can be used outside functions and with older versions of Rust. A similar macro is found in Linux, whereas the "static_assertions" crate has a const_assert macro that produces worse error messages. Suggested-by: Peter Maydell Signed-off-by: Paolo Bonzini Reviewed-by: Zhao Liu Reviewed-by: Peter Maydell Signed-off-by: Peter Maydell --- rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions.rs index 104dec39774..bba38cfda11 100644 --- a/rust/qemu-api/src/assertions.rs +++ b/rust/qemu-api/src/assertions.rs @@ -120,3 +120,25 @@ macro_rules! assert_match { ); }; } + +/// Assert at compile time that an expression is true. This is similar +/// to `const { assert!(...); }` but it works outside functions, as well as +/// on versions of Rust before 1.79. +/// +/// # Examples +/// +/// ``` +/// # use qemu_api::static_assert; +/// static_assert!("abc".len() == 3); +/// ``` +/// +/// ```compile_fail +/// # use qemu_api::static_assert; +/// static_assert!("abc".len() == 2); // does not compile +/// ``` +#[macro_export] +macro_rules! static_assert { + ($x:expr) => { + const _: () = assert!($x); + }; +} From patchwork Fri Mar 21 11:25:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 875233 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f4c:0:b0:38f:210b:807b with SMTP id cm12csp807379wrb; Fri, 21 Mar 2025 04:26:22 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWzV+h0OIbxBqKtAYDp+IlRZ+2Duhc91eSDd51TOcHE9T9L+fP3bMcuIApf0L3KAMQjIYUa5w==@linaro.org X-Google-Smtp-Source: AGHT+IGN7lFyn/0kWNsWTb99JH0TaqFdcZ0JRjACgF2zzSM2mJZO5yIpMXApVZVOqSv9DOO6Y5nh X-Received: by 2002:a05:622a:5914:b0:476:ac73:c3f3 with SMTP id d75a77b69052e-4771dd6230fmr48625921cf.1.1742556381870; Fri, 21 Mar 2025 04:26:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742556381; cv=none; d=google.com; s=arc-20240605; b=LwYZuYNH39aTHjCZoShb+4KEOw3s/61a5p6e1JBvO3u7Bi8XsROq7kB6XGHcpwiDCs iQBsYjCeKbu07FnqfXnj986SKnsdLjk54BFe9ZaHZp7p0Y5UDmtzxXDlxULma13ipZG+ Jybxk9wENthiO28TpPxWWk3oyOed60EUWtIu7XihgEwl57rYxyusf+n5L89YQArmzzsq 12u3udD2/t+t7LoFpBDVFSPaF3i06X70hldZzfY38VpCMFpssC30mbVJWmfC8PTvfUF5 cIHlDge/cyfZ0uOZEN9t8XRZxaq032Lq+nfJ41l+aKzAEQNhc4b1hH++aQNE5BIi9EyI UlAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=DwalI5yLXIYOeimdWTNSfaCvh6YDdb17pOS3Ktb3jnU=; fh=HLXnNMteiPzuWkYQ6K1hv7SXDKDH1L9N99dPHN0bf7o=; b=MUtb8zoXI7Sjz0dSJ9UNjicJLhY6xsmB8cdavg1Zuv9QZHvLUQ/m00uTYp8q0FCHSV QHmIzTjh+2XKReUnvJL+9wqJSgyha1wMmN3XtC/QX5CjkF7fLVTzZIMCmrbF2c1Rgz42 2mGrYzelK17w9rMPWRtLngjDnmEXC8/O7NKC8xXPYqQeH2DYz6QZOCw5z4OXMCMOyKjb s+U01RjP5lfxPURxB6gNYwY8oEU/ISXSYciEK5kqqBBCRfAajmczs/qeGCQY9vQuz4VX dPHQ3esIAzdykeFon2JGRIEy+VIkyXvcFjSt+VlrJTNVmrHkLHebXk6VL3WO043dhxFM rWKg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tMXr9FSM; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7c5b92af79csi180254485a.44.2025.03.21.04.26.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 21 Mar 2025 04:26:21 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tMXr9FSM; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tvaVT-00040e-0c; Fri, 21 Mar 2025 07:25:43 -0400 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 1tvaVJ-0003vf-4m for qemu-devel@nongnu.org; Fri, 21 Mar 2025 07:25:33 -0400 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tvaVF-0004Oc-J4 for qemu-devel@nongnu.org; Fri, 21 Mar 2025 07:25:32 -0400 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3913cf69784so1493555f8f.1 for ; Fri, 21 Mar 2025 04:25:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1742556328; x=1743161128; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DwalI5yLXIYOeimdWTNSfaCvh6YDdb17pOS3Ktb3jnU=; b=tMXr9FSMdIAnk+lOHR9mvLUnfSKrhGFfKvNQ5+SYCe2MxHrGhy3PMlWNuzZ99kYmua E3bbmZwKIyOrrjjeq+NFsgWEyw+BAC1lEj+MHklzJLQtlB14o6uoTNxOdkavQSt8Dgky GXypwXPPS6G1ShqPRbiYmm9FV0EQssYjtOR7fT/AC9DU8jdZMHg1Av7cp0jwi9/H7tXE Z/t/3EVgpQF8EUX8DJ/wVewqqcwYYO54ZipouRgAEvxx58xuMiQo8Q7lvzKvhm85yQVs s229yjtjFyRDXjCkoCXN/gaXbtd0WqoKFch8yfvqXExY9xM9/Js93kMOszeE45P+LDM1 IOZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742556328; x=1743161128; 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=DwalI5yLXIYOeimdWTNSfaCvh6YDdb17pOS3Ktb3jnU=; b=WMJC58KB1Ci+C0dN9AQNDQMp9+/28f16GahgDxlUQ0VZm1GJEZoD963T9AyCZl0il+ tYpVqGw2CgsF6I1tUAW9Xz9tuKi6EbgoG+CxJ7WQ5A68euLJPmK0aTnHD6TJvS/BvAXD Or1j/JKoKBsUMBg3oJT9k0IK+nPZerW1XQjzNPA+odPYzBwcsOg08+qWZhFWPeTF6FE3 I+geNPykTNLmiThl5f9L5bn4gcvOo8xfVe8140Pdaj+e1FT0WsZgvNb7AGY6wPeMyMzj N7aB7aGjRgQrygdyhrwtpCVjrINSYuLis7xjSfWSO/mIrlnbXFXkglgRbhcISK858de4 FV3Q== X-Forwarded-Encrypted: i=1; AJvYcCU3cqDGc86+dMEnOHDzSy8oZ8dy+LveFYLeJIDtIwkpoL+JWTBm/yZiV0lutl2hC3UWKSxcfJ0qtnle@nongnu.org X-Gm-Message-State: AOJu0Ywe69yj8LyrTwskc3ZzjBfbwrWeWQmbr8J6/5GRguFORYIf3t8p divNB0NPDnYNuOo6Exg2Mrp/ZE87DkAvaCeN/2CObyD73Umxmz3oc4Q0s02b5M4= X-Gm-Gg: ASbGncsrEZjwtenrYhyE2yDSIEQIz5VA000HkRTJRW24p46KE4ACOShyFWaNEev9H9F cSLy3PTNdaNHlzO5zxMK1JH5OkS1DuDXeFit6ha1sm2I3cW63+HCycokS8WQxK4Kc2PNPA2zI76 w7pPjI004GKpWIL39d8zbEN0GyTX/LWk5opgQrmk4UGCzXUH1XIYAVTfW6yZzZpu9Y+Jc+I1ar0 r+6e+4f4o8vzvFPnS7LpQNzKhvRj1FyB6v6yBoYNRxSJ27I60V19A06a5NXbupw0Q0v86AYEIuK lhkWkGMQkjJ8c1xnFq4fj+NRbaYBMMsvJONbpkow03sqnSZn6/T9Rotjvq0Xxw== X-Received: by 2002:a05:6000:188d:b0:391:3124:f287 with SMTP id ffacd0b85a97d-3997f9014a6mr3430455f8f.16.1742556327776; Fri, 21 Mar 2025 04:25:27 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9e6767sm2132614f8f.66.2025.03.21.04.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 04:25:26 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, Paolo Bonzini , Zhao Liu Subject: [PATCH v2 2/3] hw/char/pl011: Pad PL011State struct to same size as Rust impl Date: Fri, 21 Mar 2025 11:25:22 +0000 Message-ID: <20250321112523.1774131-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250321112523.1774131-1-peter.maydell@linaro.org> References: <20250321112523.1774131-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::434; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x434.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We have some users of the PL011 struct which embed it directly into their own state structs. This means that the Rust version of the device must have a state struct that is the same size or smaller than the C struct. In commit 9b642097d6b7 ("rust: pl011: switch to safe chardev operation") the Rust PL011 state struct changed from having a bindings::CharBackend to a chardev::CharBackend, which made it grow larger than the C version. This results in an assertion at startup when QEMU was built with Rust enabled: $ qemu-system-arm -M raspi2b -display none ERROR:../../qom/object.c:562:object_initialize_with_type: assertion failed: (size >= type->instance_size) The long-term better approach to this problem would be to move our C device code patterns away from "embed a struct" and (back) to "have a pointer to the device", so we can make the C PL011State struct a private implementation detail rather than exposed to its users. For the short term, add a padding field at the end of the C struct so it's big enough that the Rust state struct can fit. Fixes: 9b642097d6b7 ("rust: pl011: switch to safe chardev operation") Reviewed-by: Zhao Liu Signed-off-by: Peter Maydell --- include/hw/char/pl011.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/hw/char/pl011.h b/include/hw/char/pl011.h index 4fcaf3d7d30..299ca9b18bb 100644 --- a/include/hw/char/pl011.h +++ b/include/hw/char/pl011.h @@ -52,6 +52,11 @@ struct PL011State { Clock *clk; bool migrate_clk; const unsigned char *id; + /* + * Since some users embed this struct directly, we must + * ensure that the C struct is at least as big as the Rust one. + */ + uint8_t padding_for_rust[16]; }; DeviceState *pl011_create(hwaddr addr, qemu_irq irq, Chardev *chr); From patchwork Fri Mar 21 11:25:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 875231 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f4c:0:b0:38f:210b:807b with SMTP id cm12csp807189wrb; Fri, 21 Mar 2025 04:25:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVKDSR7nJI2+3ykhlxITKMUbkCRVMDs1Hbiudra5gR3ENhR5DCzLiT61IrAo795Yxpsnm7ktw==@linaro.org X-Google-Smtp-Source: AGHT+IEAv/RWNC66E1E2FRbyJjDz8tC+5P5jIY8nIfAiHIP0r6kJ6WtLA4DL1B9dbukbpnO8/Z84 X-Received: by 2002:a05:622a:59ca:b0:477:113a:2b49 with SMTP id d75a77b69052e-4771dd5d8eamr45430621cf.3.1742556354852; Fri, 21 Mar 2025 04:25:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742556354; cv=none; d=google.com; s=arc-20240605; b=Fb3+gfX6Wx9euWPG87leRe1ELcOC/hHAI2shaVOe42M+20VkE/0zBfbhzE9b1wHp8t 98q7VeM8dN8DMCj+UVSTHiKkwbTK78h7sTtOatJF0IIF8dQ9I0WVuyhaaN1Wyr4hY3pK 8M4zK09jVCZRsvQcFZ2lG3KzmNyy4tXwOhgH7Pd62SJTdPG4d9Eq1WQbzLXj5lMIRXzZ tnWaHg3BTVpUFilxn3wDmC+jcNeBpK0sLyTOEF8OU+YbA+Ndekhpeks2J1fhKyn/l7Hg n0Efddj6iWf9ei0JvPnrgGhtUCiUvtBMmxMtB/xdhpQvf+L7p2nQU8EkyZhPBhG01Gfp WCXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NzDaV833+sNCdch5vTHYgYjsDrkvTWOgMEcsYmvQ7Aw=; fh=nkUwK14Rko481OoP9lZW22Eno4pNVh/EeqL/ISZn+LU=; b=RBK/xSA1ztAcjlUfpOSnDEm4wX908hCmH4Q46LwBv7nRTL82EFvL0kw7sG+mbsFGBL EWffreQ7i7CO/eLlHy27JD6nyBZ2Tzzu9agEMnU6uRF0crezFodfuZ18rxAxUHgumMEa snP0btMYX/XlJHZN0HecPfNP0eexxLLqoC66M8iOY3usEscjLpSKvnpRK0TLT9bnZjv9 CiFxQeZhv47nbps3kvgauba0taqzjR8nrrzQQm04eXO0NoLVgchpYd9KHVjYDJ9GHjR5 yF743J8cc05Ot842GSTOQopFOgN3xmoXHnI4oQn08YJaz+nqbmpZFOqBgMPV5UPZsO7W lK3w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O6i5iEGG; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d75a77b69052e-4771d63ab00si16873431cf.520.2025.03.21.04.25.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 21 Mar 2025 04:25:54 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O6i5iEGG; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tvaVO-0003z5-Uw; Fri, 21 Mar 2025 07:25:39 -0400 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 1tvaVJ-0003vd-4f for qemu-devel@nongnu.org; Fri, 21 Mar 2025 07:25:33 -0400 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tvaVG-0004P0-AE for qemu-devel@nongnu.org; Fri, 21 Mar 2025 07:25:32 -0400 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-43d0618746bso12611005e9.2 for ; Fri, 21 Mar 2025 04:25:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1742556329; x=1743161129; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NzDaV833+sNCdch5vTHYgYjsDrkvTWOgMEcsYmvQ7Aw=; b=O6i5iEGGX5ipDvgPxkTufU2YNGxN1dr8Uxy7Lzluxr4+UHIUkOUnaSU7pZ93kd+G9p 3JvME4u+hKabGBuscYPgCT+nPxStBtLeZibenh0mJWGpR4w9iybI1iPAUGS06QT22v6i JPlGjZtYlPYFUBRNmCAspBM+pLpLAK9cb8ies5acxes+z40tzAiV/payNsJ1WpOVPxC2 XCvx29+S54E3DqXbhY0N6bXTA5pISwnUAQPdeso/BHZhjCBz3PKnW2V2NsDVV8l1hvnB YPinfweBxRSzyE5xuz/7Ll978O3zMHcZkXc7+g3G/hrZf0SBHrmYsvVel3ZOM0n3U0r1 UQow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742556329; x=1743161129; 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=NzDaV833+sNCdch5vTHYgYjsDrkvTWOgMEcsYmvQ7Aw=; b=G2d4yA9Syl1FSLgDxkdL+IFiaVjObMJ1PQDT/eo8J97mu9jyWnPmUsYS3i95rcu5YP 6nteATDZN/u1ix+fYog1WbJa4us3n73SBAeN07kLGezJRi9szDlCe6UBlf/ZNVXLlnrL xxg6Qe7ZBmsxMj84DOeG5s2rtwaWiN7rQGSjL0JF1J65CW+2PURhogQevJv+qUrTKX4p DCKt3yvcSssIB0fbArzgrU8UXD+7efaqNEOIPk2XZEWWicoeSUrs+IeB/nP8IThmEMoi REda+rg9Alm+yr2L59uv8STePkyO1p0+Hn3e+AFyDAtBS/P0pWkn5HiwHegrtUSy1crk NDQw== X-Forwarded-Encrypted: i=1; AJvYcCXSlqz0bRZd1vOze3jDYVM8/g0OtngRPwndAG7I1XCQs2AaYpfZX0rC+TiYer0295I8sO/yW5G8V114@nongnu.org X-Gm-Message-State: AOJu0YwNyRO9JxT6acghIYVM0ouW5qSin597Xp1YcyTQfMvg8U/UEToO 4w71gw6anhR4abNcvneW7giBRo14+YTo1OT3yi5yTE8HJZkCge63YKJPMbksryOw0QFiRecpaRJ 2 X-Gm-Gg: ASbGncsoTgEHOfGjiYH78K5/+fvbwZQif6KYF6SzdhQEDXMzXrjVWnNdpEEBak4oOsn jhYu3qjBFi7tVoq2LK09/9TPeWjV1ORdQl3lIbnoCxRMmU5Uehkjyr94D5742AWAfSQSCm72maw nR/rEU6pPaetdty/M08unUeRTGfmMEojAzMhdIB30Mfi7LunTED2Hu/qRS+2ASIEhvi6Af/dml5 H7M1UrC+HVz72bSYnup0yFOrSWFlQktPkSyQCKxhSrj7AhcJbj74edi+I414jhtYh7Hj268O0fs RqYOB/vmQMwTcSoLlbJ4zFbtmQ1oIVsIapZI510Bn+Q6+O/8jDQ= X-Received: by 2002:a5d:47a5:0:b0:390:fc83:a070 with SMTP id ffacd0b85a97d-3997f8a4e9cmr2587541f8f.0.1742556328752; Fri, 21 Mar 2025 04:25:28 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9e6767sm2132614f8f.66.2025.03.21.04.25.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 04:25:28 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, Paolo Bonzini , Zhao Liu Subject: [PATCH v2 3/3] rust: pl011: Check size of state struct at compile time Date: Fri, 21 Mar 2025 11:25:23 +0000 Message-ID: <20250321112523.1774131-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250321112523.1774131-1-peter.maydell@linaro.org> References: <20250321112523.1774131-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::332; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x332.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The PL011 device's C implementation exposes its PL011State struct to users of the device, and one common usage pattern is to embed that struct into the user's own state struct. (The internals of the struct are technically visible to the C user of the device, but in practice are treated as implementation details.) This means that the Rust version of the state struct must not be larger than the C version's struct; otherwise it will trip a runtime assertion in object_initialize_type() when the C user attempts to in-place initialize the type. Add a compile-time assertion on the Rust side, so that if we accidentally make the Rust device state larger we know immediately that we need to expand the padding in the C version of the struct. Reviewed-by: Zhao Liu Signed-off-by: Peter Maydell --- v1->v2: fix alpha order in 'use'; use 'size_of' and 'qemu_api::bindings::PL011State rather than 'mem::size_of' and 'bindings::PL011State'. --- rust/wrapper.h | 1 + rust/hw/char/pl011/src/device.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/rust/wrapper.h b/rust/wrapper.h index d927ad6799d..d4fec546571 100644 --- a/rust/wrapper.h +++ b/rust/wrapper.h @@ -65,3 +65,4 @@ typedef enum memory_order { #include "exec/memattrs.h" #include "qemu/timer.h" #include "exec/address-spaces.h" +#include "hw/char/pl011.h" diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs index f137b49feaf..bf88e0b00a0 100644 --- a/rust/hw/char/pl011/src/device.rs +++ b/rust/hw/char/pl011/src/device.rs @@ -2,7 +2,7 @@ // Author(s): Manos Pitsidianakis // SPDX-License-Identifier: GPL-2.0-or-later -use std::{ffi::CStr, ptr::addr_of_mut}; +use std::{ffi::CStr, mem::size_of, ptr::addr_of_mut}; use qemu_api::{ chardev::{CharBackend, Chardev, Event}, @@ -12,6 +12,7 @@ prelude::*, qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, Property, ResetType, ResettablePhasesImpl}, qom::{ObjectImpl, Owned, ParentField}, + static_assert, sysbus::{SysBusDevice, SysBusDeviceImpl}, vmstate::VMStateDescription, }; @@ -124,6 +125,12 @@ pub struct PL011State { pub migrate_clock: bool, } +// Some C users of this device embed its state struct into their own +// structs, so the size of the Rust version must not be any larger +// than the size of the C one. If this assert triggers you need to +// expand the padding_for_rust[] array in the C PL011State struct. +static_assert!(size_of::() <= size_of::()); + qom_isa!(PL011State : SysBusDevice, DeviceState, Object); #[repr(C)]