Skip to main content

UserRef

Struct UserRef 

Source
pub struct UserRef<T: ?Sized>(/* private fields */);
🔬This is a nightly-only experimental API. (sgx_platform #56975)
Available on Fortanix and SGX only.
Expand description

A reference to some type in userspace memory. &UserRef<T> is equivalent to &T in enclave memory. Access to the memory is only allowed by copying to avoid TOCTTOU issues. After copying, code should make sure to completely check the value before use.

It is also possible to obtain a mutable reference &mut UserRef<T>. Unlike regular mutable references, these are not exclusive. Userspace may always write to the backing memory at any time, so it can’t be assumed that there the pointed-to memory is uniquely borrowed. The two different reference types are used solely to indicate intent: a mutable reference is for writing to user memory, an immutable reference for reading from user memory.

Implementations§

Source§

impl<T> UserRef<T>
where T: UserSafe + ?Sized,

Source

pub unsafe fn from_ptr<'a>(ptr: *const T) -> &'a Self

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Creates a &UserRef<[T]> from a raw pointer.

§Safety

The caller must ensure ptr points to T.

§Panics

This function panics if:

  • The pointer is not aligned
  • The pointer is null
  • The pointed-to range is not in user memory
Source

pub unsafe fn from_mut_ptr<'a>(ptr: *mut T) -> &'a mut Self

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Creates a &mut UserRef<[T]> from a raw pointer. See the struct documentation for the nuances regarding a &mut UserRef<T>.

§Safety

The caller must ensure ptr points to T.

§Panics

This function panics if:

  • The pointer is not aligned
  • The pointer is null
  • The pointed-to range is not in user memory
Source

pub fn copy_from_enclave(&mut self, val: &T)

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Copies val into user memory.

§Panics

This function panics if the destination doesn’t have the same size as the source. This can happen for dynamically-sized types such as slices.

Source

pub fn copy_to_enclave<U: ?Sized + UserSafeCopyDestination<T>>( &self, dest: &mut U, )

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Copies the value from user memory and place it into dest.

§Panics

This function panics if the destination doesn’t have the same size as the source. This can happen for dynamically-sized types such as slices.

Source

pub fn as_raw_ptr(&self) -> *const T

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Obtain a raw pointer from this reference.

Source

pub fn as_raw_mut_ptr(&mut self) -> *mut T

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Obtain a raw pointer from this reference.

Source§

impl<T> UserRef<T>
where T: UserSafe,

Source

pub fn to_enclave(&self) -> T

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Copies the value from user memory into enclave memory.

Source§

impl<T> UserRef<[T]>
where [T]: UserSafe,

Source

pub unsafe fn from_raw_parts<'a>(ptr: *const T, len: usize) -> &'a Self

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Creates a &UserRef<[T]> from a raw thin pointer and a slice length.

§Safety

The caller must ensure ptr points to n elements of T.

§Panics

This function panics if:

  • The pointer is not aligned
  • The pointer is null
  • The pointed-to range does not fit in the address space
  • The pointed-to range is not in user memory
Source

pub unsafe fn from_raw_parts_mut<'a>(ptr: *mut T, len: usize) -> &'a mut Self

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Creates a &mut UserRef<[T]> from a raw thin pointer and a slice length. See the struct documentation for the nuances regarding a &mut UserRef<T>.

§Safety

The caller must ensure ptr points to n elements of T.

§Panics

This function panics if:

  • The pointer is not aligned
  • The pointer is null
  • The pointed-to range does not fit in the address space
  • The pointed-to range is not in user memory
Source

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

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Obtain a raw pointer to the first element of this user slice.

Source

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

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Obtain a raw pointer to the first element of this user slice.

Source

pub fn len(&self) -> usize

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Obtain the number of elements in this user slice.

Source

pub fn append_to_enclave_vec(&self, dest: &mut Vec<T>)

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Copies the value from user memory and appends it to dest.

Source

pub fn to_enclave(&self) -> Vec<T>

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Copies the value from user memory into a vector in enclave memory.

Source

pub fn iter(&self) -> Iter<'_, T> ⓘ
where T: UserSafe,

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Returns an iterator over the slice.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
where T: UserSafe,

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Returns an iterator that allows modifying each value.

Source§

impl UserRef<ByteBuffer>

Source

pub fn copy_user_buffer(&self) -> Vec<u8> ⓘ

🔬This is a nightly-only experimental API. (sgx_platform #56975)

Copies the user memory range pointed to by the user ByteBuffer to enclave memory.

§Panics

This function panics if, in the user ByteBuffer:

  • The pointer is null
  • The pointed-to range does not fit in the address space
  • The pointed-to range is not in user memory

Trait Implementations§

Source§

impl<T: CoerceUnsized<U>, U> CoerceUnsized<UserRef<U>> for UserRef<T>

Source§

impl<T, I> Index<I> for UserRef<[T]>

Source§

type Output = UserRef<<I as SliceIndex<[T]>>::Output>

The returned type after indexing.
Source§

fn index(&self, index: I) -> &UserRef<I::Output>

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, I> IndexMut<I> for UserRef<[T]>

Source§

fn index_mut(&mut self, index: I) -> &mut UserRef<I::Output>

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for UserRef<T>

§

impl<T> !RefUnwindSafe for UserRef<T>

§

impl<T> !Sync for UserRef<T>

§

impl<T> Send for UserRef<T>
where UnsafeCell<T>: Send, T: ?Sized,

§

impl<T> Unpin for UserRef<T>
where UnsafeCell<T>: Unpin, T: ?Sized,

§

impl<T> UnsafeUnpin for UserRef<T>
where UnsafeCell<T>: UnsafeUnpin, T: ?Sized,

§

impl<T> UnwindSafe for UserRef<T>
where UnsafeCell<T>: UnwindSafe, T: ?Sized,

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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> UserSafeCopyDestination<T> for T

Source§

fn as_mut_ptr(&mut self) -> *mut T

🔬This is a nightly-only experimental API. (sgx_platform #56975)
Available on Fortanix and SGX only.
Returns a pointer for writing to the value.