Skip to content

Commit 27011b4

Browse files
author
Michael Benfield
committed
Use more LFS functions.
On Linux, use mmap64, open64, openat64, and sendfile64 in place of their non-LFS counterparts. This is relevant to #94173. With these changes (together with rust-lang/backtrace-rs#501), the simple binaries I produce with rustc seem to have no non-LFS functions, so maybe #94173 is fixed. But I can't be sure if I've missed something and maybe some non-LFS functions could sneak in somehow.
1 parent 01fbc5a commit 27011b4

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1743,8 +1743,13 @@ mod remove_dir_impl {
17431743
use crate::sys::common::small_c_string::run_path_with_cstr;
17441744
use crate::sys::{cvt, cvt_r};
17451745

1746-
#[cfg(not(all(target_os = "macos", not(target_arch = "aarch64")),))]
1746+
#[cfg(not(any(
1747+
target_os = "linux",
1748+
all(target_os = "macos", not(target_arch = "aarch64"))
1749+
)))]
17471750
use libc::{fdopendir, openat, unlinkat};
1751+
#[cfg(target_os = "linux")]
1752+
use libc::{fdopendir, openat64 as openat, unlinkat};
17481753
#[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
17491754
use macos_weak::{fdopendir, openat, unlinkat};
17501755

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ use crate::ptr;
6161
use crate::sync::atomic::{AtomicBool, AtomicU8, Ordering};
6262
use crate::sys::cvt;
6363
use crate::sys::weak::syscall;
64+
#[cfg(not(target_os = "linux"))]
65+
use libc::sendfile as sendfile64;
66+
#[cfg(target_os = "linux")]
67+
use libc::sendfile64;
6468
use libc::{EBADF, EINVAL, ENOSYS, EOPNOTSUPP, EOVERFLOW, EPERM, EXDEV};
6569

6670
#[cfg(test)]
@@ -647,7 +651,7 @@ fn sendfile_splice(mode: SpliceMode, reader: RawFd, writer: RawFd, len: u64) ->
647651

648652
let result = match mode {
649653
SpliceMode::Sendfile => {
650-
cvt(unsafe { libc::sendfile(writer, reader, ptr::null_mut(), chunk_size) })
654+
cvt(unsafe { sendfile64(writer, reader, ptr::null_mut(), chunk_size) })
651655
}
652656
SpliceMode::Splice => cvt(unsafe {
653657
splice(reader, ptr::null_mut(), writer, ptr::null_mut(), chunk_size, 0)

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
9595
)))]
9696
'poll: {
9797
use crate::sys::os::errno;
98+
#[cfg(not(target_os = "linux"))]
99+
use libc::open as open64;
100+
#[cfg(target_os = "linux")]
101+
use libc::open64;
98102
let pfds: &mut [_] = &mut [
99103
libc::pollfd { fd: 0, events: 0, revents: 0 },
100104
libc::pollfd { fd: 1, events: 0, revents: 0 },
@@ -116,7 +120,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
116120
if pfd.revents & libc::POLLNVAL == 0 {
117121
continue;
118122
}
119-
if libc::open("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
123+
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
120124
// If the stream is closed but we failed to reopen it, abort the
121125
// process. Otherwise we wouldn't preserve the safety of
122126
// operations on the corresponding Rust object Stdin, Stdout, or
@@ -139,9 +143,13 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
139143
)))]
140144
{
141145
use crate::sys::os::errno;
146+
#[cfg(not(target_os = "linux"))]
147+
use libc::open as open64;
148+
#[cfg(target_os = "linux")]
149+
use libc::open64;
142150
for fd in 0..3 {
143151
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
144-
if libc::open("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
152+
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
145153
// If the stream is closed but we failed to reopen it, abort the
146154
// process. Otherwise we wouldn't preserve the safety of
147155
// operations on the corresponding Rust object Stdin, Stdout, or

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ mod imp {
4545
use crate::thread;
4646

4747
use libc::MAP_FAILED;
48-
use libc::{mmap, munmap};
48+
#[cfg(not(target_os = "linux"))]
49+
use libc::{mmap as mmap64, munmap};
50+
#[cfg(target_os = "linux")]
51+
use libc::{mmap64, munmap};
4952
use libc::{sigaction, sighandler_t, SA_ONSTACK, SA_SIGINFO, SIGBUS, SIG_DFL};
5053
use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
5154
use libc::{MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE, SIGSEGV};
@@ -135,7 +138,7 @@ mod imp {
135138
#[cfg(not(any(target_os = "openbsd", target_os = "netbsd", target_os = "linux",)))]
136139
let flags = MAP_PRIVATE | MAP_ANON;
137140
let stackp =
138-
mmap(ptr::null_mut(), SIGSTKSZ + page_size(), PROT_READ | PROT_WRITE, flags, -1, 0);
141+
mmap64(ptr::null_mut(), SIGSTKSZ + page_size(), PROT_READ | PROT_WRITE, flags, -1, 0);
139142
if stackp == MAP_FAILED {
140143
panic!("failed to allocate an alternative stack: {}", io::Error::last_os_error());
141144
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,10 @@ pub mod guard {
658658
))]
659659
#[cfg_attr(test, allow(dead_code))]
660660
pub mod guard {
661-
use libc::{mmap, mprotect};
661+
#[cfg(not(target_os = "linux"))]
662+
use libc::{mmap as mmap64, mprotect};
663+
#[cfg(target_os = "linux")]
664+
use libc::{mmap64, mprotect};
662665
use libc::{MAP_ANON, MAP_FAILED, MAP_FIXED, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE};
663666

664667
use crate::io;
@@ -808,7 +811,7 @@ pub mod guard {
808811
// read/write permissions and only then mprotect() it to
809812
// no permissions at all. See issue #50313.
810813
let stackptr = get_stack_start_aligned()?;
811-
let result = mmap(
814+
let result = mmap64(
812815
stackptr,
813816
page_size,
814817
PROT_READ | PROT_WRITE,

0 commit comments

Comments
 (0)