Skip to content

Commit 1497744

Browse files
authored
Rollup merge of #130350 - RalfJung:strict-provenance, r=dtolnay
stabilize Strict Provenance and Exposed Provenance APIs Given that [RFC 3559](https://rust-lang.github.io/rfcs/3559-rust-has-provenance.html) has been accepted, t-lang has approved the concept of provenance to exist in the language. So I think it's time that we stabilize the strict provenance and exposed provenance APIs, and discuss provenance explicitly in the docs: ```rust // core::ptr pub const fn without_provenance<T>(addr: usize) -> *const T; pub const fn dangling<T>() -> *const T; pub const fn without_provenance_mut<T>(addr: usize) -> *mut T; pub const fn dangling_mut<T>() -> *mut T; pub fn with_exposed_provenance<T>(addr: usize) -> *const T; pub fn with_exposed_provenance_mut<T>(addr: usize) -> *mut T; impl<T: ?Sized> *const T { pub fn addr(self) -> usize; pub fn expose_provenance(self) -> usize; pub fn with_addr(self, addr: usize) -> Self; pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self; } impl<T: ?Sized> *mut T { pub fn addr(self) -> usize; pub fn expose_provenance(self) -> usize; pub fn with_addr(self, addr: usize) -> Self; pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self; } impl<T: ?Sized> NonNull<T> { pub fn addr(self) -> NonZero<usize>; pub fn with_addr(self, addr: NonZero<usize>) -> Self; pub fn map_addr(self, f: impl FnOnce(NonZero<usize>) -> NonZero<usize>) -> Self; } ``` I also did a pass over the docs to adjust them, because this is no longer an "experiment". The `ptr` docs now discuss the concept of provenance in general, and then they go into the two families of APIs for dealing with provenance: Strict Provenance and Exposed Provenance. I removed the discussion of how pointers also have an associated "address space" -- that is not actually tracked in the pointer value, it is tracked in the type, so IMO it just distracts from the core point of provenance. I also adjusted the docs for `with_exposed_provenance` to make it clear that we cannot guarantee much about this function, it's all best-effort. There are two unstable lints associated with the strict_provenance feature gate; I moved them to a new [strict_provenance_lints](rust-lang/rust#130351) feature since I didn't want this PR to have an even bigger FCP. ;) `@rust-lang/opsem` Would be great to get some feedback on the docs here. :) Nominating for `@rust-lang/libs-api.` Part of rust-lang/rust#95228. [FCP comment](rust-lang/rust#130350 (comment))
2 parents 1556144 + 9e0205d commit 1497744

37 files changed

+4
-38
lines changed

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#![feature(let_chains)]
1212
#![feature(trait_upcasting)]
1313
#![feature(strict_overflow_ops)]
14-
#![feature(strict_provenance)]
15-
#![feature(exposed_provenance)]
1614
#![feature(pointer_is_aligned_to)]
1715
#![feature(unqualified_local_imports)]
1816
// Configure clippy and other lints

tests/fail/dangling_pointers/dangling_pointer_to_raw_pointer.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(strict_provenance)]
21
use std::ptr;
32

43
fn direct_raw(x: *const (i32, i32)) -> *const i32 {

tests/fail/dangling_pointers/deref_dangling_box.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Should be caught even without retagging
22
//@compile-flags: -Zmiri-disable-stacked-borrows
3-
#![feature(strict_provenance)]
43
use std::ptr::{self, addr_of_mut};
54

65
// Deref'ing a dangling raw pointer is fine, but for a dangling box it is not.

tests/fail/dangling_pointers/deref_dangling_ref.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Should be caught even without retagging
22
//@compile-flags: -Zmiri-disable-stacked-borrows
3-
#![feature(strict_provenance)]
43
use std::ptr::{self, addr_of_mut};
54

65
// Deref'ing a dangling raw pointer is fine, but for a dangling reference it is not.

tests/fail/intrinsics/ptr_offset_from_different_ints.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(strict_provenance)]
21
use core::ptr;
32

43
fn main() {

tests/fail/provenance/int_copy_looses_provenance3.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(strict_provenance)]
21
use std::mem;
32

43
#[repr(C, usize)]

tests/fail/provenance/provenance_transmute.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-permissive-provenance
2-
#![feature(strict_provenance)]
32

43
use std::mem;
54

tests/fail/provenance/ptr_int_unexposed.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-permissive-provenance
2-
#![feature(strict_provenance, exposed_provenance)]
32

43
fn main() {
54
let x: i32 = 3;

tests/fail/provenance/ptr_invalid.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(strict_provenance, exposed_provenance)]
21

32
// Ensure that a `ptr::without_provenance` ptr is truly invalid.
43
fn main() {

tests/fail/provenance/ptr_invalid_offset.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-strict-provenance
2-
#![feature(strict_provenance)]
32

43
fn main() {
54
let x = 22;

tests/fail/provenance/strict_provenance_cast.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-strict-provenance
2-
#![feature(exposed_provenance)]
32

43
fn main() {
54
let addr = &0 as *const i32 as usize;

tests/fail/stacked_borrows/exposed_only_ro.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-permissive-provenance
2-
#![feature(exposed_provenance)]
32

43
// If we have only exposed read-only pointers, doing a write through a wildcard ptr should fail.
54

tests/fail/unaligned_pointers/promise_alignment.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@compile-flags: -Zmiri-symbolic-alignment-check
22
//@revisions: call_unaligned_ptr read_unaligned_ptr
3-
#![feature(strict_provenance)]
43

54
#[path = "../../utils/mod.rs"]
65
mod utils;

tests/fail/uninit/uninit_alloc_diagnostic_with_provenance.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@compile-flags: -Zmiri-disable-validation
22
//@error-in-other-file: memory is uninitialized at [0x4..0x8]
33
//@normalize-stderr-test: "a[0-9]+" -> "ALLOC"
4-
#![feature(strict_provenance)]
54
#![allow(dropping_copy_types)]
65

76
// Test printing allocations that contain single-byte provenance.

tests/pass-dep/libc/libc-affinity.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//@compile-flags: -Zmiri-disable-isolation -Zmiri-num-cpus=4
44
#![feature(io_error_more)]
55
#![feature(pointer_is_aligned_to)]
6-
#![feature(strict_provenance)]
76

87
use std::mem::{size_of, size_of_val};
98

tests/pass-dep/libc/libc-epoll-no-blocking.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@only-target: linux
22

3-
#![feature(strict_provenance)]
43
use std::convert::TryInto;
54

65
fn main() {

tests/pass-dep/libc/libc-mem.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(strict_provenance, pointer_is_aligned_to)]
1+
#![feature(pointer_is_aligned_to)]
22
use std::{mem, ptr, slice};
33

44
fn test_memcpy() {

tests/pass-dep/libc/libc-misc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//@compile-flags: -Zmiri-disable-isolation
33
#![feature(io_error_more)]
44
#![feature(pointer_is_aligned_to)]
5-
#![feature(strict_provenance)]
65

76
use std::mem::transmute;
87

tests/pass-dep/libc/mmap.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@ignore-target: windows # No mmap on Windows
22
//@compile-flags: -Zmiri-disable-isolation -Zmiri-permissive-provenance
3-
#![feature(strict_provenance)]
43

54
use std::io::Error;
65
use std::{ptr, slice};

tests/pass/align_offset_symbolic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-symbolic-alignment-check
2-
#![feature(strict_provenance)]
32

43
use std::mem;
54

tests/pass/atomic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@[tree]compile-flags: -Zmiri-tree-borrows
33
//@compile-flags: -Zmiri-strict-provenance
44

5-
#![feature(strict_provenance, strict_provenance_atomic_ptr)]
5+
#![feature(strict_provenance_atomic_ptr)]
66
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
77
#![allow(static_mut_refs)]
88

tests/pass/box-custom-alloc-aliasing.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//@revisions: stack tree
66
//@[tree]compile-flags: -Zmiri-tree-borrows
77
#![feature(allocator_api)]
8-
#![feature(strict_provenance)]
98

109
use std::alloc::{AllocError, Allocator, Layout};
1110
use std::cell::{Cell, UnsafeCell};

tests/pass/concurrency/address_reuse_happens_before.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Regression test for <https://github.com/rust-lang/miri/issues/3450>:
22
//! When the address gets reused, there should be a happens-before relation.
33
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=1.0
4-
#![feature(strict_provenance)]
54
#![feature(sync_unsafe_cell)]
65

76
use std::cell::SyncUnsafeCell;

tests/pass/const-addrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// MIR inlining will put every evaluation of the const we're repeatedly evaluating into the same
88
// stack frame, breaking this test.
99
//@compile-flags: -Zinline-mir=no
10-
#![feature(strict_provenance)]
1110

1211
const EVALS: usize = 256;
1312

tests/pass/drop_type_without_drop_glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(custom_mir, core_intrinsics, strict_provenance)]
1+
#![feature(custom_mir, core_intrinsics)]
22
use std::intrinsics::mir::*;
33

44
// The `Drop` terminator on a type with no drop glue should be a NOP.

tests/pass/extern_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
#![feature(extern_types, strict_provenance)]
3+
#![feature(extern_types)]
44

55
use std::ptr;
66

tests/pass/provenance.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
#![feature(strict_provenance)]
43
use std::{mem, ptr};
54

65
const PTR_SIZE: usize = mem::size_of::<&i32>();

tests/pass/ptr_int_from_exposed.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Tree Borrows doesn't support int2ptr casts, but let's make sure we don't immediately crash either.
33
//@[tree]compile-flags: -Zmiri-tree-borrows
44
//@[stack]compile-flags: -Zmiri-permissive-provenance
5-
#![feature(strict_provenance, exposed_provenance)]
65

76
use std::ptr;
87

tests/pass/ptr_raw.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(strict_provenance)]
21
use std::mem;
32
use std::ptr::{self, addr_of};
43

tests/pass/shims/ptr_mask.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(ptr_mask)]
2-
#![feature(strict_provenance)]
32

43
fn main() {
54
let v: u32 = 0xABCDABCD;

tests/pass/slices.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![feature(slice_as_chunks)]
55
#![feature(slice_partition_dedup)]
66
#![feature(layout_for_ptr)]
7-
#![feature(strict_provenance)]
87

98
use std::{ptr, slice};
109

tests/pass/stacked-borrows/int-to-ptr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-permissive-provenance
2-
#![feature(exposed_provenance)]
32
use std::ptr;
43

54
// Just to make sure that casting a ref to raw, to int and back to raw

tests/pass/stacked-borrows/stack-printing.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// printing, not how it interacts with the GC.
33
//@compile-flags: -Zmiri-permissive-provenance -Zmiri-provenance-gc=0
44

5-
#![feature(strict_provenance)]
65
use std::alloc::{self, Layout};
76
use std::mem::ManuallyDrop;
87

tests/pass/stacked-borrows/unknown-bottom-gc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-permissive-provenance
2-
#![feature(exposed_provenance)]
32

43
use std::ptr;
54

tests/pass/transmute_ptr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
#![feature(strict_provenance)]
43
use std::{mem, ptr};
54

65
fn t1() {

tests/pass/underscore_pattern.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Various tests ensuring that underscore patterns really just construct the place, but don't check its contents.
2-
#![feature(strict_provenance)]
32
#![feature(never_type)]
43

54
use std::ptr;

tests/pass/zero-sized-accesses-and-offsets.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Tests specific for <https://github.com/rust-lang/rust/issues/117945>: zero-sized operations.
2-
#![feature(strict_provenance)]
32
43
use std::ptr;
54

0 commit comments

Comments
 (0)