Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ci/docker/dist-various-2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ RUN /tmp/build-solaris-toolchain.sh sparcv9 sparcv9 solaris-sparc
COPY dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/
# We pass the commit id of the port of LLVM's libunwind to the build script.
# Any update to the commit id here, should cause the container image to be re-built from this point on.
RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "53b586346f2c7870e20b170decdc30729d97c42b"
RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "63b1b1c5b1ca008099095aa0f8a081a80c8d27f4"

COPY dist-various-2/build-wasi-toolchain.sh /tmp/
RUN /tmp/build-wasi-toolchain.sh
Expand Down
24 changes: 13 additions & 11 deletions src/libstd/sys/sgx/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub struct RWLock {
writer: SpinMutex<WaitVariable<bool>>,
}

// Below is to check at compile time, that RWLock has size of 128 bytes.
// Check at compile time that RWLock size matches C definition (see test_c_rwlock_initializer below)
#[allow(dead_code)]
unsafe fn rw_lock_size_assert(r: RWLock) {
mem::transmute::<RWLock, [u8; 128]>(r);
mem::transmute::<RWLock, [u8; 144]>(r);
}

impl RWLock {
Expand Down Expand Up @@ -210,15 +210,17 @@ mod tests {
// be changed too.
#[test]
fn test_c_rwlock_initializer() {
#[rustfmt::skip]
const RWLOCK_INIT: &[u8] = &[
0x1, 0x0, 0x0, 0x0, 0x0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0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
/* 0x50 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
/* 0x60 */ 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
/* 0x70 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
/* 0x80 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
];

#[inline(never)]
Expand All @@ -239,7 +241,7 @@ mod tests {
zero_stack();
let mut init = MaybeUninit::<RWLock>::zeroed();
rwlock_new(&mut init);
assert_eq!(mem::transmute::<_, [u8; 128]>(init.assume_init()).as_slice(), RWLOCK_INIT)
assert_eq!(mem::transmute::<_, [u8; 144]>(init.assume_init()).as_slice(), RWLOCK_INIT)
Copy link

@eddyb eddyb Jan 31, 2020

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)

Copy link
Author

@jethrogb jethrogb Jan 31, 2020

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

transmute::<_, RWLock>(*RWLOCK_INIT) needs to be equivalent to RWLock::new(). This test tries to assert that. Since part of the representation of RWLock::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.

if some bytes are uninitialized, the comparison is UB, and there's no real way around that (without the proposed LLVM freeze operation), AFAIK.

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.

And MaybeUninit::::zeroed() has the same semantics as [0u8; 144], doesn't it? So there's no way any bytes wouldn't be zeroed.

I'm not sure what you mean by this. Where are we using [0u8; 144]?

Copy link

@RalfJung RalfJung Jan 31, 2020

Choose a reason for hiding this comment

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

And MaybeUninit::::zeroed() has the same semantics as [0u8; 144], doesn't it?

It's not; the former resets any padding to Undef as happens any time a struct is copied.

Note that UB only occurs during the execution of this test, normal use of this primitive never uses uninitialized memory.

If the test deliberately causes UB, it should have a comment saying that.

Copy link

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.

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 of RWLock are initialized to 0, which this tests tries to prove (and arguably fails because its comparison of undef bytes is UB)?

Copy link

Choose a reason for hiding this comment

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

Wouldn't that also imply that it's impossible for RWLock::new() to guarantee the padding bytes of RWLock are initialized to 0

Yes. (I am not sure if RwLock has any padding bytes, though.)

Copy link

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 a const, in a way that would cause an error if any of the bytes were undef.

Copy link

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 of RwLock::new() must be fine? If the answer is the latter, when padding wouldn't actually be a problem (except for the test).

Copy link

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 as RWLock::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 the RWLock, 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).

Copy link
Author

Choose a reason for hiding this comment

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

is it crucial that RwLock::new() returns something all-0, or is it just crucial that using something all-0 instead of RwLock::new() must be fine?

The latter

the comment could probably be improved

@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?

I think the simplest way to get zeroed data (although I don't know how UB it is) is to use a static

Neat, I think this is a pretty good way.

Copy link

Choose a reason for hiding this comment

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

would you mind drafting a comment that makes sense to you?

Well, if the static works, it's easier to explain that the undef 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 the zero_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.

};
}
}