Check for raw pointer dereference in THIR unsafeck

This commit is contained in:
LeSeulArtichaut 2021-05-14 23:19:59 +02:00
parent f94942d842
commit 27fe959c2c
22 changed files with 113 additions and 10 deletions

View File

@ -153,6 +153,11 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => { ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
self.requires_unsafe(expr.span, UseOfInlineAssembly); self.requires_unsafe(expr.span, UseOfInlineAssembly);
} }
ExprKind::Deref { arg } => {
if self.thir[arg].ty.is_unsafe_ptr() {
self.requires_unsafe(expr.span, DerefOfRawPointer);
}
}
_ => {} _ => {}
} }
@ -203,7 +208,6 @@ enum UnsafeOpKind {
UseOfMutableStatic, UseOfMutableStatic,
#[allow(dead_code)] // FIXME #[allow(dead_code)] // FIXME
UseOfExternStatic, UseOfExternStatic,
#[allow(dead_code)] // FIXME
DerefOfRawPointer, DerefOfRawPointer,
#[allow(dead_code)] // FIXME #[allow(dead_code)] // FIXME
AssignToDroppingUnionField, AssignToDroppingUnionField,

View File

@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block 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; LL | *(1 as *mut u32) = 42;
| ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer | ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
#![feature(generators)] #![feature(generators)]
fn main() { fn main() {

View File

@ -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`.

View File

@ -1,5 +1,5 @@
error[E0133]: access to union field is unsafe and requires unsafe function or block 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 {} LL | match u.void {}
| ^^^^^^ access to union field | ^^^^^^ access to union field
@ -7,7 +7,7 @@ LL | match u.void {}
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = 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 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 {} LL | match *ptr {}
| ^^^^ dereference of raw pointer | ^^^^ dereference of raw pointer

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum Void {} enum Void {}
@ -9,7 +12,8 @@ fn union_field() {
union Union { unit: (), void: Void } union Union { unit: (), void: Void }
let u = Union { unit: () }; let u = Union { unit: () };
match u.void {} 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() { fn raw_ptr_deref() {

View File

@ -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`.

View File

@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block 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; LL | *self += 1;
| ^^^^^^^^^^ dereference of raw pointer | ^^^^^^^^^^ dereference of raw pointer

View File

@ -1,6 +1,9 @@
// Check that an unsafe impl does not imply that unsafe actions are // Check that an unsafe impl does not imply that unsafe actions are
// legal in the methods. // legal in the methods.
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
unsafe trait UnsafeTrait : Sized { unsafe trait UnsafeTrait : Sized {
fn foo(self) { } fn foo(self) { }
} }

View File

@ -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`.

View File

@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block 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; LL | *(1 as *mut u32) = 42;
| ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer | ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
fn main() { fn main() {
return; return;
*(1 as *mut u32) = 42; *(1 as *mut u32) = 42;

View File

@ -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`.

View File

@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block 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; LL | *p = 0;
| ^^^^^^ dereference of raw pointer | ^^^^^^ dereference of raw pointer

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
fn f(p: *mut u8) { fn f(p: *mut u8) {
*p = 0; //~ ERROR dereference of raw pointer is unsafe *p = 0; //~ ERROR dereference of raw pointer is unsafe
return; return;

View File

@ -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`.

View File

@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block 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; LL | return *p;
| ^^ dereference of raw pointer | ^^ dereference of raw pointer

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
fn f(p: *const u8) -> u8 { fn f(p: *const u8) -> u8 {
return *p; //~ ERROR dereference of raw pointer is unsafe return *p; //~ ERROR dereference of raw pointer is unsafe
} }

View File

@ -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`.

View File

@ -1,5 +1,5 @@
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block 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 LL | *a == b
| ^^ dereference of raw pointer | ^^ dereference of raw pointer

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
#![stable(feature = "foo", since = "1.33.0")] #![stable(feature = "foo", since = "1.33.0")]
#![feature(staged_api)] #![feature(staged_api)]
#![feature(const_raw_ptr_deref)] #![feature(const_raw_ptr_deref)]

View File

@ -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`.