Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do we need even more agressive validation? #508

Closed
RalfJung opened this issue Nov 3, 2018 · 5 comments
Closed

Do we need even more agressive validation? #508

RalfJung opened this issue Nov 3, 2018 · 5 comments
Labels
A-validation Area: This affects enforcing the validity invariant, and related UB checking C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement

Comments

@RalfJung
Copy link
Member

RalfJung commented Nov 3, 2018

Currently, we do not catch this:

#![feature(rustc_attrs)]
#![allow(unused_attributes)]

#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);

fn main() {
    let mut x = Some(NonZero(1));
    let xref = match x {
        Some(ref mut c) => c,
        None => panic!(),
    };
    *xref = NonZero(0);
}

In the MIR, the *xref = NonZero(0) becomes an assignment of the only field of this struct, and that field is of type i32 and hence value 0 is no problem.

I could imagine doing validation of prefixes of the path involved in an assignment, but I see no way to catch the following:

fn main() {
    let mut x = Some(unsafe { NonZero(1) }); // this one is fine
    let xref = match x {
        Some(NonZero(ref mut c)) => c,
        None => panic!(),
    };
    *xref = 0; // this one is not
}

This won't even be caught by @oli-obk's new unsafety check for constructing NonZero.

(The last example is not specific to NonZero at all; writing 2 into a &mut bool after casting it to *mut u8 is similar.)

@oli-obk
Copy link
Contributor

oli-obk commented Nov 3, 2018

aaand now I'm gonna make taking references to fields of rustc_layout_scalar_valid_range_start unsafe, too.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 3, 2018

cc @Centril ralf found "the next hole". That didn't take very long

@Centril
Copy link

Centril commented Nov 3, 2018

Dear lord. =P

@Centril
Copy link

Centril commented Nov 3, 2018

PS: I think we can treat these changes as rustc internals that is inconsequential wrt. the language team's review of exposed stable behavior (since it doesn't affect any stable behavior...).

@RalfJung RalfJung added C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement A-validation Area: This affects enforcing the validity invariant, and related UB checking labels Nov 17, 2018
@RalfJung
Copy link
Member Author

RalfJung commented Aug 2, 2019

Closing in favor of rust-lang/unsafe-code-guidelines#189: this is first and foremost a question of "what is the spec", not how to implement it.

@RalfJung RalfJung closed this as completed Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validation Area: This affects enforcing the validity invariant, and related UB checking C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement
Projects
None yet
Development

No branches or pull requests

3 participants