diff --git a/rust/extractor/src/main.rs b/rust/extractor/src/main.rs index 92f119b68777..8cc30615b8cd 100644 --- a/rust/extractor/src/main.rs +++ b/rust/extractor/src/main.rs @@ -330,8 +330,12 @@ fn main() -> anyhow::Result<()> { } // Fast path when there is no ambiguity in dependency extraction. if dependency_extraction_ambiguity { - for (_, file) in dependency_files(&db, &vfs, &source_files) { - extractor.extract_with_semantics(file, &semantics, &vfs, library_mode); + for (file_id, file) in dependency_files(&db, &vfs, &source_files) { + // Dependency files outside any crate's module tree (e.g. + // tests, examples or cfg-disabled modules) are skipped. + if semantics.file_to_module_def(file_id).is_some() { + extractor.extract_with_semantics(file, &semantics, &vfs, library_mode); + } } } else { dependencies.add_manifest(manifest, &semantics, &vfs, &source_files); @@ -366,13 +370,15 @@ fn main() -> anyhow::Result<()> { // the case `file_to_module_def` will return `None` for the // file. When this is the case and there are alternatives we // do not extract the file yet. Instead the file will be - // considered again in the next iteration of the loop. - if !dependencies.has_alternative(&file) - || path_to_file_id(&file, &vfs) - .and_then(|id| semantics.file_to_module_def(id)) - .is_some() - { + // considered again in the next iteration of the loop. If + // no configuration includes the file, it is not extracted. + let in_module = path_to_file_id(&file, &vfs) + .and_then(|id| semantics.file_to_module_def(id)) + .is_some(); + if in_module { extractor.extract_with_semantics(&file, &semantics, &vfs, library_mode); + } + if in_module || !dependencies.has_alternative(&file) { dependencies.extracted(&file); } } diff --git a/rust/ql/integration-tests/dependency-features/dependencies/alpha/src/orphan.rs b/rust/ql/integration-tests/dependency-features/dependencies/alpha/src/orphan.rs new file mode 100644 index 000000000000..0ec6e756896a --- /dev/null +++ b/rust/ql/integration-tests/dependency-features/dependencies/alpha/src/orphan.rs @@ -0,0 +1,2 @@ +// Not part of alpha's module tree, so it must not be extracted. +pub fn orphan() {} diff --git a/rust/ql/integration-tests/dependency-features/inactive-module.ql b/rust/ql/integration-tests/dependency-features/inactive-module.ql index 9096a1ac43c6..d6c96f606d05 100644 --- a/rust/ql/integration-tests/dependency-features/inactive-module.ql +++ b/rust/ql/integration-tests/dependency-features/inactive-module.ql @@ -1,5 +1,5 @@ import rust from Function f -where f.getFile().getBaseName() = "only_a.rs" +where f.getFile().getBaseName() = ["only_a.rs", "orphan.rs"] select f, f.getName() diff --git a/rust/ql/integration-tests/dependency-features/test_dependency_features.py b/rust/ql/integration-tests/dependency-features/test_dependency_features.py index a7da594e6908..f90242ff0f71 100644 --- a/rust/ql/integration-tests/dependency-features/test_dependency_features.py +++ b/rust/ql/integration-tests/dependency-features/test_dependency_features.py @@ -35,7 +35,10 @@ def test_dependency_features(codeql, rust, cwd, configurations, reverse): |-- dependencies/ | |-- alpha/ | | |-- Cargo.toml - | | `-- src/lib.rs # feature-gated functions and invoke! macro + | | `-- src/ + | | |-- lib.rs # feature-gated functions and invoke! macro + | | |-- only_a.rs # feature-gated module + | | `-- orphan.rs # not in the module tree | `-- beta/ | |-- Cargo.toml | `-- src/lib.rs # feature-gated functions and invoke! macro @@ -55,7 +58,8 @@ def test_dependency_features(codeql, rust, cwd, configurations, reverse): Alpha's `only_a` module is disabled by feature `b`. Its item macro must still be expanded using a configuration in which the module is enabled, even when - a richer configuration wins for alpha's other files. + a richer configuration wins for alpha's other files. Alpha's `orphan.rs` is + not in the module tree of any configuration, so it must not be extracted. """ if reverse: configurations = configurations[::-1] diff --git a/rust/ql/lib/change-notes/2026-12-02-skip-orphan-dependency-files.md b/rust/ql/lib/change-notes/2026-12-02-skip-orphan-dependency-files.md new file mode 100644 index 000000000000..458d573241c2 --- /dev/null +++ b/rust/ql/lib/change-notes/2026-12-02-skip-orphan-dependency-files.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The Rust extractor no longer extracts dependency files that are not part of any crate's module tree (for example tests, examples, or modules disabled for the current target). This makes extraction faster and the resulting databases smaller. diff --git a/rust/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue.expected b/rust/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue.expected index 50e78116e348..96fad6bd10d7 100644 --- a/rust/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue.expected +++ b/rust/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue.expected @@ -38,36 +38,36 @@ edges | test_cipher.rs:18:28:18:36 | &... [&ref] | test_cipher.rs:18:9:18:14 | const1 [&ref] | provenance | | | test_cipher.rs:18:29:18:36 | [0u8; 16] | test_cipher.rs:18:28:18:36 | &... [&ref] | provenance | | | test_cipher.rs:19:49:19:79 | ...::from_slice(...) [&ref] | test_cipher.rs:19:49:19:79 | ...::from_slice(...) | provenance | Sink:MaD:3 | -| test_cipher.rs:19:73:19:78 | const1 [&ref] | test_cipher.rs:19:49:19:79 | ...::from_slice(...) [&ref] | provenance | MaD:22 | +| test_cipher.rs:19:73:19:78 | const1 [&ref] | test_cipher.rs:19:49:19:79 | ...::from_slice(...) [&ref] | provenance | MaD:21 | | test_cipher.rs:25:9:25:14 | const4 [&ref] | test_cipher.rs:26:66:26:71 | const4 [&ref] | provenance | | | test_cipher.rs:25:28:25:36 | &... [&ref] | test_cipher.rs:25:9:25:14 | const4 [&ref] | provenance | | | test_cipher.rs:25:29:25:36 | [0u8; 16] | test_cipher.rs:25:28:25:36 | &... [&ref] | provenance | | | test_cipher.rs:26:42:26:72 | ...::from_slice(...) [&ref] | test_cipher.rs:26:42:26:72 | ...::from_slice(...) | provenance | Sink:MaD:4 | -| test_cipher.rs:26:66:26:71 | const4 [&ref] | test_cipher.rs:26:42:26:72 | ...::from_slice(...) [&ref] | provenance | MaD:22 | +| test_cipher.rs:26:66:26:71 | const4 [&ref] | test_cipher.rs:26:42:26:72 | ...::from_slice(...) [&ref] | provenance | MaD:21 | | test_cipher.rs:29:9:29:14 | const5 [&ref] | test_cipher.rs:30:95:30:100 | const5 [&ref] | provenance | | | test_cipher.rs:29:28:29:36 | &... [&ref] | test_cipher.rs:29:9:29:14 | const5 [&ref] | provenance | | | test_cipher.rs:29:29:29:36 | [0u8; 16] | test_cipher.rs:29:28:29:36 | &... [&ref] | provenance | | | test_cipher.rs:30:72:30:101 | ...::from_slice(...) [&ref] | test_cipher.rs:30:72:30:101 | ...::from_slice(...) | provenance | Sink:MaD:5 | -| test_cipher.rs:30:95:30:100 | const5 [&ref] | test_cipher.rs:30:72:30:101 | ...::from_slice(...) [&ref] | provenance | MaD:22 | +| test_cipher.rs:30:95:30:100 | const5 [&ref] | test_cipher.rs:30:72:30:101 | ...::from_slice(...) [&ref] | provenance | MaD:21 | | test_cipher.rs:37:9:37:14 | const7 | test_cipher.rs:38:74:38:79 | const7 | provenance | | | test_cipher.rs:37:27:37:74 | [...] | test_cipher.rs:37:9:37:14 | const7 | provenance | | | test_cipher.rs:38:49:38:80 | ...::from_slice(...) [&ref] | test_cipher.rs:38:49:38:80 | ...::from_slice(...) | provenance | Sink:MaD:3 | -| test_cipher.rs:38:73:38:79 | &const7 [&ref] | test_cipher.rs:38:49:38:80 | ...::from_slice(...) [&ref] | provenance | MaD:22 | +| test_cipher.rs:38:73:38:79 | &const7 [&ref] | test_cipher.rs:38:49:38:80 | ...::from_slice(...) [&ref] | provenance | MaD:21 | | test_cipher.rs:38:74:38:79 | const7 | test_cipher.rs:38:73:38:79 | &const7 [&ref] | provenance | | | test_cipher.rs:41:9:41:14 | const8 [&ref] | test_cipher.rs:42:73:42:78 | const8 [&ref] | provenance | | | test_cipher.rs:41:28:41:76 | &... [&ref] | test_cipher.rs:41:9:41:14 | const8 [&ref] | provenance | | | test_cipher.rs:41:29:41:76 | [...] | test_cipher.rs:41:28:41:76 | &... [&ref] | provenance | | | test_cipher.rs:42:49:42:79 | ...::from_slice(...) [&ref] | test_cipher.rs:42:49:42:79 | ...::from_slice(...) | provenance | Sink:MaD:3 | -| test_cipher.rs:42:73:42:78 | const8 [&ref] | test_cipher.rs:42:49:42:79 | ...::from_slice(...) [&ref] | provenance | MaD:22 | +| test_cipher.rs:42:73:42:78 | const8 [&ref] | test_cipher.rs:42:49:42:79 | ...::from_slice(...) [&ref] | provenance | MaD:21 | | test_cipher.rs:50:9:50:15 | const10 [element] | test_cipher.rs:51:75:51:81 | const10 [element] | provenance | | | test_cipher.rs:50:37:50:54 | ...::zeroed(...) | test_cipher.rs:50:9:50:15 | const10 [element] | provenance | Src:MaD:11 | | test_cipher.rs:51:50:51:82 | ...::from_slice(...) [&ref, element] | test_cipher.rs:51:50:51:82 | ...::from_slice(...) | provenance | Sink:MaD:3 Sink:MaD:3 | -| test_cipher.rs:51:74:51:81 | &const10 [&ref, element] | test_cipher.rs:51:50:51:82 | ...::from_slice(...) [&ref, element] | provenance | MaD:22 | +| test_cipher.rs:51:74:51:81 | &const10 [&ref, element] | test_cipher.rs:51:50:51:82 | ...::from_slice(...) [&ref, element] | provenance | MaD:21 | | test_cipher.rs:51:75:51:81 | const10 [element] | test_cipher.rs:51:74:51:81 | &const10 [&ref, element] | provenance | | | test_cipher.rs:66:9:66:14 | const2 [&ref] | test_cipher.rs:67:35:67:40 | const2 [&ref] | provenance | | | test_cipher.rs:66:18:66:26 | &... [&ref] | test_cipher.rs:66:9:66:14 | const2 [&ref] | provenance | | | test_cipher.rs:66:19:66:26 | [0u8; 32] | test_cipher.rs:66:18:66:26 | &... [&ref] | provenance | | -| test_cipher.rs:67:35:67:40 | const2 [&ref] | test_cipher.rs:67:35:67:47 | const2.into() [element] | provenance | MaD:20 | +| test_cipher.rs:67:35:67:40 | const2 [&ref] | test_cipher.rs:67:35:67:47 | const2.into() [element] | provenance | MaD:13 | | test_cipher.rs:67:35:67:47 | const2.into() [element] | test_cipher.rs:67:35:67:47 | const2.into() | provenance | Sink:MaD:7 | | test_cipher.rs:73:9:73:14 | const2 [&ref] | test_cipher.rs:74:46:74:51 | const2 | provenance | Sink:MaD:8 | | test_cipher.rs:73:18:73:26 | &... [&ref] | test_cipher.rs:73:9:73:14 | const2 [&ref] | provenance | | @@ -75,15 +75,15 @@ edges | test_cipher.rs:80:9:80:14 | const6 [&ref] | test_cipher.rs:81:63:81:68 | const6 [&ref] | provenance | | | test_cipher.rs:80:18:80:26 | &... [&ref] | test_cipher.rs:80:9:80:14 | const6 [&ref] | provenance | | | test_cipher.rs:80:19:80:26 | [0u8; 32] | test_cipher.rs:80:18:80:26 | &... [&ref] | provenance | | -| test_cipher.rs:81:63:81:68 | const6 [&ref] | test_cipher.rs:81:63:81:75 | const6.into() [element] | provenance | MaD:20 | +| test_cipher.rs:81:63:81:68 | const6 [&ref] | test_cipher.rs:81:63:81:75 | const6.into() [element] | provenance | MaD:13 | | test_cipher.rs:81:63:81:75 | const6.into() [element] | test_cipher.rs:81:63:81:75 | const6.into() | provenance | Sink:MaD:9 | | test_cipher.rs:84:9:84:14 | const7 [&ref] | test_cipher.rs:85:75:85:80 | const7 [&ref] | provenance | | | test_cipher.rs:84:18:84:27 | &... [&ref] | test_cipher.rs:84:9:84:14 | const7 [&ref] | provenance | | | test_cipher.rs:84:19:84:27 | [0u8; 16] | test_cipher.rs:84:18:84:27 | &... [&ref] | provenance | | -| test_cipher.rs:85:75:85:80 | const7 [&ref] | test_cipher.rs:85:75:85:87 | const7.into() [element] | provenance | MaD:20 | +| test_cipher.rs:85:75:85:80 | const7 [&ref] | test_cipher.rs:85:75:85:87 | const7.into() [element] | provenance | MaD:13 | | test_cipher.rs:85:75:85:87 | const7.into() [element] | test_cipher.rs:85:75:85:87 | const7.into() | provenance | Sink:MaD:10 | | test_cipher.rs:94:9:94:12 | key9 [&ref] | test_cipher.rs:95:63:95:66 | key9 [&ref] | provenance | | -| test_cipher.rs:94:23:94:40 | "1234567890123456" | test_cipher.rs:94:23:94:51 | "1234567890123456".as_bytes() [&ref] | provenance | MaD:21 | +| test_cipher.rs:94:23:94:40 | "1234567890123456" | test_cipher.rs:94:23:94:51 | "1234567890123456".as_bytes() [&ref] | provenance | MaD:20 | | test_cipher.rs:94:23:94:51 | "1234567890123456".as_bytes() [&ref] | test_cipher.rs:94:9:94:12 | key9 [&ref] | provenance | | | test_cipher.rs:95:63:95:66 | key9 [&ref] | test_cipher.rs:95:63:95:73 | key9.into() [element] | provenance | MaD:13 | | test_cipher.rs:95:63:95:73 | key9.into() [element] | test_cipher.rs:95:63:95:73 | key9.into() | provenance | Sink:MaD:9 | @@ -110,7 +110,7 @@ edges | test_cipher.rs:129:33:129:41 | [0xff; 32] | test_cipher.rs:129:32:129:41 | &... [&ref] | provenance | | | test_cipher.rs:130:9:130:12 | key3 [&ref] | test_cipher.rs:132:35:132:38 | key3 [&ref] | provenance | | | test_cipher.rs:130:16:130:55 | ...::from_slice(...) [&ref] | test_cipher.rs:130:9:130:12 | key3 [&ref] | provenance | | -| test_cipher.rs:130:45:130:54 | key3_array [&ref] | test_cipher.rs:130:16:130:55 | ...::from_slice(...) [&ref] | provenance | MaD:22 | +| test_cipher.rs:130:45:130:54 | key3_array [&ref] | test_cipher.rs:130:16:130:55 | ...::from_slice(...) [&ref] | provenance | MaD:21 | | test_cipher.rs:131:9:131:14 | nonce3 | test_cipher.rs:133:30:133:35 | nonce3 | provenance | | | test_cipher.rs:131:27:131:35 | [0xff; 12] | test_cipher.rs:131:9:131:14 | nonce3 | provenance | | | test_cipher.rs:132:34:132:38 | &key3 [&ref, &ref] | test_cipher.rs:132:34:132:38 | &key3 | provenance | Sink:MaD:7 Sink:MaD:7 | @@ -132,7 +132,7 @@ edges | test_cookie.rs:22:27:22:32 | array2 | test_cookie.rs:22:26:22:32 | &array2 [&ref] | provenance | | | test_cookie.rs:25:9:25:12 | str3 | test_cookie.rs:30:26:30:29 | str3 | provenance | | | test_cookie.rs:25:16:29:5 | match val { ... } | test_cookie.rs:25:9:25:12 | str3 | provenance | | -| test_cookie.rs:30:26:30:29 | str3 | test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | provenance | MaD:21 | +| test_cookie.rs:30:26:30:29 | str3 | test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | provenance | MaD:20 | | test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | test_cookie.rs:30:26:30:40 | str3.as_bytes() | provenance | Sink:MaD:6 | | test_cookie.rs:33:9:33:14 | array4 | test_cookie.rs:38:27:38:32 | array4 | provenance | | | test_cookie.rs:33:27:37:5 | [...] | test_cookie.rs:33:9:33:14 | array4 | provenance | | @@ -146,7 +146,7 @@ edges | test_cookie.rs:54:28:54:36 | [0u8; 64] | test_cookie.rs:54:18:54:37 | ...::from(...) | provenance | MaD:18 | | test_cookie.rs:54:28:54:36 | [0u8; 64] | test_cookie.rs:54:18:54:37 | ...::from(...) | provenance | MaD:19 | | test_cookie.rs:65:9:65:14 | array3 [element] | test_cookie.rs:69:34:69:39 | array3 | provenance | Sink:MaD:2 | -| test_cookie.rs:65:23:65:25 | 0u8 | test_cookie.rs:65:23:65:29 | ...::from_elem(...) [element] | provenance | MaD:23 | +| test_cookie.rs:65:23:65:25 | 0u8 | test_cookie.rs:65:23:65:29 | ...::from_elem(...) [element] | provenance | MaD:22 | | test_cookie.rs:65:23:65:29 | ...::from_elem(...) [element] | test_cookie.rs:65:9:65:14 | array3 [element] | provenance | | | test_heuristic.rs:38:25:38:30 | 0xFFFF | test_heuristic.rs:81:22:81:31 | MY_CONST_1 | provenance | | | test_heuristic.rs:39:25:39:59 | ... as u64 | test_heuristic.rs:82:22:82:31 | MY_CONST_2 | provenance | | @@ -179,10 +179,9 @@ models | 17 | Summary: ::from; Argument[0].Field[alloc::bstr::ByteString(0)]; ReturnValue; value | | 18 | Summary: ::from; Argument[0].Field[alloc::collections::binary_heap::BinaryHeap::data]; ReturnValue; value | | 19 | Summary: ::from; Argument[0].Field[alloc::string::String::vec]; ReturnValue; value | -| 20 | Summary: ::into; Argument[self].Reference.Element; ReturnValue.Element; taint | -| 21 | Summary: ::as_bytes; Argument[self].Reference; ReturnValue.Reference; taint | -| 22 | Summary: ::from_slice; Argument[0].Reference; ReturnValue.Reference; value | -| 23 | Summary: alloc::vec::from_elem; Argument[0]; ReturnValue.Element; value | +| 20 | Summary: ::as_bytes; Argument[self].Reference; ReturnValue.Reference; taint | +| 21 | Summary: ::from_slice; Argument[0].Reference; ReturnValue.Reference; value | +| 22 | Summary: alloc::vec::from_elem; Argument[0]; ReturnValue.Element; value | nodes | test_cipher.rs:18:9:18:14 | const1 [&ref] | semmle.label | const1 [&ref] | | test_cipher.rs:18:28:18:36 | &... [&ref] | semmle.label | &... [&ref] |