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 👇

fix(lisdex): M4 audit - stop the synthesizer fabricating crashes inside targets

CarterPerez-dev512f4ad1710b
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/compile.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/compile.rs
index 995edb8b..daa6fe13 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/compile.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/compile.rs
@@ -67,8 +67,12 @@ pub fn build_command(spec: &BuildSpec) -> CommandSpec {
.path_arg(&spec.source)
.arg("-o")
.path_arg(&spec.output);
- for archive in &spec.archives {
- command = command.path_arg(archive);
+ if !spec.archives.is_empty() {
+ command = command.arg("-Wl,--start-group");
+ for archive in &spec.archives {
+ command = command.path_arg(archive);
+ }
+ command = command.arg("-Wl,--end-group");
}
command = command.path_arg(&spec.driver);
for link in &spec.link_args {
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs
index cadb25b2..6dd85003 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs
@@ -212,6 +212,9 @@ fn render_stmt(out: &mut String, statement: &Stmt) {
}
}
}
+ Stmt::Discard { local } => {
+ let _ = writeln!(out, " (void){local};");
+ }
Stmt::BailIfNull { local } => {
let _ = writeln!(out, " if ({local} == NULL) {{");
let _ = writeln!(out, " return 0;");
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/oracle.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/oracle.rs
index 8b5cf113..a050936d 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/oracle.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/oracle.rs
@@ -43,6 +43,7 @@ pub enum Rejection {
ConstantCoverageIncrease { amount: usize },
InvalidSeedsIndistinguishable,
NoGlobalNovelty,
+ ParentMapIncomplete { seed: PathBuf },
}
impl std::fmt::Display for Rejection {
@@ -84,6 +85,11 @@ impl std::fmt::Display for Rejection {
formatter,
"adds no edge that an already-accepted harness does not already reach"
),
+ Rejection::ParentMapIncomplete { seed } => write!(
+ formatter,
+ "the parent coverage map has no entry for seed {} - the linearity oracle cannot run and must not silently pass",
+ seed.display()
+ ),
}
}
}
@@ -173,7 +179,9 @@ pub fn judge_linear_increase(
let mut gains = Vec::with_capacity(valid.len());
for run in valid {
let Some(before) = parent.get(&run.seed) else {
- return Ok(());
+ return Err(Rejection::ParentMapIncomplete {
+ seed: run.seed.clone(),
+ });
};
gains.push(run.map.novel_against(before));
}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs
index 2efdd55a..22396653 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs
@@ -45,6 +45,9 @@ pub enum Stmt {
args: Vec<Value>,
capture: Option<Binding>,
},
+ Discard {
+ local: String,
+ },
BailIfNull {
local: String,
},
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rs
index 9d3c2225..17def7a2 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rs
@@ -1,6 +1,8 @@
// ©AngelaMos | 2026
// synth.rs
+use std::collections::BTreeMap;
+
use lisdex_surface::extract::Vocabulary;
use lisdex_surface::rank::Candidate;
use lisdex_surface::signature::{Fuzzability, Param, ParamRole, Pointee, Shape};
@@ -44,23 +46,47 @@ impl std::fmt::Display for Refusal {
}
}
+fn shared_leading_tokens(left: &str, right: &str) -> usize {
+ left.split('_')
+ .zip(right.split('_'))
+ .take_while(|(one, other)| one == other)
+ .count()
+}
+
#[must_use]
pub fn macro_for_field(record: &str, field: &str, vocabulary: &Vocabulary) -> Option<String> {
- let direct = format!("{}_{}", record.to_uppercase(), field.to_uppercase());
+ let record_upper = record.to_uppercase();
+ let direct = format!("{record_upper}_{}", field.to_uppercase());
if vocabulary.macros.contains(&direct) {
return Some(direct);
}
let suffix = format!("_{}", field.to_uppercase());
- let record_upper = record.to_uppercase();
- vocabulary
+ let mut best: Option<(usize, String)> = None;
+ let mut tied = false;
+ for name in vocabulary
.macros
.iter()
.filter(|name| name.ends_with(&suffix))
- .find(|name| {
- let head = &name[..name.len() - suffix.len()];
- record_upper.starts_with(head) || head.starts_with(&record_upper)
- })
- .cloned()
+ {
+ let head = &name[..name.len() - suffix.len()];
+ if !(record_upper.starts_with(head) || head.starts_with(&record_upper)) {
+ continue;
+ }
+ let score = shared_leading_tokens(head, &record_upper);
+ match &best {
+ Some((top, _)) if score < *top => {}
+ Some((top, _)) if score == *top => tied = true,
+ _ => {
+ best = Some((score, name.clone()));
+ tied = false;
+ }
+ }
+ }
+ if tied {
+ None
+ } else {
+ best.map(|(_, name)| name)
+ }
}
fn is_double_pointer(param: &Param) -> bool {
@@ -73,11 +99,23 @@ fn is_double_pointer(param: &Param) -> bool {
)
}
-fn cast_for(param: &Param) -> Option<String> {
+fn cast_for(param: &Param) -> Result<Option<String>, Refusal> {
+ let label = || param.name.clone().unwrap_or_else(|| param.spelling.clone());
match &param.shape {
- Shape::Pointer { pointee, .. } if pointee.is_exact_byte() => None,
- Shape::Pointer { .. } => Some(param.spelling.clone()),
- _ => None,
+ Shape::Pointer { pointee, .. } if pointee.is_exact_byte() => Ok(None),
+ Shape::Pointer { pointee, is_const } => {
+ let base = match pointee {
+ Pointee::Void => "void",
+ Pointee::Char | Pointee::SignedChar | Pointee::UnsignedChar => "char",
+ _ => return Err(Refusal::UnsupportedShape { param: label() }),
+ };
+ Ok(Some(if *is_const {
+ format!("const {base} *")
+ } else {
+ format!("{base} *")
+ }))
+ }
+ _ => Err(Refusal::UnsupportedShape { param: label() }),
}
}
@@ -91,11 +129,15 @@ fn context_record(param: &Param) -> Option<&str> {
}
}
-fn bind(param: &Param, role: ParamRole, context: Option<&str>) -> Result<Value, Refusal> {
+fn bind(
+ param: &Param,
+ role: ParamRole,
+ locals: &BTreeMap<String, String>,
+) -> Result<Value, Refusal> {
let label = param.name.clone().unwrap_or_else(|| param.spelling.clone());
match role {
ParamRole::Buffer => Ok(Value::Data {
- cast: cast_for(param),
+ cast: cast_for(param)?,
}),
ParamRole::Length => Ok(Value::Size),
ParamRole::Config => Ok(Value::Zero),
@@ -106,19 +148,28 @@ fn bind(param: &Param, role: ParamRole, context: Option<&str>) -> Result<Value,
if is_double_pointer(param) {
return Err(Refusal::UnsizedOutParam { param: label });
}
- match context {
- Some(local) => Ok(Value::AddressOf(local.to_owned())),
- None => Err(Refusal::UnsupportedShape { param: label }),
- }
+ // Binding every context param to one shared local passed a small struct's
+ // address where a large one was expected and produced a real ASAN
+ // stack-buffer-overflow attributed to the library, not the harness.
+ let record = context_record(param).ok_or_else(|| Refusal::UnsupportedShape {
+ param: label.clone(),
+ })?;
+ locals
+ .get(record)
+ .map(|local| Value::AddressOf(local.clone()))
+ .ok_or_else(|| Refusal::OpaqueContext {
+ record: record.to_owned(),
+ })
}
}
}
fn field_bindings(record: &str, vocabulary: &Vocabulary) -> Vec<(String, String)> {
- let Some(fields) = vocabulary.records.get(record) else {
+ let Some(entry) = vocabulary.records.get(record) else {
return Vec::new();
};
- fields
+ entry
+ .fields
.iter()
.filter(|field| field.shape == Shape::Integral)
.filter_map(|field| {
@@ -138,38 +189,48 @@ pub fn plan_candidate(
return Err(Refusal::NoDataInput);
}
- let context = candidate
- .params
- .iter()
- .zip(&candidate.analysis.roles)
- .find(|(param, role)| {
- matches!(role, ParamRole::Context | ParamRole::Output) && !is_double_pointer(param)
- })
- .and_then(|(param, _)| context_record(param));
-
- if let Some(record) = context
- && !vocabulary.records.contains_key(record)
- {
- return Err(Refusal::OpaqueContext {
- record: record.to_owned(),
- });
+ let mut locals: BTreeMap<String, String> = BTreeMap::new();
+ let mut ordered: Vec<(String, String)> = Vec::new();
+ for (param, role) in candidate.params.iter().zip(&candidate.analysis.roles) {
+ if !matches!(role, ParamRole::Context | ParamRole::Output) || is_double_pointer(param) {
+ continue;
+ }
+ let Some(record) = context_record(param) else {
+ continue;
+ };
+ if !vocabulary.records.contains_key(record) {
+ return Err(Refusal::OpaqueContext {
+ record: record.to_owned(),
+ });
+ }
+ if !locals.contains_key(record) {
+ let local = if locals.is_empty() {
+ CONTEXT_LOCAL.to_owned()
+ } else {
+ format!("{CONTEXT_LOCAL}{}", locals.len())
+ };
+ locals.insert(record.to_owned(), local.clone());
+ ordered.push((record.to_owned(), local));
+ }
}
let mut args = Vec::with_capacity(candidate.params.len());
for (param, role) in candidate.params.iter().zip(&candidate.analysis.roles) {
- args.push(bind(param, *role, context.map(|_| CONTEXT_LOCAL))?);
+ args.push(bind(param, *role, &locals)?);
}
let mut base = HarnessPlan::new(candidate.name.clone()).with_include(include);
- if let Some(record) = context {
+ for (record, local) in &ordered {
base = base.with(Stmt::Declare {
- ty: record.to_owned(),
- name: CONTEXT_LOCAL.to_owned(),
+ ty: vocabulary.records[record].spelling.clone(),
+ name: local.clone(),
init: Init::ZeroBytes,
});
}
- let bindings = context.map(|record| field_bindings(record, vocabulary));
+ let bindings = ordered
+ .first()
+ .map(|(record, _)| field_bindings(record, vocabulary));
let call = |plan: HarnessPlan| -> HarnessPlan {
let capture = match candidate.result.shape {
Shape::Void | Shape::Other => None,
@@ -178,11 +239,19 @@ pub fn plan_candidate(
name: RESULT_LOCAL.to_owned(),
}),
};
- plan.with(Stmt::Call {
+ let captured = capture.is_some();
+ let plan = plan.with(Stmt::Call {
function: candidate.name.clone(),
args: args.clone(),
capture,
- })
+ });
+ if captured {
+ plan.with(Stmt::Discard {
+ local: RESULT_LOCAL.to_owned(),
+ })
+ } else {
+ plan
+ }
};
let mut plans = Vec::new();
@@ -207,12 +276,19 @@ pub fn plan_candidate(
#[cfg(test)]
mod tests {
use super::*;
- use lisdex_surface::extract::Field;
+ use lisdex_surface::extract::{Field, Record};
use lisdex_surface::rank::Complexity;
use lisdex_surface::signature::{Analysis, Returns};
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
+ fn call_in(plan: &HarnessPlan) -> &Stmt {
+ plan.body
+ .iter()
+ .find(|statement| matches!(statement, Stmt::Call { .. }))
+ .expect("the plan calls the entry point")
+ }
+
fn param(name: &str, spelling: &str, shape: Shape) -> Param {
Param {
name: Some(name.to_owned()),
@@ -253,21 +329,24 @@ mod tests {
macros: BTreeSet::from(["PNG_IMAGE_VERSION".to_owned(), "PNG_FORMAT_RGBA".to_owned()]),
records: BTreeMap::from([(
"png_image".to_owned(),
- vec![
- Field {
- name: "opaque".to_owned(),
- spelling: "png_controlp".to_owned(),
- shape: Shape::Pointer {
- pointee: Pointee::Record("png_control".to_owned()),
- is_const: false,
+ Record {
+ spelling: "png_image".to_owned(),
+ fields: vec![
+ Field {
+ name: "opaque".to_owned(),
+ spelling: "png_controlp".to_owned(),
+ shape: Shape::Pointer {
+ pointee: Pointee::Record("png_control".to_owned()),
+ is_const: false,
+ },
},
- },
- Field {
- name: "version".to_owned(),
- spelling: "png_uint_32".to_owned(),
- shape: Shape::Integral,
- },
- ],
+ Field {
+ name: "version".to_owned(),
+ spelling: "png_uint_32".to_owned(),
+ shape: Shape::Integral,
+ },
+ ],
+ },
)]),
}
}
@@ -362,8 +441,8 @@ mod tests {
init: Init::ZeroBytes,
}
);
- let Some(Stmt::Call { args, .. }) = plans[0].body.last() else {
- panic!("expected a trailing call");
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
};
assert_eq!(args[0], Value::AddressOf(CONTEXT_LOCAL.to_owned()));
}
@@ -371,14 +450,15 @@ mod tests {
#[test]
fn the_buffer_is_cast_and_the_length_is_the_size() {
let plans = plan_candidate(&begin_read(), "png.h", &png_image_vocabulary()).unwrap();
- let Some(Stmt::Call { args, .. }) = plans[0].body.last() else {
- panic!("expected a trailing call");
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
};
assert_eq!(
args[1],
Value::Data {
- cast: Some("png_const_voidp".to_owned())
- }
+ cast: Some("const void *".to_owned())
+ },
+ "the cast is built from the resolved Shape, never from the typedef spelling"
);
assert_eq!(args[2], Value::Size);
}
@@ -386,8 +466,8 @@ mod tests {
#[test]
fn a_non_void_result_is_captured() {
let plans = plan_candidate(&begin_read(), "png.h", &png_image_vocabulary()).unwrap();
- let Some(Stmt::Call { capture, .. }) = plans[0].body.last() else {
- panic!("expected a trailing call");
+ let Stmt::Call { capture, .. } = call_in(&plans[0]) else {
+ unreachable!()
};
assert_eq!(
capture.as_ref().map(|binding| binding.ty.as_str()),
@@ -395,6 +475,192 @@ mod tests {
);
}
+ #[test]
+ fn two_distinct_record_contexts_get_two_distinct_locals() {
+ let mut vocab = png_image_vocabulary();
+ vocab.records.insert(
+ "big_out".to_owned(),
+ Record {
+ spelling: "struct big_out".to_owned(),
+ fields: vec![Field {
+ name: "bytes".to_owned(),
+ spelling: "unsigned char[256]".to_owned(),
+ shape: Shape::Other,
+ }],
+ },
+ );
+ let two = candidate(
+ "two_parse",
+ vec![
+ param(
+ "small",
+ "png_imagep",
+ Shape::Pointer {
+ pointee: Pointee::Record("png_image".to_owned()),
+ is_const: false,
+ },
+ ),
+ param(
+ "out",
+ "struct big_out *",
+ Shape::Pointer {
+ pointee: Pointee::Record("big_out".to_owned()),
+ is_const: false,
+ },
+ ),
+ param(
+ "d",
+ "const void *",
+ Shape::Pointer {
+ pointee: Pointee::Void,
+ is_const: true,
+ },
+ ),
+ param("n", "size_t", Shape::Integral),
+ ],
+ vec![
+ ParamRole::Context,
+ ParamRole::Output,
+ ParamRole::Buffer,
+ ParamRole::Length,
+ ],
+ Fuzzability::CastBuffer,
+ Returns::default(),
+ );
+ let plans = plan_candidate(&two, "x.h", &vocab).unwrap();
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
+ };
+ assert_ne!(
+ args[0], args[1],
+ "passing one local's address as two different record types produced a real ASAN stack-buffer-overflow blamed on the library"
+ );
+ let declared: Vec<&Stmt> = plans[0]
+ .body
+ .iter()
+ .filter(|s| matches!(s, Stmt::Declare { .. }))
+ .collect();
+ assert_eq!(declared.len(), 2);
+ }
+
+ #[test]
+ fn a_context_that_is_not_a_record_pointer_is_refused() {
+ let mut with_int_out = begin_read();
+ with_int_out.params[0] = param(
+ "outlen",
+ "int *",
+ Shape::Pointer {
+ pointee: Pointee::Integral,
+ is_const: false,
+ },
+ );
+ assert!(matches!(
+ plan_candidate(&with_int_out, "png.h", &png_image_vocabulary()),
+ Err(Refusal::UnsupportedShape { .. })
+ ));
+ }
+
+ #[test]
+ fn a_record_declares_itself_with_the_spelling_not_the_lookup_key() {
+ let mut vocab = png_image_vocabulary();
+ vocab.records.get_mut("png_image").unwrap().spelling = "struct png_image".to_owned();
+ let plans = plan_candidate(&begin_read(), "png.h", &vocab).unwrap();
+ assert_eq!(
+ plans[0].body[0],
+ Stmt::Declare {
+ ty: "struct png_image".to_owned(),
+ name: CONTEXT_LOCAL.to_owned(),
+ init: Init::ZeroBytes,
+ }
+ );
+ }
+
+ #[test]
+ fn a_cast_is_built_from_the_shape_never_from_an_array_spelling() {
+ let arr = candidate(
+ "arr_parse",
+ vec![
+ param(
+ "buf",
+ "const char[]",
+ Shape::Pointer {
+ pointee: Pointee::Char,
+ is_const: true,
+ },
+ ),
+ param("size", "size_t", Shape::Integral),
+ ],
+ vec![ParamRole::Buffer, ParamRole::Length],
+ Fuzzability::CastBuffer,
+ Returns::default(),
+ );
+ let plans = plan_candidate(&arr, "x.h", &Vocabulary::default()).unwrap();
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
+ };
+ assert_eq!(
+ args[0],
+ Value::Data {
+ cast: Some("const char *".to_owned())
+ }
+ );
+ }
+
+ #[test]
+ fn a_buffer_whose_shape_yields_no_valid_cast_is_refused() {
+ let odd = candidate(
+ "odd_parse",
+ vec![
+ param(
+ "buf",
+ "const wchar_t *",
+ Shape::Pointer {
+ pointee: Pointee::Other,
+ is_const: true,
+ },
+ ),
+ param("size", "size_t", Shape::Integral),
+ ],
+ vec![ParamRole::Buffer, ParamRole::Length],
+ Fuzzability::CastBuffer,
+ Returns::default(),
+ );
+ assert!(matches!(
+ plan_candidate(&odd, "x.h", &Vocabulary::default()),
+ Err(Refusal::UnsupportedShape { .. })
+ ));
+ }
+
+ #[test]
+ fn macro_affinity_prefers_the_closest_family_not_the_alphabetically_first() {
+ let mut vocab = png_image_vocabulary();
+ vocab.macros.insert("PNG2_VERSION".to_owned());
+ vocab.macros.insert("PNG_IMAGE_THING_VERSION".to_owned());
+ vocab.macros.remove("PNG_IMAGE_VERSION");
+ assert_eq!(
+ macro_for_field("png_image", "version", &vocab),
+ Some("PNG_IMAGE_THING_VERSION".to_owned()),
+ "PNG2_VERSION sorts first in ASCII but shares no token run with png_image"
+ );
+ }
+
+ #[test]
+ fn an_ambiguous_macro_family_binds_to_nothing_rather_than_guessing() {
+ let mut vocab = png_image_vocabulary();
+ vocab.macros.remove("PNG_IMAGE_VERSION");
+ vocab.macros.insert("PNG_IMAGE_A_VERSION".to_owned());
+ vocab.macros.insert("PNG_IMAGE_B_VERSION".to_owned());
+ assert_eq!(macro_for_field("png_image", "version", &vocab), None);
+ }
+
+ #[test]
+ fn a_captured_result_is_discarded_so_wall_werror_targets_still_build() {
+ let plans = plan_candidate(&begin_read(), "png.h", &png_image_vocabulary()).unwrap();
+ assert!(plans[0].body.contains(&Stmt::Discard {
+ local: RESULT_LOCAL.to_owned()
+ }));
+ }
+
#[test]
fn an_opaque_context_is_refused_rather_than_guessed() {
let mut opaque = begin_read();
@@ -513,8 +779,8 @@ mod tests {
.iter()
.any(|s| matches!(s, Stmt::Declare { .. }))
);
- let Some(Stmt::Call { args, .. }) = plans[0].body.last() else {
- panic!("expected a trailing call");
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
};
assert_eq!(args[0], Value::Data { cast: None });
}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs
index 6cecfefd..3e9a146c 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs
@@ -67,17 +67,23 @@ pub struct Field {
pub shape: Shape,
}
+#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize)]
+pub struct Record {
+ pub spelling: String,
+ pub fields: Vec<Field>,
+}
+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Vocabulary {
pub macros: BTreeSet<String>,
- pub records: BTreeMap<String, Vec<Field>>,
+ pub records: BTreeMap<String, Record>,
}
impl Vocabulary {
pub fn absorb(&mut self, other: Vocabulary) {
self.macros.extend(other.macros);
- for (name, fields) in other.records {
- self.records.entry(name).or_insert(fields);
+ for (name, record) in other.records {
+ self.records.entry(name).or_insert(record);
}
}
}
@@ -293,7 +299,7 @@ fn collect_vocabulary(entity: &Entity, into: &mut Vocabulary) {
for child in entity.get_children() {
match child.get_kind() {
EntityKind::MacroDefinition => {
- if child.is_builtin_macro() {
+ if child.is_builtin_macro() || child.is_function_like_macro() {
continue;
}
if let Some(name) = child.get_name() {
@@ -305,9 +311,19 @@ fn collect_vocabulary(entity: &Entity, into: &mut Vocabulary) {
if !fields.is_empty()
&& let Some(ty) = child.get_type()
{
+ let canonical = ty.get_canonical_type();
+ let key = record_name(&canonical);
+ // clang spells an anonymous typedef'd struct
+ // `struct (unnamed at foo.h:3:9)`, which is not a declarable type.
+ let display = canonical.get_display_name();
+ let spelling = if display.contains('(') {
+ key.clone()
+ } else {
+ display
+ };
into.records
- .entry(record_name(&ty.get_canonical_type()))
- .or_insert(fields);
+ .entry(key)
+ .or_insert(Record { spelling, fields });
}
}
EntityKind::LinkageSpec | EntityKind::Namespace | EntityKind::UnexposedDecl => {
@@ -339,6 +355,22 @@ pub fn parse_vocabulary(path: &Path, options: &ParseOptions) -> Result<Vocabular
reason: source.to_string(),
})?;
+ let failures: Vec<String> = unit
+ .get_diagnostics()
+ .iter()
+ .filter(|diagnostic| {
+ matches!(diagnostic.get_severity(), Severity::Error | Severity::Fatal)
+ })
+ .map(Diagnostic::get_text)
+ .collect();
+ if let Some(first) = failures.first() {
+ return Err(ExtractError::Diagnostics {
+ path: target.clone(),
+ count: failures.len(),
+ first: first.clone(),
+ });
+ }
+
let mut vocabulary = Vocabulary::default();
collect_vocabulary(&unit.get_entity(), &mut vocabulary);
Ok(vocabulary)
@@ -463,14 +495,66 @@ int png_image_begin_read_from_memory(png_image *image, const void *m, unsigned l
#[test]
fn record_fields_carry_names_and_shapes() {
let vocab = vocabulary("fields", SIMPLIFIED_API);
- let fields = vocab.records.values().next().expect("one record");
- let version = fields
+ let record = vocab.records.values().next().expect("one record");
+ let version = record
+ .fields
.iter()
.find(|field| field.name == "version")
.expect("version field");
assert_eq!(version.shape, Shape::Integral);
}
+ #[test]
+ fn a_tagged_record_keeps_its_struct_tag_even_when_typedefd() {
+ let vocab = vocabulary("spelling_typedef", SIMPLIFIED_API);
+ assert_eq!(vocab.records["png_image"].spelling, "struct png_image");
+ }
+
+ #[test]
+ fn an_anonymous_typedefd_record_falls_back_to_a_declarable_name() {
+ let vocab = vocabulary(
+ "spelling_anon",
+ "typedef struct { unsigned int version; } anon_ctx;\nvoid f(anon_ctx *c);",
+ );
+ let record = vocab.records.values().next().expect("one record");
+ assert!(
+ !record.spelling.contains('('),
+ "`struct (unnamed at ...)` is not a declarable type, got {:?}",
+ record.spelling
+ );
+ }
+
+ #[test]
+ fn a_record_with_no_typedef_keeps_its_struct_tag() {
+ let vocab = vocabulary(
+ "spelling_tag",
+ "struct parser_ctx { unsigned int version; }; void f(struct parser_ctx *c);",
+ );
+ assert_eq!(
+ vocab.records["parser_ctx"].spelling, "struct parser_ctx",
+ "declaring `parser_ctx ctx;` without the tag is a hard compile error in C"
+ );
+ }
+
+ #[test]
+ fn function_like_macros_are_not_collected_because_they_cannot_be_values() {
+ let vocab = vocabulary(
+ "fnlike",
+ "#define BLOB_CTX_SIZE(n) ((n) * 4u)\n#define BLOB_CTX_VERSION 2\ntypedef struct blob_ctx { unsigned int version; } blob_ctx;",
+ );
+ assert!(!vocab.macros.contains("BLOB_CTX_SIZE"));
+ assert!(vocab.macros.contains("BLOB_CTX_VERSION"));
+ }
+
+ #[test]
+ fn a_vocabulary_parse_that_does_not_type_check_is_an_error() {
+ let path = header("vocab_broken", "#include \"nope/missing.h\"\nint f(void);");
+ assert!(matches!(
+ parse_vocabulary(&path, &ParseOptions::default()),
+ Err(ExtractError::Diagnostics { .. })
+ ));
+ }
+
#[test]
fn a_record_with_no_fields_is_not_recorded() {
let vocab = vocabulary(
@@ -485,17 +569,20 @@ int png_image_begin_read_from_memory(png_image *image, const void *m, unsigned l
let mut left = Vocabulary::default();
left.records.insert(
"r".to_owned(),
- vec![Field {
- name: "a".to_owned(),
- spelling: "int".to_owned(),
- shape: Shape::Integral,
- }],
+ Record {
+ spelling: "struct r".to_owned(),
+ fields: vec![Field {
+ name: "a".to_owned(),
+ spelling: "int".to_owned(),
+ shape: Shape::Integral,
+ }],
+ },
);
let mut right = Vocabulary::default();
- right.records.insert("r".to_owned(), Vec::new());
+ right.records.insert("r".to_owned(), Record::default());
right.macros.insert("M".to_owned());
left.absorb(right);
- assert_eq!(left.records["r"].len(), 1);
+ assert_eq!(left.records["r"].fields.len(), 1);
assert!(left.macros.contains("M"));
}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/surface.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/surface.rs
index e87e940a..a5d0aa6a 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/surface.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/surface.rs
@@ -95,6 +95,7 @@ pub struct SkippedHeader {
#[derive(Debug, Clone, serde::Serialize)]
pub struct Surface {
pub source: PathBuf,
+ pub header_root: PathBuf,
pub origin: headers::HeaderOrigin,
pub headers_read: usize,
pub skipped: Vec<SkippedHeader>,
@@ -256,6 +257,7 @@ pub fn analyze(options: &SurfaceOptions) -> Result<Surface, SurfaceError> {
Ok(Surface {
source: options.source.clone(),
+ header_root: set.root.clone(),
origin: set.origin,
headers_read: set.headers.len(),
skipped,
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 446073fe..ff03abdc 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
@@ -194,14 +194,16 @@ fn harness(args: &HarnessArgs) -> Result<(), Box<dyn Error>> {
let analysis = surface::analyze(&options)?;
let mut refusals = Vec::new();
+ let root = analysis.header_root.clone();
for candidate in analysis.ranking.candidates.iter().take(args.top) {
- let root = candidate.header.parent().unwrap_or(&args.source);
let vocabulary =
- extract::parse_vocabulary(&candidate.header, &options.parse_options(root))?;
- let include = candidate.header.file_name().map_or_else(
- || candidate.header.display().to_string(),
- |name| name.to_string_lossy().into_owned(),
- );
+ extract::parse_vocabulary(&candidate.header, &options.parse_options(&root))?;
+ let include = candidate
+ .header
+ .strip_prefix(&root)
+ .unwrap_or(&candidate.header)
+ .display()
+ .to_string();
match synth::plan_candidate(candidate, &include, &vocabulary) {
Ok(plans) => {
let filename = format!("{}.c", candidate.name);