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): B28 - Leopard-V's headline term is in, and it moves the ranking zero

CarterPerez-dev9b094f294511
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/complexity.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/complexity.rs
index e1f2caff..7a0244c5 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/complexity.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/complexity.rs
@@ -50,6 +50,7 @@ pub struct Defined {
pub loops: u32,
pub nested_loops: u32,
pub max_nesting: u32,
+ pub pointer_arithmetic: u32,
pub calls: Vec<Symbol>,
pub indirect: Vec<String>,
pub exported: bool,
@@ -85,6 +86,7 @@ impl Defined {
.saturating_add(self.loops)
.saturating_add(self.nested_loops)
.saturating_add(self.max_nesting)
+ .saturating_add(self.pointer_arithmetic)
}
#[must_use]
@@ -130,6 +132,7 @@ struct Metrics {
loops: u32,
nested_loops: u32,
max_nesting: u32,
+ pointer_arithmetic: u32,
calls: BTreeSet<Symbol>,
indirect: BTreeSet<String>,
taken: BTreeSet<(String, Symbol)>,
@@ -183,6 +186,57 @@ fn indirect_signature(entity: &Entity) -> Option<String> {
signature_of(&callee.get_type()?)
}
+fn is_pointer(entity: &Entity) -> bool {
+ entity
+ .get_type()
+ .is_some_and(|kind| kind.get_canonical_type().get_kind() == TypeKind::Pointer)
+}
+
+fn pointer_operands(entity: &Entity) -> usize {
+ entity
+ .get_children()
+ .iter()
+ .filter(|child| is_pointer(child))
+ .count()
+}
+
+// Leopard-V's pointer-arithmetic term, defined from types alone because clang-2.1.0 exposes
+// EntityKind::BinaryOperator but no accessor for which operator it is. Each arm below is
+// separated by the RESULT type and the operand count rather than by a spelling:
+// `p == q` and `p - q` are integer-typed and never reach here, and `p = q` carries TWO
+// pointer operands where `p + n` carries one.
+fn is_pointer_arithmetic(entity: &Entity) -> bool {
+ match entity.get_kind() {
+ EntityKind::ArraySubscriptExpr => entity
+ .get_children()
+ .first()
+ .is_some_and(subscript_base_is_pointer),
+ EntityKind::BinaryOperator | EntityKind::UnaryOperator => {
+ is_pointer(entity) && pointer_operands(entity) == 1
+ }
+ EntityKind::CompoundAssignOperator => is_pointer(entity),
+ _ => false,
+ }
+}
+
+// A subscript on a true array decays to a pointer at the cast, so the cast's own type cannot
+// separate `p[i]` from `arr[i]`. The declared type underneath it can.
+fn subscript_base_is_pointer(base: &Entity) -> bool {
+ let mut node = *base;
+ loop {
+ if let Some(target) = node.get_reference() {
+ return target
+ .get_type()
+ .is_some_and(|kind| kind.get_canonical_type().get_kind() == TypeKind::Pointer);
+ }
+ let children = node.get_children();
+ if children.len() != 1 {
+ return is_pointer(&node);
+ }
+ node = children[0];
+ }
+}
+
fn walk(entity: &Entity, metrics: &mut Metrics, depth: u32, loop_depth: u32, in_callee: bool) {
let children = entity.get_children();
let parent_is_if = entity.get_kind() == EntityKind::IfStmt;
@@ -200,6 +254,9 @@ fn walk(entity: &Entity, metrics: &mut Metrics, depth: u32, loop_depth: u32, in_
metrics.nested_loops += 1;
}
}
+ if is_pointer_arithmetic(child) {
+ metrics.pointer_arithmetic += 1;
+ }
if kind == EntityKind::CallExpr {
match child.get_reference() {
Some(target)
@@ -321,6 +378,7 @@ fn collect(entity: &Entity, found: &mut Sources, seen: &mut BTreeSet<String>) {
loops: metrics.loops,
nested_loops: metrics.nested_loops,
max_nesting: metrics.max_nesting,
+ pointer_arithmetic: metrics.pointer_arithmetic,
calls: metrics.calls.into_iter().collect(),
indirect: metrics.indirect.into_iter().collect(),
exported: entity.get_linkage() != Some(Linkage::Internal),
@@ -580,6 +638,78 @@ mod tests {
assert!(find(&found, "shown").exported);
}
+ #[test]
+ fn pointer_arithmetic_counts_advancing_a_pointer_and_nothing_else() {
+ let found = defined(
+ "leopardptr",
+ "int f(char *p, char *q, int n) {\n\
+ char *a = p + n;\n\
+ char *b = p;\n\
+ b += n;\n\
+ b++;\n\
+ return a[0] + b[1] + (p == q) + (int)(p - q);\n\
+ }\n",
+ );
+ assert_eq!(
+ find(&found, "f").pointer_arithmetic,
+ 5,
+ "p + n, b += n, b++, a[0] and b[1] advance a pointer; p == q is int-typed, \
+ p - q is ptrdiff_t, and char *b = p is an initialiser rather than arithmetic"
+ );
+ }
+
+ #[test]
+ fn a_function_that_never_touches_a_pointer_scores_zero_pointer_arithmetic() {
+ let found = defined(
+ "leopardnoptr",
+ "int f(int a, int b) { int c = a + b; if (c) { c++; } return c; }\n",
+ );
+ assert_eq!(
+ find(&found, "f").pointer_arithmetic,
+ 0,
+ "integer arithmetic must not be counted, or the metric measures nothing"
+ );
+ }
+
+ #[test]
+ fn indexing_a_real_array_is_not_pointer_arithmetic() {
+ let found = defined(
+ "leopardarray",
+ "int f(int i) { int arr[10]; arr[i] = 1; return arr[0]; }\n",
+ );
+ assert_eq!(
+ find(&found, "f").pointer_arithmetic,
+ 0,
+ "a subscript on a declared array decays to a pointer at the cast, so counting the \
+ cast's type would make every array access pointer arithmetic"
+ );
+ }
+
+ #[test]
+ fn taking_an_address_is_not_pointer_arithmetic() {
+ let found = defined("leopardaddr", "int f(int a) { int *p = &a; return *p; }\n");
+ assert_eq!(
+ find(&found, "f").pointer_arithmetic,
+ 0,
+ "&a and *p are pointer-typed or pointer-operand nodes but neither advances a \
+ pointer"
+ );
+ }
+
+ #[test]
+ fn assigning_one_pointer_to_another_is_not_pointer_arithmetic() {
+ let found = defined(
+ "leopardassign",
+ "int f(char *p, char *q) { p = q; return *p; }\n",
+ );
+ assert_eq!(
+ find(&found, "f").pointer_arithmetic,
+ 0,
+ "p = q is a pointer-typed binary operator, and only the operand COUNT separates it \
+ from p + n"
+ );
+ }
+
#[test]
fn loops_are_counted_separately_from_branches() {
let found = defined(
@@ -657,6 +787,7 @@ mod tests {
loops: 0,
nested_loops: 0,
max_nesting: 0,
+ pointer_arithmetic: 0,
calls: Vec::new(),
indirect: Vec::new(),
exported: true,
@@ -713,6 +844,7 @@ mod tests {
loops: 0,
nested_loops: 0,
max_nesting: 0,
+ pointer_arithmetic: 0,
calls,
indirect: Vec::new(),
exported: true,
@@ -727,6 +859,7 @@ mod tests {
loops: 0,
nested_loops: 0,
max_nesting: 0,
+ pointer_arithmetic: 0,
calls,
indirect: Vec::new(),
exported: false,
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/rank.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/rank.rs
index ce527921..3f4036d6 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/rank.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/rank.rs
@@ -119,10 +119,15 @@ fn exclusion_for(role: Role) -> Option<ExclusionReason> {
// to 0.9045 while its accumulated danger stayed at 5086 in both runs.
//
// The value is MEASURED, not guessed, and the obvious guess is wrong. Over the six validation
-// trees the largest accumulated danger is libarchive's 14646, then libpng 5088, libtiff 4543,
-// openjpeg 4402, brotli 832, lz4 439. A scale of 10000 therefore CLAMPS: libarchive's whole
+// trees the largest accumulated danger is libarchive's 18673, then openjpeg 9855, libpng 7076,
+// libtiff 5390, brotli 1122, lz4 699. A scale of 10000 therefore CLAMPS: libarchive's whole
// top would saturate at 1.0 and stop discriminating, on the one tree where the numbers were
-// hardest won. This leaves 2.2x headroom over anything measured.
+// hardest won. This leaves 1.75x headroom over anything measured.
+//
+// B28 re-measured every figure above when Leopard-V's pointer-arithmetic term entered
+// `danger`. The previous readings were libarchive 14646, libpng 5088, libtiff 4543,
+// openjpeg 4402, brotli 832, lz4 439, so the term moved each tree between +18.6% and +123.9%.
+// openjpeg more than doubled and overtook libpng. The ranking did not move at all.
//
// A saturating ratio was tried first because it can never clamp at all, and was rejected on
// measurement: it cannot discriminate across a range this wide. At the magnitudes a small