vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
pub(crate) mod abi;
mod call_arguments;
mod call_return;
pub mod calls;
mod clobbers;
mod com;
mod constant_spills;
mod dead_code;
pub mod declarations;
mod exceptions;
mod float_result;
mod go_entry;
pub mod go_types;
mod indirect;
mod indirect_result;
pub(crate) use indirect::constant_branch_targets;
mod jump_table;
pub mod layouts;
mod lifted;
mod mips_calls;
pub mod navigation;
mod optimization;
mod pairs32;
pub mod refs;
pub mod results;
pub mod scales;
#[cfg(test)]
mod semantics_tests;
pub mod shared_structures;
pub(crate) mod signature;
mod stack_protector;
mod structures;
pub mod summary;
pub mod typing;
pub mod value_width;
pub mod variables;
pub mod variadic;
pub mod widths;

use std::time::Instant;
use vlk_program::program::Program;
use vlk_prototypes::prototypes;

pub use lifted::absorb_jump_table_cases;
use lifted::lift_function;
use optimization::optimize;
use refs::Reference;
pub use summary::summary;
use vlk_base::deadline::passed;
use vlk_base::error::Error;
use vlk_ir::ir::FunctionIr;
use vlk_loader::loader::Function;
use vlk_types::types::CType;

/// Bytes lifted for a function whose extent neither symbols nor scanning give.
pub const UNKNOWN_SIZE: u64 = 256;

#[derive(Debug, Clone)]
pub struct Analysis {
    /// Value-preserving parameter types recovered for direct calls to imports.
    pub import_parameters: std::collections::BTreeMap<u64, Vec<CType>>,
    /// Parameters unused by a callee's body, merged from its direct callers.
    pub unused_parameters: std::collections::BTreeMap<u64, prototypes::Prototype>,
    /// Recovered callee parameters that require a prototype under default promotions.
    pub callee_parameters: std::collections::BTreeMap<u64, Vec<CType>>,
    /// For a callee whose arguments a call passes in another order than it
    /// declares its parameters: the argument each parameter takes, in the
    /// order of the parameters.
    pub callee_orders: std::collections::BTreeMap<u64, Vec<usize>>,
    /// Predicate and dispatcher removals proved during analysis.
    pub simplification: crate::passes::deobfuscation::Simplification,
    pub ir: FunctionIr,
    pub references: Vec<Reference>,
}

impl Analysis {
    #[must_use]
    pub fn strings(&self) -> Vec<String> {
        self.references
            .iter()
            .filter_map(|r| r.text.clone())
            .collect()
    }
}

pub fn analyze(
    program: &Program,
    function: &Function,
    deadline: Option<Instant>,
) -> Result<Analysis, Error> {
    passed(deadline)?;
    let lifted = lift_function(program, function);
    passed(deadline)?;
    let lifted = match lifted {
        Ok(lifted) => lifted,
        Err(source) => {
            return Err(Error::Lift {
                addr: function.addr,
                source,
            });
        }
    };
    optimize(program, lifted, deadline)
}

#[cfg(test)]
mod tests;