mbox series

[v2,0/3] rust: Fix PL011State size mismatch assert

Message ID 20250321112523.1774131-1-peter.maydell@linaro.org
Headers show
Series rust: Fix PL011State size mismatch assert | expand

Message

Peter Maydell March 21, 2025, 11:25 a.m. UTC
Summary: v2 of fixing an assert due to mismatch of the
PL011 state struct size between the Rust and C impls.

Changes v1->v2:
 * in patch 3, adjust 'use' statements to maintain alpha order and to
   prefer 'size_of' and 'qemu_api::bindings::PL011State' over
   'mem::size_of' and 'bindings::PL011State'.

All patches are reviewed.

NOTE: I won't be around to do a v3 of this series next week,
I'm afraid, so if it needs any further tweaks I'd appreciate
if somebody else could pick it up and make those, since this is
a bug we need to fix for 10.0.


More detailed info, same as v1 cover letter, below:

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)

This series fixes that by the simple expedient of adding
a padding field to the end of the C struct to ensure that
it's big enough to also fit the Rust version of the device.

It also moves the failure from runtime to compiletime,
by adding a Rust compile-time assert that it hasn't made
the state bigger than the C one, so if we do this again
it should be caught before it gets into git. (We don't
need to do the same thing for the HPET device, because there
the HPETState is a private implementation detail of the C
code, not exposed to its users.)

thanks
-- PMM

Paolo Bonzini (1):
  rust: assertions: add static_assert

Peter Maydell (2):
  hw/char/pl011: Pad PL011State struct to same size as Rust impl
  rust: pl011: Check size of state struct at compile time

 include/hw/char/pl011.h          |  5 +++++
 rust/wrapper.h                   |  1 +
 rust/hw/char/pl011/src/device.rs |  9 ++++++++-
 rust/qemu-api/src/assertions.rs  | 22 ++++++++++++++++++++++
 4 files changed, 36 insertions(+), 1 deletion(-)