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

Check for more things in THIR unsafeck #85555

Merged
merged 5 commits into from
May 21, 2021
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
77 changes: 68 additions & 9 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
use rustc_session::lint::Level;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::symbol::Symbol;
use rustc_span::Span;

use std::ops::Bound;

struct UnsafetyVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
thir: &'a Thir<'tcx>,
Expand All @@ -19,6 +22,10 @@ struct UnsafetyVisitor<'a, 'tcx> {
/// `unsafe` block, and whether it has been used.
safety_context: SafetyContext,
body_unsafety: BodyUnsafety,
/// The `#[target_feature]` attributes of the body. Used for checking
/// calls to functions with `#[target_feature]` (RFC 2396).
body_target_features: &'tcx Vec<Symbol>,
is_const: bool,
}

impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
Expand Down Expand Up @@ -148,11 +155,55 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
ExprKind::Call { fun, ty: _, args: _, from_hir_call: _, fn_span: _ } => {
if self.thir[fun].ty.fn_sig(self.tcx).unsafety() == hir::Unsafety::Unsafe {
self.requires_unsafe(expr.span, CallToUnsafeFunction);
} else if let &ty::FnDef(func_did, _) = self.thir[fun].ty.kind() {
// If the called function has target features the calling function hasn't,
// the call requires `unsafe`.
if !self
.tcx
.codegen_fn_attrs(func_did)
.target_features
.iter()
.all(|feature| self.body_target_features.contains(feature))
{
self.requires_unsafe(expr.span, CallToFunctionWith);
}
}
}
ExprKind::Deref { arg } => {
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind {
if self.tcx.is_mutable_static(def_id) {
self.requires_unsafe(expr.span, UseOfMutableStatic);
} else if self.tcx.is_foreign_item(def_id) {
self.requires_unsafe(expr.span, UseOfExternStatic);
}
} else if self.thir[arg].ty.is_unsafe_ptr() {
self.requires_unsafe(expr.span, DerefOfRawPointer);
}
}
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
self.requires_unsafe(expr.span, UseOfInlineAssembly);
}
ExprKind::Adt {
adt_def,
variant_index: _,
substs: _,
user_ty: _,
fields: _,
base: _,
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
(Bound::Unbounded, Bound::Unbounded) => {}
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
},
ExprKind::Cast { source } => {
let source = &self.thir[source];
if self.tcx.features().const_raw_ptr_to_usize_cast
&& self.is_const
&& (source.ty.is_unsafe_ptr() || source.ty.is_fn_ptr())
&& expr.ty.is_integral()
{
self.requires_unsafe(expr.span, CastOfPointerToInt);
}
}
_ => {}
}

Expand Down Expand Up @@ -195,15 +246,10 @@ impl BodyUnsafety {
enum UnsafeOpKind {
CallToUnsafeFunction,
UseOfInlineAssembly,
#[allow(dead_code)] // FIXME
InitializingTypeWith,
#[allow(dead_code)] // FIXME
CastOfPointerToInt,
#[allow(dead_code)] // FIXME
UseOfMutableStatic,
#[allow(dead_code)] // FIXME
UseOfExternStatic,
#[allow(dead_code)] // FIXME
DerefOfRawPointer,
#[allow(dead_code)] // FIXME
AssignToDroppingUnionField,
Expand All @@ -213,7 +259,6 @@ enum UnsafeOpKind {
MutationOfLayoutConstrainedField,
#[allow(dead_code)] // FIXME
BorrowOfLayoutConstrainedField,
#[allow(dead_code)] // FIXME
CallToFunctionWith,
}

Expand Down Expand Up @@ -287,6 +332,7 @@ pub fn check_unsafety<'tcx>(
tcx: TyCtxt<'tcx>,
thir: &Thir<'tcx>,
expr: ExprId,
def_id: LocalDefId,
hir_id: hir::HirId,
) {
let body_unsafety = tcx.hir().fn_sig_by_hir_id(hir_id).map_or(BodyUnsafety::Safe, |fn_sig| {
Expand All @@ -296,10 +342,23 @@ pub fn check_unsafety<'tcx>(
BodyUnsafety::Safe
}
});
let body_target_features = &tcx.codegen_fn_attrs(def_id).target_features;
let safety_context =
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
let mut visitor =
UnsafetyVisitor { tcx, thir, safety_context, hir_context: hir_id, body_unsafety };
let is_const = match tcx.hir().body_owner_kind(hir_id) {
hir::BodyOwnerKind::Closure => false,
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def_id.to_def_id()),
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => true,
};
let mut visitor = UnsafetyVisitor {
tcx,
thir,
safety_context,
hir_context: hir_id,
body_unsafety,
body_target_features,
is_const,
};
visitor.visit_expr(&thir[expr]);
}

Expand All @@ -311,7 +370,7 @@ crate fn thir_check_unsafety_inner<'tcx>(
let body_id = tcx.hir().body_owned_by(hir_id);
let body = tcx.hir().body(body_id);
let (thir, expr) = cx::build_thir(tcx, def, &body.value);
check_unsafety(tcx, &thir, expr, hir_id);
check_unsafety(tcx, &thir, expr, def.did, hir_id);
}

crate fn thir_check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/cast/cast-ptr-to-int-const.mir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
--> $DIR/cast-ptr-to-int-const.rs:10:9
|
LL | &Y as *const u32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
|
= note: casting pointers to integers in constants

error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
--> $DIR/cast-ptr-to-int-const.rs:17:5
|
LL | &0 as *const i32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
|
= note: casting pointers to integers in constants

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0133`.
20 changes: 7 additions & 13 deletions src/test/ui/cast/cast-ptr-to-int-const.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
// gate-test-const_raw_ptr_to_usize_cast
// revisions: with_feature without_feature
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

#![cfg_attr(with_feature, feature(const_raw_ptr_to_usize_cast))]
#![feature(const_raw_ptr_to_usize_cast)]

fn main() {
const X: usize = unsafe {
main as usize //[without_feature]~ ERROR casting pointers to integers in constants is unstable
};
const Y: u32 = 0;
const Z: usize = unsafe {
&Y as *const u32 as usize //[without_feature]~ ERROR is unstable
};
// Cast in `const` without `unsafe` block
const SAFE: usize = {
&Y as *const u32 as usize //[without_feature]~ ERROR is unstable
//[with_feature]~^ ERROR cast of pointer to int is unsafe and requires unsafe
&Y as *const u32 as usize
//~^ ERROR cast of pointer to int is unsafe and requires unsafe
};
}

// Cast in `const fn` without `unsafe` block
const fn test() -> usize {
&0 as *const i32 as usize //[without_feature]~ ERROR is unstable
//[with_feature]~^ ERROR cast of pointer to int is unsafe and requires unsafe
&0 as *const i32 as usize
//~^ ERROR cast of pointer to int is unsafe and requires unsafe
}
19 changes: 19 additions & 0 deletions src/test/ui/cast/cast-ptr-to-int-const.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
--> $DIR/cast-ptr-to-int-const.rs:10:9
|
LL | &Y as *const u32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
|
= note: casting pointers to integers in constants

error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
--> $DIR/cast-ptr-to-int-const.rs:17:5
|
LL | &0 as *const i32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
|
= note: casting pointers to integers in constants

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0133`.
13 changes: 13 additions & 0 deletions src/test/ui/cast/feature-gate-const_raw_ptr_to_usize_cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
const X: usize = unsafe {
main as usize //~ ERROR casting pointers to integers in constants is unstable
};
const Y: u32 = 0;
const Z: usize = unsafe {
&Y as *const u32 as usize //~ ERROR is unstable
};
}

const fn test() -> usize {
&0 as *const i32 as usize //~ ERROR is unstable
}
30 changes: 30 additions & 0 deletions src/test/ui/cast/feature-gate-const_raw_ptr_to_usize_cast.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0658]: casting pointers to integers in constants is unstable
--> $DIR/feature-gate-const_raw_ptr_to_usize_cast.rs:3:9
|
LL | main as usize
| ^^^^^^^^^^^^^
|
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable

error[E0658]: casting pointers to integers in constants is unstable
--> $DIR/feature-gate-const_raw_ptr_to_usize_cast.rs:7:9
|
LL | &Y as *const u32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable

error[E0658]: casting pointers to integers in constant functions is unstable
--> $DIR/feature-gate-const_raw_ptr_to_usize_cast.rs:12:5
|
LL | &0 as *const i32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
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,5 +1,5 @@
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-28575.rs:8:5
--> $DIR/issue-28575.rs:11:5
|
LL | FOO()
| ^^^ use of extern static
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/intrinsics/issue-28575.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(intrinsics)]

extern "C" {
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/intrinsics/issue-28575.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-28575.rs:11:5
|
LL | FOO()
| ^^^ use of extern static
|
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause 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]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-14227.rs:4:21
--> $DIR/issue-14227.rs:7:21
|
LL | static CRASH: u32 = symbol;
| ^^^^^^ use of extern static
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/issues/issue-14227.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

extern "C" {
pub static symbol: u32;
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-14227.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-14227.rs:7:21
|
LL | static CRASH: u32 = symbol;
| ^^^^^^ use of extern static
|
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause 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,11 +1,11 @@
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-16538.rs:11:27
--> $DIR/issue-16538.rs:14:27
|
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: `*const usize` cannot be shared between threads safely
--> $DIR/issue-16538.rs:11:1
--> $DIR/issue-16538.rs:14:1
|
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const usize` cannot be shared between threads safely
Expand All @@ -14,7 +14,7 @@ LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
= note: shared static variables must have a type that implements `Sync`

error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-16538.rs:11:34
--> $DIR/issue-16538.rs:14:34
|
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
| ^^^^ use of extern static
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/issues/issue-16538.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

mod Y {
pub type X = usize;
extern "C" {
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/issues/issue-16538.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-16538.rs:14:27
|
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: `*const usize` cannot be shared between threads safely
--> $DIR/issue-16538.rs:14:1
|
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const usize` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `*const usize`
= note: shared static variables must have a type that implements `Sync`

error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-16538.rs:14:34
|
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
| ^^^^ use of extern static
|
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0015, E0133, E0277.
For more information about an error, try `rustc --explain E0015`.
Loading