Skip to content

Add safety preconditions to core/src/iter/range.rs #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/kani.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ jobs:
- name: Run Kani Verification
run: |
scripts/run-kani.sh --run autoharness --kani-args \
--include-pattern "iter::range::Step>::backward_unchecked" \
--include-pattern "iter::range::Step>::forward_unchecked" \
--include-pattern alloc::layout::Layout::from_size_align \
--include-pattern ascii::ascii_char::AsciiChar::from_u8 \
--include-pattern char::convert::from_u32_unchecked \
Expand Down
25 changes: 25 additions & 0 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use safety::requires;

use super::{
FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, TrustedStep,
};
use crate::ascii::Char as AsciiChar;
#[cfg(kani)]
use crate::kani;
use crate::mem;
use crate::net::{Ipv4Addr, Ipv6Addr};
use crate::num::NonZero;
Expand Down Expand Up @@ -183,12 +187,14 @@ pub trait Step: Clone + PartialOrd + Sized {
// than the signed::MAX value. Therefore `as` casting to the signed type would be incorrect.
macro_rules! step_signed_methods {
($unsigned: ty) => {
#[requires(start.checked_add_unsigned(n as $unsigned).is_some())]
#[inline]
unsafe fn forward_unchecked(start: Self, n: usize) -> Self {
// SAFETY: the caller has to guarantee that `start + n` doesn't overflow.
unsafe { start.checked_add_unsigned(n as $unsigned).unwrap_unchecked() }
}

#[requires(start.checked_sub_unsigned(n as $unsigned).is_some())]
#[inline]
unsafe fn backward_unchecked(start: Self, n: usize) -> Self {
// SAFETY: the caller has to guarantee that `start - n` doesn't overflow.
Expand All @@ -199,12 +205,14 @@ macro_rules! step_signed_methods {

macro_rules! step_unsigned_methods {
() => {
#[requires(start.checked_add(n as Self).is_some())]
#[inline]
unsafe fn forward_unchecked(start: Self, n: usize) -> Self {
// SAFETY: the caller has to guarantee that `start + n` doesn't overflow.
unsafe { start.unchecked_add(n as Self) }
}

#[requires(start >= (n as Self))]
#[inline]
unsafe fn backward_unchecked(start: Self, n: usize) -> Self {
// SAFETY: the caller has to guarantee that `start - n` doesn't overflow.
Expand Down Expand Up @@ -494,6 +502,13 @@ impl Step for char {
Some(unsafe { char::from_u32_unchecked(res) })
}

#[requires((start as u32).checked_add(count as u32).is_some())]
#[requires({
let dist = (start as u32).checked_add(count as u32);
dist.is_some() &&
((start as u32) >= 0xD800 || dist.unwrap() < 0xD800 ||
dist.unwrap().checked_add(0x800).is_some())
})]
#[inline]
unsafe fn forward_unchecked(start: char, count: usize) -> char {
let start = start as u32;
Expand All @@ -510,6 +525,12 @@ impl Step for char {
unsafe { char::from_u32_unchecked(res) }
}

#[requires({
let dist = (start as u32).checked_sub(count as u32);
dist.is_some() &&
((start as u32) < 0xE000 || dist.unwrap() >= 0xE000 ||
dist.unwrap().checked_sub(0x800).is_some())
})]
#[inline]
unsafe fn backward_unchecked(start: char, count: usize) -> char {
let start = start as u32;
Expand Down Expand Up @@ -548,6 +569,7 @@ impl Step for AsciiChar {
Some(unsafe { AsciiChar::from_u8_unchecked(end) })
}

#[requires((start.to_u8() as u32).checked_add(count as u32).is_some())]
#[inline]
unsafe fn forward_unchecked(start: AsciiChar, count: usize) -> AsciiChar {
// SAFETY: Caller asserts that result is a valid ASCII character,
Expand All @@ -558,6 +580,7 @@ impl Step for AsciiChar {
unsafe { AsciiChar::from_u8_unchecked(end) }
}

#[requires((start.to_u8() as u32).checked_sub(count as u32).is_some())]
#[inline]
unsafe fn backward_unchecked(start: AsciiChar, count: usize) -> AsciiChar {
// SAFETY: Caller asserts that result is a valid ASCII character,
Expand Down Expand Up @@ -586,13 +609,15 @@ impl Step for Ipv4Addr {
u32::backward_checked(start.to_bits(), count).map(Ipv4Addr::from_bits)
}

#[requires(start.to_bits().checked_add(count as u32).is_some())]
#[inline]
unsafe fn forward_unchecked(start: Ipv4Addr, count: usize) -> Ipv4Addr {
// SAFETY: Since u32 and Ipv4Addr are losslessly convertible,
// this is as safe as the u32 version.
Ipv4Addr::from_bits(unsafe { u32::forward_unchecked(start.to_bits(), count) })
}

#[requires(start.to_bits().checked_sub(count as u32).is_some())]
#[inline]
unsafe fn backward_unchecked(start: Ipv4Addr, count: usize) -> Ipv4Addr {
// SAFETY: Since u32 and Ipv4Addr are losslessly convertible,
Expand Down
Loading