Skip to main content

Protection

Enum Protection 

Source
pub enum Protection {
    None,
    Read,
    ReadWrite,
    ReadExecute,
    ReadWriteExecute,
}
Expand description

The access a page range permits: which of read, write, and execute the CPU will allow.

A Region is created read/write so code or data can be written into it, then moved to another protection with Region::protect. The pattern a JIT follows is write xor execute (W^X): fill the region while it is ReadWrite, then flip it to ReadExecute before running the code. Memory that is writable and executable at the same time is a standing invitation to turn a stray write into code execution, so ReadWriteExecute exists for the rare cases that need it but should be avoided where the two-step flip works.

The five cases map directly onto the platform primitives: PROT_* bits for mprotect on Unix, PAGE_* constants for VirtualProtect on Windows.

§Examples

use pager_lang::Protection;

assert!(Protection::ReadWrite.is_writable());
assert!(!Protection::ReadWrite.is_executable());

assert!(Protection::ReadExecute.is_executable());
assert!(!Protection::ReadExecute.is_writable());

// A guard page allows nothing at all.
assert!(!Protection::None.is_readable());

Variants§

§

None

No access. Any read, write, or execute through the range faults. This is what guard pages carry.

§

Read

Read-only. Suitable for constant data that must not change once written.

§

ReadWrite

Read and write — the protection a freshly created Region starts with, so machine code or data can be written into it.

§

ReadExecute

Read and execute. The target for finished JIT code: readable and runnable, but no longer writable, completing the W^X flip.

§

ReadWriteExecute

Read, write, and execute simultaneously. Convenient but it defeats W^X; prefer writing under ReadWrite and flipping to ReadExecute. Some hardened platforms refuse this protection outright.

Implementations§

Source§

impl Protection

Source

pub const fn is_readable(self) -> bool

Whether reads are permitted. True for every protection except None.

§Examples
use pager_lang::Protection;

assert!(Protection::Read.is_readable());
assert!(Protection::ReadExecute.is_readable());
assert!(!Protection::None.is_readable());
Source

pub const fn is_writable(self) -> bool

Whether writes are permitted. True for ReadWrite and ReadWriteExecute.

Region::write and Region::as_mut_slice succeed exactly when the region’s protection reports this true.

§Examples
use pager_lang::Protection;

assert!(Protection::ReadWrite.is_writable());
assert!(!Protection::ReadExecute.is_writable());
Source

pub const fn is_executable(self) -> bool

Whether execution is permitted. True for ReadExecute and ReadWriteExecute.

§Examples
use pager_lang::Protection;

assert!(Protection::ReadExecute.is_executable());
assert!(!Protection::ReadWrite.is_executable());

Trait Implementations§

Source§

impl Clone for Protection

Source§

fn clone(&self) -> Protection

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Protection

Source§

impl Debug for Protection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Protection

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Protection

Source§

impl Hash for Protection

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Protection

Source§

fn eq(&self, other: &Protection) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Protection

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Protection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.