From f40debb8d4d93ea09f769284dab0b42eb6c6781e Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Mon, 2 Oct 2023 15:58:03 +0000 Subject: [PATCH] validate struct layout --- plugins/zenoh-plugin-trait/src/lib.rs | 39 ++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/plugins/zenoh-plugin-trait/src/lib.rs b/plugins/zenoh-plugin-trait/src/lib.rs index 486aa8fbe7..380241b649 100644 --- a/plugins/zenoh-plugin-trait/src/lib.rs +++ b/plugins/zenoh-plugin-trait/src/lib.rs @@ -23,6 +23,24 @@ pub mod vtable; use zenoh_result::ZResult; +#[repr(C)] +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct StructLayout { + pub name: &'static str, + pub size: usize, + pub alignment: usize, + pub fields_count: usize, + pub fields: &'static [StructField], +} + +#[repr(C)] +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct StructField { + pub name: &'static str, + pub offset: usize, + pub size: usize, +} + #[repr(C)] #[derive(Debug, PartialEq, Eq, Clone)] pub struct Compatibility { @@ -31,6 +49,7 @@ pub struct Compatibility { patch: u64, stable: bool, commit: &'static str, + structs: &'static [StructLayout], } const RELEASE_AND_COMMIT: (&str, &str) = zenoh_macros::rustc_version_release!(); impl Compatibility { @@ -48,14 +67,28 @@ impl Compatibility { patch: split.next().unwrap().parse().unwrap(), stable, commit, + structs: &[], }) } pub fn are_compatible(a: &Self, b: &Self) -> bool { + // Compare compiler versions if a.stable && b.stable { - a.major == b.major && a.minor == b.minor && a.patch == b.patch - } else { - a == b + if a.major != b.major || a.minor != b.minor || a.patch != b.patch { + return false; + } + } else if a != b { + return false; + } + // Compare declared structs layouts. The count and poisions of structs must match + if a.structs.len() != b.structs.len() { + return false; + } + for (a, b) in a.structs.iter().zip(b.structs.iter()) { + if a != b { + return false; + } } + true } }