Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Challenge 26: Verify reference-counted Cell implementation

  • Status: Open
  • Solution: Option field to point to the PR that solved this challenge.
  • Tracking Issue: #382
  • Start date: 2025/06/01
  • End date: 2025/12/31
  • Reward: 10,000 USD

Goal

The goal of this challenge is to verify Rc (meaning "Reference counted") and its related Weak implementation. Rc is the library-provided building block that enables safe multiple ownership of data through reference counting for single-threaded cases, as opposed to the usual ownership types used by Rust. A related challenge verifies the Arc implementation, which is atomic multi-threaded.

Motivation

The Rc (for single-threaded code) and Arc (atomic multi-threaded) cell types are widely used in Rust programs to enable shared ownership of data through reference counting. Since shared ownership is generally not permitted by Rust's type system, these implementations use unsafe code to bypass Rust's usual compile-time checks. Verifying the Rust standard library thus fundamentally requires verification of these types.

Description

This challenge verifies a number of Rc methods that encapsulate unsafety, as well as providing contracts for unsafe methods that impose safety conditions on their callers for correct use.

A key part of the Rc implementation is the Weak struct, which allows keeping a temporary reference to an Rc without creating circular references and without protecting the inner value from being dropped.

Assumptions

Some properties needed for safety are beyond the ability of the Rust type system to express. This is true for all challenges, but we point out some of the properties that are relevant for this challenge.

  • You are not required to check that the reference count is greater than 0 when it is being decremented.

  • Showing that something is initialized, as required by assume_init, appears to be beyond the current state of the art for type systems, so it may be impossible to express the full safety property required there.

Success Criteria

All the following pub unsafe functions must be annotated with safety contracts and the contracts have been verified:

FunctionLocation
Rc<mem::MaybeUninit<T>,A>::assume_initalloc::rc
Rc<[mem::MaybeUninit<T>],A>::assume_initalloc::rc
Rc<T:?Sized>::from_rawalloc::rc
Rc<T:?Sized>::increment_strong_countalloc::rc
Rc<T:?Sized>::decrement_strong_countalloc::rc
Rc<T:?Sized,A:Allocator>::from_raw_inalloc::rc
Rc<T:?Sized,A:Allocator>::increment_strong_count_inalloc::rc
Rc<T:?Sized,A:Allocator>::decrement_strong_count_inalloc::rc
Rc<T:?Sized,A:Allocator>::get_mut_uncheckedalloc::rc
Rc<dyn Any,A:Allocator>::downcast_uncheckedalloc::rc
Weak<T:?Sized>::from_rawalloc::rc
Weak<T:?Sized,A:Allocator>::from_raw_inalloc::rc

These (not necessarily public) functions contain unsafe code in their bodies but are not themselves marked unsafe. At least 75% of these should be proven unconditionally safe, or safety contracts should be added.

FunctionLocation
Rc<T:?Sized, A:Allocator>::inneralloc::rc
Rc<T:?Sized, A:Allocator>::into_inner_with_allocatoralloc::rc
Rc<T>::newalloc::rc
Rc<T>::new_uninitalloc::rc
Rc<T>::new_zeroedalloc::rc
Rc<T>::try_newalloc::rc
Rc<T>::try_new_uninitalloc::rc
Rc<T>::try_new_zeroedalloc::rc
Rc<T>::pinalloc::rc
Rc<T,A:Allocator>::new_uninit_inalloc::rc
Rc<T,A:Allocator>::new_zeroed_inalloc::rc
Rc<T,A:Allocator>::new_cyclic_inalloc::rc
Rc<T,A:Allocator>::try_new_inalloc::rc
Rc<T,A:Allocator>::try_new_uninit_inalloc::rc
Rc<T,A:Allocator>::try_new_zeroed_inalloc::rc
Rc<T,A:Allocator>::pin_inalloc::rc
Rc<T,A:Allocator>::try_unwrapalloc::rc
Rc<[T]>::new_uninit_slicealloc::rc
Rc<[T]>::new_zeroed_slicealloc::rc
Rc<[T]>::into_arrayalloc::rc
Rc<[T],A:Allocator>::new_uninit_slice_inalloc::rc
Rc<[T],A:Allocator>::new_zeroed_slice_inalloc::rc
Rc<T:?Sized,A:Allocator>::into_raw_with_allocatoralloc::rc
Rc<T:?Sized,A:Allocator>::as_ptralloc::rc
Rc<T:?Sized,A:Allocator>::get_mutalloc::rc
Rc<T:?Sized+CloneToUninit, A:Allocator+Clone>::make_mutalloc::rc
Rc<dyn Any,A:Allocator>::downcastalloc::rc
Rc<T:?Sized,A:Allocator>::from_box_inalloc::rc
RcFromSlice<T: Clone>::from_slicealloc::rc
RcFromSlice<T: Copy>::from_slicealloc::rc
Drop<T: ?Sized, A:Allocator>::drop for Rcalloc::rc
Clone<T: ?Sized, A:Allocator>::clone for Rcalloc::rc
Default<T:Default>::defaultalloc::rc
Default<str>::defaultalloc::rc
From<&Str>::fromalloc::rc
From<Vec<T,A:Allocator>>::fromalloc::rc
From<Rc<str>>::fromalloc::rc
TryFrom<Rc[T],A:Allocator>::try_fromalloc::rc
ToRcSlice<T, I>::to_rc_slicealloc::rc
Weak<T:?Sized,A:Allocator>::as_ptralloc::rc
Weak<T:?Sized,A:Allocator>::into_raw_with_allocatoralloc::rc
Weak<T:?Sized,A:Allocator>::upgradealloc::rc
Weak<T:?Sized,A:Allocator>::inneralloc::rc
Drop<T:?Sized, A:Allocator>::drop for Weakalloc::rc
RcInnerPtr::inc_strongalloc::rc
RcInnerPtr::inc_weakalloc::rc
UniqueRc<T:?Sized,A:Allocator>::into_rcalloc::rc
UniqueRc<T:?Sized,A:Allocator+Clone>::downgradealloc::rc
Deref<T:?Sized,A:Allocator>::derefalloc::rc
DerefMut<T:?Sized,A:Allocator>::deref_mutalloc::rc
Drop<T:?Sized, A:Allocator>::drop for UniqueRcalloc::rc
UniqueRcUninit<T:?Sized, A:Allocator>::newalloc::rc
UniqueRcUninit<T:?Sized, A:Allocator>::data_ptralloc::rc
Drop<T:?Sized, A:Allocator>::drop for UniqueRcUninitalloc::rc

This list excludes non-public unsafe functions; relevant ones should be called from public unsafe functions.

For functions taking inputs of generic type 'T', the proofs can be limited to primitive types only. Moreover, for functions taking allocators as input, the proofs can be limited to only the allocators implemented in the standard library (Global/System).

List of UBs

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:

  • Accessing (loading from or storing to) a place that is dangling or based on a misaligned pointer.
  • Invoking undefined behavior via compiler intrinsics.
  • Mutating immutable bytes.
  • Producing an invalid value.

Note: All solutions to verification challenges need to satisfy the criteria established in the challenge book in addition to the ones listed above.