Skip to content

Commit 4b85f27

Browse files
committed
Fix SGX implementation
1 parent 035c0c9 commit 4b85f27

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

library/std/src/sys/pal/sgx/abi/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use core::arch::global_asm;
44
use core::sync::atomic::{Atomic, AtomicUsize, Ordering};
55

6+
use crate::alloc::System;
67
use crate::io::Write;
78

89
// runtime features
@@ -63,7 +64,9 @@ unsafe extern "C" fn tcs_init(secondary: bool) {
6364
#[unsafe(no_mangle)]
6465
extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> EntryReturn {
6566
// FIXME: how to support TLS in library mode?
66-
let tls = Box::new(tls::Tls::new());
67+
// We use the System allocator here such that the global allocator may use
68+
// thread-locals.
69+
let tls = Box::new_in(tls::Tls::new(), System);
6770
let tls_guard = unsafe { tls.activate() };
6871

6972
if secondary {

library/std/src/sys/pal/sgx/abi/tls/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ impl Tls {
8989
ActiveTls { tls: self }
9090
}
9191

92-
#[allow(unused)]
93-
pub unsafe fn activate_persistent(self: Box<Self>) {
94-
// FIXME: Needs safety information. See entry.S for `set_tls_ptr` definition.
95-
let ptr = Box::into_raw(self).cast_const().cast::<u8>();
96-
unsafe { set_tls_ptr(ptr) };
97-
}
98-
9992
unsafe fn current<'a>() -> &'a Tls {
10093
// FIXME: Needs safety information. See entry.S for `set_tls_ptr` definition.
10194
unsafe { &*(get_tls_ptr() as *const Tls) }

library/std/src/sys/thread/sgx.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use crate::io;
44
use crate::sys::pal::abi::{thread, usercalls};
5+
use crate::thread::ThreadInit;
56
use crate::time::Duration;
67

78
pub struct Thread(task_queue::JoinHandle);
@@ -13,6 +14,7 @@ pub use self::task_queue::JoinNotifier;
1314
mod task_queue {
1415
use super::wait_notify;
1516
use crate::sync::{Mutex, MutexGuard};
17+
use crate::thread::ThreadInit;
1618

1719
pub type JoinHandle = wait_notify::Waiter;
1820

@@ -25,19 +27,20 @@ mod task_queue {
2527
}
2628

2729
pub(super) struct Task {
28-
p: Box<dyn FnOnce() + Send>,
30+
init: Box<ThreadInit>,
2931
done: JoinNotifier,
3032
}
3133

3234
impl Task {
33-
pub(super) fn new(p: Box<dyn FnOnce() + Send>) -> (Task, JoinHandle) {
35+
pub(super) fn new(init: Box<ThreadInit>) -> (Task, JoinHandle) {
3436
let (done, recv) = wait_notify::new();
3537
let done = JoinNotifier(Some(done));
36-
(Task { p, done }, recv)
38+
(Task { init, done }, recv)
3739
}
3840

3941
pub(super) fn run(self) -> JoinNotifier {
40-
(self.p)();
42+
let rust_start = self.init.init();
43+
rust_start();
4144
self.done
4245
}
4346
}
@@ -93,14 +96,10 @@ pub mod wait_notify {
9396

9497
impl Thread {
9598
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
96-
pub unsafe fn new(
97-
_stack: usize,
98-
_name: Option<&str>,
99-
p: Box<dyn FnOnce() + Send>,
100-
) -> io::Result<Thread> {
99+
pub unsafe fn new(_stack: usize, init: Box<ThreadInit>) -> io::Result<Thread> {
101100
let mut queue_lock = task_queue::lock();
102101
unsafe { usercalls::launch_thread()? };
103-
let (task, handle) = task_queue::Task::new(p);
102+
let (task, handle) = task_queue::Task::new(init);
104103
queue_lock.push(task);
105104
Ok(Thread(handle))
106105
}

library/std/src/thread/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ pub mod local_impl {
218218
/// the global allocator works.
219219
pub(crate) struct ThreadInit {
220220
pub handle: Thread,
221-
pub rust_start: Box<dyn FnOnce()>,
221+
pub rust_start: Box<dyn FnOnce() + Send>,
222222
}
223223

224224
impl ThreadInit {
225225
/// Initialize the 'current thread' mechanism on this thread, returning the
226226
/// Rust entry point.
227-
pub fn init(self: Box<Self>) -> Box<dyn FnOnce()> {
227+
pub fn init(self: Box<Self>) -> Box<dyn FnOnce() + Send> {
228228
// Set the current thread before any (de)allocations on the global allocator occur,
229229
// so that it may call std::thread::current() in its implementation. This is also
230230
// why we take Box<Self>, to ensure the Box is not destroyed until after this point.

0 commit comments

Comments
 (0)