vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
pub mod coalesce;
pub mod copies;
pub mod cse;
pub mod dce;
pub mod deobfuscation;
pub mod division;
pub mod frame;
pub mod frame_slots;
pub mod layout;
mod mba;
pub mod memory;
pub mod read_once;
pub mod simplify;
pub mod widths;

use vlk_ir::ir::FunctionIr;

/// Runs the pass `site`; with the `validate` feature, records an SMT
/// obligation for each statement it rewrites.
#[inline]
pub(crate) fn checked<R>(
    site: &'static str,
    ir: &mut FunctionIr,
    run: impl FnOnce(&mut FunctionIr) -> R,
) -> R {
    checked_assuming(site, ir, &Callees::default(), run)
}

/// What the summaries of a function's callees tell a pass that relies on
/// them.
#[derive(Debug, Default)]
pub struct Callees {
    /// The callees that return 32-bit values.
    pub narrow_results: Vec<u64>,
    /// The callees that take no result address in `AArch64`'s x8.
    pub without_result_address: Vec<u64>,
}

/// The same for a pass that relies on what `callees` says.
#[inline]
pub(crate) fn checked_assuming<R>(
    site: &'static str,
    ir: &mut FunctionIr,
    callees: &Callees,
    run: impl FnOnce(&mut FunctionIr) -> R,
) -> R {
    #[cfg(feature = "validate")]
    if let Some(validation) = VALIDATION.get()
        && (validation.enabled)()
    {
        let before = ir.blocks.clone();
        let result = run(ir);
        (validation.applied)(site, before, ir, callees);
        return result;
    }
    #[cfg(not(feature = "validate"))]
    let _ = (site, callees);
    run(ir)
}

/// What checks each pass in a build with the `validate` feature: whether
/// the checks are on, and what is told of a pass that has run, with the
/// blocks it was given.
#[cfg(feature = "validate")]
pub struct Validation {
    pub enabled: fn() -> bool,
    pub applied: fn(&'static str, Vec<vlk_ir::ir::Block>, &FunctionIr, &Callees),
}

/// Set by the code that checks, when checking is first asked for.
#[cfg(feature = "validate")]
pub static VALIDATION: std::sync::OnceLock<Validation> = std::sync::OnceLock::new();

/// A call record that names `dependencies` as the registers the call reads,
/// for tests.
#[cfg(test)]
pub(crate) fn test_call_abi(
    dependencies: Vec<u16>,
    deferred_memory: bool,
) -> std::sync::Arc<vlk_types::types::calls::CallAbi> {
    use std::sync::Arc;

    use vlk_types::types::calls::{CallAbi, CallDeclaration, CallFallback};
    use vlk_types::types::{AbiIssue, CType, Convention, DeclaredSignature};

    Arc::new(CallAbi {
        kind: vlk_types::types::CallKind::Invoke,
        declaration: Arc::new(CallDeclaration {
            signature: DeclaredSignature {
                params: Vec::new(),
                ret: CType::Void,
                variadic: false,
                abi: Err(AbiIssue::UnknownLayout),
            },
            sources: Vec::new(),
        }),
        target: 0,
        convention: Convention::Aapcs64,
        arguments: Vec::new(),
        hidden_result: None,
        dependencies,
        deferred_memory,
        fallback: CallFallback::Named {
            count: 0,
            floats_named: true,
        },
    })
}

/// Functions with recovered jump tables skip the flow-sensitive cleanups.
pub fn optimize(ir: &mut FunctionIr, has_jump_tables: bool) -> deobfuscation::Simplification {
    cse::cse(ir);
    simplify::simplify(ir);
    division::recover_divisions(ir);
    simplify::simplify(ir);
    frame::elide_frame_pointer(ir);
    frame::elide_callee_saves(ir);
    copies::copy_propagate(ir);
    dce::dce(ir);
    // The pop matching the push goes first when nothing reads it.
    frame::drop_alignment_pushes(ir);
    if !has_jump_tables {
        dce::dce_flow(ir);
    }
    simplify::simplify(ir);
    copies::copy_propagate_guarded(ir);
    dce::dce(ir);
    if !has_jump_tables {
        dce::dce_flow(ir);
        memory::forward_stored_values(ir);
        copies::inline_local_copies(ir);
        copies::copy_propagate_guarded(ir);
        simplify::simplify(ir);
        dce::dce(ir);
    }
    memory::fuse_load_op_store(ir);
    let mut changes = deobfuscation::predicates(ir);
    changes.mba_expressions_simplified = mba::simplify(ir);
    changes.simplified |= changes.mba_expressions_simplified != 0;
    simplify::simplify(ir);
    dce::dce(ir);
    changes
}