From 798e6e6f99c99dbff3ad9b49b225336f243bc9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Wed, 23 Oct 2024 14:08:29 -0700 Subject: [PATCH] Generalize OS based conditional compilation conditions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We currently special case the Window target operating system in a few places. Conceptually, we support Mac OS in the same capacity as Windows (read: in a very limited fashion) and so it makes little sense to special case one but not the other. The main reason for this behavior has been that we don't actually run tests on Mac OS. We certainly could and should be doing that, however, and so as a first step towards this future, let's switch OS based conditional compilation switches from "is-windows" to "is-not-linux" (and similar). Signed-off-by: Daniel Müller --- dev/build.rs | 4 ++-- src/file_cache.rs | 6 +++--- src/lib.rs | 2 +- src/normalize/ioctl.rs | 4 ++-- src/normalize/normalizer.rs | 2 +- src/symbolize/perf_map.rs | 2 +- src/symbolize/symbolizer.rs | 8 ++++---- src/util.rs | 6 +++--- tests/allocs.rs | 4 ++-- tests/blazesym.rs | 10 +++++----- tests/common/mod.rs | 4 ++-- tests/permission.rs | 6 +++--- tests/permissionless.rs | 10 +++++----- 13 files changed, 34 insertions(+), 34 deletions(-) diff --git a/dev/build.rs b/dev/build.rs index 758f89a1..14382139 100644 --- a/dev/build.rs +++ b/dev/build.rs @@ -32,7 +32,7 @@ fn data_dir() -> PathBuf { } /// Retrieve the system's page size. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] fn page_size() -> Result { // SAFETY: `sysconf` is always safe to call. let rc = unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) }; @@ -45,7 +45,7 @@ fn page_size() -> Result { Ok(usize::try_from(rc).unwrap()) } -#[cfg(windows)] +#[cfg(not(target_os = "linux"))] fn page_size() -> Result { unimplemented!() } diff --git a/src/file_cache.rs b/src/file_cache.rs index 619bc4fc..09646b2a 100644 --- a/src/file_cache.rs +++ b/src/file_cache.rs @@ -19,7 +19,7 @@ struct FileMeta { inode: libc::ino_t, size: libc::off_t, mtime_sec: libc::time_t, - #[cfg(not(windows))] + #[cfg(target_os = "linux")] mtime_nsec: i64, } @@ -32,7 +32,7 @@ impl From<&libc::stat> for FileMeta { inode: other.st_ino as _, size: other.st_size as _, mtime_sec: other.st_mtime, - #[cfg(not(windows))] + #[cfg(target_os = "linux")] mtime_nsec: other.st_mtime_nsec as _, } } @@ -219,7 +219,7 @@ mod tests { /// Check that our `FileCache` does not represent symbolic links /// pointing to the same file as equal entries. - #[cfg(not(windows))] + #[cfg(target_os = "linux")] #[test] fn file_symlinks() { use std::os::fd::AsRawFd as _; diff --git a/src/lib.rs b/src/lib.rs index 14a1030f..0e643056 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,7 @@ )), allow(dead_code, unused_imports) )] -#![cfg_attr(windows, allow(dead_code, unused_imports))] +#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))] #[cfg(feature = "nightly")] diff --git a/src/normalize/ioctl.rs b/src/normalize/ioctl.rs index b27afd19..ddc30a56 100644 --- a/src/normalize/ioctl.rs +++ b/src/normalize/ioctl.rs @@ -139,7 +139,7 @@ fn vma_flags_to_perm(vma_flags: u64) -> Perm { /// The caller is responsible for checking that the returned `MapsEntry` /// actually covers the provided address. If it does not, it represents /// the next known entry. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] pub(crate) fn query_procmap( file: &File, pid: Pid, @@ -224,7 +224,7 @@ pub(crate) fn query_procmap( Ok(Some(entry)) } -#[cfg(windows)] +#[cfg(not(target_os = "linux"))] pub(crate) fn query_procmap( _file: &File, _pid: Pid, diff --git a/src/normalize/normalizer.rs b/src/normalize/normalizer.rs index e9cd659b..b7669bb0 100644 --- a/src/normalize/normalizer.rs +++ b/src/normalize/normalizer.rs @@ -412,7 +412,7 @@ mod tests { } /// Check that we can normalize user addresses. - #[cfg(not(windows))] + #[cfg(target_os = "linux")] // `libc` on Arm doesn't have `__errno_location`. #[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))] #[test] diff --git a/src/symbolize/perf_map.rs b/src/symbolize/perf_map.rs index f1d9217f..8589c9c2 100644 --- a/src/symbolize/perf_map.rs +++ b/src/symbolize/perf_map.rs @@ -311,7 +311,7 @@ mod tests { } /// Check that we can symbolize an address using a perf map. - #[cfg(not(windows))] + #[cfg(target_os = "linux")] #[test] #[ignore = "test requires python 3.12 or higher"] fn symbolize_perf_map() { diff --git a/src/symbolize/symbolizer.rs b/src/symbolize/symbolizer.rs index f5f5a6c1..995835c3 100644 --- a/src/symbolize/symbolizer.rs +++ b/src/symbolize/symbolizer.rs @@ -36,7 +36,7 @@ use crate::symbolize::InlinedFn; use crate::symbolize::Resolve; use crate::symbolize::TranslateFileOffset; use crate::util; -#[cfg(not(windows))] +#[cfg(target_os = "linux")] use crate::util::uname_release; use crate::util::Dbg; #[cfg(feature = "tracing")] @@ -891,7 +891,7 @@ impl Symbolizer { Ok(resolver) } - #[cfg(not(windows))] + #[cfg(target_os = "linux")] fn create_kernel_resolver(&self, src: &Kernel) -> Result { let Kernel { kallsyms, @@ -954,10 +954,10 @@ impl Symbolizer { KernelResolver::new(ksym_resolver.cloned(), elf_resolver.cloned()) } - #[cfg(windows)] + #[cfg(not(target_os = "linux"))] fn create_kernel_resolver(&self, _src: &Kernel) -> Result { Err(Error::with_unsupported( - "kernel address symbolization support is not present on Windows", + "kernel address symbolization is unsupported on operating systems other than Linux", )) } diff --git a/src/util.rs b/src/util.rs index 886df1eb..f50c3071 100644 --- a/src/util.rs +++ b/src/util.rs @@ -280,7 +280,7 @@ pub fn stat(path: &Path) -> io::Result { } -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[cfg(test)] #[allow(clippy::absolute_paths)] fn fstat(fd: std::os::unix::io::RawFd) -> io::Result { @@ -295,7 +295,7 @@ fn fstat(fd: std::os::unix::io::RawFd) -> io::Result { } -#[cfg(not(windows))] +#[cfg(target_os = "linux")] pub(crate) fn uname_release() -> io::Result { let mut dst = MaybeUninit::uninit(); let rc = unsafe { libc::uname(dst.as_mut_ptr()) }; @@ -741,7 +741,7 @@ mod tests { /// Check that we can retrieve meta-data about a file using `stat` /// and `fstat`. - #[cfg(not(windows))] + #[cfg(target_os = "linux")] #[test] fn file_stating() { use std::os::fd::AsRawFd as _; diff --git a/tests/allocs.rs b/tests/allocs.rs index 9c791304..f967672f 100644 --- a/tests/allocs.rs +++ b/tests/allocs.rs @@ -4,7 +4,7 @@ clippy::let_and_return, clippy::let_unit_value )] -#![cfg_attr(windows, allow(dead_code, unused_imports))] +#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))] use std::alloc::GlobalAlloc; use std::alloc::Layout; @@ -56,7 +56,7 @@ unsafe impl GlobalAlloc for TracingAlloc { /// Normalize addresses in the current process and print allocation /// statistics. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[test] fn normalize_process() { let region = Region::new(&GLOBAL); diff --git a/tests/blazesym.rs b/tests/blazesym.rs index 9ff7e158..17486e9a 100644 --- a/tests/blazesym.rs +++ b/tests/blazesym.rs @@ -3,7 +3,7 @@ clippy::let_and_return, clippy::let_unit_value )] -#![cfg_attr(windows, allow(dead_code, unused_imports))] +#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))] use std::collections::HashMap; use std::env; @@ -16,7 +16,7 @@ use std::io::Read as _; use std::io::Write as _; use std::ops::ControlFlow; use std::ops::Deref as _; -#[cfg(not(windows))] +#[cfg(target_os = "linux")] use std::os::unix::ffi::OsStringExt as _; use std::path::Path; use std::process::Command; @@ -699,7 +699,7 @@ fn symbolize_process() { /// Check that we can symbolize an address in a process using a binary /// located in a local mount namespace. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[test] fn symbolize_process_in_mount_namespace() { use libc::kill; @@ -806,7 +806,7 @@ fn symbolize_process_with_custom_dispatch() { } /// Check that we can normalize addresses in an ELF shared object. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[test] fn normalize_elf_addr() { fn test(so: &str, map_files: bool) { @@ -872,7 +872,7 @@ fn normalize_elf_addr() { /// Check that we can enable/disable the reading of build IDs. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[test] fn normalize_build_id_reading() { fn test(read_build_ids: bool) { diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 91e74293..bfc66661 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -15,7 +15,7 @@ use libc::uid_t; /// Run a function with a different effective user ID. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] pub fn as_user(ruid: uid_t, euid: uid_t, f: F) -> R where F: FnOnce() -> R + UnwindSafe, @@ -44,7 +44,7 @@ where result.unwrap() } -#[cfg(windows)] +#[cfg(not(target_os = "linux"))] pub fn as_user(ruid: uid_t, euid: uid_t, f: F) -> R where F: FnOnce() -> R + UnwindSafe, diff --git a/tests/permission.rs b/tests/permission.rs index 0c209aa9..f811b370 100644 --- a/tests/permission.rs +++ b/tests/permission.rs @@ -3,9 +3,9 @@ clippy::let_and_return, clippy::let_unit_value )] -#![cfg_attr(windows, allow(dead_code, unused_imports))] +#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))] -#[cfg(not(windows))] +#[cfg(target_os = "linux")] mod common; use std::fs::copy; @@ -35,7 +35,7 @@ fn symbolize_no_permission_impl(path: &Path) { /// Check that we fail symbolization as expected when we don't have the /// permission to open the symbolization source. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[test] fn symbolize_no_permission() { use common::as_user; diff --git a/tests/permissionless.rs b/tests/permissionless.rs index cbf74ef2..9d20b687 100644 --- a/tests/permissionless.rs +++ b/tests/permissionless.rs @@ -3,9 +3,9 @@ clippy::let_and_return, clippy::let_unit_value )] -#![cfg_attr(windows, allow(dead_code, unused_imports))] +#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))] -#[cfg(not(windows))] +#[cfg(target_os = "linux")] mod common; use std::io::Error; @@ -75,7 +75,7 @@ fn normalize_permissionless_impl(pid: Pid, addr: Addr, test_lib: &Path) { ); } -#[cfg(not(windows))] +#[cfg(target_os = "linux")] fn run_test(callback_fn: F) where F: FnOnce(Pid, u64, &Path) + UnwindSafe, @@ -145,7 +145,7 @@ where /// Check that we can symbolize an address in a process using only /// symbolic paths. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[test] fn symbolize_process_symbolic_paths() { run_test(symbolize_permissionless_impl) @@ -153,7 +153,7 @@ fn symbolize_process_symbolic_paths() { /// Check that we can normalize an address in a process using only /// symbolic paths. -#[cfg(not(windows))] +#[cfg(target_os = "linux")] #[test] fn normalize_process_symbolic_paths() { run_test(normalize_permissionless_impl)