From 69e549f40094be05b24119d7662a1701cb59b912 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Wed, 11 Jun 2025 15:11:23 +0200 Subject: [PATCH 1/2] Rust: Generate canonical paths for builtins --- .../ql/lib/codeql/rust/internal/PathResolution.qll | 3 +++ .../canonical_path/canonical_paths.expected | 1 + .../canonical_path/canonical_paths.ql | 14 +++++++++++++- .../canonical_path/canonical_paths.qlref | 2 ++ .../canonical_paths.expected | 1 + .../canonical_path_disabled/canonical_paths.qlref | 3 ++- 6 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 rust/ql/test/extractor-tests/canonical_path/canonical_paths.qlref diff --git a/rust/ql/lib/codeql/rust/internal/PathResolution.qll b/rust/ql/lib/codeql/rust/internal/PathResolution.qll index 5e8c991a5820..8c6ba765ba38 100644 --- a/rust/ql/lib/codeql/rust/internal/PathResolution.qll +++ b/rust/ql/lib/codeql/rust/internal/PathResolution.qll @@ -374,6 +374,9 @@ class CrateItemNode extends ItemNode instanceof Crate { not file = child.(SourceFileItemNode).getSuper() and file = super.getSourceFile() ) + or + this.getName() = "core" and + child instanceof Builtins::BuiltinType } override string getCanonicalPath(Crate c) { c = this and result = Crate.super.getName() } diff --git a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected index 69ea1bb7b0e3..0376d52b26ed 100644 --- a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected +++ b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected @@ -1,6 +1,7 @@ canonicalPath | anonymous.rs:3:1:32:1 | fn canonicals | test::anonymous::canonicals | | anonymous.rs:34:1:36:1 | fn other | test::anonymous::other | +| {EXTERNAL LOCATION} | fn trim | ::trim | | lib.rs:1:1:1:14 | mod anonymous | test::anonymous | | lib.rs:2:1:2:12 | mod regular | test::regular | | regular.rs:1:1:2:18 | struct Struct | test::regular::Struct | diff --git a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql index 16aa82eee21c..b4ef7f1d86d1 100644 --- a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql +++ b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql @@ -1,8 +1,20 @@ import rust import TestUtils +private import codeql.rust.internal.PathResolution +private import codeql.rust.frameworks.stdlib.Bultins query predicate canonicalPath(Addressable a, string path) { - toBeTested(a) and + ( + toBeTested(a) + or + // test that we also generate canonical paths for builtins + a = + any(ImplItemNode i | + i.resolveSelfTy() instanceof Str and + not i.(Impl).hasTrait() + ).getAnAssocItem() and + a.(Function).getName().getText() = "trim" + ) and path = a.getCanonicalPath(_) } diff --git a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.qlref b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.qlref new file mode 100644 index 000000000000..c65650adf9ae --- /dev/null +++ b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.qlref @@ -0,0 +1,2 @@ +query: canonical_paths.ql +postprocess: utils/test/ExternalLocationPostProcessing.ql \ No newline at end of file diff --git a/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.expected b/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.expected index 2605a806f6f7..215645153853 100644 --- a/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.expected +++ b/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.expected @@ -1,6 +1,7 @@ canonicalPath | anonymous.rs:6:1:35:1 | fn canonicals | test::anonymous::canonicals | | anonymous.rs:37:1:39:1 | fn other | test::anonymous::other | +| {EXTERNAL LOCATION} | fn trim | ::trim | | lib.rs:1:1:1:14 | mod anonymous | test::anonymous | | lib.rs:2:1:2:12 | mod regular | test::regular | | regular.rs:4:1:5:18 | struct Struct | test::regular::Struct | diff --git a/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.qlref b/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.qlref index f40a74f5aeda..9a5712b5135b 100644 --- a/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.qlref +++ b/rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.qlref @@ -1 +1,2 @@ -extractor-tests/canonical_path/canonical_paths.ql +query: extractor-tests/canonical_path/canonical_paths.ql +postprocess: utils/test/ExternalLocationPostProcessing.ql \ No newline at end of file From 383cc5c2a796dd601249682376336e7796d1aa8c Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Thu, 12 Jun 2025 09:12:32 +0200 Subject: [PATCH 2/2] Rust: Rename `Bultins.qll` -> `Builtins.qll` --- .../codeql/rust/frameworks/stdlib/{Bultins.qll => Builtins.qll} | 0 rust/ql/lib/codeql/rust/internal/PathResolution.qll | 2 +- rust/ql/lib/codeql/rust/internal/TypeInference.qll | 2 +- rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename rust/ql/lib/codeql/rust/frameworks/stdlib/{Bultins.qll => Builtins.qll} (100%) diff --git a/rust/ql/lib/codeql/rust/frameworks/stdlib/Bultins.qll b/rust/ql/lib/codeql/rust/frameworks/stdlib/Builtins.qll similarity index 100% rename from rust/ql/lib/codeql/rust/frameworks/stdlib/Bultins.qll rename to rust/ql/lib/codeql/rust/frameworks/stdlib/Builtins.qll diff --git a/rust/ql/lib/codeql/rust/internal/PathResolution.qll b/rust/ql/lib/codeql/rust/internal/PathResolution.qll index 8c6ba765ba38..748fa0fa45da 100644 --- a/rust/ql/lib/codeql/rust/internal/PathResolution.qll +++ b/rust/ql/lib/codeql/rust/internal/PathResolution.qll @@ -5,7 +5,7 @@ private import rust private import codeql.rust.elements.internal.generated.ParentChild private import codeql.rust.internal.CachedStages -private import codeql.rust.frameworks.stdlib.Bultins as Builtins +private import codeql.rust.frameworks.stdlib.Builtins as Builtins private newtype TNamespace = TTypeNamespace() or diff --git a/rust/ql/lib/codeql/rust/internal/TypeInference.qll b/rust/ql/lib/codeql/rust/internal/TypeInference.qll index a6151eb62e79..715c0e3d4a34 100644 --- a/rust/ql/lib/codeql/rust/internal/TypeInference.qll +++ b/rust/ql/lib/codeql/rust/internal/TypeInference.qll @@ -7,7 +7,7 @@ private import Type as T private import TypeMention private import codeql.typeinference.internal.TypeInference private import codeql.rust.frameworks.stdlib.Stdlib -private import codeql.rust.frameworks.stdlib.Bultins as Builtins +private import codeql.rust.frameworks.stdlib.Builtins as Builtins class Type = T::Type; diff --git a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql index b4ef7f1d86d1..51fc3d4c2432 100644 --- a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql +++ b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql @@ -1,7 +1,7 @@ import rust import TestUtils private import codeql.rust.internal.PathResolution -private import codeql.rust.frameworks.stdlib.Bultins +private import codeql.rust.frameworks.stdlib.Builtins query predicate canonicalPath(Addressable a, string path) { (