Skip to content

Commit 01c6ea2

Browse files
committed
Auto merge of #56813 - oli-obk:main_🧶, r=pnkfelix
Always run rustc in a thread cc @ishitatsuyuki @eddyb r? @pnkfelix [Previously](#48575) we moved to only producing threads when absolutely necessary. Even before we opted to only create threads in some cases, which [is unsound](#48575 (comment)) due to the way we use thread local storage.
2 parents 6d34ec1 + 6b96827 commit 01c6ea2

File tree

8 files changed

+5
-103
lines changed

8 files changed

+5
-103
lines changed

src/librustc_driver/lib.rs

+5-62
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#![feature(rustc_diagnostic_macros)]
2626
#![feature(slice_sort_by_cached_key)]
2727
#![feature(set_stdio)]
28-
#![feature(rustc_stack_internals)]
2928
#![feature(no_debug)]
3029

3130
#![recursion_limit="256"]
@@ -1481,69 +1480,13 @@ pub fn in_named_rustc_thread<F, R>(name: String, f: F) -> Result<R, Box<dyn Any
14811480
where F: FnOnce() -> R + Send + 'static,
14821481
R: Send + 'static,
14831482
{
1484-
#[cfg(all(unix, not(target_os = "haiku")))]
1485-
let spawn_thread = unsafe {
1486-
// Fetch the current resource limits
1487-
let mut rlim = libc::rlimit {
1488-
rlim_cur: 0,
1489-
rlim_max: 0,
1490-
};
1491-
if libc::getrlimit(libc::RLIMIT_STACK, &mut rlim) != 0 {
1492-
let err = io::Error::last_os_error();
1493-
error!("in_rustc_thread: error calling getrlimit: {}", err);
1494-
true
1495-
} else if rlim.rlim_max < STACK_SIZE as libc::rlim_t {
1496-
true
1497-
} else if rlim.rlim_cur < STACK_SIZE as libc::rlim_t {
1498-
std::rt::deinit_stack_guard();
1499-
rlim.rlim_cur = STACK_SIZE as libc::rlim_t;
1500-
if libc::setrlimit(libc::RLIMIT_STACK, &mut rlim) != 0 {
1501-
let err = io::Error::last_os_error();
1502-
error!("in_rustc_thread: error calling setrlimit: {}", err);
1503-
std::rt::update_stack_guard();
1504-
true
1505-
} else {
1506-
std::rt::update_stack_guard();
1507-
false
1508-
}
1509-
} else {
1510-
false
1511-
}
1512-
};
1513-
1514-
// We set the stack size at link time. See src/rustc/rustc.rs.
1515-
#[cfg(windows)]
1516-
let spawn_thread = false;
1517-
1518-
#[cfg(target_os = "haiku")]
1519-
let spawn_thread = unsafe {
1520-
// Haiku does not have setrlimit implemented for the stack size.
1521-
// By default it does have the 16 MB stack limit, but we check this in
1522-
// case the minimum STACK_SIZE changes or Haiku's defaults change.
1523-
let mut rlim = libc::rlimit {
1524-
rlim_cur: 0,
1525-
rlim_max: 0,
1526-
};
1527-
if libc::getrlimit(libc::RLIMIT_STACK, &mut rlim) != 0 {
1528-
let err = io::Error::last_os_error();
1529-
error!("in_rustc_thread: error calling getrlimit: {}", err);
1530-
true
1531-
} else if rlim.rlim_cur >= STACK_SIZE {
1532-
false
1533-
} else {
1534-
true
1535-
}
1536-
};
1537-
1538-
#[cfg(not(any(windows, unix)))]
1539-
let spawn_thread = true;
1540-
1541-
// The or condition is added from backward compatibility.
1542-
if spawn_thread || env::var_os("RUST_MIN_STACK").is_some() {
1483+
// We need a thread for soundness of thread local storage in rustc. For debugging purposes
1484+
// we allow an escape hatch where everything runs on the main thread.
1485+
if env::var_os("RUSTC_UNSTABLE_NO_MAIN_THREAD").is_none() {
15431486
let mut cfg = thread::Builder::new().name(name);
15441487

1545-
// FIXME: Hacks on hacks. If the env is trying to override the stack size
1546-
// then *don't* set it explicitly.
1488+
// If the env is trying to override the stack size then *don't* set it explicitly.
1489+
// The libstd thread impl will fetch the `RUST_MIN_STACK` env var itself.
15471490
if env::var_os("RUST_MIN_STACK").is_none() {
15481491
cfg = cfg.stack_size(STACK_SIZE);
15491492
}

src/libstd/rt.rs

-15
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,3 @@ fn lang_start<T: ::process::Termination + 'static>
7373
{
7474
lang_start_internal(&move || main().report(), argc, argv)
7575
}
76-
77-
/// Function used for reverting changes to the main stack before setrlimit().
78-
/// This is POSIX (non-Linux) specific and unlikely to be directly stabilized.
79-
#[unstable(feature = "rustc_stack_internals", issue = "0")]
80-
pub unsafe fn deinit_stack_guard() {
81-
::sys::thread::guard::deinit();
82-
}
83-
84-
/// Function used for resetting the main stack guard address after setrlimit().
85-
/// This is POSIX specific and unlikely to be directly stabilized.
86-
#[unstable(feature = "rustc_stack_internals", issue = "0")]
87-
pub unsafe fn update_stack_guard() {
88-
let main_guard = ::sys::thread::guard::init();
89-
::sys_common::thread_info::reset_guard(main_guard);
90-
}

src/libstd/sys/cloudabi/thread.rs

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ pub mod guard {
121121
pub unsafe fn init() -> Option<Guard> {
122122
None
123123
}
124-
pub unsafe fn deinit() {}
125124
}
126125

127126
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {

src/libstd/sys/redox/thread.rs

-1
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,4 @@ pub mod guard {
9292
pub type Guard = !;
9393
pub unsafe fn current() -> Option<Guard> { None }
9494
pub unsafe fn init() -> Option<Guard> { None }
95-
pub unsafe fn deinit() {}
9695
}

src/libstd/sys/sgx/thread.rs

-1
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,4 @@ pub mod guard {
9797
pub type Guard = !;
9898
pub unsafe fn current() -> Option<Guard> { None }
9999
pub unsafe fn init() -> Option<Guard> { None }
100-
pub unsafe fn deinit() {}
101100
}

src/libstd/sys/unix/thread.rs

-21
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ pub mod guard {
211211
pub type Guard = Range<usize>;
212212
pub unsafe fn current() -> Option<Guard> { None }
213213
pub unsafe fn init() -> Option<Guard> { None }
214-
pub unsafe fn deinit() {}
215214
}
216215

217216

@@ -355,26 +354,6 @@ pub mod guard {
355354
}
356355
}
357356

358-
pub unsafe fn deinit() {
359-
if !cfg!(target_os = "linux") {
360-
if let Some(stackaddr) = get_stack_start_aligned() {
361-
// Remove the protection on the guard page.
362-
// FIXME: we cannot unmap the page, because when we mmap()
363-
// above it may be already mapped by the OS, which we can't
364-
// detect from mmap()'s return value. If we unmap this page,
365-
// it will lead to failure growing stack size on platforms like
366-
// macOS. Instead, just restore the page to a writable state.
367-
// This ain't Linux, so we probably don't need to care about
368-
// execstack.
369-
let result = mprotect(stackaddr, PAGE_SIZE, PROT_READ | PROT_WRITE);
370-
371-
if result != 0 {
372-
panic!("unable to reset the guard page");
373-
}
374-
}
375-
}
376-
}
377-
378357
#[cfg(any(target_os = "macos",
379358
target_os = "bitrig",
380359
target_os = "openbsd",

src/libstd/sys/wasm/thread.rs

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub mod guard {
6868
pub type Guard = !;
6969
pub unsafe fn current() -> Option<Guard> { None }
7070
pub unsafe fn init() -> Option<Guard> { None }
71-
pub unsafe fn deinit() {}
7271
}
7372

7473
cfg_if! {

src/libstd/sys/windows/thread.rs

-1
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,4 @@ pub mod guard {
9898
pub type Guard = !;
9999
pub unsafe fn current() -> Option<Guard> { None }
100100
pub unsafe fn init() -> Option<Guard> { None }
101-
pub unsafe fn deinit() {}
102101
}

0 commit comments

Comments
 (0)