Skip to content

Commit bcac8fd

Browse files
authored
Miscellaneous cleanups. (#1404)
Documentation: - Document that `rustix::buffer` is now a public API enabled by default. - Update comments to not say `get_` now that `get_` prefixes are removed. - Add `doc(cfg(feature = "alloc"))` to a few more functions. - Document several not_implemented functions that have been discussed recently. - Add a `#[doc(alias)]` for a renamed flag. Misc cleanups: - Remove `runtime::startup_tls_info` and `runtime::StartupTlsInfo`, since that code [moved] to origin, and the code in rustix by now lacked some subsequent bug fixes. - Remove redundant `#[cfg(linux_raw)]`s in src/runtime.rs. - Add `#[must_use]` to a few private functions. - Remove some redundant `.as_fd()` calls. - Add some more special-fd-value tests. - Eliminate some spurious diffs between src/backend/linux_raw/net/addr.rs and src/backend/libc/net/addr.rs. - Add a test for `O_LARGEFILE` being non-zero. [moved]: sunfishcode/origin#58
1 parent ffcecaa commit bcac8fd

File tree

29 files changed

+141
-219
lines changed

29 files changed

+141
-219
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ building.
5252

5353
## Cargo features
5454

55-
The modules [`rustix::io`], [`rustix::fd`], [`rustix::ffi`], and
56-
[`rustix::ioctl`] are enabled by default. The rest of the API modules are
57-
conditional with cargo feature flags.
55+
The modules [`rustix::io`], [`rustix::buffer`], [`rustix::fd`],
56+
[`rustix::ffi`], and [`rustix::ioctl`] are enabled by default. The rest of the
57+
API modules are conditional with cargo feature flags.
5858

5959
| Name | Description |
6060
| ---------- | -------------------------------------------------------------- |

src/backend/libc/c.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,16 @@ pub(crate) use __fsid_t as fsid_t;
518518
#[cfg(feature = "mm")]
519519
#[cfg(target_os = "android")]
520520
pub(crate) const MAP_DROPPABLE: c_int = bitcast!(linux_raw_sys::general::MAP_DROPPABLE);
521+
522+
#[cfg(test)]
523+
mod tests {
524+
use super::*;
525+
526+
#[test]
527+
#[cfg(linux_kernel)]
528+
fn test_flags() {
529+
// libc may publicly define `O_LARGEFILE` to 0, but we want the real
530+
// non-zero value.
531+
assert_ne!(O_LARGEFILE, 0);
532+
}
533+
}

src/backend/libc/event/epoll.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ bitflags! {
6666
const WAKEUP = bitcast!(c::EPOLLWAKEUP);
6767

6868
/// `EPOLLEXCLUSIVE`
69-
#[cfg(not(target_os = "android"))]
7069
const EXCLUSIVE = bitcast!(c::EPOLLEXCLUSIVE);
7170

7271
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>

src/backend/libc/fs/syscalls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ pub(crate) fn fadvise(
12831283
// turn a very large value into a negative value.
12841284
//
12851285
// On FreeBSD, this could cause `posix_fadvise` to fail with
1286-
// `Errrno::INVAL`. Because we don't expose the signed type in our API, we
1286+
// `Errno::INVAL`. Because we don't expose the signed type in our API, we
12871287
// also avoid exposing this artifact of casting an unsigned value to the
12881288
// signed type. To do this, we use a no-op call in this case.
12891289
//
@@ -1298,7 +1298,7 @@ pub(crate) fn fadvise(
12981298

12991299
#[cold]
13001300
fn fadvise_noop(fd: BorrowedFd<'_>) -> io::Result<()> {
1301-
// Us an `fcntl` to report `Errno::EBADF` if needed, but otherwise
1301+
// Use an `fcntl` to report `Errno::BADF` if needed, but otherwise
13021302
// do nothing.
13031303
fcntl_getfl(fd).map(|_| ())
13041304
}

src/backend/libc/net/addr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ impl SocketAddrUnix {
140140
let bytes = self.bytes()?;
141141
if !bytes.is_empty() && bytes[0] != 0 {
142142
if self.unix.sun_path.len() == bytes.len() {
143-
// SAFETY: no NULs are contained in bytes
144-
unsafe { self.path_with_termination(bytes) }
143+
// SAFETY: There are no NULs contained in bytes.
144+
unsafe { Self::path_with_termination(bytes) }
145145
} else {
146146
// SAFETY: `from_bytes_with_nul_unchecked` since the string is
147147
// NUL-terminated.
@@ -154,9 +154,10 @@ impl SocketAddrUnix {
154154

155155
/// If the `sun_path` field is not NUL-terminated, terminate it.
156156
///
157-
/// SAFETY: the input `bytes` must not contain any NULs
157+
/// SAFETY: The input `bytes` must not contain any NULs.
158158
#[cfg(feature = "alloc")]
159-
unsafe fn path_with_termination(&self, bytes: &[u8]) -> Option<Cow<'_, CStr>> {
159+
#[cold]
160+
unsafe fn path_with_termination(bytes: &[u8]) -> Option<Cow<'_, CStr>> {
160161
let mut owned = Vec::with_capacity(bytes.len() + 1);
161162
owned.extend_from_slice(bytes);
162163
owned.push(b'\0');

src/backend/linux_raw/fs/syscalls.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ use crate::backend::conv::{
2222
};
2323
#[cfg(target_pointer_width = "64")]
2424
use crate::backend::conv::{loff_t, loff_t_from_u64, ret_u64};
25-
#[cfg(any(
26-
target_arch = "aarch64",
27-
target_arch = "riscv64",
28-
target_arch = "mips64",
29-
target_arch = "mips64r6",
30-
target_pointer_width = "32",
31-
))]
32-
use crate::fd::AsFd;
3325
use crate::fd::{BorrowedFd, OwnedFd};
3426
use crate::ffi::CStr;
3527
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
@@ -62,7 +54,7 @@ pub(crate) fn open(path: &CStr, flags: OFlags, mode: Mode) -> io::Result<OwnedFd
6254

6355
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
6456
{
65-
openat(CWD.as_fd(), path, flags, mode)
57+
openat(CWD, path, flags, mode)
6658
}
6759
#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
6860
unsafe {
@@ -552,7 +544,7 @@ pub(crate) fn stat(path: &CStr) -> io::Result<Stat> {
552544
))]
553545
{
554546
match crate::fs::statx(
555-
crate::fs::CWD.as_fd(),
547+
crate::fs::CWD,
556548
path,
557549
AtFlags::empty(),
558550
StatxFlags::BASIC_STATS,
@@ -669,7 +661,7 @@ pub(crate) fn lstat(path: &CStr) -> io::Result<Stat> {
669661
#[cfg(any(target_pointer_width = "32", target_arch = "mips64"))]
670662
{
671663
match crate::fs::statx(
672-
crate::fs::CWD.as_fd(),
664+
crate::fs::CWD,
673665
path,
674666
AtFlags::SYMLINK_NOFOLLOW,
675667
StatxFlags::BASIC_STATS,
@@ -1393,7 +1385,7 @@ pub(crate) fn futimens(fd: BorrowedFd<'_>, times: &Timestamps) -> io::Result<()>
13931385
pub(crate) fn access(path: &CStr, access: Access) -> io::Result<()> {
13941386
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
13951387
{
1396-
accessat_noflags(CWD.as_fd(), path, access)
1388+
accessat_noflags(CWD, path, access)
13971389
}
13981390

13991391
#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]

src/backend/linux_raw/net/addr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl SocketAddrUnix {
102102
let bytes = self.bytes()?;
103103
if !bytes.is_empty() && bytes[0] != 0 {
104104
if self.unix.sun_path.len() == bytes.len() {
105-
// SAFETY: no NULs are contained in bytes
105+
// SAFETY: There are no NULs contained in bytes.
106106
unsafe { Self::path_with_termination(bytes) }
107107
} else {
108108
// SAFETY: `from_bytes_with_nul_unchecked` since the string is
@@ -116,7 +116,7 @@ impl SocketAddrUnix {
116116

117117
/// If the `sun_path` field is not NUL-terminated, terminate it.
118118
///
119-
/// SAFETY: the input `bytes` must not contain any NULs
119+
/// SAFETY: The input `bytes` must not contain any NULs.
120120
#[cfg(feature = "alloc")]
121121
#[cold]
122122
unsafe fn path_with_termination(bytes: &[u8]) -> Option<Cow<'_, CStr>> {

src/backend/linux_raw/pid/syscalls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::backend::conv::ret_usize_infallible;
99
use crate::pid::{Pid, RawPid};
1010

1111
#[inline]
12+
#[must_use]
1213
pub(crate) fn getpid() -> Pid {
1314
unsafe {
1415
let pid = ret_usize_infallible(syscall_readonly!(__NR_getpid)) as RawPid;

src/backend/linux_raw/process/syscalls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub(crate) fn getcwd(buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
5858
}
5959

6060
#[inline]
61+
#[must_use]
6162
pub(crate) fn getppid() -> Option<Pid> {
6263
unsafe {
6364
let ppid = ret_c_int_infallible(syscall_readonly!(__NR_getppid));
@@ -86,6 +87,7 @@ pub(crate) fn setpgid(pid: Option<Pid>, pgid: Option<Pid>) -> io::Result<()> {
8687
}
8788

8889
#[inline]
90+
#[must_use]
8991
pub(crate) fn getpgrp() -> Pid {
9092
// Use the `getpgrp` syscall if available.
9193
#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]

src/backend/linux_raw/runtime/tls.rs

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,7 @@
11
//! TLS utilities.
2-
//!
3-
//! # Safety
4-
//!
5-
//! This file contains code that reads the raw phdr array pointed to by the
6-
//! kernel-provided AUXV values.
7-
#![allow(unsafe_code)]
8-
9-
use crate::backend::c;
10-
use crate::backend::param::auxv::exe_phdrs;
11-
use core::arch::global_asm;
12-
use core::ptr::{null, NonNull};
13-
use linux_raw_sys::elf::*;
142
153
/// For use with [`set_thread_area`].
164
///
175
/// [`set_thread_area`]: crate::runtime::set_thread_area
186
#[cfg(target_arch = "x86")]
197
pub type UserDesc = linux_raw_sys::general::user_desc;
20-
21-
pub(crate) fn startup_tls_info() -> StartupTlsInfo {
22-
let mut base = null();
23-
let mut tls_phdr = null();
24-
let mut stack_size = 0;
25-
26-
let (first_phdr, phent, phnum) = exe_phdrs();
27-
let mut current_phdr = first_phdr.cast::<Elf_Phdr>();
28-
29-
// The dynamic address of the dynamic section, which we can compare with
30-
// the `PT_DYNAMIC` header's static address, if present.
31-
let dynamic_addr: *const c::c_void = unsafe { &_DYNAMIC };
32-
33-
// SAFETY: We assume the phdr array pointer and length the kernel provided
34-
// to the process describe a valid phdr array.
35-
unsafe {
36-
let phdrs_end = current_phdr.cast::<u8>().add(phnum * phent).cast();
37-
while current_phdr != phdrs_end {
38-
let phdr = &*current_phdr;
39-
current_phdr = current_phdr.cast::<u8>().add(phent).cast();
40-
41-
match phdr.p_type {
42-
// Compute the offset from the static virtual addresses
43-
// in the `p_vaddr` fields to the dynamic addresses. We don't
44-
// always get a `PT_PHDR` or `PT_DYNAMIC` header, so use
45-
// whichever one we get.
46-
PT_PHDR => base = first_phdr.cast::<u8>().wrapping_sub(phdr.p_vaddr),
47-
PT_DYNAMIC => base = dynamic_addr.cast::<u8>().wrapping_sub(phdr.p_vaddr),
48-
49-
PT_TLS => tls_phdr = phdr,
50-
PT_GNU_STACK => stack_size = phdr.p_memsz,
51-
_ => {}
52-
}
53-
}
54-
55-
if tls_phdr.is_null() {
56-
StartupTlsInfo {
57-
addr: NonNull::dangling().as_ptr(),
58-
mem_size: 0,
59-
file_size: 0,
60-
align: 1,
61-
stack_size: 0,
62-
}
63-
} else {
64-
StartupTlsInfo {
65-
addr: base.cast::<u8>().wrapping_add((*tls_phdr).p_vaddr).cast(),
66-
mem_size: (*tls_phdr).p_memsz,
67-
file_size: (*tls_phdr).p_filesz,
68-
align: (*tls_phdr).p_align,
69-
stack_size,
70-
}
71-
}
72-
}
73-
}
74-
75-
extern "C" {
76-
/// Declare the `_DYNAMIC` symbol so that we can compare its address with
77-
/// the static address in the `PT_DYNAMIC` header to learn our offset. Use
78-
/// a weak symbol because `_DYNAMIC` is not always present.
79-
static _DYNAMIC: c::c_void;
80-
}
81-
// Rust has `extern_weak` but it isn't stable, so use a `global_asm`.
82-
global_asm!(".weak _DYNAMIC");
83-
84-
/// The values returned from [`startup_tls_info`].
85-
///
86-
/// [`startup_tls_info`]: crate::runtime::startup_tls_info
87-
pub struct StartupTlsInfo {
88-
/// The base address of the TLS segment.
89-
pub addr: *const c::c_void,
90-
/// The size of the memory region.
91-
pub mem_size: usize,
92-
/// The size beyond which all memory is zero-initialized.
93-
pub file_size: usize,
94-
/// The required alignment for the TLS segment.
95-
pub align: usize,
96-
/// The requested minimum size for stacks.
97-
pub stack_size: usize,
98-
}

0 commit comments

Comments
 (0)