From 0e228db61ff0fdab69ecdb774523f1a50b5775f8 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 1 Oct 2026 20:19:23 -0700 Subject: [PATCH] Reduce more Linux specific code in base --- native/src/Application.mk | 2 +- native/src/base/base.cpp | 13 +------------ native/src/base/files.rs | 25 +++++++------------------ native/src/base/result.rs | 9 +++++++++ 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/native/src/Application.mk b/native/src/Application.mk index 4299ff233..1db608079 100644 --- a/native/src/Application.mk +++ b/native/src/Application.mk @@ -1,5 +1,5 @@ APP_BUILD_SCRIPT := src/Android.mk -APP_CFLAGS := -Wall -Oz -fomit-frame-pointer +APP_CFLAGS := -Wall -Oz -fomit-frame-pointer -D_FILE_OFFSET_BITS=64 APP_CPPFLAGS := -std=c++23 APP_STL := none APP_PLATFORM := android-23 diff --git a/native/src/base/base.cpp b/native/src/base/base.cpp index a1d555003..5fd5839bb 100644 --- a/native/src/base/base.cpp +++ b/native/src/base/base.cpp @@ -1,22 +1,11 @@ #include #include -#include #include #include using namespace std; -#ifndef __call_bypassing_fortify -#define __call_bypassing_fortify(fn) (&fn) -#endif - -#ifdef __LP64__ -static_assert(BLKGETSIZE64 == 0x80081272); -#else -static_assert(BLKGETSIZE64 == 0x80041272); -#endif - rust::Vec byte_data::patch(byte_view from, byte_view to) const { rust::Vec v; if (ptr == nullptr) @@ -128,7 +117,7 @@ static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) { char buf[sz]; buf[0] = '\0'; // Fortify logs when a fatal error occurs. Do not run through fortify again - int len = std::min(__call_bypassing_fortify(vsnprintf)(buf, sz, fmt, ap), sz - 1); + int len = std::min((&vsnprintf)(buf, sz, fmt, ap), sz - 1); log_with_rs(level, Utf8CStr(buf, len + 1)); return len; } diff --git a/native/src/base/files.rs b/native/src/base/files.rs index e9d190552..94ef96ab4 100644 --- a/native/src/base/files.rs +++ b/native/src/base/files.rs @@ -757,11 +757,6 @@ impl Drop for MappedFile { } } -unsafe extern "C" { - // Don't use the declaration from the libc crate as request should be u32 not i32 - fn ioctl(fd: RawFd, request: u32, ...) -> i32; -} - // We mark the returned slice static because it is valid until explicitly unmapped pub(crate) fn map_file(path: &Utf8CStr, rw: bool) -> OsResult<'_, &'static mut [u8]> { map_file_at(AT_FDCWD, path, rw) @@ -772,27 +767,21 @@ pub(crate) fn map_file_at<'a>( path: &'a Utf8CStr, rw: bool, ) -> OsResult<'a, &'static mut [u8]> { - #[cfg(target_pointer_width = "64")] - const BLKGETSIZE64: u32 = 0x80081272; - - #[cfg(target_pointer_width = "32")] - const BLKGETSIZE64: u32 = 0x80041272; - let flag = if rw { OFlag::O_RDWR } else { OFlag::O_RDONLY }; let fd = nix::fcntl::openat(dirfd, path, flag | OFlag::O_CLOEXEC, Mode::empty()) .into_os_result("openat", Some(path), None)?; - let attr = fd_get_attr(fd.as_raw_fd())?; + let mut file = File::from(fd); + let attr = fd_get_attr(file.as_raw_fd())?; let sz = if attr.is_block_device() { - let mut sz = 0_u64; - unsafe { - ioctl(fd.as_raw_fd(), BLKGETSIZE64, &mut sz).check_os_err("ioctl", Some(path), None)?; - } - sz + // Use the seek function from std::fs instead of nix so that the API is consistent + // across all Unix OS (lseek64 does not exist on macOS). + file.seek(SeekFrom::End(0)) + .into_os_result("lseek", Some(path), None)? } else { attr.st.st_size as u64 }; - map_fd(fd.as_fd(), sz as usize, rw).map_err(|e| e.set_args(Some(path), None)) + map_fd(file.as_fd(), sz as usize, rw).map_err(|e| e.set_args(Some(path), None)) } pub(crate) fn map_fd(fd: BorrowedFd, sz: usize, rw: bool) -> OsResult<'static, &'static mut [u8]> { diff --git a/native/src/base/result.rs b/native/src/base/result.rs index 6fe853ef2..ef32b040a 100644 --- a/native/src/base/result.rs +++ b/native/src/base/result.rs @@ -270,6 +270,15 @@ impl LibcReturn for *mut T { } } +impl LibcReturn for std::io::Result { + type Value = T; + + #[inline(always)] + fn check_err(self) -> nix::Result { + self.map_err(|e| e.raw_os_error().map_or_else(Errno::last, Errno::from_raw)) + } +} + impl LibcReturn for nix::Result { type Value = T;