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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! A driver concretely executes a Falcon IL programs.

use architecture::Architecture;
use error::*;
use executor::State;
use executor::successor::*;
use il;
use RC;

/// A driver for a concrete executor over Falcon IL.
#[derive(Debug, Clone)]
pub struct Driver {
    program: RC<il::Program>,
    location: il::ProgramLocation,
    state: State,
    architecture: RC<Architecture>,
}


impl Driver {
    /// Create a new driver for concrete execution over Falcon IL.
    pub fn new(
        program: RC<il::Program>,
        location: il::ProgramLocation,
        state: State,
        architecture: RC<Architecture>,
    ) -> Driver {
        Driver {
            program: program,
            location: location,
            state: state,
            architecture: architecture
        }
    }

    /// Step forward over Falcon IL.
    pub fn step(self) -> Result<Driver> {
        let location =
            self.location
                .apply(&self.program)
                .ok_or("Failed to apply program location")?;
        match *location.function_location() {
            il::RefFunctionLocation::Instruction(_, instruction) => {
                let successor = self.state.execute(instruction.operation())?;

                match successor.type_().clone() {
                    SuccessorType::FallThrough => {
                        let locations = location.forward()?;
                        if locations.len() == 1 {
                            return Ok(Driver::new(
                                self.program.clone(),
                                locations[0].clone().into(),
                                successor.into(),
                                self.architecture
                            ));
                        }
                        else {
                            // every location should be an edge, and only one
                            // edge should be satisfiable
                            for location in locations {
                                if let il::RefFunctionLocation::Edge(edge) = *location.function_location() {
                                    if successor.state()
                                                .symbolize_and_eval(
                                                    edge.condition()
                                                        .ok_or("Failed to get edge condition")?)?
                                                .is_one() {
                                        return Ok(Driver::new(
                                            self.program.clone(),
                                            location.clone().into(),
                                            successor.into(),
                                            self.architecture
                                        ));
                                    }
                                }
                            }
                            bail!("No valid successor location found on fall through");
                        }
                    },
                    SuccessorType::Branch(address) => {
                        match il::RefProgramLocation::from_address(&self.program, address) {
                            Some(location) => return Ok(Driver::new(
                                self.program.clone(),
                                location.into(),
                                successor.into(),
                                self.architecture
                            )),
                            None => {
                                let state: State = successor.into();
                                let function = self.architecture
                                                   .translator()
                                                   .translate_function(state.memory(), address)
                                                   .expect(&format!("Failed to lift function at 0x{:x}", address));
                                let mut program = self.program.clone();
                                RC::make_mut(&mut program).add_function(function);
                                let location: il::ProgramLocation =
                                    il::RefProgramLocation::from_address(&program,
                                                                         address)
                                        .ok_or("Failed to get location for newly lifted function")?
                                        .into();
                                return Ok(Driver::new(
                                    program.clone(),
                                    location,
                                    state,
                                    self.architecture
                                ));
                            }
                        }
                    },
                    SuccessorType::Intrinsic(ref intrinsic) => {
                        bail!(format!("Intrinsic is unimplemented, {}",
                                      intrinsic.instruction_str()));
                    }
                }
            },
            il::RefFunctionLocation::Edge(_) => {
                let locations = location.forward()?;
                return Ok(Driver::new(
                    self.program.clone(),
                    locations[0].clone().into(),
                    self.state,
                    self.architecture
                ));
            },
            il::RefFunctionLocation::EmptyBlock(_) => {
                let locations = location.forward()?;
                if locations.len() == 1 {
                    return Ok(Driver::new(
                        self.program.clone(),
                        locations[0].clone().into(),
                        self.state,
                        self.architecture
                    ));
                }
                else {
                    for location in locations {
                        if let il::RefFunctionLocation::Edge(edge) = *location.function_location() {
                            if self.state
                                   .symbolize_and_eval(
                                        edge.condition()
                                            .ok_or("Failed to get edge condition")?)?
                                   .is_one() {
                                return Ok(Driver::new(
                                    self.program.clone(),
                                    location.clone().into(),
                                    self.state,
                                    self.architecture
                                ));
                            }
                        }
                    }
                }
                bail!("No valid location out of empty block");
            }
        }
    }

    /// Retrieve the Falcon IL program associated with this driver.
    pub fn program(&self) -> &il::Program {
        &self.program
    }

    /// If this driver is sitting on an instruction with an address, return
    /// that address.
    pub fn address(&self) -> Option<u64> {
        self.location
            .apply(&self.program)
            .expect("Failed to apply program location")
            .address()
    }

    /// Retrieve the `il::ProgramLocation` associated with this driver.
    pub fn location(&self) -> &il::ProgramLocation {
        &self.location
    }

    /// Retrieve the concrete `State` associated with this driver.
    pub fn state(&self) -> &State {
        &self.state
    }

    /// Retrieve a mutable reference to the `State` associated with this driver.
    pub fn state_mut(&mut self) -> &mut State {
        &mut self.state
    }
}