|
| 1 | +/** |
| 2 | + * Models local flow edges. Each equivalence class in the local flow relation becomes a super node. |
| 3 | + */ |
| 4 | + |
| 5 | +private import codeql_ql.dataflow.DataFlow |
| 6 | +private import codeql_ql.ast.Ast |
| 7 | +private import codeql_ql.ast.internal.AstNodeNumbering |
| 8 | +private import NodesInternal |
| 9 | +private import VarScoping |
| 10 | +private import DataFlowNumbering |
| 11 | + |
| 12 | +private module Cached { |
| 13 | + /** |
| 14 | + * Holds if `x` and `y` are bound by an equality (intra-predicate only). |
| 15 | + * |
| 16 | + * This edge has no orientation, and is used to construct the equivalence relation. |
| 17 | + * Each equivalence class becomes a `SuperNode`. |
| 18 | + */ |
| 19 | + private predicate localEdge(Node x, Node y) { |
| 20 | + exists(AstNode a, AstNode b | |
| 21 | + x = astNode(a) and |
| 22 | + y = astNode(b) |
| 23 | + | |
| 24 | + // x ~ any(x) |
| 25 | + a = b.(Any).getExpr(0) |
| 26 | + or |
| 27 | + // v ~ any(T v) |
| 28 | + a = b.(Any).getArgument(0) |
| 29 | + or |
| 30 | + // x ~ x as VAR |
| 31 | + a = b.(AsExpr).getInnerExpr() |
| 32 | + or |
| 33 | + // x ~ x.(Type) |
| 34 | + a = b.(InlineCast).getBase() |
| 35 | + or |
| 36 | + // x = y ==> x ~ y |
| 37 | + exists(ComparisonFormula compare | |
| 38 | + compare.getOperator() = "=" and |
| 39 | + a = compare.getLeftOperand() and |
| 40 | + b = compare.getRightOperand() |
| 41 | + ) |
| 42 | + ) |
| 43 | + or |
| 44 | + // VarAccess -> ScopedVariable |
| 45 | + exists(VarDef var, VarAccess access, VarAccessOrDisjunct scope | |
| 46 | + isRefinement(var, access, scope) and |
| 47 | + x = astNode(access) and |
| 48 | + y = scopedVariable(var, scope) |
| 49 | + ) |
| 50 | + or |
| 51 | + // VarAccess -> VarDef (if no refinement exists) |
| 52 | + exists(VarDef var, VarAccess access | |
| 53 | + isRefinement(var, access, getVarDefScope(var)) and |
| 54 | + x = astNode(access) and |
| 55 | + y = astNode(var) |
| 56 | + ) |
| 57 | + or |
| 58 | + // result ~ enclosing 'result' node |
| 59 | + x = resultNode(y.(AstNodeNode).getAstNode().(ResultAccess).getEnclosingPredicate()) |
| 60 | + or |
| 61 | + // this ~ enclosing 'this' node |
| 62 | + x = thisNode(y.(AstNodeNode).getAstNode().(ThisAccess).getEnclosingPredicate()) |
| 63 | + or |
| 64 | + // f ~ enclosing field node for 'f' |
| 65 | + exists(FieldAccess access | |
| 66 | + x = astNode(access) and |
| 67 | + y = fieldNode(access.getEnclosingPredicate(), access.getDeclaration()) |
| 68 | + ) |
| 69 | + or |
| 70 | + // field declaration ~ field node in the charpred |
| 71 | + exists(FieldDecl field, Class cls | |
| 72 | + cls.getAField() = field and |
| 73 | + x = astNode(field.getVarDecl()) and |
| 74 | + y = fieldNode(cls.getCharPred(), field) |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + /** Like `localEdge` but the parameters are mapped to their internal ID. */ |
| 79 | + private predicate rawLocalEdge(int x, int y) { |
| 80 | + exists(Node a, Node b | |
| 81 | + localEdge(a, b) and |
| 82 | + x = getInternalId(a) and |
| 83 | + y = getInternalId(b) |
| 84 | + ) |
| 85 | + or |
| 86 | + // Ensure a representative is generated for singleton components |
| 87 | + x = getInternalId(_) and |
| 88 | + y = x |
| 89 | + } |
| 90 | + |
| 91 | + /** Gets the representative for the equivalence class containing the node with ID `x`. */ |
| 92 | + private int getRawRepr(int x) = equivalenceRelation(rawLocalEdge/2)(x, result) |
| 93 | + |
| 94 | + /** Gets the ID for the equivalence class containing `node`. */ |
| 95 | + cached |
| 96 | + int getRepr(Node node) { result = getRawRepr(getInternalId(node)) } |
| 97 | + |
| 98 | + cached |
| 99 | + newtype TSuperNode = MkSuperNode(int repr) { repr = getRepr(_) } |
| 100 | +} |
| 101 | + |
| 102 | +import Cached |
0 commit comments