Skip to content

Commit db412a3

Browse files
uran0sHepilys
authored andcommitted
Use desc/split and remove descriptor.rs
Use 'virtio_queue::desc::split::Descriptor' to replace 'virtio_queue::Descriptor'. Signed-off-by: Wenyu Huang <huangwenyuu@outlook.com>
1 parent 4eb6318 commit db412a3

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)
@@ -298,9 +303,24 @@ mod tests {
298303
// The `build_desc_chain` function will populate the `NEXT` related flags and field.
299304
let v = [
300305
// A device-writable request header descriptor.
301-
Descriptor::new(0x10_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
302-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
303-
Descriptor::new(0x30_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
306+
RawDescriptor::from(SplitDescriptor::new(
307+
0x10_0000,
308+
0x100,
309+
VRING_DESC_F_WRITE as u16,
310+
0,
311+
)),
312+
RawDescriptor::from(SplitDescriptor::new(
313+
0x20_0000,
314+
0x100,
315+
VRING_DESC_F_WRITE as u16,
316+
0,
317+
)),
318+
RawDescriptor::from(SplitDescriptor::new(
319+
0x30_0000,
320+
0x100,
321+
VRING_DESC_F_WRITE as u16,
322+
0,
323+
)),
304324
];
305325
// Create a queue of max 16 descriptors and a descriptor chain based on the array above.
306326
let queue = MockSplitQueue::new(&mem, 16);
@@ -320,10 +340,15 @@ mod tests {
320340
);
321341

322342
let v = [
323-
Descriptor::new(0x10_0000, 0x100, 0, 0),
324-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
343+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
344+
RawDescriptor::from(SplitDescriptor::new(
345+
0x20_0000,
346+
0x100,
347+
VRING_DESC_F_WRITE as u16,
348+
0,
349+
)),
325350
// A device-readable request status descriptor.
326-
Descriptor::new(0x30_0000, 0x100, 0, 0),
351+
RawDescriptor::from(SplitDescriptor::new(0x30_0000, 0x100, 0, 0)),
327352
];
328353
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
329354

@@ -334,10 +359,20 @@ mod tests {
334359
);
335360

336361
let v = [
337-
Descriptor::new(0x10_0000, 0x100, 0, 0),
338-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
362+
RawDescriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
363+
RawDescriptor::from(SplitDescriptor::new(
364+
0x20_0000,
365+
0x100,
366+
VRING_DESC_F_WRITE as u16,
367+
0,
368+
)),
339369
// Status descriptor with len = 0.
340-
Descriptor::new(0x30_0000, 0x0, VRING_DESC_F_WRITE as u16, 0),
370+
RawDescriptor::from(SplitDescriptor::new(
371+
0x30_0000,
372+
0x0,
373+
VRING_DESC_F_WRITE as u16,
374+
0,
375+
)),
341376
];
342377
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
343378
assert_eq!(
@@ -346,9 +381,14 @@ mod tests {
346381
);
347382

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

@@ -378,10 +418,25 @@ mod tests {
378418

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

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

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

0 commit comments

Comments
 (0)