Skip to content

Commit

Permalink
Merge pull request rust-lang#41 from alexcrichton/assert-small
Browse files Browse the repository at this point in the history
Assert that diassembly is "small"
  • Loading branch information
alexcrichton authored Sep 25, 2017
2 parents 1afaf49 + c74d9ea commit 411e125
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions assert-instr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,25 @@ pub fn assert(fnptr: usize, fnname: &str, expected: &str) {

// Look for `expected` as the first part of any instruction in this
// function, returning if we do indeed find it.
let mut found = false;
for instr in function.instrs.iter() {
// Gets the first instruction, e.g. tzcntl in tzcntl %rax,%rax
if let Some(part) = instr.parts.get(0) {
// Truncates the instruction with the length of the expected
// instruction: tzcntl => tzcnt and compares that.
if part.starts_with(expected) {
return
found = true;
break
}
}
}

let probably_only_one_instruction = function.instrs.len() < 20;

if found && probably_only_one_instruction {
return
}

// Help debug by printing out the found disassembly, and then panic as we
// didn't find the instruction.
println!("disassembly for {}: ", sym.as_ref().unwrap());
Expand All @@ -277,5 +285,10 @@ pub fn assert(fnptr: usize, fnname: &str, expected: &str) {
}
println!("");
}
panic!("failed to find instruction `{}` in the disassembly", expected);

if !found {
panic!("failed to find instruction `{}` in the disassembly", expected);
} else if !probably_only_one_instruction {
panic!("too many instructions in the disassembly");
}
}

0 comments on commit 411e125

Please sign in to comment.