Skip to main content

xtask/tasks/fmt/lints/
cfg_target_arch.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Checks that code uses `guest_arch` instead of `target_arch` for
5//! guest-architecture-specific conditionals.
6
7use super::Lint;
8use super::LintCtx;
9use super::Lintable;
10use std::path::Path;
11use toml_edit::DocumentMut;
12
13const SUPPRESS: &str = "xtask-fmt allow-target-arch";
14
15/// Using `target_arch` in order to execute CPU-specific intrinsics
16const SUPPRESS_REASON_CPU_INTRINSIC: &str = "cpu-intrinsic";
17/// Using `target_arch` in order to implement a '*-sys'-like crate (where the
18/// structure changes depending on the host-arch)
19const SUPPRESS_REASON_SYS_CRATE: &str = "sys-crate";
20/// A dependency of this crate will not build on other target architectures
21const SUPPRESS_REASON_DEPENDENCY: &str = "dependency";
22/// One off - support for the auto-arch selection logic in
23/// `build_rs_guest_arch`.
24const SUPPRESS_REASON_ONEOFF_GUEST_ARCH_IMPL: &str = "oneoff-guest-arch-impl";
25/// One off - used as part of flowey CI infra
26const SUPPRESS_REASON_ONEOFF_FLOWEY: &str = "oneoff-flowey";
27/// One off - used by petri to select native test dependencies
28const SUPPRESS_REASON_ONEOFF_PETRI_NATIVE_TEST_DEPS: &str = "oneoff-petri-native-test-deps";
29/// One off - used by petri to return the host architecture for test filtering
30const SUPPRESS_REASON_ONEOFF_PETRI_HOST_ARCH: &str = "oneoff-petri-host-arch";
31
32fn has_suppress(s: &str) -> bool {
33    let Some((_, after)) = s.split_once(SUPPRESS) else {
34        return false;
35    };
36
37    let after = after.trim();
38    let justification = after.split(' ').next().unwrap();
39
40    matches!(
41        justification,
42        SUPPRESS_REASON_CPU_INTRINSIC
43            | SUPPRESS_REASON_SYS_CRATE
44            | SUPPRESS_REASON_DEPENDENCY
45            | SUPPRESS_REASON_ONEOFF_GUEST_ARCH_IMPL
46            | SUPPRESS_REASON_ONEOFF_FLOWEY
47            | SUPPRESS_REASON_ONEOFF_PETRI_NATIVE_TEST_DEPS
48            | SUPPRESS_REASON_ONEOFF_PETRI_HOST_ARCH
49    )
50}
51
52/// Paths that are exempt from the target_arch lint.
53fn is_exempt(path: &Path) -> bool {
54    // guest_test_uefi is a guest-side crate (the code runs in the guest), so
55    // target_arch here is actually referring to the guest_arch
56    //
57    // openhcl_boot uses target_arch liberally, since it runs in VTL2 entirely
58    // in-service to the VTL2 linux kernel, which will always be native-arch.
59    // Similar for the sidecar kernel and TMKs. And minimal_rt provides the
60    // (arch-specific) runtime for both of them.
61    //
62    // support crates are not VM specific, so guest_arch doesn't make sense
63    // there.
64    //
65    // the whp/kvm crates are inherently arch-specific, as they contain
66    // low-level bindings to a particular platform's virtualization APIs
67    //
68    // The [open]TMK-related crates run in the guest and are inherently arch-specific.
69    path.starts_with("guest_test_uefi")
70        || path.starts_with("openhcl/openhcl_boot")
71        || path.starts_with("openhcl/minimal_rt")
72        || path.starts_with("openhcl/minimal_rt_reloc")
73        || path.starts_with("openhcl/sidecar")
74        || path.starts_with("support")
75        || path.starts_with("tmk/simple_tmk")
76        || path.starts_with("tmk/tmk_core")
77        || path.starts_with("opentmk")
78        || path.starts_with("vm/whp")
79        || path.starts_with("vm/kvm")
80}
81
82pub struct CfgTargetArch;
83
84impl Lint for CfgTargetArch {
85    fn new(_ctx: &LintCtx) -> Self {
86        CfgTargetArch
87    }
88
89    fn enter_workspace(&mut self, _content: &Lintable<DocumentMut>) {}
90    fn enter_crate(&mut self, _content: &Lintable<DocumentMut>) {}
91
92    fn visit_file(&mut self, content: &mut Lintable<String>) {
93        let path = content.path();
94
95        // Exclude ourselves from the lint (we mention the patterns in strings).
96        if path.ends_with(file!()) || is_exempt(path) {
97            return;
98        }
99
100        let mut prev_line = "";
101        for (i, line) in content.lines().enumerate() {
102            if line.contains("target_arch =") || line.contains("CARGO_CFG_TARGET_ARCH") {
103                // check if current line contains valid suppress, or is commented out
104                if !line.trim().starts_with("//") && !has_suppress(line) && !has_suppress(prev_line)
105                {
106                    content.unfixable(&format!(
107                        "unjustified `cfg(target_arch = ...)` at line {}",
108                        i + 1
109                    ));
110                }
111            }
112            prev_line = line;
113        }
114    }
115
116    fn exit_crate(&mut self, _content: &mut Lintable<DocumentMut>) {}
117    fn exit_workspace(&mut self, _content: &mut Lintable<DocumentMut>) {}
118}
119
120#[cfg(test)]
121mod tests {
122    use super::super::test_support::failed;
123    use super::super::test_support::lintable;
124    use super::*;
125
126    fn fails(path: &str, text: &str) -> bool {
127        let mut file = lintable(path, text.to_owned(), false);
128        CfgTargetArch.visit_file(&mut file);
129        failed(&file)
130    }
131
132    // Test: Lint target-architecture conditionals that are unjustified, justified
133    //       on the same or previous line, commented out, or use an unknown reason.
134    // Expected: Only recognized justifications, comments and exempt paths pass;
135    //       unknown or empty reasons and justifications two lines above fail.
136    #[test]
137    fn requires_known_adjacent_justification_outside_exempt_paths() {
138        let p = "vm/devices/x/src/lib.rs";
139        let cfg = "#[cfg(target_arch = \"x86_64\")]";
140        assert!(fails(p, cfg));
141        assert!(fails(p, "let a = env!(\"CARGO_CFG_TARGET_ARCH\");"));
142        assert!(!fails(
143            p,
144            &format!("{cfg} // xtask-fmt allow-target-arch sys-crate")
145        ));
146        assert!(!fails(
147            p,
148            &format!("// xtask-fmt allow-target-arch cpu-intrinsic\n{cfg}")
149        ));
150        assert!(fails(
151            p,
152            &format!("// xtask-fmt allow-target-arch cpu-intrinsic\n\n{cfg}")
153        ));
154        assert!(fails(
155            p,
156            &format!("{cfg} // xtask-fmt allow-target-arch because")
157        ));
158        assert!(fails(p, &format!("{cfg} // xtask-fmt allow-target-arch")));
159        assert!(!fails(p, &format!("   // {cfg}")));
160        assert!(!fails(p, "#[cfg(guest_arch = \"x86_64\")]"));
161        for exempt in [
162            "support/a/src/lib.rs",
163            "opentmk/x/src/lib.rs",
164            "vm/kvm/src/lib.rs",
165        ] {
166            assert!(!fails(exempt, cfg), "{exempt}");
167        }
168        assert!(fails("vm/kvmx/src/lib.rs", cfg));
169    }
170}