|
20 | 20 | */ |
21 | 21 |
|
22 | 22 | private import python |
23 | | -private import semmle.python.pointsto.PointsTo |
24 | | -private import semmle.python.objects.Modules |
| 23 | +import semmle.python.pointsto.Base |
| 24 | +import semmle.python.pointsto.Context |
| 25 | +import semmle.python.pointsto.PointsTo |
| 26 | +import semmle.python.pointsto.PointsToContext |
| 27 | +import semmle.python.objects.ObjectAPI |
| 28 | +import semmle.python.objects.ObjectInternal |
| 29 | +import semmle.python.types.Object |
| 30 | +import semmle.python.types.ClassObject |
| 31 | +import semmle.python.types.FunctionObject |
| 32 | +import semmle.python.types.ModuleObject |
| 33 | +import semmle.python.types.Exceptions |
| 34 | +import semmle.python.types.Properties |
| 35 | +import semmle.python.types.Descriptors |
| 36 | +import semmle.python.SelfAttribute |
| 37 | +import semmle.python.Metrics |
25 | 38 |
|
26 | 39 | /** |
27 | 40 | * An extension of `ControlFlowNode` that provides points-to predicates. |
@@ -93,6 +106,24 @@ class ControlFlowNodeWithPointsTo extends ControlFlowNode { |
93 | 106 | // for that variable. |
94 | 107 | exists(SsaVariable v | v.getAUse() = this | varHasCompletePointsToSet(v)) |
95 | 108 | } |
| 109 | + |
| 110 | + /** Whether it is unlikely that this ControlFlowNode can be reached */ |
| 111 | + predicate unlikelyReachable() { |
| 112 | + not start_bb_likely_reachable(this.getBasicBlock()) |
| 113 | + or |
| 114 | + exists(BasicBlock b | |
| 115 | + start_bb_likely_reachable(b) and |
| 116 | + not end_bb_likely_reachable(b) and |
| 117 | + // If there is an unlikely successor edge earlier in the BB |
| 118 | + // than this node, then this node must be unreachable. |
| 119 | + exists(ControlFlowNode p, int i, int j | |
| 120 | + p.(RaisingNode).unlikelySuccessor(_) and |
| 121 | + p = b.getNode(i) and |
| 122 | + this = b.getNode(j) and |
| 123 | + i < j |
| 124 | + ) |
| 125 | + ) |
| 126 | + } |
96 | 127 | } |
97 | 128 |
|
98 | 129 | /** |
@@ -121,6 +152,45 @@ private predicate varHasCompletePointsToSet(SsaVariable var) { |
121 | 152 | ) |
122 | 153 | } |
123 | 154 |
|
| 155 | +private predicate start_bb_likely_reachable(BasicBlock b) { |
| 156 | + exists(Scope s | s.getEntryNode() = b.getNode(_)) |
| 157 | + or |
| 158 | + exists(BasicBlock pred | |
| 159 | + pred = b.getAPredecessor() and |
| 160 | + end_bb_likely_reachable(pred) and |
| 161 | + not pred.getLastNode().(RaisingNode).unlikelySuccessor(b) |
| 162 | + ) |
| 163 | +} |
| 164 | + |
| 165 | +private predicate end_bb_likely_reachable(BasicBlock b) { |
| 166 | + start_bb_likely_reachable(b) and |
| 167 | + not exists(ControlFlowNode p, ControlFlowNode s | |
| 168 | + p.(RaisingNode).unlikelySuccessor(s) and |
| 169 | + p = b.getNode(_) and |
| 170 | + s = b.getNode(_) and |
| 171 | + not p = b.getLastNode() |
| 172 | + ) |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * An extension of `BasicBlock` that provides points-to related methods. |
| 177 | + */ |
| 178 | +class BasicBlockWithPointsTo extends BasicBlock { |
| 179 | + /** |
| 180 | + * Whether (as inferred by type inference) it is highly unlikely (or impossible) for control to flow from this to succ. |
| 181 | + */ |
| 182 | + predicate unlikelySuccessor(BasicBlockWithPointsTo succ) { |
| 183 | + this.getLastNode().(RaisingNode).unlikelySuccessor(succ.firstNode()) |
| 184 | + or |
| 185 | + not end_bb_likely_reachable(this) and succ = this.getASuccessor() |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Whether (as inferred by type inference) this basic block is likely to be reachable. |
| 190 | + */ |
| 191 | + predicate likelyReachable() { start_bb_likely_reachable(this) } |
| 192 | +} |
| 193 | + |
124 | 194 | /** |
125 | 195 | * An extension of `Expr` that provides points-to predicates. |
126 | 196 | */ |
@@ -208,3 +278,155 @@ class ModuleWithPointsTo extends Module { |
208 | 278 |
|
209 | 279 | override string getAQlClass() { none() } |
210 | 280 | } |
| 281 | + |
| 282 | +/** |
| 283 | + * An extension of `Function` that provides points-to related methods. |
| 284 | + */ |
| 285 | +class FunctionWithPointsTo extends Function { |
| 286 | + /** Gets the FunctionObject corresponding to this function */ |
| 287 | + FunctionObject getFunctionObject() { result.getOrigin() = this.getDefinition() } |
| 288 | + |
| 289 | + override string getAQlClass() { none() } |
| 290 | +} |
| 291 | + |
| 292 | +/** |
| 293 | + * An extension of `Class` that provides points-to related methods. |
| 294 | + */ |
| 295 | +class ClassWithPointsTo extends Class { |
| 296 | + /** Gets the ClassObject corresponding to this class */ |
| 297 | + ClassObject getClassObject() { result.getOrigin() = this.getParent() } |
| 298 | + |
| 299 | + override string getAQlClass() { none() } |
| 300 | +} |
| 301 | + |
| 302 | +/** Gets the `Object` corresponding to the immutable literal `l`. */ |
| 303 | +Object getLiteralObject(ImmutableLiteral l) { |
| 304 | + l instanceof IntegerLiteral and |
| 305 | + ( |
| 306 | + py_cobjecttypes(result, theIntType()) and py_cobjectnames(result, l.(Num).getN()) |
| 307 | + or |
| 308 | + py_cobjecttypes(result, theLongType()) and py_cobjectnames(result, l.(Num).getN()) |
| 309 | + ) |
| 310 | + or |
| 311 | + l instanceof FloatLiteral and |
| 312 | + py_cobjecttypes(result, theFloatType()) and |
| 313 | + py_cobjectnames(result, l.(Num).getN()) |
| 314 | + or |
| 315 | + l instanceof ImaginaryLiteral and |
| 316 | + py_cobjecttypes(result, theComplexType()) and |
| 317 | + py_cobjectnames(result, l.(Num).getN()) |
| 318 | + or |
| 319 | + l instanceof NegativeIntegerLiteral and |
| 320 | + ( |
| 321 | + (py_cobjecttypes(result, theIntType()) or py_cobjecttypes(result, theLongType())) and |
| 322 | + py_cobjectnames(result, "-" + l.(UnaryExpr).getOperand().(IntegerLiteral).getN()) |
| 323 | + ) |
| 324 | + or |
| 325 | + l instanceof Bytes and |
| 326 | + py_cobjecttypes(result, theBytesType()) and |
| 327 | + py_cobjectnames(result, l.(Bytes).quotedString()) |
| 328 | + or |
| 329 | + l instanceof Unicode and |
| 330 | + py_cobjecttypes(result, theUnicodeType()) and |
| 331 | + py_cobjectnames(result, l.(Unicode).quotedString()) |
| 332 | + or |
| 333 | + l instanceof True and |
| 334 | + name_consts(l, "True") and |
| 335 | + result = theTrueObject() |
| 336 | + or |
| 337 | + l instanceof False and |
| 338 | + name_consts(l, "False") and |
| 339 | + result = theFalseObject() |
| 340 | + or |
| 341 | + l instanceof None and |
| 342 | + name_consts(l, "None") and |
| 343 | + result = theNoneObject() |
| 344 | +} |
| 345 | + |
| 346 | +private predicate gettext_installed() { |
| 347 | + // Good enough (and fast) approximation |
| 348 | + exists(Module m | m.getName() = "gettext") |
| 349 | +} |
| 350 | + |
| 351 | +private predicate builtin_constant(string name) { |
| 352 | + exists(Object::builtin(name)) |
| 353 | + or |
| 354 | + name = "WindowsError" |
| 355 | + or |
| 356 | + name = "_" and gettext_installed() |
| 357 | +} |
| 358 | + |
| 359 | +/** Whether this name is (almost) always defined, ie. it is a builtin or VM defined name */ |
| 360 | +predicate globallyDefinedName(string name) { builtin_constant(name) or auto_name(name) } |
| 361 | + |
| 362 | +private predicate auto_name(string name) { |
| 363 | + name = "__file__" or name = "__builtins__" or name = "__name__" |
| 364 | +} |
| 365 | + |
| 366 | +/** An extension of `SsaVariable` that provides points-to related methods. */ |
| 367 | +class SsaVariableWithPointsTo extends SsaVariable { |
| 368 | + /** Gets an argument of the phi function defining this variable, pruned of unlikely edges. */ |
| 369 | + SsaVariable getAPrunedPhiInput() { |
| 370 | + result = this.getAPhiInput() and |
| 371 | + exists(BasicBlock incoming | incoming = this.getPredecessorBlockForPhiArgument(result) | |
| 372 | + not incoming.getLastNode().(RaisingNode).unlikelySuccessor(this.getDefinition()) |
| 373 | + ) |
| 374 | + } |
| 375 | + |
| 376 | + /** Gets the incoming edges for a Phi node, pruned of unlikely edges. */ |
| 377 | + private BasicBlockWithPointsTo getAPrunedPredecessorBlockForPhi() { |
| 378 | + result = this.getAPredecessorBlockForPhi() and |
| 379 | + not result.unlikelySuccessor(this.getDefinition().getBasicBlock()) |
| 380 | + } |
| 381 | + |
| 382 | + private predicate implicitlyDefined() { |
| 383 | + not exists(this.getDefinition()) and |
| 384 | + not py_ssa_phi(this, _) and |
| 385 | + exists(GlobalVariable var | this.getVariable() = var | |
| 386 | + globallyDefinedName(var.getId()) |
| 387 | + or |
| 388 | + var.getId() = "__path__" and var.getScope().(Module).isPackageInit() |
| 389 | + ) |
| 390 | + } |
| 391 | + |
| 392 | + /** Whether this variable may be undefined */ |
| 393 | + predicate maybeUndefined() { |
| 394 | + not exists(this.getDefinition()) and not py_ssa_phi(this, _) and not this.implicitlyDefined() |
| 395 | + or |
| 396 | + this.getDefinition().isDelete() |
| 397 | + or |
| 398 | + exists(SsaVariableWithPointsTo var | var = this.getAPrunedPhiInput() | var.maybeUndefined()) |
| 399 | + or |
| 400 | + /* |
| 401 | + * For phi-nodes, there must be a corresponding phi-input for each control-flow |
| 402 | + * predecessor. Otherwise, the variable will be undefined on that incoming edge. |
| 403 | + * WARNING: the same phi-input may cover multiple predecessors, so this check |
| 404 | + * cannot be done by counting. |
| 405 | + */ |
| 406 | + |
| 407 | + exists(BasicBlock incoming | |
| 408 | + reaches_end(incoming) and |
| 409 | + incoming = this.getAPrunedPredecessorBlockForPhi() and |
| 410 | + not this.getAPhiInput().getDefinition().getBasicBlock().dominates(incoming) |
| 411 | + ) |
| 412 | + } |
| 413 | + |
| 414 | + override string getAQlClass() { none() } |
| 415 | +} |
| 416 | + |
| 417 | +private predicate reaches_end(BasicBlock b) { |
| 418 | + not exits_early(b) and |
| 419 | + ( |
| 420 | + /* Entry point */ |
| 421 | + not exists(BasicBlock prev | prev.getASuccessor() = b) |
| 422 | + or |
| 423 | + exists(BasicBlock prev | prev.getASuccessor() = b | reaches_end(prev)) |
| 424 | + ) |
| 425 | +} |
| 426 | + |
| 427 | +private predicate exits_early(BasicBlock b) { |
| 428 | + exists(FunctionObject f | |
| 429 | + f.neverReturns() and |
| 430 | + f.getACall().getBasicBlock() = b |
| 431 | + ) |
| 432 | +} |
0 commit comments