|
| 1 | +/** |
| 2 | + * DEPRECATED: Using the methods in this module may lead to a degradation of performance. Use at |
| 3 | + * your own peril. |
| 4 | + * |
| 5 | + * This module contains legacy points-to predicates and methods for various classes in the |
| 6 | + * points-to analysis. |
| 7 | + * |
| 8 | + * Existing code that depends on, say, points-to predicates on `ControlFlowNode` should be modified |
| 9 | + * to use `ControlFlowNodeWithPointsTo` instead. In particular, if inside a method call chain such |
| 10 | + * as |
| 11 | + * |
| 12 | + * `someCallNode.getFunction().pointsTo(...)` |
| 13 | + * |
| 14 | + * an explicit cast should be added as follows |
| 15 | + * |
| 16 | + * `someCallNode.getFunction().(ControlFlowNodeWithPointsTo).pointsTo(...)` |
| 17 | + * |
| 18 | + * Similarly, if a bound variable has type `ControlFlowNode`, and a points-to method is called on |
| 19 | + * it, the type should be changed to `ControlFlowNodeWithPointsTo`. |
| 20 | + */ |
| 21 | + |
| 22 | +private import python |
| 23 | +private import semmle.python.pointsto.PointsTo |
| 24 | +private import semmle.python.objects.Modules |
| 25 | + |
| 26 | +/** |
| 27 | + * An extension of `ControlFlowNode` that provides points-to predicates. |
| 28 | + */ |
| 29 | +class ControlFlowNodeWithPointsTo extends ControlFlowNode { |
| 30 | + /** Gets the value that this ControlFlowNode points-to. */ |
| 31 | + predicate pointsTo(Value value) { this.pointsTo(_, value, _) } |
| 32 | + |
| 33 | + /** Gets the value that this ControlFlowNode points-to. */ |
| 34 | + Value pointsTo() { this.pointsTo(_, result, _) } |
| 35 | + |
| 36 | + /** Gets a value that this ControlFlowNode may points-to. */ |
| 37 | + Value inferredValue() { this.pointsTo(_, result, _) } |
| 38 | + |
| 39 | + /** Gets the value and origin that this ControlFlowNode points-to. */ |
| 40 | + predicate pointsTo(Value value, ControlFlowNode origin) { this.pointsTo(_, value, origin) } |
| 41 | + |
| 42 | + /** Gets the value and origin that this ControlFlowNode points-to, given the context. */ |
| 43 | + predicate pointsTo(Context context, Value value, ControlFlowNode origin) { |
| 44 | + PointsTo::pointsTo(this, context, value, origin) |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets what this flow node might "refer-to". Performs a combination of localized (intra-procedural) points-to |
| 49 | + * analysis and global module-level analysis. This points-to analysis favours precision over recall. It is highly |
| 50 | + * precise, but may not provide information for a significant number of flow-nodes. |
| 51 | + * If the class is unimportant then use `refersTo(value)` or `refersTo(value, origin)` instead. |
| 52 | + */ |
| 53 | + pragma[nomagic] |
| 54 | + predicate refersTo(Object obj, ClassObject cls, ControlFlowNode origin) { |
| 55 | + this.refersTo(_, obj, cls, origin) |
| 56 | + } |
| 57 | + |
| 58 | + /** Gets what this expression might "refer-to" in the given `context`. */ |
| 59 | + pragma[nomagic] |
| 60 | + predicate refersTo(Context context, Object obj, ClassObject cls, ControlFlowNode origin) { |
| 61 | + not obj = unknownValue() and |
| 62 | + not cls = theUnknownType() and |
| 63 | + PointsTo::points_to(this, context, obj, cls, origin) |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Whether this flow node might "refer-to" to `value` which is from `origin` |
| 68 | + * Unlike `this.refersTo(value, _, origin)` this predicate includes results |
| 69 | + * where the class cannot be inferred. |
| 70 | + */ |
| 71 | + pragma[nomagic] |
| 72 | + predicate refersTo(Object obj, ControlFlowNode origin) { |
| 73 | + not obj = unknownValue() and |
| 74 | + PointsTo::points_to(this, _, obj, _, origin) |
| 75 | + } |
| 76 | + |
| 77 | + /** Equivalent to `this.refersTo(value, _)` */ |
| 78 | + predicate refersTo(Object obj) { this.refersTo(obj, _) } |
| 79 | + |
| 80 | + /** |
| 81 | + * Check whether this control-flow node has complete points-to information. |
| 82 | + * This would mean that the analysis managed to infer an over approximation |
| 83 | + * of possible values at runtime. |
| 84 | + */ |
| 85 | + predicate hasCompletePointsToSet() { |
| 86 | + // If the tracking failed, then `this` will be its own "origin". In that |
| 87 | + // case, we want to exclude nodes for which there is also a different |
| 88 | + // origin, as that would indicate that some paths failed and some did not. |
| 89 | + this.refersTo(_, _, this) and |
| 90 | + not exists(ControlFlowNode other | other != this and this.refersTo(_, _, other)) |
| 91 | + or |
| 92 | + // If `this` is a use of a variable, then we must have complete points-to |
| 93 | + // for that variable. |
| 94 | + exists(SsaVariable v | v.getAUse() = this | varHasCompletePointsToSet(v)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Check whether a SSA variable has complete points-to information. |
| 100 | + * This would mean that the analysis managed to infer an overapproximation |
| 101 | + * of possible values at runtime. |
| 102 | + */ |
| 103 | +private predicate varHasCompletePointsToSet(SsaVariable var) { |
| 104 | + // Global variables may be modified non-locally or concurrently. |
| 105 | + not var.getVariable() instanceof GlobalVariable and |
| 106 | + ( |
| 107 | + // If we have complete points-to information on the definition of |
| 108 | + // this variable, then the variable has complete information. |
| 109 | + var.getDefinition() |
| 110 | + .(DefinitionNode) |
| 111 | + .getValue() |
| 112 | + .(ControlFlowNodeWithPointsTo) |
| 113 | + .hasCompletePointsToSet() |
| 114 | + or |
| 115 | + // If this variable is a phi output, then we have complete |
| 116 | + // points-to information about it if all phi inputs had complete |
| 117 | + // information. |
| 118 | + forex(SsaVariable phiInput | phiInput = var.getAPhiInput() | |
| 119 | + varHasCompletePointsToSet(phiInput) |
| 120 | + ) |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * An extension of `Expr` that provides points-to predicates. |
| 126 | + */ |
| 127 | +class ExprWithPointsTo extends Expr { |
| 128 | + /** |
| 129 | + * NOTE: `refersTo` will be deprecated in 2019. Use `pointsTo` instead. |
| 130 | + * Gets what this expression might "refer-to". Performs a combination of localized (intra-procedural) points-to |
| 131 | + * analysis and global module-level analysis. This points-to analysis favours precision over recall. It is highly |
| 132 | + * precise, but may not provide information for a significant number of flow-nodes. |
| 133 | + * If the class is unimportant then use `refersTo(value)` or `refersTo(value, origin)` instead. |
| 134 | + * NOTE: For complex dataflow, involving multiple stages of points-to analysis, it may be more precise to use |
| 135 | + * `ControlFlowNode.refersTo(...)` instead. |
| 136 | + */ |
| 137 | + predicate refersTo(Object obj, ClassObject cls, AstNode origin) { |
| 138 | + this.refersTo(_, obj, cls, origin) |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * NOTE: `refersTo` will be deprecated in 2019. Use `pointsTo` instead. |
| 143 | + * Gets what this expression might "refer-to" in the given `context`. |
| 144 | + */ |
| 145 | + predicate refersTo(Context context, Object obj, ClassObject cls, AstNode origin) { |
| 146 | + this.getAFlowNode() |
| 147 | + .(ControlFlowNodeWithPointsTo) |
| 148 | + .refersTo(context, obj, cls, origin.getAFlowNode()) |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * NOTE: `refersTo` will be deprecated in 2019. Use `pointsTo` instead. |
| 153 | + * Holds if this expression might "refer-to" to `value` which is from `origin` |
| 154 | + * Unlike `this.refersTo(value, _, origin)`, this predicate includes results |
| 155 | + * where the class cannot be inferred. |
| 156 | + */ |
| 157 | + pragma[nomagic] |
| 158 | + predicate refersTo(Object obj, AstNode origin) { |
| 159 | + this.getAFlowNode().(ControlFlowNodeWithPointsTo).refersTo(obj, origin.getAFlowNode()) |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * NOTE: `refersTo` will be deprecated in 2019. Use `pointsTo` instead. |
| 164 | + * Equivalent to `this.refersTo(value, _)` |
| 165 | + */ |
| 166 | + predicate refersTo(Object obj) { this.refersTo(obj, _) } |
| 167 | + |
| 168 | + /** |
| 169 | + * Holds if this expression might "point-to" to `value` which is from `origin` |
| 170 | + * in the given `context`. |
| 171 | + */ |
| 172 | + predicate pointsTo(Context context, Value value, AstNode origin) { |
| 173 | + this.getAFlowNode() |
| 174 | + .(ControlFlowNodeWithPointsTo) |
| 175 | + .pointsTo(context, value, origin.getAFlowNode()) |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Holds if this expression might "point-to" to `value` which is from `origin`. |
| 180 | + */ |
| 181 | + predicate pointsTo(Value value, AstNode origin) { |
| 182 | + this.getAFlowNode().(ControlFlowNodeWithPointsTo).pointsTo(value, origin.getAFlowNode()) |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Holds if this expression might "point-to" to `value`. |
| 187 | + */ |
| 188 | + predicate pointsTo(Value value) { this.pointsTo(value, _) } |
| 189 | + |
| 190 | + /** Gets a value that this expression might "point-to". */ |
| 191 | + Value pointsTo() { this.pointsTo(result) } |
| 192 | + |
| 193 | + override string getAQlClass() { none() } |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * An extension of `Module` that provides points-to related methods. |
| 198 | + */ |
| 199 | +class ModuleWithPointsTo extends Module { |
| 200 | + /** Gets a name exported by this module, that is the names that will be added to a namespace by 'from this-module import *' */ |
| 201 | + string getAnExport() { |
| 202 | + py_exports(this, result) |
| 203 | + or |
| 204 | + exists(ModuleObjectInternal mod | mod.getSource() = this.getEntryNode() | |
| 205 | + mod.(ModuleValue).exports(result) |
| 206 | + ) |
| 207 | + } |
| 208 | + |
| 209 | + override string getAQlClass() { none() } |
| 210 | +} |
0 commit comments