Skip to content

Commit f7243ee

Browse files
committed
Python: Remove points-to dependency from parts of SSA
For whatever reason, the CFG node for exceptions and exception groups was placed with the points-to code. (Probably because a lot of the predicates depended on points-to.) However, as it turned out, two of the SSA modules only depended on non-points-to properties of these nodes, and so it was fairly straightforward to remove the imports of `LegacyPointsTo` for those modules. In the process, I moved the aforementioned CFG node types into `Flow.qll`, and changed the classes in the `Exceptions` module to the `...WithPointsTo` form that we introduced elsewhere.
1 parent a4720d2 commit f7243ee

File tree

9 files changed

+70
-70
lines changed

9 files changed

+70
-70
lines changed

python/ql/lib/semmle/python/Flow.qll

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,58 @@ class StarredNode extends ControlFlowNode {
883883
ControlFlowNode getValue() { toAst(result) = toAst(this).(Starred).getValue() }
884884
}
885885

886+
/** The ControlFlowNode for an 'except' statement. */
887+
class ExceptFlowNode extends ControlFlowNode {
888+
ExceptFlowNode() { this.getNode() instanceof ExceptStmt }
889+
890+
/**
891+
* Gets the type handled by this exception handler.
892+
* `ExceptionType` in `except ExceptionType as e:`
893+
*/
894+
ControlFlowNode getType() {
895+
exists(ExceptStmt ex |
896+
this.getBasicBlock().dominates(result.getBasicBlock()) and
897+
ex = this.getNode() and
898+
result = ex.getType().getAFlowNode()
899+
)
900+
}
901+
902+
/**
903+
* Gets the name assigned to the handled exception, if any.
904+
* `e` in `except ExceptionType as e:`
905+
*/
906+
ControlFlowNode getName() {
907+
exists(ExceptStmt ex |
908+
this.getBasicBlock().dominates(result.getBasicBlock()) and
909+
ex = this.getNode() and
910+
result = ex.getName().getAFlowNode()
911+
)
912+
}
913+
}
914+
915+
/** The ControlFlowNode for an 'except*' statement. */
916+
class ExceptGroupFlowNode extends ControlFlowNode {
917+
ExceptGroupFlowNode() { this.getNode() instanceof ExceptGroupStmt }
918+
919+
/**
920+
* Gets the type handled by this exception handler.
921+
* `ExceptionType` in `except* ExceptionType as e:`
922+
*/
923+
ControlFlowNode getType() {
924+
this.getBasicBlock().dominates(result.getBasicBlock()) and
925+
result = this.getNode().(ExceptGroupStmt).getType().getAFlowNode()
926+
}
927+
928+
/**
929+
* Gets the name assigned to the handled exception, if any.
930+
* `e` in `except* ExceptionType as e:`
931+
*/
932+
ControlFlowNode getName() {
933+
this.getBasicBlock().dominates(result.getBasicBlock()) and
934+
result = this.getNode().(ExceptGroupStmt).getName().getAFlowNode()
935+
}
936+
}
937+
886938
private module Scopes {
887939
private predicate fast_local(NameNode n) {
888940
exists(FastLocalVariable v |

python/ql/lib/semmle/python/essa/Essa.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import python
66
private import SsaCompute
77
import semmle.python.essa.Definitions
88
private import semmle.python.internal.CachedStages
9-
private import LegacyPointsTo
109
private import semmle.python.essa.SsaDefinitions
1110

1211
/** An (enhanced) SSA variable derived from `SsaSourceVariable`. */

python/ql/lib/semmle/python/essa/SsaDefinitions.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import python
77
private import semmle.python.internal.CachedStages
8-
private import LegacyPointsTo
98

109
/** Hold if `expr` is a test (a branch) and `use` is within that test */
1110
predicate test_contains(ControlFlowNode expr, ControlFlowNode use) {

python/ql/lib/semmle/python/types/Exceptions.qll

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class RaisingNode extends ControlFlowNode {
8080
or
8181
this.getNode() instanceof Print and result = theIOErrorType()
8282
or
83-
exists(ExceptFlowNode except |
83+
exists(ExceptFlowNodeWithPointsTo except |
8484
except = this.getAnExceptionalSuccessor() and
8585
except.handles_objectapi(result) and
8686
result = this.innateException_objectapi()
@@ -107,7 +107,7 @@ class RaisingNode extends ControlFlowNode {
107107
or
108108
this.getNode() instanceof Print and result = ClassValue::ioError()
109109
or
110-
exists(ExceptFlowNode except |
110+
exists(ExceptFlowNodeWithPointsTo except |
111111
except = this.getAnExceptionalSuccessor() and
112112
except.handles(result) and
113113
result = this.innateException()
@@ -200,8 +200,8 @@ class RaisingNode extends ControlFlowNode {
200200
succ = this.getAnExceptionalSuccessor() and
201201
(
202202
/* An 'except' that handles raised and there is no more previous handler */
203-
succ.(ExceptFlowNode).handles_objectapi(raised) and
204-
not exists(ExceptFlowNode other, StmtList s, int i, int j |
203+
succ.(ExceptFlowNodeWithPointsTo).handles_objectapi(raised) and
204+
not exists(ExceptFlowNodeWithPointsTo other, StmtList s, int i, int j |
205205
not other = succ and
206206
other.handles_objectapi(raised) and
207207
s.getItem(i) = succ.getNode() and
@@ -211,7 +211,7 @@ class RaisingNode extends ControlFlowNode {
211211
)
212212
or
213213
/* Any successor that is not an 'except', provided that 'raised' is not handled by a different successor. */
214-
not this.getAnExceptionalSuccessor().(ExceptFlowNode).handles_objectapi(raised) and
214+
not this.getAnExceptionalSuccessor().(ExceptFlowNodeWithPointsTo).handles_objectapi(raised) and
215215
not succ instanceof ExceptFlowNode
216216
)
217217
}
@@ -223,8 +223,8 @@ class RaisingNode extends ControlFlowNode {
223223
succ = this.getAnExceptionalSuccessor() and
224224
(
225225
/* An 'except' that handles raised and there is no more previous handler */
226-
succ.(ExceptFlowNode).handles(raised) and
227-
not exists(ExceptFlowNode other, StmtList s, int i, int j |
226+
succ.(ExceptFlowNodeWithPointsTo).handles(raised) and
227+
not exists(ExceptFlowNodeWithPointsTo other, StmtList s, int i, int j |
228228
not other = succ and
229229
other.handles(raised) and
230230
s.getItem(i) = succ.getNode() and
@@ -234,7 +234,7 @@ class RaisingNode extends ControlFlowNode {
234234
)
235235
or
236236
/* Any successor that is not an 'except', provided that 'raised' is not handled by a different successor. */
237-
not this.getAnExceptionalSuccessor().(ExceptFlowNode).handles(raised) and
237+
not this.getAnExceptionalSuccessor().(ExceptFlowNodeWithPointsTo).handles(raised) and
238238
not succ instanceof ExceptFlowNode
239239
)
240240
}
@@ -248,7 +248,7 @@ class RaisingNode extends ControlFlowNode {
248248
raised.isLegalExceptionType() and
249249
raised = this.getARaisedType_objectapi() and
250250
this.isExceptionalExit(s) and
251-
not this.getAnExceptionalSuccessor().(ExceptFlowNode).handles_objectapi(raised)
251+
not this.getAnExceptionalSuccessor().(ExceptFlowNodeWithPointsTo).handles_objectapi(raised)
252252
}
253253

254254
/**
@@ -260,7 +260,7 @@ class RaisingNode extends ControlFlowNode {
260260
raised.isLegalExceptionType() and
261261
raised = this.getARaisedType() and
262262
this.isExceptionalExit(s) and
263-
not this.getAnExceptionalSuccessor().(ExceptFlowNode).handles(raised)
263+
not this.getAnExceptionalSuccessor().(ExceptFlowNodeWithPointsTo).handles(raised)
264264
}
265265
}
266266

@@ -368,36 +368,9 @@ predicate scope_raises_unknown(Scope s) {
368368
)
369369
}
370370

371-
/** The ControlFlowNode for an 'except' statement. */
372-
class ExceptFlowNode extends ControlFlowNode {
373-
ExceptFlowNode() { this.getNode() instanceof ExceptStmt }
374-
375-
/**
376-
* Gets the type handled by this exception handler.
377-
* `ExceptionType` in `except ExceptionType as e:`
378-
*/
379-
ControlFlowNodeWithPointsTo getType() {
380-
exists(ExceptStmt ex |
381-
this.getBasicBlock().dominates(result.getBasicBlock()) and
382-
ex = this.getNode() and
383-
result = ex.getType().getAFlowNode()
384-
)
385-
}
386-
387-
/**
388-
* Gets the name assigned to the handled exception, if any.
389-
* `e` in `except ExceptionType as e:`
390-
*/
391-
ControlFlowNode getName() {
392-
exists(ExceptStmt ex |
393-
this.getBasicBlock().dominates(result.getBasicBlock()) and
394-
ex = this.getNode() and
395-
result = ex.getName().getAFlowNode()
396-
)
397-
}
398-
371+
class ExceptFlowNodeWithPointsTo extends ControlFlowNodeWithPointsTo {
399372
private predicate handledObject_objectapi(Object obj, ClassObject cls, ControlFlowNode origin) {
400-
this.getType().refersTo(obj, cls, origin)
373+
this.(ExceptFlowNode).getType().(ControlFlowNodeWithPointsTo).refersTo(obj, cls, origin)
401374
or
402375
exists(Object tup | this.handledObject_objectapi(tup, theTupleType(), _) |
403376
element_from_tuple_objectapi(tup).refersTo(obj, cls, origin)
@@ -407,7 +380,7 @@ class ExceptFlowNode extends ControlFlowNode {
407380
private predicate handledObject(Value val, ClassValue cls, ControlFlowNode origin) {
408381
val.getClass() = cls and
409382
(
410-
this.getType().pointsTo(val, origin)
383+
this.(ExceptFlowNode).getType().(ControlFlowNodeWithPointsTo).pointsTo(val, origin)
411384
or
412385
exists(TupleValue tup | this.handledObject(tup, ClassValue::tuple(), _) |
413386
val = tup.getItem(_) and origin = val.getOrigin()
@@ -452,29 +425,6 @@ class ExceptFlowNode extends ControlFlowNode {
452425
}
453426
}
454427

455-
/** The ControlFlowNode for an 'except*' statement. */
456-
class ExceptGroupFlowNode extends ControlFlowNode {
457-
ExceptGroupFlowNode() { this.getNode() instanceof ExceptGroupStmt }
458-
459-
/**
460-
* Gets the type handled by this exception handler.
461-
* `ExceptionType` in `except* ExceptionType as e:`
462-
*/
463-
ControlFlowNode getType() {
464-
this.getBasicBlock().dominates(result.getBasicBlock()) and
465-
result = this.getNode().(ExceptGroupStmt).getType().getAFlowNode()
466-
}
467-
468-
/**
469-
* Gets the name assigned to the handled exception, if any.
470-
* `e` in `except* ExceptionType as e:`
471-
*/
472-
ControlFlowNode getName() {
473-
this.getBasicBlock().dominates(result.getBasicBlock()) and
474-
result = this.getNode().(ExceptGroupStmt).getName().getAFlowNode()
475-
}
476-
}
477-
478428
private ControlFlowNodeWithPointsTo element_from_tuple_objectapi(Object tuple) {
479429
exists(Tuple t | t = tuple.getOrigin() and result = t.getAnElt().getAFlowNode())
480430
}

python/ql/src/Exceptions/IllegalExceptionHandlerType.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import python
1515
private import LegacyPointsTo
1616

17-
from ExceptFlowNode ex, Value t, ClassValue c, ControlFlowNode origin, string what
17+
from ExceptFlowNodeWithPointsTo ex, Value t, ClassValue c, ControlFlowNode origin, string what
1818
where
1919
ex.handledException(t, c, origin) and
2020
(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import python
22
private import LegacyPointsTo
33

4-
from ExceptFlowNode ex, Value val
4+
from ExceptFlowNodeWithPointsTo ex, Value val
55
where ex.handledException(val, _, _)
66
select ex.getLocation().getStartLine(), ex.toString(), val.toString()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import python
22
private import LegacyPointsTo
33

4-
from ExceptFlowNode ex, Value val
4+
from ExceptFlowNodeWithPointsTo ex, Value val
55
where ex.handledException(val, _, _)
66
select ex.getLocation().getStartLine(), ex.toString(), val.toString()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import python
22
private import LegacyPointsTo
33

4-
from ExceptFlowNode ex, Value val
4+
from ExceptFlowNodeWithPointsTo ex, Value val
55
where ex.handledException(val, _, _)
66
select ex.getLocation().getStartLine(), ex.toString(), val.toString()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import python
22
private import LegacyPointsTo
33

4-
from ExceptFlowNode n, ClassObject cls
4+
from ExceptFlowNodeWithPointsTo n, ClassObject cls
55
where n.handles_objectapi(cls)
66
select n.getLocation().getStartLine(), cls.toString()

0 commit comments

Comments
 (0)