PROLEAD
A Probing-Based Leakage Detection Tool for Hardware and Software
Loading...
Searching...
No Matches
emulator.h
Go to the documentation of this file.
1#pragma once
2#ifndef EMULATOR_HPP
3#define EMULATOR_HPP
10
12
13
14#include <functional>
15#include <map>
16#include <tuple>
17#include <vector>
18#include <algorithm>
19
20//use for PRNG
21#include "boost/random/variate_generator.hpp"
22#include "boost/random/mersenne_twister.hpp"
23#include "boost/random/uniform_int.hpp"
24
25// #define DEBUG
26
27namespace mulator
28{
29 struct CPU_State
30 {
32
33 struct
34 {
35 bool N;
36 bool Z;
37 bool C;
38 bool V;
39 bool Q;
42 } psr;
43
45
51
53
55 };
56
58 {
59 public:
60 Emulator(); //todo self written constructor
62 Emulator(Architecture arch, boost::variate_generator<boost::mt19937&, boost::uniform_int<uint64_t>> ThreadPrng, uint32_t NrOfPipelineStages); //self written constructor
63 Emulator(const Emulator& other);
65
66 /*
67 * Getters for internals.
68 */
71
72 /*
73 * Sets the flash region.
74 *
75 * @param[in] offset - the starting address
76 * @param[in] size - the memory region size
77 */
78 void set_flash_region(u32 offset, u32 size);
79
80 /*
81 * Getters for the flash region.
82 */
85 u8* get_flash() const;
86
87 /*
88 * Sets the ram region.
89 *
90 * @param[in] offset - the starting address
91 * @param[in] size - the memory region size
92 */
93 void set_ram_region(u32 offset, u32 size);
94
95 /*
96 * Getters for the ram region.
97 */
100 u8* get_ram() const;
101
102 /*
103 * Read and write access to registers.
104 * Does not fire any hooks!
105 */
107 void write_register(Register reg, u32 value);
108
109 /*
110 * Read and write access to memory.
111 * Does not fire any hooks!
112 */
113 void read_memory(u32 address, u8* buffer, u32 len) const;
114 void write_memory(u32 dst_address, const u8* buffer, u32 len);
115
116 /*
117 * Starts/continues emulation of the code from the current state.
118 * Stops automatically after a maximum number of instructions or when the end address is hit.
119 */
120 ReturnCode emulate(u64 max_instructions, uint32_t randomness_start_addr,uint32_t randomness_end_addr);
121 ReturnCode emulate(u32 end_address, u64 max_instructions, uint32_t randomness_start_addr,uint32_t randomness_end_addr);
122 ReturnCode emulateInstantiation(u64 max_isntructions, ::Software::ThreadSimulationStruct& ThreadSimulation, ::Software::ProbeTrackingStruct& ProbeTracker, ::Software::HelperStruct& Helper, std::vector<std::vector<std::vector<uint8_t>>>& ProbeValues, uint32_t randomness_start_addr, uint32_t randomness_end_addr, uint32_t SimulationIndex);
123
124 /*
125 * Starts emulation for PROLEAD
126 */
127 void emulate_PROLEAD(::Software::ThreadSimulationStruct& , ::Software::ProbeTrackingStruct&, ::Software::HelperStruct&, std::vector<std::vector<std::vector<uint8_t>>>&, const int , const uint64_t , const uint32_t , const uint32_t);
128
129 /*
130 * Stop the ongoing emulation. Useful in hooks.
131 */
133
134 /*
135 * Check whether the emulator is still emulating. Useful in hooks.
136 */
137 bool is_running() const;
138
139 /*
140 * Gets the total number of executed instructions in this emulator.
141 */
142 u32 get_time() const;
143
144 /*
145 * Gets a 32 bit random value.
146 */
148
149 /*
150 * Gets the number of executed instructions in the last 'emulate' call.
151 */
153
154 /*
155 * Returns true if emulation is currently in an IT block.
156 */
157 bool in_IT_block() const;
158
159 /*
160 * Returns true if emulation is currently at the last instruction in an IT block.
161 */
162 bool last_in_IT_block() const;
163
164 /*
165 * Returns the current CPU state.
166 * By backing up the CPU state and RAM contents the state of the emulator can be backed up completely.
167 */
169
170 /*
171 * Set the emulator to a specific CPU state.
172 */
173 void set_cpu_state(const CPU_State& state);
174
176 std::vector<CPU_State> m_pipeline_cpu_states;
177
178 private:
179 InstructionDecoder m_decoder;
180
181 ReturnCode m_return_code;
182
183 MemoryRegion m_flash;
184 MemoryRegion m_ram;
185
186 CPU_State m_cpu_state;
187 u32 m_memory_shadow_register;
188 u32 m_load_memory_shadow_register;
189 u32 m_store_memory_shadow_register;
190 u32 m_emulated_time;
191 bool m_psr_updated;
192
193 boost::variate_generator<boost::mt19937&, boost::uniform_int<uint64_t>> m_prolead_prng;
194
195 u8* validate_address(u32 address);
196 void clock_cpu(uint32_t randomness_start_addr,uint32_t randomness_end_addr);
197
198 void clock_cpu_instantiation(::Software::ProbeTrackingStruct& ProbeTracker, ::Software::ThreadSimulationStruct& ThreadSimulation, ::Software::HelperStruct& Helper, std::vector<std::vector<std::vector<uint8_t>>>& ProbeValues, uint32_t randomness_start_addr, uint32_t randomness_end_addr, uint32_t SimulationIndex);
199
200 void clock_cpu_PROLEAD();
201
202 u32 read_memory_internal(u32 address, u8 bytes);
203 void write_memory_internal(u32 address, u32 value, u8 bytes);
204
205 u32 read_register_internal(Register reg);
206 void write_register_internal(Register reg, u32 value);
207
208 Condition pop_IT_condition();
209
210 bool execute_PROLEAD(const Instruction& instr, ::Software::ThreadSimulationStruct& , ::Software::ProbeTrackingStruct& , ::Software::HelperStruct&, bool, bool&, const int, const uint64_t, const uint32_t, const uint32_t, std::vector<std::vector<std::vector<uint8_t>>>&);
211 void check_shadow_register_constraints(uint32_t& next_shadow_register_value, Software::ThreadSimulationStruct& ThreadSimulation, uint32_t address, uint8_t byte);
212
213 bool execute(const Instruction& instr, uint32_t randomness_start_addr, uint32_t randomness_end_addr);
214
215 bool execute_instantiation(const Instruction& instr, ::Software::ThreadSimulationStruct& ThreadSimulation, ::Software::ProbeTrackingStruct& ProbeTracker, ::Software::HelperStruct& Helper, uint32_t randomness_start_addr, uint32_t randomness_end_addr);
216
217 bool evaluate_condition(const Instruction& instr);
218
219 void branch_write_PC(u32 address);
220 void bx_write_PC(u32 address);
221 void blx_write_PC(u32 address);
222
223 void set_exclusive_monitors(u32 address, u32 align);
224 bool exclusive_monitors_pass(u32 address, u32 align) const;
225
226 bool in_priviledged_mode() const;
227 i32 get_execution_priority() const;
228 };
229
230} // namespace mulator
231
232#include "Software/Probing.hpp"
233
234#endif
void write_memory(u32 dst_address, const u8 *buffer, u32 len)
void emulate_PROLEAD(::Software::ThreadSimulationStruct &, ::Software::ProbeTrackingStruct &, ::Software::HelperStruct &, std::vector< std::vector< std::vector< uint8_t > > > &, const int, const uint64_t, const uint32_t, const uint32_t)
u32 read_register(Register reg) const
uint32_t m_pipeline_stages
Definition: emulator.h:175
u32 get_flash_size() const
u32 get_ram_size() const
u8 * get_flash() const
Emulator(Architecture arch, boost::variate_generator< boost::mt19937 &, boost::uniform_int< uint64_t > > ThreadPrng, uint32_t NrOfPipelineStages)
Emulator(const Emulator &other)
ReturnCode emulateInstantiation(u64 max_isntructions, ::Software::ThreadSimulationStruct &ThreadSimulation, ::Software::ProbeTrackingStruct &ProbeTracker, ::Software::HelperStruct &Helper, std::vector< std::vector< std::vector< uint8_t > > > &ProbeValues, uint32_t randomness_start_addr, uint32_t randomness_end_addr, uint32_t SimulationIndex)
Architecture get_architecture() const
void write_register(Register reg, u32 value)
void read_memory(u32 address, u8 *buffer, u32 len) const
u32 get_ram_offset() const
CPU_State get_cpu_state() const
void set_cpu_state(const CPU_State &state)
bool in_IT_block() const
u8 * get_ram() const
void set_ram_region(u32 offset, u32 size)
ReturnCode emulate(u32 end_address, u64 max_instructions, uint32_t randomness_start_addr, uint32_t randomness_end_addr)
bool last_in_IT_block() const
ReturnCode emulate(u64 max_instructions, uint32_t randomness_start_addr, uint32_t randomness_end_addr)
u32 get_flash_offset() const
u32 get_time() const
std::vector< CPU_State > m_pipeline_cpu_states
Definition: emulator.h:176
Emulator(Architecture arch)
void set_flash_region(u32 offset, u32 size)
u32 get_emulated_time() const
bool is_running() const
InstructionDecoder get_decoder() const
uint64_t u64
Definition: types.h:13
uint32_t u32
Definition: types.h:12
int32_t i32
Definition: types.h:17
const int REGISTER_COUNT
Definition: registers.h:8
uint8_t u8
Definition: types.h:10
Defines a struct that track meta information for probes.
Defines a struct that tracks metadata during the simulation.
Defines a struct that tracks necessary information for thread simulation.
struct mulator::CPU_State::@0 psr
bool containing_valid_pipeline_values
Definition: emulator.h:54
u32 registers[REGISTER_COUNT]
Definition: emulator.h:31