Skip to content

Detect instant UB caused by creating a reference to an invalid value #1638

Open
@camelid

Description

@camelid

According to rust-lang/rust#78123 (comment) and the Reference, producing an &-reference (not sure about raw pointers) to an invalid value is instant UB:

Unlike C, Undefined Behavior is pretty limited in scope in Rust. All the core language cares about is preventing the following things:
[...]
a reference/Box that is dangling, unaligned, or points to an invalid value.

For example, if I understand correctly, this program has UB, but Miri does not report any errors:

enum Void {}

fn main() {
    let _x: &Void = unsafe { std::mem::transmute(&()) };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-validationArea: This affects enforcing the validity invariant, and related UB checkingC-enhancementCategory: a PR with an enhancement or an issue tracking an accepted enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions