Python: More easy-to-get content flow

There are some things that should be rewritten, though,
but it may involve the extractor
This commit is contained in:
Rasmus Lerchedahl Petersen
2020-08-04 13:54:50 +02:00
parent 9d09b4c811
commit 9312b42e79
5 changed files with 85 additions and 9 deletions

View File

@@ -234,8 +234,8 @@ predicate storeStep(Node nodeFrom, Content c, Node nodeTo) {
or
// Comprehension
// `[x+1 for x in l]`
// nodeFrom is `x+1`
// nodeTo is `[x+1 for x in l]`
// nodeFrom is `x+1`, cfg node
// nodeTo is `[x+1 for x in l]`, cfg node
nodeTo.(CfgNode).getNode().getNode().(Comp).getElt() = nodeFrom.(CfgNode).getNode().getNode()
}
@@ -245,9 +245,42 @@ predicate storeStep(Node nodeFrom, Content c, Node nodeTo) {
predicate readStep(Node nodeFrom, Content c, Node nodeTo) {
// Subscription
// `l[3]`
// nodeFrom is `l`
// nodeTo is `l[3]`
// nodeFrom is `l`, cfg node
// nodeTo is `l[3]`, cfg node
nodeFrom.(CfgNode).getNode() = nodeTo.(CfgNode).getNode().(SubscriptNode).getObject()
or
// set.pop
// `s.pop()`
// nodeFrom is `s`, cfg node
// nodeTo is `s.pop()`, cfg node
exists(CallNode call, AttrNode a |
call.getFunction() = a and
a.getName() = "pop" and // TODO: Should be made more robust, like Value::named("set.pop").getACall()
nodeFrom.(CfgNode).getNode() = a.getObject() and
nodeTo.(CfgNode).getNode() = call
)
or
// Comprehension
// `[x+1 for x in l]`
// nodeFrom is `l`, cfg node
// nodeTo is `x`, essa var
exists(For f, Comp comp |
// Seems to need extractor changes to write this part properly
nodeFrom.(CfgNode).getNode().(SequenceNode).getNode().getParentNode() = comp and
colocated(f.getIter(), comp) and
nodeTo.(EssaNode).getVar().getDefinition().(AssignmentDefinition).getDefiningNode().getNode() = f.getTarget()
)
}
/** This should not be necessary */
predicate colocated(AstNode n1, AstNode n2) {
n1.getLocation().getFile() = n2.getLocation().getFile() and
n1.getLocation().getStartLine() = n2.getLocation().getStartLine() and
n1.getLocation().getEndLine() = n2.getLocation().getEndLine() and
n1.getLocation().getStartColumn() = n2.getLocation().getStartColumn() and
n1.getLocation().getEndColumn() = n2.getLocation().getEndColumn()
}
/**

View File

@@ -33,8 +33,15 @@ class Node extends TNode {
/** Gets the scope of this node. */
Scope getScope() { none() }
private DataFlowCallable getCallableScope(Scope s) {
result.getScope() = s
or
not exists(DataFlowCallable c | c.getScope() = s) and
result = getCallableScope(s.getEnclosingScope())
}
/** Gets the enclosing callable of this node. */
DataFlowCallable getEnclosingCallable() { result.getScope() = this.getScope() }
DataFlowCallable getEnclosingCallable() { result = getCallableScope(this.getScope()) }
/** Gets the location of this node */
Location getLocation() { none() }

View File

@@ -88,7 +88,7 @@ uniquePostUpdate
postIsInSameCallable
reverseRead
storeIsPostUpdate
| test.py:152:5:152:5 | SSA variable l | Store targets should be PostUpdateNodes. |
| test.py:152:9:152:16 | ControlFlowNode for List | Store targets should be PostUpdateNodes. |
argHasPostUpdate
| test.py:25:10:25:10 | ControlFlowNode for t | ArgumentNode is missing PostUpdateNode. |
| test.py:29:10:29:10 | ControlFlowNode for t | ArgumentNode is missing PostUpdateNode. |

View File

@@ -19,6 +19,21 @@ edges
| test.py:74:9:74:37 | ControlFlowNode for ListComp [Content] | test.py:75:10:75:10 | ControlFlowNode for x [Content] |
| test.py:74:10:74:15 | ControlFlowNode for SOURCE | test.py:74:9:74:37 | ControlFlowNode for ListComp [Content] |
| test.py:75:10:75:10 | ControlFlowNode for x [Content] | test.py:75:10:75:13 | ControlFlowNode for Subscript |
| test.py:78:9:78:29 | ControlFlowNode for ListComp [Content] | test.py:79:10:79:10 | ControlFlowNode for x [Content] |
| test.py:78:10:78:10 | ControlFlowNode for y | test.py:78:9:78:29 | ControlFlowNode for ListComp [Content] |
| test.py:78:16:78:16 | SSA variable y | test.py:78:10:78:10 | ControlFlowNode for y |
| test.py:78:21:78:28 | ControlFlowNode for List [Content] | test.py:78:16:78:16 | SSA variable y |
| test.py:78:22:78:27 | ControlFlowNode for SOURCE | test.py:78:21:78:28 | ControlFlowNode for List [Content] |
| test.py:79:10:79:10 | ControlFlowNode for x [Content] | test.py:79:10:79:13 | ControlFlowNode for Subscript |
| test.py:91:9:91:37 | ControlFlowNode for SetComp [Content] | test.py:92:10:92:10 | ControlFlowNode for x [Content] |
| test.py:91:10:91:15 | ControlFlowNode for SOURCE | test.py:91:9:91:37 | ControlFlowNode for SetComp [Content] |
| test.py:92:10:92:10 | ControlFlowNode for x [Content] | test.py:92:10:92:16 | ControlFlowNode for Attribute() |
| test.py:95:9:95:29 | ControlFlowNode for SetComp [Content] | test.py:96:10:96:10 | ControlFlowNode for x [Content] |
| test.py:95:10:95:10 | ControlFlowNode for y | test.py:95:9:95:29 | ControlFlowNode for SetComp [Content] |
| test.py:95:16:95:16 | SSA variable y | test.py:95:10:95:10 | ControlFlowNode for y |
| test.py:95:21:95:28 | ControlFlowNode for List [Content] | test.py:95:16:95:16 | SSA variable y |
| test.py:95:22:95:27 | ControlFlowNode for SOURCE | test.py:95:21:95:28 | ControlFlowNode for List [Content] |
| test.py:96:10:96:10 | ControlFlowNode for x [Content] | test.py:96:10:96:16 | ControlFlowNode for Attribute() |
| test.py:222:11:222:16 | ControlFlowNode for SOURCE | test.py:222:11:222:17 | ControlFlowNode for Tuple [Content] |
| test.py:222:11:222:17 | ControlFlowNode for Tuple [Content] | test.py:222:10:222:21 | ControlFlowNode for Subscript |
| test.py:225:10:225:17 | ControlFlowNode for List [Content] | test.py:225:10:225:20 | ControlFlowNode for Subscript |
@@ -57,6 +72,24 @@ nodes
| test.py:74:10:74:15 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
| test.py:75:10:75:10 | ControlFlowNode for x [Content] | semmle.label | ControlFlowNode for x [Content] |
| test.py:75:10:75:13 | ControlFlowNode for Subscript | semmle.label | ControlFlowNode for Subscript |
| test.py:78:9:78:29 | ControlFlowNode for ListComp [Content] | semmle.label | ControlFlowNode for ListComp [Content] |
| test.py:78:10:78:10 | ControlFlowNode for y | semmle.label | ControlFlowNode for y |
| test.py:78:16:78:16 | SSA variable y | semmle.label | SSA variable y |
| test.py:78:21:78:28 | ControlFlowNode for List [Content] | semmle.label | ControlFlowNode for List [Content] |
| test.py:78:22:78:27 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
| test.py:79:10:79:10 | ControlFlowNode for x [Content] | semmle.label | ControlFlowNode for x [Content] |
| test.py:79:10:79:13 | ControlFlowNode for Subscript | semmle.label | ControlFlowNode for Subscript |
| test.py:91:9:91:37 | ControlFlowNode for SetComp [Content] | semmle.label | ControlFlowNode for SetComp [Content] |
| test.py:91:10:91:15 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
| test.py:92:10:92:10 | ControlFlowNode for x [Content] | semmle.label | ControlFlowNode for x [Content] |
| test.py:92:10:92:16 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
| test.py:95:9:95:29 | ControlFlowNode for SetComp [Content] | semmle.label | ControlFlowNode for SetComp [Content] |
| test.py:95:10:95:10 | ControlFlowNode for y | semmle.label | ControlFlowNode for y |
| test.py:95:16:95:16 | SSA variable y | semmle.label | SSA variable y |
| test.py:95:21:95:28 | ControlFlowNode for List [Content] | semmle.label | ControlFlowNode for List [Content] |
| test.py:95:22:95:27 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
| test.py:96:10:96:10 | ControlFlowNode for x [Content] | semmle.label | ControlFlowNode for x [Content] |
| test.py:96:10:96:16 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
| test.py:222:10:222:21 | ControlFlowNode for Subscript | semmle.label | ControlFlowNode for Subscript |
| test.py:222:11:222:16 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
| test.py:222:11:222:17 | ControlFlowNode for Tuple [Content] | semmle.label | ControlFlowNode for Tuple [Content] |
@@ -80,6 +113,9 @@ nodes
| test.py:62:10:62:10 | ControlFlowNode for x | test.py:61:10:61:15 | ControlFlowNode for SOURCE | test.py:62:10:62:10 | ControlFlowNode for x | <message> |
| test.py:67:10:67:13 | ControlFlowNode for Subscript | test.py:66:10:66:15 | ControlFlowNode for SOURCE | test.py:67:10:67:13 | ControlFlowNode for Subscript | <message> |
| test.py:75:10:75:13 | ControlFlowNode for Subscript | test.py:74:10:74:15 | ControlFlowNode for SOURCE | test.py:75:10:75:13 | ControlFlowNode for Subscript | <message> |
| test.py:79:10:79:13 | ControlFlowNode for Subscript | test.py:78:22:78:27 | ControlFlowNode for SOURCE | test.py:79:10:79:13 | ControlFlowNode for Subscript | <message> |
| test.py:92:10:92:16 | ControlFlowNode for Attribute() | test.py:91:10:91:15 | ControlFlowNode for SOURCE | test.py:92:10:92:16 | ControlFlowNode for Attribute() | <message> |
| test.py:96:10:96:16 | ControlFlowNode for Attribute() | test.py:95:22:95:27 | ControlFlowNode for SOURCE | test.py:96:10:96:16 | ControlFlowNode for Attribute() | <message> |
| test.py:222:10:222:21 | ControlFlowNode for Subscript | test.py:222:11:222:16 | ControlFlowNode for SOURCE | test.py:222:10:222:21 | ControlFlowNode for Subscript | <message> |
| test.py:225:10:225:20 | ControlFlowNode for Subscript | test.py:225:11:225:16 | ControlFlowNode for SOURCE | test.py:225:10:225:20 | ControlFlowNode for Subscript | <message> |
| test.py:246:10:246:34 | ControlFlowNode for second() | test.py:246:28:246:33 | ControlFlowNode for SOURCE | test.py:246:10:246:34 | ControlFlowNode for second() | <message> |

View File

@@ -76,7 +76,7 @@ def test_list_comprehension():
def test_list_comprehension_flow():
x = [y for y in [SOURCE]]
SINK(x[0]) # Flow missing
SINK(x[0])
def test_nested_list_display():
x = [* [SOURCE]]
@@ -89,11 +89,11 @@ def test_set_display():
def test_set_comprehension():
x = {SOURCE for y in [NONSOURCE]}
SINK(x.pop()) # Flow missing
SINK(x.pop())
def test_set_comprehension_flow():
x = {y for y in [SOURCE]}
SINK(x.pop()) # Flow missing
SINK(x.pop())
def test_nested_set_display():
x = {* {SOURCE}}