rust-lang / rust-lang/rust

Does `dealloc` care about provenance?

Open
#149,702 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-allocators A-strict-provenance T-opsem
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

https://doc.rust-lang.org/alloc/alloc/fn.dealloc.html points to https://doc.rust-lang.org/alloc/alloc/trait.GlobalAlloc.html#tymethod.dealloc, which says nothing about provenance, just

  • ptr is a block of memory currently allocated via this allocator and,
  • layout is the same layout that was used to allocate that block of memory.

(https://doc.rust-lang.org/alloc/alloc/trait.Allocator.html#tymethod.deallocate also doesn't mention provenance, nor does https://doc.rust-lang.org/alloc/alloc/trait.Allocator.html#currently-allocated-memory.)

Does the ptr need to have any particular provenance? Does an allocator need to launder the provenance back to a known-valid one in order to read/write through the pointer it got in dealloc?

Whatever the answer, it would be good to say either way in docs.

FWIW, MIRI rejects this today ("pointer not dereferenceable"):

let layout = Layout::new::<i32>();
let p: *mut u8 = alloc(layout);
let other: *mut u8 = without_provenance_mut(p.addr());
dealloc(other, layout);

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=0f058facb5747bdb47e8e44ccdfc2b71

And also rejects this (with an odd "does not point to the beginning of an object" error that's arguably not true):

let layout = Layout::new::<i32>();
let p: *mut u8 = alloc(layout);
let mut mylocal = 10_u8;
let other = (&raw mut mylocal).with_addr(p.addr());
dealloc(other, layout);

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=5977dfe5da7f5700f902f8386f1a7f59

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the linked alloc::dealloc, GlobalAlloc::dealloc, Allocator::deallocate, and currently-allocated-memory documentation, then reproduce the two Miri examples. Research and document whether the pointer passed to dealloc requires particular provenance, including any implications for allocator reads or writes. Done means the relevant docs state the provenance requirement unambiguously.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.