Skip to content

Commit 06b1d8e

Browse files
authored
Merge pull request github#18177 from hvitved/rust/dataflow-variant-canonical-path
Rust: Use canonical paths for variants in data flow
2 parents cc4382c + 3c767b0 commit 06b1d8e

File tree

6 files changed

+418
-358
lines changed

6 files changed

+418
-358
lines changed

rust/ql/lib/codeql/rust/dataflow/FlowSummary.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private module Summaries {
1313
UnwrapSummary() { this = "lang:core::_::<crate::option::Option>::unwrap" }
1414

1515
override predicate propagatesFlow(string input, string output, boolean preservesValue) {
16-
input = "Argument[self].Variant[crate::std::option::Option::Some(0)]" and
16+
input = "Argument[self].Variant[crate::option::Option::Some(0)]" and
1717
output = "ReturnValue" and
1818
preservesValue = true
1919
}

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,7 @@ module RustDataFlow implements InputSig<Location> {
788788
(
789789
LocalFlow::localFlowStepCommon(nodeFrom, nodeTo)
790790
or
791-
exists(SsaImpl::DefinitionExt def, boolean isUseStep |
792-
SsaFlow::localFlowStep(def, nodeFrom, nodeTo, isUseStep)
793-
|
791+
exists(boolean isUseStep | SsaFlow::localFlowStep(_, nodeFrom, nodeTo, isUseStep) |
794792
isUseStep = false
795793
or
796794
isUseStep = true and
@@ -825,15 +823,13 @@ module RustDataFlow implements InputSig<Location> {
825823
exists(CrateOriginOption crate, string path |
826824
resolveExtendedCanonicalPath(p.getQualifier(), crate, path) and
827825
v = MkVariantCanonicalPath(crate, path, p.getPart().getNameRef().getText())
826+
or
827+
exists(string name |
828+
not p.hasQualifier() and
829+
resolveExtendedCanonicalPath(p, crate, path + "::" + name) and
830+
v = MkVariantCanonicalPath(crate, path, name)
831+
)
828832
)
829-
or
830-
// TODO: Remove once library types are extracted
831-
not p.hasQualifier() and
832-
v = MkVariantCanonicalPath(_, "crate::std::option::Option", p.getPart().getNameRef().getText())
833-
or
834-
// TODO: Remove once library types are extracted
835-
not p.hasQualifier() and
836-
v = MkVariantCanonicalPath(_, "crate::std::result::Result", p.getPart().getNameRef().getText())
837833
}
838834

839835
/** Holds if `p` destructs an enum variant `v`. */
@@ -1099,20 +1095,22 @@ private module Cached {
10991095
cached
11001096
newtype TReturnKind = TNormalReturnKind()
11011097

1098+
private CrateOriginOption langCoreCrate() { result.asSome() = "lang:core" }
1099+
11021100
cached
11031101
newtype TVariantCanonicalPath =
11041102
MkVariantCanonicalPath(CrateOriginOption crate, string path, string name) {
11051103
variantHasExtendedCanonicalPath(_, _, crate, path, name)
11061104
or
11071105
// TODO: Remove once library types are extracted
1108-
crate.isNone() and
1109-
path = "crate::std::option::Option" and
1110-
name = "Some"
1111-
or
1112-
// TODO: Remove once library types are extracted
1113-
crate.isNone() and
1114-
path = "crate::std::result::Result" and
1115-
name = ["Ok", "Err"]
1106+
crate = langCoreCrate() and
1107+
(
1108+
path = "crate::option::Option" and
1109+
name = "Some"
1110+
or
1111+
path = "crate::result::Result" and
1112+
name = ["Ok", "Err"]
1113+
)
11161114
}
11171115

11181116
cached
@@ -1127,11 +1125,11 @@ private module Cached {
11271125
pos in [0 .. v.getVariant().getFieldList().(TupleFieldList).getNumberOfFields() - 1]
11281126
or
11291127
// TODO: Remove once library types are extracted
1130-
v = MkVariantCanonicalPath(_, "crate::std::option::Option", "Some") and
1128+
v = MkVariantCanonicalPath(langCoreCrate(), "crate::option::Option", "Some") and
11311129
pos = 0
11321130
or
11331131
// TODO: Remove once library types are extracted
1134-
v = MkVariantCanonicalPath(_, "crate::std::result::Result", ["Ok", "Err"]) and
1132+
v = MkVariantCanonicalPath(langCoreCrate(), "crate::result::Result", ["Ok", "Err"]) and
11351133
pos = 0
11361134
} or
11371135
TVariantFieldContent(VariantCanonicalPath v, string field) {

0 commit comments

Comments
 (0)