From 1930896fd1e9cf6f1b1ff35f275f18ae5604ee5b Mon Sep 17 00:00:00 2001 From: maks Date: Fri, 27 Mar 2026 11:48:08 +0100 Subject: [PATCH 1/2] [tests] Add regression tests for #2445 --- zerocopy-derive/tests/struct_no_cell.rs | 27 ++++++++++++++++++++++++ zerocopy-derive/tests/struct_to_bytes.rs | 27 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/zerocopy-derive/tests/struct_no_cell.rs b/zerocopy-derive/tests/struct_no_cell.rs index 2b1857cced..182af84e85 100644 --- a/zerocopy-derive/tests/struct_no_cell.rs +++ b/zerocopy-derive/tests/struct_no_cell.rs @@ -103,3 +103,30 @@ where T: 'a + 'b + imp::Immutable; util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::Immutable); + +// Regression test for #2445: Fuchsia has `repr(C)` packet types with a large +// computed array field, and they should still be `Immutable`. +const FX_LOG_MAX_DATAGRAM_LEN: usize = 2032; +const FX_LOG_METADATA_SIZE: usize = 32; + +#[derive(imp::Immutable)] +#[zerocopy(crate = "zerocopy_renamed")] +#[repr(C)] +struct FxLogMetadata { + pid: u64, + tid: u64, + time: i64, + severity: i32, + dropped_logs: u32, +} + +#[derive(imp::Immutable)] +#[zerocopy(crate = "zerocopy_renamed")] +#[repr(C)] +struct FxLogPacket { + metadata: FxLogMetadata, + data: [u8; FX_LOG_MAX_DATAGRAM_LEN - FX_LOG_METADATA_SIZE], +} + +util_assert_impl_all!(FxLogMetadata: imp::Immutable); +util_assert_impl_all!(FxLogPacket: imp::Immutable); diff --git a/zerocopy-derive/tests/struct_to_bytes.rs b/zerocopy-derive/tests/struct_to_bytes.rs index 40fa2e5a91..bab8b21a73 100644 --- a/zerocopy-derive/tests/struct_to_bytes.rs +++ b/zerocopy-derive/tests/struct_to_bytes.rs @@ -227,3 +227,30 @@ pub struct IndexEntry { util_assert_impl_all!(IndexEntry<0>: imp::IntoBytes); util_assert_impl_all!(IndexEntry<1>: imp::IntoBytes); + +// Regression test for #2445: Fuchsia has `repr(C)` packet types with a large +// computed array field, and they should still be serializable with zerocopy. +const FX_LOG_MAX_DATAGRAM_LEN: usize = 2032; +const FX_LOG_METADATA_SIZE: usize = 32; + +#[derive(imp::IntoBytes)] +#[zerocopy(crate = "zerocopy_renamed")] +#[repr(C)] +struct FxLogMetadata { + pid: u64, + tid: u64, + time: i64, + severity: i32, + dropped_logs: u32, +} + +#[derive(imp::IntoBytes)] +#[zerocopy(crate = "zerocopy_renamed")] +#[repr(C)] +struct FxLogPacket { + metadata: FxLogMetadata, + data: [u8; FX_LOG_MAX_DATAGRAM_LEN - FX_LOG_METADATA_SIZE], +} + +util_assert_impl_all!(FxLogMetadata: imp::IntoBytes); +util_assert_impl_all!(FxLogPacket: imp::IntoBytes); From 941b49e1e6778fcf69d1ca1dc63f8ff2ddddfa5e Mon Sep 17 00:00:00 2001 From: maks Date: Fri, 27 Mar 2026 16:06:15 +0100 Subject: [PATCH 2/2] test(derive): use size_of for Fuchsia log metadata size --- zerocopy-derive/tests/struct_no_cell.rs | 3 ++- zerocopy-derive/tests/struct_to_bytes.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/zerocopy-derive/tests/struct_no_cell.rs b/zerocopy-derive/tests/struct_no_cell.rs index 182af84e85..22a8f70943 100644 --- a/zerocopy-derive/tests/struct_no_cell.rs +++ b/zerocopy-derive/tests/struct_no_cell.rs @@ -107,7 +107,6 @@ util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::Immutable); // Regression test for #2445: Fuchsia has `repr(C)` packet types with a large // computed array field, and they should still be `Immutable`. const FX_LOG_MAX_DATAGRAM_LEN: usize = 2032; -const FX_LOG_METADATA_SIZE: usize = 32; #[derive(imp::Immutable)] #[zerocopy(crate = "zerocopy_renamed")] @@ -120,6 +119,8 @@ struct FxLogMetadata { dropped_logs: u32, } +const FX_LOG_METADATA_SIZE: usize = imp::core::mem::size_of::(); + #[derive(imp::Immutable)] #[zerocopy(crate = "zerocopy_renamed")] #[repr(C)] diff --git a/zerocopy-derive/tests/struct_to_bytes.rs b/zerocopy-derive/tests/struct_to_bytes.rs index bab8b21a73..eca3df1068 100644 --- a/zerocopy-derive/tests/struct_to_bytes.rs +++ b/zerocopy-derive/tests/struct_to_bytes.rs @@ -231,7 +231,6 @@ util_assert_impl_all!(IndexEntry<1>: imp::IntoBytes); // Regression test for #2445: Fuchsia has `repr(C)` packet types with a large // computed array field, and they should still be serializable with zerocopy. const FX_LOG_MAX_DATAGRAM_LEN: usize = 2032; -const FX_LOG_METADATA_SIZE: usize = 32; #[derive(imp::IntoBytes)] #[zerocopy(crate = "zerocopy_renamed")] @@ -244,6 +243,8 @@ struct FxLogMetadata { dropped_logs: u32, } +const FX_LOG_METADATA_SIZE: usize = imp::core::mem::size_of::(); + #[derive(imp::IntoBytes)] #[zerocopy(crate = "zerocopy_renamed")] #[repr(C)]