11use super :: Block ;
22use crate :: asid:: Asid ;
33use crate :: config:: Config ;
4+ use crate :: error:: {
5+ deref_ptresult, deref_ptresult_mut, ensure_ptok, extract_pterr, PtError , PtErrorCode ,
6+ } ;
47use crate :: event:: Event ;
58use crate :: flags:: Status ;
69use crate :: image:: Image ;
7- use crate :: error:: {
8- PtError , ensure_ptok,
9- extract_pterr, deref_ptresult,
10- deref_ptresult_mut, PtErrorCode
11- } ;
1210
11+ use std:: marker:: PhantomData ;
1312use std:: mem;
1413use std:: ptr;
15- use std:: marker:: PhantomData ;
1614
1715use libipt_sys:: {
18- pt_event,
19- pt_block,
20- pt_block_decoder,
21- pt_blk_alloc_decoder,
22- pt_blk_core_bus_ratio,
23- pt_blk_free_decoder,
24- pt_blk_get_config,
25- pt_blk_get_image,
26- pt_blk_get_offset,
27- pt_blk_get_sync_offset,
28- pt_blk_set_image,
29- pt_blk_sync_backward,
30- pt_blk_sync_forward,
31- pt_blk_sync_set,
32- pt_blk_time,
33- pt_blk_next,
34- pt_blk_event,
35- pt_blk_asid,
36- pt_asid
16+ pt_asid, pt_blk_alloc_decoder, pt_blk_asid, pt_blk_core_bus_ratio, pt_blk_event,
17+ pt_blk_free_decoder, pt_blk_get_config, pt_blk_get_image, pt_blk_get_offset,
18+ pt_blk_get_sync_offset, pt_blk_next, pt_blk_set_image, pt_blk_sync_backward,
19+ pt_blk_sync_forward, pt_blk_sync_set, pt_blk_time, pt_block, pt_block_decoder, pt_event,
3720} ;
3821
3922#[ cfg( test) ]
@@ -44,23 +27,19 @@ mod test {
4427 #[ test]
4528 fn test_blkdec_alloc ( ) {
4629 let kek = & mut [ 1 ; 2 ] ;
47- BlockDecoder :: new (
48- & ConfigBuilder :: new ( kek) . unwrap ( ) . finish ( )
49- ) . unwrap ( ) ;
30+ BlockDecoder :: new ( & ConfigBuilder :: new ( kek) . unwrap ( ) . finish ( ) ) . unwrap ( ) ;
5031 }
5132
52- #[ test ]
33+ #[ test]
5334 fn test_blkdec_props ( ) {
5435 let kek = & mut [ 1 ; 2 ] ;
5536 // this just checks memory safety for property access
5637 // usage can be found in the integration tests
57- let mut b = BlockDecoder :: new (
58- & ConfigBuilder :: new ( kek) . unwrap ( ) . finish ( )
59- ) . unwrap ( ) ;
38+ let mut b = BlockDecoder :: new ( & ConfigBuilder :: new ( kek) . unwrap ( ) . finish ( ) ) . unwrap ( ) ;
6039 let a = b. asid ( ) . unwrap ( ) ;
6140 assert ! ( a. cr3( ) . is_none( ) ) ;
6241 assert ! ( a. vmcs( ) . is_none( ) ) ;
63- assert ! ( b. core_bus_ratio( ) . is_err ( ) ) ;
42+ assert ! ( b. core_bus_ratio( ) . is_ok ( ) ) ;
6443 assert ! ( b. event( ) . is_err( ) ) ;
6544 assert ! ( b. config( ) . is_ok( ) ) ;
6645 assert ! ( b. image( ) . unwrap( ) . name( ) . is_none( ) ) ;
@@ -69,7 +48,7 @@ mod test {
6948 assert ! ( b. next( ) . is_err( ) ) ;
7049 assert ! ( b. sync_backward( ) . is_err( ) ) ;
7150 assert ! ( b. sync_forward( ) . is_err( ) ) ;
72- assert ! ( b. time( ) . is_err ( ) ) ;
51+ assert ! ( b. time( ) . is_ok ( ) ) ;
7352 }
7453}
7554
@@ -89,7 +68,7 @@ impl<'a, T> BlockDecoder<'a, T> {
8968 pub fn new ( cfg : & Config < T > ) -> Result < Self , PtError > {
9069 // deref_ptresult(unsafe{ pt_blk_alloc_decoder(&cfg.0) })
9170 // .map(|x| BlockDecoder::<T>(*x, PhantomData))
92- deref_ptresult_mut ( unsafe { pt_blk_alloc_decoder ( cfg. 0 . as_ref ( ) ) } )
71+ deref_ptresult_mut ( unsafe { pt_blk_alloc_decoder ( cfg. 0 . as_ref ( ) ) } )
9372 . map ( |x| BlockDecoder :: < T > ( x, PhantomData ) )
9473 }
9574
@@ -99,10 +78,7 @@ impl<'a, T> BlockDecoder<'a, T> {
9978 /// Returns Asid on success, a PtError otherwise.
10079 pub fn asid ( & self ) -> Result < Asid , PtError > {
10180 let mut a: Asid = Default :: default ( ) ;
102- unsafe {
103- ensure_ptok ( pt_blk_asid ( self . 0 , & mut a. 0 , mem:: size_of :: < pt_asid > ( ) ) )
104- . map ( |_| a)
105- }
81+ unsafe { ensure_ptok ( pt_blk_asid ( self . 0 , & mut a. 0 , mem:: size_of :: < pt_asid > ( ) ) ) . map ( |_| a) }
10682 }
10783
10884 /// Return the current core bus ratio.
@@ -121,25 +97,20 @@ impl<'a, T> BlockDecoder<'a, T> {
12197 /// Returns BadQuery if there is no event.
12298 pub fn event ( & mut self ) -> Result < ( Event , Status ) , PtError > {
12399 let mut evt: pt_event = unsafe { mem:: zeroed ( ) } ;
124- extract_pterr ( unsafe {
125- pt_blk_event ( self . 0 ,
126- & mut evt,
127- mem:: size_of :: < pt_event > ( ) )
128- } ) . map ( |s| ( Event ( evt) , Status :: from_bits ( s) . unwrap ( ) ) )
100+ extract_pterr ( unsafe { pt_blk_event ( self . 0 , & mut evt, mem:: size_of :: < pt_event > ( ) ) } )
101+ . map ( |s| ( Event ( evt) , Status :: from_bits ( s) . unwrap ( ) ) )
129102 }
130103
131104 pub fn config ( & self ) -> Result < Config < T > , PtError > {
132- deref_ptresult ( unsafe { pt_blk_get_config ( self . 0 ) } )
133- . map ( Config :: from)
105+ deref_ptresult ( unsafe { pt_blk_get_config ( self . 0 ) } ) . map ( Config :: from)
134106 }
135107
136108 /// Get the traced image.
137109 ///
138110 /// The returned image may be modified as long as @decoder is not running.
139111 /// Returns the traced image the decoder uses for reading memory.
140112 pub fn image ( & mut self ) -> Result < Image , PtError > {
141- deref_ptresult_mut ( unsafe { pt_blk_get_image ( self . 0 ) } )
142- . map ( Image :: from)
113+ deref_ptresult_mut ( unsafe { pt_blk_get_image ( self . 0 ) } ) . map ( Image :: from)
143114 }
144115
145116 /// Get the current decoder position.
@@ -148,17 +119,15 @@ impl<'a, T> BlockDecoder<'a, T> {
148119 /// Returns Nosync if decoder is out of sync.
149120 pub fn offset ( & self ) -> Result < u64 , PtError > {
150121 let mut off: u64 = 0 ;
151- ensure_ptok ( unsafe { pt_blk_get_offset ( self . 0 , & mut off) } )
152- . map ( |_| off)
122+ ensure_ptok ( unsafe { pt_blk_get_offset ( self . 0 , & mut off) } ) . map ( |_| off)
153123 }
154124
155125 /// Get the position of the last synchronization point.
156126 ///
157127 /// Returns Nosync if the decoder is out of sync.
158128 pub fn sync_offset ( & self ) -> Result < u64 , PtError > {
159129 let mut off: u64 = 0 ;
160- ensure_ptok ( unsafe { pt_blk_get_sync_offset ( self . 0 , & mut off) } )
161- . map ( |_| off)
130+ ensure_ptok ( unsafe { pt_blk_get_sync_offset ( self . 0 , & mut off) } ) . map ( |_| off)
162131 }
163132
164133 /// Determine the next block of instructions.
@@ -176,13 +145,8 @@ impl<'a, T> BlockDecoder<'a, T> {
176145 /// Returns Nosync if the decoder is out of sync.
177146 pub fn next ( & mut self ) -> Result < ( Block , Status ) , PtError > {
178147 let mut blk: pt_block = unsafe { mem:: zeroed ( ) } ;
179- extract_pterr (
180- unsafe {
181- pt_blk_next ( self . 0 ,
182- & mut blk,
183- mem:: size_of :: < pt_block > ( ) )
184- }
185- ) . map ( |s| ( Block ( blk) , Status :: from_bits ( s) . unwrap ( ) ) )
148+ extract_pterr ( unsafe { pt_blk_next ( self . 0 , & mut blk, mem:: size_of :: < pt_block > ( ) ) } )
149+ . map ( |s| ( Block ( blk) , Status :: from_bits ( s) . unwrap ( ) ) )
186150 }
187151
188152 /// Set the traced image.
@@ -192,11 +156,13 @@ impl<'a, T> BlockDecoder<'a, T> {
192156 /// Only one image can be active at any time.
193157 pub fn set_image ( & mut self , img : Option < & mut Image > ) -> Result < ( ) , PtError > {
194158 ensure_ptok ( unsafe {
195- pt_blk_set_image ( self . 0 ,
196- match img {
197- None => ptr:: null_mut ( ) ,
198- Some ( i) => i. inner
199- } )
159+ pt_blk_set_image (
160+ self . 0 ,
161+ match img {
162+ None => ptr:: null_mut ( ) ,
163+ Some ( i) => i. inner ,
164+ } ,
165+ )
200166 } )
201167 }
202168
@@ -214,8 +180,7 @@ impl<'a, T> BlockDecoder<'a, T> {
214180 /// Returns BadPacket if an unknown packet payload is encountered.
215181 /// Returns Eos if no further synchronization point is found.
216182 pub fn sync_forward ( & mut self ) -> Result < Status , PtError > {
217- extract_pterr ( unsafe { pt_blk_sync_forward ( self . 0 ) } )
218- . map ( |s| Status :: from_bits ( s) . unwrap ( ) )
183+ extract_pterr ( unsafe { pt_blk_sync_forward ( self . 0 ) } ) . map ( |s| Status :: from_bits ( s) . unwrap ( ) )
219184 }
220185
221186 /// Manually synchronize an Intel PT block decoder.
@@ -227,7 +192,7 @@ impl<'a, T> BlockDecoder<'a, T> {
227192 /// Returns Eos if the decoder reaches the end of its trace buffer.
228193 /// Returns Nosync if there is no syncpoint at @offset.
229194 pub fn set_sync ( & mut self , offset : u64 ) -> Result < ( ) , PtError > {
230- ensure_ptok ( unsafe { pt_blk_sync_set ( self . 0 , offset) } )
195+ ensure_ptok ( unsafe { pt_blk_sync_set ( self . 0 , offset) } )
231196 }
232197
233198 /// Return the current time.
@@ -247,14 +212,8 @@ impl<'a, T> BlockDecoder<'a, T> {
247212 let mut time: u64 = 0 ;
248213 let mut lost_mtc: u32 = 0 ;
249214 let mut lost_cyc: u32 = 0 ;
250- ensure_ptok (
251- unsafe {
252- pt_blk_time ( self . 0 ,
253- & mut time,
254- & mut lost_mtc,
255- & mut lost_cyc)
256- }
257- ) . map ( |_| ( time, lost_mtc, lost_cyc) )
215+ ensure_ptok ( unsafe { pt_blk_time ( self . 0 , & mut time, & mut lost_mtc, & mut lost_cyc) } )
216+ . map ( |_| ( time, lost_mtc, lost_cyc) )
258217 }
259218}
260219
@@ -265,7 +224,7 @@ impl<'a, T> Iterator for BlockDecoder<'a, T> {
265224 match self . next ( ) {
266225 // eos to stop iterating
267226 Err ( x) if x. code ( ) == PtErrorCode :: Eos => None ,
268- x => Some ( x)
227+ x => Some ( x) ,
269228 }
270229 }
271230}
0 commit comments