islet-project / islet

An on-device confidential computing platform
Apache License 2.0
93 stars 17 forks source link

Safe instance creation at given memory address #344

Closed bitboom closed 3 weeks ago

bitboom commented 3 months ago

This PR presents a quick draft for safely creating an instance at a given memory address. As discussed in https://github.com/islet-project/islet/pull/342, utilizing the idea proposed by @L0czek, this approach involves parsing the given address as uninitialized memory, then copying that instance to that address.

For REC and RD, the copy cost was not considered significant since the operation is called only once.

Concept

// Create Instance
let addr = GIVEN_ADDRESS;
let value = T;
unsafe {
  let src: core::mem::MaybeUninit<T> = core::mem::MaybeUninit::new(value);
  let src = &src as *const core::mem::MaybeUninit<T>;
  let dst = addr as *mut core::mem::MaybeUninit<T>;
  core::ptr::copy_nonoverlapping(src, dst, 1);
}

// Access Instance
let ptr = addr as *mut core::mem::MaybeUninit<T>;
let value = (*ptr).assume_init_ref();

API changed

- let mut rec = rec_granule.content_mut::<Rec<'_>>()?;
+ let mut rec = rec_granule.new_uninit_with::<Rec<'_>>(Rec::new())?;