@@ -269,6 +269,254 @@ impl Queue {
269
269
}
270
270
}
271
271
272
+ #[ cfg( kani) ]
273
+ #[ allow( dead_code) ]
274
+ mod verification {
275
+ use std:: mem:: ManuallyDrop ;
276
+ use vm_memory:: MmapRegion ;
277
+
278
+ use std:: num:: Wrapping ;
279
+ use vm_memory:: FileOffset ;
280
+
281
+ use vm_memory:: { GuestMemoryRegion , MemoryRegionAddress } ;
282
+
283
+ use super :: * ;
284
+
285
+ /// A made-for-kani version of `vm_memory::GuestMemoryMmap`. Unlike the real
286
+ /// `GuestMemoryMmap`, which manages a list of regions and then does a binary
287
+ /// search to determine which region a specific read or write request goes to,
288
+ /// this only uses a single region. Eliminating this binary search significantly
289
+ /// speeds up all queue proofs, because it eliminates the only loop contained herein,
290
+ /// meaning we can use `kani::unwind(0)` instead of `kani::unwind(2)`. Functionally,
291
+ /// it works identically to `GuestMemoryMmap` with only a single contained region.
292
+ pub struct ProofGuestMemory {
293
+ the_region : vm_memory:: GuestRegionMmap ,
294
+ }
295
+
296
+ impl GuestMemory for ProofGuestMemory {
297
+ type R = vm_memory:: GuestRegionMmap ;
298
+ //type I = Self;
299
+
300
+ fn num_regions ( & self ) -> usize {
301
+ 1
302
+ }
303
+
304
+ fn find_region ( & self , addr : GuestAddress ) -> Option < & Self :: R > {
305
+ self . the_region
306
+ . to_region_addr ( addr)
307
+ . map ( |_| & self . the_region )
308
+ }
309
+
310
+ fn iter ( & self ) -> impl Iterator < Item = & Self :: R > {
311
+ std:: iter:: once ( & self . the_region )
312
+ }
313
+
314
+ fn try_access < F > (
315
+ & self ,
316
+ count : usize ,
317
+ addr : GuestAddress ,
318
+ mut f : F ,
319
+ ) -> vm_memory:: guest_memory:: Result < usize >
320
+ where
321
+ F : FnMut (
322
+ usize ,
323
+ usize ,
324
+ MemoryRegionAddress ,
325
+ & Self :: R ,
326
+ ) -> vm_memory:: guest_memory:: Result < usize > ,
327
+ {
328
+ // We only have a single region, meaning a lot of the complications of the default
329
+ // try_access implementation for dealing with reads/writes across multiple
330
+ // regions does not apply.
331
+ let region_addr = self
332
+ . the_region
333
+ . to_region_addr ( addr)
334
+ . ok_or ( vm_memory:: guest_memory:: Error :: InvalidGuestAddress ( addr) ) ?;
335
+ self . the_region
336
+ . checked_offset ( region_addr, count)
337
+ . ok_or ( vm_memory:: guest_memory:: Error :: InvalidGuestAddress ( addr) ) ?;
338
+ f ( 0 , count, region_addr, & self . the_region )
339
+ }
340
+ }
341
+
342
+ pub struct ProofContext ( pub Queue , pub ProofGuestMemory ) ;
343
+
344
+ pub struct MmapRegionStub {
345
+ addr : * mut u8 ,
346
+ size : usize ,
347
+ bitmap : ( ) ,
348
+ file_offset : Option < FileOffset > ,
349
+ prot : i32 ,
350
+ flags : i32 ,
351
+ owned : bool ,
352
+ hugetlbfs : Option < bool > ,
353
+ }
354
+
355
+ /// We start the first guest memory region at an offset so that harnesses using
356
+ /// Queue::any() will be exposed to queue segments both before and after valid guest memory.
357
+ /// This is conforming to MockSplitQueue::new() that uses `0` as starting address of the
358
+ /// virtqueue. Also, QUEUE_END is the size only if GUEST_MEMORY_BASE is `0`
359
+ const GUEST_MEMORY_BASE : u64 = 0 ;
360
+
361
+ // We size our guest memory to fit a properly aligned queue, plus some wiggles bytes
362
+ // to make sure we not only test queues where all segments are consecutively aligned.
363
+ // We need to give at least 16 bytes of buffer space for the descriptor table to be
364
+ // able to change its address, as it is 16-byte aligned.
365
+ const GUEST_MEMORY_SIZE : usize = QUEUE_END as usize + 30 ;
366
+
367
+ fn guest_memory ( memory : * mut u8 ) -> ProofGuestMemory {
368
+ // Ideally, we'd want to do
369
+ // let region = unsafe {MmapRegionBuilder::new(GUEST_MEMORY_SIZE)
370
+ // .with_raw_mmap_pointer(bytes.as_mut_ptr())
371
+ // .build()
372
+ // .unwrap()};
373
+ // However, .build() calls to .build_raw(), which contains a call to libc::sysconf.
374
+ // Since kani 0.34.0, stubbing out foreign functions is supported, but due to the rust
375
+ // standard library using a special version of the libc crate, it runs into some problems
376
+ // [1] Even if we work around those problems, we run into performance problems [2].
377
+ // Therefore, for now we stick to this ugly transmute hack (which only works because
378
+ // the kani compiler will never re-order fields, so we can treat repr(Rust) as repr(C)).
379
+ //
380
+ // [1]: https://github.com/model-checking/kani/issues/2673
381
+ // [2]: https://github.com/model-checking/kani/issues/2538
382
+ let region_stub = MmapRegionStub {
383
+ addr : memory,
384
+ size : GUEST_MEMORY_SIZE ,
385
+ bitmap : Default :: default ( ) ,
386
+ file_offset : None ,
387
+ prot : 0 ,
388
+ flags : libc:: MAP_ANONYMOUS | libc:: MAP_PRIVATE ,
389
+ owned : false ,
390
+ hugetlbfs : None ,
391
+ } ;
392
+
393
+ let region: MmapRegion < ( ) > = unsafe { std:: mem:: transmute ( region_stub) } ;
394
+
395
+ let guest_region =
396
+ vm_memory:: GuestRegionMmap :: new ( region, GuestAddress ( GUEST_MEMORY_BASE ) ) . unwrap ( ) ;
397
+
398
+ // Use a single memory region, just as firecracker does for guests of size < 2GB
399
+ // For largest guests, firecracker uses two regions (due to the MMIO gap being
400
+ // at the top of 32-bit address space)
401
+ ProofGuestMemory {
402
+ the_region : guest_region,
403
+ }
404
+ }
405
+
406
+ // can't implement kani::Arbitrary for the relevant types due to orphan rules
407
+ fn setup_kani_guest_memory ( ) -> ProofGuestMemory {
408
+ // Non-deterministic Vec that will be used as the guest memory. We use `exact_vec` for now
409
+ // as `any_vec` will likely result in worse performance. We do not loose much from
410
+ // `exact_vec`, as our proofs do not make any assumptions about "filling" guest
411
+ // memory: Since everything is placed at non-deterministic addresses with
412
+ // non-deterministic lengths, we still cover all scenarios that would be covered by
413
+ // smaller guest memory closely. We leak the memory allocated here, so that it
414
+ // doesnt get deallocated at the end of this function. We do not explicitly
415
+ // de-allocate, but since this is a kani proof, that does not matter.
416
+ guest_memory (
417
+ ManuallyDrop :: new ( kani:: vec:: exact_vec :: < u8 , GUEST_MEMORY_SIZE > ( ) ) . as_mut_ptr ( ) ,
418
+ )
419
+ }
420
+
421
+ const MAX_QUEUE_SIZE : u16 = 256 ;
422
+
423
+ // Constants describing the in-memory layout of a queue of size MAX_QUEUE_SIZE starting
424
+ // at the beginning of guest memory. These are based on Section 2.7 of the VirtIO 1.2
425
+ // specification.
426
+ const QUEUE_BASE_ADDRESS : u64 = GUEST_MEMORY_BASE ;
427
+
428
+ /// descriptor table has 16 bytes per entry, avail ring starts right after
429
+ const AVAIL_RING_BASE_ADDRESS : u64 = QUEUE_BASE_ADDRESS + MAX_QUEUE_SIZE as u64 * 16 ;
430
+
431
+ /// Used ring starts after avail ring (which has size 6 + 2 * MAX_QUEUE_SIZE),
432
+ /// and needs 2 bytes of padding
433
+ const USED_RING_BASE_ADDRESS : u64 = AVAIL_RING_BASE_ADDRESS + 6 + 2 * MAX_QUEUE_SIZE as u64 + 2 ;
434
+
435
+ /// The address of the first byte after the queue. Since our queue starts at guest physical
436
+ /// address 0, this is also the size of the memory area occupied by the queue.
437
+ /// Note that the used ring structure has size 6 + 8 * MAX_QUEUE_SIZE
438
+ const QUEUE_END : u64 = USED_RING_BASE_ADDRESS + 6 + 8 * MAX_QUEUE_SIZE as u64 ;
439
+
440
+ impl kani:: Arbitrary for ProofContext {
441
+ fn any ( ) -> Self {
442
+ let mem = setup_kani_guest_memory ( ) ;
443
+
444
+ let mut queue = Queue :: new ( MAX_QUEUE_SIZE ) . unwrap ( ) ;
445
+
446
+ queue. ready = true ;
447
+
448
+ queue. set_desc_table_address (
449
+ Some ( QUEUE_BASE_ADDRESS as u32 ) ,
450
+ Some ( ( QUEUE_BASE_ADDRESS >> 32 ) as u32 ) ,
451
+ ) ;
452
+
453
+ queue. set_avail_ring_address (
454
+ Some ( AVAIL_RING_BASE_ADDRESS as u32 ) ,
455
+ Some ( ( AVAIL_RING_BASE_ADDRESS >> 32 ) as u32 ) ,
456
+ ) ;
457
+
458
+ queue. set_used_ring_address (
459
+ Some ( USED_RING_BASE_ADDRESS as u32 ) ,
460
+ Some ( ( USED_RING_BASE_ADDRESS >> 32 ) as u32 ) ,
461
+ ) ;
462
+
463
+ queue. set_next_avail ( kani:: any ( ) ) ;
464
+ queue. set_next_used ( kani:: any ( ) ) ;
465
+ queue. set_event_idx ( kani:: any ( ) ) ;
466
+ queue. num_added = Wrapping ( kani:: any ( ) ) ;
467
+
468
+ kani:: assume ( queue. is_valid ( & mem) ) ;
469
+
470
+ ProofContext ( queue, mem)
471
+ }
472
+ }
473
+
474
+ #[ kani:: proof]
475
+ #[ kani:: unwind( 0 ) ] // There are no loops anywhere, but kani really enjoys getting stuck in std::ptr::drop_in_place.
476
+ // This is a compiler intrinsic that has a "dummy" implementation in stdlib that just
477
+ // recursively calls itself. Kani will generally unwind this recursion infinitely
478
+ fn verify_spec_2_7_7_2 ( ) {
479
+ // Section 2.7.7.2 deals with device-to-driver notification suppression.
480
+ // It describes a mechanism by which the driver can tell the device that it does not
481
+ // want notifications (IRQs) about the device finishing processing individual buffers
482
+ // (descriptor chain heads) from the avail ring until a specific number of descriptors
483
+ // has been processed. This is done by the driver
484
+ // defining a "used_event" index, which tells the device "please do not notify me until
485
+ // used.ring[used_event] has been written to by you".
486
+ let ProofContext ( mut queue, mem) = kani:: any ( ) ;
487
+
488
+ let num_added_old = queue. num_added . 0 ;
489
+ let needs_notification = queue. needs_notification ( & mem) ;
490
+
491
+ // event_idx_enabled equivalent to VIRTIO_F_EVENT_IDX negotiated
492
+ if !queue. event_idx_enabled {
493
+ // The specification here says
494
+ // After the device writes a descriptor index into the used ring:
495
+ // – If flags is 1, the device SHOULD NOT send a notification.
496
+ // – If flags is 0, the device MUST send a notification
497
+ // flags is the first field in the avail_ring_address, which we completely ignore. We
498
+ // always send a notification, and as there only is a SHOULD NOT, that is okay
499
+ assert ! ( needs_notification. unwrap( ) ) ;
500
+ } else {
501
+ // next_used - 1 is where the previous descriptor was placed
502
+ if Wrapping ( queue. used_event ( & mem, Ordering :: Relaxed ) . unwrap ( ) )
503
+ == std:: num:: Wrapping ( queue. next_used - Wrapping ( 1 ) )
504
+ && num_added_old > 0
505
+ {
506
+ // If the idx field in the used ring (which determined where that descriptor index
507
+ // was placed) was equal to used_event, the device MUST send a
508
+ // notification.
509
+ assert ! ( needs_notification. unwrap( ) ) ;
510
+
511
+ kani:: cover!( ) ;
512
+ }
513
+
514
+ // The other case is handled by a "SHOULD NOT send a notification" in the spec.
515
+ // So we do not care
516
+ }
517
+ }
518
+ }
519
+
272
520
impl < ' a > QueueGuard < ' a > for Queue {
273
521
type G = & ' a mut Self ;
274
522
}
0 commit comments