pub unsafe fn mprotect(
addr: NonNull<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_anonymous(None, one_k_non_zero, ProtFlags::PROT_NONE, MapFlags::MAP_PRIVATE)
.unwrap();
mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap();
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
assert_eq!(slice[0], 0x00);
slice[0] = 0xFF;
assert_eq!(slice[0], 0xFF);