Skip to content

Does dealloc care about provenance? #149702

@scottmcm

Description

@scottmcm

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsA-strict-provenanceArea: Strict provenance for raw pointersT-opsemRelevant to the opsem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions