summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/mm
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/rustix/src/mm
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix/src/mm')
-rw-r--r--vendor/rustix/src/mm/madvise.rs35
-rw-r--r--vendor/rustix/src/mm/mmap.rs231
-rw-r--r--vendor/rustix/src/mm/mod.rs21
-rw-r--r--vendor/rustix/src/mm/msync.rs32
-rw-r--r--vendor/rustix/src/mm/userfaultfd.rs30
5 files changed, 349 insertions, 0 deletions
diff --git a/vendor/rustix/src/mm/madvise.rs b/vendor/rustix/src/mm/madvise.rs
new file mode 100644
index 000000000..317c3b515
--- /dev/null
+++ b/vendor/rustix/src/mm/madvise.rs
@@ -0,0 +1,35 @@
+//! The `madvise` function.
+//!
+//! # Safety
+//!
+//! `madvise` operates on a raw pointer. Some forms of `madvise` may
+//! mutate the memory or have other side effects.
+#![allow(unsafe_code)]
+
+use crate::{imp, io};
+use core::ffi::c_void;
+
+pub use imp::mm::types::Advice;
+
+/// `posix_madvise(addr, len, advice)`—Declares an expected access pattern
+/// for a memory-mapped file.
+///
+/// # Safety
+///
+/// `addr` must be a valid pointer to memory that is appropriate to
+/// call `posix_madvise` on. Some forms of `advice` may mutate the memory
+/// or evoke a variety of side-effects on the mapping and/or the file.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux `madvise`]
+/// - [Linux `posix_madvise`]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_madvise.html
+/// [Linux `madvise`]: https://man7.org/linux/man-pages/man2/madvise.2.html
+/// [Linux `posix_madvise`]: https://man7.org/linux/man-pages/man3/posix_madvise.3.html
+#[inline]
+#[doc(alias = "posix_madvise")]
+pub unsafe fn madvise(addr: *mut c_void, len: usize, advice: Advice) -> io::Result<()> {
+ imp::mm::syscalls::madvise(addr, len, advice)
+}
diff --git a/vendor/rustix/src/mm/mmap.rs b/vendor/rustix/src/mm/mmap.rs
new file mode 100644
index 000000000..7be02464e
--- /dev/null
+++ b/vendor/rustix/src/mm/mmap.rs
@@ -0,0 +1,231 @@
+//! The `mmap` API.
+//!
+//! # Safety
+//!
+//! `mmap` and related functions manipulate raw pointers and have special
+//! semantics and are wildly unsafe.
+#![allow(unsafe_code)]
+
+use crate::{imp, io};
+use core::ffi::c_void;
+use imp::fd::AsFd;
+
+#[cfg(any(target_os = "android", target_os = "linux"))]
+pub use imp::mm::types::MlockFlags;
+#[cfg(any(linux_raw, all(libc, target_os = "linux")))]
+pub use imp::mm::types::MremapFlags;
+pub use imp::mm::types::{MapFlags, MprotectFlags, ProtFlags};
+
+/// `mmap(ptr, len, prot, flags, fd, offset)`—Create a file-backed memory
+/// mapping.
+///
+/// For anonymous mappings (`MAP_ANON`/`MAP_ANONYMOUS`), see
+/// [`mmap_anonymous`].
+///
+/// # Safety
+///
+/// Raw pointers and lots of special semantics.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html
+/// [Linux]: https://man7.org/linux/man-pages/man2/mmap.2.html
+#[inline]
+pub unsafe fn mmap<Fd: AsFd>(
+ ptr: *mut c_void,
+ len: usize,
+ prot: ProtFlags,
+ flags: MapFlags,
+ fd: Fd,
+ offset: u64,
+) -> io::Result<*mut c_void> {
+ imp::mm::syscalls::mmap(ptr, len, prot, flags, fd.as_fd(), offset)
+}
+
+/// `mmap(ptr, len, prot, MAP_ANONYMOUS | flags, -1, 0)`—Create an anonymous
+/// memory mapping.
+///
+/// For file-backed mappings, see [`mmap`].
+///
+/// # Safety
+///
+/// Raw pointers and lots of special semantics.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html
+/// [Linux]: https://man7.org/linux/man-pages/man2/mmap.2.html
+#[inline]
+#[doc(alias = "mmap")]
+pub unsafe fn mmap_anonymous(
+ ptr: *mut c_void,
+ len: usize,
+ prot: ProtFlags,
+ flags: MapFlags,
+) -> io::Result<*mut c_void> {
+ imp::mm::syscalls::mmap_anonymous(ptr, len, prot, flags)
+}
+
+/// `munmap(ptr, len)`
+///
+/// # Safety
+///
+/// Raw pointers and lots of special semantics.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/munmap.html
+/// [Linux]: https://man7.org/linux/man-pages/man2/munmap.2.html
+#[inline]
+pub unsafe fn munmap(ptr: *mut c_void, len: usize) -> io::Result<()> {
+ imp::mm::syscalls::munmap(ptr, len)
+}
+
+/// `mremap(old_address, old_size, new_size, flags)`—Resize, modify,
+/// and/or move a memory mapping.
+///
+/// For moving a mapping to a fixed address (`MREMAP_FIXED`), see
+/// [`mremap_fixed`].
+///
+/// # Safety
+///
+/// Raw pointers and lots of special semantics.
+///
+/// # References
+/// - [Linux]
+///
+/// [Linux]: https://man7.org/linux/man-pages/man2/mremap.2.html
+#[cfg(any(linux_raw, all(libc, target_os = "linux")))]
+#[inline]
+pub unsafe fn mremap(
+ old_address: *mut c_void,
+ old_size: usize,
+ new_size: usize,
+ flags: MremapFlags,
+) -> io::Result<*mut c_void> {
+ imp::mm::syscalls::mremap(old_address, old_size, new_size, flags)
+}
+
+/// `mremap(old_address, old_size, new_size, MREMAP_FIXED | flags)`—Resize,
+/// modify, and/or move a memory mapping to a specific address.
+///
+/// For `mremap` without moving to a specific address, see [`mremap`].
+/// [`mremap_fixed`].
+///
+/// # Safety
+///
+/// Raw pointers and lots of special semantics.
+///
+/// # References
+/// - [Linux]
+///
+/// [Linux]: https://man7.org/linux/man-pages/man2/mremap.2.html
+#[cfg(any(linux_raw, all(libc, target_os = "linux")))]
+#[inline]
+#[doc(alias = "mremap")]
+pub unsafe fn mremap_fixed(
+ old_address: *mut c_void,
+ old_size: usize,
+ new_size: usize,
+ flags: MremapFlags,
+ new_address: *mut c_void,
+) -> io::Result<*mut c_void> {
+ imp::mm::syscalls::mremap_fixed(old_address, old_size, new_size, flags, new_address)
+}
+
+/// `mprotect(ptr, len, flags)`
+///
+/// # Safety
+///
+/// Raw pointers and lots of special semantics.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html
+/// [Linux]: https://man7.org/linux/man-pages/man2/mprotect.2.html
+#[inline]
+pub unsafe fn mprotect(ptr: *mut c_void, len: usize, flags: MprotectFlags) -> io::Result<()> {
+ imp::mm::syscalls::mprotect(ptr, len, flags)
+}
+
+/// `mlock(ptr, len)`—Lock memory into RAM.
+///
+/// # Safety
+///
+/// This function operates on raw pointers, but it should only be used on
+/// memory which the caller owns. Technically, locking memory shouldn't violate
+/// any invariants, but since unlocking it can violate invariants, this
+/// function is also unsafe for symmetry.
+///
+/// Some implementations implicitly round the memory region out to the nearest
+/// page boundaries, so this function may lock more memory than explicitly
+/// requested if the memory isn't page-aligned.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlock.html
+/// [Linux]: https://man7.org/linux/man-pages/man2/mlock.2.html
+#[inline]
+pub unsafe fn mlock(ptr: *mut c_void, len: usize) -> io::Result<()> {
+ imp::mm::syscalls::mlock(ptr, len)
+}
+
+/// `mlock2(ptr, len, flags)`—Lock memory into RAM, with
+/// flags.
+///
+/// `mlock_with` is the same as [`mlock`] but adds an additional flags operand.
+///
+/// # Safety
+///
+/// This function operates on raw pointers, but it should only be used on
+/// memory which the caller owns. Technically, locking memory shouldn't violate
+/// any invariants, but since unlocking it can violate invariants, this
+/// function is also unsafe for symmetry.
+///
+/// Some implementations implicitly round the memory region out to the nearest
+/// page boundaries, so this function may lock more memory than explicitly
+/// requested if the memory isn't page-aligned.
+///
+/// # References
+/// - [Linux]
+///
+/// [Linux]: https://man7.org/linux/man-pages/man2/mlock2.2.html
+#[cfg(any(target_os = "android", target_os = "linux"))]
+#[inline]
+#[doc(alias = "mlock2")]
+pub unsafe fn mlock_with(ptr: *mut c_void, len: usize, flags: MlockFlags) -> io::Result<()> {
+ imp::mm::syscalls::mlock_with(ptr, len, flags)
+}
+
+/// `munlock(ptr, len)`—Unlock memory.
+///
+/// # Safety
+///
+/// This function operates on raw pointers, but it should only be used on
+/// memory which the caller owns, to avoid compromising the `mlock` invariants
+/// of other unrelated code in the process.
+///
+/// Some implementations implicitly round the memory region out to the nearest
+/// page boundaries, so this function may unlock more memory than explicitly
+/// requested if the memory isn't page-aligned.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/munlock.html
+/// [Linux]: https://man7.org/linux/man-pages/man2/munlock.2.html
+#[inline]
+pub unsafe fn munlock(ptr: *mut c_void, len: usize) -> io::Result<()> {
+ imp::mm::syscalls::munlock(ptr, len)
+}
diff --git a/vendor/rustix/src/mm/mod.rs b/vendor/rustix/src/mm/mod.rs
new file mode 100644
index 000000000..9fabea87f
--- /dev/null
+++ b/vendor/rustix/src/mm/mod.rs
@@ -0,0 +1,21 @@
+//! Memory map operations.
+
+#[cfg(not(target_os = "redox"))]
+mod madvise;
+mod mmap;
+mod msync;
+#[cfg(any(target_os = "android", target_os = "linux"))]
+mod userfaultfd;
+
+#[cfg(not(target_os = "redox"))]
+pub use madvise::{madvise, Advice};
+pub use mmap::{
+ mlock, mmap, mmap_anonymous, mprotect, munlock, munmap, MapFlags, MprotectFlags, ProtFlags,
+};
+#[cfg(any(target_os = "android", target_os = "linux"))]
+pub use mmap::{mlock_with, MlockFlags};
+#[cfg(any(linux_raw, all(libc, target_os = "linux")))]
+pub use mmap::{mremap, mremap_fixed, MremapFlags};
+pub use msync::{msync, MsyncFlags};
+#[cfg(any(target_os = "android", target_os = "linux"))]
+pub use userfaultfd::{userfaultfd, UserfaultfdFlags};
diff --git a/vendor/rustix/src/mm/msync.rs b/vendor/rustix/src/mm/msync.rs
new file mode 100644
index 000000000..7117a5067
--- /dev/null
+++ b/vendor/rustix/src/mm/msync.rs
@@ -0,0 +1,32 @@
+//! The `msync` function.
+//!
+//! # Safety
+//!
+//! `msync` operates on a raw pointer. Some forms of `msync` may mutate the
+//! memory or have other side effects.
+#![allow(unsafe_code)]
+
+use crate::{imp, io};
+use core::ffi::c_void;
+
+pub use imp::mm::types::MsyncFlags;
+
+/// `msync(addr, len, flags)`—Synchronizes a memory-mapping with its backing
+/// storage.
+///
+/// # Safety
+///
+/// `addr` must be a valid pointer to memory that is appropriate to
+/// call `msync` on. Some forms of `msync` may mutate the memory
+/// or evoke a variety of side-effects on the mapping and/or the file.
+///
+/// # References
+/// - [POSIX]
+/// - [Linux `msync`]
+///
+/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/msync.html
+/// [Linux `msync`]: https://man7.org/linux/man-pages/man2/msync.2.html
+#[inline]
+pub unsafe fn msync(addr: *mut c_void, len: usize, flags: MsyncFlags) -> io::Result<()> {
+ imp::mm::syscalls::msync(addr, len, flags)
+}
diff --git a/vendor/rustix/src/mm/userfaultfd.rs b/vendor/rustix/src/mm/userfaultfd.rs
new file mode 100644
index 000000000..65b4a0dd8
--- /dev/null
+++ b/vendor/rustix/src/mm/userfaultfd.rs
@@ -0,0 +1,30 @@
+//! The Linux `userfaultfd` API.
+//!
+//! # Safety
+//!
+//! Calling `userfaultfd` is safe, but the returned file descriptor lets users
+//! observe and manipulate process memory in magical ways.
+#![allow(unsafe_code)]
+
+use crate::imp;
+use crate::io::{self, OwnedFd};
+
+pub use imp::mm::types::UserfaultfdFlags;
+
+/// `userfaultfd(flags)`
+///
+/// # Safety
+///
+/// The call itself is safe, but the returned file descriptor lets users
+/// observe and manipulate process memory in magical ways.
+///
+/// # References
+/// - [Linux]
+/// - [Linux userfaultfd]
+///
+/// [Linux]: https://man7.org/linux/man-pages/man2/userfaultfd.2.html
+/// [Linux userfaultfd]: https://www.kernel.org/doc/Documentation/vm/userfaultfd.txt
+#[inline]
+pub unsafe fn userfaultfd(flags: UserfaultfdFlags) -> io::Result<OwnedFd> {
+ imp::mm::syscalls::userfaultfd(flags)
+}