Skip to content

Commit 36c7a84

Browse files
authored
Merge pull request #1112 from markshannon/python-forward-compatible-points-to-extensions
Python: Allow points-to extensions to specify just the object.
2 parents fb499b0 + 7213b72 commit 36c7a84

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

python/ql/src/semmle/python/pointsto/Base.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ ClassObject simple_types(Object obj) {
5050
obj.getOrigin() instanceof Module and result = theModuleType()
5151
or
5252
result = builtin_object_type(obj)
53+
or
54+
obj = unknownValue() and result = theUnknownType()
5355
}
5456

5557
private ClassObject comprehension(Expr e) {

python/ql/src/semmle/python/types/Extensions.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*
44
* This should be considered an advance feature. Modifying the points-to analysis
55
* can cause queries to give strange and misleading results, if not done with care.
6+
*
7+
* WARNING:
8+
* This module interacts with the internals of points-to analysis and
9+
* the classes here are more likely to change than the rest of the library.
610
*/
711

812
import python
@@ -34,6 +38,18 @@ abstract class CustomPointsToOriginFact extends CustomPointsToFact {
3438

3539
}
3640

41+
/* Custom points-to fact with inferred class */
42+
abstract class CustomPointsToObjectFact extends CustomPointsToFact {
43+
44+
abstract predicate pointsTo(Object value);
45+
46+
override predicate pointsTo(Context context, Object value, ClassObject cls, ControlFlowNode origin) {
47+
this.pointsTo(value) and cls = simple_types(value) and origin = this and context.appliesTo(this)
48+
}
49+
50+
}
51+
52+
3753
/** INTERNAL -- Do not use */
3854
abstract class CustomPointsToAttribute extends Object {
3955

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
| test.py:4:1:4:3 | ControlFlowNode for one | int 1 |
22
| test.py:5:1:5:3 | ControlFlowNode for two | int 2 |
33
| test.py:8:1:8:1 | ControlFlowNode for IntegerLiteral | int 1 |
4-
| test.py:8:1:8:7 | ControlFlowNode for Tuple | Tuple |
4+
| test.py:8:1:8:11 | ControlFlowNode for Tuple | Tuple |
55
| test.py:8:3:8:3 | ControlFlowNode for IntegerLiteral | int 2 |
66
| test.py:8:5:8:5 | ControlFlowNode for IntegerLiteral | int 3 |
77
| test.py:8:7:8:7 | ControlFlowNode for IntegerLiteral | int 4 |
8+
| test.py:8:9:8:9 | ControlFlowNode for IntegerLiteral | int 5 |
9+
| test.py:8:11:8:11 | ControlFlowNode for IntegerLiteral | int 6 |
810
| test.py:10:1:10:2 | ControlFlowNode for a3 | int 3 |
911
| test.py:10:6:10:7 | ControlFlowNode for Tuple | Tuple |
1012
| test.py:10:6:10:13 | ControlFlowNode for Attribute | int 3 |
@@ -13,3 +15,5 @@
1315
| test.py:11:6:11:15 | ControlFlowNode for Attribute | int 4 |
1416
| test.py:13:1:13:2 | ControlFlowNode for a3 | int 3 |
1517
| test.py:14:1:14:2 | ControlFlowNode for a4 | int 4 |
18+
| test.py:16:1:16:4 | ControlFlowNode for five | int 5 |
19+
| test.py:17:1:17:3 | ControlFlowNode for six | int 6 |

python/ql/test/library-tests/PointsTo/extensions/Extend.ql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ class AttributeExtension extends CustomPointsToAttribute {
3838

3939
}
4040

41+
class NoClassExtension extends CustomPointsToObjectFact {
42+
43+
NoClassExtension() { this = this }
44+
45+
override predicate pointsTo(Object value) {
46+
this.(NameNode).getId() = "five" and value.(NumericObject).intValue() = 5
47+
or
48+
this.(NameNode).getId() = "six" and value.(NumericObject).intValue() = 6
49+
}
50+
51+
}
52+
53+
4154
from ControlFlowNode f, Object o
4255
where f.getLocation().getFile().getBaseName() = "test.py" and f.refersTo(o)
4356
select f, o.toString()

python/ql/test/library-tests/PointsTo/extensions/test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
two
66

77
#Make sure values exist in DB
8-
1,2,3,4
8+
1,2,3,4,5,6
99

1010
a3 = ().three
1111
a4 = False.four
1212

1313
a3
1414
a4
15+
16+
five
17+
six

0 commit comments

Comments
 (0)