MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / test_mlock

Function test_mlock

tests/mm/mmap.rs:94–133  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

92
93#[test]
94fn test_mlock() {
95 use rustix::mm::{mlock, mmap_anonymous, munlock, munmap, MapFlags, ProtFlags};
96 #[cfg(linux_kernel)]
97 use rustix::mm::{mlock_with, MlockFlags};
98 use std::ptr::null_mut;
99
100 unsafe {
101 let addr = mmap_anonymous(null_mut(), 8192, ProtFlags::READ, MapFlags::PRIVATE).unwrap();
102
103 match mlock(addr, 8192) {
104 Ok(()) => munlock(addr, 8192).unwrap(),
105 // Tests won't always have enough memory or permissions, and that's ok.
106 Err(rustix::io::Errno::PERM | rustix::io::Errno::NOMEM) => (),
107 // But they shouldn't fail otherwise.
108 Err(other) => panic!("{:?}", other),
109 }
110
111 #[cfg(linux_kernel)]
112 {
113 match mlock_with(addr, 8192, MlockFlags::empty()) {
114 Err(rustix::io::Errno::NOSYS) => (),
115 Err(err) => panic!("{:?}", err),
116 Ok(()) => munlock(addr, 8192).unwrap(),
117 }
118
119 match mlock_with(addr, 8192, MlockFlags::ONFAULT) {
120 // Linux versions that lack `mlock` return this.
121 Err(rustix::io::Errno::NOSYS) => (),
122 // Linux versions that don't recognize `ONFAULT` return this.
123 Err(rustix::io::Errno::INVAL) => (),
124 Err(err) => panic!("{:?}", err),
125 Ok(()) => munlock(addr, 8192).unwrap(),
126 }
127
128 munlock(addr, 8192).unwrap();
129 }
130
131 munmap(addr, 8192).unwrap();
132 }
133}
134
135#[cfg(not(target_os = "redox"))]
136#[test]

Callers

nothing calls this directly

Calls 5

mmap_anonymousFunction · 0.50
mlockFunction · 0.50
munlockFunction · 0.50
mlock_withFunction · 0.50
munmapFunction · 0.50

Tested by

no test coverage detected