Skip to content

Commit

Permalink
Check for raw pointer dereference in THIR unsafeck
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed May 19, 2021
1 parent f94942d commit 27fe959
Show file tree
Hide file tree
Showing 22 changed files with 113 additions and 10 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
self.requires_unsafe(expr.span, UseOfInlineAssembly);
}
ExprKind::Deref { arg } => {
if self.thir[arg].ty.is_unsafe_ptr() {
self.requires_unsafe(expr.span, DerefOfRawPointer);
}
}
_ => {}
}

Expand Down Expand Up @@ -203,7 +208,6 @@ enum UnsafeOpKind {
UseOfMutableStatic,
#[allow(dead_code)] // FIXME
UseOfExternStatic,
#[allow(dead_code)] // FIXME
DerefOfRawPointer,
#[allow(dead_code)] // FIXME
AssignToDroppingUnionField,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/issue-45729-unsafe-in-generator.rs:5:9
--> $DIR/issue-45729-unsafe-in-generator.rs:8:9
|
LL | *(1 as *mut u32) = 42;
| ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/generator/issue-45729-unsafe-in-generator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

#![feature(generators)]

fn main() {
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/generator/issue-45729-unsafe-in-generator.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/issue-45729-unsafe-in-generator.rs:8:9
|
LL | *(1 as *mut u32) = 42;
| ^^^^^^^^^^^^^^^^ dereference of raw pointer
|
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/issue-47412.rs:11:11
--> $DIR/issue-47412.rs:14:11
|
LL | match u.void {}
| ^^^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/issue-47412.rs:17:11
--> $DIR/issue-47412.rs:21:11
|
LL | match *ptr {}
| ^^^^ dereference of raw pointer
Expand Down
6 changes: 5 additions & 1 deletion src/test/ui/issues/issue-47412.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

#[derive(Copy, Clone)]
enum Void {}

Expand All @@ -9,7 +12,8 @@ fn union_field() {
union Union { unit: (), void: Void }
let u = Union { unit: () };
match u.void {}
//~^ ERROR access to union field is unsafe
//[mir]~^ ERROR access to union field is unsafe
// FIXME(thir-unsafeck): AccessToUnionField unimplemented
}

fn raw_ptr_deref() {
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-47412.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/issue-47412.rs:21:11
|
LL | match *ptr {}
| ^^^^ dereference of raw pointer
|
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/safety-fn-body.rs:11:9
--> $DIR/safety-fn-body.rs:14:9
|
LL | *self += 1;
| ^^^^^^^^^^ dereference of raw pointer
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/traits/safety-fn-body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Check that an unsafe impl does not imply that unsafe actions are
// legal in the methods.

// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

unsafe trait UnsafeTrait : Sized {
fn foo(self) { }
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/traits/safety-fn-body.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/safety-fn-body.rs:14:9
|
LL | *self += 1;
| ^^^^^ dereference of raw pointer
|
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/issue-45087-unreachable-unsafe.rs:3:5
--> $DIR/issue-45087-unreachable-unsafe.rs:6:5
|
LL | *(1 as *mut u32) = 42;
| ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/unsafe/issue-45087-unreachable-unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

fn main() {
return;
*(1 as *mut u32) = 42;
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/unsafe/issue-45087-unreachable-unsafe.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/issue-45087-unreachable-unsafe.rs:6:5
|
LL | *(1 as *mut u32) = 42;
| ^^^^^^^^^^^^^^^^ dereference of raw pointer
|
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/unsafe-fn-assign-deref-ptr.rs:2:5
--> $DIR/unsafe-fn-assign-deref-ptr.rs:5:5
|
LL | *p = 0;
| ^^^^^^ dereference of raw pointer
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/unsafe/unsafe-fn-assign-deref-ptr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

fn f(p: *mut u8) {
*p = 0; //~ ERROR dereference of raw pointer is unsafe
return;
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/unsafe/unsafe-fn-assign-deref-ptr.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/unsafe-fn-assign-deref-ptr.rs:5:5
|
LL | *p = 0;
| ^^ dereference of raw pointer
|
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/unsafe-fn-deref-ptr.rs:2:12
--> $DIR/unsafe-fn-deref-ptr.rs:5:12
|
LL | return *p;
| ^^ dereference of raw pointer
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/unsafe/unsafe-fn-deref-ptr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

fn f(p: *const u8) -> u8 {
return *p; //~ ERROR dereference of raw pointer is unsafe
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/unsafe/unsafe-fn-deref-ptr.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/unsafe-fn-deref-ptr.rs:5:12
|
LL | return *p;
| ^^ dereference of raw pointer
|
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/unsafe-unstable-const-fn.rs:8:5
--> $DIR/unsafe-unstable-const-fn.rs:11:5
|
LL | *a == b
| ^^ dereference of raw pointer
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/unsafe/unsafe-unstable-const-fn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

#![stable(feature = "foo", since = "1.33.0")]
#![feature(staged_api)]
#![feature(const_raw_ptr_deref)]
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/unsafe/unsafe-unstable-const-fn.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/unsafe-unstable-const-fn.rs:11:5
|
LL | *a == b
| ^^ dereference of raw pointer
|
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.

0 comments on commit 27fe959

Please sign in to comment.