Skip to content

Commit b05d290

Browse files
committed
Rust: Exclude data flow inconsistencies that stem from other inconsistencies
1 parent 1b224c1 commit b05d290

File tree

7 files changed

+71
-29
lines changed

7 files changed

+71
-29
lines changed

rust/ql/consistency-queries/DataFlowConsistency.ql

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,4 @@
55
* @id rust/diagnostics/data-flow-consistency
66
*/
77

8-
import codeql.rust.dataflow.DataFlow::DataFlow as DataFlow
9-
private import rust
10-
private import codeql.rust.dataflow.internal.DataFlowImpl
11-
private import codeql.rust.dataflow.internal.TaintTrackingImpl
12-
private import codeql.dataflow.internal.DataFlowImplConsistency
13-
14-
private module Input implements InputSig<Location, RustDataFlow> { }
15-
16-
import MakeConsistency<Location, RustDataFlow, RustTaintTracking, Input>
8+
import codeql.rust.dataflow.internal.DataFlowConsistency

rust/ql/lib/codeql/rust/AstConsistency.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ query predicate multipleToStrings(Element e, string cls, string s) {
2121
*/
2222
query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) > 1 }
2323

24+
/**
25+
* Holds if `e` does not have a `Location`.
26+
*/
27+
query predicate noLocation(Locatable e) { not exists(e.getLocation()) }
28+
2429
private predicate multiplePrimaryQlClasses(Element e) {
2530
strictcount(string cls | cls = e.getAPrimaryQlClass() and cls != "VariableAccess") > 1
2631
}
@@ -58,6 +63,9 @@ int getAstInconsistencyCounts(string type) {
5863
type = "Multiple locations" and
5964
result = count(Element e | multipleLocations(e) | e)
6065
or
66+
type = "No location" and
67+
result = count(Element e | noLocation(e) | e)
68+
or
6169
type = "Multiple primary QL classes" and
6270
result = count(Element e | multiplePrimaryQlClasses(e) | e)
6371
or
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import codeql.rust.dataflow.DataFlow::DataFlow as DataFlow
2+
private import rust
3+
private import codeql.rust.dataflow.internal.DataFlowImpl
4+
private import codeql.rust.dataflow.internal.TaintTrackingImpl
5+
private import codeql.dataflow.internal.DataFlowImplConsistency
6+
7+
private module Input implements InputSig<Location, RustDataFlow> {
8+
predicate uniqueNodeLocationExclude(RustDataFlow::Node n) {
9+
// Exclude nodes where the missing location can be explained by the
10+
// underlying AST node not having a location.
11+
not exists(n.asExpr().getLocation())
12+
}
13+
14+
predicate missingLocationExclude(RustDataFlow::Node n) { not exists(n.asExpr().getLocation()) }
15+
}
16+
17+
import MakeConsistency<Location, RustDataFlow, RustTaintTracking, Input>

rust/ql/src/queries/diagnostics/DataFlowConsistencyCounts.ql

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@
55
* @id rust/diagnostics/data-flow-consistency-counts
66
*/
77

8-
private import rust
9-
private import codeql.rust.dataflow.internal.DataFlowImpl
10-
private import codeql.rust.dataflow.internal.TaintTrackingImpl
11-
private import codeql.dataflow.internal.DataFlowImplConsistency
12-
13-
private module Input implements InputSig<Location, RustDataFlow> { }
8+
import codeql.rust.dataflow.internal.DataFlowConsistency as Consistency
149

1510
// see also `rust/diagnostics/data-flow-consistency`, which lists the
1611
// individual inconsistency results.
1712
from string type, int num
18-
where
19-
num =
20-
MakeConsistency<Location, RustDataFlow, RustTaintTracking, Input>::getInconsistencyCounts(type)
13+
where num = Consistency::getInconsistencyCounts(type)
2114
select type, num
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
noLocation
2+
| file://:0:0:0:0 | ... .parent(...) |
3+
| file://:0:0:0:0 | ... .unwrap(...) |
4+
| file://:0:0:0:0 | ...: ... |
5+
| file://:0:0:0:0 | ...::Path |
6+
| file://:0:0:0:0 | ...::path |
7+
| file://:0:0:0:0 | ArgList |
8+
| file://:0:0:0:0 | ArgList |
9+
| file://:0:0:0:0 | MacroItems |
10+
| file://:0:0:0:0 | ParamList |
11+
| file://:0:0:0:0 | Path |
12+
| file://:0:0:0:0 | Path |
13+
| file://:0:0:0:0 | Path |
14+
| file://:0:0:0:0 | Path |
15+
| file://:0:0:0:0 | Path |
16+
| file://:0:0:0:0 | Path |
17+
| file://:0:0:0:0 | Path |
18+
| file://:0:0:0:0 | Path |
19+
| file://:0:0:0:0 | Path |
20+
| file://:0:0:0:0 | Path |
21+
| file://:0:0:0:0 | RefType |
22+
| file://:0:0:0:0 | RefType |
23+
| file://:0:0:0:0 | RetType |
24+
| file://:0:0:0:0 | StmtList |
25+
| file://:0:0:0:0 | Use |
26+
| file://:0:0:0:0 | UseTree |
27+
| file://:0:0:0:0 | fn get_parent |
28+
| file://:0:0:0:0 | get_parent |
29+
| file://:0:0:0:0 | parent |
30+
| file://:0:0:0:0 | path |
31+
| file://:0:0:0:0 | path |
32+
| file://:0:0:0:0 | path |
33+
| file://:0:0:0:0 | path |
34+
| file://:0:0:0:0 | path |
35+
| file://:0:0:0:0 | path |
36+
| file://:0:0:0:0 | path |
37+
| file://:0:0:0:0 | path |
38+
| file://:0:0:0:0 | std |
39+
| file://:0:0:0:0 | std |
40+
| file://:0:0:0:0 | std |
41+
| file://:0:0:0:0 | unwrap |
42+
| file://:0:0:0:0 | { ... } |

rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/DataFlowConsistency.expected

Lines changed: 0 additions & 11 deletions
This file was deleted.

rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| Multiple parents | 0 |
33
| Multiple primary QL classes | 0 |
44
| Multiple toStrings | 0 |
5+
| No location | 0 |

0 commit comments

Comments
 (0)