Merge pull request #41 from alexcrichton/assert-small

Assert that diassembly is "small"
This commit is contained in:
Alex Crichton 2017-09-25 16:10:36 -05:00 committed by GitHub
commit 299b2f3c29

View file

@ -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());
@ -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");
}
}