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 π
Go aicertificationcloud-engineeringcybercybersecuritycybersecurity-analystcybersecurity-certificationcybersecurity-certificationscybersecurity-educationcybersecurity-jobscybersecurity-portfoliocybersecurity-projectscybersecurity-toolsgrchackinglearningpentestingproject-repositoryprojetcspython
feat(lisdex): B36 - a disabled block is not a call, and a prototype is not a program
CarterPerez-dev0cce9552a713
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rsindex 7802e75c..9b24a8c9 100644--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rs+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rs@@ -120,6 +120,144 @@ fn without_comments_or_literals(body: &str) -> String {kept}+// `#if 0` around a block of real calls is how a C author comments out code that still has to+// compile if switched back on, and openjpeg's own J2K fuzzer carries one. The identifiers inside+// it are not calls this program makes. A directive LINE never contributes a call either: the+// body of `#define WRAP(x) inner_call(x)` yielded both `WRAP` and `inner_call`, neither of which+// anything called.+fn is_always_false(rest: &str) -> bool {+ matches!(rest.trim(), "0" | "false")+}++fn directive_word(directive: &str) -> &str {+ directive+ .split(|character: char| !character.is_alphanumeric() && character != '_')+ .next()+ .unwrap_or("")+}++fn without_inactive_preprocessor(body: &str) -> String {+ let mut kept = String::with_capacity(body.len());+ let mut lines = body.lines();+ let mut depth = 0usize;+ let mut skipping: Option<usize> = None;++ while let Some(line) = lines.next() {+ // A directive continued with a trailing backslash is one logical line, so a multi-line+ // `#define` does not leak its tail into the kept text.+ let mut logical = line.to_owned();+ while logical.trim_end().ends_with('\\') {+ let Some(next) = lines.next() else { break };+ let trimmed = logical.trim_end();+ logical = format!("{} {next}", &trimmed[..trimmed.len() - 1]);+ }++ if let Some(directive) = logical.trim_start().strip_prefix('#') {+ let directive = directive.trim_start();+ let word = directive_word(directive);+ match word {+ "if" | "ifdef" | "ifndef" => {+ depth += 1;+ if skipping.is_none()+ && word == "if"+ && is_always_false(&directive[word.len()..])+ {+ skipping = Some(depth);+ }+ }+ "elif" | "else" => {+ if skipping == Some(depth) {+ skipping = None;+ }+ }+ "endif" => {+ if skipping == Some(depth) {+ skipping = None;+ }+ depth = depth.saturating_sub(1);+ }+ _ => {}+ }+ kept.push('\n');+ continue;+ }++ if skipping.is_some() {+ kept.push('\n');+ continue;+ }+ kept.push_str(&logical);+ kept.push('\n');+ }++ kept+}++// What separates `png_read_info(p, i)` from `int png_read_info(png_structp, png_infop);` is the+// token in front of it: a declarator is introduced by its return type, a call never is. The+// keyword list is what keeps `return foo(x)` and `case FOO: bar(x)` on the call side, and the+// `*` hop is what puts `void *lisdex_malloc(...)` on the declarator side.+//+// A DEFINITION is deliberately NOT filtered, and the distinction is load-bearing rather than+// pedantic: `defines_a_program` decides a file is a consumer by finding `main` call-shaped, so+// suppressing every declarator took `main` with it and emptied the ladder's whole consumer tier+// - three ladder tests and the program-detection test went red together. Only the form that+// ends in `;` is a declaration; the form that ends in `{` is the author writing the function.+fn is_declaration(chars: &[char], start: usize, open: usize) -> bool {+ is_declarator(chars, start) && closes_into_semicolon(chars, open)+}++fn closes_into_semicolon(chars: &[char], open: usize) -> bool {+ let mut depth = 0usize;+ let mut index = open;+ while index < chars.len() {+ match chars[index] {+ '(' => depth += 1,+ ')' => {+ depth -= 1;+ if depth == 0 {+ break;+ }+ }+ _ => {}+ }+ index += 1;+ }+ index += 1;+ while index < chars.len() && chars[index].is_whitespace() {+ index += 1;+ }+ chars.get(index) == Some(&';')+}++fn is_declarator(chars: &[char], start: usize) -> bool {+ let mut index = start;+ let mut hopped_star = false;+ loop {+ while index > 0 && chars[index - 1].is_whitespace() {+ index -= 1;+ }+ if index > 0 && chars[index - 1] == '*' && !hopped_star {+ index -= 1;+ hopped_star = true;+ continue;+ }+ break;+ }+ if index == 0 {+ return false;+ }+ let end = index;+ while index > 0 && (chars[index - 1].is_alphanumeric() || chars[index - 1] == '_') {+ index -= 1;+ }+ if index == end {+ return false;+ }+ let preceding: String = chars[index..end].iter().collect();+ !NOT_CALLS.contains(&preceding.as_str())+}+// Whole-FILE order is only the author's call order when the file holds one function. zlib's// `test/infcover.c` defines twenty, so first-occurrence order over the file put `inflate`// at position 25 and `inflateInit` nowhere before it - the setup replay therefore found@@ -202,7 +340,7 @@ fn find_identifier(chars: &[char], name: &[char], from: usize) -> Option<usize>// first. Dedup on FIRST occurrence, because that is where the author established the order.#[must_use]pub fn extract_calls(body: &str) -> Vec<String> {- let body = without_comments_or_literals(body);+ let body = without_inactive_preprocessor(&without_comments_or_literals(body));let chars: Vec<char> = body.chars().collect();let mut seen = BTreeSet::new();let mut found = Vec::new();@@ -226,6 +364,7 @@ pub fn extract_calls(body: &str) -> Vec<String> {&& !NOT_CALLS.contains(&identifier.as_str())&& identifier != ENTRY_POINT&& identifier != INITIALIZE_POINT+ && !is_declaration(&chars, start, lookahead)&& seen.insert(identifier.clone()){found.push(identifier);@@ -418,6 +557,80 @@ mod order_tests {assert_eq!(calls, vec!["png_read_info"], "{calls:?}");}+ // B36. openjpeg's own J2K fuzzer parks a tile-size check behind `#if 0`, and both calls in+ // it were mined into the sequence the ladder chose - `opj_get_cstr_info` and+ // `opj_destroy_cstr_info`, neither of which that program ever makes.+ #[test]+ fn a_call_inside_an_if_zero_block_is_not_a_call_this_program_makes() {+ let calls = super::extract_calls("#if 0\n dead_call(x);\n#endif\n live_call(y);");+ assert_eq!(calls, vec!["live_call"], "{calls:?}");+ }++ // The nesting has to be tracked or the FIRST `#endif` reopens mining inside a block that is+ // still switched off.+ #[test]+ fn a_nested_directive_does_not_reopen_the_disabled_block_early() {+ let calls = super::extract_calls(+ "#if 0\n#ifdef SOMETHING\n dead_call(x);\n#endif\n also_dead(y);\n#endif\n live_call(z);",+ );+ assert_eq!(calls, vec!["live_call"], "{calls:?}");+ }++ // libpng's `example.c` opens with `#if 0 /* in case someone actually tries to compile this */`+ // and wraps the whole file, so its `main` made a documentation file libpng says must not+ // compile into the 28th consumer of the tree. The trailing comment is why the directive is+ // read AFTER comments are stripped rather than before.+ #[test]+ fn a_disabled_block_is_recognised_with_a_comment_on_the_directive() {+ let calls = super::extract_calls(+ "#if 0 /* in case someone actually tries to compile this */\nint main(int c) { png_read_png(p); }\n#endif\n",+ );+ assert!(calls.is_empty(), "{calls:?}");+ }++ // A directive LINE is never a call site either: the macro's own name and its body both+ // mined, so `#define WRAP(x) inner_call(x)` produced two calls nothing made.+ #[test]+ fn a_macro_definition_contributes_neither_its_name_nor_its_body() {+ let calls = super::extract_calls("#define WRAP(x) inner_call(x)\n live_call(y);");+ assert_eq!(calls, vec!["live_call"], "{calls:?}");+ }++ #[test]+ fn a_multi_line_macro_definition_does_not_leak_its_tail() {+ let calls = super::extract_calls(+ "#define WRAP(x) \\\n inner_call(x); \\\n second_call(x)\n live_call(y);",+ );+ assert_eq!(calls, vec!["live_call"], "{calls:?}");+ }++ // A prototype is the author saying the function EXISTS, not that this program calls it.+ // Mined as a call it makes the function "reached", and a reached function is suppressed as+ // a target, so a declaration demotes the very entry point it names.+ #[test]+ fn a_prototype_is_not_a_call_but_a_definition_still_is() {+ let calls = super::extract_calls("int declared_only(void);\n live_call(y);");+ assert_eq!(calls, vec!["live_call"], "{calls:?}");++ // The other half, and it is load-bearing: `defines_a_program` finds a consumer by+ // seeing `main` call-shaped, so filtering every declarator would empty the ladder's+ // consumer tier. Only the form ending in `;` is a declaration.+ let defined = super::extract_calls("int main(int argc, char **argv)\n{\n work(argc);\n}");+ assert_eq!(defined, vec!["main", "work"], "{defined:?}");+ }++ // libpng's `contrib/pngminus/png2pnm.c` does both, in that order, and it must stay a+ // consumer.+ #[test]+ fn a_file_that_declares_and_then_defines_main_is_still_a_program() {+ let source = "int main (int argc, char *argv[]);\n\nint main (int argc, char *argv[])\n{\n png2pnm(argc);\n}";+ assert!(+ super::defines_a_program(source),+ "{:?}",+ super::extract_calls(source)+ );+ }+// Isolates body scoping. zlib's `test/infcover.c` defines twenty functions, so// first-occurrence order over the FILE put `inflate` at position 25 with `inflateInit`// nowhere before it, and the setup replay found nothing to replay. The fixture keeps