@@ -19,7 +19,7 @@ use capstone::{arch::x86, arch::BuildsCapstone, arch::BuildsCapstoneSyntax, Caps
19
19
pub fn roundtrip ( inst : & Inst < FuzzRegs > ) {
20
20
// Check that we can actually assemble this instruction.
21
21
let assembled = assemble ( inst) ;
22
- let expected = disassemble ( & assembled) ;
22
+ let expected = disassemble ( & assembled, inst ) ;
23
23
24
24
// Check that our pretty-printed output matches the known-good output. Trim
25
25
// off the instruction offset first.
@@ -39,37 +39,46 @@ pub fn roundtrip(inst: &Inst<FuzzRegs>) {
39
39
///
40
40
/// This will skip any traps or label registrations, but this is fine for the
41
41
/// single-instruction disassembly we're doing here.
42
- fn assemble ( insn : & Inst < FuzzRegs > ) -> Vec < u8 > {
42
+ fn assemble ( inst : & Inst < FuzzRegs > ) -> Vec < u8 > {
43
43
let mut buffer = Vec :: new ( ) ;
44
44
let offsets: Vec < i32 > = Vec :: new ( ) ;
45
- insn . encode ( & mut buffer, & offsets) ;
45
+ inst . encode ( & mut buffer, & offsets) ;
46
46
buffer
47
47
}
48
48
49
49
/// Building a new `Capstone` each time is suboptimal (TODO).
50
- fn disassemble ( assembled : & [ u8 ] ) -> String {
50
+ fn disassemble ( assembled : & [ u8 ] , original : & Inst < FuzzRegs > ) -> String {
51
51
let cs = Capstone :: new ( )
52
52
. x86 ( )
53
53
. mode ( x86:: ArchMode :: Mode64 )
54
54
. syntax ( x86:: ArchSyntax :: Att )
55
55
. detail ( true )
56
56
. build ( )
57
57
. expect ( "failed to create Capstone object" ) ;
58
- let insns = cs
58
+ let insts = cs
59
59
. disasm_all ( assembled, 0x0 )
60
60
. expect ( "failed to disassemble" ) ;
61
- assert_eq ! ( insns. len( ) , 1 , "not a single instruction: {assembled:02x?}" ) ;
62
- let insn = insns. first ( ) . expect ( "at least one instruction" ) ;
63
- assert_eq ! (
64
- assembled. len( ) ,
65
- insn. len( ) ,
66
- "\n cranelift generated {} bytes: {assembled:02x?}\n \
67
- capstone generated {} bytes: {:02x?}",
68
- assembled. len( ) ,
69
- insn. len( ) ,
70
- insn. bytes( ) ,
71
- ) ;
72
- insn. to_string ( )
61
+
62
+ if insts. len ( ) != 1 {
63
+ println ! ( "> {original}" ) ;
64
+ println ! ( " debug: {original:x?}" ) ;
65
+ println ! ( " assembled: {}" , pretty_print_hexadecimal( & assembled) ) ;
66
+ assert_eq ! ( insts. len( ) , 1 , "not a single instruction" ) ;
67
+ }
68
+
69
+ let inst = insts. first ( ) . expect ( "at least one instruction" ) ;
70
+ if assembled. len ( ) != inst. len ( ) {
71
+ println ! ( "> {original}" ) ;
72
+ println ! ( " debug: {original:x?}" ) ;
73
+ println ! ( " assembled: {}" , pretty_print_hexadecimal( & assembled) ) ;
74
+ println ! (
75
+ " capstone-assembled: {}" ,
76
+ pretty_print_hexadecimal( inst. bytes( ) )
77
+ ) ;
78
+ assert_eq ! ( assembled. len( ) , inst. len( ) , "extra bytes not disassembled" ) ;
79
+ }
80
+
81
+ inst. to_string ( )
73
82
}
74
83
75
84
fn pretty_print_hexadecimal ( hex : & [ u8 ] ) -> String {
0 commit comments