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

Soundness Hole: Aliasing and Mutating static references in safe Rust #50162

Closed
CryZe opened this issue Apr 22, 2018 · 5 comments
Closed

Soundness Hole: Aliasing and Mutating static references in safe Rust #50162

CryZe opened this issue Apr 22, 2018 · 5 comments

Comments

@CryZe
Copy link
Contributor

CryZe commented Apr 22, 2018

fn static_val() -> &'static mut u8 {
    let ref mut x = 0;
    x
}

fn main() {
    let ref1 = static_val();
    let ref2 = static_val();
    *ref1 = 20;
    *ref2 = 32;
    println!("{}", ref1);
}

The variable x is a mutable reference to a u8 that is promoted to a &'static mut u8. By calling that function multiple times, you can alias the mutable reference in safe Rust.

@memoryruins
Copy link
Contributor

fn static_table() -> &'static mut [u8; 20] {
    let ref mut table = [0; 20];
    table
}

fn main() {
    let table = static_table();
    table[4] = 20;
    println!("{:?}", table);
}

The above returns different arrays of values depending on stable, beta, or release channel. Only in debug does table[4] = 20; set the index's value to 20.

@memoryruins
Copy link
Contributor

Neither examples compile before 1.21.0.

@jonas-schievink
Copy link
Contributor

looks like #46557

@memoryruins
Copy link
Contributor

It appears so. Enabling nll prevents it from compiling.

@CryZe
Copy link
Contributor Author

CryZe commented Apr 22, 2018

Closing as it seems to be a duplicate

@CryZe CryZe closed this as completed Apr 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants