Function region::protect [] [src]

pub unsafe fn protect(
    address: *const u8,
    size: usize,
    protection: Flag
) -> Result<()>

Changes the memory protection of one or more regions.

The address range may overlap one or more regions, and if so, all regions within the range will be modified. The previous protection flags are not preserved (to reset protection flags to their inital values, query_range can be used prior to this call).

If the size is zero this will affect the whole page located at the address

  • The range is [address, address + size)
  • The address is rounded down to the closest page boundary.
  • The address may not be null.
  • The size is rounded up to the closest page boundary, relative to the address.

Examples

use region::{Protection};

let ret5 = [0xB8, 0x05, 0x00, 0x00, 0x00, 0xC3];
let x: extern "C" fn() -> i32 = unsafe {
  region::protect(ret5.as_ptr(), ret5.len(), Protection::ReadWriteExecute).unwrap();
  std::mem::transmute(ret5.as_ptr())
};
assert_eq!(x(), 5);