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

make sure we catch UB in match place even with _ pattern #3150

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
--> $DIR/dangling_pointer_project_underscore.rs:LL:CC
--> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC
|
LL | let _ = (*p).1;
| ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: ALLOC was allocated here:
--> $DIR/dangling_pointer_project_underscore.rs:LL:CC
--> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_pointer_project_underscore.rs:LL:CC
--> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/dangling_pointer_project_underscore.rs:LL:CC
= note: inside `main` at $DIR/dangling_pointer_project_underscore_let.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Make sure we find these even with many checks disabled.
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation

fn main() {
let p = {
let b = Box::new(42);
&*b as *const i32 as *const (u8, u8, u8, u8)
};
unsafe {
let _: u8 = (*p).1; //~ ERROR: out-of-bounds pointer arithmetic
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
--> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC
|
LL | let _: u8 = (*p).1;
| ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: ALLOC was allocated here:
--> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Make sure we find these even with many checks disabled.
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation

fn main() {
let p = {
let b = Box::new(42);
&*b as *const i32 as *const (u8, u8, u8, u8)
};
unsafe {
match (*p).1 {
//~^ ERROR: out-of-bounds pointer arithmetic
_ => {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
--> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC
|
LL | match (*p).1 {
| ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: ALLOC was allocated here:
--> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/dangling_pointer_project_underscore_match.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error

17 changes: 6 additions & 11 deletions tests/pass/underscore_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use std::ptr;

fn main() {
dangling_deref_match();
union_uninhabited_match();
dangling_match();
invalid_match();
dangling_let();
invalid_let();
dangling_let_type_annotation();
invalid_let_type_annotation();
}

fn dangling_deref_match() {
fn dangling_match() {
let p = {
let b = Box::new(42);
&*b as *const i32
Expand All @@ -23,20 +23,15 @@ fn dangling_deref_match() {
}
}

fn union_uninhabited_match() {
#[derive(Copy, Clone)]
enum Void {}
fn invalid_match() {
union Uninit<T: Copy> {
value: T,
uninit: (),
}
unsafe {
let x: Uninit<Void> = Uninit { uninit: () };
let x: Uninit<bool> = Uninit { uninit: () };
match x.value {
// rustc warns about un unreachable pattern,
// but is wrong in unsafe code.
#[allow(unreachable_patterns)]
_ => println!("hi from the void!"),
_ => {}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/pass/underscore_pattern.stdout

This file was deleted.