Skip to content

Commit bb0067c

Browse files
committed
Auto merge of #76278 - jethrogb:jb/sgx-rwlock-init-test, r=Mark-Simulacrum
Improve SGX RWLock initializer test r? `@eddyb` This addresses pnkfelix#1 (comment) Fixes fortanix/rust-sgx#213
2 parents 57c5f40 + 0b5e681 commit bb0067c

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed
+11-23
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use super::*;
2-
use crate::mem::{self, MaybeUninit};
3-
use core::array::FixedSizeArray;
42

5-
// Verify that the bytes of initialized RWLock are the same as in
6-
// libunwind. If they change, `src/UnwindRustSgx.h` in libunwind needs to
7-
// be changed too.
3+
// Verify that the byte pattern libunwind uses to initialize an RWLock is
4+
// equivalent to the value of RWLock::new(). If the value changes,
5+
// `src/UnwindRustSgx.h` in libunwind needs to be changed too.
86
#[test]
97
fn test_c_rwlock_initializer() {
108
#[rustfmt::skip]
11-
const RWLOCK_INIT: &[u8] = &[
9+
const C_RWLOCK_INIT: &[u8] = &[
1210
/* 0x00 */ 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1311
/* 0x10 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1412
/* 0x20 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
@@ -20,24 +18,14 @@ fn test_c_rwlock_initializer() {
2018
/* 0x80 */ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
2119
];
2220

23-
#[inline(never)]
24-
fn zero_stack() {
25-
test::black_box(MaybeUninit::<[RWLock; 16]>::zeroed());
26-
}
27-
28-
#[inline(never)]
29-
unsafe fn rwlock_new(init: &mut MaybeUninit<RWLock>) {
30-
init.write(RWLock::new());
31-
}
21+
// For the test to work, we need the padding/unused bytes in RWLock to be
22+
// initialized as 0. In practice, this is the case with statics.
23+
static RUST_RWLOCK_INIT: RWLock = RWLock::new();
3224

3325
unsafe {
34-
// try hard to make sure that the padding/unused bytes in RWLock
35-
// get initialized as 0. If the assertion below fails, that might
36-
// just be an issue with the test code and not with the value of
37-
// RWLOCK_INIT.
38-
zero_stack();
39-
let mut init = MaybeUninit::<RWLock>::zeroed();
40-
rwlock_new(&mut init);
41-
assert_eq!(mem::transmute::<_, [u8; 144]>(init.assume_init()).as_slice(), RWLOCK_INIT)
26+
// If the assertion fails, that not necessarily an issue with the value
27+
// of C_RWLOCK_INIT. It might just be an issue with the way padding
28+
// bytes are initialized in the test code.
29+
assert_eq!(&crate::mem::transmute_copy::<_, [u8; 144]>(&RUST_RWLOCK_INIT), C_RWLOCK_INIT);
4230
};
4331
}

0 commit comments

Comments
 (0)