Skip to content

Commit 2fdb714

Browse files
committed
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 5449f5d commit 2fdb714

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/librustc_lint/builtin.rs

+66
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,72 @@ impl LintPass for InvalidNoMangleItems {
21222122
}
21232123
}
21242124

2125+
#[derive(Clone, Copy)]
2126+
pub struct MutableTransmutes;
2127+
2128+
declare_lint! {
2129+
MUTABLE_TRANSMUTES,
2130+
Deny,
2131+
"mutating transmuted &mut T from &T may cause undefined behavior"
2132+
}
2133+
2134+
impl LintPass for MutableTransmutes {
2135+
fn get_lints(&self) -> LintArray {
2136+
lint_array!(MUTABLE_TRANSMUTES)
2137+
}
2138+
2139+
fn check_expr(&mut self, cx: &Context, expr: &ast::Expr) {
2140+
use syntax::ast::DefId;
2141+
use syntax::abi::RustIntrinsic;
2142+
let msg = "mutating transmuted &mut T from &T may cause undefined behavior,\
2143+
consider instead using an UnsafeCell";
2144+
match get_transmute_from_to(cx, expr) {
2145+
Some((&ty::ty_rptr(_, from_mt), &ty::ty_rptr(_, to_mt))) => {
2146+
if to_mt.mutbl == ast::Mutability::MutMutable
2147+
&& from_mt.mutbl == ast::Mutability::MutImmutable {
2148+
cx.span_lint(MUTABLE_TRANSMUTES, expr.span, msg);
2149+
}
2150+
}
2151+
_ => ()
2152+
}
2153+
2154+
fn get_transmute_from_to<'a, 'tcx>(cx: &Context<'a, 'tcx>, expr: &ast::Expr)
2155+
-> Option<(&'tcx ty::sty<'tcx>, &'tcx ty::sty<'tcx>)> {
2156+
match expr.node {
2157+
ast::ExprPath(..) => (),
2158+
_ => return None
2159+
}
2160+
if let DefFn(did, _) = ty::resolve_expr(cx.tcx, expr) {
2161+
if !def_id_is_transmute(cx, did) {
2162+
return None;
2163+
}
2164+
let typ = ty::node_id_to_type(cx.tcx, expr.id);
2165+
match typ.sty {
2166+
ty::ty_bare_fn(_, ref bare_fn) if bare_fn.abi == RustIntrinsic => {
2167+
if let ty::FnConverging(to) = bare_fn.sig.0.output {
2168+
let from = bare_fn.sig.0.inputs[0];
2169+
return Some((&from.sty, &to.sty));
2170+
}
2171+
},
2172+
_ => ()
2173+
}
2174+
}
2175+
None
2176+
}
2177+
2178+
fn def_id_is_transmute(cx: &Context, def_id: DefId) -> bool {
2179+
match ty::lookup_item_type(cx.tcx, def_id).ty.sty {
2180+
ty::ty_bare_fn(_, ref bfty) if bfty.abi == RustIntrinsic => (),
2181+
_ => return false
2182+
}
2183+
ty::with_path(cx.tcx, def_id, |path| match path.last() {
2184+
Some(ref last) => last.name().as_str() == "transmute",
2185+
_ => false
2186+
})
2187+
}
2188+
}
2189+
}
2190+
21252191
/// Forbids using the `#[feature(...)]` attribute
21262192
#[derive(Copy, Clone)]
21272193
pub struct UnstableFeatures;

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
109109
InvalidNoMangleItems,
110110
PluginAsLibrary,
111111
DropWithReprExtern,
112+
MutableTransmutes,
112113
);
113114

114115
add_builtin_with_new!(sess,
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 transmuting from immutable to mutable is undefined behavior
18+
}
19+
20+

0 commit comments

Comments
 (0)