Struct mmap_rs::MmapNone

source ·
pub struct MmapNone { /* private fields */ }
Expand description

Represents an inaccessible memory mapping.

Implementations§

source§

impl MmapNone

source

pub fn lock(&mut self) -> Result<(), Error>

Locks the physical pages in memory such that accessing the mapping causes no page faults.

source

pub fn unlock(&mut self) -> Result<(), Error>

Unlocks the physical pages in memory, allowing the operating system to swap out the pages backing this memory mapping.

source

pub fn flush(&self, range: Range<usize>) -> Result<(), Error>

Flushes a range of the memory mapping, i.e. this initiates writing dirty pages within that range to the disk. Dirty pages are those whose contents have changed since the file was mapped.

On Microsoft Windows, this function does not flush the file metadata. Thus, it must be followed with a call to File::sync_all to flush the file metadata. This also causes the flush operaton to be synchronous.

On other platforms, the flush operation is synchronous, i.e. this waits until the flush operation completes.

source

pub fn flush_async(&self, range: Range<usize>) -> Result<(), Error>

Flushes a range of the memory mapping asynchronously, i.e. this initiates writing dirty pages within that range to the disk without waiting for the flush operation to complete. Dirty pages are those whose contents have changed since the file was mapped.

source

pub fn flush_icache(&self) -> Result<(), Error>

This function can be used to flush the instruction cache on architectures where this is required.

While the x86 and x86-64 architectures guarantee cache coherency between the L1 instruction and the L1 data cache, other architectures such as Arm and AArch64 do not. If the user modified the pages, then executing the code may result in undefined behavior. To ensure correct behavior a user has to flush the instruction cache after modifying and before executing the page.

source

pub fn make_none(self) -> Result<MmapNone, (Self, Error)>

Remaps this memory mapping as inaccessible.

In case of failure, this returns the ownership of self.

source

pub fn make_read_only(self) -> Result<Mmap, (Self, Error)>

Remaps this memory mapping as immutable.

In case of failure, this returns the ownership of self. If you are not interested in this feature, you can use the implementation of the TryFrom trait instead.

source

pub fn make_exec(self) -> Result<Mmap, (Self, Error)>

Remaps this memory mapping as executable.

In case of failure, this returns the ownership of self.

source

pub unsafe fn make_exec_no_flush(self) -> Result<Mmap, (Self, Error)>

Remaps this memory mapping as executable, but does not flush the instruction cache.

§Safety

While the x86 and x86-64 architectures guarantee cache coherency between the L1 instruction and the L1 data cache, other architectures such as Arm and AArch64 do not. If the user modified the pages, then executing the code may result in undefined behavior. To ensure correct behavior a user has to flush the instruction cache after modifying and before executing the page.

In case of failure, this returns the ownership of self.

source

pub fn make_mut(self) -> Result<MmapMut, (Self, Error)>

Remaps this mapping to be mutable.

In case of failure, this returns the ownership of self. If you are not interested in this feature, you can use the implementation of the TryFrom trait instead.

source

pub unsafe fn make_exec_mut(self) -> Result<MmapMut, (Self, Error)>

Remaps this mapping to be executable and mutable.

While this may seem useful for self-modifying code and JIT engines, it is instead recommended to convert between mutable and executable mappings using Mmap::make_mut() and MmapMut::make_exec() instead.

Make sure to read the text below to understand the complications of this function before using it. The UnsafeMmapFlags::JIT flag must be set for this function to succeed.

§Safety

RWX pages are an interesting targets to attackers, e.g. for buffer overflow attacks, as RWX mappings can potentially simplify such attacks. Without RWX mappings, attackers instead have to resort to return-oriented programming (ROP) gadgets. To prevent buffer overflow attacks, contemporary CPUs allow pages to be marked as non-executable which is then used by the operating system to ensure that pages are either marked as writeable or as executable, but not both. This is also known as W^X.

While the x86 and x86-64 architectures guarantee cache coherency between the L1 instruction and the L1 data cache, other architectures such as Arm and AArch64 do not. If the user modified the pages, then executing the code may result in undefined behavior. To ensure correct behavior a user has to flush the instruction cache after modifying and before executing the page.

In case of failure, this returns the ownership of self.

source§

impl MmapNone

source

pub fn start(&self) -> usize

Returns the start address of this mapping.

source

pub fn end(&self) -> usize

Returns the end address of this mapping.

source

pub fn as_ptr(&self) -> *const u8

Yields a raw immutable pointer of this mapping.

source

pub fn as_mut_ptr(&mut self) -> *mut u8

Yields a raw mutable pointer of this mapping.

source

pub fn size(&self) -> usize

Yields the size of this mapping.

source

pub fn merge(&mut self, other: Self) -> Result<(), (Error, Self)>

Merges the memory maps into one. The memory maps must be adjacent to each other and share the same attributes and backing. On success, this consumes the other memory map object. Otherwise, this returns an error together with the original memory map that failed to be merged.

source

pub fn split_off(&mut self, at: usize) -> Result<Self, Error>

Splits the memory map into two at the given byte offset. The byte offset must be page size aligned.

Afterwards self is limited to the range [0, at), and the returning memory mapping is limited to [at, len).

source

pub fn split_to(&mut self, at: usize) -> Result<Self, Error>

Splits the memory map into two at the given byte offset. The byte offset must be page size aligned.

Afterwards self is limited to the range [at, len), and the returning memory mapping is limited to [0, at).

Trait Implementations§

source§

impl Debug for MmapNone

source§

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

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

impl TryFrom<MmapNone> for Mmap

§

type Error = Error

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

fn try_from(mmap_none: MmapNone) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<MmapNone> for MmapMut

§

type Error = Error

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

fn try_from(mmap_none: MmapNone) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<ReservedNone> for MmapNone

§

type Error = Error

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

fn try_from(reserved_none: ReservedNone) -> Result<MmapNone, Error>

Performs the conversion.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.