1
1
use super :: Block ;
2
2
use crate :: asid:: Asid ;
3
3
use crate :: config:: Config ;
4
+ use crate :: error:: {
5
+ deref_ptresult, deref_ptresult_mut, ensure_ptok, extract_pterr, PtError , PtErrorCode ,
6
+ } ;
4
7
use crate :: event:: Event ;
5
8
use crate :: flags:: Status ;
6
9
use crate :: image:: Image ;
7
- use crate :: error:: {
8
- PtError , ensure_ptok,
9
- extract_pterr, deref_ptresult,
10
- deref_ptresult_mut, PtErrorCode
11
- } ;
12
10
11
+ use std:: marker:: PhantomData ;
13
12
use std:: mem;
14
13
use std:: ptr;
15
- use std:: marker:: PhantomData ;
16
14
17
15
use 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,
37
20
} ;
38
21
39
22
#[ cfg( test) ]
@@ -44,23 +27,19 @@ mod test {
44
27
#[ test]
45
28
fn test_blkdec_alloc ( ) {
46
29
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 ( ) ;
50
31
}
51
32
52
- #[ test ]
33
+ #[ test]
53
34
fn test_blkdec_props ( ) {
54
35
let kek = & mut [ 1 ; 2 ] ;
55
36
// this just checks memory safety for property access
56
37
// 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 ( ) ;
60
39
let a = b. asid ( ) . unwrap ( ) ;
61
40
assert ! ( a. cr3( ) . is_none( ) ) ;
62
41
assert ! ( a. vmcs( ) . is_none( ) ) ;
63
- assert ! ( b. core_bus_ratio( ) . is_err ( ) ) ;
42
+ assert ! ( b. core_bus_ratio( ) . is_ok ( ) ) ;
64
43
assert ! ( b. event( ) . is_err( ) ) ;
65
44
assert ! ( b. config( ) . is_ok( ) ) ;
66
45
assert ! ( b. image( ) . unwrap( ) . name( ) . is_none( ) ) ;
@@ -69,7 +48,7 @@ mod test {
69
48
assert ! ( b. next( ) . is_err( ) ) ;
70
49
assert ! ( b. sync_backward( ) . is_err( ) ) ;
71
50
assert ! ( b. sync_forward( ) . is_err( ) ) ;
72
- assert ! ( b. time( ) . is_err ( ) ) ;
51
+ assert ! ( b. time( ) . is_ok ( ) ) ;
73
52
}
74
53
}
75
54
@@ -89,7 +68,7 @@ impl<'a, T> BlockDecoder<'a, T> {
89
68
pub fn new ( cfg : & Config < T > ) -> Result < Self , PtError > {
90
69
// deref_ptresult(unsafe{ pt_blk_alloc_decoder(&cfg.0) })
91
70
// .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 ( ) ) } )
93
72
. map ( |x| BlockDecoder :: < T > ( x, PhantomData ) )
94
73
}
95
74
@@ -99,10 +78,7 @@ impl<'a, T> BlockDecoder<'a, T> {
99
78
/// Returns Asid on success, a PtError otherwise.
100
79
pub fn asid ( & self ) -> Result < Asid , PtError > {
101
80
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) }
106
82
}
107
83
108
84
/// Return the current core bus ratio.
@@ -121,25 +97,20 @@ impl<'a, T> BlockDecoder<'a, T> {
121
97
/// Returns BadQuery if there is no event.
122
98
pub fn event ( & mut self ) -> Result < ( Event , Status ) , PtError > {
123
99
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 ( ) ) )
129
102
}
130
103
131
104
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)
134
106
}
135
107
136
108
/// Get the traced image.
137
109
///
138
110
/// The returned image may be modified as long as @decoder is not running.
139
111
/// Returns the traced image the decoder uses for reading memory.
140
112
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)
143
114
}
144
115
145
116
/// Get the current decoder position.
@@ -148,17 +119,15 @@ impl<'a, T> BlockDecoder<'a, T> {
148
119
/// Returns Nosync if decoder is out of sync.
149
120
pub fn offset ( & self ) -> Result < u64 , PtError > {
150
121
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)
153
123
}
154
124
155
125
/// Get the position of the last synchronization point.
156
126
///
157
127
/// Returns Nosync if the decoder is out of sync.
158
128
pub fn sync_offset ( & self ) -> Result < u64 , PtError > {
159
129
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)
162
131
}
163
132
164
133
/// Determine the next block of instructions.
@@ -176,13 +145,8 @@ impl<'a, T> BlockDecoder<'a, T> {
176
145
/// Returns Nosync if the decoder is out of sync.
177
146
pub fn next ( & mut self ) -> Result < ( Block , Status ) , PtError > {
178
147
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 ( ) ) )
186
150
}
187
151
188
152
/// Set the traced image.
@@ -192,11 +156,13 @@ impl<'a, T> BlockDecoder<'a, T> {
192
156
/// Only one image can be active at any time.
193
157
pub fn set_image ( & mut self , img : Option < & mut Image > ) -> Result < ( ) , PtError > {
194
158
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
+ )
200
166
} )
201
167
}
202
168
@@ -214,8 +180,7 @@ impl<'a, T> BlockDecoder<'a, T> {
214
180
/// Returns BadPacket if an unknown packet payload is encountered.
215
181
/// Returns Eos if no further synchronization point is found.
216
182
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 ( ) )
219
184
}
220
185
221
186
/// Manually synchronize an Intel PT block decoder.
@@ -227,7 +192,7 @@ impl<'a, T> BlockDecoder<'a, T> {
227
192
/// Returns Eos if the decoder reaches the end of its trace buffer.
228
193
/// Returns Nosync if there is no syncpoint at @offset.
229
194
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) } )
231
196
}
232
197
233
198
/// Return the current time.
@@ -247,14 +212,8 @@ impl<'a, T> BlockDecoder<'a, T> {
247
212
let mut time: u64 = 0 ;
248
213
let mut lost_mtc: u32 = 0 ;
249
214
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) )
258
217
}
259
218
}
260
219
@@ -265,7 +224,7 @@ impl<'a, T> Iterator for BlockDecoder<'a, T> {
265
224
match self . next ( ) {
266
225
// eos to stop iterating
267
226
Err ( x) if x. code ( ) == PtErrorCode :: Eos => None ,
268
- x => Some ( x)
227
+ x => Some ( x) ,
269
228
}
270
229
}
271
230
}
0 commit comments