]> git.lizzy.rs Git - rust.git/blob - library/core/src/ptr/mod.rs
Rollup merge of #95530 - notriddle:notriddle/private-kw-prim, r=jsha
[rust.git] / library / core / src / ptr / mod.rs
1 //! Manually manage memory through raw pointers.
2 //!
3 //! *[See also the pointer primitive types](pointer).*
4 //!
5 //! # Safety
6 //!
7 //! Many functions in this module take raw pointers as arguments and read from
8 //! or write to them. For this to be safe, these pointers must be *valid*.
9 //! Whether a pointer is valid depends on the operation it is used for
10 //! (read or write), and the extent of the memory that is accessed (i.e.,
11 //! how many bytes are read/written). Most functions use `*mut T` and `*const T`
12 //! to access only a single value, in which case the documentation omits the size
13 //! and implicitly assumes it to be `size_of::<T>()` bytes.
14 //!
15 //! The precise rules for validity are not determined yet. The guarantees that are
16 //! provided at this point are very minimal:
17 //!
18 //! * A [null] pointer is *never* valid, not even for accesses of [size zero][zst].
19 //! * For a pointer to be valid, it is necessary, but not always sufficient, that the pointer
20 //!   be *dereferenceable*: the memory range of the given size starting at the pointer must all be
21 //!   within the bounds of a single allocated object. Note that in Rust,
22 //!   every (stack-allocated) variable is considered a separate allocated object.
23 //! * Even for operations of [size zero][zst], the pointer must not be pointing to deallocated
24 //!   memory, i.e., deallocation makes pointers invalid even for zero-sized operations. However,
25 //!   casting any non-zero integer *literal* to a pointer is valid for zero-sized accesses, even if
26 //!   some memory happens to exist at that address and gets deallocated. This corresponds to writing
27 //!   your own allocator: allocating zero-sized objects is not very hard. The canonical way to
28 //!   obtain a pointer that is valid for zero-sized accesses is [`NonNull::dangling`].
29 //! * All accesses performed by functions in this module are *non-atomic* in the sense
30 //!   of [atomic operations] used to synchronize between threads. This means it is
31 //!   undefined behavior to perform two concurrent accesses to the same location from different
32 //!   threads unless both accesses only read from memory. Notice that this explicitly
33 //!   includes [`read_volatile`] and [`write_volatile`]: Volatile accesses cannot
34 //!   be used for inter-thread synchronization.
35 //! * The result of casting a reference to a pointer is valid for as long as the
36 //!   underlying object is live and no reference (just raw pointers) is used to
37 //!   access the same memory.
38 //!
39 //! These axioms, along with careful use of [`offset`] for pointer arithmetic,
40 //! are enough to correctly implement many useful things in unsafe code. Stronger guarantees
41 //! will be provided eventually, as the [aliasing] rules are being determined. For more
42 //! information, see the [book] as well as the section in the reference devoted
43 //! to [undefined behavior][ub].
44 //!
45 //! ## Alignment
46 //!
47 //! Valid raw pointers as defined above are not necessarily properly aligned (where
48 //! "proper" alignment is defined by the pointee type, i.e., `*const T` must be
49 //! aligned to `mem::align_of::<T>()`). However, most functions require their
50 //! arguments to be properly aligned, and will explicitly state
51 //! this requirement in their documentation. Notable exceptions to this are
52 //! [`read_unaligned`] and [`write_unaligned`].
53 //!
54 //! When a function requires proper alignment, it does so even if the access
55 //! has size 0, i.e., even if memory is not actually touched. Consider using
56 //! [`NonNull::dangling`] in such cases.
57 //!
58 //! ## Allocated object
59 //!
60 //! For several operations, such as [`offset`] or field projections (`expr.field`), the notion of an
61 //! "allocated object" becomes relevant. An allocated object is a contiguous region of memory.
62 //! Common examples of allocated objects include stack-allocated variables (each variable is a
63 //! separate allocated object), heap allocations (each allocation created by the global allocator is
64 //! a separate allocated object), and `static` variables.
65 //!
66 //!
67 //! # Strict Provenance
68 //!
69 //! **The following text is non-normative, insufficiently formal, and is an extremely strict
70 //! interpretation of provenance. It's ok if your code doesn't strictly conform to it.**
71 //!
72 //! [Strict Provenance][] is an experimental set of APIs that help tools that try
73 //! to validate the memory-safety of your program's execution. Notably this includes [miri][]
74 //! and [CHERI][], which can detect when you access out of bounds memory or otherwise violate
75 //! Rust's memory model.
76 //!
77 //! Provenance must exist in some form for any programming
78 //! language compiled for modern computer architectures, but specifying a model for provenance
79 //! in a way that is useful to both compilers and programmers is an ongoing challenge.
80 //! The [Strict Provenance][] experiment seeks to explore the question: *what if we just said you
81 //! couldn't do all the nasty operations that make provenance so messy?*
82 //!
83 //! What APIs would have to be removed? What APIs would have to be added? How much would code
84 //! have to change, and is it worse or better now? Would any patterns become truly inexpressible?
85 //! Could we carve out special exceptions for those patterns? Should we?
86 //!
87 //! A secondary goal of this project is to see if we can disambiguate the many functions of
88 //! pointer<->integer casts enough for the definition of `usize` to be loosened so that it
89 //! isn't *pointer*-sized but address-space/offset/allocation-sized (we'll probably continue
90 //! to conflate these notions). This would potentially make it possible to more efficiently
91 //! target platforms where pointers are larger than offsets, such as CHERI and maybe some
92 //! segmented architecures.
93 //!
94 //! ## Provenance
95 //!
96 //! **This section is *non-normative* and is part of the [Strict Provenance][] experiment.**
97 //!
98 //! Pointers are not *simply* an "integer" or "address". For instance, it's uncontroversial
99 //! to say that a Use After Free is clearly Undefined Behaviour, even if you "get lucky"
100 //! and the freed memory gets reallocated before your read/write (in fact this is the
101 //! worst-case scenario, UAFs would be much less concerning if this didn't happen!).
102 //! To rationalize this claim, pointers need to somehow be *more* than just their addresses:
103 //! they must have provenance.
104 //!
105 //! When an allocation is created, that allocation has a unique Original Pointer. For alloc
106 //! APIs this is literally the pointer the call returns, and for local variables and statics,
107 //! this is the name of the variable/static. This is mildly overloading the term "pointer"
108 //! for the sake of brevity/exposition.
109 //!
110 //! The Original Pointer for an allocation is guaranteed to have unique access to the entire
111 //! allocation and *only* that allocation. In this sense, an allocation can be thought of
112 //! as a "sandbox" that cannot be broken into or out of. *Provenance* is the permission
113 //! to access an allocation's sandbox and has both a *spatial* and *temporal* component:
114 //!
115 //! * Spatial: A range of bytes that the pointer is allowed to access.
116 //! * Temporal: The lifetime (of the allocation) that access to these bytes is tied to.
117 //!
118 //! Spatial provenance makes sure you don't go beyond your sandbox, while temporal provenance
119 //! makes sure that you can't "get lucky" after your permission to access some memory
120 //! has been revoked (either through deallocations or borrows expiring).
121 //!
122 //! Provenance is implicitly shared with all pointers transitively derived from
123 //! The Original Pointer through operations like [`offset`], borrowing, and pointer casts.
124 //! Some operations may *shrink* the derived provenance, limiting how much memory it can
125 //! access or how long it's valid for (i.e. borrowing a subfield and subslicing).
126 //!
127 //! Shrinking provenance cannot be undone: even if you "know" there is a larger allocation, you
128 //! can't derive a pointer with a larger provenance. Similarly, you cannot "recombine"
129 //! two contiguous provenances back into one (i.e. with a `fn merge(&[T], &[T]) -> &[T]`).
130 //!
131 //! A reference to a value always has provenance over exactly the memory that field occupies.
132 //! A reference to a slice always has provenance over exactly the range that slice describes.
133 //!
134 //! If an allocation is deallocated, all pointers with provenance to that allocation become
135 //! invalidated, and effectively lose their provenance.
136 //!
137 //! The strict provenance experiment is mostly only interested in exploring stricter *spatial*
138 //! provenance. In this sense it can be thought of as a subset of the more ambitious and
139 //! formal [Stacked Borrows][] research project, which is what tools like [miri][] are based on.
140 //! In particular, Stacked Borrows is necessary to properly describe what borrows are allowed
141 //! to do and when they become invalidated. This necessarily involves much more complex
142 //! *temporal* reasoning than simply identifying allocations. Adjusting APIs and code
143 //! for the strict provenance experiment will also greatly help Stacked Borrows.
144 //!
145 //!
146 //! ## Pointer Vs Addresses
147 //!
148 //! **This section is *non-normative* and is part of the [Strict Provenance][] experiment.**
149 //!
150 //! One of the largest historical issues with trying to define provenance is that programmers
151 //! freely convert between pointers and integers. Once you allow for this, it generally becomes
152 //! impossible to accurately track and preserve provenance information, and you need to appeal
153 //! to very complex and unreliable heuristics. But of course, converting between pointers and
154 //! integers is very useful, so what can we do?
155 //!
156 //! Also did you know WASM is actually a "Harvard Architecture"? As in function pointers are
157 //! handled completely differently from data pointers? And we kind of just shipped Rust on WASM
158 //! without really addressing the fact that we let you freely convert between function pointers
159 //! and data pointers, because it mostly Just Works? Let's just put that on the "pointer casts
160 //! are dubious" pile.
161 //!
162 //! Strict Provenance attempts to square these circles by decoupling Rust's traditional conflation
163 //! of pointers and `usize` (and `isize`), and defining a pointer to semantically contain the
164 //! following information:
165 //!
166 //! * The **address-space** it is part of (e.g. "data" vs "code" in WASM).
167 //! * The **address** it points to, which can be represented by a `usize`.
168 //! * The **provenance** it has, defining the memory it has permission to access.
169 //!
170 //! Under Strict Provenance, a usize *cannot* accurately represent a pointer, and converting from
171 //! a pointer to a usize is generally an operation which *only* extracts the address. It is
172 //! therefore *impossible* to construct a valid pointer from a usize because there is no way
173 //! to restore the address-space and provenance.
174 //!
175 //! The key insight to making this model *at all* viable is the [`with_addr`][] method:
176 //!
177 //! ```text
178 //!     /// Creates a new pointer with the given address.
179 //!     ///
180 //!     /// This performs the same operation as an `addr as ptr` cast, but copies
181 //!     /// the *address-space* and *provenance* of `self` to the new pointer.
182 //!     /// This allows us to dynamically preserve and propagate this important
183 //!     /// information in a way that is otherwise impossible with a unary cast.
184 //!     ///
185 //!     /// This is equivalent to using `wrapping_offset` to offset `self` to the
186 //!     /// given address, and therefore has all the same capabilities and restrictions.
187 //!     pub fn with_addr(self, addr: usize) -> Self;
188 //! ```
189 //!
190 //! So you're still able to drop down to the address representation and do whatever
191 //! clever bit tricks you want *as long as* you're able to keep around a pointer
192 //! into the allocation you care about that can "reconstitute" the other parts of the pointer.
193 //! Usually this is very easy, because you only are taking a pointer, messing with the address,
194 //! and then immediately converting back to a pointer. To make this use case more ergonomic,
195 //! we provide the [`map_addr`][] method.
196 //!
197 //! To help make it clear that code is "following" Strict Provenance semantics, we also
198 //! provide an [`addr`][] method which is currently equivalent to `ptr as usize`. In the
199 //! future we may provide a lint for pointer<->integer casts to help you audit if your
200 //! code conforms to strict provenance.
201 //!
202 //!
203 //! ## Using Strict Provenance
204 //!
205 //! Most code needs no changes to conform to strict provenance, as the only really concerning
206 //! operation that *wasn't* obviously already Undefined Behaviour is casts from usize to a
207 //! pointer. For code which *does* cast a usize to a pointer, the scope of the change depends
208 //! on exactly what you're doing.
209 //!
210 //! In general you just need to make sure that if you want to convert a usize address to a
211 //! pointer and then use that pointer to read/write memory, you need to keep around a pointer
212 //! that has sufficient provenance to perform that read/write itself. In this way all of your
213 //! casts from an address to a pointer are essentially just applying offsets/indexing.
214 //!
215 //! This is generally trivial to do for simple cases like tagged pointers *as long as you
216 //! represent the tagged pointer as an actual pointer and not a usize*. For instance:
217 //!
218 //! ```
219 //! #![feature(strict_provenance)]
220 //!
221 //! unsafe {
222 //!     // A flag we want to pack into our pointer
223 //!     static HAS_DATA: usize = 0x1;
224 //!     static FLAG_MASK: usize = !HAS_DATA;
225 //!
226 //!     // Our value, which must have enough alignment to have spare least-significant-bits.
227 //!     let my_precious_data: u32 = 17;
228 //!     assert!(core::mem::align_of::<u32>() > 1);
229 //!
230 //!     // Create a tagged pointer
231 //!     let ptr = &my_precious_data as *const u32;
232 //!     let tagged = ptr.map_addr(|addr| addr | HAS_DATA);
233 //!
234 //!     // Check the flag:
235 //!     if tagged.addr() & HAS_DATA != 0 {
236 //!         // Untag and read the pointer
237 //!         let data = *tagged.map_addr(|addr| addr & FLAG_MASK);
238 //!         assert_eq!(data, 17);
239 //!     } else {
240 //!         unreachable!()
241 //!     }
242 //! }
243 //! ```
244 //!
245 //! (Yes, if you've been using AtomicUsize for pointers in concurrent datastructures, you should
246 //! be using AtomicPtr instead. If that messes up the way you atomically manipulate pointers,
247 //! we would like to know why, and what needs to be done to fix it.)
248 //!
249 //! Something more complicated and just generally *evil* like an XOR-List requires more significant
250 //! changes like allocating all nodes in a pre-allocated Vec or Arena and using a pointer
251 //! to the whole allocation to reconstitute the XORed addresses.
252 //!
253 //! Situations where a valid pointer *must* be created from just an address, such as baremetal code
254 //! accessing a memory-mapped interface at a fixed address, are an open question on how to support.
255 //! These situations *will* still be allowed, but we might require some kind of "I know what I'm
256 //! doing" annotation to explain the situation to the compiler. It's also possible they need no
257 //! special attention at all, because they're generally accessing memory outside the scope of
258 //! "the abstract machine", or already using "I know what I'm doing" annotations like "volatile".
259 //!
260 //! Under [Strict Provenance] it is Undefined Behaviour to:
261 //!
262 //! * Access memory through a pointer that does not have provenance over that memory.
263 //!
264 //! * [`offset`] a pointer to or from an address it doesn't have provenance over.
265 //!   This means it's always UB to offset a pointer derived from something deallocated,
266 //!   even if the offset is 0. Note that a pointer "one past the end" of its provenance
267 //!   is not actually outside its provenance, it just has 0 bytes it can load/store.
268 //!
269 //! But it *is* still sound to:
270 //!
271 //! * Create an invalid pointer from just an address (see [`ptr::invalid`][]). This can
272 //!   be used for sentinel values like `null` *or* to represent a tagged pointer that will
273 //!   never be dereferencable. In general, it is always sound for an integer to pretend
274 //!   to be a pointer "for fun" as long as you don't use operations on it which require
275 //!   it to be valid (offset, read, write, etc).
276 //!
277 //! * Forge an allocation of size zero at any sufficiently aligned non-null address.
278 //!   i.e. the usual "ZSTs are fake, do what you want" rules apply *but* this only applies
279 //!   for actual forgery (integers cast to pointers). If you borrow some struct's field
280 //!   that *happens* to be zero-sized, the resulting pointer will have provenance tied to
281 //!   that allocation and it will still get invalidated if the allocation gets deallocated.
282 //!   In the future we may introduce an API to make such a forged allocation explicit.
283 //!
284 //! * [`wrapping_offset`][] a pointer outside its provenance. This includes invalid pointers
285 //!   which have "no" provenance. Unfortunately there may be practical limits on this for a
286 //!   particular platform, and it's an open question as to how to specify this (if at all).
287 //!   Notably, [CHERI][] relies on a compression scheme that can't handle a
288 //!   pointer getting offset "too far" out of bounds. If this happens, the address
289 //!   returned by `addr` will be the value you expect, but the provenance will get invalidated
290 //!   and using it to read/write will fault. The details of this are architecture-specific
291 //!   and based on alignment, but the buffer on either side of the pointer's range is pretty
292 //!   generous (think kilobytes, not bytes).
293 //!
294 //! * Compare arbitrary pointers by address. Addresses *are* just integers and so there is
295 //!   always a coherent answer, even if the pointers are invalid or from different
296 //!   address-spaces/provenances. Of course, comparing addresses from different address-spaces
297 //!   is generally going to be *meaningless*, but so is comparing Kilograms to Meters, and Rust
298 //!   doesn't prevent that either. Similarly, if you get "lucky" and notice that a pointer
299 //!   one-past-the-end is the "same" address as the start of an unrelated allocation, anything
300 //!   you do with that fact is *probably* going to be gibberish. The scope of that gibberish
301 //!   is kept under control by the fact that the two pointers *still* aren't allowed to access
302 //!   the other's allocation (bytes), because they still have different provenance.
303 //!
304 //! * Perform pointer tagging tricks. This falls out of [`wrapping_offset`] but is worth
305 //!   mentioning in more detail because of the limitations of [CHERI][]. Low-bit tagging
306 //!   is very robust, and often doesn't even go out of bounds because types ensure
307 //!   size >= align (and over-aligning actually gives CHERI more flexibility). Anything
308 //!   more complex than this rapidly enters "extremely platform-specific" territory as
309 //!   certain things may or may not be allowed based on specific supported operations.
310 //!   For instance, ARM explicitly supports high-bit tagging, and so CHERI on ARM inherits
311 //!   that and should support it.
312 //!
313 //!
314 //! [aliasing]: ../../nomicon/aliasing.html
315 //! [book]: ../../book/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer
316 //! [ub]: ../../reference/behavior-considered-undefined.html
317 //! [zst]: ../../nomicon/exotic-sizes.html#zero-sized-types-zsts
318 //! [atomic operations]: crate::sync::atomic
319 //! [`offset`]: pointer::offset
320 //! [`wrapping_offset`]: pointer::wrapping_offset
321 //! [`with_addr`]: pointer::with_addr
322 //! [`map_addr`]: pointer::map_addr
323 //! [`addr`]: pointer::addr
324 //! [`ptr::invalid`]: core::ptr::invalid
325 //! [miri]: https://github.com/rust-lang/miri
326 //! [CHERI]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
327 //! [Strict Provenance]: https://github.com/rust-lang/rust/issues/95228
328 //! [Stacked Borrows]: https://plv.mpi-sws.org/rustbelt/stacked-borrows/
329
330 #![stable(feature = "rust1", since = "1.0.0")]
331
332 use crate::cmp::Ordering;
333 use crate::fmt;
334 use crate::hash;
335 use crate::intrinsics::{self, abort, is_aligned_and_not_null};
336 use crate::mem::{self, MaybeUninit};
337
338 #[stable(feature = "rust1", since = "1.0.0")]
339 #[doc(inline)]
340 pub use crate::intrinsics::copy_nonoverlapping;
341
342 #[stable(feature = "rust1", since = "1.0.0")]
343 #[doc(inline)]
344 pub use crate::intrinsics::copy;
345
346 #[stable(feature = "rust1", since = "1.0.0")]
347 #[doc(inline)]
348 pub use crate::intrinsics::write_bytes;
349
350 mod metadata;
351 pub(crate) use metadata::PtrRepr;
352 #[unstable(feature = "ptr_metadata", issue = "81513")]
353 pub use metadata::{from_raw_parts, from_raw_parts_mut, metadata, DynMetadata, Pointee, Thin};
354
355 mod non_null;
356 #[stable(feature = "nonnull", since = "1.25.0")]
357 pub use non_null::NonNull;
358
359 mod unique;
360 #[unstable(feature = "ptr_internals", issue = "none")]
361 pub use unique::Unique;
362
363 mod const_ptr;
364 mod mut_ptr;
365
366 /// Executes the destructor (if any) of the pointed-to value.
367 ///
368 /// This is semantically equivalent to calling [`ptr::read`] and discarding
369 /// the result, but has the following advantages:
370 ///
371 /// * It is *required* to use `drop_in_place` to drop unsized types like
372 ///   trait objects, because they can't be read out onto the stack and
373 ///   dropped normally.
374 ///
375 /// * It is friendlier to the optimizer to do this over [`ptr::read`] when
376 ///   dropping manually allocated memory (e.g., in the implementations of
377 ///   `Box`/`Rc`/`Vec`), as the compiler doesn't need to prove that it's
378 ///   sound to elide the copy.
379 ///
380 /// * It can be used to drop [pinned] data when `T` is not `repr(packed)`
381 ///   (pinned data must not be moved before it is dropped).
382 ///
383 /// Unaligned values cannot be dropped in place, they must be copied to an aligned
384 /// location first using [`ptr::read_unaligned`]. For packed structs, this move is
385 /// done automatically by the compiler. This means the fields of packed structs
386 /// are not dropped in-place.
387 ///
388 /// [`ptr::read`]: self::read
389 /// [`ptr::read_unaligned`]: self::read_unaligned
390 /// [pinned]: crate::pin
391 ///
392 /// # Safety
393 ///
394 /// Behavior is undefined if any of the following conditions are violated:
395 ///
396 /// * `to_drop` must be [valid] for both reads and writes.
397 ///
398 /// * `to_drop` must be properly aligned.
399 ///
400 /// * The value `to_drop` points to must be valid for dropping, which may mean it must uphold
401 ///   additional invariants - this is type-dependent.
402 ///
403 /// Additionally, if `T` is not [`Copy`], using the pointed-to value after
404 /// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
405 /// foo` counts as a use because it will cause the value to be dropped
406 /// again. [`write()`] can be used to overwrite data without causing it to be
407 /// dropped.
408 ///
409 /// Note that even if `T` has size `0`, the pointer must be non-null and properly aligned.
410 ///
411 /// [valid]: self#safety
412 ///
413 /// # Examples
414 ///
415 /// Manually remove the last item from a vector:
416 ///
417 /// ```
418 /// use std::ptr;
419 /// use std::rc::Rc;
420 ///
421 /// let last = Rc::new(1);
422 /// let weak = Rc::downgrade(&last);
423 ///
424 /// let mut v = vec![Rc::new(0), last];
425 ///
426 /// unsafe {
427 ///     // Get a raw pointer to the last element in `v`.
428 ///     let ptr = &mut v[1] as *mut _;
429 ///     // Shorten `v` to prevent the last item from being dropped. We do that first,
430 ///     // to prevent issues if the `drop_in_place` below panics.
431 ///     v.set_len(1);
432 ///     // Without a call `drop_in_place`, the last item would never be dropped,
433 ///     // and the memory it manages would be leaked.
434 ///     ptr::drop_in_place(ptr);
435 /// }
436 ///
437 /// assert_eq!(v, &[0.into()]);
438 ///
439 /// // Ensure that the last item was dropped.
440 /// assert!(weak.upgrade().is_none());
441 /// ```
442 #[stable(feature = "drop_in_place", since = "1.8.0")]
443 #[lang = "drop_in_place"]
444 #[allow(unconditional_recursion)]
445 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
446     // Code here does not matter - this is replaced by the
447     // real drop glue by the compiler.
448
449     // SAFETY: see comment above
450     unsafe { drop_in_place(to_drop) }
451 }
452
453 /// Creates a null raw pointer.
454 ///
455 /// # Examples
456 ///
457 /// ```
458 /// use std::ptr;
459 ///
460 /// let p: *const i32 = ptr::null();
461 /// assert!(p.is_null());
462 /// ```
463 #[inline(always)]
464 #[must_use]
465 #[stable(feature = "rust1", since = "1.0.0")]
466 #[rustc_promotable]
467 #[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
468 #[rustc_diagnostic_item = "ptr_null"]
469 pub const fn null<T>() -> *const T {
470     invalid(0)
471 }
472
473 /// Creates a null mutable raw pointer.
474 ///
475 /// # Examples
476 ///
477 /// ```
478 /// use std::ptr;
479 ///
480 /// let p: *mut i32 = ptr::null_mut();
481 /// assert!(p.is_null());
482 /// ```
483 #[inline(always)]
484 #[must_use]
485 #[stable(feature = "rust1", since = "1.0.0")]
486 #[rustc_promotable]
487 #[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
488 #[rustc_diagnostic_item = "ptr_null_mut"]
489 pub const fn null_mut<T>() -> *mut T {
490     invalid_mut(0)
491 }
492
493 /// Creates an invalid pointer with the given address.
494 ///
495 /// This is *currently* equivalent to `addr as *const T` but it expresses the intended semantic
496 /// more clearly, and may become important under future memory models.
497 ///
498 /// The module's top-level documentation discusses the precise meaning of an "invalid"
499 /// pointer but essentially this expresses that the pointer is not associated
500 /// with any actual allocation and is little more than a usize address in disguise.
501 ///
502 /// This pointer will have no provenance associated with it and is therefore
503 /// UB to read/write/offset. This mostly exists to facilitate things
504 /// like ptr::null and NonNull::dangling which make invalid pointers.
505 ///
506 /// (Standard "Zero-Sized-Types get to cheat and lie" caveats apply, although it
507 /// may be desirable to give them their own API just to make that 100% clear.)
508 ///
509 /// This API and its claimed semantics are part of the Strict Provenance experiment,
510 /// see the [module documentation][crate::ptr] for details.
511 #[inline(always)]
512 #[must_use]
513 #[rustc_const_stable(feature = "strict_provenance", since = "1.61.0")]
514 #[unstable(feature = "strict_provenance", issue = "95228")]
515 pub const fn invalid<T>(addr: usize) -> *const T {
516     // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
517     addr as *const T
518 }
519
520 /// Creates an invalid mutable pointer with the given address.
521 ///
522 /// This is *currently* equivalent to `addr as *mut T` but it expresses the intended semantic
523 /// more clearly, and may become important under future memory models.
524 ///
525 /// The module's top-level documentation discusses the precise meaning of an "invalid"
526 /// pointer but essentially this expresses that the pointer is not associated
527 /// with any actual allocation and is little more than a usize address in disguise.
528 ///
529 /// This pointer will have no provenance associated with it and is therefore
530 /// UB to read/write/offset. This mostly exists to facilitate things
531 /// like ptr::null and NonNull::dangling which make invalid pointers.
532 ///
533 /// (Standard "Zero-Sized-Types get to cheat and lie" caveats apply, although it
534 /// may be desirable to give them their own API just to make that 100% clear.)
535 ///
536 /// This API and its claimed semantics are part of the Strict Provenance experiment,
537 /// see the [module documentation][crate::ptr] for details.
538 #[inline(always)]
539 #[must_use]
540 #[rustc_const_stable(feature = "strict_provenance", since = "1.61.0")]
541 #[unstable(feature = "strict_provenance", issue = "95228")]
542 pub const fn invalid_mut<T>(addr: usize) -> *mut T {
543     // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
544     addr as *mut T
545 }
546
547 /// Forms a raw slice from a pointer and a length.
548 ///
549 /// The `len` argument is the number of **elements**, not the number of bytes.
550 ///
551 /// This function is safe, but actually using the return value is unsafe.
552 /// See the documentation of [`slice::from_raw_parts`] for slice safety requirements.
553 ///
554 /// [`slice::from_raw_parts`]: crate::slice::from_raw_parts
555 ///
556 /// # Examples
557 ///
558 /// ```rust
559 /// use std::ptr;
560 ///
561 /// // create a slice pointer when starting out with a pointer to the first element
562 /// let x = [5, 6, 7];
563 /// let raw_pointer = x.as_ptr();
564 /// let slice = ptr::slice_from_raw_parts(raw_pointer, 3);
565 /// assert_eq!(unsafe { &*slice }[2], 7);
566 /// ```
567 #[inline]
568 #[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
569 #[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
570 pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
571     from_raw_parts(data.cast(), len)
572 }
573
574 /// Performs the same functionality as [`slice_from_raw_parts`], except that a
575 /// raw mutable slice is returned, as opposed to a raw immutable slice.
576 ///
577 /// See the documentation of [`slice_from_raw_parts`] for more details.
578 ///
579 /// This function is safe, but actually using the return value is unsafe.
580 /// See the documentation of [`slice::from_raw_parts_mut`] for slice safety requirements.
581 ///
582 /// [`slice::from_raw_parts_mut`]: crate::slice::from_raw_parts_mut
583 ///
584 /// # Examples
585 ///
586 /// ```rust
587 /// use std::ptr;
588 ///
589 /// let x = &mut [5, 6, 7];
590 /// let raw_pointer = x.as_mut_ptr();
591 /// let slice = ptr::slice_from_raw_parts_mut(raw_pointer, 3);
592 ///
593 /// unsafe {
594 ///     (*slice)[2] = 99; // assign a value at an index in the slice
595 /// };
596 ///
597 /// assert_eq!(unsafe { &*slice }[2], 99);
598 /// ```
599 #[inline]
600 #[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
601 #[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
602 pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
603     from_raw_parts_mut(data.cast(), len)
604 }
605
606 /// Swaps the values at two mutable locations of the same type, without
607 /// deinitializing either.
608 ///
609 /// But for the following two exceptions, this function is semantically
610 /// equivalent to [`mem::swap`]:
611 ///
612 /// * It operates on raw pointers instead of references. When references are
613 ///   available, [`mem::swap`] should be preferred.
614 ///
615 /// * The two pointed-to values may overlap. If the values do overlap, then the
616 ///   overlapping region of memory from `x` will be used. This is demonstrated
617 ///   in the second example below.
618 ///
619 /// # Safety
620 ///
621 /// Behavior is undefined if any of the following conditions are violated:
622 ///
623 /// * Both `x` and `y` must be [valid] for both reads and writes.
624 ///
625 /// * Both `x` and `y` must be properly aligned.
626 ///
627 /// Note that even if `T` has size `0`, the pointers must be non-null and properly aligned.
628 ///
629 /// [valid]: self#safety
630 ///
631 /// # Examples
632 ///
633 /// Swapping two non-overlapping regions:
634 ///
635 /// ```
636 /// use std::ptr;
637 ///
638 /// let mut array = [0, 1, 2, 3];
639 ///
640 /// let x = array[0..].as_mut_ptr() as *mut [u32; 2]; // this is `array[0..2]`
641 /// let y = array[2..].as_mut_ptr() as *mut [u32; 2]; // this is `array[2..4]`
642 ///
643 /// unsafe {
644 ///     ptr::swap(x, y);
645 ///     assert_eq!([2, 3, 0, 1], array);
646 /// }
647 /// ```
648 ///
649 /// Swapping two overlapping regions:
650 ///
651 /// ```
652 /// use std::ptr;
653 ///
654 /// let mut array: [i32; 4] = [0, 1, 2, 3];
655 ///
656 /// let array_ptr: *mut i32 = array.as_mut_ptr();
657 ///
658 /// let x = array_ptr as *mut [i32; 3]; // this is `array[0..3]`
659 /// let y = unsafe { array_ptr.add(1) } as *mut [i32; 3]; // this is `array[1..4]`
660 ///
661 /// unsafe {
662 ///     ptr::swap(x, y);
663 ///     // The indices `1..3` of the slice overlap between `x` and `y`.
664 ///     // Reasonable results would be for to them be `[2, 3]`, so that indices `0..3` are
665 ///     // `[1, 2, 3]` (matching `y` before the `swap`); or for them to be `[0, 1]`
666 ///     // so that indices `1..4` are `[0, 1, 2]` (matching `x` before the `swap`).
667 ///     // This implementation is defined to make the latter choice.
668 ///     assert_eq!([1, 0, 1, 2], array);
669 /// }
670 /// ```
671 #[inline]
672 #[stable(feature = "rust1", since = "1.0.0")]
673 #[rustc_const_unstable(feature = "const_swap", issue = "83163")]
674 pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
675     // Give ourselves some scratch space to work with.
676     // We do not have to worry about drops: `MaybeUninit` does nothing when dropped.
677     let mut tmp = MaybeUninit::<T>::uninit();
678
679     // Perform the swap
680     // SAFETY: the caller must guarantee that `x` and `y` are
681     // valid for writes and properly aligned. `tmp` cannot be
682     // overlapping either `x` or `y` because `tmp` was just allocated
683     // on the stack as a separate allocated object.
684     unsafe {
685         copy_nonoverlapping(x, tmp.as_mut_ptr(), 1);
686         copy(y, x, 1); // `x` and `y` may overlap
687         copy_nonoverlapping(tmp.as_ptr(), y, 1);
688     }
689 }
690
691 /// Swaps `count * size_of::<T>()` bytes between the two regions of memory
692 /// beginning at `x` and `y`. The two regions must *not* overlap.
693 ///
694 /// # Safety
695 ///
696 /// Behavior is undefined if any of the following conditions are violated:
697 ///
698 /// * Both `x` and `y` must be [valid] for both reads and writes of `count *
699 ///   size_of::<T>()` bytes.
700 ///
701 /// * Both `x` and `y` must be properly aligned.
702 ///
703 /// * The region of memory beginning at `x` with a size of `count *
704 ///   size_of::<T>()` bytes must *not* overlap with the region of memory
705 ///   beginning at `y` with the same size.
706 ///
707 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is `0`,
708 /// the pointers must be non-null and properly aligned.
709 ///
710 /// [valid]: self#safety
711 ///
712 /// # Examples
713 ///
714 /// Basic usage:
715 ///
716 /// ```
717 /// use std::ptr;
718 ///
719 /// let mut x = [1, 2, 3, 4];
720 /// let mut y = [7, 8, 9];
721 ///
722 /// unsafe {
723 ///     ptr::swap_nonoverlapping(x.as_mut_ptr(), y.as_mut_ptr(), 2);
724 /// }
725 ///
726 /// assert_eq!(x, [7, 8, 3, 4]);
727 /// assert_eq!(y, [1, 2, 9]);
728 /// ```
729 #[inline]
730 #[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
731 #[rustc_const_unstable(feature = "const_swap", issue = "83163")]
732 pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
733     #[allow(unused)]
734     macro_rules! attempt_swap_as_chunks {
735         ($ChunkTy:ty) => {
736             if mem::align_of::<T>() >= mem::align_of::<$ChunkTy>()
737                 && mem::size_of::<T>() % mem::size_of::<$ChunkTy>() == 0
738             {
739                 let x: *mut MaybeUninit<$ChunkTy> = x.cast();
740                 let y: *mut MaybeUninit<$ChunkTy> = y.cast();
741                 let count = count * (mem::size_of::<T>() / mem::size_of::<$ChunkTy>());
742                 // SAFETY: these are the same bytes that the caller promised were
743                 // ok, just typed as `MaybeUninit<ChunkTy>`s instead of as `T`s.
744                 // The `if` condition above ensures that we're not violating
745                 // alignment requirements, and that the division is exact so
746                 // that we don't lose any bytes off the end.
747                 return unsafe { swap_nonoverlapping_simple(x, y, count) };
748             }
749         };
750     }
751
752     // NOTE(scottmcm) MIRI is disabled here as reading in smaller units is a
753     // pessimization for it.  Also, if the type contains any unaligned pointers,
754     // copying those over multiple reads is difficult to support.
755     #[cfg(not(miri))]
756     {
757         // Split up the slice into small power-of-two-sized chunks that LLVM is able
758         // to vectorize (unless it's a special type with more-than-pointer alignment,
759         // because we don't want to pessimize things like slices of SIMD vectors.)
760         if mem::align_of::<T>() <= mem::size_of::<usize>()
761             && (!mem::size_of::<T>().is_power_of_two()
762                 || mem::size_of::<T>() > mem::size_of::<usize>() * 2)
763         {
764             attempt_swap_as_chunks!(usize);
765             attempt_swap_as_chunks!(u8);
766         }
767     }
768
769     // SAFETY: Same preconditions as this function
770     unsafe { swap_nonoverlapping_simple(x, y, count) }
771 }
772
773 /// Same behaviour and safety conditions as [`swap_nonoverlapping`]
774 ///
775 /// LLVM can vectorize this (at least it can for the power-of-two-sized types
776 /// `swap_nonoverlapping` tries to use) so no need to manually SIMD it.
777 #[inline]
778 #[rustc_const_unstable(feature = "const_swap", issue = "83163")]
779 const unsafe fn swap_nonoverlapping_simple<T>(x: *mut T, y: *mut T, count: usize) {
780     let mut i = 0;
781     while i < count {
782         let x: &mut T =
783             // SAFETY: By precondition, `i` is in-bounds because it's below `n`
784             unsafe { &mut *x.add(i) };
785         let y: &mut T =
786             // SAFETY: By precondition, `i` is in-bounds because it's below `n`
787             // and it's distinct from `x` since the ranges are non-overlapping
788             unsafe { &mut *y.add(i) };
789         mem::swap_simple(x, y);
790
791         i += 1;
792     }
793 }
794
795 /// Moves `src` into the pointed `dst`, returning the previous `dst` value.
796 ///
797 /// Neither value is dropped.
798 ///
799 /// This function is semantically equivalent to [`mem::replace`] except that it
800 /// operates on raw pointers instead of references. When references are
801 /// available, [`mem::replace`] should be preferred.
802 ///
803 /// # Safety
804 ///
805 /// Behavior is undefined if any of the following conditions are violated:
806 ///
807 /// * `dst` must be [valid] for both reads and writes.
808 ///
809 /// * `dst` must be properly aligned.
810 ///
811 /// * `dst` must point to a properly initialized value of type `T`.
812 ///
813 /// Note that even if `T` has size `0`, the pointer must be non-null and properly aligned.
814 ///
815 /// [valid]: self#safety
816 ///
817 /// # Examples
818 ///
819 /// ```
820 /// use std::ptr;
821 ///
822 /// let mut rust = vec!['b', 'u', 's', 't'];
823 ///
824 /// // `mem::replace` would have the same effect without requiring the unsafe
825 /// // block.
826 /// let b = unsafe {
827 ///     ptr::replace(&mut rust[0], 'r')
828 /// };
829 ///
830 /// assert_eq!(b, 'b');
831 /// assert_eq!(rust, &['r', 'u', 's', 't']);
832 /// ```
833 #[inline]
834 #[stable(feature = "rust1", since = "1.0.0")]
835 #[rustc_const_unstable(feature = "const_replace", issue = "83164")]
836 pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
837     // SAFETY: the caller must guarantee that `dst` is valid to be
838     // cast to a mutable reference (valid for writes, aligned, initialized),
839     // and cannot overlap `src` since `dst` must point to a distinct
840     // allocated object.
841     unsafe {
842         mem::swap(&mut *dst, &mut src); // cannot overlap
843     }
844     src
845 }
846
847 /// Reads the value from `src` without moving it. This leaves the
848 /// memory in `src` unchanged.
849 ///
850 /// # Safety
851 ///
852 /// Behavior is undefined if any of the following conditions are violated:
853 ///
854 /// * `src` must be [valid] for reads.
855 ///
856 /// * `src` must be properly aligned. Use [`read_unaligned`] if this is not the
857 ///   case.
858 ///
859 /// * `src` must point to a properly initialized value of type `T`.
860 ///
861 /// Note that even if `T` has size `0`, the pointer must be non-null and properly aligned.
862 ///
863 /// # Examples
864 ///
865 /// Basic usage:
866 ///
867 /// ```
868 /// let x = 12;
869 /// let y = &x as *const i32;
870 ///
871 /// unsafe {
872 ///     assert_eq!(std::ptr::read(y), 12);
873 /// }
874 /// ```
875 ///
876 /// Manually implement [`mem::swap`]:
877 ///
878 /// ```
879 /// use std::ptr;
880 ///
881 /// fn swap<T>(a: &mut T, b: &mut T) {
882 ///     unsafe {
883 ///         // Create a bitwise copy of the value at `a` in `tmp`.
884 ///         let tmp = ptr::read(a);
885 ///
886 ///         // Exiting at this point (either by explicitly returning or by
887 ///         // calling a function which panics) would cause the value in `tmp` to
888 ///         // be dropped while the same value is still referenced by `a`. This
889 ///         // could trigger undefined behavior if `T` is not `Copy`.
890 ///
891 ///         // Create a bitwise copy of the value at `b` in `a`.
892 ///         // This is safe because mutable references cannot alias.
893 ///         ptr::copy_nonoverlapping(b, a, 1);
894 ///
895 ///         // As above, exiting here could trigger undefined behavior because
896 ///         // the same value is referenced by `a` and `b`.
897 ///
898 ///         // Move `tmp` into `b`.
899 ///         ptr::write(b, tmp);
900 ///
901 ///         // `tmp` has been moved (`write` takes ownership of its second argument),
902 ///         // so nothing is dropped implicitly here.
903 ///     }
904 /// }
905 ///
906 /// let mut foo = "foo".to_owned();
907 /// let mut bar = "bar".to_owned();
908 ///
909 /// swap(&mut foo, &mut bar);
910 ///
911 /// assert_eq!(foo, "bar");
912 /// assert_eq!(bar, "foo");
913 /// ```
914 ///
915 /// ## Ownership of the Returned Value
916 ///
917 /// `read` creates a bitwise copy of `T`, regardless of whether `T` is [`Copy`].
918 /// If `T` is not [`Copy`], using both the returned value and the value at
919 /// `*src` can violate memory safety. Note that assigning to `*src` counts as a
920 /// use because it will attempt to drop the value at `*src`.
921 ///
922 /// [`write()`] can be used to overwrite data without causing it to be dropped.
923 ///
924 /// ```
925 /// use std::ptr;
926 ///
927 /// let mut s = String::from("foo");
928 /// unsafe {
929 ///     // `s2` now points to the same underlying memory as `s`.
930 ///     let mut s2: String = ptr::read(&s);
931 ///
932 ///     assert_eq!(s2, "foo");
933 ///
934 ///     // Assigning to `s2` causes its original value to be dropped. Beyond
935 ///     // this point, `s` must no longer be used, as the underlying memory has
936 ///     // been freed.
937 ///     s2 = String::default();
938 ///     assert_eq!(s2, "");
939 ///
940 ///     // Assigning to `s` would cause the old value to be dropped again,
941 ///     // resulting in undefined behavior.
942 ///     // s = String::from("bar"); // ERROR
943 ///
944 ///     // `ptr::write` can be used to overwrite a value without dropping it.
945 ///     ptr::write(&mut s, String::from("bar"));
946 /// }
947 ///
948 /// assert_eq!(s, "bar");
949 /// ```
950 ///
951 /// [valid]: self#safety
952 #[inline]
953 #[stable(feature = "rust1", since = "1.0.0")]
954 #[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
955 pub const unsafe fn read<T>(src: *const T) -> T {
956     // We are calling the intrinsics directly to avoid function calls in the generated code
957     // as `intrinsics::copy_nonoverlapping` is a wrapper function.
958     extern "rust-intrinsic" {
959         #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
960         fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
961     }
962
963     let mut tmp = MaybeUninit::<T>::uninit();
964     // SAFETY: the caller must guarantee that `src` is valid for reads.
965     // `src` cannot overlap `tmp` because `tmp` was just allocated on
966     // the stack as a separate allocated object.
967     //
968     // Also, since we just wrote a valid value into `tmp`, it is guaranteed
969     // to be properly initialized.
970     unsafe {
971         copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
972         tmp.assume_init()
973     }
974 }
975
976 /// Reads the value from `src` without moving it. This leaves the
977 /// memory in `src` unchanged.
978 ///
979 /// Unlike [`read`], `read_unaligned` works with unaligned pointers.
980 ///
981 /// # Safety
982 ///
983 /// Behavior is undefined if any of the following conditions are violated:
984 ///
985 /// * `src` must be [valid] for reads.
986 ///
987 /// * `src` must point to a properly initialized value of type `T`.
988 ///
989 /// Like [`read`], `read_unaligned` creates a bitwise copy of `T`, regardless of
990 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
991 /// value and the value at `*src` can [violate memory safety][read-ownership].
992 ///
993 /// Note that even if `T` has size `0`, the pointer must be non-null.
994 ///
995 /// [read-ownership]: read#ownership-of-the-returned-value
996 /// [valid]: self#safety
997 ///
998 /// ## On `packed` structs
999 ///
1000 /// Attempting to create a raw pointer to an `unaligned` struct field with
1001 /// an expression such as `&packed.unaligned as *const FieldType` creates an
1002 /// intermediate unaligned reference before converting that to a raw pointer.
1003 /// That this reference is temporary and immediately cast is inconsequential
1004 /// as the compiler always expects references to be properly aligned.
1005 /// As a result, using `&packed.unaligned as *const FieldType` causes immediate
1006 /// *undefined behavior* in your program.
1007 ///
1008 /// Instead you must use the [`ptr::addr_of!`](addr_of) macro to
1009 /// create the pointer. You may use that returned pointer together with this
1010 /// function.
1011 ///
1012 /// An example of what not to do and how this relates to `read_unaligned` is:
1013 ///
1014 /// ```
1015 /// #[repr(packed, C)]
1016 /// struct Packed {
1017 ///     _padding: u8,
1018 ///     unaligned: u32,
1019 /// }
1020 ///
1021 /// let packed = Packed {
1022 ///     _padding: 0x00,
1023 ///     unaligned: 0x01020304,
1024 /// };
1025 ///
1026 /// // Take the address of a 32-bit integer which is not aligned.
1027 /// // In contrast to `&packed.unaligned as *const _`, this has no undefined behavior.
1028 /// let unaligned = std::ptr::addr_of!(packed.unaligned);
1029 ///
1030 /// let v = unsafe { std::ptr::read_unaligned(unaligned) };
1031 /// assert_eq!(v, 0x01020304);
1032 /// ```
1033 ///
1034 /// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however.
1035 ///
1036 /// # Examples
1037 ///
1038 /// Read a usize value from a byte buffer:
1039 ///
1040 /// ```
1041 /// use std::mem;
1042 ///
1043 /// fn read_usize(x: &[u8]) -> usize {
1044 ///     assert!(x.len() >= mem::size_of::<usize>());
1045 ///
1046 ///     let ptr = x.as_ptr() as *const usize;
1047 ///
1048 ///     unsafe { ptr.read_unaligned() }
1049 /// }
1050 /// ```
1051 #[inline]
1052 #[stable(feature = "ptr_unaligned", since = "1.17.0")]
1053 #[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1054 pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
1055     let mut tmp = MaybeUninit::<T>::uninit();
1056     // SAFETY: the caller must guarantee that `src` is valid for reads.
1057     // `src` cannot overlap `tmp` because `tmp` was just allocated on
1058     // the stack as a separate allocated object.
1059     //
1060     // Also, since we just wrote a valid value into `tmp`, it is guaranteed
1061     // to be properly initialized.
1062     unsafe {
1063         copy_nonoverlapping(src as *const u8, tmp.as_mut_ptr() as *mut u8, mem::size_of::<T>());
1064         tmp.assume_init()
1065     }
1066 }
1067
1068 /// Overwrites a memory location with the given value without reading or
1069 /// dropping the old value.
1070 ///
1071 /// `write` does not drop the contents of `dst`. This is safe, but it could leak
1072 /// allocations or resources, so care should be taken not to overwrite an object
1073 /// that should be dropped.
1074 ///
1075 /// Additionally, it does not drop `src`. Semantically, `src` is moved into the
1076 /// location pointed to by `dst`.
1077 ///
1078 /// This is appropriate for initializing uninitialized memory, or overwriting
1079 /// memory that has previously been [`read`] from.
1080 ///
1081 /// # Safety
1082 ///
1083 /// Behavior is undefined if any of the following conditions are violated:
1084 ///
1085 /// * `dst` must be [valid] for writes.
1086 ///
1087 /// * `dst` must be properly aligned. Use [`write_unaligned`] if this is not the
1088 ///   case.
1089 ///
1090 /// Note that even if `T` has size `0`, the pointer must be non-null and properly aligned.
1091 ///
1092 /// [valid]: self#safety
1093 ///
1094 /// # Examples
1095 ///
1096 /// Basic usage:
1097 ///
1098 /// ```
1099 /// let mut x = 0;
1100 /// let y = &mut x as *mut i32;
1101 /// let z = 12;
1102 ///
1103 /// unsafe {
1104 ///     std::ptr::write(y, z);
1105 ///     assert_eq!(std::ptr::read(y), 12);
1106 /// }
1107 /// ```
1108 ///
1109 /// Manually implement [`mem::swap`]:
1110 ///
1111 /// ```
1112 /// use std::ptr;
1113 ///
1114 /// fn swap<T>(a: &mut T, b: &mut T) {
1115 ///     unsafe {
1116 ///         // Create a bitwise copy of the value at `a` in `tmp`.
1117 ///         let tmp = ptr::read(a);
1118 ///
1119 ///         // Exiting at this point (either by explicitly returning or by
1120 ///         // calling a function which panics) would cause the value in `tmp` to
1121 ///         // be dropped while the same value is still referenced by `a`. This
1122 ///         // could trigger undefined behavior if `T` is not `Copy`.
1123 ///
1124 ///         // Create a bitwise copy of the value at `b` in `a`.
1125 ///         // This is safe because mutable references cannot alias.
1126 ///         ptr::copy_nonoverlapping(b, a, 1);
1127 ///
1128 ///         // As above, exiting here could trigger undefined behavior because
1129 ///         // the same value is referenced by `a` and `b`.
1130 ///
1131 ///         // Move `tmp` into `b`.
1132 ///         ptr::write(b, tmp);
1133 ///
1134 ///         // `tmp` has been moved (`write` takes ownership of its second argument),
1135 ///         // so nothing is dropped implicitly here.
1136 ///     }
1137 /// }
1138 ///
1139 /// let mut foo = "foo".to_owned();
1140 /// let mut bar = "bar".to_owned();
1141 ///
1142 /// swap(&mut foo, &mut bar);
1143 ///
1144 /// assert_eq!(foo, "bar");
1145 /// assert_eq!(bar, "foo");
1146 /// ```
1147 #[inline]
1148 #[stable(feature = "rust1", since = "1.0.0")]
1149 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1150 pub const unsafe fn write<T>(dst: *mut T, src: T) {
1151     // We are calling the intrinsics directly to avoid function calls in the generated code
1152     // as `intrinsics::copy_nonoverlapping` is a wrapper function.
1153     extern "rust-intrinsic" {
1154         #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
1155         fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1156     }
1157
1158     // SAFETY: the caller must guarantee that `dst` is valid for writes.
1159     // `dst` cannot overlap `src` because the caller has mutable access
1160     // to `dst` while `src` is owned by this function.
1161     unsafe {
1162         copy_nonoverlapping(&src as *const T, dst, 1);
1163         intrinsics::forget(src);
1164     }
1165 }
1166
1167 /// Overwrites a memory location with the given value without reading or
1168 /// dropping the old value.
1169 ///
1170 /// Unlike [`write()`], the pointer may be unaligned.
1171 ///
1172 /// `write_unaligned` does not drop the contents of `dst`. This is safe, but it
1173 /// could leak allocations or resources, so care should be taken not to overwrite
1174 /// an object that should be dropped.
1175 ///
1176 /// Additionally, it does not drop `src`. Semantically, `src` is moved into the
1177 /// location pointed to by `dst`.
1178 ///
1179 /// This is appropriate for initializing uninitialized memory, or overwriting
1180 /// memory that has previously been read with [`read_unaligned`].
1181 ///
1182 /// # Safety
1183 ///
1184 /// Behavior is undefined if any of the following conditions are violated:
1185 ///
1186 /// * `dst` must be [valid] for writes.
1187 ///
1188 /// Note that even if `T` has size `0`, the pointer must be non-null.
1189 ///
1190 /// [valid]: self#safety
1191 ///
1192 /// ## On `packed` structs
1193 ///
1194 /// Attempting to create a raw pointer to an `unaligned` struct field with
1195 /// an expression such as `&packed.unaligned as *const FieldType` creates an
1196 /// intermediate unaligned reference before converting that to a raw pointer.
1197 /// That this reference is temporary and immediately cast is inconsequential
1198 /// as the compiler always expects references to be properly aligned.
1199 /// As a result, using `&packed.unaligned as *const FieldType` causes immediate
1200 /// *undefined behavior* in your program.
1201 ///
1202 /// Instead you must use the [`ptr::addr_of_mut!`](addr_of_mut)
1203 /// macro to create the pointer. You may use that returned pointer together with
1204 /// this function.
1205 ///
1206 /// An example of how to do it and how this relates to `write_unaligned` is:
1207 ///
1208 /// ```
1209 /// #[repr(packed, C)]
1210 /// struct Packed {
1211 ///     _padding: u8,
1212 ///     unaligned: u32,
1213 /// }
1214 ///
1215 /// let mut packed: Packed = unsafe { std::mem::zeroed() };
1216 ///
1217 /// // Take the address of a 32-bit integer which is not aligned.
1218 /// // In contrast to `&packed.unaligned as *mut _`, this has no undefined behavior.
1219 /// let unaligned = std::ptr::addr_of_mut!(packed.unaligned);
1220 ///
1221 /// unsafe { std::ptr::write_unaligned(unaligned, 42) };
1222 ///
1223 /// assert_eq!({packed.unaligned}, 42); // `{...}` forces copying the field instead of creating a reference.
1224 /// ```
1225 ///
1226 /// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however
1227 /// (as can be seen in the `assert_eq!` above).
1228 ///
1229 /// # Examples
1230 ///
1231 /// Write a usize value to a byte buffer:
1232 ///
1233 /// ```
1234 /// use std::mem;
1235 ///
1236 /// fn write_usize(x: &mut [u8], val: usize) {
1237 ///     assert!(x.len() >= mem::size_of::<usize>());
1238 ///
1239 ///     let ptr = x.as_mut_ptr() as *mut usize;
1240 ///
1241 ///     unsafe { ptr.write_unaligned(val) }
1242 /// }
1243 /// ```
1244 #[inline]
1245 #[stable(feature = "ptr_unaligned", since = "1.17.0")]
1246 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1247 pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
1248     // SAFETY: the caller must guarantee that `dst` is valid for writes.
1249     // `dst` cannot overlap `src` because the caller has mutable access
1250     // to `dst` while `src` is owned by this function.
1251     unsafe {
1252         copy_nonoverlapping(&src as *const T as *const u8, dst as *mut u8, mem::size_of::<T>());
1253         // We are calling the intrinsic directly to avoid function calls in the generated code.
1254         intrinsics::forget(src);
1255     }
1256 }
1257
1258 /// Performs a volatile read of the value from `src` without moving it. This
1259 /// leaves the memory in `src` unchanged.
1260 ///
1261 /// Volatile operations are intended to act on I/O memory, and are guaranteed
1262 /// to not be elided or reordered by the compiler across other volatile
1263 /// operations.
1264 ///
1265 /// # Notes
1266 ///
1267 /// Rust does not currently have a rigorously and formally defined memory model,
1268 /// so the precise semantics of what "volatile" means here is subject to change
1269 /// over time. That being said, the semantics will almost always end up pretty
1270 /// similar to [C11's definition of volatile][c11].
1271 ///
1272 /// The compiler shouldn't change the relative order or number of volatile
1273 /// memory operations. However, volatile memory operations on zero-sized types
1274 /// (e.g., if a zero-sized type is passed to `read_volatile`) are noops
1275 /// and may be ignored.
1276 ///
1277 /// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
1278 ///
1279 /// # Safety
1280 ///
1281 /// Behavior is undefined if any of the following conditions are violated:
1282 ///
1283 /// * `src` must be [valid] for reads.
1284 ///
1285 /// * `src` must be properly aligned.
1286 ///
1287 /// * `src` must point to a properly initialized value of type `T`.
1288 ///
1289 /// Like [`read`], `read_volatile` creates a bitwise copy of `T`, regardless of
1290 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
1291 /// value and the value at `*src` can [violate memory safety][read-ownership].
1292 /// However, storing non-[`Copy`] types in volatile memory is almost certainly
1293 /// incorrect.
1294 ///
1295 /// Note that even if `T` has size `0`, the pointer must be non-null and properly aligned.
1296 ///
1297 /// [valid]: self#safety
1298 /// [read-ownership]: read#ownership-of-the-returned-value
1299 ///
1300 /// Just like in C, whether an operation is volatile has no bearing whatsoever
1301 /// on questions involving concurrent access from multiple threads. Volatile
1302 /// accesses behave exactly like non-atomic accesses in that regard. In particular,
1303 /// a race between a `read_volatile` and any write operation to the same location
1304 /// is undefined behavior.
1305 ///
1306 /// # Examples
1307 ///
1308 /// Basic usage:
1309 ///
1310 /// ```
1311 /// let x = 12;
1312 /// let y = &x as *const i32;
1313 ///
1314 /// unsafe {
1315 ///     assert_eq!(std::ptr::read_volatile(y), 12);
1316 /// }
1317 /// ```
1318 #[inline]
1319 #[stable(feature = "volatile", since = "1.9.0")]
1320 pub unsafe fn read_volatile<T>(src: *const T) -> T {
1321     if cfg!(debug_assertions) && !is_aligned_and_not_null(src) {
1322         // Not panicking to keep codegen impact smaller.
1323         abort();
1324     }
1325     // SAFETY: the caller must uphold the safety contract for `volatile_load`.
1326     unsafe { intrinsics::volatile_load(src) }
1327 }
1328
1329 /// Performs a volatile write of a memory location with the given value without
1330 /// reading or dropping the old value.
1331 ///
1332 /// Volatile operations are intended to act on I/O memory, and are guaranteed
1333 /// to not be elided or reordered by the compiler across other volatile
1334 /// operations.
1335 ///
1336 /// `write_volatile` does not drop the contents of `dst`. This is safe, but it
1337 /// could leak allocations or resources, so care should be taken not to overwrite
1338 /// an object that should be dropped.
1339 ///
1340 /// Additionally, it does not drop `src`. Semantically, `src` is moved into the
1341 /// location pointed to by `dst`.
1342 ///
1343 /// # Notes
1344 ///
1345 /// Rust does not currently have a rigorously and formally defined memory model,
1346 /// so the precise semantics of what "volatile" means here is subject to change
1347 /// over time. That being said, the semantics will almost always end up pretty
1348 /// similar to [C11's definition of volatile][c11].
1349 ///
1350 /// The compiler shouldn't change the relative order or number of volatile
1351 /// memory operations. However, volatile memory operations on zero-sized types
1352 /// (e.g., if a zero-sized type is passed to `write_volatile`) are noops
1353 /// and may be ignored.
1354 ///
1355 /// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
1356 ///
1357 /// # Safety
1358 ///
1359 /// Behavior is undefined if any of the following conditions are violated:
1360 ///
1361 /// * `dst` must be [valid] for writes.
1362 ///
1363 /// * `dst` must be properly aligned.
1364 ///
1365 /// Note that even if `T` has size `0`, the pointer must be non-null and properly aligned.
1366 ///
1367 /// [valid]: self#safety
1368 ///
1369 /// Just like in C, whether an operation is volatile has no bearing whatsoever
1370 /// on questions involving concurrent access from multiple threads. Volatile
1371 /// accesses behave exactly like non-atomic accesses in that regard. In particular,
1372 /// a race between a `write_volatile` and any other operation (reading or writing)
1373 /// on the same location is undefined behavior.
1374 ///
1375 /// # Examples
1376 ///
1377 /// Basic usage:
1378 ///
1379 /// ```
1380 /// let mut x = 0;
1381 /// let y = &mut x as *mut i32;
1382 /// let z = 12;
1383 ///
1384 /// unsafe {
1385 ///     std::ptr::write_volatile(y, z);
1386 ///     assert_eq!(std::ptr::read_volatile(y), 12);
1387 /// }
1388 /// ```
1389 #[inline]
1390 #[stable(feature = "volatile", since = "1.9.0")]
1391 pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
1392     if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) {
1393         // Not panicking to keep codegen impact smaller.
1394         abort();
1395     }
1396     // SAFETY: the caller must uphold the safety contract for `volatile_store`.
1397     unsafe {
1398         intrinsics::volatile_store(dst, src);
1399     }
1400 }
1401
1402 /// Align pointer `p`.
1403 ///
1404 /// Calculate offset (in terms of elements of `stride` stride) that has to be applied
1405 /// to pointer `p` so that pointer `p` would get aligned to `a`.
1406 ///
1407 /// Note: This implementation has been carefully tailored to not panic. It is UB for this to panic.
1408 /// The only real change that can be made here is change of `INV_TABLE_MOD_16` and associated
1409 /// constants.
1410 ///
1411 /// If we ever decide to make it possible to call the intrinsic with `a` that is not a
1412 /// power-of-two, it will probably be more prudent to just change to a naive implementation rather
1413 /// than trying to adapt this to accommodate that change.
1414 ///
1415 /// Any questions go to @nagisa.
1416 #[lang = "align_offset"]
1417 pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
1418     // FIXME(#75598): Direct use of these intrinsics improves codegen significantly at opt-level <=
1419     // 1, where the method versions of these operations are not inlined.
1420     use intrinsics::{
1421         unchecked_shl, unchecked_shr, unchecked_sub, wrapping_add, wrapping_mul, wrapping_sub,
1422     };
1423
1424     let addr = p.addr();
1425
1426     /// Calculate multiplicative modular inverse of `x` modulo `m`.
1427     ///
1428     /// This implementation is tailored for `align_offset` and has following preconditions:
1429     ///
1430     /// * `m` is a power-of-two;
1431     /// * `x < m`; (if `x ≥ m`, pass in `x % m` instead)
1432     ///
1433     /// Implementation of this function shall not panic. Ever.
1434     #[inline]
1435     unsafe fn mod_inv(x: usize, m: usize) -> usize {
1436         /// Multiplicative modular inverse table modulo 2⁴ = 16.
1437         ///
1438         /// Note, that this table does not contain values where inverse does not exist (i.e., for
1439         /// `0⁻¹ mod 16`, `2⁻¹ mod 16`, etc.)
1440         const INV_TABLE_MOD_16: [u8; 8] = [1, 11, 13, 7, 9, 3, 5, 15];
1441         /// Modulo for which the `INV_TABLE_MOD_16` is intended.
1442         const INV_TABLE_MOD: usize = 16;
1443         /// INV_TABLE_MOD²
1444         const INV_TABLE_MOD_SQUARED: usize = INV_TABLE_MOD * INV_TABLE_MOD;
1445
1446         let table_inverse = INV_TABLE_MOD_16[(x & (INV_TABLE_MOD - 1)) >> 1] as usize;
1447         // SAFETY: `m` is required to be a power-of-two, hence non-zero.
1448         let m_minus_one = unsafe { unchecked_sub(m, 1) };
1449         if m <= INV_TABLE_MOD {
1450             table_inverse & m_minus_one
1451         } else {
1452             // We iterate "up" using the following formula:
1453             //
1454             // $$ xy ≡ 1 (mod 2ⁿ) → xy (2 - xy) ≡ 1 (mod 2²ⁿ) $$
1455             //
1456             // until 2²ⁿ ≥ m. Then we can reduce to our desired `m` by taking the result `mod m`.
1457             let mut inverse = table_inverse;
1458             let mut going_mod = INV_TABLE_MOD_SQUARED;
1459             loop {
1460                 // y = y * (2 - xy) mod n
1461                 //
1462                 // Note, that we use wrapping operations here intentionally – the original formula
1463                 // uses e.g., subtraction `mod n`. It is entirely fine to do them `mod
1464                 // usize::MAX` instead, because we take the result `mod n` at the end
1465                 // anyway.
1466                 inverse = wrapping_mul(inverse, wrapping_sub(2usize, wrapping_mul(x, inverse)));
1467                 if going_mod >= m {
1468                     return inverse & m_minus_one;
1469                 }
1470                 going_mod = wrapping_mul(going_mod, going_mod);
1471             }
1472         }
1473     }
1474
1475     let stride = mem::size_of::<T>();
1476     // SAFETY: `a` is a power-of-two, therefore non-zero.
1477     let a_minus_one = unsafe { unchecked_sub(a, 1) };
1478     if stride == 1 {
1479         // `stride == 1` case can be computed more simply through `-p (mod a)`, but doing so
1480         // inhibits LLVM's ability to select instructions like `lea`. Instead we compute
1481         //
1482         //    round_up_to_next_alignment(p, a) - p
1483         //
1484         // which distributes operations around the load-bearing, but pessimizing `and` sufficiently
1485         // for LLVM to be able to utilize the various optimizations it knows about.
1486         return wrapping_sub(wrapping_add(addr, a_minus_one) & wrapping_sub(0, a), addr);
1487     }
1488
1489     let pmoda = addr & a_minus_one;
1490     if pmoda == 0 {
1491         // Already aligned. Yay!
1492         return 0;
1493     } else if stride == 0 {
1494         // If the pointer is not aligned, and the element is zero-sized, then no amount of
1495         // elements will ever align the pointer.
1496         return usize::MAX;
1497     }
1498
1499     let smoda = stride & a_minus_one;
1500     // SAFETY: a is power-of-two hence non-zero. stride == 0 case is handled above.
1501     let gcdpow = unsafe { intrinsics::cttz_nonzero(stride).min(intrinsics::cttz_nonzero(a)) };
1502     // SAFETY: gcdpow has an upper-bound that’s at most the number of bits in a usize.
1503     let gcd = unsafe { unchecked_shl(1usize, gcdpow) };
1504
1505     // SAFETY: gcd is always greater or equal to 1.
1506     if addr & unsafe { unchecked_sub(gcd, 1) } == 0 {
1507         // This branch solves for the following linear congruence equation:
1508         //
1509         // ` p + so = 0 mod a `
1510         //
1511         // `p` here is the pointer value, `s` - stride of `T`, `o` offset in `T`s, and `a` - the
1512         // requested alignment.
1513         //
1514         // With `g = gcd(a, s)`, and the above condition asserting that `p` is also divisible by
1515         // `g`, we can denote `a' = a/g`, `s' = s/g`, `p' = p/g`, then this becomes equivalent to:
1516         //
1517         // ` p' + s'o = 0 mod a' `
1518         // ` o = (a' - (p' mod a')) * (s'^-1 mod a') `
1519         //
1520         // The first term is "the relative alignment of `p` to `a`" (divided by the `g`), the second
1521         // term is "how does incrementing `p` by `s` bytes change the relative alignment of `p`" (again
1522         // divided by `g`).
1523         // Division by `g` is necessary to make the inverse well formed if `a` and `s` are not
1524         // co-prime.
1525         //
1526         // Furthermore, the result produced by this solution is not "minimal", so it is necessary
1527         // to take the result `o mod lcm(s, a)`. We can replace `lcm(s, a)` with just a `a'`.
1528
1529         // SAFETY: `gcdpow` has an upper-bound not greater than the number of trailing 0-bits in
1530         // `a`.
1531         let a2 = unsafe { unchecked_shr(a, gcdpow) };
1532         // SAFETY: `a2` is non-zero. Shifting `a` by `gcdpow` cannot shift out any of the set bits
1533         // in `a` (of which it has exactly one).
1534         let a2minus1 = unsafe { unchecked_sub(a2, 1) };
1535         // SAFETY: `gcdpow` has an upper-bound not greater than the number of trailing 0-bits in
1536         // `a`.
1537         let s2 = unsafe { unchecked_shr(smoda, gcdpow) };
1538         // SAFETY: `gcdpow` has an upper-bound not greater than the number of trailing 0-bits in
1539         // `a`. Furthermore, the subtraction cannot overflow, because `a2 = a >> gcdpow` will
1540         // always be strictly greater than `(p % a) >> gcdpow`.
1541         let minusp2 = unsafe { unchecked_sub(a2, unchecked_shr(pmoda, gcdpow)) };
1542         // SAFETY: `a2` is a power-of-two, as proven above. `s2` is strictly less than `a2`
1543         // because `(s % a) >> gcdpow` is strictly less than `a >> gcdpow`.
1544         return wrapping_mul(minusp2, unsafe { mod_inv(s2, a2) }) & a2minus1;
1545     }
1546
1547     // Cannot be aligned at all.
1548     usize::MAX
1549 }
1550
1551 /// Compares raw pointers for equality.
1552 ///
1553 /// This is the same as using the `==` operator, but less generic:
1554 /// the arguments have to be `*const T` raw pointers,
1555 /// not anything that implements `PartialEq`.
1556 ///
1557 /// This can be used to compare `&T` references (which coerce to `*const T` implicitly)
1558 /// by their address rather than comparing the values they point to
1559 /// (which is what the `PartialEq for &T` implementation does).
1560 ///
1561 /// # Examples
1562 ///
1563 /// ```
1564 /// use std::ptr;
1565 ///
1566 /// let five = 5;
1567 /// let other_five = 5;
1568 /// let five_ref = &five;
1569 /// let same_five_ref = &five;
1570 /// let other_five_ref = &other_five;
1571 ///
1572 /// assert!(five_ref == same_five_ref);
1573 /// assert!(ptr::eq(five_ref, same_five_ref));
1574 ///
1575 /// assert!(five_ref == other_five_ref);
1576 /// assert!(!ptr::eq(five_ref, other_five_ref));
1577 /// ```
1578 ///
1579 /// Slices are also compared by their length (fat pointers):
1580 ///
1581 /// ```
1582 /// let a = [1, 2, 3];
1583 /// assert!(std::ptr::eq(&a[..3], &a[..3]));
1584 /// assert!(!std::ptr::eq(&a[..2], &a[..3]));
1585 /// assert!(!std::ptr::eq(&a[0..2], &a[1..3]));
1586 /// ```
1587 ///
1588 /// Traits are also compared by their implementation:
1589 ///
1590 /// ```
1591 /// #[repr(transparent)]
1592 /// struct Wrapper { member: i32 }
1593 ///
1594 /// trait Trait {}
1595 /// impl Trait for Wrapper {}
1596 /// impl Trait for i32 {}
1597 ///
1598 /// let wrapper = Wrapper { member: 10 };
1599 ///
1600 /// // Pointers have equal addresses.
1601 /// assert!(std::ptr::eq(
1602 ///     &wrapper as *const Wrapper as *const u8,
1603 ///     &wrapper.member as *const i32 as *const u8
1604 /// ));
1605 ///
1606 /// // Objects have equal addresses, but `Trait` has different implementations.
1607 /// assert!(!std::ptr::eq(
1608 ///     &wrapper as &dyn Trait,
1609 ///     &wrapper.member as &dyn Trait,
1610 /// ));
1611 /// assert!(!std::ptr::eq(
1612 ///     &wrapper as &dyn Trait as *const dyn Trait,
1613 ///     &wrapper.member as &dyn Trait as *const dyn Trait,
1614 /// ));
1615 ///
1616 /// // Converting the reference to a `*const u8` compares by address.
1617 /// assert!(std::ptr::eq(
1618 ///     &wrapper as &dyn Trait as *const dyn Trait as *const u8,
1619 ///     &wrapper.member as &dyn Trait as *const dyn Trait as *const u8,
1620 /// ));
1621 /// ```
1622 #[stable(feature = "ptr_eq", since = "1.17.0")]
1623 #[inline]
1624 pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
1625     a == b
1626 }
1627
1628 /// Hash a raw pointer.
1629 ///
1630 /// This can be used to hash a `&T` reference (which coerces to `*const T` implicitly)
1631 /// by its address rather than the value it points to
1632 /// (which is what the `Hash for &T` implementation does).
1633 ///
1634 /// # Examples
1635 ///
1636 /// ```
1637 /// use std::collections::hash_map::DefaultHasher;
1638 /// use std::hash::{Hash, Hasher};
1639 /// use std::ptr;
1640 ///
1641 /// let five = 5;
1642 /// let five_ref = &five;
1643 ///
1644 /// let mut hasher = DefaultHasher::new();
1645 /// ptr::hash(five_ref, &mut hasher);
1646 /// let actual = hasher.finish();
1647 ///
1648 /// let mut hasher = DefaultHasher::new();
1649 /// (five_ref as *const i32).hash(&mut hasher);
1650 /// let expected = hasher.finish();
1651 ///
1652 /// assert_eq!(actual, expected);
1653 /// ```
1654 #[stable(feature = "ptr_hash", since = "1.35.0")]
1655 pub fn hash<T: ?Sized, S: hash::Hasher>(hashee: *const T, into: &mut S) {
1656     use crate::hash::Hash;
1657     hashee.hash(into);
1658 }
1659
1660 // FIXME(strict_provenance_magic): function pointers have buggy codegen that
1661 // necessitates casting to a usize to get the backend to do the right thing.
1662 // for now I will break AVR to silence *a billion* lints. We should probably
1663 // have a proper "opaque function pointer type" to handle this kind of thing.
1664
1665 // Impls for function pointers
1666 macro_rules! fnptr_impls_safety_abi {
1667     ($FnTy: ty, $($Arg: ident),*) => {
1668         #[stable(feature = "fnptr_impls", since = "1.4.0")]
1669         impl<Ret, $($Arg),*> PartialEq for $FnTy {
1670             #[inline]
1671             fn eq(&self, other: &Self) -> bool {
1672                 *self as usize == *other as usize
1673             }
1674         }
1675
1676         #[stable(feature = "fnptr_impls", since = "1.4.0")]
1677         impl<Ret, $($Arg),*> Eq for $FnTy {}
1678
1679         #[stable(feature = "fnptr_impls", since = "1.4.0")]
1680         impl<Ret, $($Arg),*> PartialOrd for $FnTy {
1681             #[inline]
1682             fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1683                 (*self as usize).partial_cmp(&(*other as usize))
1684             }
1685         }
1686
1687         #[stable(feature = "fnptr_impls", since = "1.4.0")]
1688         impl<Ret, $($Arg),*> Ord for $FnTy {
1689             #[inline]
1690             fn cmp(&self, other: &Self) -> Ordering {
1691                 (*self as usize).cmp(&(*other as usize))
1692             }
1693         }
1694
1695         #[stable(feature = "fnptr_impls", since = "1.4.0")]
1696         impl<Ret, $($Arg),*> hash::Hash for $FnTy {
1697             fn hash<HH: hash::Hasher>(&self, state: &mut HH) {
1698                 state.write_usize(*self as usize)
1699             }
1700         }
1701
1702         #[stable(feature = "fnptr_impls", since = "1.4.0")]
1703         impl<Ret, $($Arg),*> fmt::Pointer for $FnTy {
1704             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1705                 // HACK: The intermediate cast as usize is required for AVR
1706                 // so that the address space of the source function pointer
1707                 // is preserved in the final function pointer.
1708                 //
1709                 // https://github.com/avr-rust/rust/issues/143
1710                 fmt::Pointer::fmt(&(*self as usize as *const ()), f)
1711             }
1712         }
1713
1714         #[stable(feature = "fnptr_impls", since = "1.4.0")]
1715         impl<Ret, $($Arg),*> fmt::Debug for $FnTy {
1716             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1717                 // HACK: The intermediate cast as usize is required for AVR
1718                 // so that the address space of the source function pointer
1719                 // is preserved in the final function pointer.
1720                 //
1721                 // https://github.com/avr-rust/rust/issues/143
1722                 fmt::Pointer::fmt(&(*self as usize as *const ()), f)
1723             }
1724         }
1725     }
1726 }
1727
1728 macro_rules! fnptr_impls_args {
1729     ($($Arg: ident),+) => {
1730         fnptr_impls_safety_abi! { extern "Rust" fn($($Arg),+) -> Ret, $($Arg),+ }
1731         fnptr_impls_safety_abi! { extern "C" fn($($Arg),+) -> Ret, $($Arg),+ }
1732         fnptr_impls_safety_abi! { extern "C" fn($($Arg),+ , ...) -> Ret, $($Arg),+ }
1733         fnptr_impls_safety_abi! { unsafe extern "Rust" fn($($Arg),+) -> Ret, $($Arg),+ }
1734         fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),+) -> Ret, $($Arg),+ }
1735         fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),+ , ...) -> Ret, $($Arg),+ }
1736     };
1737     () => {
1738         // No variadic functions with 0 parameters
1739         fnptr_impls_safety_abi! { extern "Rust" fn() -> Ret, }
1740         fnptr_impls_safety_abi! { extern "C" fn() -> Ret, }
1741         fnptr_impls_safety_abi! { unsafe extern "Rust" fn() -> Ret, }
1742         fnptr_impls_safety_abi! { unsafe extern "C" fn() -> Ret, }
1743     };
1744 }
1745
1746 fnptr_impls_args! {}
1747 fnptr_impls_args! { A }
1748 fnptr_impls_args! { A, B }
1749 fnptr_impls_args! { A, B, C }
1750 fnptr_impls_args! { A, B, C, D }
1751 fnptr_impls_args! { A, B, C, D, E }
1752 fnptr_impls_args! { A, B, C, D, E, F }
1753 fnptr_impls_args! { A, B, C, D, E, F, G }
1754 fnptr_impls_args! { A, B, C, D, E, F, G, H }
1755 fnptr_impls_args! { A, B, C, D, E, F, G, H, I }
1756 fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J }
1757 fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K }
1758 fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K, L }
1759
1760 /// Create a `const` raw pointer to a place, without creating an intermediate reference.
1761 ///
1762 /// Creating a reference with `&`/`&mut` is only allowed if the pointer is properly aligned
1763 /// and points to initialized data. For cases where those requirements do not hold,
1764 /// raw pointers should be used instead. However, `&expr as *const _` creates a reference
1765 /// before casting it to a raw pointer, and that reference is subject to the same rules
1766 /// as all other references. This macro can create a raw pointer *without* creating
1767 /// a reference first.
1768 ///
1769 /// Note, however, that the `expr` in `addr_of!(expr)` is still subject to all
1770 /// the usual rules. In particular, `addr_of!(*ptr::null())` is Undefined
1771 /// Behavior because it dereferences a null pointer.
1772 ///
1773 /// # Example
1774 ///
1775 /// ```
1776 /// use std::ptr;
1777 ///
1778 /// #[repr(packed)]
1779 /// struct Packed {
1780 ///     f1: u8,
1781 ///     f2: u16,
1782 /// }
1783 ///
1784 /// let packed = Packed { f1: 1, f2: 2 };
1785 /// // `&packed.f2` would create an unaligned reference, and thus be Undefined Behavior!
1786 /// let raw_f2 = ptr::addr_of!(packed.f2);
1787 /// assert_eq!(unsafe { raw_f2.read_unaligned() }, 2);
1788 /// ```
1789 ///
1790 /// See [`addr_of_mut`] for how to create a pointer to unininitialized data.
1791 /// Doing that with `addr_of` would not make much sense since one could only
1792 /// read the data, and that would be Undefined Behavior.
1793 #[stable(feature = "raw_ref_macros", since = "1.51.0")]
1794 #[rustc_macro_transparency = "semitransparent"]
1795 #[allow_internal_unstable(raw_ref_op)]
1796 pub macro addr_of($place:expr) {
1797     &raw const $place
1798 }
1799
1800 /// Create a `mut` raw pointer to a place, without creating an intermediate reference.
1801 ///
1802 /// Creating a reference with `&`/`&mut` is only allowed if the pointer is properly aligned
1803 /// and points to initialized data. For cases where those requirements do not hold,
1804 /// raw pointers should be used instead. However, `&mut expr as *mut _` creates a reference
1805 /// before casting it to a raw pointer, and that reference is subject to the same rules
1806 /// as all other references. This macro can create a raw pointer *without* creating
1807 /// a reference first.
1808 ///
1809 /// Note, however, that the `expr` in `addr_of_mut!(expr)` is still subject to all
1810 /// the usual rules. In particular, `addr_of_mut!(*ptr::null_mut())` is Undefined
1811 /// Behavior because it dereferences a null pointer.
1812 ///
1813 /// # Examples
1814 ///
1815 /// **Creating a pointer to unaligned data:**
1816 ///
1817 /// ```
1818 /// use std::ptr;
1819 ///
1820 /// #[repr(packed)]
1821 /// struct Packed {
1822 ///     f1: u8,
1823 ///     f2: u16,
1824 /// }
1825 ///
1826 /// let mut packed = Packed { f1: 1, f2: 2 };
1827 /// // `&mut packed.f2` would create an unaligned reference, and thus be Undefined Behavior!
1828 /// let raw_f2 = ptr::addr_of_mut!(packed.f2);
1829 /// unsafe { raw_f2.write_unaligned(42); }
1830 /// assert_eq!({packed.f2}, 42); // `{...}` forces copying the field instead of creating a reference.
1831 /// ```
1832 ///
1833 /// **Creating a pointer to uninitialized data:**
1834 ///
1835 /// ```rust
1836 /// use std::{ptr, mem::MaybeUninit};
1837 ///
1838 /// struct Demo {
1839 ///     field: bool,
1840 /// }
1841 ///
1842 /// let mut uninit = MaybeUninit::<Demo>::uninit();
1843 /// // `&uninit.as_mut().field` would create a reference to an uninitialized `bool`,
1844 /// // and thus be Undefined Behavior!
1845 /// let f1_ptr = unsafe { ptr::addr_of_mut!((*uninit.as_mut_ptr()).field) };
1846 /// unsafe { f1_ptr.write(true); }
1847 /// let init = unsafe { uninit.assume_init() };
1848 /// ```
1849 #[stable(feature = "raw_ref_macros", since = "1.51.0")]
1850 #[rustc_macro_transparency = "semitransparent"]
1851 #[allow_internal_unstable(raw_ref_op)]
1852 pub macro addr_of_mut($place:expr) {
1853     &raw mut $place
1854 }