@@ -5,28 +5,26 @@ use core::{
5
5
fmt:: Debug ,
6
6
hash:: { Hash , Hasher } ,
7
7
ops:: { Deref , DerefMut } ,
8
+ ptr:: NonNull ,
8
9
} ;
9
10
10
11
use ahash:: RandomState ;
11
- use libafl_bolts:: { ownedref:: OwnedMutSlice , AsSlice , AsSliceMut , HasLen , Named } ;
12
+ use libafl_bolts:: { ownedref:: OwnedMutSizedSlice , HasLen , Named } ;
12
13
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
13
14
14
15
use crate :: {
15
- observers:: { map:: MapObserver , Observer , VariableLengthMapObserver } ,
16
+ observers:: { map:: MapObserver , ConstLenMapObserver , Observer } ,
16
17
Error ,
17
18
} ;
18
19
19
- // TODO: remove the size field and implement ConstantLengthMapObserver
20
-
21
20
/// Use a const size to speedup `Feedback::is_interesting` when the user can
22
21
/// know the size of the map at compile time.
23
22
#[ derive( Serialize , Deserialize , Debug ) ]
24
23
#[ allow( clippy:: unsafe_derive_deserialize) ]
25
24
pub struct ConstMapObserver < ' a , T , const N : usize > {
26
- map : OwnedMutSlice < ' a , T > ,
25
+ map : OwnedMutSizedSlice < ' a , T , N > ,
27
26
initial : T ,
28
27
name : Cow < ' static , str > ,
29
- size : usize ,
30
28
}
31
29
32
30
impl < I , S , T , const N : usize > Observer < I , S > for ConstMapObserver < ' _ , T , N >
@@ -87,19 +85,19 @@ where
87
85
88
86
#[ inline]
89
87
fn get ( & self , idx : usize ) -> T {
90
- self . as_slice ( ) [ idx]
88
+ self [ idx]
91
89
}
92
90
93
91
#[ inline]
94
92
fn set ( & mut self , idx : usize , val : T ) {
95
- self . map . as_slice_mut ( ) [ idx] = val;
93
+ ( * self ) [ idx] = val;
96
94
}
97
95
98
96
/// Count the set bytes in the map
99
97
fn count_bytes ( & self ) -> u64 {
100
98
let initial = self . initial ( ) ;
101
99
let cnt = self . usable_count ( ) ;
102
- let map = self . as_slice ( ) ;
100
+ let map = self . map . as_slice ( ) ;
103
101
let mut res = 0 ;
104
102
for x in & map[ 0 ..cnt] {
105
103
if * x != initial {
@@ -110,7 +108,7 @@ where
110
108
}
111
109
112
110
fn usable_count ( & self ) -> usize {
113
- self . as_slice ( ) . len ( )
111
+ self . len ( )
114
112
}
115
113
116
114
#[ inline]
@@ -124,22 +122,22 @@ where
124
122
// Normal memset, see https://rust.godbolt.org/z/Trs5hv
125
123
let initial = self . initial ( ) ;
126
124
let cnt = self . usable_count ( ) ;
127
- let map = self . as_slice_mut ( ) ;
125
+ let map = & mut ( * self ) ;
128
126
for x in & mut map[ 0 ..cnt] {
129
127
* x = initial;
130
128
}
131
129
Ok ( ( ) )
132
130
}
133
131
134
132
fn to_vec ( & self ) -> Vec < T > {
135
- self . as_slice ( ) . to_vec ( )
133
+ self . map . to_vec ( )
136
134
}
137
135
138
136
/// Get the number of set entries with the specified indexes
139
137
fn how_many_set ( & self , indexes : & [ usize ] ) -> usize {
140
138
let initial = self . initial ( ) ;
141
139
let cnt = self . usable_count ( ) ;
142
- let map = self . as_slice ( ) ;
140
+ let map = self . map . as_slice ( ) ;
143
141
let mut res = 0 ;
144
142
for i in indexes {
145
143
if * i < cnt && map[ * i] != initial {
@@ -150,37 +148,30 @@ where
150
148
}
151
149
}
152
150
153
- impl < T , const N : usize > VariableLengthMapObserver for ConstMapObserver < ' _ , T , N >
151
+ impl < T , const N : usize > ConstLenMapObserver < N > for ConstMapObserver < ' _ , T , N >
154
152
where
155
153
T : PartialEq + Copy + Hash + Serialize + DeserializeOwned + Debug + ' static ,
156
154
{
157
- fn map_slice ( & mut self ) -> & [ Self :: Entry ] {
158
- self . map . as_slice ( )
159
- }
160
-
161
- fn map_slice_mut ( & mut self ) -> & mut [ Self :: Entry ] {
162
- self . map . as_slice_mut ( )
163
- }
164
-
165
- fn size ( & mut self ) -> & usize {
166
- & N
155
+ fn map_slice ( & self ) -> & [ Self :: Entry ; N ] {
156
+ & self . map
167
157
}
168
158
169
- fn size_mut ( & mut self ) -> & mut usize {
170
- & mut self . size
159
+ fn map_slice_mut ( & mut self ) -> & mut [ Self :: Entry ; N ] {
160
+ & mut self . map
171
161
}
172
162
}
173
163
174
164
impl < T , const N : usize > Deref for ConstMapObserver < ' _ , T , N > {
175
165
type Target = [ T ] ;
166
+
176
167
fn deref ( & self ) -> & [ T ] {
177
- & self . map
168
+ self . map . as_slice ( )
178
169
}
179
170
}
180
171
181
172
impl < T , const N : usize > DerefMut for ConstMapObserver < ' _ , T , N > {
182
173
fn deref_mut ( & mut self ) -> & mut [ T ] {
183
- & mut self . map
174
+ self . map . as_mut_slice ( )
184
175
}
185
176
}
186
177
@@ -194,48 +185,25 @@ where
194
185
/// Will get a pointer to the map and dereference it at any point in time.
195
186
/// The map must not move in memory!
196
187
#[ must_use]
197
- pub fn new ( name : & ' static str , map : & ' a mut [ T ] ) -> Self {
188
+ pub fn new ( name : & ' static str , map : & ' a mut [ T ; N ] ) -> Self {
198
189
assert ! ( map. len( ) >= N ) ;
199
190
Self {
200
- map : OwnedMutSlice :: from ( map) ,
191
+ map : OwnedMutSizedSlice :: from ( map) ,
201
192
name : Cow :: from ( name) ,
202
193
initial : T :: default ( ) ,
203
- size : N ,
204
194
}
205
195
}
206
196
207
197
/// Creates a new [`MapObserver`] from a raw pointer
208
198
///
209
199
/// # Safety
210
200
/// Will dereference the `map_ptr` with up to len elements.
211
- pub unsafe fn from_mut_ptr ( name : & ' static str , map_ptr : * mut T ) -> Self {
201
+ #[ must_use]
202
+ pub unsafe fn from_mut_ptr ( name : & ' static str , map_ptr : NonNull < T > ) -> Self {
212
203
ConstMapObserver {
213
- map : OwnedMutSlice :: from_raw_parts_mut ( map_ptr, N ) ,
204
+ map : OwnedMutSizedSlice :: from_raw_mut ( map_ptr) ,
214
205
name : Cow :: from ( name) ,
215
206
initial : T :: default ( ) ,
216
- size : N ,
217
- }
218
- }
219
- }
220
-
221
- impl < T , const N : usize > ConstMapObserver < ' _ , T , N >
222
- where
223
- T : Default + Clone ,
224
- {
225
- /// Creates a new [`MapObserver`] with an owned map
226
- #[ must_use]
227
- pub fn owned ( name : & ' static str , map : Vec < T > ) -> Self {
228
- assert ! ( map. len( ) >= N ) ;
229
- let initial = if map. is_empty ( ) {
230
- T :: default ( )
231
- } else {
232
- map[ 0 ] . clone ( )
233
- } ;
234
- Self {
235
- map : OwnedMutSlice :: from ( map) ,
236
- name : Cow :: from ( name) ,
237
- initial,
238
- size : N ,
239
207
}
240
208
}
241
209
}
0 commit comments