From 7556d7b57b3bf4258d3c805115c17e358c56f3cc Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Mon, 7 Jul 2025 18:01:07 +0200 Subject: [PATCH 1/3] Rust: add test with extern block --- .../canonical_path/canonical_paths.expected | 6 ++++++ rust/ql/test/extractor-tests/canonical_path/regular.rs | 8 ++++++++ 2 files changed, 14 insertions(+) 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 b44276608445..cf52b904bada 100644 --- a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected +++ b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected @@ -25,6 +25,7 @@ canonicalPath | regular.rs:40:1:46:1 | fn enum_qualified_usage | test::regular::enum_qualified_usage | | regular.rs:48:1:55:1 | fn enum_unqualified_usage | test::regular::enum_unqualified_usage | | regular.rs:57:1:63:1 | fn enum_match | test::regular::enum_match | +| regular.rs:69:1:71:1 | fn is_number_or_letter | test::regular::is_number_or_letter | canonicalPaths | anonymous.rs:1:1:1:26 | use ...::Trait | None | None | | anonymous.rs:3:1:32:1 | fn canonicals | repo::test | crate::anonymous::canonicals | @@ -65,6 +66,9 @@ canonicalPaths | regular.rs:48:1:55:1 | fn enum_unqualified_usage | repo::test | crate::regular::enum_unqualified_usage | | regular.rs:51:5:51:18 | use MyEnum::* | None | None | | regular.rs:57:1:63:1 | fn enum_match | repo::test | crate::regular::enum_match | +| regular.rs:65:1:67:1 | ExternBlock | None | None | +| regular.rs:66:5:66:40 | fn is_alphanum | repo::test | ::is_alphanum | +| regular.rs:69:1:71:1 | fn is_number_or_letter | repo::test | crate::regular::is_number_or_letter | resolvedPaths | anonymous.rs:27:17:27:30 | OtherStruct {...} | None | None | | anonymous.rs:28:9:28:9 | s | None | None | @@ -96,3 +100,5 @@ resolvedPaths | regular.rs:59:9:59:24 | ...::Variant1 | repo::test | crate::regular::MyEnum::Variant1 | | regular.rs:60:9:60:27 | ...::Variant2(...) | repo::test | crate::regular::MyEnum::Variant2 | | regular.rs:61:9:61:31 | ...::Variant3 {...} | repo::test | crate::regular::MyEnum::Variant3 | +| regular.rs:70:14:70:24 | is_alphanum | repo::test | ::is_alphanum | +| regular.rs:70:26:70:28 | chr | None | None | diff --git a/rust/ql/test/extractor-tests/canonical_path/regular.rs b/rust/ql/test/extractor-tests/canonical_path/regular.rs index 82b0525489fb..67464ecc1858 100644 --- a/rust/ql/test/extractor-tests/canonical_path/regular.rs +++ b/rust/ql/test/extractor-tests/canonical_path/regular.rs @@ -61,3 +61,11 @@ fn enum_match(e: MyEnum) { MyEnum::Variant3 { .. } => {} } } + +extern "C" { + pub fn is_alphanum(chr: u8) -> bool; +} + +pub fn is_number_or_letter(chr: u8) -> bool { + unsafe { is_alphanum(chr) } +} From da2f0f6069820582f3b699c9b82c30fd1a4400a6 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Mon, 7 Jul 2025 12:56:35 +0200 Subject: [PATCH 2/3] Rust: remove MacroCallItemNode Macro calls are not really items, so they can just be skipped --- .../codeql/rust/internal/PathResolution.qll | 42 ------------------- 1 file changed, 42 deletions(-) diff --git a/rust/ql/lib/codeql/rust/internal/PathResolution.qll b/rust/ql/lib/codeql/rust/internal/PathResolution.qll index 0d4e8e163be1..a460691bb3fc 100644 --- a/rust/ql/lib/codeql/rust/internal/PathResolution.qll +++ b/rust/ql/lib/codeql/rust/internal/PathResolution.qll @@ -147,12 +147,6 @@ abstract class ItemNode extends Locatable { ) ) or - // items made available through macro calls are available to nodes that contain the macro call - exists(MacroCallItemNode call | - call = this.getASuccessorRec(_) and - result = call.(ItemNode).getASuccessorRec(name) - ) - or // a trait has access to the associated items of its supertraits this = any(TraitItemNode trait | @@ -680,32 +674,6 @@ private class ImplTraitTypeReprItemNode extends ItemNode instanceof ImplTraitTyp override string getCanonicalPath(Crate c) { none() } } -private class MacroCallItemNode extends AssocItemNode instanceof MacroCall { - override string getName() { result = "(macro call)" } - - override predicate hasImplementation() { none() } - - override Namespace getNamespace() { none() } - - override TypeParam getTypeParam(int i) { none() } - - override Visibility getVisibility() { none() } - - override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) { - any(ItemNode parent).providesCanonicalPathPrefixFor(c, this) and - child.getImmediateParent() = this - } - - override string getCanonicalPathPrefixFor(Crate c, ItemNode child) { - result = this.getCanonicalPathPrefix(c) and - this.providesCanonicalPathPrefixFor(c, child) - } - - override predicate hasCanonicalPath(Crate c) { none() } - - override string getCanonicalPath(Crate c) { none() } -} - private class ModuleItemNode extends ModuleLikeNode instanceof Module { override string getName() { result = Module.super.getName().getText() } @@ -726,11 +694,6 @@ private class ModuleItemNode extends ModuleLikeNode instanceof Module { ) or this = child.getImmediateParent() - or - exists(ItemNode mid | - this.providesCanonicalPathPrefixFor(c, mid) and - mid.(MacroCallItemNode) = child.getImmediateParent() - ) ) } @@ -1186,11 +1149,6 @@ private predicate declares(ItemNode item, Namespace ns, string name) { useTreeDeclares(child.(Use).getUseTree(), name) and exists(ns) // `use foo::bar` can refer to both a value and a type ) - or - exists(MacroCallItemNode call | - declares(call, ns, name) and - call.getImmediateParent() = item - ) } /** A path that does not access a local variable. */ From 7721d14314443e469f57fcebf7b85d07d06c38fa Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Mon, 7 Jul 2025 13:01:11 +0200 Subject: [PATCH 3/3] Rust: use getADescendant instead of getAnItem This should handle all cases where items contained in intermediate nodes, such as MacroCall, ExternBlock and MacroItem nodes. --- rust/ql/lib/codeql/rust/internal/PathResolution.qll | 6 +++--- .../extractor-tests/canonical_path/canonical_paths.expected | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/rust/ql/lib/codeql/rust/internal/PathResolution.qll b/rust/ql/lib/codeql/rust/internal/PathResolution.qll index a460691bb3fc..dbfa9a62dd77 100644 --- a/rust/ql/lib/codeql/rust/internal/PathResolution.qll +++ b/rust/ql/lib/codeql/rust/internal/PathResolution.qll @@ -567,7 +567,7 @@ class ImplItemNode extends ImplOrTraitItemNode instanceof Impl { TraitItemNode resolveTraitTy() { result = resolvePath(this.getTraitPath()) } - override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() } + override AssocItemNode getAnAssocItem() { result = this.getADescendant() } override string getName() { result = "(impl)" } @@ -760,7 +760,7 @@ class TraitItemNode extends ImplOrTraitItemNode, TypeItemNode instanceof Trait { pragma[nomagic] ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) } - override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() } + override AssocItemNode getAnAssocItem() { result = this.getADescendant() } override string getName() { result = Trait.super.getName().getText() } @@ -956,7 +956,7 @@ class TypeParamItemNode extends TypeItemNode instanceof TypeParam { /** Holds if `item` has the name `name` and is a top-level item inside `f`. */ private predicate sourceFileEdge(SourceFile f, string name, ItemNode item) { - item = f.getAnItem() and + item = f.(ItemNode).getADescendant() and name = item.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 cf52b904bada..4983ee6e7ca0 100644 --- a/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected +++ b/rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected @@ -25,6 +25,7 @@ canonicalPath | regular.rs:40:1:46:1 | fn enum_qualified_usage | test::regular::enum_qualified_usage | | regular.rs:48:1:55:1 | fn enum_unqualified_usage | test::regular::enum_unqualified_usage | | regular.rs:57:1:63:1 | fn enum_match | test::regular::enum_match | +| regular.rs:66:5:66:40 | fn is_alphanum | test::regular::is_alphanum | | regular.rs:69:1:71:1 | fn is_number_or_letter | test::regular::is_number_or_letter | canonicalPaths | anonymous.rs:1:1:1:26 | use ...::Trait | None | None | pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy