use super::image::{Image, PointsTo};
use super::tables::*;
use super::types::named_by;
pub(super) fn graph_node_struct(img: &Image, name: &str, addr: u32) -> Option<(u32, u16)> {
if name == "GraphNode" {
let t = u16::from_le_bytes(
img.mem
.get(addr as usize..addr as usize + 2)?
.try_into()
.ok()?,
);
let &(_, n) = NODE_TYPES.iter().find(|x| x.0 == t)?;
let (prev, next) = (img.read(addr + 4)?, img.read(addr + 8)?);
if prev == 0 || next == 0 || img.read(next + 4)? != addr || img.read(prev + 8)? != addr {
return None;
}
return named_by(n);
}
named_by(name)
}
pub(super) fn plausible(img: &Image, name: &str, addr: u32) -> bool {
match name {
"SpawnInfo" => {
let ok = |v: Option<u32>| {
v.is_some_and(|v| {
matches!(
img.classify(v),
PointsTo::Null | PointsTo::Heap(..) | PointsTo::Global(_)
)
})
};
img.read(addr + 0x14)
.is_some_and(|b| match img.classify(b) {
PointsTo::Global(g) => BEHAVIOR_POINTER_SLOTS
.binary_search_by_key(&g, |x| x.0)
.is_ok(),
_ => false,
})
&& ok(img.read(addr + 0x18))
&& ok(img.read(addr + 0x1c))
}
"AllocOnlyPool" => {
let (Some(last), Some(size), Some(next)) =
(img.read(addr), img.read(addr + 4), img.read(addr + 8))
else {
return false;
};
next <= size
&& match img.classify(last) {
PointsTo::Null | PointsTo::Global(_) => true,
PointsTo::Heap(ci, off) => off == 0 && img.chunks[ci].len >= size,
PointsTo::Unresolved => false,
}
}
"MemoryPool" => img.read(addr).is_some_and(|p| match img.classify(p) {
PointsTo::Heap(..) => plausible(img, "AllocOnlyPool", p),
_ => false,
}),
"Object" => false,
"Surface" => img
.read(addr + 0x2c)
.is_some_and(|o| matches!(img.classify(o), PointsTo::Null | PointsTo::Global(_))),
_ => true,
}
}