Expand description
Identifies a sequential block of instructions within the original
bytecode sequence. That is, a sequence does not contain a jump
destination (other than at the very start), and ends either with a
terminating instruction (e.g. RETURN
, REVERT
, etc) or an
unconditional branch (to another block).
Fields§
§start: usize
Starting offset (in bytes) of this block.
end: usize
End offset (in bytes) of this block. That is the first byte which is not part of this block.
Implementations§
source§impl Block
impl Block
sourcepub fn new(start: usize, end: usize) -> Self
pub fn new(start: usize, end: usize) -> Self
Examples found in repository?
src/disassembler.rs (line 252)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
fn scan_blocks(bytes: &[u8]) -> Vec<Block> {
let mut blocks = Vec::new();
// Current position in bytecodes
let mut pc = 0;
// Identifies start of current block.
let mut start = 0;
// Parse the block
while pc < bytes.len() {
// Decode instruction at the current position
let insn = Instruction::decode(pc,&bytes);
// Increment PC for next instruction
pc = pc + insn.length(&[]);
// Check whether terminating instruction
match insn {
JUMPDEST(n) => {
// Determine whether start of this block, or next
// block.
if (pc - 1) != start {
// Start of next block
blocks.push(Block::new(start,pc-1));
start = pc - 1;
}
}
INVALID|JUMP|RETURN|REVERT|STOP => {
blocks.push(Block::new(start,pc));
start = pc;
}
_ => {}
}
}
// Append last block (if necessary)
if start != pc {
blocks.push(Block::new(start,pc));
}
// Done
blocks
}
sourcepub fn encloses(&self, pc: usize) -> bool
pub fn encloses(&self, pc: usize) -> bool
Check whether this block encloses (i.e. includes) the given bytecode address.
Examples found in repository?
src/disassembler.rs (line 138)
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
pub fn get_enclosing_block(&self, pc: usize) -> &Block {
for i in 0..self.blocks.len() {
if self.blocks[i].encloses(pc) {
return &self.blocks[i];
}
}
panic!("invalid bytecode address");
}
/// Determine whether a given block is currently considered
/// reachable or not. Observe the root block (`id=0`) is _always_
/// considered reachable.
pub fn is_block_reachable(&self, id: usize) -> bool {
id == 0 || self.contexts[id].is_reachable()
}
/// Read a slice of bytes from the bytecode program, padding with
/// zeros as necessary.
pub fn read_bytes(&self, start: usize, end: usize) -> Vec<u8> {
let n = self.bytes.len();
if start >= n {
vec![0; end-start]
} else if end > n {
// Determine lower potion
let mut slice = self.bytes[start..n].to_vec();
// Probably a more idiomatic way to do this?
for i in end .. n { slice.push(0); }
//
slice
} else {
// Easy case
self.bytes[start..end].to_vec()
}
}
/// Refine this disassembly to something (ideally) more precise
/// use a fixed point dataflow analysis. This destroys the
/// original disassembly.
pub fn refine<S>(self) -> Disassembly<'a,S>
where S:AbstractState+From<T> {
let mut contexts = Vec::new();
// Should be able to do this with a map?
for ctx in self.contexts {
contexts.push(S::from(ctx));
}
// Done
Disassembly{bytes: self.bytes, blocks: self.blocks, contexts}
}
/// Flattern the disassembly into a sequence of instructions.
pub fn to_vec(&self) -> Vec<Instruction> {
let mut insns = Vec::new();
let mut last = 0;
// 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));
}
// Update gap information
last = blk.end;
}
//
insns
}
// ================================================================
// Helpers
// ================================================================
/// Disassemble a given block into a sequence of instructions.
fn disassemble_into(&self, blk: &Block, insns: &mut Vec<Instruction>) {
let mut pc = blk.start;
// Parse the block
while pc < blk.end {
// Decode instruction at the current position
let insn = Instruction::decode(pc,&self.bytes);
// Increment PC for next instruction
pc = pc + insn.length(&[]);
//
insns.push(insn);
}
}
/// Perform a linear scan splitting out the blocks. This is an
/// over approximation of the truth, as some blocks may turn out
/// to be unreachable (e.g. they are data).
fn scan_blocks(bytes: &[u8]) -> Vec<Block> {
let mut blocks = Vec::new();
// Current position in bytecodes
let mut pc = 0;
// Identifies start of current block.
let mut start = 0;
// Parse the block
while pc < bytes.len() {
// Decode instruction at the current position
let insn = Instruction::decode(pc,&bytes);
// Increment PC for next instruction
pc = pc + insn.length(&[]);
// Check whether terminating instruction
match insn {
JUMPDEST(n) => {
// Determine whether start of this block, or next
// block.
if (pc - 1) != start {
// Start of next block
blocks.push(Block::new(start,pc-1));
start = pc - 1;
}
}
INVALID|JUMP|RETURN|REVERT|STOP => {
blocks.push(Block::new(start,pc));
start = pc;
}
_ => {}
}
}
// Append last block (if necessary)
if start != pc {
blocks.push(Block::new(start,pc));
}
// Done
blocks
}
/// Determine the enclosing block number for a given bytecode
/// address.
fn get_enclosing_block_id(&self, pc: usize) -> usize {
for i in 0..self.blocks.len() {
if self.blocks[i].encloses(pc) {
return i;
}
}
panic!("invalid bytecode address");
}
Trait Implementations§
source§impl PartialEq<Block> for Block
impl PartialEq<Block> for Block
impl Copy for Block
impl Eq for Block
impl StructuralEq for Block
impl StructuralPartialEq for Block
Auto Trait Implementations§
impl RefUnwindSafe for Block
impl Send for Block
impl Sync for Block
impl Unpin for Block
impl UnwindSafe for Block
Blanket Implementations§
§impl<T> CloneAny for Twhere
T: Any + Clone,
impl<T> CloneAny for Twhere
T: Any + Clone,
fn clone_any(&self) -> Box<dyn CloneAny + 'static, Global>
fn clone_any_send(&self) -> Box<dyn CloneAny + Send + 'static, Global>where
T: Send,
fn clone_any_sync(&self) -> Box<dyn CloneAny + Sync + 'static, Global>where
T: Sync,
fn clone_any_send_sync(
&self
) -> Box<dyn CloneAny + Send + Sync + 'static, Global>where
T: Send + Sync,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.