From 068bcab5331071579bf613668647d18a93dc82dc Mon Sep 17 00:00:00 2001 From: Daniel Larkin Date: Thu, 26 Nov 2026 21:02:46 -0500 Subject: [PATCH 1/2] Move strlen's const-eval loop into a named function Items inside `const_eval_select!` can't be named from outside, so a harness can't reach the loop. Move it, unchanged, into `strlen_loop`. No change in behaviour. --- library/core/src/ffi/c_str.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs index f7a9b3b12423a..a333e7e699fb7 100644 --- a/library/core/src/ffi/c_str.rs +++ b/library/core/src/ffi/c_str.rs @@ -874,12 +874,8 @@ const unsafe fn strlen(ptr: *const c_char) -> UsizeNoHighBitMinusOne { const_eval_select!( @capture { s: *const c_char = ptr } -> UsizeNoHighBitMinusOne: if const { - let mut len = 0; - // SAFETY: Outer caller has provided a pointer to a valid C string. - while unsafe { *s.add(len) } != 0 { - len += 1; - } + let len = unsafe { strlen_loop(s) }; UsizeNoHighBitMinusOne::new(len).unwrap() } else { @@ -895,6 +891,23 @@ const unsafe fn strlen(ptr: *const c_char) -> UsizeNoHighBitMinusOne { ) } +/// The loop of `strlen`'s `if const` arm, moved out of it unchanged so that verification harnesses +/// can name it: items inside the arms of `const_eval_select!` cannot be named from outside. +/// +/// # Safety +/// +/// `s` must point to a valid C string. +const unsafe fn strlen_loop(s: *const c_char) -> usize { + let mut len = 0; + + // SAFETY: Outer caller has provided a pointer to a valid C string. + while unsafe { *s.add(len) } != 0 { + len += 1; + } + + len +} + /// An iterator over the bytes of a [`CStr`], without the nul terminator. /// /// This struct is created by the [`bytes`] method on [`CStr`]. From 7b4315fd36ccc42224187decea867ac7e970d5fe Mon Sep 17 00:00:00 2001 From: Daniel Larkin Date: Thu, 26 Nov 2026 21:02:46 -0500 Subject: [PATCH 2/2] Verify strlen's loop without an unwind bound (Challenge 13) Add a loop invariant to `strlen_loop` and a harness that checks it for strings of any length. Both are Kani-only. --- library/core/src/ffi/c_str.rs | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs index a333e7e699fb7..cb92aa947c685 100644 --- a/library/core/src/ffi/c_str.rs +++ b/library/core/src/ffi/c_str.rs @@ -900,6 +900,7 @@ const unsafe fn strlen(ptr: *const c_char) -> UsizeNoHighBitMinusOne { const unsafe fn strlen_loop(s: *const c_char) -> usize { let mut len = 0; + #[safety::loop_invariant(len <= verify::strlen_ghost_nul_at())] // SAFETY: Outer caller has provided a pointer to a valid C string. while unsafe { *s.add(len) } != 0 { len += 1; @@ -1433,6 +1434,60 @@ mod verify { kani::cover(len > 0, "non-vacuity: a non-empty C string (strlen > 0) is reachable"); } + // The offset of a nul in the string, for `strlen_loop`'s loop invariant. `strlen_loop` is a + // `const fn`, which can't call `kani::any`, so the harness passes it through this static. + static mut STRLEN_GHOST_NUL_AT: usize = 0; + + pub(super) fn strlen_ghost_nul_at() -> usize { + // SAFETY: Kani is single-threaded. + unsafe { STRLEN_GHOST_NUL_AT } + } + + // `strlen_loop` on strings of any length: the buffer can be any size up to + // `MAX_ALLOCATION_BYTES`, and there's no `kani::unwind`. The string starts at the start of the + // buffer, which loses nothing, since the loop only reads forward from `s`. + #[kani::proof] + fn check_strlen_loop_unbounded() { + // Fits in the largest object CBMC can address with the `--object-bits 12` that CI passes. + const MAX_ALLOCATION_BYTES: usize = 1 << 48; + + // `core` can't call `alloc::alloc`, so this calls Kani's model of the allocator directly. + unsafe extern "Rust" { + fn __rust_alloc(size: usize, align: usize) -> *mut u8; + } + let size: usize = kani::any_where(|&n: &usize| n > 0 && n <= MAX_ALLOCATION_BYTES); + // SAFETY: `size` is nonzero, and 1 is a valid alignment. + let ptr = unsafe { __rust_alloc(size, 1) } as *const c_char; + kani::assume(!ptr.is_null()); + + // `strlen`'s precondition: a nul within `isize::MAX` bytes of `ptr`, readable up to it. + // SAFETY: `&&` short-circuits, so `*ptr.add(nul_at)` is only read once it's readable. + let nul_at: usize = kani::any(); + kani::assume( + nul_at < isize::MAX as usize + && kani::mem::can_dereference(crate::ptr::slice_from_raw_parts(ptr, nul_at + 1)) + && unsafe { *ptr.add(nul_at) } == 0, + ); + // SAFETY: Kani is single-threaded. + unsafe { STRLEN_GHOST_NUL_AT = nul_at }; + + // SAFETY: by the assumptions above, `ptr` is a valid C string. + let len = unsafe { super::strlen_loop(ptr) }; + + // `strlen`'s postcondition: `len` is the offset of the first nul. + // SAFETY: the loop read every byte up to and including `*ptr.add(len)`. + assert!(len < isize::MAX as usize); + assert!(unsafe { *ptr.add(len) } == 0); + // `nul_at` can be any nul, so `len` is at most the offset of every nul: none is before it. + assert!(len <= nul_at); + + kani::cover(len == 0, "non-vacuity: an empty string"); + kani::cover( + size == MAX_ALLOCATION_BYTES && len == size - 1, + "non-vacuity: the longest string in the largest buffer", + ); + } + // pub const unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr (criterion 3) // // `proof_for_contract` assumes the precondition (non-null, a nul within