Skip to content

Commit 7b712f3

Browse files
committed
Rust: Calculate a total of CFG inconsistencies.
1 parent 4398c83 commit 7b712f3

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

rust/ql/lib/codeql/rust/controlflow/internal/CfgConsistency.qll

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,50 @@ query predicate deadEnd(CfgImpl::Node node) {
4545
Consistency::deadEnd(node) and
4646
not letElsePanic(node.getAstNode())
4747
}
48+
49+
/**
50+
* Gets counts of control flow graph inconsistencies of each type.
51+
*/
52+
int getCfgInconsistencyCounts(string type) {
53+
// total results from all the CFG consistency query predicates in:
54+
// - `codeql.rust.controlflow.internal.CfgConsistency` (this file)
55+
// - `shared.controlflow.codeql.controlflow.Cfg`
56+
type = "Non-unique set representation" and
57+
result = count(CfgImpl::Splits ss | Consistency::nonUniqueSetRepresentation(ss, _) | ss)
58+
or
59+
type = "Splitting invariant 2" and
60+
result = count(AstNode n | Consistency::breakInvariant2(n, _, _, _, _, _) | n)
61+
or
62+
type = "Splitting invariant 3" and
63+
result = count(AstNode n | Consistency::breakInvariant3(n, _, _, _, _, _) | n)
64+
or
65+
type = "Splitting invariant 4" and
66+
result = count(AstNode n | Consistency::breakInvariant4(n, _, _, _, _, _) | n)
67+
or
68+
type = "Splitting invariant 5" and
69+
result = count(AstNode n | Consistency::breakInvariant5(n, _, _, _, _, _) | n)
70+
or
71+
type = "Multiple successors of the same type" and
72+
result = count(CfgNode n | Consistency::multipleSuccessors(n, _, _) | n)
73+
or
74+
type = "Simple and normal successors" and
75+
result = count(CfgNode n | Consistency::simpleAndNormalSuccessors(n, _, _, _, _) | n)
76+
or
77+
type = "Dead end" and
78+
result = count(CfgNode n | Consistency::deadEnd(n) | n)
79+
or
80+
type = "Non-unique split kind" and
81+
result = count(CfgImpl::SplitImpl si | Consistency::nonUniqueSplitKind(si, _) | si)
82+
or
83+
type = "Non-unique list order" and
84+
result = count(CfgImpl::SplitKind sk | Consistency::nonUniqueListOrder(sk, _) | sk)
85+
or
86+
type = "Multiple toStrings" and
87+
result = count(CfgNode n | Consistency::multipleToString(n, _) | n)
88+
or
89+
type = "CFG scope lacks initial AST node" and
90+
result = count(CfgScope s | Consistency::scopeNoFirst(s) | s)
91+
or
92+
type = "Non-PostOrderTree Expr node" and
93+
result = count(Expr e | nonPostOrderExpr(e, _) | e)
94+
}

rust/ql/src/queries/summary/Stats.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
*/
44

55
import rust
6+
private import codeql.rust.controlflow.internal.CfgConsistency as CfgConsistency
67

8+
/**
9+
* Gets a count of the total number of lines of code in the database.
10+
*/
711
int getLinesOfCode() { result = sum(File f | | f.getNumberOfLinesOfCode()) }
812

13+
/**
14+
* Gets a count of the total number of lines of code from the source code directory in the database.
15+
*/
916
int getLinesOfUserCode() {
1017
result = sum(File f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode())
1118
}
19+
20+
/**
21+
* Gets a count of the total number of control flow graph inconsistencies in the database.
22+
*/
23+
int getTotalCfgInconsistencies() { result = sum(CfgConsistency::getCfgInconsistencyCounts(_)) }

rust/ql/src/queries/summary/SummaryStats.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ where
3333
key = "Lines of code extracted" and value = getLinesOfCode().toString()
3434
or
3535
key = "Lines of user code extracted" and value = getLinesOfUserCode().toString()
36+
or
37+
key = "Inconsistencies - CFG" and value = getTotalCfgInconsistencies().toString()
3638
select key, value

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
| Files extracted - total | 7 |
66
| Files extracted - with errors | 2 |
77
| Files extracted - without errors | 5 |
8+
| Inconsistencies - CFG | 0 |
89
| Lines of code extracted | 59 |
910
| Lines of user code extracted | 59 |

0 commit comments

Comments
 (0)