Skip to content

Commit 93bf791

Browse files
authored
Rollup merge of #129248 - compiler-errors:raw-ref-deref, r=nnethercote
Taking a raw ref (`&raw (const|mut)`) of a deref of pointer (`*ptr`) is always safe T-opsem decided in rust-lang/reference#1387 that `*ptr` is only unsafe if the place is accessed. This means that taking a raw ref of a deref expr is always safe, since it doesn't constitute a read. This also relaxes the `DEREF_NULLPTR` lint to stop warning in the case of raw ref of a deref'd nullptr, and updates its docs to reflect that change in the UB specification. This does not change the behavior of `addr_of!((*ptr).field)`, since field projections still require the projection is in-bounds. I'm on the fence whether this requires an FCP, since it's something that is guaranteed by the reference you could ostensibly call this a bugfix since we were counting truly safe operations as unsafe. Perhaps someone on opsem has a strong opinion? cc `@rust-lang/opsem`
2 parents 8aca4ba + 367183b commit 93bf791

8 files changed

+46
-52
lines changed

compiler/rustc_lint/src/builtin.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -2657,8 +2657,8 @@ declare_lint! {
26572657
///
26582658
/// ### Explanation
26592659
///
2660-
/// Dereferencing a null pointer causes [undefined behavior] even as a place expression,
2661-
/// like `&*(0 as *const i32)` or `addr_of!(*(0 as *const i32))`.
2660+
/// Dereferencing a null pointer causes [undefined behavior] if it is accessed
2661+
/// (loaded from or stored to).
26622662
///
26632663
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
26642664
pub DEREF_NULLPTR,
@@ -2673,14 +2673,14 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
26732673
/// test if expression is a null ptr
26742674
fn is_null_ptr(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
26752675
match &expr.kind {
2676-
rustc_hir::ExprKind::Cast(expr, ty) => {
2677-
if let rustc_hir::TyKind::Ptr(_) = ty.kind {
2676+
hir::ExprKind::Cast(expr, ty) => {
2677+
if let hir::TyKind::Ptr(_) = ty.kind {
26782678
return is_zero(expr) || is_null_ptr(cx, expr);
26792679
}
26802680
}
26812681
// check for call to `core::ptr::null` or `core::ptr::null_mut`
2682-
rustc_hir::ExprKind::Call(path, _) => {
2683-
if let rustc_hir::ExprKind::Path(ref qpath) = path.kind {
2682+
hir::ExprKind::Call(path, _) => {
2683+
if let hir::ExprKind::Path(ref qpath) = path.kind {
26842684
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() {
26852685
return matches!(
26862686
cx.tcx.get_diagnostic_name(def_id),
@@ -2697,7 +2697,7 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
26972697
/// test if expression is the literal `0`
26982698
fn is_zero(expr: &hir::Expr<'_>) -> bool {
26992699
match &expr.kind {
2700-
rustc_hir::ExprKind::Lit(lit) => {
2700+
hir::ExprKind::Lit(lit) => {
27012701
if let LitKind::Int(a, _) = lit.node {
27022702
return a == 0;
27032703
}
@@ -2707,8 +2707,16 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
27072707
false
27082708
}
27092709

2710-
if let rustc_hir::ExprKind::Unary(rustc_hir::UnOp::Deref, expr_deref) = expr.kind {
2711-
if is_null_ptr(cx, expr_deref) {
2710+
if let hir::ExprKind::Unary(hir::UnOp::Deref, expr_deref) = expr.kind
2711+
&& is_null_ptr(cx, expr_deref)
2712+
{
2713+
if let hir::Node::Expr(hir::Expr {
2714+
kind: hir::ExprKind::AddrOf(hir::BorrowKind::Raw, ..),
2715+
..
2716+
}) = cx.tcx.parent_hir_node(expr.hir_id)
2717+
{
2718+
// `&raw *NULL` is ok.
2719+
} else {
27122720
cx.emit_span_lint(DEREF_NULLPTR, expr.span, BuiltinDerefNullptr {
27132721
label: expr.span,
27142722
});

compiler/rustc_mir_build/src/check_unsafety.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -509,20 +509,12 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
509509
}
510510
ExprKind::RawBorrow { arg, .. } => {
511511
if let ExprKind::Scope { value: arg, .. } = self.thir[arg].kind
512-
// THIR desugars UNSAFE_STATIC into *UNSAFE_STATIC_REF, where
513-
// UNSAFE_STATIC_REF holds the addr of the UNSAFE_STATIC, so: take two steps
514512
&& let ExprKind::Deref { arg } = self.thir[arg].kind
515-
// FIXME(workingjubiee): we lack a clear reason to reject ThreadLocalRef here,
516-
// but we also have no conclusive reason to allow it either!
517-
&& let ExprKind::StaticRef { .. } = self.thir[arg].kind
518513
{
519-
// A raw ref to a place expr, even an "unsafe static", is okay!
520-
// We short-circuit to not recursively traverse this expression.
514+
// Taking a raw ref to a deref place expr is always safe.
515+
// Make sure the expression we're deref'ing is safe, though.
516+
visit::walk_expr(self, &self.thir[arg]);
521517
return;
522-
// note: const_mut_refs enables this code, and it currently remains unsafe:
523-
// static mut BYTE: u8 = 0;
524-
// static mut BYTE_PTR: *mut u8 = unsafe { addr_of_mut!(BYTE) };
525-
// static mut DEREF_BYTE_PTR: *mut u8 = unsafe { addr_of_mut!(*BYTE_PTR) };
526518
}
527519
}
528520
ExprKind::Deref { arg } => {

tests/ui/lint/lint-deref-nullptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ fn f() {
2727
let ub = &*ptr::null_mut::<i32>();
2828
//~^ ERROR dereferencing a null pointer
2929
ptr::addr_of!(*ptr::null::<i32>());
30-
//~^ ERROR dereferencing a null pointer
30+
// ^^ OKAY
3131
ptr::addr_of_mut!(*ptr::null_mut::<i32>());
32-
//~^ ERROR dereferencing a null pointer
32+
// ^^ OKAY
3333
let offset = ptr::addr_of!((*ptr::null::<Struct>()).field);
3434
//~^ ERROR dereferencing a null pointer
3535
}

tests/ui/lint/lint-deref-nullptr.stderr

+1-13
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,11 @@ error: dereferencing a null pointer
4646
LL | let ub = &*ptr::null_mut::<i32>();
4747
| ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
4848

49-
error: dereferencing a null pointer
50-
--> $DIR/lint-deref-nullptr.rs:29:23
51-
|
52-
LL | ptr::addr_of!(*ptr::null::<i32>());
53-
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
54-
55-
error: dereferencing a null pointer
56-
--> $DIR/lint-deref-nullptr.rs:31:27
57-
|
58-
LL | ptr::addr_of_mut!(*ptr::null_mut::<i32>());
59-
| ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
60-
6149
error: dereferencing a null pointer
6250
--> $DIR/lint-deref-nullptr.rs:33:36
6351
|
6452
LL | let offset = ptr::addr_of!((*ptr::null::<Struct>()).field);
6553
| ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
6654

67-
error: aborting due to 10 previous errors
55+
error: aborting due to 8 previous errors
6856

tests/ui/static/raw-ref-deref-with-unsafe.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//@ check-pass
22
use std::ptr;
33

4-
// This code should remain unsafe because of the two unsafe operations here,
5-
// even if in a hypothetical future we deem all &raw (const|mut) *ptr exprs safe.
6-
74
static mut BYTE: u8 = 0;
85
static mut BYTE_PTR: *mut u8 = ptr::addr_of_mut!(BYTE);
6+
7+
// This code should remain unsafe because reading from a static mut is *always* unsafe.
8+
99
// An unsafe static's ident is a place expression in its own right, so despite the above being safe
10-
// (it's fine to create raw refs to places!) the following derefs the ptr before creating its ref
10+
// (it's fine to create raw refs to places!) the following *reads* from the static mut place before
11+
// derefing it explicitly with the `*` below.
1112
static mut DEREF_BYTE_PTR: *mut u8 = unsafe { ptr::addr_of_mut!(*BYTE_PTR) };
1213

1314
fn main() {

tests/ui/static/raw-ref-deref-without-unsafe.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use std::ptr;
22

3-
// This code should remain unsafe because of the two unsafe operations here,
4-
// even if in a hypothetical future we deem all &raw (const|mut) *ptr exprs safe.
5-
63
static mut BYTE: u8 = 0;
74
static mut BYTE_PTR: *mut u8 = ptr::addr_of_mut!(BYTE);
5+
6+
// This code should remain unsafe because reading from a static mut is *always* unsafe.
7+
88
// An unsafe static's ident is a place expression in its own right, so despite the above being safe
99
// (it's fine to create raw refs to places!) the following derefs the ptr before creating its ref!
1010
static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR);
1111
//~^ ERROR: use of mutable static
12-
//~| ERROR: dereference of raw pointer
1312

1413
fn main() {
1514
let _ = unsafe { DEREF_BYTE_PTR };
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/raw-ref-deref-without-unsafe.rs:10:56
3-
|
4-
LL | static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR);
5-
| ^^^^^^^^^ dereference of raw pointer
6-
|
7-
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
8-
91
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
102
--> $DIR/raw-ref-deref-without-unsafe.rs:10:57
113
|
@@ -14,6 +6,6 @@ LL | static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR);
146
|
157
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
168

17-
error: aborting due to 2 previous errors
9+
error: aborting due to 1 previous error
1810

1911
For more information about this error, try `rustc --explain E0133`.

tests/ui/unsafe/place-expr-safe.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ check-pass
2+
3+
fn main() {
4+
let ptr = std::ptr::null_mut::<i32>();
5+
let addr = &raw const *ptr;
6+
7+
let local = 1;
8+
let ptr = &local as *const i32;
9+
let addr = &raw const *ptr;
10+
11+
let boxed = Box::new(1);
12+
let ptr = &*boxed as *const i32;
13+
let addr = &raw const *ptr;
14+
}

0 commit comments

Comments
 (0)