From 1609eea70a6e7c0f60dfa8f15c7deb474907f51e Mon Sep 17 00:00:00 2001 From: Justus Adam Date: Wed, 18 Dec 2024 11:54:36 -0500 Subject: [PATCH] Implement serialization magic --- crates/paralegal-spdg/Cargo.toml | 3 ++ crates/paralegal-spdg/build.rs | 71 ++++++++++++++++++++++++++++++++ crates/paralegal-spdg/src/ser.rs | 23 ++++++++++- 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 crates/paralegal-spdg/build.rs diff --git a/crates/paralegal-spdg/Cargo.toml b/crates/paralegal-spdg/Cargo.toml index 2642af828b..3b542ed572 100644 --- a/crates/paralegal-spdg/Cargo.toml +++ b/crates/paralegal-spdg/Cargo.toml @@ -30,3 +30,6 @@ dot = { git = "https://github.com/JustusAdam/dot-rust", rev = "ff2b42ceda98c639c serde_json = { version = "1" } bincode = { version = "1.1.3", optional = true } anyhow = { workspace = true } + +[build-dependencies] +anyhow = "1" diff --git a/crates/paralegal-spdg/build.rs b/crates/paralegal-spdg/build.rs new file mode 100644 index 0000000000..157716d614 --- /dev/null +++ b/crates/paralegal-spdg/build.rs @@ -0,0 +1,71 @@ +use anyhow::Context; +use anyhow::Result; +use std::collections::hash_map::DefaultHasher; +use std::env; +use std::fs; +use std::hash::{Hash, Hasher}; +use std::path::{Path, PathBuf}; +use std::time::SystemTime; + +fn rustup_toolchain_path() -> PathBuf { + let rustup_home = env::var("RUSTUP_HOME").unwrap(); + let rustup_tc = env::var("RUSTUP_TOOLCHAIN").unwrap(); + [&rustup_home, "toolchains", &rustup_tc] + .into_iter() + .collect() +} + +fn get_rustup_lib_path() -> PathBuf { + let mut rustup_lib = rustup_toolchain_path(); + rustup_lib.push("lib"); + rustup_lib +} + +fn visit_dirs(dir: &Path, hasher: &mut DefaultHasher) -> Result<()> { + if !dir.is_dir() { + return Ok(()); + } + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + if path.is_dir() { + visit_dirs(&path, hasher)?; + } else if path.extension().map_or(false, |ext| ext == "rs") { + // Get the modification time + let metadata = entry.metadata()?; + let modified = metadata.modified()?; + + // Convert SystemTime to duration since UNIX_EPOCH + let duration = modified.duration_since(SystemTime::UNIX_EPOCH)?; + duration.as_secs().hash(hasher); + // Tell Cargo to rerun if this source file changes + println!("cargo:rerun-if-changed={}", path.display()); + } + } + Ok(()) +} + +fn calculate_source_hash() -> u64 { + let mut hasher = DefaultHasher::new(); + + // Start from the src directory + visit_dirs(Path::new("src"), &mut hasher) + .with_context(|| "Calculating source hash") + .unwrap(); + + hasher.finish() +} + +fn main() { + // Calculate the hash of modification times + let magic = calculate_source_hash(); + + // Emit the hash as an environment variable + println!("cargo:rustc-env=SER_MAGIC={:0}", magic); + + // Original linux-specific code + if cfg!(target_os = "linux") { + let rustup_lib = get_rustup_lib_path(); + println!("cargo:rustc-link-search=native={}", rustup_lib.display()); + } +} diff --git a/crates/paralegal-spdg/src/ser.rs b/crates/paralegal-spdg/src/ser.rs index 59ff56be98..55ea3e3ec0 100644 --- a/crates/paralegal-spdg/src/ser.rs +++ b/crates/paralegal-spdg/src/ser.rs @@ -3,7 +3,11 @@ //! use anyhow::{Context, Ok, Result}; use cfg_if::cfg_if; -use std::{fs::File, path::Path}; +use std::{ + fs::File, + io::{Read, Write}, + path::Path, +}; use crate::ProgramDescription; @@ -15,11 +19,17 @@ cfg_if! { } } +fn ser_magic() -> u64 { + const SER_MAGIC: &str = env!("SER_MAGIC"); + SER_MAGIC.parse().unwrap() +} + impl ProgramDescription { /// Write `self` using the configured serialization format pub fn canonical_write(&self, path: impl AsRef) -> Result<()> { let path = path.as_ref(); let mut out_file = File::create(path)?; + out_file.write_all(&ser_magic().to_le_bytes())?; cfg_if! { if #[cfg(feature = "binenc")] { let write = bincode::serialize_into( @@ -47,7 +57,16 @@ impl ProgramDescription { /// Read `self` using the configured serialization format pub fn canonical_read(path: impl AsRef) -> Result { let path = path.as_ref(); - let in_file = File::open(path)?; + let mut in_file = File::open(path)?; + let magic = { + let mut buf = [0u8; 8]; + in_file.read_exact(&mut buf).context("Reading magic")?; + u64::from_le_bytes(buf) + }; + let ser_magic = ser_magic(); + if magic != ser_magic { + anyhow::bail!("Magic number mismatch: Expected {ser_magic:x}, got {magic:x}. Likely this application was compiled against a different version of the paralegal-spdg library then used by the flow analyzer."); + } cfg_if! { if #[cfg(feature = "binenc")] { let read = bincode::deserialize_from(