|
| 1 | +# Challenge 11: Safety of Methods for Numeric Primitive Types |
| 2 | + |
| 3 | + |
| 4 | +- **Status:** Open |
| 5 | +- **Tracking Issue:** [Link to issue](https://github.com/model-checking/verify-rust-std/issues/59) |
| 6 | +- **Start date:** *2024-08-20* |
| 7 | +- **End date:** *2024-12-10* |
| 8 | + |
| 9 | +------------------- |
| 10 | + |
| 11 | +## Goal |
| 12 | + |
| 13 | +Verify the safety of public unsafe methods for floats and integers in `core::num`. |
| 14 | + |
| 15 | +To find the documentation for these methods, navigate first to the [`core::num` documentation](https://doc.rust-lang.org/core/num/index.html), then click on the appropriate primitive type in the left-side menu bar. For example, for `i8::unchecked_add`, click on `i8`. |
| 16 | + |
| 17 | +## Success Criteria |
| 18 | + |
| 19 | +### Part 1: Unsafe Integer Methods |
| 20 | + |
| 21 | +Prove the absence of arithmetic overflow/underflow and undefined behavior in the following methods for each of the listed integer types, given that their safety preconditions are satisfied: |
| 22 | + |
| 23 | +| Method | Integer Types | |
| 24 | +| :--- | :--- |
| 25 | +| `unchecked_add` | `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
| 26 | +| `unchecked_sub` | `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
| 27 | +| `unchecked_mul` | `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
| 28 | +| `unchecked_shl` | `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
| 29 | +| `unchecked_shr` | `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
| 30 | +| `unchecked_neg` | `i8`, `i16`, `i32`, `i64`, `i128` | |
| 31 | + |
| 32 | + |
| 33 | +### Part 2: Safe API Verification |
| 34 | + |
| 35 | +Now, verify some of the safe APIs that leverage the unsafe integer methods from Part 1. Verify the absence of arithmetic overflow/underflow and undefined behavior of the following methods for each of the listed integer types: |
| 36 | + |
| 37 | +| Method | Integer Types | |
| 38 | +| :--- | :--- |
| 39 | +| `wrapping_shl` | `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
| 40 | +| `wrapping_shr` | `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
| 41 | +| `widening_mul` | `u8`, `u16`, `u32`, `u64` | |
| 42 | +| `carrying_mul` | `u8`, `u16`, `u32`, `u64` | |
| 43 | + |
| 44 | + |
| 45 | +### Part 3: Float to Integer Conversion |
| 46 | + |
| 47 | +Verify the absence of arithmetic overflow/underflow and undefined behavior in `to_int_unchecked` for `f16`, `f32`, `f64`, and `f128`. |
| 48 | + |
| 49 | + |
| 50 | +## List of UBs |
| 51 | + |
| 52 | +In addition to any properties called out as SAFETY comments in the source code, 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): |
| 53 | + |
| 54 | +* Invoking undefined behavior via compiler intrinsics. |
| 55 | +* Reading from uninitialized memory. |
| 56 | +* Mutating immutable bytes. |
| 57 | +* Producing an invalid value. |
| 58 | + |
| 59 | +Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](../general-rules.md) in addition to the ones listed above. |
0 commit comments