diff mbox series

[2/3] hw/char/pl011: Pad PL011State struct to same size as Rust impl

Message ID 20250320133248.1679485-3-peter.maydell@linaro.org
State Superseded
Headers show
Series rust: Fix PL011State size mismatch assert | expand

Commit Message

Peter Maydell March 20, 2025, 1:32 p.m. UTC
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 <peter.maydell@linaro.org>
---
 include/hw/char/pl011.h | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Zhao Liu March 20, 2025, 3:28 p.m. UTC | #1
On Thu, Mar 20, 2025 at 01:32:47PM +0000, Peter Maydell wrote:
> Date: Thu, 20 Mar 2025 13:32:47 +0000
> From: Peter Maydell <peter.maydell@linaro.org>
> Subject: [PATCH 2/3] hw/char/pl011: Pad PL011State struct to same size as
>  Rust impl
> X-Mailer: git-send-email 2.43.0
> 
> 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 <peter.maydell@linaro.org>
> ---
>  include/hw/char/pl011.h | 5 +++++
>  1 file changed, 5 insertions(+)

LGTM, BqlRefCell<> has extra fields to make BqlRefCell<T> bigger than T,

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
diff mbox series

Patch

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);