forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
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 and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix SGX RWLock representation for UnsafeCell niche fix #1
Merged
pnkfelix
merged 1 commit into
pnkfelix:hide-niches-under-unsafe-cell
from
jethrogb:jb/sgx-unsafecell-niche-fix
Feb 3, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand this test, and it's "zero the stack" thing - if some bytes are uninitialized, the comparison is UB, and there's no real way around that (without the proposed LLVM
freeze
operation), AFAIK.And
MaybeUninit::<RWLock>::zeroed()
has the same semantics as[0u8; 144]
, doesn't it? So there's no way any bytes wouldn't be zeroed.I know this is offtopic, but I'm a bit worried this test (and perhaps more like it?) isn't actually testing what it's supposed to (cc @RalfJung)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transmute::<_, RWLock>(*RWLOCK_INIT)
needs to be equivalent toRWLock::new()
. This test tries to assert that. Since part of the representation ofRWLock::new()
may contain uninitialized memory, some trickery (and UB) is needed, but this (trying very hard to get the uninitialized memory to be 0) is the best we can do.Yes, this is exactly what the comment just above this code block tries to explain (“issue with the test code”). However, this test has been working fine and this is the best we can do (I think? Happy to hear alternatives!). If changes to codegen are made that make this test fail, we can consider other options at that time. Note that UB only occurs during the execution of this test, normal use of this primitive never uses uninitialized memory.
I'm not sure what you mean by this. Where are we using
[0u8; 144]
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not; the former resets any padding to
Undef
as happens any time a struct is copied.If the test deliberately causes UB, it should have a comment saying that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right I forgot about that (sadly we don't tell LLVM about this, since we just use a plain
memcpy
).Wouldn't that also imply that it's impossible for
RWLock::new()
to guarantee the padding bytes ofRWLock
are initialized to 0, which this tests tries to prove (and arguably fails because its comparison of undef bytes is UB)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. (I am not sure if
RwLock
has any padding bytes, though.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to test for "no padding bytes", I wonder if this test should rely on const-eval instead, e.g. transmuting to
[u8; 144]
in aconst
, in a way that would cause an error if any of the bytes wereundef
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jethrogb is it crucial that
RwLock::new()
returns something all-0, or is it just crucial that using something all-0 instead ofRwLock::new()
must be fine? If the answer is the latter, when padding wouldn't actually be a problem (except for the test).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay turns out I was very misled by the "try hard to make sure" comment - it doesn't describe what is being tested, but specifically the
zero_stack()
call.What this test appears to be checking is that the hardcoded bytes in
RWLOCK_INIT
(which presumably are also hardcoded on the C side) are the same asRWLock::new()
ignoring padding.But there's no way to automatically ignore padding, so what's being done here is trying to zero the stack space where
RWLock::new()
(and all of its transitive callers) writes every part of theRWLock
, so that the padding likely stays zeroed.I'm less worried about the test, but the comment could probably be improved to make it clearer that
RWLock::new()
having padding left uninitialized is not an issue, it just makes testing the initialized bytes harder.I think the simplest way to get zeroed data (although I don't know how UB it is) is to use a
static
, e.g.: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3abaa186579b5522baad9f492a784087 (that works even on stable, heh).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latter
@eddyb I think you and @RalfJung have a pretty good idea of what I'm trying to achieve here, and you also have a better understanding of undef than I have, would you mind drafting a comment that makes sense to you?
Neat, I think this is a pretty good way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if the
static
works, it's easier to explain that theundef
parts will get zero-initialized in practice.To be honest, one of the most confusing things to me was that
zero_stack();
didn't have an empty line after it, which made me assume incorrectly what the comment referred to (if I remove thezero_stack()
call, the test fails, and I think when I saw that I understood what the comment meant).A
static
should be clearer from the start compared to zeroing the stack in an unguaranteed way.