@@ -258,6 +258,29 @@ pub unsafe fn madvise(addr: *mut c_void, length: size_t, advise: MmapAdvise) ->
258258 Errno :: result ( libc:: madvise ( addr, length, advise as i32 ) ) . map ( drop)
259259}
260260
261+ /// Set protection of memory mapping.
262+ ///
263+ /// See [`mprotect(3)`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html) for
264+ /// details.
265+ ///
266+ /// # Safety Calls to `mprotect` are inherently unsafe, as changes to memory protections can lead to
267+ /// SIGSEGVs.
268+ ///
269+ /// ```
270+ /// # use nix::libc::size_t;
271+ /// # use nix::sys::mman::{mmap, mprotect, MapFlags, ProtFlags};
272+ /// # use std::ptr;
273+ /// const ONE_K: size_t = 1024;
274+ /// let mut slice: &mut [u8] = unsafe {
275+ /// let mem = mmap(ptr::null_mut(), ONE_K, ProtFlags::PROT_NONE,
276+ /// MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE, -1, 0).unwrap();
277+ /// mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap();
278+ /// std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K)
279+ /// };
280+ /// assert_eq!(slice[0], 0x00);
281+ /// slice[0] = 0xFF;
282+ /// assert_eq!(slice[0], 0xFF);
283+ /// ```
261284pub unsafe fn mprotect ( addr : * mut c_void , length : size_t , prot : ProtFlags ) -> Result < ( ) > {
262285 Errno :: result ( libc:: mprotect ( addr, length, prot. bits ( ) ) ) . map ( drop)
263286}
0 commit comments