Skip to content

Require unsafe fn or proven wrapper for raw-pointer validity preconditions #366

Closed

Description

@ehartford

Summary

The raw-pointer spec now says passing a raw pointer to a function is not unsafe by itself. The obligation lives in the callee's contract. A function that dereferences, retains, mutates through, converts, or otherwise relies on caller-guaranteed validity of a raw-pointer parameter has a safety precondition that the raw pointer type does not encode, so it must be an unsafe fn unless the compiler or binding provides a proven safe wrapper.

Current behavior permits a safe function to dereference a raw pointer internally under an unsafe block, then be called safely by users with no indication that the caller must provide a valid pointer.

Repro

fn touch(p: *mut i32) -> i32:
    unsafe *p

fn main:
    var value = 1
    let p = (&raw mut value) as *mut i32
    let x = touch(p)  // currently safe call
    let _ = x

Observed with ./out/bin/with check: currently reports ok.

Expected

The compiler should not let raw-pointer validity preconditions hide behind a safe callable surface.

Possible implementation shape:

  • A function that performs an unsafe raw-pointer memory access or raw-to-safe conversion using a raw pointer parameter is inferred/required to be unsafe fn unless it proves or models the precondition internally.
  • A genuinely safe wrapper can remain safe when it validates/models nullability, bounds, lifetime, ownership, retention, mutation, and permissions before exposing safe behavior.
  • Calling an unsafe raw-pointer-precondition function requires unsafe at the call site.
  • A function that only computes, compares, observes, or relabels raw pointer values remains safe.

Root cause / 5 Whys

  1. Why can callers invoke touch(p) safely? The unsafe operation is contained inside the callee body, so the public callable type stays safe.
  2. Why is that wrong? The caller still supplies the validity fact needed by unsafe *p.
  3. Why doesn't *mut T encode that fact? Raw pointers are validity-less values; they do not encode liveness, alignment, initialization, ownership, permissions, or provenance.
  4. Why can't an internal unsafe block be enough? It localizes the memory access but hides the caller-facing safety precondition.
  5. Root cause: function safety currently tracks explicit unsafe operations syntactically, not whether a callable's public contract relies on caller-guaranteed raw-pointer validity.

Acceptance criteria

  • Safe functions may accept raw pointers when they only compute/compare/relabel them or when they prove/model validity internally.
  • Functions whose correctness relies on caller-provided raw-pointer validity are unsafe fn or rejected until marked/converted.
  • Manual extern and raw/unmodeled ABI functions with raw-pointer contracts remain unsafe by default unless a safe wrapper models the contract.
  • Tests cover safe raw-pointer computation helpers, unsafe raw-pointer dereference helpers, and a safe wrapper that validates/models its contract.
  • with build, with build :fixpoint, with build :test, and with build :test-green pass.

Activity

  1. ehartford commented on Jun 9, 2026

    @ehartford
    ContributorAuthor

    Implemented in 0030c68 (pushed to main). Verified with with build, with build :fixpoint, with build :test, and with build :test-green on the rebased main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions