File tree 2 files changed +46
-0
lines changed
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ libc = { version = "0.2", default-features = false }
21
21
[dev-dependencies ]
22
22
macho = " 0.*"
23
23
criterion = " 0.2"
24
+ rayon = " 1.1"
24
25
25
26
[[bench ]]
26
27
name = " my_benchmark"
Original file line number Diff line number Diff line change
1
+ //! This example shows how to disassemble in parallel. You need a separate `Capstone` struct for
2
+ //! each thread.
3
+ //!
4
+ //! We shard the input by using parallel iterators from the rayon crate.
5
+
6
+ use capstone:: prelude:: * ;
7
+ use rayon:: prelude:: * ;
8
+
9
+ fn main ( ) -> CsResult < ( ) > {
10
+ // Closure to create `Capstone` instance
11
+ let create_cs = || -> CsResult < Capstone > {
12
+ let cs = Capstone :: new ( )
13
+ . x86 ( )
14
+ . mode ( arch:: x86:: ArchMode :: Mode64 )
15
+ . detail ( true )
16
+ . build ( ) ?;
17
+ Ok ( cs)
18
+ } ;
19
+
20
+ // Slice of code to disassemble
21
+ let input_code: & [ & [ u8 ] ] = & [
22
+ b"\x55 \x48 \x8b \x05 \xb8 \x13 \x00 \x00 \xe9 \x14 \x9e \x08 \x00 \x45 \x31 \xe4 " ,
23
+ b"\x90 \x41 \xe8 \x04 \x03 \x02 \x01 " ,
24
+ b"\xff \xff \xff \xff \xff " ,
25
+ ] ;
26
+
27
+ let results: Vec < CsResult < Vec < String > > > = input_code
28
+ . par_iter ( ) // iterate in parallel
29
+ . map ( |bytes| {
30
+ // map input byte to output mnemonic
31
+ let cs = create_cs ( ) ?;
32
+ let insns = cs. disasm_all ( bytes, 0x1000 ) ?;
33
+ let result: Option < Vec < String > > = insns
34
+ . iter ( )
35
+ . map ( |insn| -> Option < String > { Some ( insn. mnemonic ( ) ?. to_string ( ) ) } )
36
+ . collect ( ) ;
37
+ let result = result. ok_or ( capstone:: Error :: CustomError ( "No mnemonic" ) ) ?;
38
+ Ok ( result)
39
+ } )
40
+ . collect ( ) ;
41
+
42
+ println ! ( "{:#?}" , results) ;
43
+
44
+ Ok ( ( ) )
45
+ }
You can’t perform that action at this time.
0 commit comments