Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

3.8 KB
Raw Blame
// ©AngelaMos | 2026
// crash.rs

use crate::campaign::CampaignId;
use crate::report::SanitizerReport;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CrashId(pub i64);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Crash {
    campaign_id: CampaignId,
    input: Vec<u8>,
    sanitizer_report: String,
    stack_hash: Option<String>,
}

impl Crash {
    #[must_use]
    pub fn new(
        campaign_id: CampaignId,
        input: impl Into<Vec<u8>>,
        sanitizer_report: impl Into<String>,
        stack_hash: Option<String>,
    ) -> Self {
        Self {
            campaign_id,
            input: input.into(),
            sanitizer_report: sanitizer_report.into(),
            stack_hash,
        }
    }

    #[must_use]
    pub fn from_report(
        campaign_id: CampaignId,
        input: impl Into<Vec<u8>>,
        sanitizer_report: impl Into<String>,
    ) -> Self {
        let sanitizer_report = sanitizer_report.into();
        let stack_hash =
            SanitizerReport::parse(&sanitizer_report).and_then(|report| report.stack_hash());
        Self {
            campaign_id,
            input: input.into(),
            sanitizer_report,
            stack_hash,
        }
    }

    #[must_use]
    pub fn campaign_id(&self) -> CampaignId {
        self.campaign_id
    }

    #[must_use]
    pub fn input(&self) -> &[u8] {
        &self.input
    }

    #[must_use]
    pub fn sanitizer_report(&self) -> &str {
        &self.sanitizer_report
    }

    #[must_use]
    pub fn stack_hash(&self) -> Option<&str> {
        self.stack_hash.as_deref()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const KEYED_REPORT: &str = "\
==14==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000018 at pc 0x4a1
    #0 0x55a in png_handle_PLTE pngrutil.c:992:4
";

    const UNSYMBOLIZED_REPORT: &str = "\
==12==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000018 at pc 0x4a1
    #0 0x7f0c8a2 (/lib/x86_64-linux-gnu/libc.so.6+0x21b97)
";

    #[test]
    fn from_report_derives_the_key_instead_of_taking_one_on_trust() {
        let crash = Crash::from_report(CampaignId(1), b"input".to_vec(), KEYED_REPORT);
        let expected = SanitizerReport::parse(KEYED_REPORT)
            .and_then(|report| report.stack_hash())
            .unwrap();
        assert_eq!(crash.stack_hash(), Some(expected.as_str()));
    }

    #[test]
    fn from_report_refuses_a_key_the_current_filter_cannot_produce() {
        let crash = Crash::from_report(CampaignId(1), b"input".to_vec(), UNSYMBOLIZED_REPORT);
        assert_eq!(
            crash.stack_hash(),
            None,
            "every frame is unsymbolized, so the current algorithm refuses a key; a crash rehydrated from an older row must inherit that refusal rather than an obsolete key"
        );
    }

    #[test]
    fn from_report_on_an_unreproduced_crash_invents_nothing() {
        let crash = Crash::from_report(CampaignId(1), b"junk".to_vec(), "");
        assert_eq!(crash.stack_hash(), None);
        assert_eq!(crash.sanitizer_report(), "");
    }

    #[test]
    fn new_crash_exposes_all_fields() {
        let crash = Crash::new(
            CampaignId(7),
            b"\x89PNG\r\n".to_vec(),
            "ERROR: AddressSanitizer: heap-buffer-overflow",
            Some("deadbeef".to_owned()),
        );
        assert_eq!(crash.campaign_id(), CampaignId(7));
        assert_eq!(crash.input(), b"\x89PNG\r\n");
        assert_eq!(
            crash.sanitizer_report(),
            "ERROR: AddressSanitizer: heap-buffer-overflow"
        );
        assert_eq!(crash.stack_hash(), Some("deadbeef"));
    }

    #[test]
    fn unreproduced_crash_has_no_stack_hash() {
        let crash = Crash::new(CampaignId(1), b"junk".to_vec(), "", None);
        assert_eq!(crash.stack_hash(), None);
    }
}