Function region::protect

source ·
pub unsafe fn protect(
    address: *const u8,
    size: usize,
    protection: Protection
) -> Result<()>
Expand description

Changes the memory protection of one or more pages.

The address range may overlap one or more pages, and if so, all pages within the range will be modified. The previous protection flags are not preserved (if reset of protection flags is desired, use protect_with_handle).

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

Safety

This is unsafe since it can change read-only properties of constants and/or modify the executable properties of any code segments.

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::READ_WRITE_EXECUTE).unwrap();
  std::mem::transmute(ret5.as_ptr())
};
assert_eq!(x(), 5);