pub unsafe fn munlock(addr: *const c_void, len: usize) -> Result<()>Available on Unix only.
Expand description
Unlock the pages containing the specified memory region.
Returns:
Ok(())on successErr(...)withlast_os_error()on failureErr(Unsupported)on platforms where this call is not available
ยงSafety
The caller must ensure that (addr, len) refers to a valid, non-null memory
region owned by this process for the duration of the call, and that the region
is not deallocated, unmapped, or remapped concurrently.
Examples found in repository?
examples/locked_vec.rs (line 91)
82 fn drop(&mut self) {
83 // Zeroize contents while still locked (if locked).
84 for b in &mut self.buf {
85 *b = 0;
86 }
87
88 if self.locked {
89 let ptr = self.buf.as_ptr() as *const std::os::raw::c_void;
90 let len = self.buf.len();
91 match unsafe { os_memlock::munlock(ptr, len) } {
92 Ok(()) => (),
93 Err(e) if e.kind() == io::ErrorKind::Unsupported => {
94 // Not supported on this target; ignore.
95 }
96 Err(e) => {
97 // We can't do much in Drop; log error.
98 eprintln!("os-memlock: munlock failed: {e}");
99 }
100 }
101 }
102 }More examples
examples/simple.rs (line 63)
20fn main() -> io::Result<()> {
21 // Simple buffer representing secret data. Use a page-sized allocation for clarity.
22 // On many systems a page is 4096 bytes; this example uses that common size.
23 const PAGE_LEN: usize = 4096;
24 let mut secret = vec![0u8; PAGE_LEN];
25
26 // Put some dummy secret bytes (for demo only).
27 secret[..16].copy_from_slice(b"super-secret-data");
28
29 let ptr = secret.as_ptr() as *const std::os::raw::c_void;
30
31 let len = secret.len();
32
33 println!("Attempting to lock {} bytes at {:p}", len, ptr);
34
35 // Try to mlock the buffer. This is unsafe and may return Unsupported on some builds/platforms.
36 match unsafe { os_memlock::mlock(ptr, len) } {
37 Ok(()) => println!("mlock succeeded"),
38 Err(e) if e.kind() == io::ErrorKind::Unsupported => {
39 println!("mlock is unsupported on this platform/build; continuing without page-lock")
40 }
41 Err(e) => return Err(e),
42 }
43
44 // Best-effort: on Linux, advise the kernel not to include the mapping in core dumps.
45 #[cfg(target_os = "linux")]
46 {
47 let mut_ptr = secret.as_mut_ptr() as *mut std::os::raw::c_void;
48 match unsafe { os_memlock::madvise_dontdump(mut_ptr, len) } {
49 Ok(()) => println!("madvise(MADV_DONTDUMP) applied"),
50 Err(e) if e.kind() == io::ErrorKind::Unsupported => {
51 println!("madvise(MADV_DONTDUMP) unsupported on this platform/build")
52 }
53 Err(e) => eprintln!("madvise failed: {:#}", e),
54 }
55 }
56
57 // Do some work while memory is (hopefully) locked.
58 println!("Working with secret data (simulated)...");
59 // Sleep briefly to simulate lifetime of locked secret. (In real code avoid sleeping.)
60 thread::sleep(Duration::from_millis(250));
61
62 // Before dropping or unmapping the buffer, munlock.
63 match unsafe { os_memlock::munlock(ptr, len) } {
64 Ok(()) => println!("munlock succeeded"),
65 Err(e) if e.kind() == io::ErrorKind::Unsupported => {
66 println!("munlock unsupported (no-op for this platform/build)")
67 }
68 Err(e) => return Err(e),
69 }
70
71 // Zeroize secret before drop as a good hygiene (example only; use a proper zeroize crate in production).
72 for b in &mut secret {
73 *b = 0;
74 }
75
76 println!("Secret zeroized and example complete.");
77 Ok(())
78}