33//! # Safety
44//!
55//! `mmap` and related functions manipulate raw pointers and have special
6- //! semantics and are wildly unsafe .
6+ //! semantics.
77#![ allow( unsafe_code) ]
88
99use crate :: { backend, io} ;
@@ -52,7 +52,16 @@ impl MapFlags {
5252///
5353/// # Safety
5454///
55- /// Raw pointers and lots of special semantics.
55+ /// If `ptr` is not null, it must be aligned to the applicable page size, and
56+ /// the range of memory starting at `ptr` and extending for `len` bytes,
57+ /// rounded up to the applicable page size, must be valid to mutate using
58+ /// `ptr`'s provenance.
59+ ///
60+ /// If there exist any Rust references referring to the memory region, or if
61+ /// you subsequently create a Rust reference referring to the resulting region,
62+ /// it is your responsibility to ensure that the Rust reference invariants are
63+ /// preserved, including ensuring that the memory is not mutated in a way that
64+ /// a Rust reference would not expect.
5665///
5766/// # References
5867/// - [POSIX]
@@ -93,7 +102,10 @@ pub unsafe fn mmap<Fd: AsFd>(
93102///
94103/// # Safety
95104///
96- /// Raw pointers and lots of special semantics.
105+ /// If `ptr` is not null, it must be aligned to the applicable page size, and
106+ /// the range of memory starting at `ptr` and extending for `len` bytes,
107+ /// rounded up to the applicable page size, must be valid to mutate with
108+ /// `ptr`'s provenance.
97109///
98110/// # References
99111/// - [POSIX]
@@ -130,7 +142,10 @@ pub unsafe fn mmap_anonymous(
130142///
131143/// # Safety
132144///
133- /// Raw pointers and lots of special semantics.
145+ /// `ptr` must be aligned to the applicable page size, and the range of memory
146+ /// starting at `ptr` and extending for `len` bytes, rounded up to the
147+ /// applicable page size, must be valid to mutate with `ptr`'s provenance. And
148+ /// there must be no Rust references referring to that memory.
134149///
135150/// # References
136151/// - [POSIX]
@@ -165,7 +180,15 @@ pub unsafe fn munmap(ptr: *mut c_void, len: usize) -> io::Result<()> {
165180///
166181/// # Safety
167182///
168- /// Raw pointers and lots of special semantics.
183+ /// `old_address` must be aligned to the applicable page size, and the range of
184+ /// memory starting at `old_address` and extending for `old_size` bytes,
185+ /// rounded up to the applicable page size, must be valid to mutate with
186+ /// `old_address`'s provenance. If `MremapFlags::MAY_MOVE` is set in `flags`,
187+ /// there must be no Rust references referring to that the memory.
188+ ///
189+ /// If `new_size` is less than `old_size`, than there must be no Rust
190+ /// references referring to the memory starting at offset `new_size` and ending
191+ /// at `old_size`.
169192///
170193/// # References
171194/// - [Linux]
@@ -190,7 +213,16 @@ pub unsafe fn mremap(
190213///
191214/// # Safety
192215///
193- /// Raw pointers and lots of special semantics.
216+ /// `old_address` and `new_address` must be aligned to the applicable page
217+ /// size, the range of memory starting at `old_address` and extending for
218+ /// `old_size` bytes, rounded up to the applicable page size, must be valid to
219+ /// mutate with `old_address`'s provenance, and the range of memory starting at
220+ /// `new_address` and extending for `new_size` bytes, rounded up to the
221+ /// applicable page size, must be valid to mutate with `new_address`'s
222+ /// provenance.
223+ ///
224+ /// There must be no Rust references referring to either of those memory
225+ /// regions.
194226///
195227/// # References
196228/// - [Linux]
@@ -214,7 +246,9 @@ pub unsafe fn mremap_fixed(
214246///
215247/// # Safety
216248///
217- /// Raw pointers and lots of special semantics.
249+ /// The range of memory starting at `ptr` and extending for `len` bytes,
250+ /// rounded up to the applicable page size, must be valid to read with `ptr`'s
251+ /// provenance.
218252///
219253/// # References
220254/// - [POSIX]
@@ -241,18 +275,17 @@ pub unsafe fn mprotect(ptr: *mut c_void, len: usize, flags: MprotectFlags) -> io
241275
242276/// `mlock(ptr, len)`—Lock memory into RAM.
243277///
244- /// # Safety
245- ///
246- /// This function operates on raw pointers, but it should only be used on
247- /// memory which the caller owns. Technically, locking memory shouldn't violate
248- /// any invariants, but since unlocking it can violate invariants, this
249- /// function is also unsafe for symmetry.
250- ///
251278/// Some implementations implicitly round the memory region out to the nearest
252279/// page boundaries, so this function may lock more memory than explicitly
253280/// requested if the memory isn't page-aligned. Other implementations fail if
254281/// the memory isn't page-aligned.
255282///
283+ /// # Safety
284+ ///
285+ /// The range of memory starting at `ptr`, rounded down to the applicable page
286+ /// boundary, and extending for `len` bytes, rounded up to the applicable page
287+ /// size, must be valid to read with `ptr`'s provenance.
288+ ///
256289/// # References
257290/// - [POSIX]
258291/// - [Linux]
@@ -282,17 +315,16 @@ pub unsafe fn mlock(ptr: *mut c_void, len: usize) -> io::Result<()> {
282315///
283316/// `mlock_with` is the same as [`mlock`] but adds an additional flags operand.
284317///
285- /// # Safety
286- ///
287- /// This function operates on raw pointers, but it should only be used on
288- /// memory which the caller owns. Technically, locking memory shouldn't violate
289- /// any invariants, but since unlocking it can violate invariants, this
290- /// function is also unsafe for symmetry.
291- ///
292318/// Some implementations implicitly round the memory region out to the nearest
293319/// page boundaries, so this function may lock more memory than explicitly
294320/// requested if the memory isn't page-aligned.
295321///
322+ /// # Safety
323+ ///
324+ /// The range of memory starting at `ptr`, rounded down to the applicable page
325+ /// boundary, and extending for `len` bytes, rounded up to the applicable page
326+ /// size, must be valid to read with `ptr`'s provenance.
327+ ///
296328/// # References
297329/// - [Linux]
298330/// - [glibc]
@@ -308,16 +340,16 @@ pub unsafe fn mlock_with(ptr: *mut c_void, len: usize, flags: MlockFlags) -> io:
308340
309341/// `munlock(ptr, len)`—Unlock memory.
310342///
311- /// # Safety
312- ///
313- /// This function operates on raw pointers, but it should only be used on
314- /// memory which the caller owns, to avoid compromising the `mlock` invariants
315- /// of other unrelated code in the process.
316- ///
317343/// Some implementations implicitly round the memory region out to the nearest
318344/// page boundaries, so this function may unlock more memory than explicitly
319345/// requested if the memory isn't page-aligned.
320346///
347+ /// # Safety
348+ ///
349+ /// The range of memory starting at `ptr`, rounded down to the applicable page
350+ /// boundary, and extending for `len` bytes, rounded up to the applicable page
351+ /// size, must be valid to read with `ptr`'s provenance.
352+ ///
321353/// # References
322354/// - [POSIX]
323355/// - [Linux]
0 commit comments