-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mlockall
and munlockall
#872
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ use crate::{backend, io}; | |
use backend::fd::AsFd; | ||
use core::ffi::c_void; | ||
|
||
#[cfg(any(linux_kernel, freebsdlike, netbsdlike))] | ||
pub use backend::mm::types::MlockAllFlags; | ||
#[cfg(linux_kernel)] | ||
pub use backend::mm::types::MlockFlags; | ||
#[cfg(any(target_os = "emscripten", target_os = "linux"))] | ||
|
@@ -340,3 +342,66 @@ pub unsafe fn mlock_with(ptr: *mut c_void, len: usize, flags: MlockFlags) -> io: | |
pub unsafe fn munlock(ptr: *mut c_void, len: usize) -> io::Result<()> { | ||
backend::mm::syscalls::munlock(ptr, len) | ||
} | ||
|
||
/// Locks all pages mapped into the address space of the calling process. | ||
/// | ||
/// This includes the pages of the code, data and stack segment, as well as shared libraries, | ||
/// user space kernel data, shared memory, and memory-mapped files. All mapped pages are | ||
/// guaranteed to be resident in RAM when the call returns successfully; | ||
/// the pages are guaranteed to stay in RAM until later unlocked. | ||
/// | ||
/// # References | ||
/// - [POSIX] | ||
/// - [Linux] | ||
/// - [FreeBSD] | ||
/// - [NetBSD] | ||
/// - [OpenBSD] | ||
/// - [DragonFly BSD] | ||
/// - [illumos] | ||
/// - [glibc] | ||
/// | ||
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlockall.html | ||
/// [Linux]: https://man7.org/linux/man-pages/man2/mlockall.2.html | ||
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=mlockall&sektion=2 | ||
/// [NetBSD]: https://man.netbsd.org/mlockall.2 | ||
/// [OpenBSD]: https://man.openbsd.org/mlockall.2 | ||
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=mlockall§ion=2 | ||
/// [illumos]: https://illumos.org/man/3C/mlockall | ||
/// [glibc]: https://www.gnu.org/software/libc/manual/html_node/Page-Lock-Functions.html#index-mlockall | ||
#[cfg(any(linux_kernel, freebsdlike, netbsdlike))] | ||
#[inline] | ||
pub fn mlockall(flags: MlockAllFlags) -> io::Result<()> { | ||
backend::mm::syscalls::mlockall(flags) | ||
} | ||
|
||
/// Unlocks all pages mapped into the address space of the calling process. | ||
/// | ||
/// # Warnings | ||
/// | ||
/// This function is aware of all the memory pages in the process, as if it were a debugger. | ||
/// It unlocks all the pages, which could potentially compromise security assumptions made by | ||
/// code about memory it has encapsulated. | ||
/// | ||
/// # References | ||
/// - [POSIX] | ||
/// - [Linux] | ||
/// - [FreeBSD] | ||
/// - [NetBSD] | ||
/// - [OpenBSD] | ||
/// - [DragonFly BSD] | ||
/// - [illumos] | ||
/// - [glibc] | ||
/// | ||
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/munlockall.html | ||
/// [Linux]: https://man7.org/linux/man-pages/man2/munlockall.2.html | ||
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=munlockall&sektion=2 | ||
/// [NetBSD]: https://man.netbsd.org/munlockall.2 | ||
/// [OpenBSD]: https://man.openbsd.org/munlockall.2 | ||
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=munlockall§ion=2 | ||
/// [illumos]: https://illumos.org/man/3C/munlockall | ||
/// [glibc]: https://www.gnu.org/software/libc/manual/html_node/Page-Lock-Functions.html#index-munlockall | ||
#[cfg(any(linux_kernel, freebsdlike, netbsdlike))] | ||
#[inline] | ||
pub fn munlockall() -> io::Result<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, Should But on the other, since I'm open to other opinions here, but right now I think I buy this argument that The comment should say something along the lines of "warning, this function is aware of all the memory pages in the process, as if it were a debugger. It unlocks all the pages, which could potentially compromise security assumptions made by code about memory it has encapsulated". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add a warning comment, but I don't think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I ultimately also concluded that they should be safe too, though for slightly different reasons. I'm aware of the discussions. I believe the strong opinions about this are motivated by the perpetual need to push back against the endless stream of miscellaneous invariants that might get added to My interpretation of memory safety is that it's about anything that bypasses the Rust memory abstraction level, and instead operates at the level of "a pointer is just an integer, memory is just a global array of bytes". This is a subtle and debatable distinction, but my main point here is just that I don't see this as disagreeing with the strong opinions, it's just a specific interpretation of them. Separately, "/proc/self/mem", teaches us that memory safety is not the only question. We also need to ask something like "is it a debugger?". That's also a subtle question, but my point here is just to say that in this moment, in this context, I think |
||
backend::mm::syscalls::munlockall() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust-lang/libc#3380