pub unsafe fn mprotect(
addr: *mut c_void,
length: size_t,
prot: ProtFlags,
) -> Result<()>
Expand description
Set protection of memory mapping.
See mprotect(3)
for
details.
ยงSafety
Calls to mprotect
are inherently unsafe, as changes to memory protections can lead to
SIGSEGVs.
const ONE_K: size_t = 1024;
let one_k_non_zero = std::num::NonZeroUsize::new(ONE_K).unwrap();
let mut slice: &mut [u8] = unsafe {
let mem = mmap(None, one_k_non_zero, ProtFlags::PROT_NONE,
MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, -1, 0).unwrap();
mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap();
std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K)
};
assert_eq!(slice[0], 0x00);
slice[0] = 0xFF;
assert_eq!(slice[0], 0xFF);