Skip to content

Commit e2014c5

Browse files
authored
Miscellaneous documentation and clippy fixes for futex. (#1134)
1 parent d0c4b8f commit e2014c5

4 files changed

Lines changed: 124 additions & 118 deletions

File tree

src/backend/libc/thread/syscalls.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ use crate::io;
77
use crate::thread::{NanosleepRelativeResult, Timespec};
88
#[cfg(all(target_env = "gnu", fix_y2038))]
99
use crate::timespec::LibcTimespec;
10+
#[cfg(all(
11+
linux_kernel,
12+
target_pointer_width = "32",
13+
not(any(target_arch = "aarch64", target_arch = "x86_64"))
14+
))]
15+
use crate::utils::option_as_ptr;
1016
use core::mem::MaybeUninit;
17+
#[cfg(linux_kernel)]
1118
use core::sync::atomic::AtomicU32;
1219
#[cfg(linux_kernel)]
1320
use {
@@ -416,6 +423,9 @@ pub(crate) fn setresgid_thread(
416423
unsafe { ret(setresgid(rgid.as_raw(), egid.as_raw(), sgid.as_raw())) }
417424
}
418425

426+
/// # Safety
427+
///
428+
/// The raw pointers must point to valid aligned memory.
419429
#[cfg(linux_kernel)]
420430
pub(crate) unsafe fn futex_val2(
421431
uaddr: *const AtomicU32,
@@ -426,9 +436,12 @@ pub(crate) unsafe fn futex_val2(
426436
uaddr2: *const AtomicU32,
427437
val3: u32,
428438
) -> io::Result<usize> {
429-
// The least-significant four bytes of the timeout pointer are used as `val2`.
430-
// ["the kernel casts the timeout value first to unsigned long, then to uint32_t"](https://man7.org/linux/man-pages/man2/futex.2.html),
431-
// so we perform that exact conversion in reverse to create the pointer.
439+
// Pass `val2` in the least-significant bytes of the `timeout` argument.
440+
// [“the kernel casts the timeout value first to unsigned long, then to
441+
// uint32_t”], so we perform that exact conversion in reverse to create
442+
// the pointer.
443+
//
444+
// [“the kernel casts the timeout value first to unsigned long, then to uint32_t”]: https://man7.org/linux/man-pages/man2/futex.2.html
432445
let timeout = val2 as usize as *const Timespec;
433446

434447
#[cfg(all(
@@ -489,6 +502,9 @@ pub(crate) unsafe fn futex_val2(
489502
}
490503
}
491504

505+
/// # Safety
506+
///
507+
/// The raw pointers must point to valid aligned memory.
492508
#[cfg(linux_kernel)]
493509
pub(crate) unsafe fn futex_timeout(
494510
uaddr: *const AtomicU32,
@@ -566,6 +582,9 @@ pub(crate) unsafe fn futex_timeout(
566582
}
567583
}
568584

585+
/// # Safety
586+
///
587+
/// The raw pointers must point to valid aligned memory.
569588
#[cfg(linux_kernel)]
570589
#[cfg(all(
571590
target_pointer_width = "32",
@@ -606,10 +625,7 @@ unsafe fn futex_old_timespec(
606625
uaddr,
607626
op as i32 | flags.bits() as i32,
608627
val,
609-
old_timeout
610-
.as_ref()
611-
.map(|timeout| timeout as *const linux_raw_sys::general::__kernel_old_timespec)
612-
.unwrap_or(core::ptr::null()),
628+
option_as_ptr(old_timeout.as_ref()),
613629
uaddr2,
614630
val3,
615631
) as isize)

src/backend/linux_raw/thread/syscalls.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use crate::pid::Pid;
1616
use crate::thread::{futex, ClockId, NanosleepRelativeResult, Timespec};
1717
use core::mem::MaybeUninit;
1818
use core::sync::atomic::AtomicU32;
19-
#[cfg(target_pointer_width = "32")]
20-
use linux_raw_sys::general::timespec as __kernel_old_timespec;
2119
use linux_raw_sys::general::{__kernel_timespec, TIMER_ABSTIME};
20+
#[cfg(target_pointer_width = "32")]
21+
use {crate::utils::option_as_ptr, linux_raw_sys::general::timespec as __kernel_old_timespec};
2222

2323
#[inline]
2424
pub(crate) fn clock_nanosleep_relative(
@@ -204,6 +204,9 @@ pub(crate) fn gettid() -> Pid {
204204
}
205205
}
206206

207+
/// # Safety
208+
///
209+
/// The raw pointers must point to valid aligned memory.
207210
#[inline]
208211
pub(crate) unsafe fn futex_val2(
209212
uaddr: *const AtomicU32,
@@ -214,9 +217,12 @@ pub(crate) unsafe fn futex_val2(
214217
uaddr2: *const AtomicU32,
215218
val3: u32,
216219
) -> io::Result<usize> {
217-
// The least-significant four bytes of the timeout pointer are used as `val2`.
218-
// ["the kernel casts the timeout value first to unsigned long, then to uint32_t"](https://man7.org/linux/man-pages/man2/futex.2.html),
219-
// so we perform that exact conversion in reverse to create the pointer.
220+
// Pass `val2` in the least-significant bytes of the `timeout` argument.
221+
// [“the kernel casts the timeout value first to unsigned long, then to
222+
// uint32_t”], so we perform that exact conversion in reverse to create
223+
// the pointer.
224+
//
225+
// [“the kernel casts the timeout value first to unsigned long, then to uint32_t”]: https://man7.org/linux/man-pages/man2/futex.2.html
220226
let timeout = val2 as usize as *const Timespec;
221227

222228
#[cfg(target_pointer_width = "32")]
@@ -243,6 +249,9 @@ pub(crate) unsafe fn futex_val2(
243249
))
244250
}
245251

252+
/// # Safety
253+
///
254+
/// The raw pointers must point to valid aligned memory.
246255
#[inline]
247256
pub(crate) unsafe fn futex_timeout(
248257
uaddr: *const AtomicU32,
@@ -286,6 +295,9 @@ pub(crate) unsafe fn futex_timeout(
286295
))
287296
}
288297

298+
/// # Safety
299+
///
300+
/// The raw pointers must point to valid aligned memory.
289301
#[cfg(target_pointer_width = "32")]
290302
unsafe fn futex_old_timespec(
291303
uaddr: *const AtomicU32,
@@ -312,10 +324,7 @@ unsafe fn futex_old_timespec(
312324
uaddr,
313325
(op, flags),
314326
c_uint(val),
315-
old_timeout
316-
.as_ref()
317-
.map(|timeout| timeout as *const __kernel_old_timespec)
318-
.unwrap_or(core::ptr::null()),
327+
option_as_ptr(old_timeout.as_ref()),
319328
uaddr2,
320329
c_uint(val3)
321330
))

0 commit comments

Comments
 (0)