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 đŸ‘‡

feat(lisdex): M2 auto-build + instrumentation, gate builds 6 real parsers

CarterPerez-devaee866f05958
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.lock b/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.lock
index c257645d..306defef 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.lock
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.lock
@@ -228,6 +228,12 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
+[[package]]
+name = "itoa"
+version = "1.0.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
+
[[package]]
name = "libc"
version = "0.2.189"
@@ -250,8 +256,21 @@ name = "lisdex"
version = "0.0.0"
dependencies = [
"clap",
+ "lisdex-build",
"lisdex-core",
"lisdex-fuzz",
+ "lisdex-ingest",
+]
+
+[[package]]
+name = "lisdex-build"
+version = "0.0.0"
+dependencies = [
+ "lisdex-core",
+ "lisdex-ingest",
+ "serde",
+ "serde_json",
+ "thiserror",
]
[[package]]
@@ -271,6 +290,20 @@ dependencies = [
"thiserror",
]
+[[package]]
+name = "lisdex-ingest"
+version = "0.0.0"
+dependencies = [
+ "lisdex-core",
+ "thiserror",
+]
+
+[[package]]
+name = "memchr"
+version = "2.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
+
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -321,6 +354,49 @@ dependencies = [
"smallvec",
]
+[[package]]
+name = "serde"
+version = "1.0.229"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
+dependencies = [
+ "serde_core",
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_core"
+version = "1.0.229"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.229"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 3.0.3",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.151"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
+dependencies = [
+ "itoa",
+ "memchr",
+ "serde",
+ "serde_core",
+ "zmij",
+]
+
[[package]]
name = "sha2"
version = "0.10.9"
@@ -456,3 +532,9 @@ dependencies = [
"quote",
"syn 2.0.119",
]
+
+[[package]]
+name = "zmij"
+version = "1.0.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.toml b/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.toml
index ab41b909..ee3ce817 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.toml
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/Cargo.toml
@@ -16,8 +16,12 @@ thiserror = "2"
rusqlite = { version = "0.32", features = ["bundled"] }
sha2 = "0.10"
clap = { version = "4", features = ["derive"] }
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
lisdex-core = { path = "crates/lisdex-core" }
lisdex-fuzz = { path = "crates/lisdex-fuzz" }
+lisdex-ingest = { path = "crates/lisdex-ingest" }
+lisdex-build = { path = "crates/lisdex-build" }
[workspace.lints.rust]
unsafe_code = "deny"
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/Dockerfile.dev b/PROJECTS/advanced/zero-day-vulnerability-scanner/Dockerfile.dev
index c69e63b0..a3146743 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/Dockerfile.dev
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/Dockerfile.dev
@@ -15,17 +15,29 @@ ENV PATH=/usr/local/cargo/bin:/usr/lib/llvm-20/bin:$PATH
ENV MAGMA_ROOT=/opt/magma
ENV MAGMA_WORK=/opt/magma-work
ENV AFL_PATH=/usr/local/lib/afl
-ENV AFL_DRIVER=/usr/local/lib/afl/libAFLDriver.a
+ENV LISDEX_AFL_DRIVER=/usr/local/lib/afl/libAFLDriver.a
RUN apt-get update && apt-get install -y --no-install-recommends \
autoconf \
automake \
+ autoconf-archive \
libtool \
+ gettext \
+ gtk-doc-tools \
pkg-config \
zlib1g-dev \
+ libreadline-dev \
+ liblzma-dev \
+ tcl \
libclang-dev \
bison \
flex \
+ gperf \
+ nasm \
+ bear \
+ cmake \
+ meson \
+ ninja-build \
curl \
ca-certificates \
git \
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/Cargo.toml b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/Cargo.toml
new file mode 100644
index 00000000..cc0d0d74
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/Cargo.toml
@@ -0,0 +1,20 @@
+# ©AngelaMos | 2026
+# Cargo.toml
+
+[package]
+name = "lisdex-build"
+version = "0.0.0"
+edition.workspace = true
+rust-version.workspace = true
+license.workspace = true
+authors.workspace = true
+
+[lints]
+workspace = true
+
+[dependencies]
+thiserror.workspace = true
+serde.workspace = true
+serde_json.workspace = true
+lisdex-core.workspace = true
+lisdex-ingest.workspace = true
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/artifact.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/artifact.rs
new file mode 100644
index 00000000..30043101
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/artifact.rs
@@ -0,0 +1,441 @@
+// ©AngelaMos | 2026
+// artifact.rs
+
+use std::io::{Read, Seek, SeekFrom};
+use std::path::{Path, PathBuf};
+
+pub const MAX_DEPTH: usize = 12;
+pub const MAX_INSPECTED_MEMBERS: usize = 8192;
+
+const ELF_MAGIC: [u8; 4] = [0x7f, b'E', b'L', b'F'];
+const BITCODE_MAGIC: [u8; 4] = [0x42, 0x43, 0xc0, 0xde];
+const AR_MAGIC: [u8; 8] = *b"!<arch>\n";
+const AR_HEADER_LEN: usize = 60;
+const AR_SIZE_OFFSET: usize = 48;
+const AR_SIZE_END: usize = 58;
+const AR_NAME_END: usize = 16;
+const SPECIAL_MEMBERS: [&str; 4] = ["/", "//", "/SYM64/", "__.SYMDEF"];
+const STATIC_LIB_EXT: &str = "a";
+const SHARED_LIB_EXT: &str = "so";
+const SHARED_LIB_INFIX: &str = ".so.";
+const EXCLUDED_DIRS: [&str; 7] = [
+ "CMakeFiles",
+ "meson-private",
+ "meson-logs",
+ "meson-info",
+ ".git",
+ "Testing",
+ "_deps",
+];
+const EXCLUDED_PREFIXES: [&str; 2] = ["conftest", "cmTC_"];
+
+#[derive(Debug, thiserror::Error)]
+pub enum ArtifactError {
+ #[error("io error at {path}: {source}")]
+ Io {
+ path: PathBuf,
+ source: std::io::Error,
+ },
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum ArtifactKind {
+ StaticLib,
+ SharedLib,
+ Executable,
+}
+
+impl ArtifactKind {
+ #[must_use]
+ pub fn as_str(self) -> &'static str {
+ match self {
+ ArtifactKind::StaticLib => "static-lib",
+ ArtifactKind::SharedLib => "shared-lib",
+ ArtifactKind::Executable => "executable",
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Artifact {
+ pub path: PathBuf,
+ pub kind: ArtifactKind,
+}
+
+fn is_excluded_dir(name: &str) -> bool {
+ EXCLUDED_DIRS.contains(&name)
+}
+
+fn is_excluded_file(name: &str) -> bool {
+ EXCLUDED_PREFIXES
+ .iter()
+ .any(|prefix| name.starts_with(prefix))
+}
+
+fn is_elf(path: &Path) -> bool {
+ let Ok(mut file) = std::fs::File::open(path) else {
+ return false;
+ };
+ let mut magic = [0u8; ELF_MAGIC.len()];
+ file.read_exact(&mut magic).is_ok() && magic == ELF_MAGIC
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct MemberKinds {
+ pub bitcode: usize,
+ pub native: usize,
+}
+
+impl MemberKinds {
+ #[must_use]
+ pub fn merge(self, other: MemberKinds) -> MemberKinds {
+ MemberKinds {
+ bitcode: self.bitcode + other.bitcode,
+ native: self.native + other.native,
+ }
+ }
+
+ #[must_use]
+ pub fn holds_bitcode(self) -> bool {
+ self.bitcode > 0
+ }
+
+ #[must_use]
+ pub fn is_mixed(self) -> bool {
+ self.bitcode > 0 && self.native > 0
+ }
+}
+
+fn classify_archive(file: &mut std::fs::File) -> std::io::Result<MemberKinds> {
+ let mut kinds = MemberKinds::default();
+ let mut offset = AR_MAGIC.len() as u64;
+ for _ in 0..MAX_INSPECTED_MEMBERS {
+ file.seek(SeekFrom::Start(offset))?;
+ let mut header = [0u8; AR_HEADER_LEN];
+ if file.read_exact(&mut header).is_err() {
+ return Ok(kinds);
+ }
+ let size: u64 = String::from_utf8_lossy(&header[AR_SIZE_OFFSET..AR_SIZE_END])
+ .trim()
+ .parse()
+ .unwrap_or(0);
+ let name = String::from_utf8_lossy(&header[..AR_NAME_END]);
+ let name = name.trim();
+ let mut magic = [0u8; BITCODE_MAGIC.len()];
+ let read = file.read(&mut magic)?;
+ if read == BITCODE_MAGIC.len() && !SPECIAL_MEMBERS.contains(&name) {
+ if magic == BITCODE_MAGIC {
+ kinds.bitcode += 1;
+ } else if magic == ELF_MAGIC {
+ kinds.native += 1;
+ }
+ }
+ offset += AR_HEADER_LEN as u64 + size + (size % 2);
+ }
+ Ok(kinds)
+}
+
+#[must_use]
+pub fn member_kinds(path: &Path) -> MemberKinds {
+ let Ok(mut file) = std::fs::File::open(path) else {
+ return MemberKinds::default();
+ };
+ let mut head = [0u8; AR_MAGIC.len()];
+ let Ok(read) = file.read(&mut head) else {
+ return MemberKinds::default();
+ };
+ if read >= BITCODE_MAGIC.len() && head[..BITCODE_MAGIC.len()] == BITCODE_MAGIC {
+ return MemberKinds {
+ bitcode: 1,
+ native: 0,
+ };
+ }
+ if read != AR_MAGIC.len() || head != AR_MAGIC {
+ return MemberKinds::default();
+ }
+ classify_archive(&mut file).unwrap_or_default()
+}
+
+#[must_use]
+pub fn holds_llvm_bitcode(path: &Path) -> bool {
+ member_kinds(path).holds_bitcode()
+}
+
+fn is_executable_file(path: &Path) -> bool {
+ use std::os::unix::fs::PermissionsExt;
+ std::fs::metadata(path).is_ok_and(|meta| meta.permissions().mode() & 0o111 != 0)
+}
+
+#[must_use]
+pub fn classify(path: &Path) -> Option<ArtifactKind> {
+ let name = path.file_name()?.to_string_lossy().into_owned();
+ if is_excluded_file(&name) {
+ return None;
+ }
+ if path.extension().is_some_and(|ext| ext == STATIC_LIB_EXT) {
+ return Some(ArtifactKind::StaticLib);
+ }
+ if path.extension().is_some_and(|ext| ext == SHARED_LIB_EXT) || name.contains(SHARED_LIB_INFIX)
+ {
+ return Some(ArtifactKind::SharedLib);
+ }
+ if is_executable_file(path) && is_elf(path) {
+ return Some(ArtifactKind::Executable);
+ }
+ None
+}
+
+fn walk(root: &Path, depth: usize, found: &mut Vec<Artifact>) -> Result<(), ArtifactError> {
+ if depth > MAX_DEPTH || !root.is_dir() {
+ return Ok(());
+ }
+ let entries = std::fs::read_dir(root).map_err(|source| ArtifactError::Io {
+ path: root.to_path_buf(),
+ source,
+ })?;
+ for entry in entries {
+ let entry = entry.map_err(|source| ArtifactError::Io {
+ path: root.to_path_buf(),
+ source,
+ })?;
+ let path = entry.path();
+ let file_type = entry.file_type().map_err(|source| ArtifactError::Io {
+ path: path.clone(),
+ source,
+ })?;
+ if file_type.is_symlink() {
+ continue;
+ }
+ if file_type.is_dir() {
+ if is_excluded_dir(&entry.file_name().to_string_lossy()) {
+ continue;
+ }
+ walk(&path, depth + 1, found)?;
+ continue;
+ }
+ if let Some(kind) = classify(&path) {
+ found.push(Artifact { path, kind });
+ }
+ }
+ Ok(())
+}
+
+pub fn discover(roots: &[PathBuf]) -> Result<Vec<Artifact>, ArtifactError> {
+ let mut found = Vec::new();
+ for root in roots {
+ walk(root, 0, &mut found)?;
+ }
+ found.sort_by(|left, right| left.path.cmp(&right.path));
+ found.dedup_by(|left, right| left.path == right.path);
+ Ok(found)
+}
+
+#[must_use]
+pub fn libraries(found: &[Artifact]) -> Vec<&Artifact> {
+ found
+ .iter()
+ .filter(|artifact| artifact.kind != ArtifactKind::Executable)
+ .collect()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ struct Tree {
+ root: PathBuf,
+ }
+
+ impl Tree {
+ fn new(name: &str) -> Self {
+ let root =
+ std::env::temp_dir().join(format!("lisdex-artifact-{}-{name}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&root);
+ std::fs::create_dir_all(&root).unwrap();
+ Self { root }
+ }
+
+ fn file(self, name: &str, bytes: &[u8]) -> Self {
+ let path = self.root.join(name);
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(&path, bytes).unwrap();
+ self
+ }
+
+ fn elf(self, name: &str) -> Self {
+ use std::os::unix::fs::PermissionsExt;
+
+ let path = self.root.join(name);
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(&path, ELF_MAGIC).unwrap();
+ let mut perms = std::fs::metadata(&path).unwrap().permissions();
+ perms.set_mode(0o755);
+ std::fs::set_permissions(&path, perms).unwrap();
+ self
+ }
+
+ fn discover(&self) -> Vec<Artifact> {
+ discover(std::slice::from_ref(&self.root)).unwrap()
+ }
+ }
+
+ #[test]
+ fn static_libraries_are_found() {
+ let found = Tree::new("static")
+ .file("libpng16.a", b"!<arch>")
+ .discover();
+ assert_eq!(found.len(), 1);
+ assert_eq!(found[0].kind, ArtifactKind::StaticLib);
+ }
+
+ #[test]
+ fn versioned_shared_libraries_are_found() {
+ let found = Tree::new("shared")
+ .file("libtiff.so.6.0.2", b"\x7fELF")
+ .file("libz.so", b"\x7fELF")
+ .discover();
+ assert_eq!(found.len(), 2);
+ assert!(found.iter().all(|a| a.kind == ArtifactKind::SharedLib));
+ }
+
+ #[test]
+ fn elf_executables_are_found() {
+ let found = Tree::new("exe").elf("sqlite3").discover();
+ assert_eq!(found.len(), 1);
+ assert_eq!(found[0].kind, ArtifactKind::Executable);
+ }
+
+ #[test]
+ fn shell_scripts_are_not_artifacts() {
+ let found = Tree::new("script").elf("run.sh").discover();
+ let found = found
+ .into_iter()
+ .filter(|a| a.path.ends_with("run.sh"))
+ .count();
+ assert_eq!(found, 1);
+ let plain = Tree::new("script2")
+ .file("libtool", b"#!/bin/sh\n")
+ .discover();
+ assert!(plain.is_empty());
+ }
+
+ #[test]
+ fn cmake_compiler_probe_binaries_are_excluded() {
+ let found = Tree::new("cmakefiles")
+ .elf("CMakeFiles/3.28.3/CompilerIdC/a.out")
+ .file("CMakeFiles/probe.a", b"!<arch>")
+ .discover();
+ assert!(
+ found.is_empty(),
+ "instrumented compiler probes would falsely satisfy verification"
+ );
+ }
+
+ #[test]
+ fn autotools_conftest_leftovers_are_excluded() {
+ let found = Tree::new("conftest").elf("conftest").discover();
+ assert!(found.is_empty());
+ }
+
+ #[test]
+ fn meson_internal_directories_are_excluded() {
+ let found = Tree::new("meson")
+ .file("meson-private/sanitycheckc.a", b"!<arch>")
+ .file("src/libfoo.a", b"!<arch>")
+ .discover();
+ assert_eq!(found.len(), 1);
+ assert!(found[0].path.ends_with("src/libfoo.a"));
+ }
+
+ #[test]
+ fn nested_libtool_output_is_found() {
+ let found = Tree::new("libtool")
+ .file(".libs/libpng16.a", b"!<arch>")
+ .discover();
+ assert_eq!(found.len(), 1);
+ }
+
+ #[test]
+ fn discovery_is_sorted_and_deduplicated() {
+ let tree = Tree::new("dedup")
+ .file("b/libb.a", b"!<arch>")
+ .file("a/liba.a", b"!<arch>");
+ let found = discover(&[tree.root.clone(), tree.root.clone()]).unwrap();
+ assert_eq!(found.len(), 2);
+ assert!(found[0].path.ends_with("a/liba.a"));
+ }
+
+ #[test]
+ fn object_files_are_not_reported_as_artifacts() {
+ let found = Tree::new("objects").file("png.o", b"\x7fELF").discover();
+ assert!(found.is_empty());
+ }
+
+ #[test]
+ fn libraries_filters_out_executables() {
+ let tree = Tree::new("filter").file("libfoo.a", b"!<arch>").elf("tool");
+ let found = tree.discover();
+ assert_eq!(found.len(), 2);
+ assert_eq!(libraries(&found).len(), 1);
+ }
+
+ #[test]
+ fn a_bare_bitcode_object_is_recognised() {
+ let tree = Tree::new("bitcode-object").file("png.bc", &BITCODE_MAGIC);
+ assert!(holds_llvm_bitcode(&tree.root.join("png.bc")));
+ }
+
+ #[test]
+ fn an_archive_of_bitcode_members_is_recognised() {
+ let mut archive = Vec::new();
+ archive.extend_from_slice(&AR_MAGIC);
+ let payload = [BITCODE_MAGIC.as_slice(), b"rest"].concat();
+ let mut header = [b' '; AR_HEADER_LEN];
+ header[..8].copy_from_slice(b"png.o/ ");
+ let size = format!("{}", payload.len());
+ header[AR_SIZE_OFFSET..AR_SIZE_OFFSET + size.len()].copy_from_slice(size.as_bytes());
+ header[58..60].copy_from_slice(b"`\n");
+ archive.extend_from_slice(&header);
+ archive.extend_from_slice(&payload);
+
+ let tree = Tree::new("bitcode-archive").file("libpng.a", &archive);
+ assert!(holds_llvm_bitcode(&tree.root.join("libpng.a")));
+ }
+
+ #[test]
+ fn an_archive_of_native_objects_is_not_bitcode() {
+ let mut archive = Vec::new();
+ archive.extend_from_slice(&AR_MAGIC);
+ let payload = [ELF_MAGIC.as_slice(), b"rest"].concat();
+ let mut header = [b' '; AR_HEADER_LEN];
+ header[..8].copy_from_slice(b"png.o/ ");
+ let size = format!("{}", payload.len());
+ header[AR_SIZE_OFFSET..AR_SIZE_OFFSET + size.len()].copy_from_slice(size.as_bytes());
+ header[58..60].copy_from_slice(b"`\n");
+ archive.extend_from_slice(&header);
+ archive.extend_from_slice(&payload);
+
+ let tree = Tree::new("native-archive").file("libpng.a", &archive);
+ assert!(!holds_llvm_bitcode(&tree.root.join("libpng.a")));
+ }
+
+ #[test]
+ fn a_text_file_is_not_bitcode() {
+ let tree = Tree::new("text").file("README.md", b"# not an object\n");
+ assert!(!holds_llvm_bitcode(&tree.root.join("README.md")));
+ }
+
+ #[test]
+ fn a_missing_file_is_not_bitcode() {
+ assert!(!holds_llvm_bitcode(Path::new(
+ "/lisdex/definitely/missing.a"
+ )));
+ }
+
+ #[test]
+ fn a_missing_directory_yields_no_artifacts() {
+ let missing = std::env::temp_dir().join("lisdex-artifact-missing");
+ let _ = std::fs::remove_dir_all(&missing);
+ assert!(discover(&[missing]).unwrap().is_empty());
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/instrument.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/instrument.rs
new file mode 100644
index 00000000..52af8102
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/instrument.rs
@@ -0,0 +1,352 @@
+// ©AngelaMos | 2026
+// instrument.rs
+
+pub const AFL_CC: &str = "afl-clang-lto";
+pub const AFL_CXX: &str = "afl-clang-lto++";
+pub const PLAIN_CC: &str = "clang";
+pub const PLAIN_CXX: &str = "clang++";
+pub const LLVM_AR: &str = "llvm-ar";
+pub const LLVM_RANLIB: &str = "llvm-ranlib";
+pub const LLVM_NM: &str = "llvm-nm";
+pub const LLVM_AS: &str = "llvm-as";
+
+pub const ENV_CC: &str = "CC";
+pub const ENV_CXX: &str = "CXX";
+pub const ENV_AR: &str = "AR";
+pub const ENV_RANLIB: &str = "RANLIB";
+pub const ENV_NM: &str = "NM";
+pub const ENV_AS: &str = "AS";
+pub const ENV_CFLAGS: &str = "CFLAGS";
+pub const ENV_CXXFLAGS: &str = "CXXFLAGS";
+pub const ENV_LDFLAGS: &str = "LDFLAGS";
+pub const ENV_AFL_USE_ASAN: &str = "AFL_USE_ASAN";
+pub const ENV_AFL_USE_UBSAN: &str = "AFL_USE_UBSAN";
+pub const ENV_AFL_USE_MSAN: &str = "AFL_USE_MSAN";
+pub const ENV_AFL_LLVM_CMPLOG: &str = "AFL_LLVM_CMPLOG";
+pub const ENV_AFL_QUIET: &str = "AFL_QUIET";
+pub const ENV_AFL_NOOPT: &str = "AFL_NOOPT";
+pub const ENV_ASAN_OPTIONS: &str = "ASAN_OPTIONS";
+pub const ENV_UBSAN_OPTIONS: &str = "UBSAN_OPTIONS";
+pub const ENV_ENABLED: &str = "1";
+
+pub const BUILD_TIME_ASAN_OPTIONS: &str = "detect_leaks=0:detect_odr_violation=0";
+pub const BUILD_TIME_UBSAN_OPTIONS: &str = "print_stacktrace=0";
+
+pub const DEFAULT_CFLAGS: [&str; 3] = ["-g", "-O2", "-fno-omit-frame-pointer"];
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub enum SanitizerSet {
+ #[default]
+ AddressUndefined,
+ Memory,
+ Off,
+}
+
+impl SanitizerSet {
+ #[must_use]
+ pub fn as_str(self) -> &'static str {
+ match self {
+ SanitizerSet::AddressUndefined => "address+undefined",
+ SanitizerSet::Memory => "memory",
+ SanitizerSet::Off => "off",
+ }
+ }
+
+ #[must_use]
+ pub fn envs(self) -> Vec<(String, String)> {
+ match self {
+ SanitizerSet::AddressUndefined => vec![
+ (ENV_AFL_USE_ASAN.to_owned(), ENV_ENABLED.to_owned()),
+ (ENV_AFL_USE_UBSAN.to_owned(), ENV_ENABLED.to_owned()),
+ ],
+ SanitizerSet::Memory => vec![(ENV_AFL_USE_MSAN.to_owned(), ENV_ENABLED.to_owned())],
+ SanitizerSet::Off => Vec::new(),
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Toolchain {
+ pub cc: String,
+ pub cxx: String,
+ pub ar: String,
+ pub ranlib: String,
+ pub nm: String,
+ pub assembler: String,
+}
+
+impl Default for Toolchain {
+ fn default() -> Self {
+ Self {
+ cc: AFL_CC.to_owned(),
+ cxx: AFL_CXX.to_owned(),
+ ar: LLVM_AR.to_owned(),
+ ranlib: LLVM_RANLIB.to_owned(),
+ nm: LLVM_NM.to_owned(),
+ assembler: LLVM_AS.to_owned(),
+ }
+ }
+}
+
+impl Toolchain {
+ #[must_use]
+ pub fn plain() -> Self {
+ Self {
+ cc: PLAIN_CC.to_owned(),
+ cxx: PLAIN_CXX.to_owned(),
+ ..Self::default()
+ }
+ }
+
+ #[must_use]
+ pub fn with_compilers(mut self, cc: impl Into<String>, cxx: impl Into<String>) -> Self {
+ self.cc = cc.into();
+ self.cxx = cxx.into();
+ self
+ }
+
+ #[must_use]
+ pub fn envs(&self) -> Vec<(String, String)> {
+ vec![
+ (ENV_CC.to_owned(), self.cc.clone()),
+ (ENV_CXX.to_owned(), self.cxx.clone()),
+ (ENV_AR.to_owned(), self.ar.clone()),
+ (ENV_RANLIB.to_owned(), self.ranlib.clone()),
+ (ENV_NM.to_owned(), self.nm.clone()),
+ (ENV_AS.to_owned(), self.assembler.clone()),
+ ]
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Instrumentation {
+ pub sanitizers: SanitizerSet,
+ pub cmplog: bool,
+ pub toolchain: Toolchain,
+ pub cflags: Vec<String>,
+ pub ldflags: Vec<String>,
+}
+
+impl Default for Instrumentation {
+ fn default() -> Self {
+ Self {
+ sanitizers: SanitizerSet::default(),
+ cmplog: false,
+ toolchain: Toolchain::default(),
+ cflags: DEFAULT_CFLAGS
+ .iter()
+ .map(|flag| (*flag).to_owned())
+ .collect(),
+ ldflags: Vec::new(),
+ }
+ }
+}
+
+impl Instrumentation {
+ #[must_use]
+ pub fn instrumented() -> Self {
+ Self::default()
+ }
+
+ #[must_use]
+ pub fn uninstrumented() -> Self {
+ Self {
+ sanitizers: SanitizerSet::Off,
+ cmplog: false,
+ toolchain: Toolchain::plain(),
+ ..Self::default()
+ }
+ }
+
+ #[must_use]
+ pub fn with_cmplog(mut self, cmplog: bool) -> Self {
+ self.cmplog = cmplog;
+ self
+ }
+
+ #[must_use]
+ pub fn with_sanitizers(mut self, sanitizers: SanitizerSet) -> Self {
+ self.sanitizers = sanitizers;
+ self
+ }
+
+ #[must_use]
+ pub fn with_toolchain(mut self, toolchain: Toolchain) -> Self {
+ self.toolchain = toolchain;
+ self
+ }
+
+ #[must_use]
+ pub fn with_extra_cflags<I, S>(mut self, flags: I) -> Self
+ where
+ I: IntoIterator<Item = S>,
+ S: Into<String>,
+ {
+ self.cflags.extend(flags.into_iter().map(Into::into));
+ self
+ }
+
+ #[must_use]
+ pub fn is_instrumented(&self) -> bool {
+ self.sanitizers != SanitizerSet::Off
+ }
+
+ #[must_use]
+ pub fn cflags_string(&self) -> String {
+ self.cflags.join(" ")
+ }
+
+ #[must_use]
+ pub fn ldflags_string(&self) -> String {
+ self.ldflags.join(" ")
+ }
+
+ #[must_use]
+ pub fn envs(&self) -> Vec<(String, String)> {
+ let mut envs = self.toolchain.envs();
+ envs.push((ENV_CFLAGS.to_owned(), self.cflags_string()));
+ envs.push((ENV_CXXFLAGS.to_owned(), self.cflags_string()));
+ envs.push((ENV_LDFLAGS.to_owned(), self.ldflags_string()));
+ envs.extend(self.sanitizers.envs());
+ if self.cmplog {
+ envs.push((ENV_AFL_LLVM_CMPLOG.to_owned(), ENV_ENABLED.to_owned()));
+ }
+ envs.push((ENV_AFL_QUIET.to_owned(), ENV_ENABLED.to_owned()));
+ envs.push((
+ ENV_ASAN_OPTIONS.to_owned(),
+ BUILD_TIME_ASAN_OPTIONS.to_owned(),
+ ));
+ envs.push((
+ ENV_UBSAN_OPTIONS.to_owned(),
+ BUILD_TIME_UBSAN_OPTIONS.to_owned(),
+ ));
+ envs
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn value(envs: &[(String, String)], key: &str) -> Option<String> {
+ envs.iter()
+ .find(|(name, _)| name == key)
+ .map(|(_, value)| value.clone())
+ }
+
+ #[test]
+ fn default_instrumentation_uses_the_lto_compiler() {
+ let envs = Instrumentation::instrumented().envs();
+ assert_eq!(value(&envs, ENV_CC).as_deref(), Some(AFL_CC));
+ assert_eq!(value(&envs, ENV_CXX).as_deref(), Some(AFL_CXX));
+ }
+
+ #[test]
+ fn lto_requires_the_llvm_binutils_drop_ins() {
+ let envs = Instrumentation::instrumented().envs();
+ assert_eq!(value(&envs, ENV_AR).as_deref(), Some(LLVM_AR));
+ assert_eq!(value(&envs, ENV_RANLIB).as_deref(), Some(LLVM_RANLIB));
+ assert_eq!(value(&envs, ENV_AS).as_deref(), Some(LLVM_AS));
+ assert_eq!(value(&envs, ENV_NM).as_deref(), Some(LLVM_NM));
+ }
+
+ #[test]
+ fn the_linker_is_left_to_the_compiler_driver() {
+ let envs = Instrumentation::instrumented().envs();
+ assert!(envs.iter().all(|(key, _)| key != "LD"));
+ }
+
+ #[test]
+ fn address_and_undefined_share_one_build() {
+ let envs = Instrumentation::instrumented().envs();
+ assert_eq!(value(&envs, ENV_AFL_USE_ASAN).as_deref(), Some(ENV_ENABLED));
+ assert_eq!(
+ value(&envs, ENV_AFL_USE_UBSAN).as_deref(),
+ Some(ENV_ENABLED)
+ );
+ assert_eq!(value(&envs, ENV_AFL_USE_MSAN), None);
+ }
+
+ #[test]
+ fn memory_sanitizer_never_shares_a_build_with_address() {
+ let envs = Instrumentation::instrumented()
+ .with_sanitizers(SanitizerSet::Memory)
+ .envs();
+ assert_eq!(value(&envs, ENV_AFL_USE_MSAN).as_deref(), Some(ENV_ENABLED));
+ assert_eq!(value(&envs, ENV_AFL_USE_ASAN), None);
+ assert_eq!(value(&envs, ENV_AFL_USE_UBSAN), None);
+ }
+
+ #[test]
+ fn cmplog_is_opt_in() {
+ let plain = Instrumentation::instrumented().envs();
+ assert_eq!(value(&plain, ENV_AFL_LLVM_CMPLOG), None);
+ let cmplog = Instrumentation::instrumented().with_cmplog(true).envs();
+ assert_eq!(
+ value(&cmplog, ENV_AFL_LLVM_CMPLOG).as_deref(),
+ Some(ENV_ENABLED)
+ );
+ }
+
+ #[test]
+ fn leak_detection_is_disabled_while_building_so_configure_probes_do_not_fail() {
+ let envs = Instrumentation::instrumented().envs();
+ let options = value(&envs, ENV_ASAN_OPTIONS).unwrap();
+ assert!(options.contains("detect_leaks=0"));
+ }
+
+ #[test]
+ fn default_flags_keep_frames_and_debug_info() {
+ let flags = Instrumentation::instrumented().cflags_string();
+ assert!(flags.contains("-g"));
+ assert!(flags.contains("-fno-omit-frame-pointer"));
+ }
+
+ #[test]
+ fn default_flags_never_define_ndebug_because_assertions_are_free_oracles() {
+ let flags = Instrumentation::instrumented().cflags_string();
+ assert!(!flags.contains("NDEBUG"));
+ }
+
+ #[test]
+ fn extra_cflags_are_appended_after_the_defaults() {
+ let instrumentation =
+ Instrumentation::instrumented().with_extra_cflags(["-DMAGMA_ENABLE_CANARIES"]);
+ let flags = instrumentation.cflags_string();
+ assert!(flags.ends_with("-DMAGMA_ENABLE_CANARIES"));
+ assert!(flags.starts_with("-g"));
+ }
+
+ #[test]
+ fn c_and_cxx_flags_stay_in_lockstep() {
+ let envs = Instrumentation::instrumented()
+ .with_extra_cflags(["-DX=1"])
+ .envs();
+ assert_eq!(value(&envs, ENV_CFLAGS), value(&envs, ENV_CXXFLAGS));
+ }
+
+ #[test]
+ fn the_uninstrumented_pass_uses_plain_clang_and_no_sanitizers() {
+ let instrumentation = Instrumentation::uninstrumented();
+ assert!(!instrumentation.is_instrumented());
+ let envs = instrumentation.envs();
+ assert_eq!(value(&envs, ENV_CC).as_deref(), Some(PLAIN_CC));
+ assert_eq!(value(&envs, ENV_AFL_USE_ASAN), None);
+ }
+
+ #[test]
+ fn a_shadow_toolchain_replaces_only_the_compilers() {
+ let envs = Instrumentation::instrumented()
+ .with_toolchain(Toolchain::default().with_compilers("/shadow/cc", "/shadow/c++"))
+ .envs();
+ assert_eq!(value(&envs, ENV_CC).as_deref(), Some("/shadow/cc"));
+ assert_eq!(value(&envs, ENV_AR).as_deref(), Some(LLVM_AR));
+ }
+
+ #[test]
+ fn sanitizer_sets_have_stable_names() {
+ assert_eq!(SanitizerSet::AddressUndefined.as_str(), "address+undefined");
+ assert_eq!(SanitizerSet::Memory.as_str(), "memory");
+ assert_eq!(SanitizerSet::Off.as_str(), "off");
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/ladder.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/ladder.rs
new file mode 100644
index 00000000..d4edeeff
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/ladder.rs
@@ -0,0 +1,1028 @@
+// ©AngelaMos | 2026
+// ladder.rs
+
+use std::path::{Path, PathBuf};
+use std::time::Duration;
+
+use lisdex_core::process::{self, CommandSpec, DEFAULT_STEP_TIMEOUT_SECS};
+use lisdex_ingest::detect::{BuildSystem, Detection};
+
+use crate::artifact::{self, Artifact};
+use crate::instrument::Instrumentation;
+use crate::layout::BuildLayout;
+use crate::plan::{self, BuildPlan, Generator, PlanOptions, STEP_COMPILE};
+use crate::replay::{self, COMPILE_COMMANDS};
+use crate::shadow::{self, ENV_PATH};
+use crate::verify::{self, Requirements, SymbolCounts};
+
+pub const BEAR_BIN: &str = "bear";
+pub const LOG_EXTENSION: &str = "log";
+
+const PREPASS_VARIANT_SUFFIX: &str = "-prepass";
+const LABEL_PREPASS: &str = "prepass";
+const LABEL_INSTRUMENTED: &str = "instrumented";
+const LABEL_CAPTURE: &str = "capture";
+const STEP_CLEAN: &str = "clean";
+
+#[derive(Debug, thiserror::Error)]
+pub enum BuildError {
+ #[error("io error at {path}: {source}")]
+ Io {
+ path: PathBuf,
+ source: std::io::Error,
+ },
+ #[error("planning error: {0}")]
+ Plan(#[from] plan::PlanError),
+ #[error("process error: {0}")]
+ Process(#[from] process::ProcessError),
+ #[error("shadow error: {0}")]
+ Shadow(#[from] shadow::ShadowError),
+ #[error("replay error: {0}")]
+ Replay(#[from] replay::ReplayError),
+ #[error("artifact error: {0}")]
+ Artifact(#[from] artifact::ArtifactError),
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Tier {
+ FlagInjection,
+ CompilerShadow,
+ GeneratedSourcePrepass,
+ CompileCommandsReplay,
+}
+
+pub const TIERS: [Tier; 4] = [
+ Tier::FlagInjection,
+ Tier::CompilerShadow,
+ Tier::GeneratedSourcePrepass,
+ Tier::CompileCommandsReplay,
+];
+
+impl Tier {
+ #[must_use]
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Tier::FlagInjection => "flag-injection",
+ Tier::CompilerShadow => "compiler-shadow",
+ Tier::GeneratedSourcePrepass => "generated-source-prepass",
+ Tier::CompileCommandsReplay => "compile-commands-replay",
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct StepOutcome {
+ pub name: String,
+ pub command_line: String,
+ pub success: bool,
+ pub timed_out: bool,
+ pub log: Option<PathBuf>,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Attempt {
+ pub tier: Tier,
+ pub system: BuildSystem,
+ pub build_dir: PathBuf,
+ pub steps: Vec<StepOutcome>,
+ pub artifacts: Vec<Artifact>,
+ pub counts: SymbolCounts,
+ pub lto_bitcode: bool,
+ pub shortfalls: Vec<String>,
+ pub failure: Option<String>,
+}
+
+impl Attempt {
+ #[must_use]
+ pub fn succeeded(&self) -> bool {
+ self.failure.is_none() && self.shortfalls.is_empty() && !self.artifacts.is_empty()
+ }
+
+ #[must_use]
+ pub fn summary(&self) -> String {
+ let reason = self.failure.clone().unwrap_or_else(|| {
+ if self.artifacts.is_empty() {
+ "build produced no libraries or executables".to_owned()
+ } else {
+ self.shortfalls.join("; ")
+ }
+ });
+ format!(
+ "{} via {}: {reason}",
+ self.system.as_str(),
+ self.tier.as_str()
+ )
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct BuildOutcome {
+ pub source: PathBuf,
+ pub system: Option<BuildSystem>,
+ pub tier: Option<Tier>,
+ pub build_dir: Option<PathBuf>,
+ pub artifacts: Vec<Artifact>,
+ pub counts: SymbolCounts,
+ pub lto_bitcode: bool,
+ pub attempts: Vec<Attempt>,
+ pub degradation: Vec<String>,
+}
+
+impl BuildOutcome {
+ #[must_use]
+ pub fn succeeded(&self) -> bool {
+ self.tier.is_some()
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct BuildOptions {
+ pub variant: String,
+ pub jobs: usize,
+ pub requirements: Requirements,
+ pub step_timeout: Duration,
+ pub tiers: Vec<Tier>,
+}
+
+impl BuildOptions {
+ #[must_use]
+ pub fn new(variant: impl Into<String>, jobs: usize) -> Self {
+ Self {
+ variant: variant.into(),
+ jobs,
+ requirements: Requirements::library(),
+ step_timeout: Duration::from_secs(DEFAULT_STEP_TIMEOUT_SECS),
+ tiers: TIERS.to_vec(),
+ }
+ }
+
+ #[must_use]
+ pub fn with_requirements(mut self, requirements: Requirements) -> Self {
+ self.requirements = requirements;
+ self
+ }
+
+ #[must_use]
+ pub fn with_tiers(mut self, tiers: Vec<Tier>) -> Self {
+ self.tiers = tiers;
+ self
+ }
+}
+
+#[must_use]
+pub fn bear_wrapped(spec: &CommandSpec, database: &Path) -> CommandSpec {
+ let mut wrapped = CommandSpec::new(BEAR_BIN)
+ .arg("--output")
+ .path_arg(database)
+ .arg("--")
+ .arg(spec.program.clone())
+ .args(spec.args.clone())
+ .envs(spec.envs.clone());
+ wrapped.cwd.clone_from(&spec.cwd);
+ wrapped
+}
+
+fn log_path(
+ layout: &BuildLayout,
+ tier: Tier,
+ system: BuildSystem,
+ label: &str,
+ step: &str,
+) -> PathBuf {
+ layout.logs_dir().join(format!(
+ "{}-{}-{label}-{step}.{LOG_EXTENSION}",
+ tier.as_str(),
+ system.as_str()
+ ))
+}
+
+fn prepare_build_dir(
+ layout: &BuildLayout,
+ built: &BuildPlan,
+ timeout: Duration,
+) -> Result<Option<StepOutcome>, BuildError> {
+ if layout.is_out_of_tree(&built.build_dir) {
+ layout
+ .reset_dir(&built.build_dir)
+ .map_err(|source| BuildError::Io {
+ path: built.build_dir.clone(),
+ source,
+ })?;
+ return Ok(None);
+ }
+ let (outcome, _) = run_single(&plan::clean_spec(&built.build_dir), STEP_CLEAN, timeout)?;
+ Ok(Some(outcome))
+}
+
+fn execute(
+ layout: &BuildLayout,
+ built: &BuildPlan,
+ tier: Tier,
+ label: &str,
+ extra_envs: &[(String, String)],
+ timeout: Duration,
+) -> Result<(Vec<StepOutcome>, Option<String>), BuildError> {
+ let mut outcomes = Vec::new();
+ if let Some(cleaned) = prepare_build_dir(layout, built, timeout)? {
+ outcomes.push(cleaned);
+ }
+ plan::write_generated(built)?;
+
+ for step in &built.steps {
+ let mut spec = step.spec.clone();
+ spec.envs.extend_from_slice(extra_envs);
+ let execution = process::run(&spec, timeout)?;
+ let log = log_path(layout, tier, built.system, label, step.name);
+ let written = std::fs::write(&log, &execution.output).is_ok();
+ outcomes.push(StepOutcome {
+ name: step.name.to_owned(),
+ command_line: execution.command_line.clone(),
+ success: execution.success,
+ timed_out: execution.timed_out,
+ log: written.then(|| log.clone()),
+ });
+ if !execution.success {
+ let reason = if execution.timed_out {
+ format!("step {} timed out", step.name)
+ } else {
+ format!(
+ "step {} failed ({:?}): {}",
+ step.name,
+ execution.code,
+ execution.tail(15)
+ )
+ };
+ return Ok((outcomes, Some(reason)));
+ }
+ }
+ Ok((outcomes, None))
+}
+
+fn inspect_artifacts(
+ roots: &[PathBuf],
+ requirements: &Requirements,
+) -> Result<(Vec<Artifact>, SymbolCounts, bool, Vec<String>), BuildError> {
+ let found = artifact::discover(roots)?;
+ let measured = artifact::libraries(&found);
+ let mut counts = SymbolCounts::default();
+ let mut lto_bitcode = false;
+ let mut shortfalls = Vec::new();
+
+ if found.is_empty() {
+ return Ok((found, counts, lto_bitcode, shortfalls));
+ }
+ if measured.is_empty() {
+ shortfalls.push(
+ "no library artifacts were produced; instrumentation of the target's own code \
+ cannot be verified from executables alone, because the AFL++ runtime defines \
+ the sanitizer and coverage symbols itself"
+ .to_owned(),
+ );
+ return Ok((found, counts, lto_bitcode, shortfalls));
+ }
+
+ for item in measured {
+ let kinds = artifact::member_kinds(&item.path);
+ lto_bitcode = lto_bitcode || kinds.holds_bitcode();
+ if kinds.is_mixed() {
+ shortfalls.push(format!(
+ "{}: {} of {} archive members are native objects, so they were built by a \
+ compiler lisdex did not instrument",
+ item.path.display(),
+ kinds.native,
+ kinds.native + kinds.bitcode
+ ));
+ }
+ let measured_counts = match verify::inspect(&item.path) {
+ Ok(measured_counts) => measured_counts,
+ Err(verify::VerifyError::Unreadable { .. }) => continue,
+ Err(verify::VerifyError::Process(source)) => {
+ return Err(BuildError::Process(source));
+ }
+ };
+ counts = counts.merge(measured_counts);
+ for shortfall in requirements.shortfalls(measured_counts, kinds.holds_bitcode()) {
+ shortfalls.push(format!("{}: {shortfall}", item.path.display()));
+ }
+ }
+
+ Ok((found, counts, lto_bitcode, shortfalls))
+}
+
+fn finish_attempt(
+ layout: &BuildLayout,
+ built: &BuildPlan,
+ tier: Tier,
+ steps: Vec<StepOutcome>,
+ failure: Option<String>,
+ options: &BuildOptions,
+) -> Result<Attempt, BuildError> {
+ let roots = vec![built.build_dir.clone(), layout.source.clone()];
+ let (artifacts, counts, lto_bitcode, shortfalls) = if failure.is_some() {
+ (Vec::new(), SymbolCounts::default(), false, Vec::new())
+ } else {
+ inspect_artifacts(&roots, &options.requirements)?
+ };
+ Ok(Attempt {
+ tier,
+ system: built.system,
+ build_dir: built.build_dir.clone(),
+ steps,
+ artifacts,
+ counts,
+ lto_bitcode,
+ shortfalls,
+ failure,
+ })
+}
+
+fn attempt_planned(
+ tier: Tier,
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &BuildOptions,
+ extra_envs: &[(String, String)],
+) -> Result<Attempt, BuildError> {
+ let variants = plan::plan_variants(
+ detection,
+ system,
+ layout,
+ instrumentation,
+ &PlanOptions::new(&options.variant, options.jobs),
+ )?;
+ let mut last = None;
+ for (index, built) in variants.iter().enumerate() {
+ let (steps, failure) = execute(
+ layout,
+ built,
+ tier,
+ &format!("v{index}"),
+ extra_envs,
+ options.step_timeout,
+ )?;
+ let attempt = finish_attempt(layout, built, tier, steps, failure, options)?;
+ if attempt.succeeded() {
+ return Ok(attempt);
+ }
+ last = Some(attempt);
+ }
+ last.ok_or(BuildError::Plan(plan::PlanError::NoPlan))
+}
+
+fn attempt_flag_injection(
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &BuildOptions,
+) -> Result<Attempt, BuildError> {
+ attempt_planned(
+ Tier::FlagInjection,
+ detection,
+ system,
+ layout,
+ instrumentation,
+ options,
+ &[],
+ )
+}
+
+fn attempt_compiler_shadow(
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &BuildOptions,
+) -> Result<Attempt, BuildError> {
+ let dir = layout.shadow_dir(&options.variant);
+ shadow::install(&dir, instrumentation)?;
+ let shadowed = shadow::shadowed(instrumentation, &dir);
+
+ let mut extra = shadow::recursion_guard_envs();
+ extra.push((
+ ENV_PATH.to_owned(),
+ shadow::path_value(&dir, std::env::var(ENV_PATH).ok().as_deref()),
+ ));
+ attempt_planned(
+ Tier::CompilerShadow,
+ detection,
+ system,
+ layout,
+ &shadowed,
+ options,
+ &extra,
+ )
+}
+
+fn prepass_plan(
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ options: &BuildOptions,
+) -> Result<BuildPlan, BuildError> {
+ let variant = format!("{}{PREPASS_VARIANT_SUFFIX}", options.variant);
+ Ok(plan::plan(
+ detection,
+ system,
+ layout,
+ &Instrumentation::uninstrumented(),
+ &PlanOptions::new(variant, options.jobs).with_generator(Generator::Makefiles),
+ )?)
+}
+
+fn attempt_generated_source_prepass(
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &BuildOptions,
+) -> Result<Attempt, BuildError> {
+ let prepass = prepass_plan(detection, system, layout, options)?;
+ let (mut steps, failure) = execute(
+ layout,
+ &prepass,
+ Tier::GeneratedSourcePrepass,
+ LABEL_PREPASS,
+ &[],
+ options.step_timeout,
+ )?;
+ if let Some(failure) = failure {
+ return finish_attempt(
+ layout,
+ &prepass,
+ Tier::GeneratedSourcePrepass,
+ steps,
+ Some(failure),
+ options,
+ );
+ }
+
+ let built = plan::plan(
+ detection,
+ system,
+ layout,
+ instrumentation,
+ &PlanOptions::new(&options.variant, options.jobs),
+ )?;
+
+ let (instrumented_steps, failure) = execute(
+ layout,
+ &built,
+ Tier::GeneratedSourcePrepass,
+ LABEL_INSTRUMENTED,
+ &[],
+ options.step_timeout,
+ )?;
+ steps.extend(instrumented_steps);
+ finish_attempt(
+ layout,
+ &built,
+ Tier::GeneratedSourcePrepass,
+ steps,
+ failure,
+ options,
+ )
+}
+
+fn run_single(
+ spec: &CommandSpec,
+ name: &str,
+ timeout: Duration,
+) -> Result<(StepOutcome, Option<String>), BuildError> {
+ let execution = process::run(spec, timeout)?;
+ let failure = (!execution.success).then(|| format!("{name} failed: {}", execution.tail(15)));
+ Ok((
+ StepOutcome {
+ name: name.to_owned(),
+ command_line: execution.command_line,
+ success: execution.success,
+ timed_out: execution.timed_out,
+ log: None,
+ },
+ failure,
+ ))
+}
+
+fn capture_database(
+ layout: &BuildLayout,
+ prepass: &BuildPlan,
+ database: &Path,
+ options: &BuildOptions,
+) -> Result<(Vec<StepOutcome>, Option<String>), BuildError> {
+ if database.is_file() {
+ return Ok((Vec::new(), None));
+ }
+ let Some(compile) = prepass.step(STEP_COMPILE) else {
+ return Ok((
+ Vec::new(),
+ Some("build plan has no compile step to capture".to_owned()),
+ ));
+ };
+ let wrapped = bear_wrapped(&compile.spec, database);
+ let mut captured = prepass.clone();
+ for step in &mut captured.steps {
+ if step.name == STEP_COMPILE {
+ step.spec = wrapped.clone();
+ }
+ }
+ execute(
+ layout,
+ &captured,
+ Tier::CompileCommandsReplay,
+ LABEL_CAPTURE,
+ &[],
+ options.step_timeout,
+ )
+}
+
+fn attempt_compile_commands_replay(
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &BuildOptions,
+) -> Result<Attempt, BuildError> {
+ let prepass = prepass_plan(detection, system, layout, options)?;
+ let database = prepass.build_dir.join(COMPILE_COMMANDS);
+ let finish = |steps, failure| {
+ finish_attempt(
+ layout,
+ &prepass,
+ Tier::CompileCommandsReplay,
+ steps,
+ failure,
+ options,
+ )
+ };
+
+ let (mut steps, failure) = capture_database(layout, &prepass, &database, options)?;
+ if failure.is_some() {
+ return finish(steps, failure);
+ }
+ if !database.is_file() {
+ return finish(steps, Some(format!("no {COMPILE_COMMANDS} was produced")));
+ }
+
+ let entries = match replay::load(&database) {
+ Ok(entries) => entries,
+ Err(err) => return finish(steps, Some(err.to_string())),
+ };
+ let report = replay::replay(&entries, instrumentation)?;
+ steps.push(StepOutcome {
+ name: "replay".to_owned(),
+ command_line: format!("replay {} compile commands", report.attempted),
+ success: report.succeeded(),
+ timed_out: false,
+ log: None,
+ });
+ if !report.succeeded() {
+ return finish(
+ steps,
+ Some(format!(
+ "{} of {} replayed commands failed: {}",
+ report.failures.len(),
+ report.attempted,
+ report.failures.join(" | ")
+ )),
+ );
+ }
+
+ if let Some(relink) = prepass.step(STEP_COMPILE) {
+ let (outcome, failure) = run_single(&relink.spec, "relink", options.step_timeout)?;
+ steps.push(outcome);
+ if failure.is_some() {
+ return finish(steps, failure);
+ }
+ }
+
+ finish(steps, None)
+}
+
+fn run_tier(
+ tier: Tier,
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &BuildOptions,
+) -> Result<Attempt, BuildError> {
+ match tier {
+ Tier::FlagInjection => {
+ attempt_flag_injection(detection, system, layout, instrumentation, options)
+ }
+ Tier::CompilerShadow => {
+ attempt_compiler_shadow(detection, system, layout, instrumentation, options)
+ }
+ Tier::GeneratedSourcePrepass => {
+ attempt_generated_source_prepass(detection, system, layout, instrumentation, options)
+ }
+ Tier::CompileCommandsReplay => {
+ attempt_compile_commands_replay(detection, system, layout, instrumentation, options)
+ }
+ }
+}
+
+pub fn build(
+ detection: &Detection,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &BuildOptions,
+) -> Result<BuildOutcome, BuildError> {
+ layout.prepare().map_err(|source| BuildError::Io {
+ path: layout.work.clone(),
+ source,
+ })?;
+
+ let mut outcome = BuildOutcome {
+ source: layout.source.clone(),
+ system: None,
+ tier: None,
+ build_dir: None,
+ artifacts: Vec::new(),
+ counts: SymbolCounts::default(),
+ lto_bitcode: false,
+ attempts: Vec::new(),
+ degradation: Vec::new(),
+ };
+
+ for system in detection.unsupported() {
+ outcome.degradation.push(format!(
+ "{} detected but lisdex plans no {} build",
+ system.as_str(),
+ system.as_str()
+ ));
+ }
+
+ let supported = detection.supported();
+ if supported.is_empty() {
+ outcome
+ .degradation
+ .push("no supported build system detected in the source tree".to_owned());
+ return Ok(outcome);
+ }
+
+ let mut unplannable = Vec::new();
+ for tier in &options.tiers {
+ for system in &supported {
+ let system = *system;
+ if unplannable.contains(&system) {
+ continue;
+ }
+ let attempt = match run_tier(*tier, detection, system, layout, instrumentation, options)
+ {
+ Ok(attempt) => attempt,
+ Err(BuildError::Plan(err)) => {
+ outcome
+ .degradation
+ .push(format!("{}: {err}", system.as_str()));
+ unplannable.push(system);
+ continue;
+ }
+ Err(err) => return Err(err),
+ };
+
+ if attempt.succeeded() {
+ outcome.system = Some(system);
+ outcome.tier = Some(*tier);
+ outcome.build_dir = Some(attempt.build_dir.clone());
+ outcome.artifacts.clone_from(&attempt.artifacts);
+ outcome.counts = attempt.counts;
+ outcome.lto_bitcode = attempt.lto_bitcode;
+ outcome.attempts.push(attempt);
+ return Ok(outcome);
+ }
+
+ outcome.degradation.push(attempt.summary());
+ outcome.attempts.push(attempt);
+ }
+ }
+
+ Ok(outcome)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use lisdex_ingest::detect::{Evidence, Marker};
+
+ fn detection(candidates: Vec<BuildSystem>, markers: &[(Marker, &str)]) -> Detection {
+ Detection {
+ root: PathBuf::from("/src"),
+ evidence: markers
+ .iter()
+ .map(|(marker, file)| Evidence {
+ marker: *marker,
+ file: (*file).to_owned(),
+ })
+ .collect(),
+ candidates,
+ }
+ }
+
+ fn scratch(name: &str) -> PathBuf {
+ let dir = std::env::temp_dir().join(format!("lisdex-ladder-{}-{name}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ dir
+ }
+
+ #[test]
+ fn tiers_are_ordered_cheapest_first() {
+ assert_eq!(TIERS[0], Tier::FlagInjection);
+ assert_eq!(TIERS[1], Tier::CompilerShadow);
+ assert_eq!(TIERS.len(), 4);
+ }
+
+ #[test]
+ fn the_cheapest_tier_is_tried_on_every_build_system_before_any_costlier_tier() {
+ let dir = scratch("tier-major");
+ std::fs::write(
+ dir.join("CMakeLists.txt"),
+ "cmake_minimum_required(VERSION 3.10)\n",
+ )
+ .unwrap();
+ std::fs::write(dir.join("Makefile"), "all:\n\t@false\n").unwrap();
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let outcome = build(
+ &detection(
+ vec![BuildSystem::CMake, BuildSystem::Make],
+ &[
+ (Marker::CMakeLists, "CMakeLists.txt"),
+ (Marker::Makefile, "Makefile"),
+ ],
+ ),
+ &layout,
+ &Instrumentation::instrumented(),
+ &BuildOptions::new("afl", 1)
+ .with_tiers(vec![Tier::FlagInjection, Tier::CompilerShadow]),
+ )
+ .unwrap();
+
+ let order: Vec<(Tier, BuildSystem)> = outcome
+ .attempts
+ .iter()
+ .map(|attempt| (attempt.tier, attempt.system))
+ .collect();
+ let first_shadow = order
+ .iter()
+ .position(|(tier, _)| *tier == Tier::CompilerShadow)
+ .expect("the shadow tier should have been reached");
+ let flag_injection_attempts = order
+ .iter()
+ .filter(|(tier, _)| *tier == Tier::FlagInjection)
+ .count();
+ assert_eq!(
+ flag_injection_attempts, 2,
+ "flag injection must be tried on both systems first: {order:?}"
+ );
+ assert_eq!(first_shadow, 2, "a costlier tier ran too early: {order:?}");
+ }
+
+ #[test]
+ fn every_tier_has_a_stable_name() {
+ for tier in TIERS {
+ assert!(!tier.as_str().is_empty());
+ }
+ }
+
+ #[test]
+ fn a_tree_with_no_supported_build_system_degrades_loudly_instead_of_failing_silently() {
+ let dir = scratch("unsupported");
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let outcome = build(
+ &detection(vec![BuildSystem::Bazel], &[]),
+ &layout,
+ &Instrumentation::instrumented(),
+ &BuildOptions::new("afl", 1),
+ )
+ .unwrap();
+ assert!(!outcome.succeeded());
+ assert_eq!(outcome.degradation.len(), 2);
+ assert!(outcome.degradation[0].contains("bazel"));
+ assert!(outcome.degradation[1].contains("no supported build system"));
+ }
+
+ #[test]
+ fn an_empty_detection_degrades_loudly() {
+ let dir = scratch("empty");
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let outcome = build(
+ &detection(Vec::new(), &[]),
+ &layout,
+ &Instrumentation::instrumented(),
+ &BuildOptions::new("afl", 1),
+ )
+ .unwrap();
+ assert!(!outcome.succeeded());
+ assert!(outcome.artifacts.is_empty());
+ assert_eq!(outcome.tier, None);
+ }
+
+ #[test]
+ fn a_failing_build_records_the_attempt_and_keeps_the_log() {
+ let dir = scratch("failing");
+ std::fs::write(dir.join("Makefile"), "all:\n\t@false\n").unwrap();
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let outcome = build(
+ &detection(vec![BuildSystem::Make], &[(Marker::Makefile, "Makefile")]),
+ &layout,
+ &Instrumentation::instrumented(),
+ &BuildOptions::new("afl", 1).with_tiers(vec![Tier::FlagInjection]),
+ )
+ .unwrap();
+ assert!(!outcome.succeeded());
+ assert_eq!(outcome.attempts.len(), 1);
+ assert!(outcome.attempts[0].failure.is_some());
+ let log = outcome.attempts[0]
+ .steps
+ .iter()
+ .find_map(|step| step.log.clone())
+ .expect("the failing step must keep its log");
+ assert!(log.is_file());
+ }
+
+ #[test]
+ fn an_in_tree_make_build_never_resets_the_source_directory() {
+ let dir = scratch("in-tree");
+ std::fs::write(dir.join("Makefile"), "all:\n\t@true\n").unwrap();
+ std::fs::write(dir.join("precious.c"), "int main(void){return 0;}\n").unwrap();
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let _ = build(
+ &detection(vec![BuildSystem::Make], &[(Marker::Makefile, "Makefile")]),
+ &layout,
+ &Instrumentation::instrumented(),
+ &BuildOptions::new("afl", 1).with_tiers(vec![Tier::FlagInjection]),
+ )
+ .unwrap();
+ assert!(dir.join("precious.c").is_file());
+ assert!(dir.join("Makefile").is_file());
+ }
+
+ #[test]
+ fn a_build_that_produces_nothing_is_not_reported_as_a_success() {
+ let dir = scratch("no-artifacts");
+ std::fs::write(dir.join("Makefile"), "all:\n\t@true\n").unwrap();
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let outcome = build(
+ &detection(vec![BuildSystem::Make], &[(Marker::Makefile, "Makefile")]),
+ &layout,
+ &Instrumentation::instrumented(),
+ &BuildOptions::new("afl", 1).with_tiers(vec![Tier::FlagInjection]),
+ )
+ .unwrap();
+ assert!(!outcome.succeeded());
+ assert!(
+ outcome.degradation[0].contains("no libraries or executables"),
+ "{:?}",
+ outcome.degradation
+ );
+ }
+
+ #[test]
+ fn bear_wraps_a_compile_step_without_losing_its_environment_or_directory() {
+ let spec = CommandSpec::new("make")
+ .arg("-j4")
+ .cwd("/build")
+ .env("CC", "clang");
+ let wrapped = bear_wrapped(&spec, &PathBuf::from("/build/compile_commands.json"));
+ assert_eq!(wrapped.program, BEAR_BIN);
+ assert_eq!(wrapped.cwd, Some(PathBuf::from("/build")));
+ assert_eq!(wrapped.env_value("CC"), Some("clang"));
+ let separator = wrapped.args.iter().position(|arg| arg == "--").unwrap();
+ assert_eq!(wrapped.args[separator + 1], "make");
+ assert_eq!(wrapped.args[separator + 2], "-j4");
+ }
+
+ #[test]
+ fn attempt_summaries_name_the_system_and_the_tier() {
+ let attempt = Attempt {
+ tier: Tier::CompilerShadow,
+ system: BuildSystem::CMake,
+ build_dir: PathBuf::from("/w/build"),
+ steps: Vec::new(),
+ artifacts: Vec::new(),
+ counts: SymbolCounts::default(),
+ lto_bitcode: false,
+ shortfalls: Vec::new(),
+ failure: Some("configure failed".to_owned()),
+ };
+ assert_eq!(
+ attempt.summary(),
+ "cmake via compiler-shadow: configure failed"
+ );
+ }
+
+ #[test]
+ fn an_attempt_with_artifacts_but_missing_instrumentation_is_a_failure() {
+ let attempt = Attempt {
+ tier: Tier::FlagInjection,
+ system: BuildSystem::Autotools,
+ build_dir: PathBuf::from("/w/build"),
+ steps: Vec::new(),
+ artifacts: vec![Artifact {
+ path: PathBuf::from("/w/build/libpng.a"),
+ kind: artifact::ArtifactKind::StaticLib,
+ }],
+ counts: SymbolCounts::default(),
+ lto_bitcode: false,
+ shortfalls: vec!["address sanitizer symbols: found 0, need at least 1".to_owned()],
+ failure: None,
+ };
+ assert!(!attempt.succeeded());
+ assert!(attempt.summary().contains("address sanitizer"));
+ }
+
+ #[test]
+ fn the_prepass_uses_makefiles_so_replayed_objects_are_not_rebuilt() {
+ let dir = scratch("generator");
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let built = prepass_plan(
+ &detection(vec![BuildSystem::CMake], &[]),
+ BuildSystem::CMake,
+ &layout,
+ &BuildOptions::new("afl", 1),
+ )
+ .unwrap();
+ let configure = built.step(crate::plan::STEP_CONFIGURE).unwrap();
+ let generator = configure
+ .spec
+ .args
+ .iter()
+ .position(|arg| arg == "-G")
+ .unwrap();
+ assert_eq!(
+ configure.spec.args[generator + 1],
+ crate::plan::GENERATOR_MAKEFILES
+ );
+ }
+
+ #[test]
+ fn the_prepass_is_uninstrumented_so_code_generators_run_with_a_plain_compiler() {
+ let dir = scratch("prepass-plain");
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let built = prepass_plan(
+ &detection(vec![BuildSystem::CMake], &[]),
+ BuildSystem::CMake,
+ &layout,
+ &BuildOptions::new("afl", 1),
+ )
+ .unwrap();
+ let configure = built.step(crate::plan::STEP_CONFIGURE).unwrap();
+ assert!(!configure.spec.has_env(crate::instrument::ENV_AFL_USE_ASAN));
+ assert!(
+ configure
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg.contains(crate::instrument::PLAIN_CC))
+ );
+ }
+
+ #[test]
+ fn an_in_tree_prepass_is_cleaned_before_the_instrumented_pass_is_built() {
+ let dir = scratch("prepass-clean");
+ std::fs::write(
+ dir.join("Makefile"),
+ "all:\n\t@echo built >> $(HOME)/.lisdex-ignore || true\nclean:\n\t@touch cleaned\n",
+ )
+ .unwrap();
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let outcome = build(
+ &detection(vec![BuildSystem::Make], &[(Marker::Makefile, "Makefile")]),
+ &layout,
+ &Instrumentation::instrumented(),
+ &BuildOptions::new("afl", 1).with_tiers(vec![Tier::GeneratedSourcePrepass]),
+ )
+ .unwrap();
+ assert!(
+ dir.join("cleaned").is_file(),
+ "a raw-make prepass must clean between passes or make reuses uninstrumented objects"
+ );
+ assert!(
+ outcome.attempts[0]
+ .steps
+ .iter()
+ .any(|step| step.name == "clean")
+ );
+ }
+
+ #[test]
+ fn the_prepass_never_shares_a_build_dir_with_the_instrumented_build() {
+ let dir = scratch("prepass-dir");
+ let layout = BuildLayout::new(&dir, dir.join("work"));
+ let options = BuildOptions::new("afl", 1);
+ let prepass = prepass_plan(
+ &detection(vec![BuildSystem::CMake], &[]),
+ BuildSystem::CMake,
+ &layout,
+ &options,
+ )
+ .unwrap();
+ assert_ne!(
+ prepass.build_dir,
+ layout.build_dir(BuildSystem::CMake, &options.variant)
+ );
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/layout.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/layout.rs
new file mode 100644
index 00000000..84fea8a7
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/layout.rs
@@ -0,0 +1,180 @@
+// ©AngelaMos | 2026
+// layout.rs
+
+use std::path::{Path, PathBuf};
+
+use lisdex_ingest::detect::BuildSystem;
+
+pub const VARIANT_AFL: &str = "afl";
+pub const VARIANT_CMPLOG: &str = "cmplog";
+pub const BUILD_DIR_PREFIX: &str = "build";
+pub const INSTALL_DIR: &str = "install";
+pub const TOOLCHAIN_DIR: &str = "toolchain";
+pub const SHADOW_DIR: &str = "shadow";
+pub const LOGS_DIR: &str = "logs";
+pub const ARTIFACTS_DIR: &str = "artifacts";
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct BuildLayout {
+ pub source: PathBuf,
+ pub work: PathBuf,
+}
+
+impl BuildLayout {
+ #[must_use]
+ pub fn new(source: impl Into<PathBuf>, work: impl Into<PathBuf>) -> Self {
+ Self {
+ source: source.into(),
+ work: work.into(),
+ }
+ }
+
+ #[must_use]
+ pub fn build_dir(&self, system: BuildSystem, variant: &str) -> PathBuf {
+ self.work
+ .join(format!("{BUILD_DIR_PREFIX}-{}-{variant}", system.as_str()))
+ }
+
+ #[must_use]
+ pub fn install_dir(&self, variant: &str) -> PathBuf {
+ self.work.join(format!("{INSTALL_DIR}-{variant}"))
+ }
+
+ #[must_use]
+ pub fn toolchain_dir(&self) -> PathBuf {
+ self.work.join(TOOLCHAIN_DIR)
+ }
+
+ #[must_use]
+ pub fn shadow_dir(&self, variant: &str) -> PathBuf {
+ self.work.join(format!("{SHADOW_DIR}-{variant}"))
+ }
+
+ #[must_use]
+ pub fn logs_dir(&self) -> PathBuf {
+ self.work.join(LOGS_DIR)
+ }
+
+ #[must_use]
+ pub fn artifacts_dir(&self, variant: &str) -> PathBuf {
+ self.work.join(format!("{ARTIFACTS_DIR}-{variant}"))
+ }
+
+ pub fn prepare(&self) -> std::io::Result<()> {
+ std::fs::create_dir_all(&self.work)?;
+ std::fs::create_dir_all(self.toolchain_dir())?;
+ std::fs::create_dir_all(self.logs_dir())
+ }
+
+ pub fn reset_dir(&self, dir: &Path) -> std::io::Result<()> {
+ if dir.exists() {
+ std::fs::remove_dir_all(dir)?;
+ }
+ std::fs::create_dir_all(dir)
+ }
+
+ #[must_use]
+ pub fn is_out_of_tree(&self, build_dir: &Path) -> bool {
+ !build_dir.starts_with(&self.source)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn layout() -> BuildLayout {
+ BuildLayout::new("/work/src/libpng", "/work/build/libpng")
+ }
+
+ #[test]
+ fn build_dirs_are_namespaced_by_system_and_variant() {
+ assert_eq!(
+ layout().build_dir(BuildSystem::CMake, VARIANT_AFL),
+ PathBuf::from("/work/build/libpng/build-cmake-afl")
+ );
+ assert_eq!(
+ layout().build_dir(BuildSystem::Autotools, VARIANT_CMPLOG),
+ PathBuf::from("/work/build/libpng/build-autotools-cmplog")
+ );
+ }
+
+ #[test]
+ fn the_two_variants_never_share_a_build_dir() {
+ let afl = layout().build_dir(BuildSystem::CMake, VARIANT_AFL);
+ let cmplog = layout().build_dir(BuildSystem::CMake, VARIANT_CMPLOG);
+ assert_ne!(afl, cmplog);
+ }
+
+ #[test]
+ fn trying_a_second_build_system_does_not_reuse_the_first_ones_directory() {
+ let cmake = layout().build_dir(BuildSystem::CMake, VARIANT_AFL);
+ let autotools = layout().build_dir(BuildSystem::Autotools, VARIANT_AFL);
+ assert_ne!(cmake, autotools);
+ }
+
+ #[test]
+ fn builds_live_outside_the_source_tree() {
+ let layout = layout();
+ let build = layout.build_dir(BuildSystem::CMake, VARIANT_AFL);
+ assert!(layout.is_out_of_tree(&build));
+ }
+
+ #[test]
+ fn an_in_tree_build_dir_is_reported_as_such() {
+ let layout = BuildLayout::new("/work/src/lua", "/work/src/lua");
+ let build = layout.build_dir(BuildSystem::Make, VARIANT_AFL);
+ assert!(!layout.is_out_of_tree(&build));
+ }
+
+ #[test]
+ fn install_and_artifact_dirs_are_variant_scoped() {
+ assert_eq!(
+ layout().install_dir(VARIANT_AFL),
+ PathBuf::from("/work/build/libpng/install-afl")
+ );
+ assert_eq!(
+ layout().artifacts_dir(VARIANT_CMPLOG),
+ PathBuf::from("/work/build/libpng/artifacts-cmplog")
+ );
+ }
+
+ #[test]
+ fn prepare_creates_the_scratch_tree() {
+ let work = std::env::temp_dir().join(format!("lisdex-layout-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&work);
+ let layout = BuildLayout::new("/src", &work);
+ layout.prepare().unwrap();
+ assert!(layout.toolchain_dir().is_dir());
+ assert!(layout.logs_dir().is_dir());
+ }
+
+ #[test]
+ fn resetting_a_build_dir_clears_stale_state() {
+ let work = std::env::temp_dir().join(format!("lisdex-layout-reset-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&work);
+ let layout = BuildLayout::new("/src", &work);
+ layout.prepare().unwrap();
+ let dir = layout.build_dir(BuildSystem::CMake, VARIANT_AFL);
+ layout.reset_dir(&dir).unwrap();
+ std::fs::write(dir.join("CMakeCache.txt"), "stale").unwrap();
+ layout.reset_dir(&dir).unwrap();
+ assert!(!dir.join("CMakeCache.txt").exists());
+ }
+
+ #[test]
+ fn a_prepass_directory_is_reset_by_its_own_path_not_a_reparsed_name() {
+ let work =
+ std::env::temp_dir().join(format!("lisdex-layout-prepass-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&work);
+ let layout = BuildLayout::new("/src", &work);
+ layout.prepare().unwrap();
+ let prepass = layout.build_dir(BuildSystem::CMake, "afl-prepass");
+ assert!(prepass.ends_with("build-cmake-afl-prepass"));
+ layout.reset_dir(&prepass).unwrap();
+ std::fs::write(prepass.join("CMakeCache.txt"), "stale").unwrap();
+ layout.reset_dir(&prepass).unwrap();
+ assert!(!prepass.join("CMakeCache.txt").exists());
+ assert!(!layout.build_dir(BuildSystem::CMake, "prepass").exists());
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/lib.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/lib.rs
new file mode 100644
index 00000000..9aa17ee1
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/lib.rs
@@ -0,0 +1,11 @@
+// ©AngelaMos | 2026
+// lib.rs
+
+pub mod artifact;
+pub mod instrument;
+pub mod ladder;
+pub mod layout;
+pub mod plan;
+pub mod replay;
+pub mod shadow;
+pub mod verify;
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/plan.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/plan.rs
new file mode 100644
index 00000000..1eb86b3e
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/plan.rs
@@ -0,0 +1,964 @@
+// ©AngelaMos | 2026
+// plan.rs
+
+use std::path::{Path, PathBuf};
+
+use lisdex_core::process::CommandSpec;
+use lisdex_ingest::detect::{Bootstrap, BuildSystem, Detection};
+
+use crate::instrument::{
+ ENV_AFL_NOOPT, ENV_AR, ENV_CFLAGS, ENV_CXXFLAGS, ENV_ENABLED, ENV_RANLIB, Instrumentation,
+};
+use crate::layout::BuildLayout;
+
+pub const CMAKE_BIN: &str = "cmake";
+pub const MESON_BIN: &str = "meson";
+pub const MAKE_BIN: &str = "make";
+pub const AUTORECONF_BIN: &str = "autoreconf";
+pub const SH_BIN: &str = "sh";
+
+pub const STEP_BOOTSTRAP: &str = "bootstrap";
+pub const STEP_CONFIGURE: &str = "configure";
+pub const STEP_COMPILE: &str = "compile";
+
+pub const INJECT_FILE: &str = "lisdex-inject.cmake";
+pub const NATIVE_FILE: &str = "lisdex-native.ini";
+
+pub const GENERATOR_NINJA: &str = "Ninja";
+pub const GENERATOR_MAKEFILES: &str = "Unix Makefiles";
+
+pub const ENV_NOCONFIGURE: &str = "NOCONFIGURE";
+pub const ENV_CMAKE_POLICY_MINIMUM: &str = "CMAKE_POLICY_VERSION_MINIMUM";
+pub const CMAKE_POLICY_MINIMUM: &str = "3.5";
+
+const CONFIGURE_SCRIPT: &str = "configure";
+const TARGET_CLEAN: &str = "clean";
+const RELWITHDEBINFO_FLAGS: &str = "-O2 -g";
+const AUTORECONF_ARGS: [&str; 4] = ["--force", "--install", "-I", "m4"];
+const AUTOTOOLS_CONFIGURE_ARGS: [&str; 4] = [
+ "--disable-shared",
+ "--enable-static",
+ "--disable-dependency-tracking",
+ "--disable-werror",
+];
+
+#[derive(Debug, thiserror::Error)]
+pub enum PlanError {
+ #[error("{0} builds are detected but not planned by lisdex")]
+ Unsupported(&'static str),
+ #[error("autotools target has no configure script and bootstrapping is disabled")]
+ MissingConfigure,
+ #[error("no build plan could be produced")]
+ NoPlan,
+ #[error("io error at {path}: {source}")]
+ Io {
+ path: PathBuf,
+ source: std::io::Error,
+ },
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub enum Generator {
+ #[default]
+ Ninja,
+ Makefiles,
+}
+
+impl Generator {
+ #[must_use]
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Generator::Ninja => GENERATOR_NINJA,
+ Generator::Makefiles => GENERATOR_MAKEFILES,
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PlanOptions {
+ pub jobs: usize,
+ pub generator: Generator,
+ pub bootstrap: BootstrapPolicy,
+ pub variant: String,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub enum BootstrapPolicy {
+ #[default]
+ Auto,
+ Force,
+ Never,
+}
+
+impl BootstrapPolicy {
+ #[must_use]
+ pub fn wants_bootstrap(self, has_generated_configure: bool) -> bool {
+ match self {
+ BootstrapPolicy::Auto => !has_generated_configure,
+ BootstrapPolicy::Force => true,
+ BootstrapPolicy::Never => false,
+ }
+ }
+}
+
+impl PlanOptions {
+ #[must_use]
+ pub fn new(variant: impl Into<String>, jobs: usize) -> Self {
+ Self {
+ jobs,
+ generator: Generator::default(),
+ bootstrap: BootstrapPolicy::default(),
+ variant: variant.into(),
+ }
+ }
+
+ #[must_use]
+ pub fn with_generator(mut self, generator: Generator) -> Self {
+ self.generator = generator;
+ self
+ }
+
+ #[must_use]
+ pub fn with_bootstrap(mut self, bootstrap: BootstrapPolicy) -> Self {
+ self.bootstrap = bootstrap;
+ self
+ }
+
+ #[must_use]
+ pub fn without_bootstrap(self) -> Self {
+ self.with_bootstrap(BootstrapPolicy::Never)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct GeneratedFile {
+ pub path: PathBuf,
+ pub contents: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct BuildStep {
+ pub name: &'static str,
+ pub spec: CommandSpec,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct BuildPlan {
+ pub system: BuildSystem,
+ pub build_dir: PathBuf,
+ pub generated: Vec<GeneratedFile>,
+ pub steps: Vec<BuildStep>,
+}
+
+impl BuildPlan {
+ #[must_use]
+ pub fn step(&self, name: &str) -> Option<&BuildStep> {
+ self.steps.iter().find(|step| step.name == name)
+ }
+}
+
+fn jobs_arg(jobs: usize) -> String {
+ jobs.max(1).to_string()
+}
+
+#[must_use]
+pub fn absolute_tool(program: &str) -> String {
+ lisdex_core::process::resolve_on_path(program)
+ .map_or_else(|| program.to_owned(), |path| path.display().to_string())
+}
+
+#[must_use]
+pub fn clean_spec(build_dir: &Path) -> CommandSpec {
+ CommandSpec::new(MAKE_BIN)
+ .arg("-C")
+ .path_arg(build_dir)
+ .arg(TARGET_CLEAN)
+}
+
+fn is_executable(path: &Path) -> bool {
+ use std::os::unix::fs::PermissionsExt;
+ std::fs::metadata(path).is_ok_and(|meta| meta.permissions().mode() & 0o111 != 0)
+}
+
+fn script_spec(script: &Path) -> CommandSpec {
+ if is_executable(script) {
+ CommandSpec::new(script.display().to_string())
+ } else {
+ CommandSpec::new(SH_BIN).path_arg(script)
+ }
+}
+
+fn without_flag_env(envs: Vec<(String, String)>) -> Vec<(String, String)> {
+ envs.into_iter()
+ .filter(|(key, _)| key != ENV_CFLAGS && key != ENV_CXXFLAGS)
+ .collect()
+}
+
+fn cmake_inject_file(path: PathBuf, instrumentation: &Instrumentation) -> GeneratedFile {
+ use std::fmt::Write;
+
+ let mut contents = String::from("# ©AngelaMos | 2026\n# lisdex-inject.cmake\n\n");
+ if !instrumentation.cflags.is_empty() {
+ let _ = writeln!(
+ contents,
+ "add_compile_options({})",
+ instrumentation.cflags.join(" ")
+ );
+ }
+ if !instrumentation.ldflags.is_empty() {
+ let _ = writeln!(
+ contents,
+ "add_link_options({})",
+ instrumentation.ldflags.join(" ")
+ );
+ }
+ GeneratedFile { path, contents }
+}
+
+fn meson_native_file(path: PathBuf, instrumentation: &Instrumentation) -> GeneratedFile {
+ let quoted = |flags: &[String]| {
+ flags
+ .iter()
+ .map(|flag| format!("'{flag}'"))
+ .collect::<Vec<String>>()
+ .join(", ")
+ };
+ let toolchain = &instrumentation.toolchain;
+ let contents = format!(
+ "# ©AngelaMos | 2026\n# lisdex-native.ini\n\n\
+ [binaries]\n\
+ c = '{}'\n\
+ cpp = '{}'\n\
+ ar = '{}'\n\
+ nm = '{}'\n\n\
+ [built-in options]\n\
+ c_args = [{}]\n\
+ cpp_args = [{}]\n\
+ c_link_args = [{}]\n\
+ cpp_link_args = [{}]\n",
+ absolute_tool(&toolchain.cc),
+ absolute_tool(&toolchain.cxx),
+ absolute_tool(&toolchain.ar),
+ absolute_tool(&toolchain.nm),
+ quoted(&instrumentation.cflags),
+ quoted(&instrumentation.cflags),
+ quoted(&instrumentation.ldflags),
+ quoted(&instrumentation.ldflags),
+ );
+ GeneratedFile { path, contents }
+}
+
+fn cmake_plan(
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &PlanOptions,
+) -> BuildPlan {
+ let build_dir = layout.build_dir(BuildSystem::CMake, &options.variant);
+ let inject = cmake_inject_file(layout.toolchain_dir().join(INJECT_FILE), instrumentation);
+ let toolchain = &instrumentation.toolchain;
+
+ let configure = CommandSpec::new(CMAKE_BIN)
+ .arg("-S")
+ .path_arg(&layout.source)
+ .arg("-B")
+ .path_arg(&build_dir)
+ .arg("-G")
+ .arg(options.generator.as_str())
+ .arg(format!(
+ "-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={}",
+ inject.path.display()
+ ))
+ .arg(format!("-DCMAKE_C_COMPILER={}", toolchain.cc))
+ .arg(format!("-DCMAKE_CXX_COMPILER={}", toolchain.cxx))
+ .arg(format!("-DCMAKE_AR={}", absolute_tool(&toolchain.ar)))
+ .arg(format!(
+ "-DCMAKE_RANLIB={}",
+ absolute_tool(&toolchain.ranlib)
+ ))
+ .arg("-DBUILD_SHARED_LIBS=OFF")
+ .arg("-DCMAKE_BUILD_TYPE=RelWithDebInfo")
+ .arg(format!(
+ "-DCMAKE_C_FLAGS_RELWITHDEBINFO={RELWITHDEBINFO_FLAGS}"
+ ))
+ .arg(format!(
+ "-DCMAKE_CXX_FLAGS_RELWITHDEBINFO={RELWITHDEBINFO_FLAGS}"
+ ))
+ .arg("-DCMAKE_EXPORT_COMPILE_COMMANDS=ON")
+ .arg(format!(
+ "-D{ENV_CMAKE_POLICY_MINIMUM}={CMAKE_POLICY_MINIMUM}"
+ ))
+ .envs(without_flag_env(instrumentation.envs()))
+ .env(ENV_CMAKE_POLICY_MINIMUM, CMAKE_POLICY_MINIMUM)
+ .env(ENV_AFL_NOOPT, ENV_ENABLED);
+
+ let compile = CommandSpec::new(CMAKE_BIN)
+ .arg("--build")
+ .path_arg(&build_dir)
+ .arg("-j")
+ .arg(jobs_arg(options.jobs))
+ .envs(without_flag_env(instrumentation.envs()));
+
+ BuildPlan {
+ system: BuildSystem::CMake,
+ build_dir,
+ generated: vec![inject],
+ steps: vec![
+ BuildStep {
+ name: STEP_CONFIGURE,
+ spec: configure,
+ },
+ BuildStep {
+ name: STEP_COMPILE,
+ spec: compile,
+ },
+ ],
+ }
+}
+
+fn meson_plan(
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &PlanOptions,
+) -> BuildPlan {
+ let build_dir = layout.build_dir(BuildSystem::Meson, &options.variant);
+ let native = meson_native_file(layout.toolchain_dir().join(NATIVE_FILE), instrumentation);
+
+ let configure = CommandSpec::new(MESON_BIN)
+ .arg("setup")
+ .path_arg(&build_dir)
+ .path_arg(&layout.source)
+ .arg("--native-file")
+ .path_arg(&native.path)
+ .arg("--buildtype=debugoptimized")
+ .arg("-Ddefault_library=static")
+ .arg("-Dwerror=false")
+ .envs(without_flag_env(instrumentation.envs()))
+ .env(ENV_AFL_NOOPT, ENV_ENABLED);
+
+ let compile = CommandSpec::new(MESON_BIN)
+ .arg("compile")
+ .arg("-C")
+ .path_arg(&build_dir)
+ .arg("-j")
+ .arg(jobs_arg(options.jobs))
+ .envs(without_flag_env(instrumentation.envs()));
+
+ BuildPlan {
+ system: BuildSystem::Meson,
+ build_dir,
+ generated: vec![native],
+ steps: vec![
+ BuildStep {
+ name: STEP_CONFIGURE,
+ spec: configure,
+ },
+ BuildStep {
+ name: STEP_COMPILE,
+ spec: compile,
+ },
+ ],
+ }
+}
+
+fn autotools_plan(
+ detection: &Detection,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &PlanOptions,
+) -> Result<BuildPlan, PlanError> {
+ let build_dir = layout.build_dir(BuildSystem::Autotools, &options.variant);
+ let configure_path = layout.source.join(CONFIGURE_SCRIPT);
+ let mut steps = Vec::new();
+
+ if options
+ .bootstrap
+ .wants_bootstrap(detection.has_generated_configure())
+ {
+ match detection.bootstrap() {
+ Bootstrap::Autogen(script) => {
+ steps.push(BuildStep {
+ name: STEP_BOOTSTRAP,
+ spec: script_spec(&layout.source.join(&script))
+ .cwd(&layout.source)
+ .envs(instrumentation.envs())
+ .env(ENV_NOCONFIGURE, ENV_ENABLED)
+ .env(ENV_AFL_NOOPT, ENV_ENABLED),
+ });
+ }
+ Bootstrap::Autoreconf => {
+ steps.push(BuildStep {
+ name: STEP_BOOTSTRAP,
+ spec: CommandSpec::new(AUTORECONF_BIN)
+ .args(AUTORECONF_ARGS)
+ .cwd(&layout.source)
+ .envs(instrumentation.envs())
+ .env(ENV_AFL_NOOPT, ENV_ENABLED),
+ });
+ }
+ Bootstrap::None => {}
+ }
+ }
+
+ if steps.is_empty() && !detection.has_generated_configure() {
+ return Err(PlanError::MissingConfigure);
+ }
+
+ steps.push(BuildStep {
+ name: STEP_CONFIGURE,
+ spec: script_spec(&configure_path)
+ .args(AUTOTOOLS_CONFIGURE_ARGS)
+ .cwd(&build_dir)
+ .envs(instrumentation.envs())
+ .env(ENV_AFL_NOOPT, ENV_ENABLED),
+ });
+
+ steps.push(BuildStep {
+ name: STEP_COMPILE,
+ spec: CommandSpec::new(MAKE_BIN)
+ .arg("-j")
+ .arg(jobs_arg(options.jobs))
+ .arg(format!("{ENV_AR}={}", instrumentation.toolchain.ar))
+ .arg(format!("{ENV_RANLIB}={}", instrumentation.toolchain.ranlib))
+ .cwd(&build_dir)
+ .envs(instrumentation.envs()),
+ });
+
+ Ok(BuildPlan {
+ system: BuildSystem::Autotools,
+ build_dir,
+ generated: Vec::new(),
+ steps,
+ })
+}
+
+fn make_plan(
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &PlanOptions,
+) -> BuildPlan {
+ let toolchain = &instrumentation.toolchain;
+ let compile = CommandSpec::new(MAKE_BIN)
+ .arg("-j")
+ .arg(jobs_arg(options.jobs))
+ .arg("-C")
+ .path_arg(&layout.source)
+ .arg(format!("CC={}", toolchain.cc))
+ .arg(format!("CXX={}", toolchain.cxx))
+ .envs(instrumentation.envs());
+
+ BuildPlan {
+ system: BuildSystem::Make,
+ build_dir: layout.source.clone(),
+ generated: Vec::new(),
+ steps: vec![BuildStep {
+ name: STEP_COMPILE,
+ spec: compile,
+ }],
+ }
+}
+
+pub fn plan(
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &PlanOptions,
+) -> Result<BuildPlan, PlanError> {
+ match system {
+ BuildSystem::CMake => Ok(cmake_plan(layout, instrumentation, options)),
+ BuildSystem::Meson => Ok(meson_plan(layout, instrumentation, options)),
+ BuildSystem::Autotools => autotools_plan(detection, layout, instrumentation, options),
+ BuildSystem::Make => Ok(make_plan(layout, instrumentation, options)),
+ BuildSystem::Bazel => Err(PlanError::Unsupported("bazel")),
+ BuildSystem::Scons => Err(PlanError::Unsupported("scons")),
+ BuildSystem::Waf => Err(PlanError::Unsupported("waf")),
+ }
+}
+
+pub fn plan_variants(
+ detection: &Detection,
+ system: BuildSystem,
+ layout: &BuildLayout,
+ instrumentation: &Instrumentation,
+ options: &PlanOptions,
+) -> Result<Vec<BuildPlan>, PlanError> {
+ let mut variants = vec![plan(detection, system, layout, instrumentation, options)?];
+ let bootstrap_would_help = system == BuildSystem::Autotools
+ && options.bootstrap == BootstrapPolicy::Auto
+ && detection.has_generated_configure()
+ && detection.bootstrap() != Bootstrap::None;
+ if bootstrap_would_help {
+ variants.push(plan(
+ detection,
+ system,
+ layout,
+ instrumentation,
+ &options.clone().with_bootstrap(BootstrapPolicy::Force),
+ )?);
+ }
+ Ok(variants)
+}
+
+pub fn write_generated(plan: &BuildPlan) -> Result<(), PlanError> {
+ for file in &plan.generated {
+ if let Some(parent) = file.path.parent() {
+ std::fs::create_dir_all(parent).map_err(|source| PlanError::Io {
+ path: parent.to_path_buf(),
+ source,
+ })?;
+ }
+ std::fs::write(&file.path, &file.contents).map_err(|source| PlanError::Io {
+ path: file.path.clone(),
+ source,
+ })?;
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use lisdex_ingest::detect::{Evidence, Marker};
+
+ fn layout() -> BuildLayout {
+ BuildLayout::new("/src/libpng", "/work/libpng")
+ }
+
+ fn options() -> PlanOptions {
+ PlanOptions::new("afl", 4)
+ }
+
+ fn detection(markers: &[(Marker, &str)]) -> Detection {
+ Detection {
+ root: PathBuf::from("/src/libpng"),
+ evidence: markers
+ .iter()
+ .map(|(marker, file)| Evidence {
+ marker: *marker,
+ file: (*file).to_owned(),
+ })
+ .collect(),
+ candidates: Vec::new(),
+ }
+ }
+
+ fn plan_for(system: BuildSystem, detection: &Detection) -> BuildPlan {
+ plan(
+ detection,
+ system,
+ &layout(),
+ &Instrumentation::instrumented(),
+ &options(),
+ )
+ .unwrap()
+ }
+
+ #[test]
+ fn cmake_injects_flags_through_the_top_level_include_hook() {
+ let built = plan_for(BuildSystem::CMake, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert!(
+ configure
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg.starts_with("-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES="))
+ );
+ let inject = &built.generated[0];
+ assert!(inject.contents.contains("add_compile_options("));
+ assert!(inject.path.ends_with(INJECT_FILE));
+ }
+
+ #[test]
+ fn cmake_never_relies_on_cmake_c_flags_which_projects_clobber() {
+ let built = plan_for(BuildSystem::CMake, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert!(
+ !configure
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg.starts_with("-DCMAKE_C_FLAGS=")
+ || arg.starts_with("-DCMAKE_C_FLAGS_INIT="))
+ );
+ }
+
+ #[test]
+ fn cmake_relwithdebinfo_flags_are_overridden_to_drop_ndebug() {
+ let built = plan_for(BuildSystem::CMake, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ let flags: Vec<&String> = configure
+ .spec
+ .args
+ .iter()
+ .filter(|arg| arg.contains("RELWITHDEBINFO"))
+ .collect();
+ assert_eq!(flags.len(), 2);
+ assert!(flags.iter().all(|arg| !arg.contains("NDEBUG")));
+ }
+
+ #[test]
+ fn cmake_exports_a_compilation_database_and_uses_a_supporting_generator() {
+ let built = plan_for(BuildSystem::CMake, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert!(
+ configure
+ .spec
+ .args
+ .contains(&"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON".to_owned())
+ );
+ let generator = configure
+ .spec
+ .args
+ .iter()
+ .position(|arg| arg == "-G")
+ .unwrap();
+ assert_eq!(configure.spec.args[generator + 1], GENERATOR_NINJA);
+ }
+
+ #[test]
+ fn cmake_carries_the_policy_minimum_on_the_command_line_and_in_the_environment() {
+ let built = plan_for(BuildSystem::CMake, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert!(configure.spec.args.contains(&format!(
+ "-D{ENV_CMAKE_POLICY_MINIMUM}={CMAKE_POLICY_MINIMUM}"
+ )));
+ assert_eq!(
+ configure.spec.env_value(ENV_CMAKE_POLICY_MINIMUM),
+ Some(CMAKE_POLICY_MINIMUM)
+ );
+ }
+
+ #[test]
+ fn cmake_builds_out_of_tree() {
+ let built = plan_for(BuildSystem::CMake, &detection(&[]));
+ assert!(layout().is_out_of_tree(&built.build_dir));
+ }
+
+ #[test]
+ fn meson_configures_through_a_native_file_and_never_through_cflags() {
+ let built = plan_for(BuildSystem::Meson, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert!(configure.spec.args.iter().any(|arg| arg == "--native-file"));
+ assert!(!configure.spec.has_env(ENV_CFLAGS));
+ assert!(
+ !configure
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg.starts_with("-Dc_args"))
+ );
+ }
+
+ #[test]
+ fn meson_never_sets_b_sanitize_because_afl_cc_owns_the_sanitizer_flags() {
+ let built = plan_for(BuildSystem::Meson, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert!(
+ !configure
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg.contains("b_sanitize"))
+ );
+ assert!(configure.spec.has_env(crate::instrument::ENV_AFL_USE_UBSAN));
+ }
+
+ #[test]
+ fn meson_native_file_names_the_compilers_and_quotes_flag_arrays() {
+ let built = plan_for(BuildSystem::Meson, &detection(&[]));
+ let native = &built.generated[0];
+ assert!(native.contents.contains("[binaries]"));
+ assert!(native.contents.contains(&format!(
+ "c = '{}'",
+ absolute_tool(crate::instrument::AFL_CC)
+ )));
+ assert!(native.contents.contains("[built-in options]"));
+ assert!(native.contents.contains("c_args = ['-g', '-O2'"));
+ }
+
+ #[test]
+ fn meson_builds_static_libraries() {
+ let built = plan_for(BuildSystem::Meson, &detection(&[]));
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert!(
+ configure
+ .spec
+ .args
+ .contains(&"-Ddefault_library=static".to_owned())
+ );
+ }
+
+ #[test]
+ fn autotools_runs_autogen_when_the_project_ships_one() {
+ let detected = detection(&[
+ (Marker::Autogen, "autogen.sh"),
+ (Marker::ConfigureAc, "configure.ac"),
+ ]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ let bootstrap = built.step(STEP_BOOTSTRAP).unwrap();
+ assert!(bootstrap.spec.command_line().contains("autogen.sh"));
+ assert_eq!(bootstrap.spec.env_value(ENV_NOCONFIGURE), Some(ENV_ENABLED));
+ }
+
+ #[test]
+ fn autotools_falls_back_to_autoreconf() {
+ let detected = detection(&[(Marker::ConfigureAc, "configure.ac")]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ let bootstrap = built.step(STEP_BOOTSTRAP).unwrap();
+ assert_eq!(bootstrap.spec.program, AUTORECONF_BIN);
+ assert!(bootstrap.spec.args.contains(&"--install".to_owned()));
+ }
+
+ #[test]
+ fn a_shipped_configure_suppresses_autogen_which_refuses_to_rerun() {
+ let detected = detection(&[
+ (Marker::Autogen, "autogen.sh"),
+ (Marker::ConfigureAc, "configure.ac"),
+ (Marker::ConfigureScript, "configure"),
+ ]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ assert!(built.step(STEP_BOOTSTRAP).is_none());
+ assert!(built.step(STEP_CONFIGURE).is_some());
+ }
+
+ #[test]
+ fn a_shipped_configure_still_yields_a_bootstrapping_fallback_variant() {
+ let detected = detection(&[
+ (Marker::Autogen, "autogen.sh"),
+ (Marker::ConfigureAc, "configure.ac"),
+ (Marker::ConfigureScript, "configure"),
+ ]);
+ let variants = plan_variants(
+ &detected,
+ BuildSystem::Autotools,
+ &layout(),
+ &Instrumentation::instrumented(),
+ &options(),
+ )
+ .unwrap();
+ assert_eq!(variants.len(), 2);
+ assert!(variants[0].step(STEP_BOOTSTRAP).is_none());
+ assert!(variants[1].step(STEP_BOOTSTRAP).is_some());
+ }
+
+ #[test]
+ fn a_tree_without_a_shipped_configure_has_a_single_variant() {
+ let detected = detection(&[(Marker::ConfigureAc, "configure.ac")]);
+ let variants = plan_variants(
+ &detected,
+ BuildSystem::Autotools,
+ &layout(),
+ &Instrumentation::instrumented(),
+ &options(),
+ )
+ .unwrap();
+ assert_eq!(variants.len(), 1);
+ assert!(variants[0].step(STEP_BOOTSTRAP).is_some());
+ }
+
+ #[test]
+ fn cmake_receives_absolute_tool_paths_because_it_resolves_them_against_the_cwd() {
+ assert!(absolute_tool("sh").starts_with('/'));
+ assert_eq!(
+ absolute_tool("lisdex-no-such-tool"),
+ "lisdex-no-such-tool".to_owned()
+ );
+ }
+
+ #[test]
+ fn a_clean_step_targets_the_build_directory() {
+ let spec = clean_spec(Path::new("/src/lua"));
+ assert_eq!(spec.program, MAKE_BIN);
+ assert_eq!(spec.args, vec!["-C", "/src/lua", TARGET_CLEAN]);
+ }
+
+ #[test]
+ fn autotools_skips_bootstrap_for_a_pre_generated_configure() {
+ let detected = detection(&[(Marker::ConfigureScript, "configure")]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ assert!(built.step(STEP_BOOTSTRAP).is_none());
+ assert!(built.step(STEP_CONFIGURE).is_some());
+ }
+
+ #[test]
+ fn autotools_without_configure_or_bootstrap_is_a_planning_error() {
+ let detected = detection(&[(Marker::ConfigureAc, "configure.ac")]);
+ let result = plan(
+ &detected,
+ BuildSystem::Autotools,
+ &layout(),
+ &Instrumentation::instrumented(),
+ &options().without_bootstrap(),
+ );
+ assert!(matches!(result, Err(PlanError::MissingConfigure)));
+ }
+
+ #[test]
+ fn autotools_configure_runs_in_the_out_of_tree_build_dir() {
+ let detected = detection(&[(Marker::ConfigureScript, "configure")]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ assert_eq!(
+ configure.spec.cwd.as_deref(),
+ Some(built.build_dir.as_path())
+ );
+ assert!(
+ configure
+ .spec
+ .command_line()
+ .contains("/src/libpng/configure")
+ );
+ }
+
+ #[test]
+ fn autotools_configure_disables_shared_and_werror() {
+ let detected = detection(&[(Marker::ConfigureScript, "configure")]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ let configure = built.step(STEP_CONFIGURE).unwrap();
+ for expected in AUTOTOOLS_CONFIGURE_ARGS {
+ assert!(configure.spec.args.contains(&expected.to_owned()));
+ }
+ }
+
+ #[test]
+ fn configure_runs_with_afl_noopt_and_the_build_does_not() {
+ let detected = detection(&[(Marker::ConfigureScript, "configure")]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ assert_eq!(
+ built
+ .step(STEP_CONFIGURE)
+ .unwrap()
+ .spec
+ .env_value(ENV_AFL_NOOPT),
+ Some(ENV_ENABLED)
+ );
+ assert!(
+ !built
+ .step(STEP_COMPILE)
+ .unwrap()
+ .spec
+ .has_env(ENV_AFL_NOOPT)
+ );
+ }
+
+ #[test]
+ fn autotools_repasses_the_archiver_to_make_because_configure_may_not_save_it() {
+ let detected = detection(&[(Marker::ConfigureScript, "configure")]);
+ let built = plan_for(BuildSystem::Autotools, &detected);
+ let compile = built.step(STEP_COMPILE).unwrap();
+ assert!(compile.spec.args.iter().any(|arg| arg == "AR=llvm-ar"));
+ assert!(
+ compile
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg == "RANLIB=llvm-ranlib")
+ );
+ }
+
+ #[test]
+ fn raw_make_overrides_a_hardcoded_compiler_with_a_command_line_variable() {
+ let built = plan_for(BuildSystem::Make, &detection(&[]));
+ let compile = built.step(STEP_COMPILE).unwrap();
+ assert!(
+ compile
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg == "CC=afl-clang-lto")
+ );
+ assert!(compile.spec.has_env(ENV_CFLAGS));
+ assert!(
+ !compile
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg.starts_with("CFLAGS="))
+ );
+ }
+
+ #[test]
+ fn raw_make_never_overrides_ar_because_makefiles_bake_flags_into_it() {
+ let built = plan_for(BuildSystem::Make, &detection(&[]));
+ let compile = built.step(STEP_COMPILE).unwrap();
+ assert!(
+ !compile
+ .spec
+ .args
+ .iter()
+ .any(|arg| arg.starts_with("AR=") || arg.starts_with("RANLIB=")),
+ "lua declares AR= ar rcu; overriding AR drops rcu and ar prints its usage"
+ );
+ }
+
+ #[test]
+ fn unsupported_build_systems_fail_planning_loudly() {
+ for system in [BuildSystem::Bazel, BuildSystem::Scons, BuildSystem::Waf] {
+ let result = plan(
+ &detection(&[]),
+ system,
+ &layout(),
+ &Instrumentation::instrumented(),
+ &options(),
+ );
+ assert!(matches!(result, Err(PlanError::Unsupported(_))));
+ }
+ }
+
+ #[test]
+ fn the_cmplog_variant_gets_its_own_build_dir_and_env() {
+ let built = plan(
+ &detection(&[]),
+ BuildSystem::CMake,
+ &layout(),
+ &Instrumentation::instrumented().with_cmplog(true),
+ &PlanOptions::new("cmplog", 4),
+ )
+ .unwrap();
+ assert!(built.build_dir.ends_with("build-cmake-cmplog"));
+ assert!(
+ built
+ .step(STEP_COMPILE)
+ .unwrap()
+ .spec
+ .has_env(crate::instrument::ENV_AFL_LLVM_CMPLOG)
+ );
+ }
+
+ #[test]
+ fn generated_files_are_written_to_disk() {
+ let work = std::env::temp_dir().join(format!("lisdex-plan-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&work);
+ let layout = BuildLayout::new("/src/libpng", &work);
+ let built = plan(
+ &detection(&[]),
+ BuildSystem::CMake,
+ &layout,
+ &Instrumentation::instrumented(),
+ &options(),
+ )
+ .unwrap();
+ write_generated(&built).unwrap();
+ let written = std::fs::read_to_string(&built.generated[0].path).unwrap();
+ assert!(written.contains("©AngelaMos"));
+ }
+
+ #[test]
+ fn a_non_executable_configure_is_run_through_a_shell() {
+ let work = std::env::temp_dir().join(format!("lisdex-plan-sh-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&work);
+ std::fs::create_dir_all(&work).unwrap();
+ std::fs::write(work.join(CONFIGURE_SCRIPT), "#!/bin/sh\n").unwrap();
+ let layout = BuildLayout::new(&work, work.join("out"));
+ let built = plan(
+ &detection(&[(Marker::ConfigureScript, "configure")]),
+ BuildSystem::Autotools,
+ &layout,
+ &Instrumentation::instrumented(),
+ &options(),
+ )
+ .unwrap();
+ assert_eq!(built.step(STEP_CONFIGURE).unwrap().spec.program, SH_BIN);
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/replay.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/replay.rs
new file mode 100644
index 00000000..7b631206
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/replay.rs
@@ -0,0 +1,377 @@
+// ©AngelaMos | 2026
+// replay.rs
+
+use std::path::{Path, PathBuf};
+use std::time::Duration;
+
+use lisdex_core::process::{self, CommandSpec};
+use serde::Deserialize;
+
+use crate::instrument::Instrumentation;
+
+pub const COMPILE_COMMANDS: &str = "compile_commands.json";
+pub const REPLAY_TIMEOUT_SECS: u64 = 600;
+
+const LAUNCHERS: [&str; 4] = ["ccache", "distcc", "sccache", "icecc"];
+const FRONTEND_ONLY_ARGS: [&str; 2] = ["-cc1", "-cc1as"];
+const CXX_EXTENSIONS: [&str; 6] = ["cpp", "cxx", "cc", "c++", "C", "CPP"];
+
+#[derive(Debug, thiserror::Error)]
+pub enum ReplayError {
+ #[error("io error at {path}: {source}")]
+ Io {
+ path: PathBuf,
+ source: std::io::Error,
+ },
+ #[error("{path} is not a valid compilation database: {source}")]
+ Malformed {
+ path: PathBuf,
+ source: serde_json::Error,
+ },
+ #[error("process error: {0}")]
+ Process(#[from] process::ProcessError),
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
+pub struct CompileCommand {
+ pub directory: String,
+ pub file: String,
+ #[serde(default)]
+ pub command: Option<String>,
+ #[serde(default)]
+ pub arguments: Option<Vec<String>>,
+ #[serde(default)]
+ pub output: Option<String>,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Default)]
+pub struct ReplayReport {
+ pub attempted: usize,
+ pub skipped: usize,
+ pub failures: Vec<String>,
+}
+
+impl ReplayReport {
+ #[must_use]
+ pub fn succeeded(&self) -> bool {
+ self.attempted > 0 && self.failures.is_empty()
+ }
+}
+
+#[must_use]
+pub fn tokenize(command: &str) -> Vec<String> {
+ let mut tokens = Vec::new();
+ let mut current = String::new();
+ let mut started = false;
+ let mut single = false;
+ let mut double = false;
+ let mut chars = command.chars();
+
+ while let Some(character) = chars.next() {
+ match character {
+ '\\' if !single => {
+ if let Some(escaped) = chars.next() {
+ current.push(escaped);
+ started = true;
+ }
+ }
+ '\'' if !double => {
+ single = !single;
+ started = true;
+ }
+ '"' if !single => {
+ double = !double;
+ started = true;
+ }
+ character if character.is_whitespace() && !single && !double => {
+ if started {
+ tokens.push(std::mem::take(&mut current));
+ started = false;
+ }
+ }
+ character => {
+ current.push(character);
+ started = true;
+ }
+ }
+ }
+ if started {
+ tokens.push(current);
+ }
+ tokens
+}
+
+#[must_use]
+pub fn argv(entry: &CompileCommand) -> Vec<String> {
+ match (&entry.arguments, &entry.command) {
+ (Some(arguments), _) if !arguments.is_empty() => arguments.clone(),
+ (_, Some(command)) => tokenize(command),
+ _ => Vec::new(),
+ }
+}
+
+fn strip_launchers(argv: Vec<String>) -> Vec<String> {
+ let mut argv = argv;
+ while let Some(first) = argv.first() {
+ let name = Path::new(first)
+ .file_name()
+ .map(|name| name.to_string_lossy().into_owned())
+ .unwrap_or_default();
+ if LAUNCHERS.contains(&name.as_str()) {
+ argv.remove(0);
+ } else {
+ break;
+ }
+ }
+ argv
+}
+
+#[must_use]
+pub fn is_cxx(file: &str) -> bool {
+ Path::new(file)
+ .extension()
+ .is_some_and(|ext| CXX_EXTENSIONS.iter().any(|known| *known == ext))
+}
+
+#[must_use]
+pub fn rewrite(entry: &CompileCommand, instrumentation: &Instrumentation) -> Option<CommandSpec> {
+ let argv = strip_launchers(argv(entry));
+ if argv
+ .iter()
+ .any(|arg| FRONTEND_ONLY_ARGS.contains(&arg.as_str()))
+ {
+ return None;
+ }
+ let (_, rest) = argv.split_first()?;
+ let compiler = if is_cxx(&entry.file) {
+ &instrumentation.toolchain.cxx
+ } else {
+ &instrumentation.toolchain.cc
+ };
+ Some(
+ CommandSpec::new(compiler)
+ .args(rest.to_vec())
+ .args(instrumentation.cflags.clone())
+ .cwd(&entry.directory)
+ .envs(instrumentation.envs()),
+ )
+}
+
+pub fn load(path: &Path) -> Result<Vec<CompileCommand>, ReplayError> {
+ let text = std::fs::read_to_string(path).map_err(|source| ReplayError::Io {
+ path: path.to_path_buf(),
+ source,
+ })?;
+ serde_json::from_str(&text).map_err(|source| ReplayError::Malformed {
+ path: path.to_path_buf(),
+ source,
+ })
+}
+
+pub fn replay(
+ entries: &[CompileCommand],
+ instrumentation: &Instrumentation,
+) -> Result<ReplayReport, ReplayError> {
+ let timeout = Duration::from_secs(REPLAY_TIMEOUT_SECS);
+ let mut report = ReplayReport::default();
+ for entry in entries {
+ let Some(spec) = rewrite(entry, instrumentation) else {
+ report.skipped += 1;
+ continue;
+ };
+ report.attempted += 1;
+ let execution = process::run(&spec, timeout)?;
+ if !execution.success {
+ report
+ .failures
+ .push(format!("{}: {}", entry.file, execution.tail(20)));
+ }
+ }
+ Ok(report)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn entry(command: &str, file: &str) -> CompileCommand {
+ CompileCommand {
+ directory: "/build".to_owned(),
+ file: file.to_owned(),
+ command: Some(command.to_owned()),
+ arguments: None,
+ output: None,
+ }
+ }
+
+ #[test]
+ fn tokenizer_splits_on_whitespace() {
+ assert_eq!(tokenize("cc -c png.c"), vec!["cc", "-c", "png.c"]);
+ }
+
+ #[test]
+ fn tokenizer_keeps_quoted_arguments_whole() {
+ assert_eq!(
+ tokenize(r#"cc -DNAME="lib png" -c png.c"#),
+ vec!["cc", "-DNAME=lib png", "-c", "png.c"]
+ );
+ }
+
+ #[test]
+ fn tokenizer_honours_single_quotes_and_backslashes() {
+ assert_eq!(
+ tokenize(r#"cc -DPATH='/a b/c' -DQ=\"x\" png.c"#),
+ vec!["cc", "-DPATH=/a b/c", "-DQ=\"x\"", "png.c"]
+ );
+ }
+
+ #[test]
+ fn tokenizer_preserves_empty_quoted_arguments() {
+ assert_eq!(
+ tokenize(r#"cc -DEMPTY="" png.c"#),
+ vec!["cc", "-DEMPTY=", "png.c"]
+ );
+ }
+
+ #[test]
+ fn arguments_are_preferred_over_command() {
+ let mut item = entry("cc -c wrong.c", "png.c");
+ item.arguments = Some(vec!["cc".to_owned(), "-c".to_owned(), "png.c".to_owned()]);
+ assert_eq!(argv(&item), vec!["cc", "-c", "png.c"]);
+ }
+
+ #[test]
+ fn the_compiler_is_replaced_but_every_other_argument_survives() {
+ let spec = rewrite(
+ &entry(
+ "/usr/bin/gcc -O2 -DHAVE_CONFIG_H -c png.c -o png.o",
+ "png.c",
+ ),
+ &Instrumentation::instrumented(),
+ )
+ .unwrap();
+ assert_eq!(spec.program, "afl-clang-lto");
+ assert!(spec.args.contains(&"-DHAVE_CONFIG_H".to_owned()));
+ assert!(spec.args.contains(&"-o".to_owned()));
+ assert!(spec.args.contains(&"png.o".to_owned()));
+ }
+
+ #[test]
+ fn cxx_sources_get_the_cxx_compiler() {
+ let spec = rewrite(
+ &entry("g++ -c poppler.cc -o poppler.o", "poppler.cc"),
+ &Instrumentation::instrumented(),
+ )
+ .unwrap();
+ assert_eq!(spec.program, "afl-clang-lto++");
+ }
+
+ #[test]
+ fn c_sources_get_the_c_compiler() {
+ assert!(!is_cxx("png.c"));
+ assert!(is_cxx("poppler.cpp"));
+ assert!(is_cxx("Object.cc"));
+ }
+
+ #[test]
+ fn compiler_launchers_are_stripped() {
+ let spec = rewrite(
+ &entry("/usr/bin/ccache /usr/bin/cc -c png.c", "png.c"),
+ &Instrumentation::instrumented(),
+ )
+ .unwrap();
+ assert_eq!(spec.program, "afl-clang-lto");
+ assert!(!spec.args.iter().any(|arg| arg.contains("ccache")));
+ assert!(!spec.args.iter().any(|arg| arg.ends_with("/cc")));
+ }
+
+ #[test]
+ fn instrumentation_flags_are_appended_after_the_original_arguments() {
+ let spec = rewrite(
+ &entry("cc -O0 -c png.c", "png.c"),
+ &Instrumentation::instrumented(),
+ )
+ .unwrap();
+ let original = spec.args.iter().position(|arg| arg == "-O0").unwrap();
+ let ours = spec.args.iter().position(|arg| arg == "-O2").unwrap();
+ assert!(ours > original);
+ }
+
+ #[test]
+ fn the_original_working_directory_is_preserved() {
+ let spec = rewrite(
+ &entry("cc -c png.c", "png.c"),
+ &Instrumentation::instrumented(),
+ )
+ .unwrap();
+ assert_eq!(spec.cwd, Some(PathBuf::from("/build")));
+ }
+
+ #[test]
+ fn sanitizer_environment_reaches_every_replayed_command() {
+ let spec = rewrite(
+ &entry("cc -c png.c", "png.c"),
+ &Instrumentation::instrumented(),
+ )
+ .unwrap();
+ assert!(spec.has_env(crate::instrument::ENV_AFL_USE_ASAN));
+ }
+
+ #[test]
+ fn intercepted_frontend_invocations_are_skipped_not_replayed() {
+ let item = entry(
+ "/usr/lib/llvm-20/bin/clang -cc1 -triple x86_64 -emit-obj -o x.o x.c",
+ "x.c",
+ );
+ assert!(
+ rewrite(&item, &Instrumentation::instrumented()).is_none(),
+ "a driver cannot replay clang's internal -cc1 invocation"
+ );
+ }
+
+ #[test]
+ fn an_empty_command_is_skipped_rather_than_run() {
+ let item = CompileCommand {
+ directory: "/build".to_owned(),
+ file: "png.c".to_owned(),
+ command: Some(String::new()),
+ arguments: None,
+ output: None,
+ };
+ assert!(rewrite(&item, &Instrumentation::instrumented()).is_none());
+ }
+
+ #[test]
+ fn a_real_compilation_database_round_trips() {
+ let path = std::env::temp_dir().join(format!("lisdex-ccdb-{}.json", std::process::id()));
+ std::fs::write(
+ &path,
+ r#"[{"directory":"/build","command":"cc -c /src/png.c -o png.o","file":"/src/png.c"},
+ {"directory":"/build","arguments":["cc","-c","/src/pngread.c"],"file":"/src/pngread.c","output":"pngread.o"}]"#,
+ )
+ .unwrap();
+ let entries = load(&path).unwrap();
+ assert_eq!(entries.len(), 2);
+ assert_eq!(entries[1].output.as_deref(), Some("pngread.o"));
+ assert_eq!(argv(&entries[1])[0], "cc");
+ }
+
+ #[test]
+ fn a_malformed_database_is_reported_not_ignored() {
+ let path =
+ std::env::temp_dir().join(format!("lisdex-ccdb-bad-{}.json", std::process::id()));
+ std::fs::write(&path, "{ not json").unwrap();
+ assert!(matches!(load(&path), Err(ReplayError::Malformed { .. })));
+ }
+
+ #[test]
+ fn a_report_with_no_attempts_is_not_a_success() {
+ assert!(!ReplayReport::default().succeeded());
+ let report = ReplayReport {
+ attempted: 3,
+ skipped: 0,
+ failures: Vec::new(),
+ };
+ assert!(report.succeeded());
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/shadow.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/shadow.rs
new file mode 100644
index 00000000..de1fa4dd
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/shadow.rs
@@ -0,0 +1,267 @@
+// ©AngelaMos | 2026
+// shadow.rs
+
+use std::path::{Path, PathBuf};
+
+use crate::instrument::{Instrumentation, Toolchain};
+
+pub const SHADOWED_C: [&str; 3] = ["cc", "gcc", "clang"];
+pub const SHADOWED_CXX: [&str; 3] = ["c++", "g++", "clang++"];
+pub const SHADOWED_AR: &str = "ar";
+pub const SHADOWED_RANLIB: &str = "ranlib";
+pub const SHADOWED_NM: &str = "nm";
+pub const ENV_PATH: &str = "PATH";
+pub const ENV_AFL_CC: &str = "AFL_CC";
+pub const ENV_AFL_CXX: &str = "AFL_CXX";
+pub const REAL_CLANG: &str = "/usr/lib/llvm-20/bin/clang";
+pub const REAL_CLANGXX: &str = "/usr/lib/llvm-20/bin/clang++";
+
+const SCRIPT_MODE: u32 = 0o755;
+
+#[derive(Debug, thiserror::Error)]
+pub enum ShadowError {
+ #[error("io error at {path}: {source}")]
+ Io {
+ path: PathBuf,
+ source: std::io::Error,
+ },
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ShadowScript {
+ pub path: PathBuf,
+ pub contents: String,
+}
+
+#[must_use]
+pub fn shell_quote(value: &str) -> String {
+ format!("'{}'", value.replace('\'', r"'\''"))
+}
+
+#[must_use]
+pub fn script_body(name: &str, compiler: &str, flags: &[String]) -> String {
+ let mut body = format!(
+ "#!/bin/sh\n# ©AngelaMos | 2026\n# {name}\n\nexec {} \"$@\"",
+ shell_quote(compiler)
+ );
+ for flag in flags {
+ body.push(' ');
+ body.push_str(&shell_quote(flag));
+ }
+ body.push('\n');
+ body
+}
+
+#[must_use]
+pub fn scripts(dir: &Path, instrumentation: &Instrumentation) -> Vec<ShadowScript> {
+ let toolchain = &instrumentation.toolchain;
+ let flags = &instrumentation.cflags;
+ let mut scripts = Vec::new();
+ for name in SHADOWED_C {
+ scripts.push(ShadowScript {
+ path: dir.join(name),
+ contents: script_body(name, &toolchain.cc, flags),
+ });
+ }
+ for name in SHADOWED_CXX {
+ scripts.push(ShadowScript {
+ path: dir.join(name),
+ contents: script_body(name, &toolchain.cxx, flags),
+ });
+ }
+ for (name, tool) in [
+ (SHADOWED_AR, &toolchain.ar),
+ (SHADOWED_RANLIB, &toolchain.ranlib),
+ (SHADOWED_NM, &toolchain.nm),
+ ] {
+ scripts.push(ShadowScript {
+ path: dir.join(name),
+ contents: script_body(name, tool, &[]),
+ });
+ }
+ scripts
+}
+
+pub fn install(dir: &Path, instrumentation: &Instrumentation) -> Result<(), ShadowError> {
+ use std::os::unix::fs::PermissionsExt;
+
+ std::fs::create_dir_all(dir).map_err(|source| ShadowError::Io {
+ path: dir.to_path_buf(),
+ source,
+ })?;
+ for script in scripts(dir, instrumentation) {
+ std::fs::write(&script.path, &script.contents).map_err(|source| ShadowError::Io {
+ path: script.path.clone(),
+ source,
+ })?;
+ let mut perms = std::fs::metadata(&script.path)
+ .map_err(|source| ShadowError::Io {
+ path: script.path.clone(),
+ source,
+ })?
+ .permissions();
+ perms.set_mode(SCRIPT_MODE);
+ std::fs::set_permissions(&script.path, perms).map_err(|source| ShadowError::Io {
+ path: script.path.clone(),
+ source,
+ })?;
+ }
+ Ok(())
+}
+
+#[must_use]
+pub fn path_value(dir: &Path, current: Option<&str>) -> String {
+ match current {
+ Some(existing) if !existing.is_empty() => format!("{}:{existing}", dir.display()),
+ _ => dir.display().to_string(),
+ }
+}
+
+#[must_use]
+pub fn shadowed(instrumentation: &Instrumentation, dir: &Path) -> Instrumentation {
+ let toolchain = Toolchain {
+ cc: dir.join(SHADOWED_C[0]).display().to_string(),
+ cxx: dir.join(SHADOWED_CXX[0]).display().to_string(),
+ ..instrumentation.toolchain.clone()
+ };
+ Instrumentation {
+ toolchain,
+ ..instrumentation.clone()
+ }
+}
+
+#[must_use]
+pub fn recursion_guard_envs() -> Vec<(String, String)> {
+ vec![
+ (ENV_AFL_CC.to_owned(), REAL_CLANG.to_owned()),
+ (ENV_AFL_CXX.to_owned(), REAL_CLANGXX.to_owned()),
+ ]
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn scratch(name: &str) -> PathBuf {
+ let dir = std::env::temp_dir().join(format!("lisdex-shadow-{}-{name}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ dir
+ }
+
+ #[test]
+ fn every_compiler_name_a_makefile_might_hardcode_is_shadowed() {
+ let generated = scripts(Path::new("/w/shadow"), &Instrumentation::instrumented());
+ let names: Vec<String> = generated
+ .iter()
+ .map(|script| {
+ script
+ .path
+ .file_name()
+ .unwrap()
+ .to_string_lossy()
+ .into_owned()
+ })
+ .collect();
+ for expected in ["cc", "gcc", "clang", "c++", "g++", "clang++"] {
+ assert!(names.contains(&expected.to_owned()), "missing {expected}");
+ }
+ }
+
+ #[test]
+ fn the_binutils_are_shadowed_so_makefiles_that_bake_flags_into_ar_still_work() {
+ let generated = scripts(Path::new("/w/shadow"), &Instrumentation::instrumented());
+ let ar = generated
+ .iter()
+ .find(|script| script.path.ends_with(SHADOWED_AR))
+ .unwrap();
+ assert!(ar.contents.contains("exec 'llvm-ar' \"$@\""));
+ assert!(
+ !ar.contents.contains("-fno-omit-frame-pointer"),
+ "compiler flags must never be appended to an archiver invocation"
+ );
+ for tool in [SHADOWED_RANLIB, SHADOWED_NM] {
+ assert!(generated.iter().any(|script| script.path.ends_with(tool)));
+ }
+ }
+
+ #[test]
+ fn the_c_preprocessor_is_never_shadowed_with_a_cxx_compiler() {
+ let generated = scripts(Path::new("/w/shadow"), &Instrumentation::instrumented());
+ assert!(!generated.iter().any(|script| script.path.ends_with("cpp")));
+ }
+
+ #[test]
+ fn c_wrappers_call_the_c_compiler_and_cxx_wrappers_the_cxx_compiler() {
+ let generated = scripts(Path::new("/w/shadow"), &Instrumentation::instrumented());
+ let gcc = generated
+ .iter()
+ .find(|script| script.path.ends_with("gcc"))
+ .unwrap();
+ let gxx = generated
+ .iter()
+ .find(|script| script.path.ends_with("g++"))
+ .unwrap();
+ assert!(gcc.contents.contains("'afl-clang-lto' \"$@\""));
+ assert!(gxx.contents.contains("'afl-clang-lto++' \"$@\""));
+ }
+
+ #[test]
+ fn injected_flags_land_after_the_projects_own_arguments_so_they_win() {
+ let instrumentation = Instrumentation::instrumented().with_extra_cflags(["-DLISDEX=1"]);
+ let body = script_body("gcc", "afl-clang-lto", &instrumentation.cflags);
+ let args = body.find("\"$@\"").unwrap();
+ let ours = body.find("-DLISDEX=1").unwrap();
+ assert!(ours > args);
+ }
+
+ #[test]
+ fn scripts_are_written_executable() {
+ use std::os::unix::fs::PermissionsExt;
+ let dir = scratch("install");
+ install(&dir, &Instrumentation::instrumented()).unwrap();
+ let mode = std::fs::metadata(dir.join("gcc"))
+ .unwrap()
+ .permissions()
+ .mode();
+ assert_eq!(mode & 0o777, SCRIPT_MODE);
+ }
+
+ #[test]
+ fn the_shadow_directory_is_prepended_to_path() {
+ let value = path_value(Path::new("/w/shadow"), Some("/usr/bin:/bin"));
+ assert_eq!(value, "/w/shadow:/usr/bin:/bin");
+ assert_eq!(path_value(Path::new("/w/shadow"), None), "/w/shadow");
+ }
+
+ #[test]
+ fn a_shadowed_instrumentation_points_the_compilers_at_the_wrappers() {
+ let shadowed = shadowed(&Instrumentation::instrumented(), Path::new("/w/shadow"));
+ assert_eq!(shadowed.toolchain.cc, "/w/shadow/cc");
+ assert_eq!(shadowed.toolchain.cxx, "/w/shadow/c++");
+ assert_eq!(shadowed.toolchain.ar, "llvm-ar");
+ }
+
+ #[test]
+ fn the_recursion_guard_pins_the_underlying_compiler_by_absolute_path() {
+ let envs = recursion_guard_envs();
+ assert!(
+ envs.iter()
+ .all(|(_, value)| value.starts_with('/') && !value.contains("shadow"))
+ );
+ }
+
+ #[test]
+ fn a_shadow_script_runs_and_forwards_its_arguments() {
+ let dir = scratch("run");
+ let instrumentation = Instrumentation::instrumented()
+ .with_toolchain(Toolchain::default().with_compilers("echo", "echo"));
+ install(&dir, &instrumentation).unwrap();
+ let spec = lisdex_core::process::CommandSpec::new(dir.join("gcc").display().to_string())
+ .arg("-c")
+ .arg("png.c");
+ let run = lisdex_core::process::run(&spec, std::time::Duration::from_secs(10)).unwrap();
+ assert!(run.success);
+ assert!(run.output.contains("-c png.c"));
+ assert!(run.output.contains("-fno-omit-frame-pointer"));
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/verify.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/verify.rs
new file mode 100644
index 00000000..24ea3e66
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/verify.rs
@@ -0,0 +1,457 @@
+// ©AngelaMos | 2026
+// verify.rs
+
+use std::path::{Path, PathBuf};
+use std::time::Duration;
+
+use lisdex_core::process::{self, CommandSpec};
+
+use crate::instrument::{LLVM_NM, SanitizerSet};
+
+pub const NM_TIMEOUT_SECS: u64 = 120;
+
+const PATTERN_ASAN: &str = "__asan_";
+const PATTERN_UBSAN: &str = "__ubsan_";
+const PATTERN_MSAN: &str = "__msan_";
+const PATTERN_AFL: &str = "__afl_";
+const PATTERN_COVERAGE: &str = "__sanitizer_cov";
+const PATTERN_CMPLOG: &str = "__cmplog";
+const PATTERN_MAGMA: &str = "magma_log";
+const MEMBER_HEADER_SUFFIX: char = ':';
+
+const ARG_NO_SORT: &str = "--no-sort";
+
+#[derive(Debug, thiserror::Error)]
+pub enum VerifyError {
+ #[error("process error: {0}")]
+ Process(#[from] process::ProcessError),
+ #[error("{nm} could not read {path}: {output}")]
+ Unreadable {
+ nm: String,
+ path: PathBuf,
+ output: String,
+ },
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct SymbolCounts {
+ pub asan: usize,
+ pub ubsan: usize,
+ pub msan: usize,
+ pub afl: usize,
+ pub cmplog: usize,
+ pub magma: usize,
+}
+
+impl SymbolCounts {
+ #[must_use]
+ pub fn merge(self, other: SymbolCounts) -> SymbolCounts {
+ SymbolCounts {
+ asan: self.asan + other.asan,
+ ubsan: self.ubsan + other.ubsan,
+ msan: self.msan + other.msan,
+ afl: self.afl + other.afl,
+ cmplog: self.cmplog + other.cmplog,
+ magma: self.magma + other.magma,
+ }
+ }
+
+ #[must_use]
+ pub fn is_empty(self) -> bool {
+ self == SymbolCounts::default()
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Requirements {
+ pub sanitizers: SanitizerSet,
+ pub min_asan: usize,
+ pub min_ubsan: usize,
+ pub min_msan: usize,
+ pub min_coverage: usize,
+ pub min_cmplog: usize,
+ pub min_magma: usize,
+}
+
+impl Default for Requirements {
+ fn default() -> Self {
+ Self {
+ sanitizers: SanitizerSet::AddressUndefined,
+ min_asan: 1,
+ min_ubsan: 0,
+ min_msan: 0,
+ min_coverage: 1,
+ min_cmplog: 0,
+ min_magma: 0,
+ }
+ }
+}
+
+impl Requirements {
+ #[must_use]
+ pub fn library() -> Self {
+ Self::default()
+ }
+
+ #[must_use]
+ pub fn for_sanitizers(sanitizers: SanitizerSet) -> Self {
+ match sanitizers {
+ SanitizerSet::AddressUndefined => Self::default(),
+ SanitizerSet::Memory => Self {
+ sanitizers,
+ min_asan: 0,
+ min_msan: 1,
+ ..Self::default()
+ },
+ SanitizerSet::Off => Self {
+ sanitizers,
+ min_asan: 0,
+ min_ubsan: 0,
+ min_msan: 0,
+ min_coverage: 0,
+ ..Self::default()
+ },
+ }
+ }
+
+ #[must_use]
+ pub fn with_cmplog(mut self, required: bool) -> Self {
+ self.min_cmplog = usize::from(required);
+ self
+ }
+
+ #[must_use]
+ pub fn with_magma_canaries(mut self, required: bool) -> Self {
+ self.min_magma = usize::from(required);
+ self
+ }
+
+ #[must_use]
+ pub fn shortfalls(&self, counts: SymbolCounts, lto_bitcode: bool) -> Vec<String> {
+ let coverage_required = if lto_bitcode { 0 } else { self.min_coverage };
+ let checks = [
+ ("address sanitizer", counts.asan, self.min_asan),
+ ("undefined sanitizer", counts.ubsan, self.min_ubsan),
+ ("memory sanitizer", counts.msan, self.min_msan),
+ (
+ "afl coverage",
+ counts.afl + counts.cmplog,
+ coverage_required,
+ ),
+ ("cmplog", counts.cmplog, self.min_cmplog),
+ ("magma canary", counts.magma, self.min_magma),
+ ];
+ checks
+ .into_iter()
+ .filter(|(_, found, required)| found < required)
+ .map(|(what, found, required)| {
+ format!("{what} symbols: found {found}, need at least {required}")
+ })
+ .collect()
+ }
+}
+
+#[must_use]
+pub fn scan_symbols(text: &str) -> SymbolCounts {
+ let mut counts = SymbolCounts::default();
+ for line in text.lines() {
+ let trimmed = line.trim_end();
+ if trimmed.ends_with(MEMBER_HEADER_SUFFIX) {
+ continue;
+ }
+ let Some(symbol) = trimmed.split_whitespace().last() else {
+ continue;
+ };
+ if symbol.contains(PATTERN_ASAN) {
+ counts.asan += 1;
+ }
+ if symbol.contains(PATTERN_UBSAN) {
+ counts.ubsan += 1;
+ }
+ if symbol.contains(PATTERN_MSAN) {
+ counts.msan += 1;
+ }
+ if symbol.contains(PATTERN_AFL) || symbol.contains(PATTERN_COVERAGE) {
+ counts.afl += 1;
+ }
+ if symbol.contains(PATTERN_CMPLOG) {
+ counts.cmplog += 1;
+ }
+ if symbol.contains(PATTERN_MAGMA) {
+ counts.magma += 1;
+ }
+ }
+ counts
+}
+
+#[must_use]
+pub fn nm_spec(path: &Path) -> CommandSpec {
+ CommandSpec::new(LLVM_NM).arg(ARG_NO_SORT).path_arg(path)
+}
+
+pub fn inspect(path: &Path) -> Result<SymbolCounts, VerifyError> {
+ let execution = process::run(&nm_spec(path), Duration::from_secs(NM_TIMEOUT_SECS))?;
+ if !execution.success {
+ return Err(VerifyError::Unreadable {
+ nm: LLVM_NM.to_owned(),
+ path: path.to_path_buf(),
+ output: execution.tail(10),
+ });
+ }
+ Ok(scan_symbols(&execution.output))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const NM_INSTRUMENTED: &str = "\
+00000000001c6370 t _Z19ubsan_GetStackTracePN11__sanitizer18BufferedStackTraceEmmmPvb
+0000000000ea0870 b _ZL13ubsan_init_mu
+0000000000185bd0 t _ZL17asan_thread_startPv
+---------------- C ___asan_globals_registered
+ U __asan_after_dynamic_init
+ U __asan_report_load4
+ U __ubsan_handle_out_of_bounds
+ U __ubsan_handle_shift_out_of_bounds
+ U __sanitizer_cov_trace_pc_guard
+0000000000ea1000 B __afl_area_ptr
+ U magma_log
+";
+
+ const NM_PLAIN: &str = "\
+0000000000001139 T png_read_info
+ U memcpy
+0000000000004010 B png_error_ptr
+";
+
+ const NM_CMPLOG: &str = "\
+ U __cmplog_ins_hook4
+ U __cmplog_rtn_hook
+ U __sanitizer_cov_trace_pc_guard
+ U __asan_report_load1
+ U __ubsan_handle_out_of_bounds
+";
+
+ const NM_ARCHIVE: &str = "\
+libpng16.a[png.o]:
+ U __asan_report_load4
+ U __ubsan_handle_shift_out_of_bounds
+0000000000000000 T png_get_io_ptr
+libpng16.a[pngread.o]:
+ U __afl_area_ptr
+";
+
+ const NM_RUNTIME_INTERNALS: &str = "\
+00000000001c6370 t _Z19ubsan_GetStackTracePN11__sanitizer18BufferedStackTraceEmmmPvb
+0000000000ea0870 b _ZL13ubsan_init_mu
+0000000000185bd0 t _ZL17asan_thread_startPv
+0000000000001139 T asan_helper
+";
+
+ const NM_FILENAME_ONLY: &str = "\
+magma_log.o:
+0000000000000000 T nothing_instrumented
+asan_shim.o:
+0000000000000000 T also_nothing
+";
+
+ #[test]
+ fn an_instrumented_binary_reports_every_family() {
+ let counts = scan_symbols(NM_INSTRUMENTED);
+ assert!(counts.asan >= 3);
+ assert!(counts.ubsan >= 1);
+ assert!(counts.afl >= 2);
+ assert_eq!(counts.magma, 1);
+ assert_eq!(counts.cmplog, 0);
+ }
+
+ #[test]
+ fn object_file_names_are_never_counted_as_instrumentation() {
+ let counts = scan_symbols(NM_FILENAME_ONLY);
+ assert!(
+ counts.is_empty(),
+ "a translation unit named magma_log.c must not satisfy --require-canaries: {counts:?}"
+ );
+ assert!(
+ Requirements::library()
+ .with_magma_canaries(true)
+ .shortfalls(counts, false)
+ .len()
+ >= 2
+ );
+ }
+
+ #[test]
+ fn sanitizer_runtime_internals_are_not_evidence_that_project_code_is_instrumented() {
+ let counts = scan_symbols(NM_RUNTIME_INTERNALS);
+ assert!(
+ counts.is_empty(),
+ "only the __asan_/__ubsan_/__msan_ interface symbols count: {counts:?}"
+ );
+ }
+
+ #[test]
+ fn a_plain_build_reports_nothing() {
+ let counts = scan_symbols(NM_PLAIN);
+ assert!(counts.is_empty());
+ }
+
+ #[test]
+ fn ubsan_symbols_are_not_miscounted_as_asan() {
+ let counts = scan_symbols(" U __ubsan_handle_add_overflow\n");
+ assert_eq!(counts.ubsan, 1);
+ assert_eq!(counts.asan, 0);
+ }
+
+ #[test]
+ fn cmplog_symbols_are_counted_separately_and_also_prove_coverage() {
+ let counts = scan_symbols(NM_CMPLOG);
+ assert_eq!(counts.cmplog, 2);
+ assert!(counts.afl >= 1);
+ }
+
+ #[test]
+ fn archive_member_headers_do_not_become_symbols() {
+ let counts = scan_symbols(NM_ARCHIVE);
+ assert_eq!(counts.asan, 1);
+ assert_eq!(counts.ubsan, 1);
+ assert_eq!(counts.afl, 1);
+ }
+
+ #[test]
+ fn empty_output_is_empty_counts() {
+ assert!(scan_symbols("").is_empty());
+ }
+
+ #[test]
+ fn counts_merge_across_artifacts() {
+ let merged = scan_symbols(NM_ARCHIVE).merge(scan_symbols(NM_CMPLOG));
+ assert_eq!(merged.asan, 2);
+ assert_eq!(merged.cmplog, 2);
+ }
+
+ #[test]
+ fn an_uninstrumented_library_fails_the_requirements_loudly() {
+ let shortfalls = Requirements::library().shortfalls(scan_symbols(NM_PLAIN), false);
+ assert_eq!(shortfalls.len(), 2);
+ assert!(shortfalls.iter().any(|line| line.contains("address")));
+ assert!(shortfalls.iter().any(|line| line.contains("afl coverage")));
+ }
+
+ #[test]
+ fn a_library_without_undefined_behaviour_checks_is_still_accepted() {
+ let counts = scan_symbols(
+ "libtiny.a[tiny.o]:\n U __asan_report_load4\n U __afl_area_ptr\n",
+ );
+ assert_eq!(counts.ubsan, 0);
+ assert!(
+ Requirements::library().shortfalls(counts, false).is_empty(),
+ "a translation unit with no UB-checkable operation emits no __ubsan_ references"
+ );
+ }
+
+ #[test]
+ fn an_instrumented_library_satisfies_the_requirements() {
+ assert!(
+ Requirements::library()
+ .shortfalls(scan_symbols(NM_ARCHIVE), false)
+ .is_empty()
+ );
+ }
+
+ #[test]
+ fn a_cmplog_build_missing_cmplog_instrumentation_is_rejected() {
+ let shortfalls = Requirements::library()
+ .with_cmplog(true)
+ .shortfalls(scan_symbols(NM_ARCHIVE), false);
+ assert_eq!(shortfalls.len(), 1);
+ assert!(shortfalls[0].contains("cmplog"));
+ }
+
+ #[test]
+ fn a_memory_sanitizer_build_does_not_demand_address_symbols() {
+ let requirements = Requirements::for_sanitizers(SanitizerSet::Memory);
+ let counts =
+ scan_symbols(" U __msan_warning\n U __afl_area_ptr\n");
+ assert!(requirements.shortfalls(counts, false).is_empty());
+ }
+
+ #[test]
+ fn magma_canaries_can_be_demanded_for_benchmark_targets() {
+ let shortfalls = Requirements::library()
+ .with_magma_canaries(true)
+ .shortfalls(scan_symbols(NM_ARCHIVE), false);
+ assert_eq!(shortfalls.len(), 1);
+ assert!(shortfalls[0].contains("magma"));
+ }
+
+ #[test]
+ fn an_lto_archive_satisfies_coverage_because_instrumentation_lands_at_link_time() {
+ let lto_only = scan_symbols(
+ "libtiff.a[tif_error.o]:\n U __asan_report_load4\n U __ubsan_handle_shift_out_of_bounds\n",
+ );
+ assert_eq!(lto_only.afl, 0);
+ assert!(
+ !Requirements::library()
+ .shortfalls(lto_only, false)
+ .is_empty()
+ );
+ assert!(
+ Requirements::library()
+ .shortfalls(lto_only, true)
+ .is_empty(),
+ "afl-clang-lto emits bitcode archives; coverage symbols only exist after the link"
+ );
+ }
+
+ #[test]
+ fn an_lto_archive_still_has_to_carry_sanitizer_instrumentation() {
+ let shortfalls = Requirements::library().shortfalls(scan_symbols(NM_PLAIN), true);
+ assert_eq!(shortfalls.len(), 1);
+ assert!(shortfalls.iter().any(|line| line.contains("address")));
+ assert!(!shortfalls.iter().any(|line| line.contains("coverage")));
+ }
+
+ #[test]
+ fn cmplog_is_never_waived_by_bitcode_because_it_is_a_compile_time_pass() {
+ let counts = scan_symbols(NM_ARCHIVE);
+ assert_eq!(counts.cmplog, 0);
+ let shortfalls = Requirements::library()
+ .with_cmplog(true)
+ .shortfalls(counts, true);
+ assert_eq!(
+ shortfalls.len(),
+ 1,
+ "afl-cc loads cmplog through -fpass-plugin, so the symbols are in the objects"
+ );
+ assert!(shortfalls[0].contains("cmplog"));
+ }
+
+ #[test]
+ fn nm_is_invoked_without_sorting_because_only_counts_matter() {
+ let spec = nm_spec(Path::new("/out/libpng16.a"));
+ assert_eq!(spec.program, LLVM_NM);
+ assert_eq!(spec.args, vec![ARG_NO_SORT, "/out/libpng16.a"]);
+ }
+
+ #[test]
+ fn inspecting_a_file_that_is_not_an_object_never_reports_instrumentation() {
+ let path = std::env::temp_dir().join(format!("lisdex-verify-{}.txt", std::process::id()));
+ std::fs::write(&path, "not an object file").unwrap();
+ match inspect(&path) {
+ Ok(counts) => panic!("a text file reported symbols: {counts:?}"),
+ Err(VerifyError::Unreadable { .. }) => {}
+ Err(VerifyError::Process(_)) => {
+ assert!(
+ process::run(
+ &CommandSpec::new(LLVM_NM).arg("--version"),
+ Duration::from_secs(10)
+ )
+ .is_err(),
+ "{LLVM_NM} is present, so an unreadable file must be reported as such"
+ );
+ }
+ }
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/lib.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/lib.rs
index 7ba84b2b..73470241 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/lib.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/lib.rs
@@ -4,5 +4,6 @@
pub mod campaign;
pub mod config;
pub mod crash;
+pub mod process;
pub mod report;
pub mod store;
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/process.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/process.rs
new file mode 100644
index 00000000..6080dc22
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/process.rs
@@ -0,0 +1,410 @@
+// ©AngelaMos | 2026
+// process.rs
+
+use std::io::Read;
+use std::path::{Path, PathBuf};
+use std::process::{Command, Stdio};
+use std::time::{Duration, Instant};
+
+pub const MAX_CAPTURED_BYTES: usize = 512 * 1024;
+pub const DEFAULT_STEP_TIMEOUT_SECS: u64 = 1800;
+
+const POLL_MS: u64 = 25;
+const ENV_PATH: &str = "PATH";
+const EXECUTABLE_BITS: u32 = 0o111;
+const DRAIN_CHUNK_BYTES: usize = 16 * 1024;
+const DRAIN_GRACE_MS: u64 = 2000;
+const KILL_BIN: &str = "kill";
+const SIGNAL_KILL: &str = "-KILL";
+const SCRUBBED_PREFIXES: [&str; 4] = ["AFL_", "ASAN_", "UBSAN_", "MSAN_"];
+const SCRUBBED_NAMES: [&str; 8] = [
+ "CC", "CXX", "AR", "RANLIB", "NM", "AS", "CFLAGS", "CXXFLAGS",
+];
+const STREAM_SEPARATOR: &str = "\n--- stderr ---\n";
+const TRUNCATION_NOTICE: &str = "\n[lisdex] output truncated\n";
+
+#[derive(Debug, thiserror::Error)]
+pub enum ProcessError {
+ #[error("could not launch {program}: {source}")]
+ Launch {
+ program: String,
+ source: std::io::Error,
+ },
+ #[error("io error while running {program}: {source}")]
+ Io {
+ program: String,
+ source: std::io::Error,
+ },
+ #[error("output reader thread for {program} panicked")]
+ ReaderPanicked { program: String },
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Default)]
+pub struct CommandSpec {
+ pub program: String,
+ pub args: Vec<String>,
+ pub cwd: Option<PathBuf>,
+ pub envs: Vec<(String, String)>,
+}
+
+impl CommandSpec {
+ #[must_use]
+ pub fn new(program: impl Into<String>) -> Self {
+ Self {
+ program: program.into(),
+ args: Vec::new(),
+ cwd: None,
+ envs: Vec::new(),
+ }
+ }
+
+ #[must_use]
+ pub fn arg(mut self, arg: impl Into<String>) -> Self {
+ self.args.push(arg.into());
+ self
+ }
+
+ #[must_use]
+ pub fn args<I, S>(mut self, args: I) -> Self
+ where
+ I: IntoIterator<Item = S>,
+ S: Into<String>,
+ {
+ self.args.extend(args.into_iter().map(Into::into));
+ self
+ }
+
+ #[must_use]
+ pub fn path_arg(self, arg: &Path) -> Self {
+ self.arg(arg.display().to_string())
+ }
+
+ #[must_use]
+ pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
+ self.cwd = Some(cwd.into());
+ self
+ }
+
+ #[must_use]
+ pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
+ self.envs.push((key.into(), value.into()));
+ self
+ }
+
+ #[must_use]
+ pub fn envs<I, K, V>(mut self, envs: I) -> Self
+ where
+ I: IntoIterator<Item = (K, V)>,
+ K: Into<String>,
+ V: Into<String>,
+ {
+ self.envs
+ .extend(envs.into_iter().map(|(k, v)| (k.into(), v.into())));
+ self
+ }
+
+ #[must_use]
+ pub fn has_env(&self, key: &str) -> bool {
+ self.envs.iter().any(|(name, _)| name == key)
+ }
+
+ #[must_use]
+ pub fn env_value(&self, key: &str) -> Option<&str> {
+ self.envs
+ .iter()
+ .find(|(name, _)| name == key)
+ .map(|(_, value)| value.as_str())
+ }
+
+ #[must_use]
+ pub fn command_line(&self) -> String {
+ let mut line = self.program.clone();
+ for arg in &self.args {
+ line.push(' ');
+ line.push_str(arg);
+ }
+ line
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Execution {
+ pub command_line: String,
+ pub code: Option<i32>,
+ pub success: bool,
+ pub timed_out: bool,
+ pub truncated: bool,
+ pub output: String,
+ pub duration: Duration,
+}
+
+impl Execution {
+ #[must_use]
+ pub fn tail(&self, lines: usize) -> String {
+ let collected: Vec<&str> = self.output.lines().collect();
+ let start = collected.len().saturating_sub(lines);
+ collected[start..].join("\n")
+ }
+}
+
+fn is_executable(path: &Path) -> bool {
+ use std::os::unix::fs::PermissionsExt;
+ std::fs::metadata(path)
+ .is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & EXECUTABLE_BITS != 0)
+}
+
+#[must_use]
+pub fn resolve_on_path(program: &str) -> Option<PathBuf> {
+ if program.contains('/') {
+ let path = PathBuf::from(program);
+ return is_executable(&path).then_some(path);
+ }
+ let paths = std::env::var_os(ENV_PATH)?;
+ std::env::split_paths(&paths).find_map(|dir| {
+ let candidate = dir.join(program);
+ is_executable(&candidate).then_some(candidate)
+ })
+}
+
+type Captured = std::sync::Arc<std::sync::Mutex<Vec<u8>>>;
+
+fn drain(mut stream: impl Read + Send + 'static) -> (Captured, std::thread::JoinHandle<()>) {
+ let captured: Captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
+ let sink = std::sync::Arc::clone(&captured);
+ let handle = std::thread::spawn(move || {
+ let mut chunk = [0u8; DRAIN_CHUNK_BYTES];
+ loop {
+ match stream.read(&mut chunk) {
+ Ok(0) | Err(_) => break,
+ Ok(read) => {
+ let Ok(mut buffer) = sink.lock() else { break };
+ let room = MAX_CAPTURED_BYTES.saturating_sub(buffer.len());
+ if room > 0 {
+ buffer.extend_from_slice(&chunk[..read.min(room)]);
+ }
+ }
+ }
+ }
+ });
+ (captured, handle)
+}
+
+fn take_captured(captured: &Captured) -> Vec<u8> {
+ captured
+ .lock()
+ .map(|buffer| buffer.clone())
+ .unwrap_or_default()
+}
+
+#[must_use]
+pub fn scrubbed_names(spec: &CommandSpec) -> Vec<String> {
+ std::env::vars()
+ .map(|(name, _)| name)
+ .filter(|name| {
+ SCRUBBED_PREFIXES
+ .iter()
+ .any(|prefix| name.starts_with(prefix))
+ || SCRUBBED_NAMES.contains(&name.as_str())
+ })
+ .filter(|name| !spec.has_env(name))
+ .collect()
+}
+
+fn terminate(child: &mut std::process::Child) {
+ let group = format!("-{}", child.id());
+ let _ = Command::new(KILL_BIN)
+ .arg(SIGNAL_KILL)
+ .arg("--")
+ .arg(&group)
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .status();
+ let _ = child.kill();
+}
+
+pub fn run(spec: &CommandSpec, timeout: Duration) -> Result<Execution, ProcessError> {
+ use std::os::unix::process::CommandExt;
+
+ let started = Instant::now();
+ let mut command = Command::new(&spec.program);
+ command.args(&spec.args);
+ if let Some(cwd) = &spec.cwd {
+ command.current_dir(cwd);
+ }
+ for name in scrubbed_names(spec) {
+ command.env_remove(name);
+ }
+ for (key, value) in &spec.envs {
+ command.env(key, value);
+ }
+
+ let mut child = command
+ .process_group(0)
+ .stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()
+ .map_err(|source| ProcessError::Launch {
+ program: spec.program.clone(),
+ source,
+ })?;
+
+ let stdout = child.stdout.take().map(drain);
+ let stderr = child.stderr.take().map(drain);
+
+ let mut timed_out = false;
+ let status = loop {
+ if let Some(status) = child.try_wait().map_err(|source| ProcessError::Io {
+ program: spec.program.clone(),
+ source,
+ })? {
+ break status;
+ }
+ if started.elapsed() >= timeout {
+ terminate(&mut child);
+ timed_out = true;
+ break child.wait().map_err(|source| ProcessError::Io {
+ program: spec.program.clone(),
+ source,
+ })?;
+ }
+ std::thread::sleep(Duration::from_millis(POLL_MS));
+ };
+
+ let deadline = Instant::now() + Duration::from_millis(DRAIN_GRACE_MS);
+ let mut captured = Vec::new();
+ let mut truncated = false;
+ for (drained, separator) in [(stdout, ""), (stderr, STREAM_SEPARATOR)] {
+ let Some((buffer, handle)) = drained else {
+ continue;
+ };
+ while !handle.is_finished() && Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(POLL_MS));
+ }
+ let bytes = take_captured(&buffer);
+ if bytes.is_empty() {
+ continue;
+ }
+ truncated = truncated || bytes.len() >= MAX_CAPTURED_BYTES;
+ captured.extend_from_slice(separator.as_bytes());
+ captured.extend_from_slice(&bytes);
+ }
+
+ let mut output = String::from_utf8_lossy(&captured).into_owned();
+ if truncated {
+ output.push_str(TRUNCATION_NOTICE);
+ }
+
+ Ok(Execution {
+ command_line: spec.command_line(),
+ code: status.code(),
+ success: status.success() && !timed_out,
+ timed_out,
+ truncated,
+ output,
+ duration: started.elapsed(),
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn spec_builds_a_readable_command_line() {
+ let spec = CommandSpec::new("cmake")
+ .arg("-S")
+ .arg("/src")
+ .args(["-B", "/build"]);
+ assert_eq!(spec.command_line(), "cmake -S /src -B /build");
+ }
+
+ #[test]
+ fn spec_records_environment_lookups() {
+ let spec = CommandSpec::new("make").env("CC", "afl-clang-lto");
+ assert!(spec.has_env("CC"));
+ assert_eq!(spec.env_value("CC"), Some("afl-clang-lto"));
+ assert_eq!(spec.env_value("CXX"), None);
+ }
+
+ #[test]
+ fn path_arg_renders_a_path_without_quoting() {
+ let spec = CommandSpec::new("sh").path_arg(Path::new("/src/configure"));
+ assert_eq!(spec.args, vec!["/src/configure".to_owned()]);
+ }
+
+ #[test]
+ fn successful_command_captures_stdout() {
+ let spec = CommandSpec::new("echo").arg("instrumented");
+ let run = run(&spec, Duration::from_secs(10)).unwrap();
+ assert!(run.success);
+ assert_eq!(run.code, Some(0));
+ assert!(run.output.contains("instrumented"));
+ assert!(!run.timed_out);
+ }
+
+ #[test]
+ fn failing_command_reports_its_exit_code_and_stderr() {
+ let spec = CommandSpec::new("sh").args(["-c", "echo boom >&2; exit 3"]);
+ let run = run(&spec, Duration::from_secs(10)).unwrap();
+ assert!(!run.success);
+ assert_eq!(run.code, Some(3));
+ assert!(run.output.contains("boom"));
+ }
+
+ #[test]
+ fn a_hung_command_is_killed_at_the_timeout() {
+ let spec = CommandSpec::new("sleep").arg("30");
+ let run = run(&spec, Duration::from_millis(200)).unwrap();
+ assert!(run.timed_out);
+ assert!(!run.success);
+ }
+
+ #[test]
+ fn large_output_is_captured_without_deadlocking_and_marked_truncated() {
+ let spec = CommandSpec::new("sh").args([
+ "-c",
+ "i=0; while [ $i -lt 40000 ]; do echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; i=$((i+1)); done",
+ ]);
+ let run = run(&spec, Duration::from_mins(1)).unwrap();
+ assert!(run.success);
+ assert!(run.truncated);
+ assert!(run.output.len() >= MAX_CAPTURED_BYTES);
+ }
+
+ #[test]
+ fn cwd_is_honoured() {
+ let dir = std::env::temp_dir().join(format!("lisdex-process-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ std::fs::write(dir.join("marker.txt"), "here").unwrap();
+ let spec = CommandSpec::new("ls").cwd(&dir);
+ let run = run(&spec, Duration::from_secs(10)).unwrap();
+ assert!(run.output.contains("marker.txt"));
+ }
+
+ #[test]
+ fn environment_reaches_the_child() {
+ let spec = CommandSpec::new("sh")
+ .args(["-c", "printf %s \"$LISDEX_PROBE\""])
+ .env("LISDEX_PROBE", "present");
+ let run = run(&spec, Duration::from_secs(10)).unwrap();
+ assert_eq!(run.output, "present");
+ }
+
+ #[test]
+ fn missing_program_is_a_launch_error() {
+ let spec = CommandSpec::new("lisdex-no-such-program-exists");
+ assert!(matches!(
+ run(&spec, Duration::from_secs(10)),
+ Err(ProcessError::Launch { .. })
+ ));
+ }
+
+ #[test]
+ fn tail_returns_the_last_lines_only() {
+ let spec = CommandSpec::new("sh").args(["-c", "printf 'one\\ntwo\\nthree\\n'"]);
+ let run = run(&spec, Duration::from_secs(10)).unwrap();
+ assert_eq!(run.tail(2), "two\nthree");
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/Cargo.toml b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/Cargo.toml
new file mode 100644
index 00000000..e45d1042
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/Cargo.toml
@@ -0,0 +1,17 @@
+# ©AngelaMos | 2026
+# Cargo.toml
+
+[package]
+name = "lisdex-ingest"
+version = "0.0.0"
+edition.workspace = true
+rust-version.workspace = true
+license.workspace = true
+authors.workspace = true
+
+[lints]
+workspace = true
+
+[dependencies]
+thiserror.workspace = true
+lisdex-core.workspace = true
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/acquire.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/acquire.rs
new file mode 100644
index 00000000..74605b25
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/acquire.rs
@@ -0,0 +1,380 @@
+// ©AngelaMos | 2026
+// acquire.rs
+
+use std::path::{Path, PathBuf};
+use std::time::Duration;
+
+use lisdex_core::process::{self, CommandSpec, Execution};
+
+pub const GIT_BIN: &str = "git";
+pub const COPY_BIN: &str = "cp";
+pub const DEFAULT_ACQUIRE_TIMEOUT_SECS: u64 = 900;
+
+const GIT_URL_PREFIXES: [&str; 5] = ["http://", "https://", "git://", "ssh://", "file://"];
+const SCP_LIKE_PREFIX: &str = "git@";
+const GIT_SUFFIX: &str = ".git";
+const GIT_DIR: &str = ".git";
+const ARG_CLONE: &str = "clone";
+const ARG_RECURSE: &str = "--recurse-submodules";
+const ARG_DEPTH: &str = "--depth";
+const DEPTH_SHALLOW: &str = "1";
+const ARG_CHECKOUT: &str = "checkout";
+const ARG_REV_PARSE: &str = "rev-parse";
+const ARG_HEAD: &str = "HEAD";
+const ARG_C: &str = "-C";
+const ARG_ARCHIVE: &str = "-a";
+const COPY_CONTENTS: &str = "/.";
+
+#[derive(Debug, thiserror::Error)]
+pub enum AcquireError {
+ #[error("process error: {0}")]
+ Process(#[from] process::ProcessError),
+ #[error("io error at {path}: {source}")]
+ Io {
+ path: PathBuf,
+ source: std::io::Error,
+ },
+ #[error("source path {0} does not exist")]
+ MissingSource(PathBuf),
+ #[error("destination {0} already holds files; pass reuse to build it again")]
+ DestinationNotEmpty(PathBuf),
+ #[error("{command} failed ({code:?}): {output}")]
+ Command {
+ command: String,
+ code: Option<i32>,
+ output: String,
+ },
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Source {
+ Local(PathBuf),
+ Git {
+ url: String,
+ reference: Option<String>,
+ },
+}
+
+impl Source {
+ #[must_use]
+ pub fn parse(raw: &str) -> Source {
+ let looks_remote = GIT_URL_PREFIXES
+ .iter()
+ .any(|prefix| raw.starts_with(prefix))
+ || raw.starts_with(SCP_LIKE_PREFIX)
+ || (raw.ends_with(GIT_SUFFIX) && !Path::new(raw).exists());
+ if looks_remote {
+ Source::Git {
+ url: raw.to_owned(),
+ reference: None,
+ }
+ } else {
+ Source::Local(PathBuf::from(raw))
+ }
+ }
+
+ #[must_use]
+ pub fn at(self, reference: Option<String>) -> Source {
+ match self {
+ Source::Git { url, .. } => Source::Git { url, reference },
+ local @ Source::Local(_) => local,
+ }
+ }
+
+ #[must_use]
+ pub fn label(&self) -> String {
+ match self {
+ Source::Local(path) => path.file_name().map_or_else(
+ || path.display().to_string(),
+ |name| name.to_string_lossy().into_owned(),
+ ),
+ Source::Git { url, .. } => url
+ .rsplit('/')
+ .next()
+ .unwrap_or(url)
+ .trim_end_matches(GIT_SUFFIX)
+ .to_owned(),
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Acquisition {
+ pub root: PathBuf,
+ pub revision: Option<String>,
+ pub reused: bool,
+}
+
+#[must_use]
+pub fn clone_spec(url: &str, reference: Option<&str>, dest: &Path) -> CommandSpec {
+ let mut spec = CommandSpec::new(GIT_BIN).arg(ARG_CLONE).arg(ARG_RECURSE);
+ if reference.is_none() {
+ spec = spec.arg(ARG_DEPTH).arg(DEPTH_SHALLOW);
+ }
+ spec.arg(url).path_arg(dest)
+}
+
+#[must_use]
+pub fn checkout_spec(dest: &Path, reference: &str) -> CommandSpec {
+ CommandSpec::new(GIT_BIN)
+ .arg(ARG_C)
+ .path_arg(dest)
+ .arg(ARG_CHECKOUT)
+ .arg(reference)
+}
+
+#[must_use]
+pub fn copy_spec(source: &Path, dest: &Path) -> CommandSpec {
+ CommandSpec::new(COPY_BIN)
+ .arg(ARG_ARCHIVE)
+ .arg(format!("{}{COPY_CONTENTS}", source.display()))
+ .path_arg(dest)
+}
+
+fn is_populated(path: &Path) -> Result<bool, AcquireError> {
+ if !path.is_dir() {
+ return Ok(false);
+ }
+ let mut entries = std::fs::read_dir(path).map_err(|source| AcquireError::Io {
+ path: path.to_path_buf(),
+ source,
+ })?;
+ Ok(entries.next().is_some())
+}
+
+fn checked(execution: Execution) -> Result<Execution, AcquireError> {
+ if execution.success {
+ return Ok(execution);
+ }
+ Err(AcquireError::Command {
+ command: execution.command_line.clone(),
+ code: execution.code,
+ output: execution.tail(40),
+ })
+}
+
+#[must_use]
+pub fn revision(root: &Path) -> Option<String> {
+ if !root.join(GIT_DIR).exists() {
+ return None;
+ }
+ let spec = CommandSpec::new(GIT_BIN)
+ .arg(ARG_C)
+ .path_arg(root)
+ .arg(ARG_REV_PARSE)
+ .arg(ARG_HEAD);
+ let execution = process::run(&spec, Duration::from_secs(DEFAULT_ACQUIRE_TIMEOUT_SECS)).ok()?;
+ if !execution.success {
+ return None;
+ }
+ let resolved = execution.output.trim().to_owned();
+ if resolved.is_empty() {
+ None
+ } else {
+ Some(resolved)
+ }
+}
+
+pub fn acquire(source: &Source, dest: &Path, reuse: bool) -> Result<Acquisition, AcquireError> {
+ if is_populated(dest)? {
+ if !reuse {
+ return Err(AcquireError::DestinationNotEmpty(dest.to_path_buf()));
+ }
+ return Ok(Acquisition {
+ root: dest.to_path_buf(),
+ revision: revision(dest),
+ reused: true,
+ });
+ }
+
+ let timeout = Duration::from_secs(DEFAULT_ACQUIRE_TIMEOUT_SECS);
+ match source {
+ Source::Local(path) => {
+ if !path.is_dir() {
+ return Err(AcquireError::MissingSource(path.clone()));
+ }
+ std::fs::create_dir_all(dest).map_err(|source| AcquireError::Io {
+ path: dest.to_path_buf(),
+ source,
+ })?;
+ checked(process::run(&copy_spec(path, dest), timeout)?)?;
+ }
+ Source::Git { url, reference } => {
+ if let Some(parent) = dest.parent() {
+ std::fs::create_dir_all(parent).map_err(|source| AcquireError::Io {
+ path: parent.to_path_buf(),
+ source,
+ })?;
+ }
+ checked(process::run(
+ &clone_spec(url, reference.as_deref(), dest),
+ timeout,
+ )?)?;
+ if let Some(reference) = reference {
+ checked(process::run(&checkout_spec(dest, reference), timeout)?)?;
+ }
+ }
+ }
+
+ Ok(Acquisition {
+ root: dest.to_path_buf(),
+ revision: revision(dest),
+ reused: false,
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn scratch(name: &str) -> PathBuf {
+ let dir =
+ std::env::temp_dir().join(format!("lisdex-acquire-{}-{name}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ dir
+ }
+
+ #[test]
+ fn https_urls_are_git_sources() {
+ assert_eq!(
+ Source::parse("https://github.com/glennrp/libpng.git"),
+ Source::Git {
+ url: "https://github.com/glennrp/libpng.git".to_owned(),
+ reference: None,
+ }
+ );
+ }
+
+ #[test]
+ fn scp_style_urls_are_git_sources() {
+ assert!(matches!(
+ Source::parse("[email protected]:glennrp/libpng.git"),
+ Source::Git { .. }
+ ));
+ }
+
+ #[test]
+ fn plain_paths_are_local_sources() {
+ assert_eq!(
+ Source::parse("/opt/magma-work/libpng/repo"),
+ Source::Local(PathBuf::from("/opt/magma-work/libpng/repo"))
+ );
+ }
+
+ #[test]
+ fn an_existing_local_bare_repo_is_not_mistaken_for_a_url() {
+ let dir = scratch("bare");
+ let bare = dir.join("libpng.git");
+ std::fs::create_dir_all(&bare).unwrap();
+ assert_eq!(
+ Source::parse(&bare.display().to_string()),
+ Source::Local(bare)
+ );
+ }
+
+ #[test]
+ fn label_is_the_repository_name() {
+ assert_eq!(
+ Source::parse("https://github.com/glennrp/libpng.git").label(),
+ "libpng"
+ );
+ assert_eq!(Source::parse("/opt/magma-work/libpng").label(), "libpng");
+ }
+
+ #[test]
+ fn clone_without_a_reference_is_shallow() {
+ let spec = clone_spec("https://example.invalid/p.git", None, Path::new("/w/p"));
+ assert_eq!(spec.program, GIT_BIN);
+ assert!(spec.args.iter().any(|arg| arg == ARG_DEPTH));
+ assert!(spec.args.iter().any(|arg| arg == ARG_RECURSE));
+ assert_eq!(spec.args.last().unwrap(), "/w/p");
+ }
+
+ #[test]
+ fn clone_with_a_reference_is_deep_so_the_commit_is_reachable() {
+ let spec = clone_spec(
+ "https://example.invalid/p.git",
+ Some("abc123"),
+ Path::new("/w/p"),
+ );
+ assert!(!spec.args.iter().any(|arg| arg == ARG_DEPTH));
+ }
+
+ #[test]
+ fn checkout_targets_the_cloned_tree() {
+ let spec = checkout_spec(Path::new("/w/p"), "abc123");
+ assert_eq!(spec.args, vec!["-C", "/w/p", "checkout", "abc123"]);
+ }
+
+ #[test]
+ fn copy_spec_copies_contents_including_dotfiles() {
+ let spec = copy_spec(Path::new("/src/repo"), Path::new("/w/repo"));
+ assert_eq!(spec.args, vec!["-a", "/src/repo/.", "/w/repo"]);
+ }
+
+ #[test]
+ fn acquiring_a_local_tree_copies_it_without_touching_the_original() {
+ let dir = scratch("local");
+ let src = dir.join("src");
+ std::fs::create_dir_all(&src).unwrap();
+ std::fs::write(src.join("configure.ac"), "AC_INIT").unwrap();
+ std::fs::write(src.join(".hidden"), "dot").unwrap();
+
+ let dest = dir.join("work");
+ let acquired = acquire(&Source::Local(src.clone()), &dest, false).unwrap();
+
+ assert_eq!(acquired.root, dest);
+ assert!(!acquired.reused);
+ assert_eq!(
+ std::fs::read_to_string(dest.join("configure.ac")).unwrap(),
+ "AC_INIT"
+ );
+ assert!(dest.join(".hidden").exists());
+ assert!(src.join("configure.ac").exists());
+ }
+
+ #[test]
+ fn acquiring_into_a_populated_destination_is_refused() {
+ let dir = scratch("populated");
+ let src = dir.join("src");
+ std::fs::create_dir_all(&src).unwrap();
+ let dest = dir.join("work");
+ std::fs::create_dir_all(&dest).unwrap();
+ std::fs::write(dest.join("stale"), "old").unwrap();
+
+ assert!(matches!(
+ acquire(&Source::Local(src), &dest, false),
+ Err(AcquireError::DestinationNotEmpty(_))
+ ));
+ }
+
+ #[test]
+ fn reuse_returns_the_existing_tree_untouched() {
+ let dir = scratch("reuse");
+ let src = dir.join("src");
+ std::fs::create_dir_all(&src).unwrap();
+ let dest = dir.join("work");
+ std::fs::create_dir_all(&dest).unwrap();
+ std::fs::write(dest.join("kept"), "old").unwrap();
+
+ let acquired = acquire(&Source::Local(src), &dest, true).unwrap();
+ assert!(acquired.reused);
+ assert_eq!(std::fs::read_to_string(dest.join("kept")).unwrap(), "old");
+ }
+
+ #[test]
+ fn acquiring_a_missing_local_tree_is_an_error() {
+ let dir = scratch("missing");
+ assert!(matches!(
+ acquire(&Source::Local(dir.join("nope")), &dir.join("work"), false),
+ Err(AcquireError::MissingSource(_))
+ ));
+ }
+
+ #[test]
+ fn revision_is_none_for_a_tree_without_git_metadata() {
+ let dir = scratch("norev");
+ assert_eq!(revision(&dir), None);
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/detect.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/detect.rs
new file mode 100644
index 00000000..389f36ff
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/detect.rs
@@ -0,0 +1,553 @@
+// ©AngelaMos | 2026
+// detect.rs
+
+use std::path::{Path, PathBuf};
+
+pub const MESON_BUILD: &str = "meson.build";
+pub const BAZEL_WORKSPACE: [&str; 3] = ["WORKSPACE", "WORKSPACE.bazel", "MODULE.bazel"];
+pub const BAZEL_BUILD: [&str; 2] = ["BUILD", "BUILD.bazel"];
+pub const CMAKE_LISTS: &str = "CMakeLists.txt";
+pub const CMAKE_PRESETS: &str = "CMakePresets.json";
+pub const CONFIGURE_AC: [&str; 2] = ["configure.ac", "configure.in"];
+pub const AUTOGEN: [&str; 3] = ["autogen.sh", "bootstrap", "buildconf"];
+pub const CONFIGURE_SCRIPT: &str = "configure";
+pub const MAKEFILE_AM: &str = "Makefile.am";
+pub const MAKEFILE_IN: &str = "Makefile.in";
+pub const SCONS_SCRIPT: &str = "SConstruct";
+pub const WAF_SCRIPT: &str = "wscript";
+pub const MAKEFILE: [&str; 3] = ["GNUmakefile", "Makefile", "makefile"];
+
+pub const PRECEDENCE: [BuildSystem; 7] = [
+ BuildSystem::Meson,
+ BuildSystem::Bazel,
+ BuildSystem::CMake,
+ BuildSystem::Autotools,
+ BuildSystem::Scons,
+ BuildSystem::Waf,
+ BuildSystem::Make,
+];
+
+#[derive(Debug, thiserror::Error)]
+pub enum DetectError {
+ #[error("io error reading {path}: {source}")]
+ Io {
+ path: PathBuf,
+ source: std::io::Error,
+ },
+ #[error("{0} is not a directory")]
+ NotADirectory(PathBuf),
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub enum BuildSystem {
+ Meson,
+ Bazel,
+ CMake,
+ Autotools,
+ Scons,
+ Waf,
+ Make,
+}
+
+impl BuildSystem {
+ #[must_use]
+ pub fn as_str(self) -> &'static str {
+ match self {
+ BuildSystem::Meson => "meson",
+ BuildSystem::Bazel => "bazel",
+ BuildSystem::CMake => "cmake",
+ BuildSystem::Autotools => "autotools",
+ BuildSystem::Scons => "scons",
+ BuildSystem::Waf => "waf",
+ BuildSystem::Make => "make",
+ }
+ }
+
+ #[must_use]
+ pub fn is_supported(self) -> bool {
+ matches!(
+ self,
+ BuildSystem::CMake | BuildSystem::Meson | BuildSystem::Autotools | BuildSystem::Make
+ )
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Marker {
+ MesonBuild,
+ BazelWorkspace,
+ BazelBuild,
+ CMakeLists,
+ CMakePresets,
+ ConfigureAc,
+ Autogen,
+ ConfigureScript,
+ MakefileAm,
+ MakefileIn,
+ SconsScript,
+ WafScript,
+ Makefile,
+}
+
+impl Marker {
+ #[must_use]
+ pub fn system(self) -> BuildSystem {
+ match self {
+ Marker::MesonBuild => BuildSystem::Meson,
+ Marker::BazelWorkspace | Marker::BazelBuild => BuildSystem::Bazel,
+ Marker::CMakeLists | Marker::CMakePresets => BuildSystem::CMake,
+ Marker::ConfigureAc
+ | Marker::Autogen
+ | Marker::ConfigureScript
+ | Marker::MakefileAm
+ | Marker::MakefileIn => BuildSystem::Autotools,
+ Marker::SconsScript => BuildSystem::Scons,
+ Marker::WafScript => BuildSystem::Waf,
+ Marker::Makefile => BuildSystem::Make,
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Evidence {
+ pub marker: Marker,
+ pub file: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Bootstrap {
+ Autogen(String),
+ Autoreconf,
+ None,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Detection {
+ pub root: PathBuf,
+ pub evidence: Vec<Evidence>,
+ pub candidates: Vec<BuildSystem>,
+}
+
+impl Detection {
+ #[must_use]
+ pub fn has(&self, marker: Marker) -> bool {
+ self.evidence.iter().any(|found| found.marker == marker)
+ }
+
+ #[must_use]
+ pub fn file_for(&self, marker: Marker) -> Option<&str> {
+ self.evidence
+ .iter()
+ .find(|found| found.marker == marker)
+ .map(|found| found.file.as_str())
+ }
+
+ #[must_use]
+ pub fn primary(&self) -> Option<BuildSystem> {
+ self.candidates.first().copied()
+ }
+
+ #[must_use]
+ pub fn supported(&self) -> Vec<BuildSystem> {
+ self.candidates
+ .iter()
+ .copied()
+ .filter(|system| system.is_supported())
+ .collect()
+ }
+
+ #[must_use]
+ pub fn unsupported(&self) -> Vec<BuildSystem> {
+ self.candidates
+ .iter()
+ .copied()
+ .filter(|system| !system.is_supported())
+ .collect()
+ }
+
+ #[must_use]
+ pub fn bootstrap(&self) -> Bootstrap {
+ if let Some(script) = self.file_for(Marker::Autogen) {
+ Bootstrap::Autogen(script.to_owned())
+ } else if self.has(Marker::ConfigureAc) {
+ Bootstrap::Autoreconf
+ } else {
+ Bootstrap::None
+ }
+ }
+
+ #[must_use]
+ pub fn has_generated_configure(&self) -> bool {
+ self.has(Marker::ConfigureScript)
+ }
+}
+
+fn marker_for(name: &str) -> Option<Marker> {
+ if name == MESON_BUILD {
+ return Some(Marker::MesonBuild);
+ }
+ if BAZEL_WORKSPACE.contains(&name) {
+ return Some(Marker::BazelWorkspace);
+ }
+ if BAZEL_BUILD.contains(&name) {
+ return Some(Marker::BazelBuild);
+ }
+ if name == CMAKE_LISTS {
+ return Some(Marker::CMakeLists);
+ }
+ if name == CMAKE_PRESETS {
+ return Some(Marker::CMakePresets);
+ }
+ if CONFIGURE_AC.contains(&name) {
+ return Some(Marker::ConfigureAc);
+ }
+ if AUTOGEN.contains(&name) {
+ return Some(Marker::Autogen);
+ }
+ if name == CONFIGURE_SCRIPT {
+ return Some(Marker::ConfigureScript);
+ }
+ if name == MAKEFILE_AM {
+ return Some(Marker::MakefileAm);
+ }
+ if name == MAKEFILE_IN {
+ return Some(Marker::MakefileIn);
+ }
+ if name == SCONS_SCRIPT {
+ return Some(Marker::SconsScript);
+ }
+ if name == WAF_SCRIPT {
+ return Some(Marker::WafScript);
+ }
+ if MAKEFILE.contains(&name) {
+ return Some(Marker::Makefile);
+ }
+ None
+}
+
+fn scan(root: &Path) -> Result<Vec<Evidence>, DetectError> {
+ let entries = std::fs::read_dir(root).map_err(|source| DetectError::Io {
+ path: root.to_path_buf(),
+ source,
+ })?;
+
+ let mut names = Vec::new();
+ for entry in entries {
+ let entry = entry.map_err(|source| DetectError::Io {
+ path: root.to_path_buf(),
+ source,
+ })?;
+ let file_type = entry.file_type().map_err(|source| DetectError::Io {
+ path: entry.path(),
+ source,
+ })?;
+ if file_type.is_dir() {
+ continue;
+ }
+ names.push(entry.file_name().to_string_lossy().into_owned());
+ }
+ names.sort();
+
+ let mut evidence = Vec::new();
+ for name in names {
+ if let Some(marker) = marker_for(&name) {
+ evidence.push(Evidence { marker, file: name });
+ }
+ }
+ Ok(evidence)
+}
+
+fn rank(evidence: &[Evidence]) -> Vec<BuildSystem> {
+ PRECEDENCE
+ .into_iter()
+ .filter(|system| {
+ evidence
+ .iter()
+ .any(|found| found.marker.system() == *system)
+ })
+ .collect()
+}
+
+fn sole_subdirectory(root: &Path) -> Result<Option<PathBuf>, DetectError> {
+ let entries = std::fs::read_dir(root).map_err(|source| DetectError::Io {
+ path: root.to_path_buf(),
+ source,
+ })?;
+
+ let mut found = None;
+ for entry in entries {
+ let entry = entry.map_err(|source| DetectError::Io {
+ path: root.to_path_buf(),
+ source,
+ })?;
+ let file_type = entry.file_type().map_err(|source| DetectError::Io {
+ path: entry.path(),
+ source,
+ })?;
+ if !file_type.is_dir() {
+ continue;
+ }
+ if entry.file_name().to_string_lossy().starts_with('.') {
+ continue;
+ }
+ if found.is_some() {
+ return Ok(None);
+ }
+ found = Some(entry.path());
+ }
+ Ok(found)
+}
+
+pub fn detect(root: &Path) -> Result<Detection, DetectError> {
+ if !root.is_dir() {
+ return Err(DetectError::NotADirectory(root.to_path_buf()));
+ }
+
+ let evidence = scan(root)?;
+ if evidence.is_empty()
+ && let Some(nested) = sole_subdirectory(root)?
+ {
+ let nested_evidence = scan(&nested)?;
+ if !nested_evidence.is_empty() {
+ let candidates = rank(&nested_evidence);
+ return Ok(Detection {
+ root: nested,
+ evidence: nested_evidence,
+ candidates,
+ });
+ }
+ }
+
+ let candidates = rank(&evidence);
+ Ok(Detection {
+ root: root.to_path_buf(),
+ evidence,
+ candidates,
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ struct Fixture {
+ root: PathBuf,
+ }
+
+ impl Fixture {
+ fn new(name: &str) -> Self {
+ let root =
+ std::env::temp_dir().join(format!("lisdex-detect-{}-{name}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&root);
+ std::fs::create_dir_all(&root).unwrap();
+ Self { root }
+ }
+
+ fn file(self, name: &str) -> Self {
+ let path = self.root.join(name);
+ if let Some(parent) = path.parent() {
+ std::fs::create_dir_all(parent).unwrap();
+ }
+ std::fs::write(path, "").unwrap();
+ self
+ }
+
+ fn dir(self, name: &str) -> Self {
+ std::fs::create_dir_all(self.root.join(name)).unwrap();
+ self
+ }
+
+ fn detect(&self) -> Detection {
+ detect(&self.root).unwrap()
+ }
+ }
+
+ #[test]
+ fn cmake_project_is_detected() {
+ let found = Fixture::new("cmake").file(CMAKE_LISTS).detect();
+ assert_eq!(found.primary(), Some(BuildSystem::CMake));
+ }
+
+ #[test]
+ fn meson_outranks_cmake() {
+ let found = Fixture::new("meson-cmake")
+ .file(MESON_BUILD)
+ .file(CMAKE_LISTS)
+ .detect();
+ assert_eq!(found.primary(), Some(BuildSystem::Meson));
+ assert_eq!(
+ found.candidates,
+ vec![BuildSystem::Meson, BuildSystem::CMake]
+ );
+ }
+
+ #[test]
+ fn cmake_outranks_a_bare_makefile_wrapper() {
+ let found = Fixture::new("cmake-make")
+ .file(CMAKE_LISTS)
+ .file("Makefile")
+ .detect();
+ assert_eq!(found.primary(), Some(BuildSystem::CMake));
+ }
+
+ #[test]
+ fn dual_cmake_and_autotools_keeps_both_candidates_in_order() {
+ let found = Fixture::new("libpng-shape")
+ .file(CMAKE_LISTS)
+ .file("configure.ac")
+ .file("Makefile.am")
+ .detect();
+ assert_eq!(
+ found.candidates,
+ vec![BuildSystem::CMake, BuildSystem::Autotools]
+ );
+ }
+
+ #[test]
+ fn generated_autotools_files_do_not_promote_raw_make() {
+ let found = Fixture::new("autotools-built")
+ .file("configure.ac")
+ .file("configure")
+ .file("Makefile.am")
+ .file("Makefile.in")
+ .file("Makefile")
+ .detect();
+ assert_eq!(found.primary(), Some(BuildSystem::Autotools));
+ assert_eq!(
+ found.candidates,
+ vec![BuildSystem::Autotools, BuildSystem::Make]
+ );
+ }
+
+ #[test]
+ fn configure_backup_file_is_not_a_marker() {
+ let found = Fixture::new("configure-backup").file("configure~").detect();
+ assert!(found.evidence.is_empty());
+ assert!(found.candidates.is_empty());
+ }
+
+ #[test]
+ fn autogen_script_is_preferred_over_autoreconf() {
+ let found = Fixture::new("autogen")
+ .file("autogen.sh")
+ .file("configure.ac")
+ .detect();
+ assert_eq!(
+ found.bootstrap(),
+ Bootstrap::Autogen("autogen.sh".to_owned())
+ );
+ }
+
+ #[test]
+ fn configure_ac_without_autogen_bootstraps_with_autoreconf() {
+ let found = Fixture::new("autoreconf").file("configure.ac").detect();
+ assert_eq!(found.bootstrap(), Bootstrap::Autoreconf);
+ assert!(!found.has_generated_configure());
+ }
+
+ #[test]
+ fn pre_generated_configure_needs_no_bootstrap() {
+ let found = Fixture::new("pregenerated").file("configure").detect();
+ assert_eq!(found.primary(), Some(BuildSystem::Autotools));
+ assert_eq!(found.bootstrap(), Bootstrap::None);
+ assert!(found.has_generated_configure());
+ }
+
+ #[test]
+ fn configure_in_is_recognised_as_configure_ac() {
+ let found = Fixture::new("configure-in").file("configure.in").detect();
+ assert!(found.has(Marker::ConfigureAc));
+ assert_eq!(found.bootstrap(), Bootstrap::Autoreconf);
+ }
+
+ #[test]
+ fn raw_makefile_project_falls_through_to_make() {
+ let found = Fixture::new("raw-make").file("Makefile").detect();
+ assert_eq!(found.primary(), Some(BuildSystem::Make));
+ }
+
+ #[test]
+ fn bazel_is_detected_but_reported_unsupported() {
+ let found = Fixture::new("bazel")
+ .file("WORKSPACE")
+ .file("BUILD.bazel")
+ .detect();
+ assert_eq!(found.primary(), Some(BuildSystem::Bazel));
+ assert!(found.supported().is_empty());
+ assert_eq!(found.unsupported(), vec![BuildSystem::Bazel]);
+ }
+
+ #[test]
+ fn scons_and_waf_are_detected_but_unsupported() {
+ let found = Fixture::new("scons-waf")
+ .file(SCONS_SCRIPT)
+ .file(WAF_SCRIPT)
+ .detect();
+ assert_eq!(
+ found.unsupported(),
+ vec![BuildSystem::Scons, BuildSystem::Waf]
+ );
+ }
+
+ #[test]
+ fn unknown_tree_yields_no_candidates() {
+ let found = Fixture::new("unknown").file("README.md").detect();
+ assert!(found.candidates.is_empty());
+ assert_eq!(found.primary(), None);
+ }
+
+ #[test]
+ fn a_directory_named_like_a_marker_is_ignored() {
+ let found = Fixture::new("marker-dir").dir(CMAKE_LISTS).detect();
+ assert!(found.candidates.is_empty());
+ }
+
+ #[test]
+ fn a_tarball_with_one_prefixed_directory_is_descended_into() {
+ let fixture = Fixture::new("tarball").file("libpng-1.6.37/configure.ac");
+ let found = fixture.detect();
+ assert_eq!(found.primary(), Some(BuildSystem::Autotools));
+ assert!(found.root.ends_with("libpng-1.6.37"));
+ }
+
+ #[test]
+ fn descent_does_not_happen_when_the_root_itself_has_markers() {
+ let fixture = Fixture::new("no-descend")
+ .file(CMAKE_LISTS)
+ .file("subproject/meson.build");
+ let found = fixture.detect();
+ assert_eq!(found.primary(), Some(BuildSystem::CMake));
+ assert_eq!(found.root, fixture.root);
+ }
+
+ #[test]
+ fn descent_does_not_happen_with_multiple_subdirectories() {
+ let fixture = Fixture::new("two-subdirs")
+ .file("one/CMakeLists.txt")
+ .file("two/CMakeLists.txt");
+ let found = fixture.detect();
+ assert!(found.candidates.is_empty());
+ }
+
+ #[test]
+ fn detecting_a_missing_directory_is_an_error() {
+ let missing = std::env::temp_dir().join("lisdex-detect-does-not-exist");
+ let _ = std::fs::remove_dir_all(&missing);
+ assert!(matches!(
+ detect(&missing),
+ Err(DetectError::NotADirectory(_))
+ ));
+ }
+
+ #[test]
+ fn every_supported_system_has_a_stable_name() {
+ for system in PRECEDENCE {
+ assert!(!system.as_str().is_empty());
+ }
+ assert!(BuildSystem::CMake.is_supported());
+ assert!(BuildSystem::Meson.is_supported());
+ assert!(BuildSystem::Autotools.is_supported());
+ assert!(BuildSystem::Make.is_supported());
+ assert!(!BuildSystem::Bazel.is_supported());
+ }
+}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/lib.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/lib.rs
new file mode 100644
index 00000000..f964337b
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/lib.rs
@@ -0,0 +1,5 @@
+// ©AngelaMos | 2026
+// lib.rs
+
+pub mod acquire;
+pub mod detect;
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/Cargo.toml b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/Cargo.toml
index e05003ee..50f6c645 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/Cargo.toml
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/Cargo.toml
@@ -20,3 +20,5 @@ path = "src/main.rs"
clap.workspace = true
lisdex-core.workspace = true
lisdex-fuzz.workspace = true
+lisdex-ingest.workspace = true
+lisdex-build.workspace = true
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs
index 64a5d99a..6d0c695e 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs
@@ -7,16 +7,24 @@ use std::process::ExitCode;
use std::time::Duration;
use clap::{Parser, Subcommand};
+use lisdex_build::instrument::Instrumentation;
+use lisdex_build::ladder::{self, BuildOptions};
+use lisdex_build::layout::{BuildLayout, VARIANT_AFL};
+use lisdex_build::verify::Requirements;
use lisdex_core::campaign::{Campaign, CampaignId, CampaignStatus};
use lisdex_core::report::SanitizerReport;
use lisdex_core::store::Store;
use lisdex_fuzz::runner::{self, AflOptions};
+use lisdex_ingest::acquire::{self, Source};
+use lisdex_ingest::detect;
const UNKNOWN_TARGET: &str = "unknown";
const UNPARSED_KIND: &str = "unparsed";
const NO_STACK_HASH: &str = "none";
const STACK_HASH_SHORT_LEN: usize = 16;
const DISPLAYED_FRAMES: usize = 6;
+const SOURCES_DIR: &str = "sources";
+const BUILDS_DIR: &str = "builds";
#[derive(Parser)]
#[command(
@@ -31,12 +39,41 @@ struct Cli {
#[derive(Subcommand)]
enum Command {
+ #[command(about = "auto-build a target under sanitizers with no hand-written build script")]
+ Build(BuildArgs),
#[command(about = "run an AFL++ campaign against an instrumented harness")]
Fuzz(FuzzArgs),
#[command(about = "list the crashes persisted for a campaign")]
Crashes(CrashesArgs),
}
+#[derive(clap::Args)]
+#[expect(clippy::struct_excessive_bools)]
+struct BuildArgs {
+ #[arg(help = "git URL or local path of the target library")]
+ target: String,
+ #[arg(long, help = "scratch directory for sources, builds and logs")]
+ work: PathBuf,
+ #[arg(long, help = "git reference to check out when the target is a URL")]
+ reference: Option<String>,
+ #[arg(long, help = "name for this target (defaults to the source name)")]
+ label: Option<String>,
+ #[arg(long, default_value = VARIANT_AFL, help = "build variant name")]
+ variant: String,
+ #[arg(long, help = "parallel build jobs")]
+ jobs: Option<usize>,
+ #[arg(long, help = "build a cmplog-instrumented variant")]
+ cmplog: bool,
+ #[arg(long, help = "build in the source tree instead of copying it first")]
+ in_place: bool,
+ #[arg(long, help = "reuse an already acquired source tree")]
+ reuse: bool,
+ #[arg(long = "cflag", help = "extra compiler flag, repeatable")]
+ cflags: Vec<String>,
+ #[arg(long, help = "require magma canary symbols in the built artifacts")]
+ require_canaries: bool,
+}
+
#[derive(clap::Args)]
struct FuzzArgs {
#[arg(long, help = "instrumented harness binary to fuzz")]
@@ -84,11 +121,97 @@ fn main() -> ExitCode {
fn dispatch() -> Result<(), Box<dyn Error>> {
match &Cli::parse().command {
+ Command::Build(args) => build(args),
Command::Fuzz(args) => fuzz(args),
Command::Crashes(args) => crashes(args),
}
}
+fn build(args: &BuildArgs) -> Result<(), Box<dyn Error>> {
+ let source = Source::parse(&args.target).at(args.reference.clone());
+ let label = args.label.clone().unwrap_or_else(|| source.label());
+
+ let root = if let (Source::Local(path), true) = (&source, args.in_place) {
+ path.clone()
+ } else {
+ let dest = args.work.join(SOURCES_DIR).join(&label);
+ let acquired = acquire::acquire(&source, &dest, args.reuse)?;
+ println!(
+ "acquired {label} at {}{}",
+ acquired.root.display(),
+ acquired
+ .revision
+ .map(|rev| format!(" ({rev})"))
+ .unwrap_or_default()
+ );
+ acquired.root
+ };
+
+ let detected = detect::detect(&root)?;
+ println!(
+ "detected {} candidate build system(s): {}",
+ detected.candidates.len(),
+ detected
+ .candidates
+ .iter()
+ .map(|system| system.as_str())
+ .collect::<Vec<&str>>()
+ .join(", ")
+ );
+
+ let instrumentation = Instrumentation::instrumented()
+ .with_cmplog(args.cmplog)
+ .with_extra_cflags(args.cflags.clone());
+
+ let layout = BuildLayout::new(&detected.root, args.work.join(BUILDS_DIR).join(&label));
+ let requirements = Requirements::library()
+ .with_cmplog(args.cmplog)
+ .with_magma_canaries(args.require_canaries);
+ let options = BuildOptions::new(
+ &args.variant,
+ args.jobs.unwrap_or_else(|| {
+ std::thread::available_parallelism().map_or(1, std::num::NonZero::get)
+ }),
+ )
+ .with_requirements(requirements);
+
+ let outcome = ladder::build(&detected, &layout, &instrumentation, &options)?;
+
+ for line in &outcome.degradation {
+ eprintln!("degraded: {line}");
+ }
+
+ if !outcome.succeeded() {
+ return Err(format!(
+ "no instrumented build of {label} was produced; {} attempt(s) failed",
+ outcome.attempts.len()
+ )
+ .into());
+ }
+
+ let system = outcome.system.map_or("unknown", |system| system.as_str());
+ let tier = outcome.tier.map_or("unknown", ladder::Tier::as_str);
+ println!("built {label} with {system} via {tier}");
+ let coverage = if outcome.counts.afl == 0 && outcome.lto_bitcode {
+ "deferred-to-link(lto-bitcode)".to_owned()
+ } else {
+ outcome.counts.afl.to_string()
+ };
+ println!(
+ "artifacts {} asan {} ubsan {} coverage {coverage} cmplog {} canaries {}",
+ outcome.artifacts.len(),
+ outcome.counts.asan,
+ outcome.counts.ubsan,
+ outcome.counts.cmplog,
+ outcome.counts.magma
+ );
+ for artifact in &outcome.artifacts {
+ println!(" {} {}", artifact.kind.as_str(), artifact.path.display());
+ }
+
+ Ok(())
+}
+
fn fuzz(args: &FuzzArgs) -> Result<(), Box<dyn Error>> {
let label = args.label.clone().unwrap_or_else(|| {
args.target
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/justfile b/PROJECTS/advanced/zero-day-vulnerability-scanner/justfile
index bfce85e6..ac8fe3e5 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/justfile
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/justfile
@@ -57,3 +57,9 @@ magma-libpng-reset:
m1-gate:
docker compose -f dev.compose.yml exec toolchain bash scripts/m1-gate.sh
+
+m2-gate:
+ docker compose -f dev.compose.yml exec toolchain bash scripts/m2-gate.sh
+
+m2-gate-reset:
+ docker compose -f dev.compose.yml exec toolchain bash scripts/m2-gate.sh reset
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/build-magma-libpng.sh b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/build-magma-libpng.sh
index efcf45bc..e3533cb4 100755
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/build-magma-libpng.sh
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/build-magma-libpng.sh
@@ -11,7 +11,7 @@ TARGET="$WORK/libpng"
SHARED="$WORK/shared"
OUT_ROOT="$WORK/out/libpng"
HARNESS_SRC="${LISDEX_HARNESS:-/work/harnesses/libpng_read.c}"
-DRIVER="${AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}"
+DRIVER="${LISDEX_AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}"
HARNESS_BIN="lisdex_libpng_read"
PATCH_STAMP="$TARGET/.lisdex-patched"
MAGMA_STAMP="$WORK/.lisdex-magma-commit"
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m2-gate.sh b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m2-gate.sh
new file mode 100755
index 00000000..16561dc6
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m2-gate.sh
@@ -0,0 +1,276 @@
+#!/usr/bin/env bash
+# ©AngelaMos | 2026
+# m2-gate.sh
+
+set -euo pipefail
+
+MAGMA_SRC="${MAGMA_ROOT:-/opt/magma}"
+ROOT="${LISDEX_M2_ROOT:-/opt/magma-work/m2}"
+CACHE="$ROOT/cache"
+WORK="$ROOT/gate"
+LISDEX="${LISDEX_BIN:-/cargo-target/debug/lisdex}"
+JOBS="${LISDEX_JOBS:-$(nproc)}"
+
+MAGMA_TARGETS=(libpng libtiff libxml2 sqlite3 lua)
+REQUIRED_MAGMA=3
+
+PARSER_NAMES=(openjpeg libarchive)
+PARSER_URLS=(
+ "https://github.com/uclouvain/openjpeg.git"
+ "https://github.com/libarchive/libarchive.git"
+)
+REQUIRED_PARSERS=2
+
+PASSED_MAGMA=0
+PASSED_PARSERS=0
+FAILURES=0
+SUMMARY=()
+
+log() {
+ printf '\n[lisdex] %s\n' "$1"
+}
+
+die() {
+ printf '\n[lisdex] FATAL: %s\n' "$1" >&2
+ exit 1
+}
+
+record() {
+ SUMMARY+=("$1")
+}
+
+[ -x "$LISDEX" ] || die "lisdex binary missing at $LISDEX (run: cargo build --workspace)"
+[ -d "$MAGMA_SRC" ] || die "magma source missing at $MAGMA_SRC"
+
+if [ "${1:-}" = "reset" ]; then
+ log "removing cache and gate work tree"
+ rm -rf "$CACHE" "$WORK"
+fi
+
+mkdir -p "$CACHE"
+rm -rf "$WORK"
+mkdir -p "$WORK"
+
+log "stage 1/4: fetch magma target sources (pinned upstream, no canary patches)"
+for NAME in "${MAGMA_TARGETS[@]}"; do
+ TARGET="$CACHE/magma-$NAME"
+ if [ ! -d "$TARGET/repo" ]; then
+ printf ' fetching %s\n' "$NAME"
+ rm -rf "$TARGET"
+ cp -r "$MAGMA_SRC/targets/$NAME" "$TARGET"
+ rm -rf "$TARGET/repo"
+ if ! TARGET="$TARGET" bash "$TARGET/fetch.sh" >"$WORK/fetch-$NAME.log" 2>&1; then
+ printf ' FAIL fetch failed, see %s\n' "$WORK/fetch-$NAME.log"
+ rm -rf "$TARGET"
+ continue
+ fi
+ fi
+ printf ' %-10s %s\n' "$NAME" "$TARGET/repo"
+done
+
+log "stage 2/4: auto-build magma targets"
+for NAME in "${MAGMA_TARGETS[@]}"; do
+ SOURCE="$CACHE/magma-$NAME/repo"
+ if [ ! -d "$SOURCE" ]; then
+ printf ' FAIL %s (not fetched)\n' "$NAME"
+ record "FAIL magma/$NAME not fetched"
+ continue
+ fi
+ printf ' building %s\n' "$NAME"
+ START=$(date +%s)
+ if OUTPUT="$("$LISDEX" build "$SOURCE" --label "$NAME" --work "$WORK/$NAME" --jobs "$JOBS" 2>&1)"; then
+ ELAPSED=$(( $(date +%s) - START ))
+ BUILT="$(printf '%s' "$OUTPUT" | grep '^built ' || true)"
+ COUNTS="$(printf '%s' "$OUTPUT" | grep '^artifacts ' || true)"
+ printf ' PASS %s (%ss)\n' "$BUILT" "$ELAPSED"
+ printf ' %s\n' "$COUNTS"
+ record "PASS magma/$NAME ${ELAPSED}s $BUILT | $COUNTS"
+ PASSED_MAGMA=$((PASSED_MAGMA + 1))
+ else
+ ELAPSED=$(( $(date +%s) - START ))
+ REASON="$(printf '%s' "$OUTPUT" | grep '^degraded: ' | tail -1 || true)"
+ printf ' FAIL %s (%ss)\n' "$NAME" "$ELAPSED"
+ printf ' %s\n' "${REASON:-no degradation reason reported}"
+ record "FAIL magma/$NAME ${ELAPSED}s ${REASON:-unknown}"
+ fi
+done
+
+log "stage 3/4: auto-build real parser libraries"
+for INDEX in "${!PARSER_NAMES[@]}"; do
+ NAME="${PARSER_NAMES[$INDEX]}"
+ URL="${PARSER_URLS[$INDEX]}"
+ CACHED="$CACHE/parser-$NAME"
+ if [ ! -d "$CACHED" ]; then
+ printf ' cloning %s\n' "$NAME"
+ if ! git clone --depth 1 --quiet "$URL" "$CACHED" >"$WORK/clone-$NAME.log" 2>&1; then
+ printf ' FAIL clone failed, see %s\n' "$WORK/clone-$NAME.log"
+ record "FAIL parser/$NAME clone failed"
+ continue
+ fi
+ fi
+ REVISION="$(git -C "$CACHED" rev-parse --short HEAD 2>/dev/null || echo unknown)"
+ printf ' building %s (%s)\n' "$NAME" "$REVISION"
+ START=$(date +%s)
+ if OUTPUT="$("$LISDEX" build "$CACHED" --label "$NAME" --work "$WORK/$NAME" --jobs "$JOBS" 2>&1)"; then
+ ELAPSED=$(( $(date +%s) - START ))
+ BUILT="$(printf '%s' "$OUTPUT" | grep '^built ' || true)"
+ COUNTS="$(printf '%s' "$OUTPUT" | grep '^artifacts ' || true)"
+ printf ' PASS %s (%ss)\n' "$BUILT" "$ELAPSED"
+ printf ' %s\n' "$COUNTS"
+ record "PASS parser/$NAME@$REVISION ${ELAPSED}s $BUILT | $COUNTS"
+ PASSED_PARSERS=$((PASSED_PARSERS + 1))
+ else
+ ELAPSED=$(( $(date +%s) - START ))
+ REASON="$(printf '%s' "$OUTPUT" | grep '^degraded: ' | tail -1 || true)"
+ printf ' FAIL %s (%ss)\n' "$NAME" "$ELAPSED"
+ printf ' %s\n' "${REASON:-no degradation reason reported}"
+ record "FAIL parser/$NAME ${ELAPSED}s ${REASON:-unknown}"
+ fi
+done
+
+log "stage 4/4: failures must degrade loudly, never silently"
+NO_BUILD="$WORK/unbuildable"
+mkdir -p "$NO_BUILD"
+printf 'this tree has no build system\n' > "$NO_BUILD/README.md"
+if OUTPUT="$("$LISDEX" build "$NO_BUILD" --work "$WORK/unbuildable-work" --jobs 1 2>&1)"; then
+ printf ' FAIL a tree with no build system reported success\n'
+ FAILURES=$((FAILURES + 1))
+ record "FAIL degradation unbuildable tree reported success"
+else
+ if printf '%s' "$OUTPUT" | grep -q 'no supported build system detected'; then
+ printf ' PASS unbuildable tree failed with a named reason\n'
+ record "PASS degradation unbuildable tree named its reason"
+ else
+ printf ' FAIL failure was not explained:\n%s\n' "$OUTPUT"
+ FAILURES=$((FAILURES + 1))
+ record "FAIL degradation failure not explained"
+ fi
+fi
+
+BAZEL_TREE="$WORK/bazel-only"
+mkdir -p "$BAZEL_TREE"
+printf 'workspace(name = "x")\n' > "$BAZEL_TREE/WORKSPACE"
+if OUTPUT="$("$LISDEX" build "$BAZEL_TREE" --work "$WORK/bazel-work" --jobs 1 2>&1)"; then
+ printf ' FAIL a bazel-only tree reported success\n'
+ FAILURES=$((FAILURES + 1))
+ record "FAIL degradation bazel tree reported success"
+else
+ if printf '%s' "$OUTPUT" | grep -q 'bazel detected but lisdex plans no bazel build'; then
+ printf ' PASS bazel is detected and reported unsupported\n'
+ record "PASS degradation bazel detected and named unsupported"
+ else
+ printf ' FAIL bazel degradation was not explained:\n%s\n' "$OUTPUT"
+ FAILURES=$((FAILURES + 1))
+ record "FAIL degradation bazel not explained"
+ fi
+fi
+
+NEGATIVE="$WORK/negative-control"
+mkdir -p "$NEGATIVE"
+NATIVE_CC="$(command -v gcc || command -v cc || true)"
+if [ -z "$NATIVE_CC" ]; then
+ printf ' FAIL no uninstrumented compiler available for the negative control\n'
+ FAILURES=$((FAILURES + 1))
+ record "FAIL negative-control no uninstrumented compiler present"
+else
+ cat > "$NEGATIVE/parser.c" <<'EOF'
+#include <stddef.h>
+int parse(const unsigned char *data, size_t size) {
+ unsigned char buffer[16];
+ size_t i;
+ for (i = 0; i < size && i < sizeof(buffer); i++) {
+ buffer[i] = data[i];
+ }
+ return size > 0 ? buffer[0] : 0;
+}
+EOF
+ {
+ printf 'all: libparser.a\n\n'
+ printf 'parser.o: parser.c\n'
+ printf '\t%s -O2 -c parser.c -o parser.o\n\n' "$NATIVE_CC"
+ printf 'libparser.a: parser.o\n'
+ printf '\tar rcs libparser.a parser.o\n\n'
+ printf 'clean:\n'
+ printf '\trm -f parser.o libparser.a\n'
+ } > "$NEGATIVE/Makefile"
+
+ if OUTPUT="$("$LISDEX" build "$NEGATIVE" --label hardcoded --work "$WORK/hardcoded-work" --jobs 1 2>&1)"; then
+ if printf '%s' "$OUTPUT" | grep -q 'via compile-commands-replay\|via compiler-shadow'; then
+ printf ' PASS a hardcoded absolute-path compiler was defeated by a later tier\n'
+ record "PASS control/hardcoded-cc $(printf '%s' "$OUTPUT" | grep '^built ')"
+ else
+ printf ' FAIL a hardcoded compiler was reported built by the wrong tier:\n%s\n' "$OUTPUT"
+ FAILURES=$((FAILURES + 1))
+ record "FAIL control/hardcoded-cc wrong tier claimed the build"
+ fi
+ else
+ printf ' FAIL no tier defeated a hardcoded absolute-path compiler:\n%s\n' "$OUTPUT"
+ FAILURES=$((FAILURES + 1))
+ record "FAIL control/hardcoded-cc no tier defeated it"
+ fi
+
+ VENDORED="$WORK/vendored-blob"
+ mkdir -p "$VENDORED/prebuilt"
+ printf 'int vendored(void) { return 7; }\n' > "$VENDORED/vendor.c"
+ "$NATIVE_CC" -O2 -c "$VENDORED/vendor.c" -o "$VENDORED/prebuilt/vendor.o"
+ ar rcs "$VENDORED/prebuilt/libvendor.a" "$VENDORED/prebuilt/vendor.o"
+ rm -f "$VENDORED/vendor.c" "$VENDORED/prebuilt/vendor.o"
+ {
+ printf 'all: libvendor.a\n\n'
+ printf 'libvendor.a: prebuilt/libvendor.a\n'
+ printf '\tcp prebuilt/libvendor.a libvendor.a\n\n'
+ printf 'clean:\n'
+ printf '\trm -f libvendor.a\n'
+ } > "$VENDORED/Makefile"
+
+ if OUTPUT="$("$LISDEX" build "$VENDORED" --label vendored --work "$WORK/vendored-work" --jobs 1 2>&1)"; then
+ printf ' FAIL a prebuilt uninstrumented archive was reported as instrumented\n'
+ printf '%s\n' "$OUTPUT"
+ FAILURES=$((FAILURES + 1))
+ record "FAIL control/vendored-blob uninstrumented archive reported as built"
+ elif printf '%s' "$OUTPUT" | grep -q 'address sanitizer symbols: found 0'; then
+ if [ -f "$WORK/vendored-work/sources/vendored/libvendor.a" ]; then
+ printf ' PASS a real archive that cannot be instrumented was rejected by symbol inspection\n'
+ record "PASS control/vendored-blob uninstrumented archive rejected"
+ else
+ printf ' FAIL the vendored control never produced an archive, so it proved nothing\n'
+ FAILURES=$((FAILURES + 1))
+ record "FAIL control/vendored-blob no archive was produced"
+ fi
+ else
+ printf ' FAIL rejection was not attributed to missing instrumentation:\n%s\n' "$OUTPUT"
+ FAILURES=$((FAILURES + 1))
+ record "FAIL control/vendored-blob wrong rejection reason"
+ fi
+fi
+
+printf '\n'
+printf '================ M2 GATE SUMMARY ================\n'
+for LINE in "${SUMMARY[@]}"; do
+ printf ' %s\n' "$LINE"
+done
+printf '\n'
+printf ' magma targets built : %d / %d required (%d attempted)\n' \
+ "$PASSED_MAGMA" "$REQUIRED_MAGMA" "${#MAGMA_TARGETS[@]}"
+printf ' real parsers built : %d / %d required (%d attempted)\n' \
+ "$PASSED_PARSERS" "$REQUIRED_PARSERS" "${#PARSER_NAMES[@]}"
+printf ' degradation failures : %d\n' "$FAILURES"
+printf '\n'
+
+if [ "$PASSED_MAGMA" -lt "$REQUIRED_MAGMA" ]; then
+ printf '[lisdex] M2 GATE FAILED: only %d magma target(s) auto-built\n' "$PASSED_MAGMA"
+ exit 1
+fi
+if [ "$PASSED_PARSERS" -lt "$REQUIRED_PARSERS" ]; then
+ printf '[lisdex] M2 GATE FAILED: only %d real parser(s) auto-built\n' "$PASSED_PARSERS"
+ exit 1
+fi
+if [ "$FAILURES" -ne 0 ]; then
+ printf '[lisdex] M2 GATE FAILED: %d degradation check(s) failed\n' "$FAILURES"
+ exit 1
+fi
+
+printf '[lisdex] M2 GATE PASSED\n'
+printf '[lisdex] instrumentation was verified by symbol inspection of the built\n'
+printf '[lisdex] libraries, not by build exit codes\n'
+exit 0
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/verify-toolchain.sh b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/verify-toolchain.sh
index b6cbe2da..e5924e77 100755
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/verify-toolchain.sh
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/verify-toolchain.sh
@@ -78,8 +78,8 @@ done
printf ' INFO %s\n' "$(clang --version | head -1)"
section "AFL++ driver + libclang"
-if [ -f "${AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}" ]; then
- ok "libAFLDriver.a present at ${AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}"
+if [ -f "${LISDEX_AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}" ]; then
+ ok "libAFLDriver.a present at ${LISDEX_AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}"
else
fail "libAFLDriver.a missing"
fi
@@ -154,7 +154,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
EOF
export AR=llvm-ar RANLIB=llvm-ranlib AS=llvm-as NM=llvm-nm
-DRIVER="${AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}"
+DRIVER="${LISDEX_AFL_DRIVER:-/usr/local/lib/afl/libAFLDriver.a}"
if AFL_USE_ASAN=1 afl-clang-lto -O0 -g "$WORK/planted.c" "$DRIVER" \
-o "$WORK/harness" >"$WORK/build.log" 2>&1; then