Skip to content

Commit c3adcaf

Browse files
committed
Use desc/split and remove descriptor.rs
We use 'virtio_queue::desc::split::Descriptor' to replace 'virtio_queue::Descriptor'. Signed-off-by: Wenyu Huang <[email protected]>
1 parent 57f84e9 commit c3adcaf

File tree

18 files changed

+539
-461
lines changed

18 files changed

+539
-461
lines changed

fuzz/common/src/blk.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ pub mod tests {
1919
use virtio_bindings::bindings::virtio_blk::VIRTIO_BLK_T_IN;
2020
use virtio_bindings::bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE};
2121
use virtio_blk::request::{Request, RequestType};
22-
use virtio_queue::{mock::MockSplitQueue, Descriptor};
22+
use virtio_queue::{
23+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
24+
mock::MockSplitQueue,
25+
};
2326
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemoryMmap};
2427

2528
// The same as the RequestHeader type in virtio_blk, with exposed fields

fuzz/common/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use ::virtio_queue::{Descriptor, Queue, QueueOwnedT, QueueT};
1+
use ::virtio_queue::{
2+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
3+
Queue, QueueOwnedT, QueueT,
4+
};
25
use std::fs::{self, File};
36
use std::path::{Path, PathBuf};
47
use std::sync::atomic::Ordering;
@@ -185,7 +188,9 @@ impl Into<Ordering> for LoadOrdering {
185188

186189
impl Into<Descriptor> for FuzzingDescriptor {
187190
fn into(self) -> Descriptor {
188-
Descriptor::new(self.addr, self.len, self.flags, self.next)
191+
Descriptor::from(SplitDescriptor::new(
192+
self.addr, self.len, self.flags, self.next,
193+
))
189194
}
190195
}
191196

fuzz/common/src/virtio_queue.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ pub mod tests {
2020

2121
use crate::create_corpus_file;
2222
use virtio_bindings::bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE};
23-
use virtio_queue::{mock::MockSplitQueue, Descriptor, Queue, QueueT};
23+
use virtio_queue::{
24+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
25+
mock::MockSplitQueue,
26+
Queue, QueueT,
27+
};
2428
use vm_memory::{GuestAddress, GuestMemoryMmap};
2529

2630
pub fn create_basic_virtio_queue_ops() -> VirtioQueueInput {

fuzz/common/src/vsock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ mod tests {
174174
use crate::virtio_queue::DEFAULT_QUEUE_SIZE;
175175
use std::io::Write;
176176
use virtio_bindings::bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE};
177+
use virtio_queue::desc::{split::Descriptor as SplitDescriptor, Descriptor};
177178
use virtio_queue::mock::MockSplitQueue;
178-
use virtio_queue::Descriptor;
179179
use virtio_vsock::packet::VsockPacket;
180180
use vm_memory::{Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
181181

fuzz/fuzz_targets/blk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use libfuzzer_sys::fuzz_target;
66
use std::hint::black_box;
77
use virtio_blk::request::Request;
88
use virtio_blk::stdio_executor::StdIoBackend;
9-
use virtio_queue::{mock::MockSplitQueue, Descriptor};
9+
use virtio_queue::{desc::Descriptor, mock::MockSplitQueue};
1010
use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap};
1111

1212
fuzz_target!(|data: &[u8]| {

fuzz/fuzz_targets/virtio_queue.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use common::{
44
virtio_queue::{VirtioQueueInput, DEFAULT_QUEUE_SIZE},
55
};
66
use libfuzzer_sys::fuzz_target;
7-
use virtio_queue::{mock::MockSplitQueue, Descriptor};
7+
use virtio_queue::{
8+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
9+
mock::MockSplitQueue,
10+
};
811
use vm_memory::{GuestAddress, GuestMemoryMmap};
912

1013
fuzz_target!(|data: &[u8]| {

fuzz/fuzz_targets/virtio_queue_ser.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use common::{
55
};
66
use libfuzzer_sys::fuzz_target;
77
use std::convert::{Into, TryFrom};
8-
use virtio_queue::{mock::MockSplitQueue, Descriptor, Queue, QueueState};
8+
use virtio_queue::{
9+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
10+
mock::MockSplitQueue,
11+
Queue, QueueState,
12+
};
913
use vm_memory::{GuestAddress, GuestMemoryMmap};
1014

1115
fuzz_target!(|data: &[u8]| {

fuzz/fuzz_targets/vsock.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
use common::virtio_queue::DEFAULT_QUEUE_SIZE;
33
use common::vsock::{InitFunction, VsockInput};
44
use libfuzzer_sys::fuzz_target;
5-
use virtio_queue::{mock::MockSplitQueue, Descriptor};
5+
use virtio_queue::{
6+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
7+
mock::MockSplitQueue,
8+
};
69
use virtio_vsock::packet::VsockPacket;
710
use vm_memory::{GuestAddress, GuestMemoryMmap};
811

virtio-blk/src/request.rs

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ use virtio_bindings::bindings::virtio_blk::{
3232
VIRTIO_BLK_T_OUT, VIRTIO_BLK_T_WRITE_ZEROES,
3333
};
3434

35-
use virtio_queue::{Descriptor, DescriptorChain};
35+
use virtio_queue::{
36+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
37+
DescriptorChain,
38+
};
3639
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError};
3740

3841
/// Block request parsing errors.
@@ -162,6 +165,7 @@ impl Request {
162165
where
163166
M: GuestMemory + ?Sized,
164167
{
168+
let desc = SplitDescriptor::from(desc);
165169
// The status MUST always be writable.
166170
if !desc.is_write_only() {
167171
return Err(Error::UnexpectedReadOnlyDescriptor);
@@ -183,6 +187,7 @@ impl Request {
183187

184188
// Checks that a descriptor meets the minimal requirements for a valid data descriptor.
185189
fn check_data_desc(desc: Descriptor, request_type: RequestType) -> Result<()> {
190+
let desc = SplitDescriptor::from(desc);
186191
// We do this check only for the device-readable buffers, as opposed to
187192
// also check that the device doesn't want to read a device-writable buffer
188193
// because this one is not a MUST (the device MAY do that for debugging or
@@ -234,14 +239,14 @@ impl Request {
234239
let mut desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
235240

236241
while desc.has_next() {
237-
Request::check_data_desc(desc, request.request_type)?;
242+
Request::check_data_desc(Descriptor::from(desc), request.request_type)?;
238243

239244
request.data.push((desc.addr(), desc.len()));
240245
desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
241246
}
242247
let status_desc = desc;
243248

244-
Request::check_status_desc(desc_chain.memory(), status_desc)?;
249+
Request::check_status_desc(desc_chain.memory(), Descriptor::from(status_desc))?;
245250

246251
request.status_addr = status_desc.addr();
247252
Ok(request)
@@ -297,9 +302,24 @@ mod tests {
297302
// The `build_desc_chain` function will populate the `NEXT` related flags and field.
298303
let v = [
299304
// A device-writable request header descriptor.
300-
Descriptor::new(0x10_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
301-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
302-
Descriptor::new(0x30_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
305+
Descriptor::from(SplitDescriptor::new(
306+
0x10_0000,
307+
0x100,
308+
VRING_DESC_F_WRITE as u16,
309+
0,
310+
)),
311+
Descriptor::from(SplitDescriptor::new(
312+
0x20_0000,
313+
0x100,
314+
VRING_DESC_F_WRITE as u16,
315+
0,
316+
)),
317+
Descriptor::from(SplitDescriptor::new(
318+
0x30_0000,
319+
0x100,
320+
VRING_DESC_F_WRITE as u16,
321+
0,
322+
)),
303323
];
304324
// Create a queue of max 16 descriptors and a descriptor chain based on the array above.
305325
let queue = MockSplitQueue::new(&mem, 16);
@@ -319,10 +339,15 @@ mod tests {
319339
);
320340

321341
let v = [
322-
Descriptor::new(0x10_0000, 0x100, 0, 0),
323-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
342+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
343+
Descriptor::from(SplitDescriptor::new(
344+
0x20_0000,
345+
0x100,
346+
VRING_DESC_F_WRITE as u16,
347+
0,
348+
)),
324349
// A device-readable request status descriptor.
325-
Descriptor::new(0x30_0000, 0x100, 0, 0),
350+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x100, 0, 0)),
326351
];
327352
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
328353

@@ -333,10 +358,20 @@ mod tests {
333358
);
334359

335360
let v = [
336-
Descriptor::new(0x10_0000, 0x100, 0, 0),
337-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
361+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
362+
Descriptor::from(SplitDescriptor::new(
363+
0x20_0000,
364+
0x100,
365+
VRING_DESC_F_WRITE as u16,
366+
0,
367+
)),
338368
// Status descriptor with len = 0.
339-
Descriptor::new(0x30_0000, 0x0, VRING_DESC_F_WRITE as u16, 0),
369+
Descriptor::from(SplitDescriptor::new(
370+
0x30_0000,
371+
0x0,
372+
VRING_DESC_F_WRITE as u16,
373+
0,
374+
)),
340375
];
341376
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
342377
assert_eq!(
@@ -345,9 +380,14 @@ mod tests {
345380
);
346381

347382
let v = [
348-
Descriptor::new(0x10_0000, 0x100, 0, 0),
349-
Descriptor::new(0x20_0000, 0x100, 0, 0),
350-
Descriptor::new(0x30_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
383+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
384+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, 0, 0)),
385+
Descriptor::from(SplitDescriptor::new(
386+
0x30_0000,
387+
0x100,
388+
VRING_DESC_F_WRITE as u16,
389+
0,
390+
)),
351391
];
352392
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
353393

@@ -377,10 +417,25 @@ mod tests {
377417

378418
// Invalid status address.
379419
let v = [
380-
Descriptor::new(0x10_0000, 0x100, 0, 0),
381-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
382-
Descriptor::new(0x30_0000, 0x200, VRING_DESC_F_WRITE as u16, 0),
383-
Descriptor::new(0x1100_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
420+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
421+
Descriptor::from(SplitDescriptor::new(
422+
0x20_0000,
423+
0x100,
424+
VRING_DESC_F_WRITE as u16,
425+
0,
426+
)),
427+
Descriptor::from(SplitDescriptor::new(
428+
0x30_0000,
429+
0x200,
430+
VRING_DESC_F_WRITE as u16,
431+
0,
432+
)),
433+
Descriptor::from(SplitDescriptor::new(
434+
0x1100_0000,
435+
0x100,
436+
VRING_DESC_F_WRITE as u16,
437+
0,
438+
)),
384439
];
385440
let req_header = RequestHeader {
386441
request_type: VIRTIO_BLK_T_OUT,
@@ -402,10 +457,25 @@ mod tests {
402457

403458
// Valid descriptor chain for OUT.
404459
let v = [
405-
Descriptor::new(0x10_0000, 0x100, 0, 0),
406-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
407-
Descriptor::new(0x30_0000, 0x200, VRING_DESC_F_WRITE as u16, 0),
408-
Descriptor::new(0x40_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
460+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
461+
Descriptor::from(SplitDescriptor::new(
462+
0x20_0000,
463+
0x100,
464+
VRING_DESC_F_WRITE as u16,
465+
0,
466+
)),
467+
Descriptor::from(SplitDescriptor::new(
468+
0x30_0000,
469+
0x200,
470+
VRING_DESC_F_WRITE as u16,
471+
0,
472+
)),
473+
Descriptor::from(SplitDescriptor::new(
474+
0x40_0000,
475+
0x100,
476+
VRING_DESC_F_WRITE as u16,
477+
0,
478+
)),
409479
];
410480
let req_header = RequestHeader {
411481
request_type: VIRTIO_BLK_T_OUT,
@@ -447,8 +517,13 @@ mod tests {
447517

448518
// Valid descriptor chain for FLUSH.
449519
let v = [
450-
Descriptor::new(0x10_0000, 0x100, 0, 0),
451-
Descriptor::new(0x40_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
520+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
521+
Descriptor::from(SplitDescriptor::new(
522+
0x40_0000,
523+
0x100,
524+
VRING_DESC_F_WRITE as u16,
525+
0,
526+
)),
452527
];
453528
let req_header = RequestHeader {
454529
request_type: VIRTIO_BLK_T_FLUSH,

0 commit comments

Comments
 (0)