2017-09-19 16:20:02 +03:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2017-09-19 16:20:02 +03:00
|
|
|
|
2018-06-13 16:44:43 +03:00
|
|
|
use rustc::ty::query::Providers;
|
2018-04-18 11:33:52 +01:00
|
|
|
use rustc::ty::{self, TyCtxt};
|
2018-12-07 18:26:46 +01:00
|
|
|
use rustc::ty::cast::CastTy;
|
2017-09-19 16:20:02 +03:00
|
|
|
use rustc::hir;
|
2018-08-25 15:56:16 +01:00
|
|
|
use rustc::hir::Node;
|
2017-09-19 16:20:02 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-11-16 20:12:23 +02:00
|
|
|
use rustc::lint::builtin::{SAFE_EXTERN_STATICS, SAFE_PACKED_BORROWS, UNUSED_UNSAFE};
|
2017-09-19 16:20:02 +03:00
|
|
|
use rustc::mir::*;
|
2018-10-26 13:22:45 +02:00
|
|
|
use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext};
|
2017-09-19 16:20:02 +03:00
|
|
|
|
2017-11-22 14:39:32 +01:00
|
|
|
use syntax::symbol::Symbol;
|
2017-09-19 16:20:02 +03:00
|
|
|
|
2018-11-03 13:03:05 +01:00
|
|
|
use std::ops::Bound;
|
|
|
|
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::util;
|
2017-09-19 16:20:02 +03:00
|
|
|
|
|
|
|
pub struct UnsafetyChecker<'a, 'tcx: 'a> {
|
|
|
|
mir: &'a Mir<'tcx>,
|
2018-12-07 18:26:46 +01:00
|
|
|
const_context: bool,
|
2018-08-22 15:56:37 +02:00
|
|
|
min_const_fn: bool,
|
2018-05-28 17:37:48 +03:00
|
|
|
source_scope_local_data: &'a IndexVec<SourceScope, SourceScopeLocalData>,
|
2017-09-19 16:20:02 +03:00
|
|
|
violations: Vec<UnsafetyViolation>,
|
|
|
|
source_info: SourceInfo,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Mark an `unsafe` block as used, so we don't lint it.
|
2019-02-22 15:48:14 +01:00
|
|
|
used_unsafe: FxHashSet<hir::HirId>,
|
|
|
|
inherited_blocks: Vec<(hir::HirId, bool)>,
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> UnsafetyChecker<'a, 'tcx> {
|
2018-08-22 15:56:37 +02:00
|
|
|
fn new(
|
2018-12-07 18:26:46 +01:00
|
|
|
const_context: bool,
|
2018-08-22 15:56:37 +02:00
|
|
|
min_const_fn: bool,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
source_scope_local_data: &'a IndexVec<SourceScope, SourceScopeLocalData>,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> Self {
|
2018-12-07 18:26:46 +01:00
|
|
|
// sanity check
|
|
|
|
if min_const_fn {
|
|
|
|
assert!(const_context);
|
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
Self {
|
|
|
|
mir,
|
2018-12-07 18:26:46 +01:00
|
|
|
const_context,
|
2018-08-22 15:56:37 +02:00
|
|
|
min_const_fn,
|
2018-05-28 17:37:48 +03:00
|
|
|
source_scope_local_data,
|
2017-09-19 16:20:02 +03:00
|
|
|
violations: vec![],
|
|
|
|
source_info: SourceInfo {
|
|
|
|
span: mir.span,
|
2018-05-28 14:16:09 +03:00
|
|
|
scope: OUTERMOST_SOURCE_SCOPE
|
2017-09-19 16:20:02 +03:00
|
|
|
},
|
|
|
|
tcx,
|
|
|
|
param_env,
|
2018-10-16 16:57:53 +02:00
|
|
|
used_unsafe: Default::default(),
|
2017-11-05 20:04:18 +02:00
|
|
|
inherited_blocks: vec![],
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
|
|
|
|
fn visit_terminator(&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
terminator: &Terminator<'tcx>,
|
|
|
|
location: Location)
|
|
|
|
{
|
|
|
|
self.source_info = terminator.source_info;
|
|
|
|
match terminator.kind {
|
|
|
|
TerminatorKind::Goto { .. } |
|
|
|
|
TerminatorKind::SwitchInt { .. } |
|
|
|
|
TerminatorKind::Drop { .. } |
|
|
|
|
TerminatorKind::Yield { .. } |
|
|
|
|
TerminatorKind::Assert { .. } |
|
|
|
|
TerminatorKind::DropAndReplace { .. } |
|
|
|
|
TerminatorKind::GeneratorDrop |
|
|
|
|
TerminatorKind::Resume |
|
2017-12-19 01:17:16 +01:00
|
|
|
TerminatorKind::Abort |
|
2017-09-19 16:20:02 +03:00
|
|
|
TerminatorKind::Return |
|
2017-10-13 16:36:15 +03:00
|
|
|
TerminatorKind::Unreachable |
|
2018-01-25 01:45:45 -05:00
|
|
|
TerminatorKind::FalseEdges { .. } |
|
|
|
|
TerminatorKind::FalseUnwind { .. } => {
|
2017-11-05 16:48:22 +02:00
|
|
|
// safe (at least as emitted during MIR construction)
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TerminatorKind::Call { ref func, .. } => {
|
|
|
|
let func_ty = func.ty(self.mir, self.tcx);
|
|
|
|
let sig = func_ty.fn_sig(self.tcx);
|
|
|
|
if let hir::Unsafety::Unsafe = sig.unsafety() {
|
2018-07-10 10:52:05 +02:00
|
|
|
self.require_unsafe("call to unsafe function",
|
|
|
|
"consult the function's documentation for information on how to avoid \
|
2018-12-22 18:06:20 +01:00
|
|
|
undefined behavior", UnsafetyViolationKind::GeneralAndConstFn)
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.super_terminator(block, terminator, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
statement: &Statement<'tcx>,
|
|
|
|
location: Location)
|
|
|
|
{
|
|
|
|
self.source_info = statement.source_info;
|
|
|
|
match statement.kind {
|
|
|
|
StatementKind::Assign(..) |
|
2018-09-14 21:05:31 +02:00
|
|
|
StatementKind::FakeRead(..) |
|
2017-09-19 16:20:02 +03:00
|
|
|
StatementKind::SetDiscriminant { .. } |
|
|
|
|
StatementKind::StorageLive(..) |
|
|
|
|
StatementKind::StorageDead(..) |
|
2018-10-24 11:47:17 +02:00
|
|
|
StatementKind::Retag { .. } |
|
2018-08-31 18:59:35 -04:00
|
|
|
StatementKind::AscribeUserType(..) |
|
2017-09-19 16:20:02 +03:00
|
|
|
StatementKind::Nop => {
|
|
|
|
// safe (at least as emitted during MIR construction)
|
|
|
|
}
|
|
|
|
|
|
|
|
StatementKind::InlineAsm { .. } => {
|
2018-07-10 10:52:05 +02:00
|
|
|
self.require_unsafe("use of inline assembly",
|
2018-11-03 00:22:12 +01:00
|
|
|
"inline assembly is entirely unchecked and can cause undefined behavior",
|
|
|
|
UnsafetyViolationKind::General)
|
2017-09-19 16:20:02 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
self.super_statement(block, statement, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_rvalue(&mut self,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
location: Location)
|
|
|
|
{
|
2018-12-07 18:26:46 +01:00
|
|
|
match rvalue {
|
|
|
|
Rvalue::Aggregate(box ref aggregate, _) => {
|
|
|
|
match aggregate {
|
|
|
|
&AggregateKind::Array(..) |
|
|
|
|
&AggregateKind::Tuple => {}
|
|
|
|
&AggregateKind::Adt(ref def, ..) => {
|
|
|
|
match self.tcx.layout_scalar_valid_range(def.did) {
|
|
|
|
(Bound::Unbounded, Bound::Unbounded) => {},
|
|
|
|
_ => self.require_unsafe(
|
|
|
|
"initializing type with `rustc_layout_scalar_valid_range` attr",
|
|
|
|
"initializing a layout restricted type's field with a value \
|
|
|
|
outside the valid range is undefined behavior",
|
|
|
|
UnsafetyViolationKind::GeneralAndConstFn,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&AggregateKind::Closure(def_id, _) |
|
|
|
|
&AggregateKind::Generator(def_id, _, _) => {
|
|
|
|
let UnsafetyCheckResult {
|
|
|
|
violations, unsafe_blocks
|
|
|
|
} = self.tcx.unsafety_check_result(def_id);
|
|
|
|
self.register_violations(&violations, &unsafe_blocks);
|
2018-11-03 13:03:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 18:26:46 +01:00
|
|
|
},
|
|
|
|
// casting pointers to ints is unsafe in const fn because the const evaluator cannot
|
|
|
|
// possibly know what the result of various operations like `address / 2` would be
|
|
|
|
// pointers during const evaluation have no integral address, only an abstract one
|
|
|
|
Rvalue::Cast(CastKind::Misc, ref operand, cast_ty)
|
|
|
|
if self.const_context && self.tcx.features().const_raw_ptr_to_usize_cast => {
|
|
|
|
let operand_ty = operand.ty(self.mir, self.tcx);
|
|
|
|
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
|
|
|
|
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
|
|
|
|
match (cast_in, cast_out) {
|
|
|
|
(CastTy::Ptr(_), CastTy::Int(_)) |
|
|
|
|
(CastTy::FnPtr, CastTy::Int(_)) => {
|
|
|
|
self.register_violations(&[UnsafetyViolation {
|
|
|
|
source_info: self.source_info,
|
|
|
|
description: Symbol::intern("cast of pointer to int").as_interned_str(),
|
|
|
|
details: Symbol::intern("casting pointers to integers in constants")
|
|
|
|
.as_interned_str(),
|
|
|
|
kind: UnsafetyViolationKind::General,
|
|
|
|
}], &[]);
|
|
|
|
},
|
|
|
|
_ => {},
|
2017-11-05 16:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 18:26:46 +01:00
|
|
|
// raw pointer and fn pointer operations are unsafe as it is not clear whether one
|
|
|
|
// pointer would be "less" or "equal" to another, because we cannot know where llvm
|
|
|
|
// or the linker will place various statics in memory. Without this information the
|
|
|
|
// result of a comparison of addresses would differ between runtime and compile-time.
|
|
|
|
Rvalue::BinaryOp(_, ref lhs, _)
|
|
|
|
if self.const_context && self.tcx.features().const_compare_raw_pointers => {
|
|
|
|
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(self.mir, self.tcx).sty {
|
|
|
|
self.register_violations(&[UnsafetyViolation {
|
|
|
|
source_info: self.source_info,
|
|
|
|
description: Symbol::intern("pointer operation").as_interned_str(),
|
|
|
|
details: Symbol::intern("operations on pointers in constants")
|
|
|
|
.as_interned_str(),
|
|
|
|
kind: UnsafetyViolationKind::General,
|
|
|
|
}], &[]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {},
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
self.super_rvalue(rvalue, location);
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
fn visit_place(&mut self,
|
|
|
|
place: &Place<'tcx>,
|
2017-12-01 14:31:47 +02:00
|
|
|
context: PlaceContext<'tcx>,
|
2017-09-19 16:20:02 +03:00
|
|
|
location: Location) {
|
2017-12-01 14:39:51 +02:00
|
|
|
match place {
|
2017-12-01 14:31:47 +02:00
|
|
|
&Place::Projection(box Projection {
|
2017-09-19 16:20:02 +03:00
|
|
|
ref base, ref elem
|
|
|
|
}) => {
|
2018-11-03 15:21:44 +01:00
|
|
|
if context.is_borrow() {
|
|
|
|
if util::is_disaligned(self.tcx, self.mir, self.param_env, place) {
|
|
|
|
let source_info = self.source_info;
|
|
|
|
let lint_root =
|
|
|
|
self.source_scope_local_data[source_info.scope].lint_root;
|
|
|
|
self.register_violations(&[UnsafetyViolation {
|
|
|
|
source_info,
|
|
|
|
description: Symbol::intern("borrow of packed field").as_interned_str(),
|
|
|
|
details:
|
|
|
|
Symbol::intern("fields of packed structs might be misaligned: \
|
|
|
|
dereferencing a misaligned pointer or even just \
|
|
|
|
creating a misaligned reference is undefined \
|
|
|
|
behavior")
|
|
|
|
.as_interned_str(),
|
|
|
|
kind: UnsafetyViolationKind::BorrowPacked(lint_root)
|
|
|
|
}], &[]);
|
|
|
|
}
|
2018-11-05 13:26:07 +01:00
|
|
|
}
|
|
|
|
let is_borrow_of_interior_mut = context.is_borrow() && !base
|
|
|
|
.ty(self.mir, self.tcx)
|
2019-03-28 19:08:31 -07:00
|
|
|
.ty
|
2018-11-05 13:26:07 +01:00
|
|
|
.is_freeze(self.tcx, self.param_env, self.source_info.span);
|
2018-11-05 13:29:17 +01:00
|
|
|
// prevent
|
|
|
|
// * `&mut x.field`
|
|
|
|
// * `x.field = y;`
|
|
|
|
// * `&x.field` if `field`'s type has interior mutability
|
|
|
|
// because either of these would allow modifying the layout constrained field and
|
|
|
|
// insert values that violate the layout constraints.
|
2018-11-05 13:26:07 +01:00
|
|
|
if context.is_mutating_use() || is_borrow_of_interior_mut {
|
|
|
|
self.check_mut_borrowing_layout_constrained_field(
|
|
|
|
place, context.is_mutating_use(),
|
|
|
|
);
|
2018-11-03 15:21:44 +01:00
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
let old_source_info = self.source_info;
|
2019-02-22 05:24:03 +01:00
|
|
|
if let &Place::Base(PlaceBase::Local(local)) = base {
|
2017-09-19 16:20:02 +03:00
|
|
|
if self.mir.local_decls[local].internal {
|
|
|
|
// Internal locals are used in the `move_val_init` desugaring.
|
|
|
|
// We want to check unsafety against the source info of the
|
|
|
|
// desugaring, rather than the source info of the RHS.
|
2018-05-29 21:31:33 +03:00
|
|
|
self.source_info = self.mir.local_decls[local].source_info;
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 19:08:31 -07:00
|
|
|
let base_ty = base.ty(self.mir, self.tcx).ty;
|
2017-09-19 16:20:02 +03:00
|
|
|
match base_ty.sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::RawPtr(..) => {
|
2018-07-10 10:52:05 +02:00
|
|
|
self.require_unsafe("dereference of raw pointer",
|
|
|
|
"raw pointers may be NULL, dangling or unaligned; they can violate \
|
|
|
|
aliasing rules and cause data races: all of these are undefined \
|
2018-11-03 00:22:12 +01:00
|
|
|
behavior", UnsafetyViolationKind::General)
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Adt(adt, _) => {
|
2017-09-27 14:23:45 +03:00
|
|
|
if adt.is_union() {
|
2018-10-26 13:22:45 +02:00
|
|
|
if context == PlaceContext::MutatingUse(MutatingUseContext::Store) ||
|
|
|
|
context == PlaceContext::MutatingUse(MutatingUseContext::Drop) ||
|
|
|
|
context == PlaceContext::MutatingUse(
|
|
|
|
MutatingUseContext::AsmOutput
|
|
|
|
)
|
2017-09-27 14:23:45 +03:00
|
|
|
{
|
|
|
|
let elem_ty = match elem {
|
|
|
|
&ProjectionElem::Field(_, ty) => ty,
|
|
|
|
_ => span_bug!(
|
|
|
|
self.source_info.span,
|
|
|
|
"non-field projection {:?} from union?",
|
2017-12-01 14:39:51 +02:00
|
|
|
place)
|
2017-09-27 14:23:45 +03:00
|
|
|
};
|
2018-11-20 11:59:06 -05:00
|
|
|
if !elem_ty.is_copy_modulo_regions(
|
|
|
|
self.tcx,
|
|
|
|
self.param_env,
|
|
|
|
self.source_info.span,
|
|
|
|
) {
|
2017-09-27 14:23:45 +03:00
|
|
|
self.require_unsafe(
|
2018-07-10 10:52:05 +02:00
|
|
|
"assignment to non-`Copy` union field",
|
2018-07-11 11:58:10 +02:00
|
|
|
"the previous content of the field will be dropped, which \
|
|
|
|
causes undefined behavior if the field was not properly \
|
2018-11-03 00:22:12 +01:00
|
|
|
initialized", UnsafetyViolationKind::General)
|
2017-09-27 14:23:45 +03:00
|
|
|
} else {
|
|
|
|
// write to non-move union, safe
|
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
} else {
|
2018-07-10 10:52:05 +02:00
|
|
|
self.require_unsafe("access to union field",
|
|
|
|
"the field may not be properly initialized: using \
|
2018-11-03 00:22:12 +01:00
|
|
|
uninitialized data will cause undefined behavior",
|
|
|
|
UnsafetyViolationKind::General)
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
2017-09-27 14:23:45 +03:00
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
self.source_info = old_source_info;
|
|
|
|
}
|
2019-02-22 05:24:03 +01:00
|
|
|
&Place::Base(PlaceBase::Local(..)) => {
|
2017-09-19 16:20:02 +03:00
|
|
|
// locals are safe
|
|
|
|
}
|
2019-03-23 18:29:02 +05:30
|
|
|
&Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. })) => {
|
|
|
|
bug!("unsafety checking should happen before promotion")
|
|
|
|
}
|
|
|
|
&Place::Base(
|
|
|
|
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })
|
|
|
|
) => {
|
2019-04-21 14:41:51 +03:00
|
|
|
if self.tcx.is_mutable_static(def_id) {
|
2019-03-17 11:12:39 +05:30
|
|
|
self.require_unsafe("use of mutable static",
|
|
|
|
"mutable statics can be mutated by multiple threads: aliasing violations \
|
|
|
|
or data races will cause undefined behavior",
|
|
|
|
UnsafetyViolationKind::General);
|
|
|
|
} else if self.tcx.is_foreign_item(def_id) {
|
|
|
|
let source_info = self.source_info;
|
|
|
|
let lint_root =
|
|
|
|
self.source_scope_local_data[source_info.scope].lint_root;
|
|
|
|
self.register_violations(&[UnsafetyViolation {
|
|
|
|
source_info,
|
|
|
|
description: Symbol::intern("use of extern static").as_interned_str(),
|
|
|
|
details:
|
|
|
|
Symbol::intern("extern statics are not controlled by the Rust type \
|
|
|
|
system: invalid data, aliasing violations or data \
|
|
|
|
races will cause undefined behavior")
|
|
|
|
.as_interned_str(),
|
|
|
|
kind: UnsafetyViolationKind::ExternStatic(lint_root)
|
|
|
|
}], &[]);
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-27 14:23:45 +03:00
|
|
|
};
|
2017-12-01 14:39:51 +02:00
|
|
|
self.super_place(place, context, location);
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
|
2018-11-03 00:22:12 +01:00
|
|
|
fn require_unsafe(
|
|
|
|
&mut self,
|
|
|
|
description: &'static str,
|
|
|
|
details: &'static str,
|
|
|
|
kind: UnsafetyViolationKind,
|
|
|
|
) {
|
2017-09-19 16:20:02 +03:00
|
|
|
let source_info = self.source_info;
|
|
|
|
self.register_violations(&[UnsafetyViolation {
|
2017-11-22 14:39:32 +01:00
|
|
|
source_info,
|
2018-04-11 23:02:41 +02:00
|
|
|
description: Symbol::intern(description).as_interned_str(),
|
2018-07-10 10:52:05 +02:00
|
|
|
details: Symbol::intern(details).as_interned_str(),
|
2018-11-03 00:22:12 +01:00
|
|
|
kind,
|
2017-11-05 20:04:18 +02:00
|
|
|
}], &[]);
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
2017-11-05 20:04:18 +02:00
|
|
|
fn register_violations(&mut self,
|
|
|
|
violations: &[UnsafetyViolation],
|
2019-02-22 15:48:14 +01:00
|
|
|
unsafe_blocks: &[(hir::HirId, bool)]) {
|
2018-11-03 00:22:12 +01:00
|
|
|
let safety = self.source_scope_local_data[self.source_info.scope].safety;
|
2018-12-11 09:07:03 +01:00
|
|
|
let within_unsafe = match safety {
|
2018-11-03 00:22:12 +01:00
|
|
|
// `unsafe` blocks are required in safe code
|
2018-12-11 09:07:03 +01:00
|
|
|
Safety::Safe => {
|
2017-09-19 16:20:02 +03:00
|
|
|
for violation in violations {
|
2018-11-03 00:22:12 +01:00
|
|
|
let mut violation = violation.clone();
|
2018-11-05 18:06:26 +01:00
|
|
|
match violation.kind {
|
|
|
|
UnsafetyViolationKind::GeneralAndConstFn |
|
|
|
|
UnsafetyViolationKind::General => {},
|
|
|
|
UnsafetyViolationKind::BorrowPacked(_) |
|
|
|
|
UnsafetyViolationKind::ExternStatic(_) => if self.min_const_fn {
|
|
|
|
// const fns don't need to be backwards compatible and can
|
|
|
|
// emit these violations as a hard error instead of a backwards
|
|
|
|
// compat lint
|
|
|
|
violation.kind = UnsafetyViolationKind::General;
|
|
|
|
},
|
2018-11-03 00:22:12 +01:00
|
|
|
}
|
|
|
|
if !self.violations.contains(&violation) {
|
|
|
|
self.violations.push(violation)
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-05 20:04:18 +02:00
|
|
|
false
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
2018-12-11 09:07:03 +01:00
|
|
|
// `unsafe` function bodies allow unsafe without additional unsafe blocks
|
|
|
|
Safety::BuiltinUnsafe | Safety::FnUnsafe => true,
|
2019-02-22 15:48:14 +01:00
|
|
|
Safety::ExplicitUnsafe(hir_id) => {
|
2018-11-03 11:09:52 +01:00
|
|
|
// mark unsafe block as used if there are any unsafe operations inside
|
2017-09-19 16:20:02 +03:00
|
|
|
if !violations.is_empty() {
|
2019-02-22 15:48:14 +01:00
|
|
|
self.used_unsafe.insert(hir_id);
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
2018-11-03 00:22:12 +01:00
|
|
|
// only some unsafety is allowed in const fn
|
|
|
|
if self.min_const_fn {
|
|
|
|
for violation in violations {
|
|
|
|
match violation.kind {
|
2018-11-05 18:06:26 +01:00
|
|
|
// these unsafe things are stable in const fn
|
|
|
|
UnsafetyViolationKind::GeneralAndConstFn => {},
|
2018-11-30 18:18:42 +01:00
|
|
|
// these things are forbidden in const fns
|
2018-11-05 18:06:26 +01:00
|
|
|
UnsafetyViolationKind::General |
|
|
|
|
UnsafetyViolationKind::BorrowPacked(_) |
|
|
|
|
UnsafetyViolationKind::ExternStatic(_) => {
|
2018-11-03 00:22:12 +01:00
|
|
|
let mut violation = violation.clone();
|
2018-11-05 18:06:26 +01:00
|
|
|
// const fns don't need to be backwards compatible and can
|
|
|
|
// emit these violations as a hard error instead of a backwards
|
|
|
|
// compat lint
|
|
|
|
violation.kind = UnsafetyViolationKind::General;
|
2018-11-03 00:22:12 +01:00
|
|
|
if !self.violations.contains(&violation) {
|
|
|
|
self.violations.push(violation)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-05 20:04:18 +02:00
|
|
|
true
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
2017-11-05 20:04:18 +02:00
|
|
|
};
|
2019-02-22 15:48:14 +01:00
|
|
|
self.inherited_blocks.extend(unsafe_blocks.iter().map(|&(hir_id, is_used)| {
|
|
|
|
(hir_id, is_used && !within_unsafe)
|
2017-11-05 20:04:18 +02:00
|
|
|
}));
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
2018-11-03 16:30:05 +01:00
|
|
|
fn check_mut_borrowing_layout_constrained_field(
|
|
|
|
&mut self,
|
|
|
|
mut place: &Place<'tcx>,
|
2018-11-05 13:26:07 +01:00
|
|
|
is_mut_use: bool,
|
2018-11-03 16:30:05 +01:00
|
|
|
) {
|
|
|
|
while let &Place::Projection(box Projection {
|
|
|
|
ref base, ref elem
|
|
|
|
}) = place {
|
|
|
|
match *elem {
|
|
|
|
ProjectionElem::Field(..) => {
|
2019-03-28 19:08:31 -07:00
|
|
|
let ty = base.ty(&self.mir.local_decls, self.tcx).ty;
|
2018-11-03 16:30:05 +01:00
|
|
|
match ty.sty {
|
|
|
|
ty::Adt(def, _) => match self.tcx.layout_scalar_valid_range(def.did) {
|
|
|
|
(Bound::Unbounded, Bound::Unbounded) => {},
|
|
|
|
_ => {
|
2018-11-05 13:26:07 +01:00
|
|
|
let (description, details) = if is_mut_use {
|
|
|
|
(
|
|
|
|
"mutation of layout constrained field",
|
|
|
|
"mutating layout constrained fields cannot statically be \
|
|
|
|
checked for valid values",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
"borrow of layout constrained field with interior \
|
|
|
|
mutability",
|
|
|
|
"references to fields of layout constrained fields \
|
|
|
|
lose the constraints. Coupled with interior mutability, \
|
|
|
|
the field can be changed to invalid values",
|
|
|
|
)
|
|
|
|
};
|
2018-11-03 16:30:05 +01:00
|
|
|
let source_info = self.source_info;
|
|
|
|
self.register_violations(&[UnsafetyViolation {
|
|
|
|
source_info,
|
2018-11-05 13:26:07 +01:00
|
|
|
description: Symbol::intern(description).as_interned_str(),
|
|
|
|
details: Symbol::intern(details).as_interned_str(),
|
2018-11-05 18:06:26 +01:00
|
|
|
kind: UnsafetyViolationKind::GeneralAndConstFn,
|
2018-11-03 16:30:05 +01:00
|
|
|
}], &[]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
place = base;
|
|
|
|
}
|
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 06:28:15 +09:00
|
|
|
pub(crate) fn provide(providers: &mut Providers<'_>) {
|
2017-09-19 16:20:02 +03:00
|
|
|
*providers = Providers {
|
2017-11-05 20:04:18 +02:00
|
|
|
unsafety_check_result,
|
2017-11-26 19:01:19 +02:00
|
|
|
unsafe_derive_on_repr_packed,
|
2017-09-19 16:20:02 +03:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-05 20:04:18 +02:00
|
|
|
struct UnusedUnsafeVisitor<'a> {
|
2019-02-22 15:48:14 +01:00
|
|
|
used_unsafe: &'a FxHashSet<hir::HirId>,
|
|
|
|
unsafe_blocks: &'a mut Vec<(hir::HirId, bool)>,
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
2017-11-05 20:04:18 +02:00
|
|
|
impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'a> {
|
2017-09-19 16:20:02 +03:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) ->
|
|
|
|
hir::intravisit::NestedVisitorMap<'this, 'tcx>
|
|
|
|
{
|
|
|
|
hir::intravisit::NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_block(&mut self, block: &'tcx hir::Block) {
|
|
|
|
hir::intravisit::walk_block(self, block);
|
|
|
|
|
|
|
|
if let hir::UnsafeBlock(hir::UserProvided) = block.rules {
|
2019-02-22 15:48:14 +01:00
|
|
|
self.unsafe_blocks.push((block.hir_id, self.used_unsafe.contains(&block.hir_id)));
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_unused_unsafe<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
def_id: DefId,
|
2019-02-22 15:48:14 +01:00
|
|
|
used_unsafe: &FxHashSet<hir::HirId>,
|
|
|
|
unsafe_blocks: &'a mut Vec<(hir::HirId, bool)>)
|
2017-09-19 16:20:02 +03:00
|
|
|
{
|
|
|
|
let body_id =
|
2019-02-22 15:48:14 +01:00
|
|
|
tcx.hir().as_local_hir_id(def_id).and_then(|hir_id| {
|
|
|
|
tcx.hir().maybe_body_owned_by_by_hir_id(hir_id)
|
2017-09-19 16:20:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
let body_id = match body_id {
|
|
|
|
Some(body) => body,
|
|
|
|
None => {
|
|
|
|
debug!("check_unused_unsafe({:?}) - no body found", def_id);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
};
|
2018-12-04 13:45:36 +01:00
|
|
|
let body = tcx.hir().body(body_id);
|
2017-09-19 16:20:02 +03:00
|
|
|
debug!("check_unused_unsafe({:?}, body={:?}, used_unsafe={:?})",
|
|
|
|
def_id, body, used_unsafe);
|
|
|
|
|
2017-11-05 20:04:18 +02:00
|
|
|
let mut visitor = UnusedUnsafeVisitor { used_unsafe, unsafe_blocks };
|
|
|
|
hir::intravisit::Visitor::visit_body(&mut visitor, body);
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
2017-11-05 20:04:18 +02:00
|
|
|
fn unsafety_check_result<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
|
|
|
|
-> UnsafetyCheckResult
|
2017-09-19 16:20:02 +03:00
|
|
|
{
|
|
|
|
debug!("unsafety_violations({:?})", def_id);
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., this borrow is valid because all the consumers of
|
2017-11-05 18:09:39 +02:00
|
|
|
// `mir_built` force this.
|
|
|
|
let mir = &tcx.mir_built(def_id).borrow();
|
2017-09-19 16:20:02 +03:00
|
|
|
|
2018-05-28 17:37:48 +03:00
|
|
|
let source_scope_local_data = match mir.source_scope_local_data {
|
2017-11-22 13:47:50 +01:00
|
|
|
ClearCrossCrate::Set(ref data) => data,
|
|
|
|
ClearCrossCrate::Clear => {
|
2017-09-19 16:20:02 +03:00
|
|
|
debug!("unsafety_violations: {:?} - remote, skipping", def_id);
|
2017-11-05 20:04:18 +02:00
|
|
|
return UnsafetyCheckResult {
|
2018-02-27 17:11:14 +01:00
|
|
|
violations: Lrc::new([]),
|
|
|
|
unsafe_blocks: Lrc::new([])
|
2017-11-05 20:04:18 +02:00
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let param_env = tcx.param_env(def_id);
|
2018-12-07 18:26:46 +01:00
|
|
|
|
2019-03-04 09:00:30 +01:00
|
|
|
let id = tcx.hir().as_local_hir_id(def_id).unwrap();
|
|
|
|
let (const_context, min_const_fn) = match tcx.hir().body_owner_kind_by_hir_id(id) {
|
2018-12-07 18:26:46 +01:00
|
|
|
hir::BodyOwnerKind::Closure => (false, false),
|
|
|
|
hir::BodyOwnerKind::Fn => (tcx.is_const_fn(def_id), tcx.is_min_const_fn(def_id)),
|
|
|
|
hir::BodyOwnerKind::Const |
|
|
|
|
hir::BodyOwnerKind::Static(_) => (true, false),
|
|
|
|
};
|
2017-09-19 16:20:02 +03:00
|
|
|
let mut checker = UnsafetyChecker::new(
|
2018-12-07 18:26:46 +01:00
|
|
|
const_context, min_const_fn,
|
2018-05-28 17:37:48 +03:00
|
|
|
mir, source_scope_local_data, tcx, param_env);
|
2017-09-19 16:20:02 +03:00
|
|
|
checker.visit_mir(mir);
|
|
|
|
|
2017-11-05 20:04:18 +02:00
|
|
|
check_unused_unsafe(tcx, def_id, &checker.used_unsafe, &mut checker.inherited_blocks);
|
|
|
|
UnsafetyCheckResult {
|
|
|
|
violations: checker.violations.into(),
|
|
|
|
unsafe_blocks: checker.inherited_blocks.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 19:01:19 +02:00
|
|
|
fn unsafe_derive_on_repr_packed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
|
2019-02-26 11:48:34 +01:00
|
|
|
let lint_hir_id = tcx.hir().as_local_hir_id(def_id).unwrap_or_else(||
|
|
|
|
bug!("checking unsafety for non-local def id {:?}", def_id));
|
2017-11-26 19:01:19 +02:00
|
|
|
|
|
|
|
// FIXME: when we make this a hard error, this should have its
|
|
|
|
// own error code.
|
2019-04-17 23:09:22 +01:00
|
|
|
let message = if tcx.generics_of(def_id).own_requires_monomorphization() {
|
2018-07-28 14:40:32 +02:00
|
|
|
"#[derive] can't be used on a #[repr(packed)] struct with \
|
2019-02-20 01:19:13 +00:00
|
|
|
type or const parameters (error E0133)".to_string()
|
2017-11-26 19:01:19 +02:00
|
|
|
} else {
|
2018-07-28 14:40:32 +02:00
|
|
|
"#[derive] can't be used on a #[repr(packed)] struct that \
|
|
|
|
does not derive Copy (error E0133)".to_string()
|
2017-11-26 19:01:19 +02:00
|
|
|
};
|
2019-02-26 11:48:34 +01:00
|
|
|
tcx.lint_hir(SAFE_PACKED_BORROWS,
|
|
|
|
lint_hir_id,
|
|
|
|
tcx.def_span(def_id),
|
|
|
|
&message);
|
2017-11-26 19:01:19 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:48:14 +01:00
|
|
|
/// Returns the `HirId` for an enclosing scope that is also `unsafe`.
|
2019-02-08 06:28:15 +09:00
|
|
|
fn is_enclosed(tcx: TyCtxt<'_, '_, '_>,
|
2019-02-22 15:48:14 +01:00
|
|
|
used_unsafe: &FxHashSet<hir::HirId>,
|
|
|
|
id: hir::HirId) -> Option<(String, hir::HirId)> {
|
|
|
|
let parent_id = tcx.hir().get_parent_node_by_hir_id(id);
|
2017-11-05 20:04:18 +02:00
|
|
|
if parent_id != id {
|
|
|
|
if used_unsafe.contains(&parent_id) {
|
|
|
|
Some(("block".to_string(), parent_id))
|
2018-08-25 15:56:16 +01:00
|
|
|
} else if let Some(Node::Item(&hir::Item {
|
2018-07-11 23:36:06 +08:00
|
|
|
node: hir::ItemKind::Fn(_, header, _, _),
|
2017-11-05 20:04:18 +02:00
|
|
|
..
|
2019-02-22 15:48:14 +01:00
|
|
|
})) = tcx.hir().find_by_hir_id(parent_id) {
|
2018-05-16 22:55:18 -07:00
|
|
|
match header.unsafety {
|
2018-02-17 22:31:14 -05:00
|
|
|
hir::Unsafety::Unsafe => Some(("fn".to_string(), parent_id)),
|
|
|
|
hir::Unsafety::Normal => None,
|
|
|
|
}
|
2017-11-05 20:04:18 +02:00
|
|
|
} else {
|
|
|
|
is_enclosed(tcx, used_unsafe, parent_id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 06:28:15 +09:00
|
|
|
fn report_unused_unsafe(tcx: TyCtxt<'_, '_, '_>,
|
2019-02-22 15:48:14 +01:00
|
|
|
used_unsafe: &FxHashSet<hir::HirId>,
|
|
|
|
id: hir::HirId) {
|
|
|
|
let span = tcx.sess.source_map().def_span(tcx.hir().span_by_hir_id(id));
|
2017-12-17 23:48:59 -08:00
|
|
|
let msg = "unnecessary `unsafe` block";
|
2019-02-22 15:48:14 +01:00
|
|
|
let mut db = tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, msg);
|
2017-12-17 23:48:59 -08:00
|
|
|
db.span_label(span, msg);
|
2017-11-05 20:04:18 +02:00
|
|
|
if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) {
|
2019-02-22 15:48:14 +01:00
|
|
|
db.span_label(tcx.sess.source_map().def_span(tcx.hir().span_by_hir_id(id)),
|
2017-12-17 23:48:59 -08:00
|
|
|
format!("because it's nested under this `unsafe` {}", kind));
|
2017-11-05 20:04:18 +02:00
|
|
|
}
|
|
|
|
db.emit();
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
2017-11-19 17:04:24 +02:00
|
|
|
fn builtin_derive_def_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Option<DefId> {
|
|
|
|
debug!("builtin_derive_def_id({:?})", def_id);
|
|
|
|
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
|
|
|
|
if tcx.has_attr(impl_def_id, "automatically_derived") {
|
|
|
|
debug!("builtin_derive_def_id({:?}) - is {:?}", def_id, impl_def_id);
|
|
|
|
Some(impl_def_id)
|
|
|
|
} else {
|
|
|
|
debug!("builtin_derive_def_id({:?}) - not automatically derived", def_id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug!("builtin_derive_def_id({:?}) - not a method", def_id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 16:20:02 +03:00
|
|
|
pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
|
|
|
|
debug!("check_unsafety({:?})", def_id);
|
2017-11-11 02:20:53 +09:00
|
|
|
|
|
|
|
// closures are handled by their parent fn.
|
|
|
|
if tcx.is_closure(def_id) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
|
2017-11-05 20:04:18 +02:00
|
|
|
let UnsafetyCheckResult {
|
|
|
|
violations,
|
|
|
|
unsafe_blocks
|
|
|
|
} = tcx.unsafety_check_result(def_id);
|
|
|
|
|
2017-09-19 16:20:02 +03:00
|
|
|
for &UnsafetyViolation {
|
2018-07-10 10:52:05 +02:00
|
|
|
source_info, description, details, kind
|
2017-11-05 20:04:18 +02:00
|
|
|
} in violations.iter() {
|
2017-09-19 16:20:02 +03:00
|
|
|
// Report an error.
|
2017-11-16 20:12:23 +02:00
|
|
|
match kind {
|
2018-11-05 18:06:26 +01:00
|
|
|
UnsafetyViolationKind::GeneralAndConstFn |
|
2017-11-16 20:12:23 +02:00
|
|
|
UnsafetyViolationKind::General => {
|
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess, source_info.span, E0133,
|
2018-07-10 10:52:05 +02:00
|
|
|
"{} is unsafe and requires unsafe function or block", description)
|
2018-04-11 23:02:41 +02:00
|
|
|
.span_label(source_info.span, &description.as_str()[..])
|
2018-07-10 10:52:05 +02:00
|
|
|
.note(&details.as_str()[..])
|
2017-11-16 20:12:23 +02:00
|
|
|
.emit();
|
|
|
|
}
|
2019-02-22 15:48:14 +01:00
|
|
|
UnsafetyViolationKind::ExternStatic(lint_hir_id) => {
|
2018-07-10 10:52:05 +02:00
|
|
|
tcx.lint_node_note(SAFE_EXTERN_STATICS,
|
2019-02-22 15:48:14 +01:00
|
|
|
lint_hir_id,
|
2017-11-16 20:12:23 +02:00
|
|
|
source_info.span,
|
2018-07-10 10:52:05 +02:00
|
|
|
&format!("{} is unsafe and requires unsafe function or block \
|
|
|
|
(error E0133)", &description.as_str()[..]),
|
|
|
|
&details.as_str()[..]);
|
2017-11-16 20:12:23 +02:00
|
|
|
}
|
2019-02-22 15:48:14 +01:00
|
|
|
UnsafetyViolationKind::BorrowPacked(lint_hir_id) => {
|
2017-11-26 19:01:19 +02:00
|
|
|
if let Some(impl_def_id) = builtin_derive_def_id(tcx, def_id) {
|
|
|
|
tcx.unsafe_derive_on_repr_packed(impl_def_id);
|
2017-11-19 17:04:24 +02:00
|
|
|
} else {
|
2018-07-10 10:52:05 +02:00
|
|
|
tcx.lint_node_note(SAFE_PACKED_BORROWS,
|
2019-02-22 15:48:14 +01:00
|
|
|
lint_hir_id,
|
2017-11-26 19:01:19 +02:00
|
|
|
source_info.span,
|
2018-07-10 10:52:05 +02:00
|
|
|
&format!("{} is unsafe and requires unsafe function or block \
|
|
|
|
(error E0133)", &description.as_str()[..]),
|
|
|
|
&details.as_str()[..]);
|
2017-11-26 19:01:19 +02:00
|
|
|
}
|
2017-11-16 20:12:23 +02:00
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-05 20:04:18 +02:00
|
|
|
|
|
|
|
let mut unsafe_blocks: Vec<_> = unsafe_blocks.into_iter().collect();
|
2019-02-22 15:48:14 +01:00
|
|
|
unsafe_blocks.sort_by_cached_key(|(hir_id, _)| tcx.hir().hir_to_node_id(*hir_id));
|
2017-11-05 20:04:18 +02:00
|
|
|
let used_unsafe: FxHashSet<_> = unsafe_blocks.iter()
|
|
|
|
.flat_map(|&&(id, used)| if used { Some(id) } else { None })
|
|
|
|
.collect();
|
|
|
|
for &(block_id, is_used) in unsafe_blocks {
|
|
|
|
if !is_used {
|
|
|
|
report_unused_unsafe(tcx, &used_unsafe, block_id);
|
|
|
|
}
|
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|