Skip to content

Commit af08f85

Browse files
authored
Merge pull request #1389 from markshannon/python-ipa-objects-fix-performance
Python: New points-to and object model with performance fixes
2 parents 80ff63a + fc2ac89 commit af08f85

File tree

158 files changed

+8076
-3968
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+8076
-3968
lines changed

python/ql/src/Exceptions/UnguardedNextInGenerator.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ FunctionObject iter() {
1616
result = Object::builtin("iter")
1717
}
1818

19-
FunctionObject next() {
19+
BuiltinFunctionObject next() {
2020
result = Object::builtin("next")
2121
}
2222

python/ql/src/Expressions/IsComparisons.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ predicate invalid_portable_is_comparison(Compare comp, Cmpop op, ClassObject cls
104104
/* OK to use 'is' when comparing items from a known set of objects */
105105
not exists(Expr left, Expr right, Object obj |
106106
comp.compares(left, op, right) and
107-
left.refersTo(obj) and right.refersTo(obj) and
108-
exists(ImmutableLiteral il | il.getLiteralObject() = obj)
107+
exists(ImmutableLiteral il | il.getLiteralObject() = obj) |
108+
left.refersTo(obj) and right.refersTo(obj)
109+
or
110+
/* Simple constant in module, probably some sort of sentinel */
111+
exists(AstNode origin |
112+
not left.refersTo(_) and right.refersTo(obj, origin) and
113+
origin.getScope().getEnclosingModule() = comp.getScope().getEnclosingModule()
114+
)
109115
)
110116
and
111117
/* OK to use 'is' when comparing with a member of an enum */

python/ql/src/Security/CWE-798/HardcodedCredentials.ql

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ predicate capitalized_word(StrConst str) {
5353
str.getText().regexpMatch("[A-Z][a-z]+")
5454
}
5555

56+
predicate format_string(StrConst str) {
57+
str.getText().matches("%{%}%")
58+
}
59+
5660
predicate maybeCredential(ControlFlowNode f) {
5761
/* A string that is not too short and unlikely to be text or an identifier. */
5862
exists(StrConst str |
@@ -66,20 +70,21 @@ predicate maybeCredential(ControlFlowNode f) {
6670
/* Not too repetitive */
6771
exists(int chars |
6872
chars = char_count(str) |
69-
chars > 20 or
70-
chars > str.getText().length()/2
73+
chars > 15 or
74+
chars*3 > str.getText().length()*2
7175
) and
7276
not possible_reflective_name(str.getText()) and
73-
not capitalized_word(str)
77+
not capitalized_word(str) and
78+
not format_string(str)
7479
)
7580
or
76-
/* Or, an integer with at least 8 digits */
81+
/* Or, an integer with over 32 bits */
7782
exists(IntegerLiteral lit |
7883
f.getNode() = lit
7984
|
80-
not exists(lit.getValue())
81-
or
82-
lit.getValue() > 10000000
85+
not exists(lit.getValue()) and
86+
/* Not a set of flags or round number */
87+
not lit.getN().matches("%00%")
8388
)
8489
}
8590

python/ql/src/analysis/CallGraphEfficiency.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import semmle.python.pointsto.PointsToContext
99

1010
from int total_facts, int total_size, int depth, float efficiency
1111
where
12-
total_facts = strictcount(ControlFlowNode call, FunctionObject func |
12+
total_facts = strictcount(ControlFlowNode call, CallableValue func |
1313
exists(PointsToContext ctx |
14-
call = PointsTo::get_a_call(func, ctx) and
14+
call = func.getACall(ctx) and
1515
depth = ctx.getDepth()
1616
)
1717
)
1818
and
19-
total_size = strictcount(ControlFlowNode call, FunctionObject func, PointsToContext ctx |
20-
call = PointsTo::get_a_call(func, ctx) and
19+
total_size = strictcount(ControlFlowNode call, CallableValue func, PointsToContext ctx |
20+
call = func.getACall(ctx) and
2121
depth = ctx.getDepth()
2222
)
2323
and

python/ql/src/analysis/CallGraphMarginalEfficiency.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import semmle.python.pointsto.PointsToContext
88

99
from int total_facts, int total_size, int depth, float efficiency
1010
where
11-
total_facts = strictcount(ControlFlowNode call, FunctionObject func |
11+
total_facts = strictcount(ControlFlowNode call, CallableValue func |
1212
exists(PointsToContext ctx |
13-
call = PointsTo::get_a_call(func, ctx) and
13+
call = func.getACall(ctx) and
1414
depth = ctx.getDepth()
1515
and not
1616
exists(PointsToContext shallower |
17-
call = PointsTo::get_a_call(func, shallower) and
17+
call = func.getACall(shallower) and
1818
shallower.getDepth() < depth
1919
)
2020
)
2121
)
2222
and
23-
total_size = strictcount(ControlFlowNode call, FunctionObject func, PointsToContext ctx |
24-
call = PointsTo::get_a_call(func, ctx) and
23+
total_size = strictcount(ControlFlowNode call, CallableValue func, PointsToContext ctx |
24+
call = func.getACall(ctx) and
2525
depth = ctx.getDepth()
2626
)
2727
and

python/ql/src/analysis/ContextMarginalEfficiency.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ total_size = strictcount(ControlFlowNode f, Object value, ClassObject cls, Point
2929
)
3030
and
3131
efficiency = 100.0 * total_facts / total_size
32-
select depth, total_facts, total_size, efficiency
32+
select depth, total_facts, total_size, efficiency

python/ql/src/analysis/FailedInference.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import python
33
import semmle.python.pointsto.PointsTo
44

5-
from ClassObject cls, string reason
5+
from ClassValue cls, string reason
66

77
where
8-
PointsTo::Types::failed_inference(cls, reason)
8+
Types::failedInference(cls, reason)
99

1010
select cls, reason
1111

python/ql/src/analysis/KeyPointsToFailure.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
*/
99

1010
import python
11+
import semmle.python.pointsto.PointsTo
1112

1213
predicate points_to_failure(Expr e) {
13-
exists(ControlFlowNode f |
14+
exists(ControlFlowNode f |
1415
f = e.getAFlowNode() |
15-
not f.refersTo(_)
16+
not PointsTo::pointsTo(f, _, _, _)
1617
)
1718
}
1819

python/ql/src/analysis/Pruned.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from int size
66

77
where
88
size = count(ControlFlowNode f |
9-
not PointsTo::Test::reachableBlock(f.getBasicBlock(), _)
9+
not PointsToInternal::reachableBlock(f.getBasicBlock(), _)
1010
)
1111

1212

python/ql/src/python.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ import semmle.dataflow.SSA
3636
import semmle.python.pointsto.Base
3737
import semmle.python.pointsto.Context
3838
import semmle.python.pointsto.CallGraph
39+
import semmle.python.objects.ObjectAPI
3940

4041
import site

0 commit comments

Comments
 (0)