From bb0e7e8929c9e23c0bb6ebebb5536ff376894a0d Mon Sep 17 00:00:00 2001 From: andrewjcg Date: Fri, 10 Nov 2023 17:26:32 -0500 Subject: [PATCH] Support facebook's cinder.3.8 Python fork (#631) * Capture build metadata in version struct Also include the build metada in the version struct. * Support facebook's cinder.3.8 Python fork Facebook's cinder.3.8 Python fork uses a different offset for the current tstate. Use the buildmetadata to detect and special case this (https://github.com/facebookincubator/cinder/blob/cinder/3.8/Include/patchlevel.h#L26). --- src/coredump.rs | 1 + src/python_bindings/mod.rs | 5 ++++- src/python_process_info.rs | 1 + src/version.rs | 46 +++++++++++++++++++++++++++++++------- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/coredump.rs b/src/coredump.rs index 51a66361..53940bdb 100644 --- a/src/coredump.rs +++ b/src/coredump.rs @@ -441,6 +441,7 @@ mod test { minor: 9, patch: 13, release_flags: "".to_owned(), + build_metadata: None, }; let python_core = PythonCoreDump { core, diff --git a/src/python_bindings/mod.rs b/src/python_bindings/mod.rs index 74a6ce89..420e3c9a 100644 --- a/src/python_bindings/mod.rs +++ b/src/python_bindings/mod.rs @@ -222,7 +222,10 @@ pub mod pyruntime { }, Version { major: 3, minor: 8, .. - } => Some(1368), + } => match version.build_metadata.as_deref() { + Some("cinder") => Some(1384), + _ => Some(1368), + }, Version { major: 3, minor: 9..=10, diff --git a/src/python_process_info.rs b/src/python_process_info.rs index 4c6210cb..dd802c67 100644 --- a/src/python_process_info.rs +++ b/src/python_process_info.rs @@ -318,6 +318,7 @@ where minor, patch: 0, release_flags: "".to_owned(), + build_metadata: None, }); } } diff --git a/src/version.rs b/src/version.rs index 27d8538b..946febc7 100644 --- a/src/version.rs +++ b/src/version.rs @@ -9,6 +9,7 @@ pub struct Version { pub minor: u64, pub patch: u64, pub release_flags: String, + pub build_metadata: Option, } impl Version { @@ -28,6 +29,11 @@ impl Version { let major = std::str::from_utf8(&cap[2])?.parse::()?; let minor = std::str::from_utf8(&cap[3])?.parse::()?; let patch = std::str::from_utf8(&cap[4])?.parse::()?; + let build_metadata = if let Some(s) = cap.get(7) { + Some(std::str::from_utf8(&s.as_bytes()[1..])?.to_owned()) + } else { + None + }; let version = std::str::from_utf8(&cap[0])?; info!("Found matching version string '{}'", version); @@ -47,6 +53,7 @@ impl Version { minor, patch, release_flags: release.to_owned(), + build_metadata, }); } Err(format_err!("failed to find version string")) @@ -59,7 +66,11 @@ impl std::fmt::Display for Version { f, "{}.{}.{}{}", self.major, self.minor, self.patch, self.release_flags - ) + )?; + if let Some(build_metadata) = &self.build_metadata { + write!(f, "+{}", build_metadata,)? + } + Ok(()) } } @@ -75,7 +86,8 @@ mod tests { major: 2, minor: 7, patch: 10, - release_flags: "".to_owned() + release_flags: "".to_owned(), + build_metadata: None, } ); @@ -89,7 +101,8 @@ mod tests { major: 3, minor: 6, patch: 3, - release_flags: "".to_owned() + release_flags: "".to_owned(), + build_metadata: None, } ); @@ -102,7 +115,8 @@ mod tests { major: 3, minor: 7, patch: 0, - release_flags: "rc1".to_owned() + release_flags: "rc1".to_owned(), + build_metadata: None, } ); @@ -115,7 +129,8 @@ mod tests { major: 3, minor: 10, patch: 0, - release_flags: "rc1".to_owned() + release_flags: "rc1".to_owned(), + build_metadata: None, } ); @@ -137,7 +152,8 @@ mod tests { major: 2, minor: 7, patch: 15, - release_flags: "".to_owned() + release_flags: "".to_owned(), + build_metadata: Some("".to_owned()), } ); @@ -148,7 +164,8 @@ mod tests { major: 2, minor: 7, patch: 10, - release_flags: "".to_owned() + release_flags: "".to_owned(), + build_metadata: Some("dcba".to_owned()), } ); @@ -159,7 +176,20 @@ mod tests { major: 2, minor: 7, patch: 10, - release_flags: "".to_owned() + release_flags: "".to_owned(), + build_metadata: Some("5-4.abcd".to_owned()), + } + ); + + let version = Version::scan_bytes(b"2.8.5+cinder (default)").unwrap(); + assert_eq!( + version, + Version { + major: 2, + minor: 8, + patch: 5, + release_flags: "".to_owned(), + build_metadata: Some("cinder".to_owned()), } ); }