MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / disassemble

Function disassemble

cranelift/assembler-x64/src/fuzz.rs:118–150  ·  view source on GitHub ↗

Building a new `Capstone` each time is suboptimal (TODO).

(assembled: &[u8], original: &Inst<FuzzRegs>)

Source from the content-addressed store, hash-verified

116
117/// Building a new `Capstone` each time is suboptimal (TODO).
118fn disassemble(assembled: &[u8], original: &Inst<FuzzRegs>) -> String {
119 let cs = Capstone::new()
120 .x86()
121 .mode(x86::ArchMode::Mode64)
122 .syntax(x86::ArchSyntax::Att)
123 .detail(true)
124 .build()
125 .expect("failed to create Capstone object");
126 let insts = cs
127 .disasm_all(assembled, 0x0)
128 .expect("failed to disassemble");
129
130 if insts.len() != 1 {
131 println!("> {original}");
132 println!(" debug: {original:x?}");
133 println!(" assembled: {}", pretty_print_hexadecimal(&assembled));
134 assert_eq!(insts.len(), 1, "not a single instruction");
135 }
136
137 let inst = insts.first().expect("at least one instruction");
138 if assembled.len() != inst.len() {
139 println!("> {original}");
140 println!(" debug: {original:x?}");
141 println!(" assembled: {}", pretty_print_hexadecimal(&assembled));
142 println!(
143 " capstone-assembled: {}",
144 pretty_print_hexadecimal(inst.bytes())
145 );
146 assert_eq!(assembled.len(), inst.len(), "extra bytes not disassembled");
147 }
148
149 inst.to_string()
150}
151
152fn pretty_print_hexadecimal(hex: &[u8]) -> String {
153 use core::fmt::Write;

Callers 1

roundtripFunction · 0.85

Calls 6

newFunction · 0.50
expectMethod · 0.45
buildMethod · 0.45
lenMethod · 0.45
firstMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected