Skip to content

Commit 103d02f

Browse files
seanmonstaralexcrichton
authored andcommitted
lint: deny transmuting from immutable to mutable, since it's undefined behavior
[breaking-change] Technically breaking, since code that had been using these transmutes before will no longer compile. However, it was undefined behavior, so really, it's a good thing. Fixing your code would require some re-working to use an UnsafeCell instead. Closes #13146
1 parent 850151a commit 103d02f

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/librustc_lint/builtin.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,72 @@ impl LintPass for InvalidNoMangleItems {
20872087
}
20882088
}
20892089

2090+
#[derive(Clone, Copy)]
2091+
pub struct MutableTransmutes;
2092+
2093+
declare_lint! {
2094+
MUTABLE_TRANSMUTES,
2095+
Deny,
2096+
"mutating transmuted &mut T from &T may cause undefined behavior"
2097+
}
2098+
2099+
impl LintPass for MutableTransmutes {
2100+
fn get_lints(&self) -> LintArray {
2101+
lint_array!(MUTABLE_TRANSMUTES)
2102+
}
2103+
2104+
fn check_expr(&mut self, cx: &Context, expr: &ast::Expr) {
2105+
use syntax::ast::DefId;
2106+
use syntax::abi::RustIntrinsic;
2107+
let msg = "mutating transmuted &mut T from &T may cause undefined behavior,\
2108+
consider instead using an UnsafeCell";
2109+
match get_transmute_from_to(cx, expr) {
2110+
Some((&ty::ty_rptr(_, from_mt), &ty::ty_rptr(_, to_mt))) => {
2111+
if to_mt.mutbl == ast::Mutability::MutMutable
2112+
&& from_mt.mutbl == ast::Mutability::MutImmutable {
2113+
cx.span_lint(MUTABLE_TRANSMUTES, expr.span, msg);
2114+
}
2115+
}
2116+
_ => ()
2117+
}
2118+
2119+
fn get_transmute_from_to<'a, 'tcx>(cx: &Context<'a, 'tcx>, expr: &ast::Expr)
2120+
-> Option<(&'tcx ty::sty<'tcx>, &'tcx ty::sty<'tcx>)> {
2121+
match expr.node {
2122+
ast::ExprPath(..) => (),
2123+
_ => return None
2124+
}
2125+
if let DefFn(did, _) = ty::resolve_expr(cx.tcx, expr) {
2126+
if !def_id_is_transmute(cx, did) {
2127+
return None;
2128+
}
2129+
let typ = ty::node_id_to_type(cx.tcx, expr.id);
2130+
match typ.sty {
2131+
ty::ty_bare_fn(_, ref bare_fn) if bare_fn.abi == RustIntrinsic => {
2132+
if let ty::FnConverging(to) = bare_fn.sig.0.output {
2133+
let from = bare_fn.sig.0.inputs[0];
2134+
return Some((&from.sty, &to.sty));
2135+
}
2136+
},
2137+
_ => ()
2138+
}
2139+
}
2140+
None
2141+
}
2142+
2143+
fn def_id_is_transmute(cx: &Context, def_id: DefId) -> bool {
2144+
match ty::lookup_item_type(cx.tcx, def_id).ty.sty {
2145+
ty::ty_bare_fn(_, ref bfty) if bfty.abi == RustIntrinsic => (),
2146+
_ => return false
2147+
}
2148+
ty::with_path(cx.tcx, def_id, |path| match path.last() {
2149+
Some(ref last) => last.name().as_str() == "transmute",
2150+
_ => false
2151+
})
2152+
}
2153+
}
2154+
}
2155+
20902156
/// Forbids using the `#[feature(...)]` attribute
20912157
#[derive(Copy, Clone)]
20922158
pub struct UnstableFeatures;

src/librustc_lint/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
110110
InvalidNoMangleItems,
111111
PluginAsLibrary,
112112
DropWithReprExtern,
113+
MutableTransmutes,
113114
);
114115

115116
add_builtin_with_new!(sess,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests that transmuting from &T to &mut T is Undefined Behavior.
12+
13+
use std::mem::transmute;
14+
15+
fn main() {
16+
let _a: &mut u8 = unsafe { transmute(&1u8) };
17+
//~^ ERROR mutating transmuted &mut T from &T may cause undefined behavior
18+
}
19+
20+

0 commit comments

Comments
 (0)