Implement read_buf and vectored read/write for SGX stdio by thaliaarchi · Pull Request #137355 · rust-lang/rust (original) (raw)

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation25 Commits3 Checks6 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

thaliaarchi

Implement read_buf, read_vectored, and write_vectored for the SGX stdio types.

Additionally, extend User<T>::copy_to_enclave to work for copying to uninitialized values and fix unsoundness in UserRef<[T]>::copy_to_enclave_vec.

cc @jethrogb

Tracked in #136756

@rustbot

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-SGX

Target: SGX

S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-libs

Relevant to the library team, which will review and decide on the PR/issue.

labels

Feb 21, 2025

@thaliaarchi

@rustbot rustbot added S-blocked

Status: Blocked on something else such as an RFC or other implementation work.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Feb 25, 2025

@jethrogb

jethrogb

/// # Panics
/// This function panics if the destination doesn't have the same length as
/// the source.
pub fn copy_to_enclave_uninit(&self, dest: &mut [MaybeUninit]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needing this “feels” wrong to me. You could do impl<T: UserSafeSized> UserSafeSized for MaybeUninit<T> and then just create a [MaybeUninit<u8>] in read_buf. However, of course the whole point of UserSafe is that you want to explicitly assume everything you copy out of userspace is initialized, so that would kind of defeat the point.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both of those options are unsatisfying to you, what would you recommend I do so this can impl read_buf?

@jethrogb

The versions of read and write in usercalls are vectored

This was explicitly requested by the T-libs reviewer.

@thaliaarchi

The versions of read and write in usercalls are vectored

This was explicitly requested by the T-libs reviewer.

I'm fine with reverting my change to this. Since new_from_enclave just defers to new_uninit_bytes and copy_from_enclave, which is what the vectored version uses, the only difference is saving a few arithmetic ops.

@thaliaarchi

I noticed that UserRef<[T]>::copy_to_enclave_vec reinterprets uninitialized memory as initialized and does not drop existing elements of the Vec, so I pushed a fix for it which uses the new copy_to_enclave_uninit. I think this supports the need for such a routine.

@jethrogb

Perhaps copy_to_enclave needs to be replaced in its entirety with your version?

@thaliaarchi

Perhaps copy_to_enclave needs to be replaced in its entirety with your version?

I think the two would actually be better coexisting, since they serve slightly different purposes. copy_to_enclave copies from a UserRef<T> to a &mut T, whereas copy_to_enclave_uninit copies from a UserRef<[T]> to a &mut [MaybeUninit<T>]. Since MaybeUninit<T> requires T: Sized, a routine which copies from a UserRef<T> to a &mut MaybeUninit<T> wouldn't work for slices.

@jethrogb

How about this?

// SAFETY: Requires that T is contained within Self using transparent representation unsafe trait UserSafeCopyDestination<T: ?Sized> { fn as_mut_ptr(&mut self) -> *mut T; }

unsafe impl UserSafeCopyDestination for T { fn as_mut_ptr(&mut self) -> *mut T { self as _ } }

unsafe impl UserSafeCopyDestination<[T]> for [T] { fn as_mut_ptr(&mut self) -> *mut [T] { self as _ } }

unsafe impl UserSafeCopyDestination for MaybeUninit { fn as_mut_ptr(&mut self) -> *mut T { self as *mut Self as _ } }

unsafe impl UserSafeCopyDestination<[T]> for [MaybeUninit] { fn as_mut_ptr(&mut self) -> *mut [T] { self as *mut Self as _ } }

impl<T: ?Sized> UserRef { pub fn copy_to_enclave<V: ?Sized + UserSafeCopyDestination>(&self, dest: &mut V) { unsafe { assert_eq!(mem::size_of_val(dest), mem::size_of_val(&*self.0.get())); copy_from_userspace( self.0.get() as *const T as *const u8, dest.as_mut_ptr() as *mut u8, mem::size_of_val(dest), ); } } }

@thaliaarchi

That's much more flexible! I've added a commit with your patch, which I attributed to you. You might want to double-check that the metadata looks good.

(FYI, it looks like your fortanix.com email isn't connected to your GitHub account, so you're not linked.)

@thaliaarchi

And since we've been talking about SGX copying APIs, I think UserRef<[T]>::copy_to_enclave_vec would be slightly better if it didn't clear existing elements and instead just appended. It's easy for the caller to .clear(), if they want, and this seems to better match other APIs that take a &mut Vec<T>.

That would be a breaking change, but I see no use of it outside of std.

@bors

Jethro Beekman and others added 2 commits

March 10, 2025 00:45

@thaliaarchi

Co-authored-by: Thalia Archibald thalia@archibald.dev

@thaliaarchi

@thaliaarchi

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

and removed S-blocked

Status: Blocked on something else such as an RFC or other implementation work.

labels

Mar 10, 2025

@thaliaarchi

I had neglected to update the set_len for the append-style logic. I've pushed a fix for that (so it needs a reapproval).

@ChrisDenton

@bors bors added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

labels

Mar 12, 2025

@ChrisDenton

@bors

📌 Commit c62aa0b has been approved by ChrisDenton

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Mar 12, 2025

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Mar 13, 2025

@matthiaskrgr

…x, r=ChrisDenton

Implement read_buf and vectored read/write for SGX stdio

Implement read_buf, read_vectored, and write_vectored for the SGX stdio types.

Additionally, extend User<T>::copy_to_enclave to work for copying to uninitialized values and fix unsoundness in UserRef<[T]>::copy_to_enclave_vec.

cc @jethrogb

Tracked in rust-lang#136756

bors added a commit to rust-lang-ci/rust that referenced this pull request

Mar 13, 2025

@bors

…iaskrgr

Rollup of 9 pull requests

Successful merges:

r? @ghost @rustbot modify labels: rollup

bors added a commit to rust-lang-ci/rust that referenced this pull request

Mar 13, 2025

@bors

…iaskrgr

Rollup of 8 pull requests

Successful merges:

r? @ghost @rustbot modify labels: rollup

rust-timer added a commit to rust-lang-ci/rust that referenced this pull request

Mar 13, 2025

@rust-timer

Rollup merge of rust-lang#137355 - thaliaarchi:io-optional-methods/sgx, r=ChrisDenton

Implement read_buf and vectored read/write for SGX stdio

Implement read_buf, read_vectored, and write_vectored for the SGX stdio types.

Additionally, extend User<T>::copy_to_enclave to work for copying to uninitialized values and fix unsoundness in UserRef<[T]>::copy_to_enclave_vec.

cc @jethrogb

Tracked in rust-lang#136756

github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request

Mar 14, 2025

@matthiaskrgr

…x, r=ChrisDenton

Implement read_buf and vectored read/write for SGX stdio

Implement read_buf, read_vectored, and write_vectored for the SGX stdio types.

Additionally, extend User<T>::copy_to_enclave to work for copying to uninitialized values and fix unsoundness in UserRef<[T]>::copy_to_enclave_vec.

cc @jethrogb

Tracked in rust-lang#136756

thaliaarchi added a commit to thaliaarchi/rust that referenced this pull request

Mar 18, 2025

@thaliaarchi

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

Kobzol added a commit to Kobzol/rust that referenced this pull request

Mar 21, 2025

@Kobzol

…orkingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

tgross35 added a commit to tgross35/rust that referenced this pull request

Mar 22, 2025

@tgross35

…orkingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

jieyouxu added a commit to jieyouxu/rust that referenced this pull request

Mar 23, 2025

@jieyouxu

…orkingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Mar 23, 2025

@matthiaskrgr

…orkingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Mar 23, 2025

@matthiaskrgr

…orkingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

compiler-errors added a commit to compiler-errors/rust that referenced this pull request

Mar 23, 2025

@compiler-errors

…orkingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

rust-timer added a commit to rust-lang-ci/rust that referenced this pull request

Mar 24, 2025

@rust-timer

Rollup merge of rust-lang#138631 - thaliaarchi:sgx-read-buf-test, r=workingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request

Apr 2, 2025

@thaliaarchi

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request

Apr 2, 2025

@compiler-errors

…orkingjubilee

Update test for SGX now implementing read_buf

In rust-lang#108326, read_buf was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that rust-lang#137355 implemented it for SGX types.

cc @jethrogb

Labels

O-SGX

Target: SGX

S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

T-libs

Relevant to the library team, which will review and decide on the PR/issue.