Skip to content
/ rust Public
forked from rust-lang/rust

Commit 9da804c

Browse files
authored
Rollup merge of rust-lang#138988 - madsmtm:internal-weak-macro-syntax, r=ibraheemdev
Change the syntax of the internal `weak!` macro Change the syntax to include parameter names and a trailing semicolon. Motivation: - Mirror the `syscall!` macro. - Allow rustfmt to format it (when wrapped in parentheses, and when not inside `cfg_if!`). - For better documentation (having the parameter names available in the source code is a bit nicer). - Allow a future improvement to this macro where we can sometimes use the symbol directly when it's statically known to be available (and thus need the parameter names to be available), see rust-lang#136868. r? libs
2 parents 429b676 + a7bafc0 commit 9da804c

File tree

9 files changed

+144
-80
lines changed

9 files changed

+144
-80
lines changed

library/std/src/sys/fs/unix.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ cfg_has_statx! {{
155155
enum STATX_STATE{ Unknown = 0, Present, Unavailable }
156156
static STATX_SAVED_STATE: AtomicU8 = AtomicU8::new(STATX_STATE::Unknown as u8);
157157

158-
syscall! {
158+
syscall!(
159159
fn statx(
160160
fd: c_int,
161161
pathname: *const c_char,
162162
flags: c_int,
163163
mask: libc::c_uint,
164-
statxbuf: *mut libc::statx
165-
) -> c_int
166-
}
164+
statxbuf: *mut libc::statx,
165+
) -> c_int;
166+
);
167167

168168
let statx_availability = STATX_SAVED_STATE.load(Ordering::Relaxed);
169169
if statx_availability == STATX_STATE::Unavailable as u8 {
@@ -1540,7 +1540,9 @@ impl File {
15401540
let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
15411541
// futimens requires Android API level 19
15421542
cvt(unsafe {
1543-
weak!(fn futimens(c_int, *const libc::timespec) -> c_int);
1543+
weak!(
1544+
fn futimens(fd: c_int, times: *const libc::timespec) -> c_int;
1545+
);
15441546
match futimens.get() {
15451547
Some(futimens) => futimens(self.as_raw_fd(), times.as_ptr()),
15461548
None => return Err(io::const_error!(
@@ -1556,7 +1558,9 @@ impl File {
15561558
use crate::sys::{time::__timespec64, weak::weak};
15571559

15581560
// Added in glibc 2.34
1559-
weak!(fn __futimens64(libc::c_int, *const __timespec64) -> libc::c_int);
1561+
weak!(
1562+
fn __futimens64(fd: c_int, times: *const __timespec64) -> c_int;
1563+
);
15601564

15611565
if let Some(futimens64) = __futimens64.get() {
15621566
let to_timespec = |time: Option<SystemTime>| time.map(|time| time.t.to_timespec64())

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

+40-12
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ impl FileDesc {
232232
// implementation if `preadv` is not available.
233233
#[cfg(all(target_os = "android", target_pointer_width = "64"))]
234234
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
235-
super::weak::syscall! {
235+
super::weak::syscall!(
236236
fn preadv(
237237
fd: libc::c_int,
238238
iovec: *const libc::iovec,
239239
n_iovec: libc::c_int,
240-
offset: off64_t
241-
) -> isize
242-
}
240+
offset: off64_t,
241+
) -> isize;
242+
);
243243

244244
let ret = cvt(unsafe {
245245
preadv(
@@ -257,7 +257,14 @@ impl FileDesc {
257257
// and its metadata from LLVM IR.
258258
#[no_sanitize(cfi)]
259259
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
260-
super::weak::weak!(fn preadv64(libc::c_int, *const libc::iovec, libc::c_int, off64_t) -> isize);
260+
super::weak::weak!(
261+
fn preadv64(
262+
fd: libc::c_int,
263+
iovec: *const libc::iovec,
264+
n_iovec: libc::c_int,
265+
offset: off64_t,
266+
) -> isize;
267+
);
261268

262269
match preadv64.get() {
263270
Some(preadv) => {
@@ -286,7 +293,14 @@ impl FileDesc {
286293
// use "weak" linking.
287294
#[cfg(target_vendor = "apple")]
288295
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
289-
super::weak::weak!(fn preadv(libc::c_int, *const libc::iovec, libc::c_int, off64_t) -> isize);
296+
super::weak::weak!(
297+
fn preadv(
298+
fd: libc::c_int,
299+
iovec: *const libc::iovec,
300+
n_iovec: libc::c_int,
301+
offset: off64_t,
302+
) -> isize;
303+
);
290304

291305
match preadv.get() {
292306
Some(preadv) => {
@@ -428,14 +442,14 @@ impl FileDesc {
428442
// implementation if `pwritev` is not available.
429443
#[cfg(all(target_os = "android", target_pointer_width = "64"))]
430444
pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
431-
super::weak::syscall! {
445+
super::weak::syscall!(
432446
fn pwritev(
433447
fd: libc::c_int,
434448
iovec: *const libc::iovec,
435449
n_iovec: libc::c_int,
436-
offset: off64_t
437-
) -> isize
438-
}
450+
offset: off64_t,
451+
) -> isize;
452+
);
439453

440454
let ret = cvt(unsafe {
441455
pwritev(
@@ -450,7 +464,14 @@ impl FileDesc {
450464

451465
#[cfg(all(target_os = "android", target_pointer_width = "32"))]
452466
pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
453-
super::weak::weak!(fn pwritev64(libc::c_int, *const libc::iovec, libc::c_int, off64_t) -> isize);
467+
super::weak::weak!(
468+
fn pwritev64(
469+
fd: libc::c_int,
470+
iovec: *const libc::iovec,
471+
n_iovec: libc::c_int,
472+
offset: off64_t,
473+
) -> isize;
474+
);
454475

455476
match pwritev64.get() {
456477
Some(pwritev) => {
@@ -479,7 +500,14 @@ impl FileDesc {
479500
// use "weak" linking.
480501
#[cfg(target_vendor = "apple")]
481502
pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
482-
super::weak::weak!(fn pwritev(libc::c_int, *const libc::iovec, libc::c_int, off64_t) -> isize);
503+
super::weak::weak!(
504+
fn pwritev(
505+
fd: libc::c_int,
506+
iovec: *const libc::iovec,
507+
n_iovec: libc::c_int,
508+
offset: off64_t,
509+
) -> isize;
510+
);
483511

484512
match pwritev.get() {
485513
Some(pwritev) => {

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -604,16 +604,16 @@ pub(super) fn copy_regular_files(reader: RawFd, writer: RawFd, max_len: u64) ->
604604
_ => true,
605605
};
606606

607-
syscall! {
607+
syscall!(
608608
fn copy_file_range(
609609
fd_in: libc::c_int,
610610
off_in: *mut libc::loff_t,
611611
fd_out: libc::c_int,
612612
off_out: *mut libc::loff_t,
613613
len: libc::size_t,
614-
flags: libc::c_uint
615-
) -> libc::ssize_t
616-
}
614+
flags: libc::c_uint,
615+
) -> libc::ssize_t;
616+
);
617617

618618
fn probe_copy_file_range_support() -> u8 {
619619
// In some cases, we cannot determine availability from the first
@@ -727,16 +727,16 @@ fn sendfile_splice(mode: SpliceMode, reader: RawFd, writer: RawFd, len: u64) ->
727727
// Android builds use feature level 14, but the libc wrapper for splice is
728728
// gated on feature level 21+, so we have to invoke the syscall directly.
729729
#[cfg(target_os = "android")]
730-
syscall! {
730+
syscall!(
731731
fn splice(
732732
srcfd: libc::c_int,
733733
src_offset: *const i64,
734734
dstfd: libc::c_int,
735735
dst_offset: *const i64,
736736
len: libc::size_t,
737-
flags: libc::c_int
738-
) -> libc::ssize_t
739-
}
737+
flags: libc::c_int,
738+
) -> libc::ssize_t;
739+
);
740740

741741
#[cfg(target_os = "linux")]
742742
use libc::splice;

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

+22-8
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,32 @@ mod imp {
424424

425425
let pages = PAGES.get_or_init(|| {
426426
use crate::sys::weak::dlsym;
427-
dlsym!(fn sysctlbyname(*const libc::c_char, *mut libc::c_void, *mut libc::size_t, *const libc::c_void, libc::size_t) -> libc::c_int);
427+
dlsym!(
428+
fn sysctlbyname(
429+
name: *const libc::c_char,
430+
oldp: *mut libc::c_void,
431+
oldlenp: *mut libc::size_t,
432+
newp: *const libc::c_void,
433+
newlen: libc::size_t,
434+
) -> libc::c_int;
435+
);
428436
let mut guard: usize = 0;
429437
let mut size = size_of_val(&guard);
430438
let oid = c"security.bsd.stack_guard_page";
431439
match sysctlbyname.get() {
432-
Some(fcn) if unsafe {
433-
fcn(oid.as_ptr(),
434-
(&raw mut guard).cast(),
435-
&raw mut size,
436-
ptr::null_mut(),
437-
0) == 0
438-
} => guard,
440+
Some(fcn)
441+
if unsafe {
442+
fcn(
443+
oid.as_ptr(),
444+
(&raw mut guard).cast(),
445+
&raw mut size,
446+
ptr::null_mut(),
447+
0,
448+
) == 0
449+
} =>
450+
{
451+
guard
452+
}
439453
_ => 1,
440454
}
441455
});

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,12 @@ impl Thread {
193193
// and its metadata from LLVM IR.
194194
#[no_sanitize(cfi)]
195195
pub fn set_name(name: &CStr) {
196-
weak! {
196+
weak!(
197197
fn pthread_setname_np(
198-
libc::pthread_t, *const libc::c_char
199-
) -> libc::c_int
200-
}
198+
thread: libc::pthread_t,
199+
name: *const libc::c_char,
200+
) -> libc::c_int;
201+
);
201202

202203
if let Some(f) = pthread_setname_np.get() {
203204
#[cfg(target_os = "nto")]
@@ -762,7 +763,9 @@ unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
762763
// We use dlsym to avoid an ELF version dependency on GLIBC_PRIVATE. (#23628)
763764
// We shouldn't really be using such an internal symbol, but there's currently
764765
// no other way to account for the TLS size.
765-
dlsym!(fn __pthread_get_minstack(*const libc::pthread_attr_t) -> libc::size_t);
766+
dlsym!(
767+
fn __pthread_get_minstack(attr: *const libc::pthread_attr_t) -> libc::size_t;
768+
);
766769

767770
match __pthread_get_minstack.get() {
768771
None => libc::PTHREAD_STACK_MIN,

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ impl Timespec {
123123

124124
// __clock_gettime64 was added to 32-bit arches in glibc 2.34,
125125
// and it handles both vDSO calls and ENOSYS fallbacks itself.
126-
weak!(fn __clock_gettime64(libc::clockid_t, *mut __timespec64) -> libc::c_int);
126+
weak!(
127+
fn __clock_gettime64(
128+
clockid: libc::clockid_t,
129+
tp: *mut __timespec64,
130+
) -> libc::c_int;
131+
);
127132

128133
if let Some(clock_gettime64) = __clock_gettime64.get() {
129134
let mut t = MaybeUninit::uninit();

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

+24-16
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{mem, ptr};
2929
// We can use true weak linkage on ELF targets.
3030
#[cfg(all(unix, not(target_vendor = "apple")))]
3131
pub(crate) macro weak {
32-
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
32+
(fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
3333
let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = {
3434
unsafe extern "C" {
3535
#[linkage = "extern_weak"]
@@ -62,10 +62,16 @@ impl<F: Copy> ExternWeak<F> {
6262
}
6363

6464
pub(crate) macro dlsym {
65-
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
66-
dlsym!(fn $name($($t),*) -> $ret, stringify!($name));
65+
(fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
66+
dlsym!(
67+
#[link_name = stringify!($name)]
68+
fn $name($($param : $t),*) -> $ret;
69+
);
6770
),
68-
(fn $name:ident($($t:ty),*) -> $ret:ty, $sym:expr) => (
71+
(
72+
#[link_name = $sym:expr]
73+
fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;
74+
) => (
6975
static DLSYM: DlsymWeak<unsafe extern "C" fn($($t),*) -> $ret> =
7076
DlsymWeak::new(concat!($sym, '\0'));
7177
let $name = &DLSYM;
@@ -143,15 +149,15 @@ unsafe fn fetch(name: &str) -> *mut libc::c_void {
143149

144150
#[cfg(not(any(target_os = "linux", target_os = "android")))]
145151
pub(crate) macro syscall {
146-
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
152+
(fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
147153
// FIXME(#115199): Rust currently omits weak function definitions
148154
// and its metadata from LLVM IR.
149155
#[no_sanitize(cfi)]
150-
unsafe fn $name($($arg_name: $t),*) -> $ret {
151-
weak! { fn $name($($t),*) -> $ret }
156+
unsafe fn $name($($param: $t),*) -> $ret {
157+
weak!(fn $name($($param: $t),*) -> $ret;);
152158

153159
if let Some(fun) = $name.get() {
154-
fun($($arg_name),*)
160+
fun($($param),*)
155161
} else {
156162
super::os::set_errno(libc::ENOSYS);
157163
-1
@@ -162,26 +168,28 @@ pub(crate) macro syscall {
162168

163169
#[cfg(any(target_os = "linux", target_os = "android"))]
164170
pub(crate) macro syscall {
165-
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
166-
unsafe fn $name($($arg_name:$t),*) -> $ret {
167-
weak! { fn $name($($t),*) -> $ret }
171+
(
172+
fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;
173+
) => (
174+
unsafe fn $name($($param: $t),*) -> $ret {
175+
weak!(fn $name($($param: $t),*) -> $ret;);
168176

169177
// Use a weak symbol from libc when possible, allowing `LD_PRELOAD`
170178
// interposition, but if it's not found just use a raw syscall.
171179
if let Some(fun) = $name.get() {
172-
fun($($arg_name),*)
180+
fun($($param),*)
173181
} else {
174-
libc::syscall(libc::${concat(SYS_, $name)}, $($arg_name),*) as $ret
182+
libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret
175183
}
176184
}
177185
)
178186
}
179187

180188
#[cfg(any(target_os = "linux", target_os = "android"))]
181189
pub(crate) macro raw_syscall {
182-
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
183-
unsafe fn $name($($arg_name:$t),*) -> $ret {
184-
libc::syscall(libc::${concat(SYS_, $name)}, $($arg_name),*) as $ret
190+
(fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
191+
unsafe fn $name($($param: $t),*) -> $ret {
192+
libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret
185193
}
186194
)
187195
}

0 commit comments

Comments
 (0)