Skip to content

Commit 3f755d1

Browse files
committed
Use desc/split and remove descriptor.rs
Use 'virtio_queue::desc::split::Descriptor' to replace 'virtio_queue::Descriptor'. Signed-off-by: Wenyu Huang <[email protected]>
1 parent 9f03f0d commit 3f755d1

File tree

18 files changed

+550
-486
lines changed

18 files changed

+550
-486
lines changed

fuzz/common/src/blk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ 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::{desc::RawDescriptor, mock::MockSplitQueue};
2323
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemoryMmap};
2424

2525
// The same as the RequestHeader type in virtio_blk, with exposed fields
@@ -91,7 +91,7 @@ pub mod tests {
9191
},
9292
];
9393

94-
let q_descriptors: Vec<Descriptor> =
94+
let q_descriptors: Vec<RawDescriptor> =
9595
descriptors.into_iter().map(|desc| desc.into()).collect();
9696
let mut chain = vq.build_multiple_desc_chains(&q_descriptors).unwrap();
9797

fuzz/common/src/lib.rs

Lines changed: 9 additions & 4 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, RawDescriptor},
3+
Queue, QueueOwnedT, QueueT,
4+
};
25
use std::fs::{self, File};
36
use std::path::{Path, PathBuf};
47
use std::sync::atomic::Ordering;
@@ -183,9 +186,11 @@ impl Into<Ordering> for LoadOrdering {
183186
}
184187
}
185188

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

fuzz/common/src/virtio_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ 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::{desc::RawDescriptor, mock::MockSplitQueue, Queue, QueueT};
2424
use vm_memory::{GuestAddress, GuestMemoryMmap};
2525

2626
pub fn create_basic_virtio_queue_ops() -> VirtioQueueInput {
@@ -51,7 +51,7 @@ pub mod tests {
5151
// To be able to call the functions we actually need to create the environment for running.
5252
let mem = GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap();
5353
let vq = MockSplitQueue::new(&mem, DEFAULT_QUEUE_SIZE);
54-
let q_descriptors: Vec<Descriptor> =
54+
let q_descriptors: Vec<RawDescriptor> =
5555
descriptors.iter().map(|desc| (*desc).into()).collect();
5656
vq.build_multiple_desc_chains(&q_descriptors).unwrap();
5757
let mut q: Queue = vq.create_queue().unwrap();

fuzz/common/src/vsock.rs

Lines changed: 3 additions & 3 deletions
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::RawDescriptor;
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

@@ -332,7 +332,7 @@ mod tests {
332332
next: 0,
333333
},
334334
];
335-
let q_descriptors: Vec<Descriptor> =
335+
let q_descriptors: Vec<RawDescriptor> =
336336
descriptors.iter().map(|desc| (*desc).into()).collect();
337337
let mut chain = vq.build_multiple_desc_chains(&q_descriptors).unwrap();
338338

@@ -382,7 +382,7 @@ mod tests {
382382
next: 0,
383383
},
384384
];
385-
let q_descriptors: Vec<Descriptor> =
385+
let q_descriptors: Vec<RawDescriptor> =
386386
descriptors.iter().map(|desc| (*desc).into()).collect();
387387
let mut chain = vq.build_multiple_desc_chains(&q_descriptors).unwrap();
388388

fuzz/fuzz_targets/blk.rs

Lines changed: 2 additions & 2 deletions
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::RawDescriptor, mock::MockSplitQueue};
1010
use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap};
1111

1212
fuzz_target!(|data: &[u8]| {
@@ -23,7 +23,7 @@ fuzz_target!(|data: &[u8]| {
2323

2424
let vq = MockSplitQueue::create(&m, start_addr, DEFAULT_QUEUE_SIZE);
2525

26-
let descriptors: Vec<Descriptor> = fuzz_input
26+
let descriptors: Vec<RawDescriptor> = fuzz_input
2727
.descriptors
2828
.iter()
2929
.map(|desc| (*desc).into())

fuzz/fuzz_targets/virtio_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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::{desc::RawDescriptor, mock::MockSplitQueue};
88
use vm_memory::{GuestAddress, GuestMemoryMmap};
99

1010
fuzz_target!(|data: &[u8]| {
@@ -20,7 +20,7 @@ fuzz_target!(|data: &[u8]| {
2020
let start_addr = GuestAddress(0x1000);
2121
let m = GuestMemoryMmap::<()>::from_ranges(&[(start_addr, 0x11000)]).unwrap();
2222
let vq = MockSplitQueue::create(&m, start_addr, DEFAULT_QUEUE_SIZE);
23-
let descriptors: Vec<Descriptor> = fuzz_input
23+
let descriptors: Vec<RawDescriptor> = fuzz_input
2424
.descriptors
2525
.iter()
2626
.map(|desc| (*desc).into())

fuzz/fuzz_targets/virtio_queue_ser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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::{desc::RawDescriptor, mock::MockSplitQueue, Queue, QueueState};
99
use vm_memory::{GuestAddress, GuestMemoryMmap};
1010

1111
fuzz_target!(|data: &[u8]| {
@@ -22,7 +22,7 @@ fuzz_target!(|data: &[u8]| {
2222
let m = GuestMemoryMmap::<()>::from_ranges(&[(start_addr, 0x11000)]).unwrap();
2323
let vq = MockSplitQueue::create(&m, start_addr, DEFAULT_QUEUE_SIZE);
2424

25-
let descriptors: Vec<Descriptor> = fuzz_input
25+
let descriptors: Vec<RawDescriptor> = fuzz_input
2626
.descriptors
2727
.iter()
2828
.map(|desc| (*desc).into())

fuzz/fuzz_targets/vsock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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::{desc::RawDescriptor, mock::MockSplitQueue};
66
use virtio_vsock::packet::VsockPacket;
77
use vm_memory::{GuestAddress, GuestMemoryMmap};
88

@@ -19,7 +19,7 @@ fuzz_target!(|data: &[u8]| {
1919
let m = GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x1000), 0x11000)]).unwrap();
2020
let vq = MockSplitQueue::create(&m, start_addr, DEFAULT_QUEUE_SIZE);
2121

22-
let descriptors: Vec<Descriptor> = fuzz_input
22+
let descriptors: Vec<RawDescriptor> = fuzz_input
2323
.descriptors
2424
.iter()
2525
.map(|desc| (*desc).into())

virtio-blk/src/request.rs

Lines changed: 102 additions & 27 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, RawDescriptor},
37+
DescriptorChain,
38+
};
3639
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError};
3740

3841
/// Block request parsing errors.
@@ -158,10 +161,11 @@ impl Request {
158161
}
159162

160163
// Checks that a descriptor meets the minimal requirements for a valid status descriptor.
161-
fn check_status_desc<M>(mem: &M, desc: Descriptor) -> Result<()>
164+
fn check_status_desc<M>(mem: &M, desc: RawDescriptor) -> Result<()>
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);
@@ -182,7 +186,8 @@ impl Request {
182186
}
183187

184188
// Checks that a descriptor meets the minimal requirements for a valid data descriptor.
185-
fn check_data_desc(desc: Descriptor, request_type: RequestType) -> Result<()> {
189+
fn check_data_desc(desc: RawDescriptor, 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(RawDescriptor::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(), RawDescriptor::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+
RawDescriptor::from(SplitDescriptor::new(
306+
0x10_0000,
307+
0x100,
308+
VRING_DESC_F_WRITE as u16,
309+
0,
310+
)),
311+
RawDescriptor::from(SplitDescriptor::new(
312+
0x20_0000,
313+
0x100,
314+
VRING_DESC_F_WRITE as u16,
315+
0,
316+
)),
317+
RawDescriptor::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+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
343+
RawDescriptor::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+
RawDescriptor::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+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
362+
RawDescriptor::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+
RawDescriptor::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+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
384+
RawDescriptor::from(SplitDescriptor::new(0x20_0000, 0x100, 0, 0)),
385+
RawDescriptor::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+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
421+
RawDescriptor::from(SplitDescriptor::new(
422+
0x20_0000,
423+
0x100,
424+
VRING_DESC_F_WRITE as u16,
425+
0,
426+
)),
427+
RawDescriptor::from(SplitDescriptor::new(
428+
0x30_0000,
429+
0x200,
430+
VRING_DESC_F_WRITE as u16,
431+
0,
432+
)),
433+
RawDescriptor::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+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
461+
RawDescriptor::from(SplitDescriptor::new(
462+
0x20_0000,
463+
0x100,
464+
VRING_DESC_F_WRITE as u16,
465+
0,
466+
)),
467+
RawDescriptor::from(SplitDescriptor::new(
468+
0x30_0000,
469+
0x200,
470+
VRING_DESC_F_WRITE as u16,
471+
0,
472+
)),
473+
RawDescriptor::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+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
521+
RawDescriptor::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)