|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | private import rust |
6 | | -private import codeql.rust.controlflow.internal.ControlFlowGraphImpl::Consistency as Consistency |
7 | | -import Consistency |
8 | | -private import codeql.rust.controlflow.ControlFlowGraph |
9 | 6 | private import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl |
10 | | -private import codeql.rust.controlflow.internal.Completion |
11 | 7 | private import codeql.rust.controlflow.internal.CfgNodes::Consistency as CfgNodes |
12 | 8 | private import codeql.rust.Diagnostics |
13 | | - |
14 | | -private predicate nonPostOrderExpr(Expr e) { |
15 | | - not e instanceof LetExpr and |
16 | | - not e instanceof ParenExpr and |
17 | | - exists(AstNode last, Completion c | |
18 | | - CfgImpl::last(e, last, c) and |
19 | | - last != e and |
20 | | - c instanceof NormalCompletion |
21 | | - ) |
22 | | -} |
23 | | - |
24 | | -/** |
25 | | - * All `Expr` nodes are `PostOrderTree`s |
26 | | - */ |
27 | | -query predicate nonPostOrderExpr(Expr e, string cls) { |
28 | | - nonPostOrderExpr(e) and |
29 | | - cls = e.getPrimaryQlClasses() |
30 | | -} |
31 | | - |
32 | | -/** |
33 | | - * Holds if CFG scope `scope` lacks an initial AST node. Overrides shared consistency predicate. |
34 | | - */ |
35 | | -query predicate scopeNoFirst(CfgScope scope) { |
36 | | - Consistency::scopeNoFirst(scope) and |
37 | | - not scope = |
38 | | - [ |
39 | | - any(AstNode f | not f.(Callable).hasBody()), |
40 | | - any(AsyncBlockExpr b | not b.hasStmtList()) |
41 | | - ] |
42 | | -} |
43 | | - |
44 | | -/** Holds if `be` is the `else` branch of a `let` statement that results in a panic. */ |
45 | | -private predicate letElsePanic(BlockExpr be) { |
46 | | - be = any(LetStmt let).getLetElse().getBlockExpr() and |
47 | | - exists(Completion c | CfgImpl::last(be, _, c) | completionIsNormal(c)) |
48 | | -} |
49 | | - |
50 | | -/** |
51 | | - * Holds if `node` is lacking a successor. Overrides shared consistency predicate. |
52 | | - */ |
53 | | -query predicate deadEnd(CfgImpl::Node node) { |
54 | | - Consistency::deadEnd(node) and |
55 | | - successfullyExtractedFile(node.getLocation().getFile()) and |
56 | | - not letElsePanic(node.getAstNode()) |
57 | | -} |
| 9 | +import CfgImpl::ControlFlow::Consistency |
58 | 10 |
|
59 | 11 | pragma[nomagic] |
60 | 12 | private predicate successfullyExtractedFile(File f) { |
61 | | - not exists(ExtractionWarning ee | ee.getLocation().getFile() = f) |
| 13 | + not exists(ExtractionWarning warning | warning.getLocation().getFile() = f) |
62 | 14 | } |
63 | 15 |
|
64 | | -query predicate missingCfgChild(CfgNode parent, string pred, int i, AstNode child) { |
| 16 | +query predicate missingCfgChild( |
| 17 | + CfgImpl::ControlFlowNode parent, string pred, int i, AstNode child |
| 18 | +) { |
65 | 19 | CfgNodes::missingCfgChild(parent, pred, i, child) and |
66 | | - successfullyExtractedFile(child.getLocation().getFile()) and |
67 | | - not exists(AstNode last, CfgImpl::Completion c | CfgImpl::last(child, last, c) | |
68 | | - // In for example `if (a && true) ...`, there is no edge from the CFG node |
69 | | - // for `true` into the `[false] a && true` node. |
70 | | - strictcount(ConditionalSuccessor cs | exists(last.getACfgNode().getASuccessor(cs)) | cs) = 1 |
71 | | - or |
72 | | - // In for example `x && return`, there is no edge from the node for |
73 | | - // `return` into the `&&` node. |
74 | | - not c instanceof CfgImpl::NormalCompletion |
75 | | - ) |
| 20 | + successfullyExtractedFile(child.getLocation().getFile()) |
76 | 21 | } |
77 | 22 |
|
78 | 23 | /** |
79 | 24 | * Gets counts of control flow graph inconsistencies of each type. |
80 | 25 | */ |
81 | 26 | int getCfgInconsistencyCounts(string type) { |
82 | | - // total results from all the CFG consistency query predicates in: |
83 | | - // - `codeql.rust.controlflow.internal.CfgConsistency` (this file) |
84 | | - // - `shared.controlflow.codeql.controlflow.Cfg` |
85 | | - type = "Non-unique set representation" and |
86 | | - result = count(CfgImpl::Splits ss | nonUniqueSetRepresentation(ss, _) | ss) |
87 | | - or |
88 | | - type = "Splitting invariant 2" and |
89 | | - result = count(AstNode n | breakInvariant2(n, _, _, _, _, _) | n) |
90 | | - or |
91 | | - type = "Splitting invariant 3" and |
92 | | - result = count(AstNode n | breakInvariant3(n, _, _, _, _, _) | n) |
93 | | - or |
94 | | - type = "Splitting invariant 4" and |
95 | | - result = count(AstNode n | breakInvariant4(n, _, _, _, _, _) | n) |
96 | | - or |
97 | | - type = "Splitting invariant 5" and |
98 | | - result = count(AstNode n | breakInvariant5(n, _, _, _, _, _) | n) |
99 | | - or |
100 | | - type = "Multiple successors of the same type" and |
101 | | - result = count(CfgNode n | multipleSuccessors(n, _, _) | n) |
102 | | - or |
103 | | - type = "Simple and normal successors" and |
104 | | - result = count(CfgNode n | simpleAndNormalSuccessors(n, _, _, _, _) | n) |
105 | | - or |
106 | | - type = "Dead end" and |
107 | | - result = count(CfgNode n | deadEnd(n) | n) |
108 | | - or |
109 | | - type = "Non-unique split kind" and |
110 | | - result = count(CfgImpl::SplitImpl si | nonUniqueSplitKind(si, _) | si) |
111 | | - or |
112 | | - type = "Non-unique list order" and |
113 | | - result = count(CfgImpl::SplitKind sk | nonUniqueListOrder(sk, _) | sk) |
114 | | - or |
115 | | - type = "Multiple toStrings" and |
116 | | - result = count(CfgNode n | multipleToString(n) | n) |
117 | | - or |
118 | | - type = "CFG scope lacks initial AST node" and |
119 | | - result = count(CfgScope s | scopeNoFirst(s) | s) |
120 | | - or |
121 | | - type = "Non-PostOrderTree Expr node" and |
122 | | - result = count(Expr e | nonPostOrderExpr(e) | e) |
| 27 | + CfgImpl::ControlFlow::Consistency::consistencyOverview(type, result) |
123 | 28 | or |
124 | 29 | type = "Missing CFG child" and |
125 | | - result = count(CfgNode parent, string pred, int child | missingCfgChild(parent, pred, child, _)) |
| 30 | + result = |
| 31 | + count(CfgImpl::ControlFlowNode parent, string pred, int child | |
| 32 | + missingCfgChild(parent, pred, child, _) |
| 33 | + ) |
126 | 34 | } |
0 commit comments