Struct evmil::Disassembly
source · pub struct Disassembly<'a, T = ()> { /* private fields */ }
Expand description
Identifies all contiguous code blocks within the bytecode program.
Here, a block is a sequence of bytecodes terminated by either
STOP
, REVERT
, RETURN
or JUMP
. Observe that a JUMPDEST
can only appear as the first instruction of a block. In fact,
every reachable block (except the root block) begins with a
JUMPDEST
.
Implementations§
source§impl<'a, T> Disassembly<'a, T>where
T: AbstractState,
impl<'a, T> Disassembly<'a, T>where
T: AbstractState,
sourcepub fn get_enclosing_block(&self, pc: usize) -> &Block
pub fn get_enclosing_block(&self, pc: usize) -> &Block
Get the enclosing block for a given bytecode location.
sourcepub fn is_block_reachable(&self, id: usize) -> bool
pub fn is_block_reachable(&self, id: usize) -> bool
Determine whether a given block is currently considered
reachable or not. Observe the root block (id=0
) is always
considered reachable.
Examples found in repository?
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
pub fn build(mut self) -> Self {
let mut changed = true;
//
while changed {
// Reset indicator
changed = false;
// Iterate blocks in order
for i in 0..self.blocks.len() {
// Sanity check whether block unreachable.
if !self.is_block_reachable(i) { continue; }
// Yes, is reachable so continue.
let blk = &self.blocks[i];
let mut ctx = self.contexts[i].clone();
let mut pc = blk.start;
// println!("BLOCK (start={}, end={}): {:?}", pc, blk.end, i);
// println!("CONTEXT (pc={}): {}", pc, ctx);
// Parse the block
while pc < blk.end {
// Decode instruction at the current position
let insn = Instruction::decode(pc,&self.bytes);
// Check whether a branch is possible
if insn.can_branch() && ctx.peek(0).is_known() {
// Determine branch target
let target = ctx.peek(0).unwrap();
// Determine branch context
let branch_ctx = ctx.branch(target,&insn);
// Convert target into block ID.
let block_id = self.get_enclosing_block_id(target);
// println!("Branch: target={} (block {})",target,block_id);
// println!("Before merge (pc={}): {}", pc, self.contexts[block_id]);
// Merge in updated state
changed |= self.contexts[block_id].merge(branch_ctx);
// println!("After merge (pc={}): {}", pc, self.contexts[block_id]);
}
// Apply the transfer function!
// print!("{:#08x}: {}",pc,ctx);
ctx = ctx.transfer(&insn);
// println!(" ==>\t{:?}\t==> {}",insn,ctx);
// Next instruction
pc = pc + insn.length(&[]);
}
// Merge state into following block.
if (i+1) < self.blocks.len() {
changed |= self.contexts[i+1].merge(ctx);
}
}
}
self
}
sourcepub fn read_bytes(&self, start: usize, end: usize) -> Vec<u8>
pub fn read_bytes(&self, start: usize, end: usize) -> Vec<u8>
Read a slice of bytes from the bytecode program, padding with zeros as necessary.
Examples found in repository?
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
pub fn to_vec(&self) -> Vec<Instruction> {
let mut insns = Vec::new();
// Iterate blocks in order
for i in 0..self.blocks.len() {
let blk = &self.blocks[i];
let ctx = &self.contexts[i];
// Check for reachability
if i == 0 || ctx.is_reachable() {
// Disassemble block
self.disassemble_into(blk,&mut insns);
} else {
// Not reachable, so must be data.
let data = self.read_bytes(blk.start,blk.end);
//
insns.push(DATA(data));
}
}
//
insns
}
sourcepub fn refine<S>(self) -> Disassembly<'a, S>where
S: AbstractState + From<T>,
pub fn refine<S>(self) -> Disassembly<'a, S>where
S: AbstractState + From<T>,
Refine this disassembly to something (ideally) more precise use a fixed point dataflow analysis. This destroys the original disassembly.
sourcepub fn to_vec(&self) -> Vec<Instruction>
pub fn to_vec(&self) -> Vec<Instruction>
Flattern the disassembly into a sequence of instructions.