Skip to content

Commit

Permalink
Generalize OS based conditional compilation conditions
Browse files Browse the repository at this point in the history
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 <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Oct 23, 2024
1 parent 555ce12 commit 798e6e6
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions dev/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
// SAFETY: `sysconf` is always safe to call.
let rc = unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) };
Expand All @@ -45,7 +45,7 @@ fn page_size() -> Result<usize> {
Ok(usize::try_from(rc).unwrap())
}

#[cfg(windows)]
#[cfg(not(target_os = "linux"))]
fn page_size() -> Result<usize> {
unimplemented!()
}
Expand Down
6 changes: 3 additions & 3 deletions src/file_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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 _,
}
}
Expand Down Expand Up @@ -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 _;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
4 changes: 2 additions & 2 deletions src/normalize/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/normalize/normalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/symbolize/perf_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions src/symbolize/symbolizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -891,7 +891,7 @@ impl Symbolizer {
Ok(resolver)
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn create_kernel_resolver(&self, src: &Kernel) -> Result<KernelResolver> {
let Kernel {
kallsyms,
Expand Down Expand Up @@ -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<KernelResolver> {
Err(Error::with_unsupported(
"kernel address symbolization support is not present on Windows",
"kernel address symbolization is unsupported on operating systems other than Linux",
))
}

Expand Down
6 changes: 3 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub fn stat(path: &Path) -> io::Result<libc::stat> {
}


#[cfg(not(windows))]
#[cfg(target_os = "linux")]
#[cfg(test)]
#[allow(clippy::absolute_paths)]
fn fstat(fd: std::os::unix::io::RawFd) -> io::Result<libc::stat> {
Expand All @@ -295,7 +295,7 @@ fn fstat(fd: std::os::unix::io::RawFd) -> io::Result<libc::stat> {
}


#[cfg(not(windows))]
#[cfg(target_os = "linux")]
pub(crate) fn uname_release() -> io::Result<CString> {
let mut dst = MaybeUninit::uninit();
let rc = unsafe { libc::uname(dst.as_mut_ptr()) };
Expand Down Expand Up @@ -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 _;
Expand Down
4 changes: 2 additions & 2 deletions tests/allocs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions tests/blazesym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, R>(ruid: uid_t, euid: uid_t, f: F) -> R
where
F: FnOnce() -> R + UnwindSafe,
Expand Down Expand Up @@ -44,7 +44,7 @@ where
result.unwrap()
}

#[cfg(windows)]
#[cfg(not(target_os = "linux"))]
pub fn as_user<F, R>(ruid: uid_t, euid: uid_t, f: F) -> R
where
F: FnOnce() -> R + UnwindSafe,
Expand Down
6 changes: 3 additions & 3 deletions tests/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions tests/permissionless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<F>(callback_fn: F)
where
F: FnOnce(Pid, u64, &Path) + UnwindSafe,
Expand Down Expand Up @@ -145,15 +145,15 @@ 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)
}

/// 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)
Expand Down

0 comments on commit 798e6e6

Please sign in to comment.