cranelift::prelude

Struct MemFlags

pub struct MemFlags { /* private fields */ }
Expand description

Flags for memory operations like load/store.

Each of these flags introduce a limited form of undefined behavior. The flags each enable certain optimizations that need to make additional assumptions. Generally, the semantics of a program does not change when a flag is removed, but adding a flag will.

In addition, the flags determine the endianness of the memory access. By default, any memory access uses the native endianness determined by the target ISA. This can be overridden for individual accesses by explicitly specifying little- or big-endian semantics via the flags.

Implementations§

§

impl MemFlags

pub const fn new() -> MemFlags

Create a new empty set of flags.

pub const fn trusted() -> MemFlags

Create a set of flags representing an access from a “trusted” address, meaning it’s known to be aligned and non-trapping.

pub const fn alias_region(self) -> Option<AliasRegion>

Reads the alias region that this memory operation works with.

pub const fn with_alias_region(self, region: Option<AliasRegion>) -> MemFlags

Sets the alias region that this works on to the specified region.

pub fn set_alias_region(&mut self, region: Option<AliasRegion>)

Sets the alias region that this works on to the specified region.

pub fn set_by_name(&mut self, name: &str) -> Result<bool, &'static str>

Set a flag bit by name.

Returns true if the flag was found and set, false for an unknown flag name.

§Errors

Returns an error message if the name is known but couldn’t be applied due to it being a semantic error.

pub const fn endianness(self, native_endianness: Endianness) -> Endianness

Return endianness of the memory access. This will return the endianness explicitly specified by the flags if any, and will default to the native endianness otherwise. The native endianness has to be provided by the caller since it is not explicitly encoded in CLIF IR – this allows a front end to create IR without having to know the target endianness.

pub const fn explicit_endianness(self) -> Option<Endianness>

Return endianness of the memory access, if explicitly specified.

If the endianness is not explicitly specified, this will return None, which means “native endianness”.

pub fn set_endianness(&mut self, endianness: Endianness)

Set endianness of the memory access.

pub const fn with_endianness(self, endianness: Endianness) -> MemFlags

Set endianness of the memory access, returning new flags.

pub const fn notrap(self) -> bool

Test if this memory operation cannot trap.

By default MemFlags will assume that any load/store can trap and is associated with a TrapCode::HeapOutOfBounds code. If the trap code is configured to None though then this method will return true and indicates that the memory operation will not trap.

If this returns true then the memory is accessible, which means that accesses will not trap. This makes it possible to delete an unused load or a dead store instruction.

pub fn set_notrap(&mut self)

Sets the trap code for this MemFlags to None.

pub const fn with_notrap(self) -> MemFlags

Sets the trap code for this MemFlags to None, returning the new flags.

pub const fn aligned(self) -> bool

Test if the aligned flag is set.

By default, Cranelift memory instructions work with any unaligned effective address. If the aligned flag is set, the instruction is permitted to trap or return a wrong result if the effective address is misaligned.

pub fn set_aligned(&mut self)

Set the aligned flag.

pub const fn with_aligned(self) -> MemFlags

Set the aligned flag, returning new flags.

pub const fn readonly(self) -> bool

Test if the readonly flag is set.

Loads with this flag have no memory dependencies. This results in undefined behavior if the dereferenced memory is mutated at any time between when the function is called and when it is exited.

pub fn set_readonly(&mut self)

Set the readonly flag.

pub const fn with_readonly(self) -> MemFlags

Set the readonly flag, returning new flags.

pub const fn checked(self) -> bool

Test if the checked bit is set.

Loads and stores with this flag are verified to access pointers only with a validated PointsTo fact attached, and with that fact validated, when using the proof-carrying-code framework. If initial facts on program inputs are correct (i.e., correctly denote the shape and types of data structures in memory), and if PCC validates the compiled output, then all checked-marked memory accesses are guaranteed (up to the checker’s correctness) to access valid memory. This can be used to ensure memory safety and sandboxing.

pub fn set_checked(&mut self)

Set the checked bit.

pub const fn with_checked(self) -> MemFlags

Set the checked bit, returning new flags.

pub const fn trap_code(self) -> Option<TrapCode>

Get the trap code to report if this memory access traps.

A None trap code indicates that this memory access does not trap.

pub const fn with_trap_code(self, code: Option<TrapCode>) -> MemFlags

Configures these flags with the specified trap code code.

A trap code indicates that this memory operation cannot be optimized away and it must “stay where it is” in the programs. Traps are considered side effects, for example, and have meaning through the trap code that is communicated and which instruction trapped.

Trait Implementations§

§

impl Clone for MemFlags

§

fn clone(&self) -> MemFlags

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for MemFlags

§

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

Formats the value using the given formatter. Read more
§

impl<'de> Deserialize<'de> for MemFlags

§

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

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

impl Display for MemFlags

§

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

Formats the value using the given formatter. Read more
§

impl Hash for MemFlags

§

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

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
§

impl PartialEq for MemFlags

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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.
§

impl Serialize for MemFlags

§

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

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

impl Copy for MemFlags

§

impl Eq for MemFlags

§

impl StructuralPartialEq for MemFlags

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, dst: *mut u8)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

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