safe_extern_static -> error

This commit is contained in:
Mazdak Farrokhzad 2019-08-03 19:34:21 +02:00
parent 79b35e90f1
commit 98d2c510dd
13 changed files with 57 additions and 94 deletions

View File

@ -151,11 +151,6 @@ To fix it, remove the `()`s.
This lint detects a specific situation of re-exporting a private `extern crate`;
## safe-extern-statics
In older versions of Rust, there was a soundness issue where `extern static`s were allowed
to be accessed in safe code. This lint now catches and denies this kind of code.
## unknown-crate-types
This lint detects an unknown crate type found in a `#[crate_type]` directive. Some

View File

@ -177,16 +177,6 @@ declare_lint! {
"lints that have been renamed or removed"
}
declare_lint! {
pub SAFE_EXTERN_STATICS,
Deny,
"safe access to extern statics was erroneously allowed",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
edition: None,
};
}
declare_lint! {
pub SAFE_PACKED_BORROWS,
Warn,
@ -535,7 +525,6 @@ declare_lint_pass! {
INVALID_TYPE_PARAM_DEFAULT,
CONST_ERR,
RENAMED_AND_REMOVED_LINTS,
SAFE_EXTERN_STATICS,
SAFE_PACKED_BORROWS,
PATTERNS_IN_FNS_WITHOUT_BODY,
MISSING_FRAGMENT_SPECIFIER,

View File

@ -2701,7 +2701,6 @@ pub enum UnsafetyViolationKind {
General,
/// Permitted both in `const fn`s and regular `fn`s.
GeneralAndConstFn,
ExternStatic(hir::HirId),
BorrowPacked(hir::HirId),
}

View File

@ -338,6 +338,8 @@ fn register_builtins(store: &mut lint::LintStore, no_interleave_lints: bool) {
"converted into hard error, see https://github.com/rust-lang/rust/issues/39207");
store.register_removed("legacy_disrectory_ownership",
"converted into hard error, see https://github.com/rust-lang/rust/issues/37872");
store.register_removed("safe_extern_statics",
"converted into hard error, see https://github.com/rust-lang/rust/issues/36247");
}
fn register_internals(store: &mut lint::LintStore) {

View File

@ -8,7 +8,7 @@ use rustc::ty::cast::CastTy;
use rustc::hir;
use rustc::hir::Node;
use rustc::hir::def_id::DefId;
use rustc::lint::builtin::{SAFE_EXTERN_STATICS, SAFE_PACKED_BORROWS, UNUSED_UNSAFE};
use rustc::lint::builtin::{SAFE_PACKED_BORROWS, UNUSED_UNSAFE};
use rustc::mir::*;
use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext};
@ -208,23 +208,20 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
}
PlaceBase::Static(box Static { kind: StaticKind::Static, def_id, .. }) => {
if self.tcx.is_mutable_static(def_id) {
self.require_unsafe("use of mutable static",
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);
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"),
details: Symbol::intern(
"extern statics are not controlled by the Rust type system: \
invalid data, aliasing violations or data races will cause \
undefined behavior"),
kind: UnsafetyViolationKind::ExternStatic(lint_root)
}], &[]);
self.require_unsafe(
"use of extern static",
"extern statics are not controlled by the Rust type system: \
invalid data, aliasing violations or data races will cause \
undefined behavior",
UnsafetyViolationKind::General,
);
}
}
}
@ -351,8 +348,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
match violation.kind {
UnsafetyViolationKind::GeneralAndConstFn |
UnsafetyViolationKind::General => {},
UnsafetyViolationKind::BorrowPacked(_) |
UnsafetyViolationKind::ExternStatic(_) => if self.min_const_fn {
UnsafetyViolationKind::BorrowPacked(_) => 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
@ -380,8 +376,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
UnsafetyViolationKind::GeneralAndConstFn => {},
// these things are forbidden in const fns
UnsafetyViolationKind::General |
UnsafetyViolationKind::BorrowPacked(_) |
UnsafetyViolationKind::ExternStatic(_) => {
UnsafetyViolationKind::BorrowPacked(_) => {
let mut violation = violation.clone();
// const fns don't need to be backwards compatible and can
// emit these violations as a hard error instead of a backwards
@ -646,14 +641,6 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) {
.note(&details.as_str())
.emit();
}
UnsafetyViolationKind::ExternStatic(lint_hir_id) => {
tcx.lint_node_note(SAFE_EXTERN_STATICS,
lint_hir_id,
source_info.span,
&format!("{} is unsafe and requires unsafe function or block \
(error E0133)", description),
&details.as_str());
}
UnsafetyViolationKind::BorrowPacked(lint_hir_id) => {
if let Some(impl_def_id) = builtin_derive_def_id(tcx, def_id) {
tcx.unsafe_derive_on_repr_packed(impl_def_id);

View File

@ -1,10 +1,7 @@
#![allow(safe_extern_statics, warnings)]
extern {
pub static symbol: u32;
}
static CRASH: u32 = symbol;
//~^ ERROR could not evaluate static initializer
//~| tried to read from foreign (extern) static
//~^ ERROR use of extern static is unsafe and requires
fn main() {}

View File

@ -1,9 +1,11 @@
error[E0080]: could not evaluate static initializer
--> $DIR/issue-14227.rs:6:21
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-14227.rs:4:21
|
LL | static CRASH: u32 = symbol;
| ^^^^^^ tried to read from foreign (extern) static
| ^^^^^^ 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 E0080`.
For more information about this error, try `rustc --explain E0133`.

View File

@ -1,5 +1,3 @@
#![allow(safe_extern_statics)]
mod Y {
pub type X = usize;
extern {
@ -13,5 +11,6 @@ mod Y {
static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
//~^ ERROR `*const usize` cannot be shared between threads safely [E0277]
//~| ERROR E0015
//~| ERROR use of extern static is unsafe and requires
fn main() {}

View File

@ -1,11 +1,11 @@
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-16538.rs:13:27
--> $DIR/issue-16538.rs:11: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:13:1
--> $DIR/issue-16538.rs:11:1
|
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const usize` cannot be shared between threads safely
@ -13,7 +13,15 @@ LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
= help: the trait `std::marker::Sync` is not implemented for `*const usize`
= note: shared static variables must have a type that implements `Sync`
error: aborting due to 2 previous errors
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-16538.rs:11: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
Some errors have detailed explanations: E0015, E0277.
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`.

View File

@ -1,11 +1,8 @@
#![allow(safe_extern_statics)]
extern {
static error_message_count: u32;
}
pub static BAZ: u32 = *&error_message_count;
//~^ ERROR could not evaluate static initializer
//~| tried to read from foreign (extern) static
//~^ ERROR use of extern static is unsafe and requires
fn main() {}

View File

@ -1,9 +1,11 @@
error[E0080]: could not evaluate static initializer
--> $DIR/issue-28324.rs:7:23
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-28324.rs:5:24
|
LL | pub static BAZ: u32 = *&error_message_count;
| ^^^^^^^^^^^^^^^^^^^^^ tried to read from foreign (extern) static
| ^^^^^^^^^^^^^^^^^^^^ 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 E0080`.
For more information about this error, try `rustc --explain E0133`.

View File

@ -1,7 +1,5 @@
// aux-build:extern-statics.rs
#![allow(unused)]
extern crate extern_statics;
use extern_statics::*;
@ -11,11 +9,7 @@ extern {
fn main() {
let a = A; //~ ERROR use of extern static is unsafe
//~^ WARN this was previously accepted by the compiler
let ra = &A; //~ ERROR use of extern static is unsafe
//~^ WARN this was previously accepted by the compiler
let xa = XA; //~ ERROR use of extern static is unsafe
//~^ WARN this was previously accepted by the compiler
let xra = &XA; //~ ERROR use of extern static is unsafe
//~^ WARN this was previously accepted by the compiler
}

View File

@ -1,43 +1,35 @@
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
--> $DIR/safe-extern-statics.rs:13:13
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics.rs:11:13
|
LL | let a = A;
| ^
| ^ use of extern static
|
= note: `#[deny(safe_extern_statics)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
--> $DIR/safe-extern-statics.rs:15:14
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics.rs:12:14
|
LL | let ra = &A;
| ^^
| ^^ use of extern static
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
--> $DIR/safe-extern-statics.rs:17:14
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics.rs:13:14
|
LL | let xa = XA;
| ^^
| ^^ use of extern static
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
--> $DIR/safe-extern-statics.rs:19:15
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics.rs:14:15
|
LL | let xra = &XA;
| ^^^
| ^^^ use of extern static
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
= 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 4 previous errors
For more information about this error, try `rustc --explain E0133`.