Skip to content

Miri preserves padding and partial initialization on copies #845

Closed
rust-lang/rust
#129778
@RalfJung

Description

@RalfJung

Miri should be able to detect that the following is UB because it prints uninitialized memory:

use std::mem;

#[repr(C)]
struct Pair(u8, u16);

fn main() { unsafe {
    let p: Pair = mem::transmute(0u32); // The copy when `Pair` is returned from `transmute` should destroy padding.
    let c = &p as *const _ as *const u8;
    println!("{}", *c.add(1)); // Print the padding byte.
} }

However, currently assignment is just implemented as an untyped memcpy, so we incorrectly preserve padding.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-interpreterArea: affects the core interpreterA-validationArea: This affects enforcing the validity invariant, and related UB checkingC-bugCategory: This is a bug.I-misses-UBImpact: makes Miri miss UB, i.e., a false negative (with default settings)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions