Skip to content
Merged
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
44 changes: 44 additions & 0 deletions src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,30 @@ impl Step for VirtAddr {
fn backward_checked(start: Self, count: usize) -> Option<Self> {
Self::backward_checked_u64(start, u64::try_from(count).ok()?)
}

// Kani's bundled toolchain predates these methods being added to `Step`.
// Exclude them there so the crate still compiles under `cargo kani`.
// This can be removed once Kani upgrades its bundled toolchain to nightly-2026-07-10 or later.
#[cfg(not(kani))]
#[inline]
fn forward_overflowing(start: Self, count: usize) -> (Self, bool) {
match Self::forward_checked(start, count) {
Some(next) => (next, false),
None => (start, true),
}
}

// Kani's bundled toolchain predates these methods being added to `Step`.
// Exclude them there so the crate still compiles under `cargo kani`.
// This can be removed once Kani upgrades its bundled toolchain to nightly-2026-07-10 or later.
#[cfg(not(kani))]
#[inline]
fn backward_overflowing(start: Self, count: usize) -> (Self, bool) {
match Self::backward_checked(start, count) {
Some(next) => (next, false),
None => (start, true),
}
}
}

#[cfg(kani)]
Expand Down Expand Up @@ -936,6 +960,26 @@ mod tests {
);
}

#[test]
#[cfg(feature = "step_trait")]
fn virtaddr_step_overflowing() {
assert_eq!(
Step::forward_overflowing(VirtAddr(0x7fff_ffff_ffff), 1),
(VirtAddr(0xffff_8000_0000_0000), false)
);
assert_eq!(
Step::backward_overflowing(VirtAddr(0xffff_8000_0000_0000), 1),
(VirtAddr(0x7fff_ffff_ffff), false)
);
assert_eq!(
Step::forward_overflowing(VirtAddr(0), 0),
(VirtAddr(0), false)
);

assert!(Step::forward_overflowing(VirtAddr(0xffff_ffff_ffff_ffff), 1).1);
assert!(Step::backward_overflowing(VirtAddr(0), 1).1);
}

#[test]
pub fn test_align_up() {
// align 1
Expand Down
40 changes: 40 additions & 0 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,28 @@ impl<S: PageSize> Step for Page<S> {
size: PhantomData,
})
}

// Kani's bundled toolchain predates these methods being added to `Step`.
// Exclude them there so the crate still compiles under `cargo kani`.
// This can be removed once Kani upgrades its bundled toolchain to nightly-2026-07-10 or later.
#[cfg(not(kani))]
fn forward_overflowing(start: Self, count: usize) -> (Self, bool) {
match Self::forward_checked(start, count) {
Some(next) => (next, false),
None => (start, true),
}
}

// Kani's bundled toolchain predates these methods being added to `Step`.
// Exclude them there so the crate still compiles under `cargo kani`.
// This can be removed once Kani upgrades its bundled toolchain to nightly-2026-07-10 or later.
#[cfg(not(kani))]
fn backward_overflowing(start: Self, count: usize) -> (Self, bool) {
match Self::backward_checked(start, count) {
Some(next) => (next, false),
None => (start, true),
}
}
}

/// A range of pages with exclusive upper bound.
Expand Down Expand Up @@ -914,6 +936,24 @@ mod tests {
assert_eq!(Step::steps_between(&start, &end), (lower, upper));
}
}

#[test]
#[cfg(feature = "step_trait")]
fn page_step_overflowing() {
let page = |addr| Page::<Size4KiB>::from_start_address(VirtAddr::new(addr)).unwrap();

assert_eq!(
Step::forward_overflowing(page(0x7fff_ffff_f000), 1),
(page(0xffff_8000_0000_0000), false)
);
assert_eq!(
Step::backward_overflowing(page(0xffff_8000_0000_0000), 1),
(page(0x7fff_ffff_f000), false)
);

assert!(Step::forward_overflowing(page(0xffff_ffff_ffff_f000), 1).1);
assert!(Step::backward_overflowing(page(0), 1).1);
}
}

#[cfg(kani)]
Expand Down
46 changes: 46 additions & 0 deletions src/structures/paging/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,30 @@ impl Step for PageTableIndex {
let idx = usize::from(start).checked_sub(count)?;
Some(Self::new(idx as u16))
}

// Kani's bundled toolchain predates these methods being added to `Step`.
// Exclude them there so the crate still compiles under `cargo kani`.
// This can be removed once Kani upgrades its bundled toolchain to nightly-2026-07-10 or later.
#[cfg(not(kani))]
#[inline]
fn forward_overflowing(start: Self, count: usize) -> (Self, bool) {
match Self::forward_checked(start, count) {
Some(next) => (next, false),
None => (start, true),
}
}

// Kani's bundled toolchain predates these methods being added to `Step`.
// Exclude them there so the crate still compiles under `cargo kani`.
// This can be removed once Kani upgrades its bundled toolchain to nightly-2026-07-10 or later.
#[cfg(not(kani))]
#[inline]
fn backward_overflowing(start: Self, count: usize) -> (Self, bool) {
match Self::backward_checked(start, count) {
Some(next) => (next, false),
None => (start, true),
}
}
}

/// A 12-bit offset into a 4KiB Page.
Expand Down Expand Up @@ -485,3 +509,25 @@ impl PageTableLevel {
1u64 << (((self as u8 - 1) * 9) + 12)
}
}

#[cfg(all(test, feature = "step_trait"))]
mod tests {
use super::*;
use core::iter::Step;

#[test]
fn page_table_index_step_overflowing() {
assert_eq!(
Step::forward_overflowing(PageTableIndex::new(0), 511),
(PageTableIndex::new(511), false)
);
assert_eq!(
Step::backward_overflowing(PageTableIndex::new(511), 511),
(PageTableIndex::new(0), false)
);

// Overflow the 0..512 range: only the flag is specified.
assert!(Step::forward_overflowing(PageTableIndex::new(511), 1).1);
assert!(Step::backward_overflowing(PageTableIndex::new(0), 1).1);
}
}
Loading