Skip to content

Commit c212257

Browse files
authored
Rollup merge of rust-lang#146629 - joboet:reorganize-weak, r=ibraheemdev
std: reorganize the UNIX-internal `weak` module This moves the `dlsym`-based and weak-linkage versions of the `weak!` macro into separate files, both of which include a common test file. As a result, both versions will be tested on all the platforms where they are used. Since the `#[link_name]` arm of the `dlsym` version was unused, I've removed it. I've also removed the unused `raw_syscall!` and non-Linux `syscall!` macros and gated the `#[allow(dead_code, unused_macros)]` to only apply on non-Linux platforms, so compilation will fail if `weak` turns out to be unused on all platforms. The last change concerns the use of `dlsym!` on FreeBSD: it is only used once, to link against `sysctlbyname`. But that symbol is always available, so there is no need for weak linkage.
2 parents eaf3cac + e043a0b commit c212257

File tree

8 files changed

+232
-270
lines changed

8 files changed

+232
-270
lines changed

library/std/src/sys/pal/unix/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
use crate::io::ErrorKind;
44

5-
#[cfg(not(target_os = "espidf"))]
6-
#[macro_use]
7-
pub mod weak;
8-
95
#[cfg(target_os = "fuchsia")]
106
pub mod fuchsia;
117
pub mod futex;
@@ -19,6 +15,7 @@ pub mod stack_overflow;
1915
pub mod sync;
2016
pub mod thread_parking;
2117
pub mod time;
18+
pub mod weak;
2219

2320
#[cfg(target_os = "espidf")]
2421
pub fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {}

library/std/src/sys/pal/unix/stack_overflow.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ mod imp {
6969
use super::Handler;
7070
use super::thread_info::{delete_current_info, set_current_info, with_current_info};
7171
use crate::ops::Range;
72-
use crate::sync::OnceLock;
7372
use crate::sync::atomic::{Atomic, AtomicBool, AtomicPtr, AtomicUsize, Ordering};
7473
use crate::sys::pal::unix::os;
7574
use crate::{io, mem, ptr};
@@ -406,6 +405,10 @@ mod imp {
406405
} else if cfg!(all(target_os = "linux", target_env = "musl")) {
407406
install_main_guard_linux_musl(page_size)
408407
} else if cfg!(target_os = "freebsd") {
408+
#[cfg(not(target_os = "freebsd"))]
409+
return None;
410+
// The FreeBSD code cannot be checked on non-BSDs.
411+
#[cfg(target_os = "freebsd")]
409412
install_main_guard_freebsd(page_size)
410413
} else if cfg!(any(target_os = "netbsd", target_os = "openbsd")) {
411414
install_main_guard_bsds(page_size)
@@ -442,6 +445,7 @@ mod imp {
442445
}
443446

444447
#[forbid(unsafe_op_in_unsafe_fn)]
448+
#[cfg(target_os = "freebsd")]
445449
unsafe fn install_main_guard_freebsd(page_size: usize) -> Option<Range<usize>> {
446450
// FreeBSD's stack autogrows, and optionally includes a guard page
447451
// at the bottom. If we try to remap the bottom of the stack
@@ -453,38 +457,23 @@ mod imp {
453457
// by the security.bsd.stack_guard_page sysctl.
454458
// By default it is 1, checking once is enough since it is
455459
// a boot time config value.
456-
static PAGES: OnceLock<usize> = OnceLock::new();
460+
static PAGES: crate::sync::OnceLock<usize> = crate::sync::OnceLock::new();
457461

458462
let pages = PAGES.get_or_init(|| {
459-
use crate::sys::weak::dlsym;
460-
dlsym!(
461-
fn sysctlbyname(
462-
name: *const libc::c_char,
463-
oldp: *mut libc::c_void,
464-
oldlenp: *mut libc::size_t,
465-
newp: *const libc::c_void,
466-
newlen: libc::size_t,
467-
) -> libc::c_int;
468-
);
469463
let mut guard: usize = 0;
470464
let mut size = size_of_val(&guard);
471465
let oid = c"security.bsd.stack_guard_page";
472-
match sysctlbyname.get() {
473-
Some(fcn)
474-
if unsafe {
475-
fcn(
476-
oid.as_ptr(),
477-
(&raw mut guard).cast(),
478-
&raw mut size,
479-
ptr::null_mut(),
480-
0,
481-
) == 0
482-
} =>
483-
{
484-
guard
485-
}
486-
_ => 1,
487-
}
466+
467+
let r = unsafe {
468+
libc::sysctlbyname(
469+
oid.as_ptr(),
470+
(&raw mut guard).cast(),
471+
&raw mut size,
472+
ptr::null_mut(),
473+
0,
474+
)
475+
};
476+
if r == 0 { guard } else { 1 }
488477
});
489478
Some(guardaddr..guardaddr + pages * page_size)
490479
}

library/std/src/sys/pal/unix/weak.rs

Lines changed: 0 additions & 225 deletions
This file was deleted.

0 commit comments

Comments
 (0)