|
| 1 | +# Challenge 9: Safe abstractions for `core::time::Duration` |
| 2 | + |
| 3 | +- **Status:** Open |
| 4 | +- **Tracking Issue:** [#72](https://github.com/model-checking/verify-rust-std/issues/72) |
| 5 | +- **Start date:** *2024-08-20* |
| 6 | +- **End date:** *2024-12-20* |
| 7 | + |
| 8 | +------------------- |
| 9 | + |
| 10 | + |
| 11 | +## Goal |
| 12 | + |
| 13 | +Write function contracts for `core::time::Duration` that can be used as safe abstractions. |
| 14 | +Even though the majority of `Duration` methods are safe, many of them are safe abstractions over unsafe code. |
| 15 | + |
| 16 | +For instance, the `new` method is implemented as follows in v1.3.0: |
| 17 | +```rust |
| 18 | +pub const fn new(secs: u64, nanos: u32) -> Duration { |
| 19 | + if nanos < NANOS_PER_SEC { |
| 20 | + // SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range |
| 21 | + Duration { secs, nanos: unsafe { Nanoseconds(nanos) } } |
| 22 | + } else { |
| 23 | + let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { |
| 24 | + Some(secs) => secs, |
| 25 | + None => panic!("overflow in Duration::new"), |
| 26 | + }; |
| 27 | + let nanos = nanos % NANOS_PER_SEC; |
| 28 | + // SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range |
| 29 | + Duration { secs, nanos: unsafe { Nanoseconds(nanos) } } |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Success Criteria |
| 35 | + |
| 36 | +Write a [type invariant](https://model-checking.github.io/kani/crates/doc/kani/derive.Invariant.html) for the struct `Duration`. Write function contracts for the following public functions. |
| 37 | + |
| 38 | +1. `Duration::new(secs: u64, nanos: u32) -> Duration` |
| 39 | +2. `Duration::from_secs(secs: u64) -> Duration` |
| 40 | +3. `Duration::from_millis(millis: u64) -> Duration` |
| 41 | +4. `Duration::from_micros(micros: u64) -> Duration` |
| 42 | +5. `Duration::from_nanos(nanos: u64) -> Duration` |
| 43 | + |
| 44 | +6. `Duration::as_secs(&self) -> u64` |
| 45 | +7. `Duration::as_millis(&self) -> u128` |
| 46 | +8. `Duration::as_micros(&self) -> u128` |
| 47 | +9. `Duration::as_nanos(&self) -> u128` |
| 48 | +10. `Duration::subsec_millis(&self) -> u32` |
| 49 | +11. `Duration::subsec_micros(&self) -> u32` |
| 50 | +12. `Duration::subsec_nanos(&self) -> u32` |
| 51 | + |
| 52 | +13. `Duration::checked_add(&self, rhs: Duration) -> Option<Duration>` |
| 53 | +14. `Duration::checked_sub(&self, rhs: Duration) -> Option<Duration>` |
| 54 | +15. `Duration::checked_mul(&self, rhs: u32) -> Option<Duration>` |
| 55 | +16. `Duration::checked_div(&self, rhs: u32) -> Option<Duration>` |
| 56 | + |
| 57 | +The memory safety and the contracts of the above listed functions must be verified |
| 58 | +for all possible input values. |
| 59 | + |
| 60 | +### List of UBs |
| 61 | + |
| 62 | +In addition to any properties called out as `SAFETY` comments in the source |
| 63 | +code, |
| 64 | +all proofs must automatically ensure the absence of the following [undefined behaviors](https://github.com/rust-lang/reference/blob/142b2ed77d33f37a9973772bd95e6144ed9dce43/src/behavior-considered-undefined.md): |
| 65 | + |
| 66 | +* Accessing (loading from or storing to) a place that is dangling or based on a misaligned pointer. |
| 67 | +* Reading from uninitialized memory. |
| 68 | +* Mutating immutable bytes. |
| 69 | +* Producing an invalid value |
| 70 | + |
| 71 | +Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](../general-rules.md) |
| 72 | +in addition to the ones listed above. |
0 commit comments