From patchwork Thu Mar 20 13:32:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 874972 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f4c:0:b0:38f:210b:807b with SMTP id cm12csp322685wrb; Thu, 20 Mar 2025 06:33:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV1BsRudsk9gFBaeQlHOm69Fhb/Ug/hWtG+rL2IETXyQ2peuFPYkemt00R72gN0mcbP715tMg==@linaro.org X-Google-Smtp-Source: AGHT+IGMH1cbZXQmkoklr98Kt4CkpSh9GtAIfjo6hCnqUhmAJZJWlk2yyW+tbqtkFxTEBsaaeQBp X-Received: by 2002:a05:620a:454e:b0:7c3:c9f4:522 with SMTP id af79cd13be357-7c5a8396ae6mr866825985a.14.1742477632655; Thu, 20 Mar 2025 06:33:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742477632; cv=none; d=google.com; s=arc-20240605; b=H7cpIcKCIJSY7aLCpfSCGmRBqJmnNr8faaBSH910Ft0AqT6XTAm9xTfW3yDFP4yi1e f332iKPj0+BRTC4FLs3ZWVCtTVo2VehArctZSceiMTk5iXRubJ6AnTsYwM4uljzuEzUH lMz/yUpqQJDY4oat7/O9IiDhCtO9yI2ktgFeBPaHsyP0ndVf4p3vN+51yR5D8Y5x85ys ADcm6hdxcXNwzwRcE8tq2yR1he9SyjISweVpqcOqCHyjtTAH+Ug+ZxkNGicQwFUOMv6B DJdU9bvjngprNGfkp/qksYNv2aJyh9VJM2XzRMhxf+wZnoBO0r3rpJvmvJAS4r9wi2FM 0WMg== 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=GZxg8BppI9WvT0Dj1sY+qDwOoI0QIu58PoW6AIBPva8=; fh=nMwgdI0F9BThzeO2EC8AA281W2EhsmfGgCO4oczsKw4=; b=RC79/WLhVomEiKaUvfuNGdqW5sZ6SJrR+liwkFiVbNS1gSHINs1ZX9l+VIefUY2qVe 9kaG2tNF1q+rmoB8KMmDeBEAAXXgcSpw7cdWvFzpcKM6iVXrQbVekIMxcEG5vArYsH7/ AMG55SojiyzKURC+Z4ZleUsIMhA8S2D71l6/n8Gt0rt1BvZtLGEVZSjzXahiNZU2Kk2/ fF5y1WwbJqs0fGnqq5dWhEJa/6pf6fMWVXR1RIvDmVT8EJbmYFWsHrdYL+EC0fMQ0dSV J351wsSKwDaQSwuvKkmS25aV/hrqZgaE6kTD+d+Rb3HZIhhgYzqdihlImPlhA7ybSoyv V8yQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XATm5mQa; 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-7c573b72f78si1621244085a.91.2025.03.20.06.33.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Mar 2025 06:33:52 -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=XATm5mQa; 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 1tvG1S-0001No-Kt; Thu, 20 Mar 2025 09:33:24 -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 1tvG16-0001Bd-N4 for qemu-devel@nongnu.org; Thu, 20 Mar 2025 09:33:04 -0400 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tvG13-0003R7-Qy for qemu-devel@nongnu.org; Thu, 20 Mar 2025 09:33:00 -0400 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-43cf0d787eeso8276245e9.3 for ; Thu, 20 Mar 2025 06:32:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1742477573; x=1743082373; 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=GZxg8BppI9WvT0Dj1sY+qDwOoI0QIu58PoW6AIBPva8=; b=XATm5mQaRfy2pBKpz6SW+Qo5FviYfzxhBvP4PIwT4NOJ4VFemdLH1SFxHkinoXAorq mnRq0dWvxeeCbEJkLEjZZWTlXKTEifyQFDseMko1+iYR236nAJkvydoNlVvxI0wKtiKR 05NwrMoEtHzc4i2Rcfo5FmhdbK22f0W0NeYg2gSg02OPvvehw4ibi9sOZjnvGf8r4g2k W4sM2wzrRwN9qK7hfGR/Sru+yTOULu7gY1v69cEp4RNhCsRExdpwY+epr7EfifGSXQC6 OhI43qRVrjD8JJ7C8rfUhoSmewOTAae4QsI+ERHROJlD5MzlVEkMik8UiuIR8wgLNMce SpsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742477573; x=1743082373; 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=GZxg8BppI9WvT0Dj1sY+qDwOoI0QIu58PoW6AIBPva8=; b=BMhgxoXbuv0//E7MVmhpjZY4DTe9HXKaNe0w4Q0v0R1iuZj/CiylWGdN3M3XKg8t1a L2srybB+0jAwJZ0WuNOih93O2Ait8j7skQx72XlZFqGXkoo3Xjskm8Sg1EcpprFqJk3S cXRoH6UwQ9fleivekTm5HfOD+B3HTRzZsnAnFTMYYbCnaHlrbZTXWDC3BjudGYEX5FRU A77GI3HGwwZDJNRMcpfO3EfkHVaJOY5KyVIUGGZJHOQQpmW9uJVqDHOVTcIqXZlvU6pY v5QZN2uEnCU82zyrFrjSvzK+lS4iP3Ekr4U0MAz0WxLtyNRLxbdbY24KoKmx9Cb+naKx Y53g== X-Forwarded-Encrypted: i=1; AJvYcCUwD2d6aoop0BD/RyZzoKzPar7kqo7m37MBAA4Gr5FPG3bEQo/igJfeGuaJfXi2E5NEZA8C/mFTgtQJ@nongnu.org X-Gm-Message-State: AOJu0YwjKvVIf300VG7+M9tacHUFOEoNMfildNfRHdc+IH63N2FGZGTO +YA9ewkx95c63AGliDDn8H5fLIi1CByaHJ2vL1Q0o7Abz0CK/WUaBJ3arl1zabM= X-Gm-Gg: ASbGnct8vKcAUe/mgrk5w7Pg4168NPfkyNAioIKP/3UqUy5Qt/5qVBasTY+3Y76rv7+ ZQP1LuUHqzy0naru2H4OWAH7cMJzKjjqQFQgt270BtYP+cDdvPq0W5TfuJMbPpd8v9+moipt143 iDS7KZOQqPN/HOQgjLGpntdhd9dw6VHCKQr3uv3Y7+7rc+ELuwArgsZfLuG7H/CtFBfi35HPCd+ FH9R4MgWRWFmEqLqa34VIYVXlOyzrY6bdFiWOc0STG3zkWS/nkJvcRkeJLeIefLYgKkcvnD8qP+ nmxMDORSRT0CeHqYpPZKmScoxWwXY+/Qx33tY2G0jb7+43dpy4Y= X-Received: by 2002:a05:6000:1882:b0:391:47f2:8d90 with SMTP id ffacd0b85a97d-399795a8f16mr4008254f8f.20.1742477572535; Thu, 20 Mar 2025 06:32:52 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d43fdeba1sm48804525e9.32.2025.03.20.06.32.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Mar 2025 06:32:51 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, Paolo Bonzini Subject: [PATCH 1/3] rust: assertions: add static_assert Date: Thu, 20 Mar 2025 13:32:46 +0000 Message-ID: <20250320133248.1679485-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250320133248.1679485-1-peter.maydell@linaro.org> References: <20250320133248.1679485-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::331; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x331.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 Supersedes: <20250320113356.799412-1-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini Signed-off-by: Peter Maydell Reviewed-by: Zhao Liu --- 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 Thu Mar 20 13:32:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 874974 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f4c:0:b0:38f:210b:807b with SMTP id cm12csp323059wrb; Thu, 20 Mar 2025 06:34:36 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW0JaD8Ue68leoAdhuPwUQaJH9skvsYywnoMCdKO8v03PTDCwv72YZVZv7/ucsES9hHkMMI4g==@linaro.org X-Google-Smtp-Source: AGHT+IGHbSAEAtmrQqPpWYl/a3OL13MjlhFzUOVKVpZwft34eBVYuj29P/5aQngS+OBFOj/TtLx3 X-Received: by 2002:a05:620a:c51:b0:7c5:3b8d:9f2f with SMTP id af79cd13be357-7c5b0527fd1mr508642785a.17.1742477676685; Thu, 20 Mar 2025 06:34:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742477676; cv=none; d=google.com; s=arc-20240605; b=SrI9U5krJaoGsszIarCXqOES6mljy2f1OT7rl5j2v2dam5+7cYbVdISdUkakDN59Sc eMYxpZUWF0YC7ZuCHn8DPQgpa79j+hO4usMl+Qx3cwXUNgx9J53EAu3egHt/OMajTOGC mo/97SJJN0zSml9SVw+sbl6XrOZ9+IhpsPQTp/guxXSwmFyY2QlNa/rKNzNT90TYB4JU L/ggErrxr+GaJ1GcL2n8GF503Rh7qndtgLC5HRVPJ+/OCtxMmAV96tfMgCZylrzSjty/ qalyBrSzEpGgmhSHqakf3zxo+Ku1+Kt1Fw/RgFtqeug29E3yEa969/MSlZHjPlkgp8lI TlqA== 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=KBdmphdV8HlBXhupNUNKIMGYjH2p/y/FMhqozqFd7Iw=; fh=SQpoZUMUUnDQA3ZwMzftC2T7HPNxluRXn1EAy4/slZs=; b=ZBHkgcyq/R5q5MsdRFiZQoZEqQ7y41fXsZo9vrswTz6G6+sW91/ocQapVaTg0zGmac emQo9rETUhHRztkR+InqdYdNp7V1GEqOJWMRk0D3Zt1v0oOt3ioGZFXsX3KezzJpjvO1 hEoJd4br8PHx4eIyu8swCz8Ky/ZjX9YCB9fYG07dixv4/zbb7c0hbcMyu1qyU1+QuE3F g0qTYZlnTVOSuDNxIKOny5VazjqBOagfxZei9FbjTp6ajmCokvdbwiTbF4/4TXT7IYpo X2s/y+WOmU4if+02Glmc7p1G4sqp3ncttpy4rWEfHb0x+smctoGp/9fOxixCMNLyo/oN w3KA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m5mYhvYG; 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-7c573cae6b8si1595598185a.201.2025.03.20.06.34.36 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Mar 2025 06:34:36 -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=m5mYhvYG; 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 1tvG1M-0001Iw-UQ; Thu, 20 Mar 2025 09:33:16 -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 1tvG16-0001Be-Q2 for qemu-devel@nongnu.org; Thu, 20 Mar 2025 09:33:04 -0400 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tvG13-0003RE-TO for qemu-devel@nongnu.org; Thu, 20 Mar 2025 09:33:00 -0400 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-4393dc02b78so4926935e9.3 for ; Thu, 20 Mar 2025 06:32:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1742477574; x=1743082374; 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=KBdmphdV8HlBXhupNUNKIMGYjH2p/y/FMhqozqFd7Iw=; b=m5mYhvYGjhjkUwzSf+0O7yUYdc+MKm2VL1LokWXPQUMDu48C5Cfzcy25YWE4wspv7i 5DDCPoSs+rQKtbnYjF2Ttqmk/wtmYlFF3FZ4Sq75Q3vA6MWNS+QYb7adqAC28fcaaMXA 52SZmPzAYOVcsIlCvfDaoHCgADPI0ajzhksExH2NGiqyd9vPQjMkSmlvI2m7oWbtLwTC OPrlF/qc7WJar4YdlQwykOxTFxu4iUAC/wm6Wzq95CBqC+yG6hhvELVxUS5OIMEAbM97 YecTps5hr1veGTrKXYDr+AGzlYqWp5ImwO5tl1NnOTVfU+I6ytVeOu2QHq8v+s5GloKz zeCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742477574; x=1743082374; 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=KBdmphdV8HlBXhupNUNKIMGYjH2p/y/FMhqozqFd7Iw=; b=PJfkRkf5OIngYBWSTMsvee+D3LxCgS1ALYAlgfhTud66u/PZN/oyTNCBTm9C+5TAZa EbykBJHh/ADYpY7LvKztRTZWMjoqRW3ZWJcZHmbQEh9+WU14VSG4raUY+v9F7rwji0ge 2IA/fkj5qFkbl+iNN26kAHhLqtzPGC8F2HhJOp5lqsbW89gFuo60Wvl8UR03oUZGRYjD J+yHQ6WJGpW0iZXCvdAg91AH1sZ9Z1J1XQnnC/sCwxE4cJcYe/4iD+zD1r7+OBBhzMSX RvdRPvnZDctPtiIXgohNu+YCE/tjcXVzjP0t80r53NP21N3qNFX0ZR+djFuqV65m0TMN 6Mtw== X-Forwarded-Encrypted: i=1; AJvYcCVsLirJOFMEPdDyVd3ZJQ+Ue0QHxcVkaCp5swQbfDqiD7v42x72YutM/Uz0w17RwUMRjs0kWZxfoeBd@nongnu.org X-Gm-Message-State: AOJu0YzBfbEtQulb/LiTx2AaSZc3U9Bl3BvGRZhLpfNi6rooO1GRcrwj NB6IAJ3uxsyvBk9oqSoF2/kaILRaY5/jLLG+w8VB4KXch2w0WyRpqxJdYVhpASM= X-Gm-Gg: ASbGncv/v/c8ym4T4dqJ/Dr3aX66j/AskEwPFdZ3SF4e1tk1G0Um4Je2RczC88kr4Ea TEy/U/KFrGJ+GP94sF3kD2PLBf79IVZwmnbjTsbaYcpsLIUmwb2xr0UAu8gnPmqC2v3whGhFHI/ Y4aQgd5CgMfl+L2HvMCzlT6H1ZrxYxQdKr012s/jYCPuqPAwOhJcMRI8TyVOubzrtz4rXHD+xyJ zAsFG4hmpWmSytm4hoJXa2aie15EFeS1mxq36y0yf8ME2LYLxMDhKWOF6RRvBmFGZ7ofVTcr6hg 9k8GmQqFRaavixsekNSdurMkbR4kd92VOZRhoLmGu8Iwaujx1+Hr+zCC6wFMOg== X-Received: by 2002:a05:600c:19d2:b0:43c:f050:fee8 with SMTP id 5b1f17b1804b1-43d437e1703mr55734325e9.20.1742477573862; Thu, 20 Mar 2025 06:32:53 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d43fdeba1sm48804525e9.32.2025.03.20.06.32.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Mar 2025 06:32:52 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, Paolo Bonzini Subject: [PATCH 2/3] hw/char/pl011: Pad PL011State struct to same size as Rust impl Date: Thu, 20 Mar 2025 13:32:47 +0000 Message-ID: <20250320133248.1679485-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250320133248.1679485-1-peter.maydell@linaro.org> References: <20250320133248.1679485-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32b; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x32b.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") Signed-off-by: Peter Maydell Reviewed-by: Zhao Liu --- 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 Thu Mar 20 13:32:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 874973 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f4c:0:b0:38f:210b:807b with SMTP id cm12csp322856wrb; Thu, 20 Mar 2025 06:34:13 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVq1lor2KkjPfUqpUN1jQdvuay4OsENRz3LULZqgZsHNFI5JF/UewtZdYNxbEAbiMP/nD1vYg==@linaro.org X-Google-Smtp-Source: AGHT+IEO38nzganH/P6ccvV1GeJFZ6deZ5kM+Ly5co9XtUV1EWybF9jKA+CDjKnHkrH/LHItYlkv X-Received: by 2002:ad4:4ee4:0:b0:6e8:f4a1:68a4 with SMTP id 6a1803df08f44-6eb29445db3mr93167406d6.39.1742477653215; Thu, 20 Mar 2025 06:34:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742477653; cv=none; d=google.com; s=arc-20240605; b=QqaAQ26xuLI/1Yo/JXTkUAUUxMUpjfSbM9AYFqWHoUQ49Mm6borTqD2uydg1uhm8+F ESsYatCTUEWR03gjDL6eMaFtxQE7FUy0RtLoVhRB0dakuguTQI6Sixaj48rDhHvovI+L bDqTVYj7oasZK78ZwQzGrs8wUBE6svEewfbJghckXPz6/2fSivwZxiQgkRZYYJRXugBI eOQMA597wDjNEimnNeyMVAkzsvEngx0E/YewBrFEjcmkM7hyYda1ZnF7ILZIkAv3EXFd QnfBtHnDVCelibQ7NSHY79+HlGfEFIo6hIKpEBVD8kfaGPPRx+JScLVOYbXuKEzPgKMn NBjA== 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=EcxOk5H8Mf+hivpUxneXluEV3ozaYLM9gcAGfs012Is=; fh=2tA+xUhn/sJhuhg9pDQvn1hbYZcXJYPYNAfdyiqnvbA=; b=g+p4PIK7slJB08zUx7/PIAnfuxzcWRy0NZ9+YDqAuvqx6QZPBo/DBDCid62nVZgNgh PE/28pd1KFRo4hA0oV5fy+xPsn03V6MdgNxIkSrnhIuQbHFU0wynZpMuHY1EVMkc8PkQ 9k659LhG7+eUgg3MUSgF+87PmU0ZIuAIvyT9zV6rJljwGfrsLDkUNRaHsbO3nmxE22JN vrxpXwIOVqKptATS/+O08lrqI0Htpz7NwNSGB7Jm0R2PjhUdiFwV6taF4sVewQuvN4Lj IVX/oBsFyl1RJHqKmZP8i+fBDjiD6erEVvqnyKh33mb7la43706jmhbociys0fTyjory bEuQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=uLGwHfsH; 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-6eade1ddeabsi164031606d6.4.2025.03.20.06.34.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Mar 2025 06:34:13 -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=uLGwHfsH; 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 1tvG1S-0001Nh-JJ; Thu, 20 Mar 2025 09:33:22 -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 1tvG1A-0001FT-Od for qemu-devel@nongnu.org; Thu, 20 Mar 2025 09:33:08 -0400 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tvG14-0003Rd-75 for qemu-devel@nongnu.org; Thu, 20 Mar 2025 09:33:02 -0400 Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-390f5f48eafso425422f8f.0 for ; Thu, 20 Mar 2025 06:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1742477575; x=1743082375; 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=EcxOk5H8Mf+hivpUxneXluEV3ozaYLM9gcAGfs012Is=; b=uLGwHfsHBX6vpRpMcVBh5IyII1WY0kglkOaLW3Rlw2vqW/F180laGzhsEkAtKRMXsM OXtF0IFqMA+3SuS2O83C/w5wKTXtAx6EIFxWWCMHx/9Ul9MEec/jl5vcKjYX2Uou7lKx x+pRF/5UJXP6NscZ7ziysxngbDrUV403fwpaDDiJ2rAasLrBNduRHWpw4m/ljhrQorTr MZBdoJgITFTmT8dLZymCcbz6WqlOfyB1kt7rkihxTXAo4u4LPf2+Qon/u/BShBIeb/RS k3dEPeJTmvnheOYUJjS4gqKb/7/F9IMnFIBn6lY1WC4edXAlMgHA1QULGAnhm7/5kCpl eX+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742477575; x=1743082375; 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=EcxOk5H8Mf+hivpUxneXluEV3ozaYLM9gcAGfs012Is=; b=ZTWBkBLEp+VUQSLhYz+NuHKhd4B8wbTvyErt0Na5gFOQlLYZ0vjCO7+pKUXQ5rwsOx wv4/X4dROhqBgnc+aEpWDhHR+kcNirwwqsRrNFri7KnNMW24gnm6bUHY8nSN/mVlp9fW MbTGgHMkUNvmnDu0Shd+XZxm8yz0QWldrbrioUxVQ5jVFWcDJyL2RVOGh4E1r/eOaMDz yiQMxWqL5dfkmPl0lHYFrttGx2s9v5jwrr1n8aaD6aqjWxpSucoEYTqOySzK5vPlTA0k LbslEaES0ZbRi0fs3gBdIslhEGtHOxrTiEwzAYURzq4Xh9YFlNck5stUm3BOqvy0oBIe XCdQ== X-Forwarded-Encrypted: i=1; AJvYcCWI7/7Y38w9Aim6NLRXMvms2FKEcQTtFo/Xnu72dfcPekqlqt3sqMFYPhX1xXuV4+MOZ+QRgqDEdtzP@nongnu.org X-Gm-Message-State: AOJu0YwejgvQZP06tA/Y8UOFH2IV1tIn92KQ96Gn2NEL1K4Ks/gCMxf2 ID8ATEzTYlSbK0BmV87xaKyaRrRO/JX0vcQxFvN2lYlihgud56cyLbCIqQL2O9geY4IY8DB0wPK k X-Gm-Gg: ASbGnctZXTDpzBsHQ5WE05iE87Br+si3bLjcN8S6DSM0ou7WwpUeDRo4Z3H5peEs5Jj 2ztW4/pfc0ok0KIELDXLQp5A6dk4VSeea92tgY0ueu7tP9CAUBW+zT6/DUaFtZYIKd2JKUCLUBw mRS2cPgJwMWbmV9wYOZABCJlovYsX6YYHEGjtPGT5SDuMfekfRDHbMuFndohTfzeIhHhMvFFYZR CgBgSWSdXzyge2GQo2jSZr5DF62YkxtyjVX6IJoUFktXppoP/wvSnpKmoDx4xM4QH9vjQ0u5B9b EBgR8OsfsEcLaw4QDQYinZyQac5kh2S3ORm4XpGv5mMVkRqB2BRP/EXw2CAyLg== X-Received: by 2002:a5d:5f87:0:b0:391:43cb:43e6 with SMTP id ffacd0b85a97d-39973b439cemr6758857f8f.51.1742477574964; Thu, 20 Mar 2025 06:32:54 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d43fdeba1sm48804525e9.32.2025.03.20.06.32.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Mar 2025 06:32:54 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, Paolo Bonzini Subject: [PATCH 3/3] rust: pl011: Check size of state struct at compile time Date: Thu, 20 Mar 2025 13:32:48 +0000 Message-ID: <20250320133248.1679485-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250320133248.1679485-1-peter.maydell@linaro.org> References: <20250320133248.1679485-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::431; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x431.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 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. Signed-off-by: Peter Maydell Reviewed-by: Zhao Liu --- rust/wrapper.h | 1 + rust/hw/char/pl011/src/device.rs | 10 +++++++++- 2 files changed, 10 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..f9b1c307289 100644 --- a/rust/hw/char/pl011/src/device.rs +++ b/rust/hw/char/pl011/src/device.rs @@ -2,10 +2,12 @@ // 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, ptr::addr_of_mut}; use qemu_api::{ + bindings, chardev::{CharBackend, Chardev, Event}, + static_assert, impl_vmstate_forward, irq::{IRQState, InterruptSource}, memory::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder}, @@ -124,6 +126,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!(mem::size_of::() <= mem::size_of::()); + qom_isa!(PL011State : SysBusDevice, DeviceState, Object); #[repr(C)]