Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5fbbde

Browse files
committed
Python: Minor performance enhancements.
1 parent 87ebc17 commit a5fbbde

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

python/ql/src/semmle/python/objects/Instances.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,16 +440,21 @@ class SuperInstance extends TSuperInstance, ObjectInternal {
440440
pragma [noinline] override predicate descriptorGetInstance(ObjectInternal instance, ObjectInternal value, CfgOrigin origin) { none() }
441441

442442
pragma [noinline] override predicate attribute(string name, ObjectInternal value, CfgOrigin origin) {
443-
PointsToInternal::attributeRequired(this, name) and
444443
exists(ObjectInternal cls_attr, CfgOrigin attr_orig |
445-
this.lookup(name, cls_attr, attr_orig)
444+
this.attribute_descriptor(name, cls_attr, attr_orig)
446445
|
447446
cls_attr.isDescriptor() = false and value = cls_attr and origin = attr_orig
448447
or
449448
cls_attr.isDescriptor() = true and cls_attr.descriptorGetInstance(this.getSelf(), value, origin)
450449
)
451450
}
452451

452+
/* Helper for `attribute` */
453+
pragma [noinline] private predicate attribute_descriptor(string name, ObjectInternal cls_attr, CfgOrigin attr_orig) {
454+
PointsToInternal::attributeRequired(this, name) and
455+
this.lookup(name, cls_attr, attr_orig)
456+
}
457+
453458
private predicate lookup(string name, ObjectInternal value, CfgOrigin origin) {
454459
Types::getMro(this.getSelf().getClass()).startingAt(this.getStartClass()).getTail().lookup(name, value, origin)
455460
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,9 +1856,13 @@ cached module Types {
18561856
result = getInheritedMetaclass(cls, 0)
18571857
or
18581858
// Best guess if base is not a known class
1859+
hasUnknownBase(cls) and result = ObjectInternal::unknownClass()
1860+
}
1861+
1862+
/* Helper for getInheritedMetaclass */
1863+
private predicate hasUnknownBase(ClassObjectInternal cls) {
18591864
exists(ObjectInternal base |
1860-
base = getBase(cls, _) and
1861-
result = ObjectInternal::unknownClass() |
1865+
base = getBase(cls, _) |
18621866
base.isClass() = false
18631867
or
18641868
base = ObjectInternal::unknownClass()
@@ -1868,14 +1872,18 @@ cached module Types {
18681872
private ClassObjectInternal getInheritedMetaclass(ClassObjectInternal cls, int n) {
18691873
exists(Class c |
18701874
c = cls.(PythonClassObjectInternal).getScope() and
1871-
n = count(c.getABase())
1875+
n = count(c.getABase()) and n != 1
18721876
|
18731877
result = ObjectInternal::type() and major_version() = 3
18741878
or
18751879
result = ObjectInternal::classType() and major_version() = 2
18761880
)
18771881
or
1882+
base_count(cls) = 1 and n = 0 and
1883+
result = getBase(cls, 0).getClass()
1884+
or
18781885
exists(ClassObjectInternal meta1, ClassObjectInternal meta2 |
1886+
base_count(cls) > 1 and
18791887
meta1 = getBase(cls, n).getClass() and
18801888
meta2 = getInheritedMetaclass(cls, n+1)
18811889
|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class PointsToContext extends TPointsToContext {
182182
this = TRuntimeContext() and executes_in_runtime_context(s)
183183
or
184184
/* Called functions, regardless of their name */
185-
exists(CallableObjectInternal callable, ControlFlowNode call, TPointsToContext outerContext |
185+
exists(PythonFunctionObjectInternal callable, ControlFlowNode call, TPointsToContext outerContext |
186186
call = callable.getACall(outerContext) and
187187
this = TCallContext(call, outerContext, _) |
188188
s = callable.getScope()

python/ql/src/semmle/python/security/TaintTracking.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ class CallContext extends TCallContext {
15121512
f.getFunction() = s and f.getACall() = call
15131513
)
15141514
or
1515-
exists(ClassValue cls,CallNode call |
1515+
exists(ClassValue cls, CallNode call |
15161516
this = TCalleeContext(call, _, _) and
15171517
call.getFunction().pointsTo(cls) and
15181518
s = cls.lookup("__init__").(CallableValue).getScope() and

0 commit comments

Comments
 (0)