1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use yaxpeax_arch::Arch;
use memory::MemoryRange;
use arch::DecodeFrom;
use arch::FunctionQuery;
use arch::FunctionImpl;
use data::Disambiguator;
use data::LocIterator;
use data::LocationAliasDescriptions;
use data::modifier::ModifierCollection;
use data::modifier::NoModifiers;
use analyses::static_single_assignment::SSAValues;
use analyses::control_flow::ControlFlowGraph;
use analyses::static_single_assignment::SSA;
use analyses::static_single_assignment::cytron::{generate_ssa, generate_refined_ssa};
use arch::AbiDefaults;

use data::Direction;

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Use {
    Read, Write, ReadWrite
}

impl Use {
    pub fn first_use(&self) -> Direction {
        match self {
            Use::Read | Use::ReadWrite => {
                Direction::Read
            },
            Use::Write => {
                Direction::Write
            }
        }
    }
}

pub struct AnalysisBuilder<
    'memory,
    'cfg,
    'functions,
    'disambiguator,
    'modifiers,
    A: Arch,
    M: ?Sized,
    F,
    LocSpec,
    D,
    U,
> {
    memory: &'memory M,
    cfg: &'cfg ControlFlowGraph<A::Address>,
    functions: &'functions F,
    loc_spec: std::marker::PhantomData<LocSpec>,
    disambiguator: &'disambiguator mut D,
    modifiers: &'modifiers U,
}

impl<
    'memory,
    'cfg,
    'functions,
    'disambiguator,
    'modifiers,
    A: Arch + SSAValues,
    M: MemoryRange<A> + ?Sized,
    F: FunctionQuery<A::Address, Function=FunctionImpl<A::Location>>,
    LocSpec,
    D: Disambiguator<A, LocSpec>,
> AnalysisBuilder<'memory, 'cfg, 'functions, 'disambiguator, 'modifiers, A, M, F, LocSpec, D, NoModifiers> where
    A::Location: 'static + AbiDefaults,
    for<'a> &'a <A as Arch>::Instruction: LocIterator<'disambiguator, 'functions, A, A::Location, D, F, Item=(Option<A::Location>, Direction), LocSpec=LocSpec>
{
    pub fn new(memory: &'memory M, cfg: &'cfg ControlFlowGraph<A::Address>, functions: &'functions F, disambiguator: &'disambiguator mut D) -> AnalysisBuilder<'memory, 'cfg, 'functions, 'disambiguator, 'static, A, M, F, LocSpec, D, NoModifiers> {
        AnalysisBuilder {
            memory,
            cfg,
            functions,
            loc_spec: std::marker::PhantomData,
            disambiguator,
            modifiers: &NoModifiers,
        }
    }
}

use data::ValueLocations;
use analyses::static_single_assignment::DFGRebase;
impl<
    'memory,
    'cfg,
    'functions,
    'disambiguator,
    'modifiers,
    A: Arch + SSAValues + for<'mem> DecodeFrom<M>,
    M: MemoryRange<A> + ?Sized,
    F: FunctionQuery<A::Address, Function=FunctionImpl<A::Location>>,
    LocSpec,
    D: Disambiguator<A, LocSpec> + LocationAliasDescriptions<A>,
    U: ModifierCollection<A>,
> AnalysisBuilder<'memory, 'cfg, 'functions, 'disambiguator, 'modifiers, A, M, F, LocSpec, D, U> where
    A::Location: 'static + AbiDefaults,
    for<'a> &'a <A as Arch>::Instruction: LocIterator<'disambiguator, 'functions, A, A::Location, D, F, Item=(Option<A::Location>, Direction), LocSpec=LocSpec>
{
    pub fn with_modifiers<'new_modifiers, NewU: ModifierCollection<A>>(self, new_modifiers: &'new_modifiers NewU) -> AnalysisBuilder<'memory, 'cfg, 'functions, 'disambiguator, 'new_modifiers, A, M, F, LocSpec, D, NewU> {
        let Self {
            memory,
            cfg,
            functions,
            loc_spec,
            disambiguator,
            ..
        } = self;

        AnalysisBuilder {
            memory,
            cfg,
            functions,
            loc_spec,
            disambiguator,
            modifiers: new_modifiers,
        }
    }

    pub fn ssa_cytron(self) -> SSA<A> {
        let Self {
            memory,
            cfg,
            functions,
            disambiguator,
            modifiers,
            ..
        } = self;

        generate_ssa(memory, cfg.entrypoint, &cfg, &cfg.graph, modifiers, disambiguator, functions)
    }

    pub fn ssa_cytron_refining(self, prior_dfg: &SSA<A>) -> SSA<A> where <A as ValueLocations>::Location: DFGRebase<A> {
        let Self {
            memory,
            cfg,
            functions,
            disambiguator,
            modifiers,
            ..
        } = self;

        generate_refined_ssa(memory, cfg.entrypoint, &cfg, &cfg.graph, prior_dfg, modifiers, disambiguator, functions)
    }
}