|
| 1 | +/** |
| 2 | + * Provides classes for recognizing control flow graph inconsistencies. |
| 3 | + */ |
| 4 | + |
| 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 | +private import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl |
| 10 | +private import codeql.rust.controlflow.internal.Completion |
| 11 | + |
| 12 | +/** |
| 13 | + * All `Expr` nodes are `PostOrderTree`s |
| 14 | + */ |
| 15 | +query predicate nonPostOrderExpr(Expr e, string cls) { |
| 16 | + cls = e.getPrimaryQlClasses() and |
| 17 | + not e instanceof LetExpr and |
| 18 | + not e instanceof ParenExpr and |
| 19 | + exists(AstNode last, Completion c | |
| 20 | + CfgImpl::last(e, last, c) and |
| 21 | + last != e and |
| 22 | + c instanceof NormalCompletion |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Holds if CFG scope `scope` lacks an initial AST node. Overrides shared consistency predicate. |
| 28 | + */ |
| 29 | +query predicate scopeNoFirst(CfgScope scope) { |
| 30 | + Consistency::scopeNoFirst(scope) and |
| 31 | + not scope = any(Function f | not exists(f.getBody())) and |
| 32 | + not scope = any(ClosureExpr c | not exists(c.getBody())) |
| 33 | +} |
| 34 | + |
| 35 | +/** Holds if `be` is the `else` branch of a `let` statement that results in a panic. */ |
| 36 | +private predicate letElsePanic(BlockExpr be) { |
| 37 | + be = any(LetStmt let).getLetElse().getBlockExpr() and |
| 38 | + exists(Completion c | CfgImpl::last(be, _, c) | completionIsNormal(c)) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Holds if `node` is lacking a successor. Overrides shared consistency predicate. |
| 43 | + */ |
| 44 | +query predicate deadEnd(CfgImpl::Node node) { |
| 45 | + Consistency::deadEnd(node) and |
| 46 | + not letElsePanic(node.getAstNode()) |
| 47 | +} |
| 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 | nonUniqueSetRepresentation(ss, _) | ss) |
| 58 | + or |
| 59 | + type = "Splitting invariant 2" and |
| 60 | + result = count(AstNode n | breakInvariant2(n, _, _, _, _, _) | n) |
| 61 | + or |
| 62 | + type = "Splitting invariant 3" and |
| 63 | + result = count(AstNode n | breakInvariant3(n, _, _, _, _, _) | n) |
| 64 | + or |
| 65 | + type = "Splitting invariant 4" and |
| 66 | + result = count(AstNode n | breakInvariant4(n, _, _, _, _, _) | n) |
| 67 | + or |
| 68 | + type = "Splitting invariant 5" and |
| 69 | + result = count(AstNode n | breakInvariant5(n, _, _, _, _, _) | n) |
| 70 | + or |
| 71 | + type = "Multiple successors of the same type" and |
| 72 | + result = count(CfgNode n | multipleSuccessors(n, _, _) | n) |
| 73 | + or |
| 74 | + type = "Simple and normal successors" and |
| 75 | + result = count(CfgNode n | simpleAndNormalSuccessors(n, _, _, _, _) | n) |
| 76 | + or |
| 77 | + type = "Dead end" and |
| 78 | + result = count(CfgNode n | deadEnd(n) | n) |
| 79 | + or |
| 80 | + type = "Non-unique split kind" and |
| 81 | + result = count(CfgImpl::SplitImpl si | nonUniqueSplitKind(si, _) | si) |
| 82 | + or |
| 83 | + type = "Non-unique list order" and |
| 84 | + result = count(CfgImpl::SplitKind sk | nonUniqueListOrder(sk, _) | sk) |
| 85 | + or |
| 86 | + type = "Multiple toStrings" and |
| 87 | + result = count(CfgNode n | multipleToString(n, _) | n) |
| 88 | + or |
| 89 | + type = "CFG scope lacks initial AST node" and |
| 90 | + result = count(CfgScope s | scopeNoFirst(s) | s) |
| 91 | + or |
| 92 | + type = "Non-PostOrderTree Expr node" and |
| 93 | + result = count(Expr e | nonPostOrderExpr(e, _) | e) |
| 94 | +} |
0 commit comments