|
| 1 | +# Challenge 12: Safety of `NonZero` |
| 2 | + |
| 3 | +- **Status:** Open |
| 4 | +- **Tracking Issue:** [Link to issue](https://github.com/model-checking/verify-rust-std/issues/71) |
| 5 | +- **Start date:** *2024-08-23* |
| 6 | +- **End date:** *2024-12-10* |
| 7 | + |
| 8 | +------------------- |
| 9 | + |
| 10 | +## Goal |
| 11 | + |
| 12 | +Verify the safety of `NonZero` in `core::num`. |
| 13 | + |
| 14 | +### Assumptions |
| 15 | + |
| 16 | +`new` and `get` leverage `transmute_unchecked`, so verifying the safety of these methods would require verifying that transmutations are safe. This task is out of scope for this challenge (instead, it's work for [Challenge 1](0001-core-transmutation.md)). For this challenge, for a transmutation from type `T` to type `U`, it suffices to write and verify a contract that `T` and `U` have the same size. |
| 17 | + |
| 18 | +You may assume that each `NonZeroInner` type upholds the safety conditions of the `ZeroablePrimitive` trait. Specifically, you need not verify that the integer primitives which implement `ZeroablePrimitive` are valid when 0, or that transmutations to the `Option` type are sound. |
| 19 | + |
| 20 | +### Success Criteria |
| 21 | + |
| 22 | +#### Part 1: `new` and `new_unchecked` |
| 23 | + |
| 24 | +Verify the safety and correctness of `NonZero::new` and `NonZero::new_unchecked`. |
| 25 | + |
| 26 | +Specifically, write and verify contracts specifying the following: |
| 27 | +1. The preconditions specified by the `SAFETY` comments are upheld. |
| 28 | +2. For an input `n`: |
| 29 | + a. A `NonZero` object is created if and only if the input was nonzero. |
| 30 | + b. The value of the `NonZeroInner` object equals `n`. |
| 31 | + |
| 32 | +#### Part 2: Other Uses of `unsafe` |
| 33 | + |
| 34 | +Verify the safety of the following functions and methods (all located within `core::num::nonzero`): |
| 35 | + |
| 36 | +| Function | |
| 37 | +|--------- | |
| 38 | +| `max` | |
| 39 | +| `min` | |
| 40 | +| `clamp` | |
| 41 | +| `bitor` (all 3 implementations) | |
| 42 | +| `count_ones` | |
| 43 | +| `rotate_left` | |
| 44 | +| `rotate_right` | |
| 45 | +| `swap_bytes` | |
| 46 | +| `reverse_bits` | |
| 47 | +| `from_be` | |
| 48 | +| `from_le` | |
| 49 | +| `to_be` | |
| 50 | +| `to_le` | |
| 51 | +| `checked_mul` | |
| 52 | +| `saturating_mul` | |
| 53 | +| `unchecked_mul` | |
| 54 | +| `checked_pow` | |
| 55 | +| `saturating_pow` | |
| 56 | +| `neg` | |
| 57 | +| `checked_add` | |
| 58 | +| `saturating_add` | |
| 59 | +| `unchecked_add` | |
| 60 | +| `checked_next_power_of_two` | |
| 61 | +| `midpoint` | |
| 62 | +| `isqrt` | |
| 63 | +| `abs` | |
| 64 | +| `checked_abs` | |
| 65 | +| `overflowing_abs` | |
| 66 | +| `saturating_abs` | |
| 67 | +| `wrapping_abs` | |
| 68 | +| `unsigned_abs` | |
| 69 | +| `checked_neg` | |
| 70 | +| `overflowing_neg` | |
| 71 | +| `wrapping_neg` | |
| 72 | +| `from_mut` | |
| 73 | +| `from_mut_unchecked` | |
| 74 | + |
| 75 | +You are not required to write correctness contracts for these methods (e.g., for `max`, ensuring that the `result` is indeed the maximum of the inputs), but it would be great to do so! |
| 76 | + |
| 77 | +### List of UBs |
| 78 | + |
| 79 | +In addition to any properties called out as `SAFETY` comments in the source |
| 80 | +code, all proofs must automatically ensure the absence of the following undefined behaviors [ref](https://github.com/rust-lang/reference/blob/142b2ed77d33f37a9973772bd95e6144ed9dce43/src/behavior-considered-undefined.md): |
| 81 | + |
| 82 | +* Invoking undefined behavior via compiler intrinsics. |
| 83 | +* Reading from uninitialized memory. |
| 84 | +* Producing an invalid value. |
| 85 | + |
| 86 | +Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](../general-rules.md) |
| 87 | +in addition to the ones listed above. |
0 commit comments