Skip to content

Miri chould warn on creating reference to improper uninit #1240

@CAD97

Description

@CAD97

Creating a reference to uninit data is most likely in error if the pointee type does not support uninit, even though this is most likely valid in the opsem. Warning in this case would be helpful in catching bugs earlier.

This is most helpful for vs , since this pointer composition operation is rarer than the normal referennce-of operation.

Specifically, I suggest checking primarily core::slice::from_raw_parts[_mut] (suggest to use core::ptr::slice_from_raw_part[_mut]), as this is effectively the stable primitive for going from raw pointer to slice reference. Checking on reference-of to places behind an unsafe pointer indirection (not that were behind a safe reference already) is also useful (suggest to use &raw), but probably needs a completely different mechanism to do the checking.

I do not envision this doing full type validation. Instead, it would just check for all-uninit and whether all-uninit is valid for the pointee type. This is a lint to detect accidental exposure to uninit earlier.

Original description

[example]

let layout = Layout::array::<u8>(10).unwrap();
let ptr = alloc(layout);
slice::from_raw_parts_mut(ptr, 10);
dealloc(ptr, layout);

This would have prevented an actual issue I had where I accidentally used slice::from_raw_parts instead of ptr::slice_from_raw_parts from a version-aware import.

More generally, this is the "create reference to uninitialized memory" catch, but since these two methods have now-stable sound alternatives, it'd be nice for miri to catch incorrect usage and point at the correct raw pointer version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-interpreterArea: affects the core interpreterC-proposalCategory: a proposal for something we might want to do, or maybe not; details still being worked out

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions