Skip to content

unsafe fn malloc_size_of doesn't explicitly document its safety requirements #8

@anforowicz

Description

@anforowicz

It would be great if the documentation of MallocSizeOfOps.malloc_size_of would explicitly list the safety requirements of this unsafe function. This would make clippy happy (see the missing_safety_doc lint) and would also make it easier to audit calls into the function (e.g. when auditing recent smallvec changes here).

It's a bit hard to reverse-engineer the safety requirements, because it is impossible to look at all the impls of the MallocSizeOfOps trait, but I have some guesses below as a tentative starting point:

  • Maybe: ptr needs to be point to memory currently allocated by the allocator associated with the given impl of MallocSizeOfOps?
    • But if so, then it may mean that shallow_size_of in smallvec should be unsafe (because its safety requirement should require that ops corresponds to the right allocator).
  • Maybe: ptr just needs to be valid for reading?
    • But if so, then it would mean that before looking up allocator-specific metadata (e.g. stored in bytes preceding the allocation) the implementation of malloc_size_of would need to somehow check if the pointer points to "currently allocated memory". And if so, then it should also be able to check if ptr is valid for reading.

The guesses above don't look quite right to me. I feel that the real safety requirements are different from the guesses above. I am probably missing an understanding of how this API is intended to work. Help please? :-)

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