Skip to content

Commit 4e817b8

Browse files
authored
Miscellaneous cleanups (#749)
- Fix and re-enable a documentation test. - Rename `optional_as_ptr` to `option_as_ptr`. - Use the `freebsdlike` and `netbsdlike` configs. - Mark auxv one-time initialization functions as `#[cold]`. - Convert some asserts to `const_assert`.
1 parent 87278a3 commit 4e817b8

File tree

9 files changed

+57
-61
lines changed

9 files changed

+57
-61
lines changed

examples/process.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! A command which prints out information about the process it runs in.
22
3-
use rustix::io;
4-
53
#[cfg(all(feature = "process", feature = "param"))]
64
#[cfg(not(windows))]
7-
fn main() -> io::Result<()> {
5+
fn main() -> rustix::io::Result<()> {
86
#[cfg(not(target_os = "espidf"))]
97
use rustix::param::*;
108
use rustix::process::*;

src/backend/libc/pipe/syscalls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {
1818
crate::backend::MAX_IOV,
1919
crate::fd::BorrowedFd,
2020
crate::pipe::{IoSliceRaw, SpliceFlags},
21-
crate::utils::optional_as_mut_ptr,
21+
crate::utils::option_as_mut_ptr,
2222
core::cmp::min,
2323
};
2424

@@ -62,8 +62,8 @@ pub fn splice(
6262
len: usize,
6363
flags: SpliceFlags,
6464
) -> io::Result<usize> {
65-
let off_in = optional_as_mut_ptr(off_in).cast();
66-
let off_out = optional_as_mut_ptr(off_out).cast();
65+
let off_in = option_as_mut_ptr(off_in).cast();
66+
let off_out = option_as_mut_ptr(off_out).cast();
6767

6868
unsafe {
6969
ret_usize(c::splice(

src/backend/linux_raw/param/auxv.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ fn pr_get_auxv() -> crate::io::Result<Vec<u8>> {
161161
/// On non-Mustang platforms, we read the aux vector via the `prctl`
162162
/// `PR_GET_AUXV`, with a fallback to /proc/self/auxv for kernels that don't
163163
/// support `PR_GET_AUXV`.
164+
#[cold]
164165
fn init_auxv() {
165166
match pr_get_auxv() {
166167
Ok(buffer) => {
@@ -185,6 +186,7 @@ fn init_auxv() {
185186
}
186187

187188
/// Process auxv entries from the open file `auxv`.
189+
#[cold]
188190
fn init_from_auxv_file(auxv: OwnedFd) -> Option<()> {
189191
let mut buffer = Vec::<u8>::with_capacity(512);
190192
loop {
@@ -220,6 +222,7 @@ fn init_from_auxv_file(auxv: OwnedFd) -> Option<()> {
220222
///
221223
/// The buffer contains `Elf_aux_t` elements, though it need not be aligned;
222224
/// function uses `read_unaligned` to read from it.
225+
#[cold]
223226
unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) -> Option<()> {
224227
let mut pagesz = 0;
225228
let mut clktck = 0;
@@ -272,6 +275,7 @@ unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) -> Option<()> {
272275
/// `base` is some value we got from a `AT_BASE` aux record somewhere,
273276
/// which hopefully holds the value of the program interpreter in memory. Do a
274277
/// series of checks to be as sure as we can that it's safe to use.
278+
#[cold]
275279
unsafe fn check_interpreter_base(base: *const Elf_Ehdr) -> Option<()> {
276280
check_elf_base(base)?;
277281
Some(())
@@ -282,6 +286,7 @@ unsafe fn check_interpreter_base(base: *const Elf_Ehdr) -> Option<()> {
282286
/// `base` is some value we got from a `AT_SYSINFO_EHDR` aux record somewhere,
283287
/// which hopefully holds the value of the kernel-provided vDSO in memory. Do a
284288
/// series of checks to be as sure as we can that it's safe to use.
289+
#[cold]
285290
unsafe fn check_vdso_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> {
286291
// In theory, we could check that we're not attempting to parse our own ELF
287292
// image, as an additional check. However, older Linux toolchains don't
@@ -331,6 +336,7 @@ unsafe fn check_vdso_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> {
331336
}
332337

333338
/// Check that `base` is a valid pointer to an ELF image.
339+
#[cold]
334340
unsafe fn check_elf_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> {
335341
// If we're reading a 64-bit auxv on a 32-bit platform, we'll see
336342
// a zero `a_val` because `AT_*` values are never greater than

src/backend/linux_raw/param/libc_auxv.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ const _SC_CLK_TCK: c::c_int = 2;
5050

5151
#[test]
5252
fn test_abi() {
53-
assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE);
54-
assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK);
55-
assert_eq!(self::AT_PHDR, ::libc::AT_PHDR);
56-
assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM);
57-
assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP);
58-
assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2);
59-
assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN);
60-
assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR);
53+
const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE);
54+
const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK);
55+
const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR);
56+
const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM);
57+
const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP);
58+
const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2);
59+
const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN);
60+
const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR);
6161
}
6262

6363
#[cfg(feature = "param")]

src/backend/linux_raw/runtime/syscalls.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::pid::Pid;
2222
use crate::runtime::{How, Sigaction, Siginfo, Sigset, Stack};
2323
use crate::signal::Signal;
2424
use crate::timespec::Timespec;
25-
use crate::utils::optional_as_ptr;
25+
use crate::utils::option_as_ptr;
2626
use core::mem::MaybeUninit;
2727
#[cfg(target_pointer_width = "32")]
2828
use linux_raw_sys::general::__kernel_old_timespec;
@@ -117,7 +117,7 @@ pub(crate) mod tls {
117117
#[inline]
118118
pub(crate) unsafe fn sigaction(signal: Signal, new: Option<Sigaction>) -> io::Result<Sigaction> {
119119
let mut old = MaybeUninit::<Sigaction>::uninit();
120-
let new = optional_as_ptr(new.as_ref());
120+
let new = option_as_ptr(new.as_ref());
121121
ret(syscall!(
122122
__NR_rt_sigaction,
123123
signal,
@@ -131,7 +131,7 @@ pub(crate) unsafe fn sigaction(signal: Signal, new: Option<Sigaction>) -> io::Re
131131
#[inline]
132132
pub(crate) unsafe fn sigaltstack(new: Option<Stack>) -> io::Result<Stack> {
133133
let mut old = MaybeUninit::<Stack>::uninit();
134-
let new = optional_as_ptr(new.as_ref());
134+
let new = option_as_ptr(new.as_ref());
135135
ret(syscall!(__NR_sigaltstack, new, &mut old))?;
136136
Ok(old.assume_init())
137137
}
@@ -144,7 +144,7 @@ pub(crate) unsafe fn tkill(tid: Pid, sig: Signal) -> io::Result<()> {
144144
#[inline]
145145
pub(crate) unsafe fn sigprocmask(how: How, new: Option<&Sigset>) -> io::Result<Sigset> {
146146
let mut old = MaybeUninit::<Sigset>::uninit();
147-
let new = optional_as_ptr(new);
147+
let new = option_as_ptr(new);
148148
ret(syscall!(
149149
__NR_rt_sigprocmask,
150150
how,
@@ -189,7 +189,7 @@ pub(crate) fn sigwaitinfo(set: &Sigset) -> io::Result<Siginfo> {
189189
#[inline]
190190
pub(crate) fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Result<Siginfo> {
191191
let mut info = MaybeUninit::<Siginfo>::uninit();
192-
let timeout_ptr = optional_as_ptr(timeout.as_ref());
192+
let timeout_ptr = option_as_ptr(timeout.as_ref());
193193

194194
// `rt_sigtimedwait_time64` was introduced in Linux 5.1. The old
195195
// `rt_sigtimedwait` syscall is not y2038-compatible on 32-bit
@@ -237,7 +237,7 @@ unsafe fn sigtimedwait_old(
237237
None => None,
238238
};
239239

240-
let old_timeout_ptr = optional_as_ptr(old_timeout.as_ref());
240+
let old_timeout_ptr = option_as_ptr(old_timeout.as_ref());
241241

242242
let _signum = ret_c_int(syscall!(
243243
__NR_rt_sigtimedwait,

src/cstr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
///
1010
/// # Examples
1111
///
12-
/// ```no_run
12+
/// ```
1313
/// # #[cfg(feature = "fs")]
1414
/// # fn main() -> rustix::io::Result<()> {
1515
/// use rustix::cstr;
1616
/// use rustix::fs::{statat, AtFlags, CWD};
1717
///
18-
/// let metadata = statat(CWD, cstr!("test.txt"), AtFlags::empty())?;
18+
/// let metadata = statat(CWD, cstr!("Cargo.toml"), AtFlags::empty())?;
1919
/// # Ok(())
2020
/// # }
2121
/// # #[cfg(not(feature = "fs"))]

src/pid.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,10 @@ fn test_sizes() {
9494

9595
// Rustix doesn't depend on `Option<Pid>` matching the ABI of a raw integer
9696
// for correctness, but it should work nonetheless.
97-
unsafe {
98-
let t: Option<Pid> = None;
99-
assert_eq!(0 as RawPid, transmute(t));
100-
101-
let t: Option<Pid> = Some(Pid::from_raw_unchecked(4567));
102-
assert_eq!(4567 as RawPid, transmute(t));
103-
}
97+
const_assert_eq!(0 as RawPid, unsafe {
98+
transmute::<Option<Pid>, RawPid>(None)
99+
});
100+
const_assert_eq!(4567 as RawPid, unsafe {
101+
transmute::<Option<Pid>, RawPid>(Some(Pid::from_raw_unchecked(4567)))
102+
});
104103
}

src/termios/types.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,12 @@ bitflags! {
283283

284284
/// `IUTF8`
285285
#[cfg(not(any(
286+
freebsdlike,
287+
netbsdlike,
286288
solarish,
287289
target_os = "aix",
288-
target_os = "dragonfly",
289290
target_os = "emscripten",
290-
target_os = "freebsd",
291291
target_os = "haiku",
292-
target_os = "netbsd",
293-
target_os = "openbsd",
294292
target_os = "redox",
295293
)))]
296294
const IUTF8 = c::IUTF8;
@@ -308,9 +306,8 @@ bitflags! {
308306
/// `OLCUC`
309307
#[cfg(not(any(
310308
apple,
309+
freebsdlike,
311310
target_os = "aix",
312-
target_os = "dragonfly",
313-
target_os = "freebsd",
314311
target_os = "netbsd",
315312
target_os = "redox",
316313
)))]
@@ -798,7 +795,7 @@ pub mod speed {
798795
/// Translate from a `c::speed_t` code to an arbitrary integer speed value
799796
/// `u32`.
800797
#[cfg(not(any(linux_kernel, bsd)))]
801-
pub(crate) fn decode(encoded_speed: c::speed_t) -> Option<u32> {
798+
pub(crate) const fn decode(encoded_speed: c::speed_t) -> Option<u32> {
802799
match encoded_speed {
803800
c::B0 => Some(0),
804801
c::B50 => Some(50),
@@ -935,7 +932,7 @@ pub mod speed {
935932
/// Translate from an arbitrary `u32` arbitrary integer speed value to a
936933
/// `c::speed_t` code.
937934
#[cfg(not(bsd))]
938-
pub(crate) fn encode(speed: u32) -> Option<c::speed_t> {
935+
pub(crate) const fn encode(speed: u32) -> Option<c::speed_t> {
939936
match speed {
940937
0 => Some(c::B0),
941938
50 => Some(c::B50),
@@ -1118,15 +1115,11 @@ impl SpecialCodeIndex {
11181115

11191116
/// `VSWTC`
11201117
#[cfg(not(any(
1121-
apple,
1118+
bsd,
11221119
solarish,
11231120
target_os = "aix",
1124-
target_os = "dragonfly",
1125-
target_os = "freebsd",
11261121
target_os = "haiku",
1127-
target_os = "netbsd",
11281122
target_os = "nto",
1129-
target_os = "openbsd",
11301123
)))]
11311124
pub const VSWTC: Self = Self(c::VSWTC as usize);
11321125

@@ -1261,7 +1254,7 @@ fn termios_layouts() {
12611254
// On everything except PowerPC, `termios` matches `termios2` except for
12621255
// the addition of `c_ispeed` and `c_ospeed`.
12631256
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
1264-
assert_eq!(
1257+
const_assert_eq!(
12651258
memoffset::offset_of!(Termios, input_speed),
12661259
core::mem::size_of::<c::termios>()
12671260
);
@@ -1346,19 +1339,19 @@ fn termios_layouts() {
13461339
)))]
13471340
fn termios_legacy() {
13481341
// Check that our doc aliases above are correct.
1349-
assert_eq!(c::EXTA, c::B19200);
1350-
assert_eq!(c::EXTB, c::B38400);
1342+
const_assert_eq!(c::EXTA, c::B19200);
1343+
const_assert_eq!(c::EXTB, c::B38400);
13511344
}
13521345

13531346
#[cfg(bsd)]
13541347
#[test]
13551348
fn termios_bsd() {
13561349
// On BSD platforms we can assume that the `B*` constants have their
13571350
// arbitrary integer speed value. Confirm this.
1358-
assert_eq!(c::B0, 0);
1359-
assert_eq!(c::B50, 50);
1360-
assert_eq!(c::B19200, 19200);
1361-
assert_eq!(c::B38400, 38400);
1351+
const_assert_eq!(c::B0, 0);
1352+
const_assert_eq!(c::B50, 50);
1353+
const_assert_eq!(c::B19200, 19200);
1354+
const_assert_eq!(c::B38400, 38400);
13621355
}
13631356

13641357
#[test]
@@ -1386,13 +1379,13 @@ fn termios_ioctl_contiguity() {
13861379
// When using `termios2`, we assume that we can add the optional actions
13871380
// value to the ioctl request code. Test this assumption.
13881381

1389-
assert_eq!(c::TCSETS2, c::TCSETS2 + 0);
1390-
assert_eq!(c::TCSETSW2, c::TCSETS2 + 1);
1391-
assert_eq!(c::TCSETSF2, c::TCSETS2 + 2);
1382+
const_assert_eq!(c::TCSETS2, c::TCSETS2 + 0);
1383+
const_assert_eq!(c::TCSETSW2, c::TCSETS2 + 1);
1384+
const_assert_eq!(c::TCSETSF2, c::TCSETS2 + 2);
13921385

1393-
assert_eq!(c::TCSANOW - c::TCSANOW, 0);
1394-
assert_eq!(c::TCSADRAIN - c::TCSANOW, 1);
1395-
assert_eq!(c::TCSAFLUSH - c::TCSANOW, 2);
1386+
const_assert_eq!(c::TCSANOW - c::TCSANOW, 0);
1387+
const_assert_eq!(c::TCSADRAIN - c::TCSANOW, 1);
1388+
const_assert_eq!(c::TCSAFLUSH - c::TCSANOW, 2);
13961389

13971390
// MIPS is different here.
13981391
#[cfg(any(target_arch = "mips", target_arch = "mips64"))]
@@ -1403,15 +1396,15 @@ fn termios_ioctl_contiguity() {
14031396
}
14041397
#[cfg(not(any(target_arch = "mips", target_arch = "mips64")))]
14051398
{
1406-
assert_eq!(c::TCSANOW, 0);
1407-
assert_eq!(c::TCSADRAIN, 1);
1408-
assert_eq!(c::TCSAFLUSH, 2);
1399+
const_assert_eq!(c::TCSANOW, 0);
1400+
const_assert_eq!(c::TCSADRAIN, 1);
1401+
const_assert_eq!(c::TCSAFLUSH, 2);
14091402
}
14101403
}
14111404

14121405
#[cfg(linux_kernel)]
14131406
#[test]
14141407
fn termios_cibaud() {
14151408
// Test an assumption.
1416-
assert_eq!(c::CIBAUD, c::CBAUD << c::IBSHIFT);
1409+
const_assert_eq!(c::CIBAUD, c::CBAUD << c::IBSHIFT);
14171410
}

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) fn as_mut_ptr<T>(t: &mut T) -> *mut T {
1818

1919
/// Convert an `Option<&T>` into a possibly-null `*const T`.
2020
#[inline]
21-
pub(crate) const fn optional_as_ptr<T>(t: Option<&T>) -> *const T {
21+
pub(crate) const fn option_as_ptr<T>(t: Option<&T>) -> *const T {
2222
match t {
2323
Some(t) => t,
2424
None => null(),
@@ -27,7 +27,7 @@ pub(crate) const fn optional_as_ptr<T>(t: Option<&T>) -> *const T {
2727

2828
/// Convert an `Option<&mut T>` into a possibly-null `*mut T`.
2929
#[inline]
30-
pub(crate) fn optional_as_mut_ptr<T>(t: Option<&mut T>) -> *mut T {
30+
pub(crate) fn option_as_mut_ptr<T>(t: Option<&mut T>) -> *mut T {
3131
match t {
3232
Some(t) => t,
3333
None => null_mut(),

0 commit comments

Comments
 (0)