Skip to content

Commit 9e1a523

Browse files
authored
Merge pull request #950 from markshannon/python-fix-a-couple-of-performance-issues
Python: fix a couple of performance issues
2 parents 9dc6245 + 4e8e4ae commit 9e1a523

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -752,12 +752,6 @@ module PointsTo {
752752
origin = orig.asCfgNodeOrHere(f)
753753
|
754754
mod.getSourceModule() = f.getEnclosingModule() and
755-
exists(EssaVariable var |
756-
var.getSourceVariable().getName() = name and var.getAUse() = f and
757-
ssa_variable_points_to(var, context, value, cls, orig)
758-
)
759-
or
760-
mod.getSourceModule() = f.getEnclosingModule() and
761755
not exists(EssaVariable var | var.getSourceVariable().getName() = name and var.getAUse() = f) and
762756
exists(EssaVariable dollar |
763757
isModuleStateVariable(dollar) and dollar.getAUse() = f and
@@ -767,6 +761,20 @@ module PointsTo {
767761
not mod.getSourceModule() = f.getEnclosingModule() and
768762
Layer::module_attribute_points_to(mod, name, value, cls, orig)
769763
)
764+
or
765+
exists(EssaVariable var, CfgOrigin orig |
766+
var = ssa_variable_for_module_attribute(f, context) and
767+
ssa_variable_points_to(var, context, value, cls, orig) and
768+
origin = orig.asCfgNodeOrHere(f)
769+
)
770+
}
771+
772+
private EssaVariable ssa_variable_for_module_attribute(ImportMemberNode f, PointsToContext context) {
773+
exists(string name, ModuleObject mod, Module m |
774+
mod.getSourceModule() = m and m = f.getEnclosingModule() and m = result.getScope() and
775+
points_to(f.getModule(name), context, mod, _, _) and
776+
result.getSourceVariable().getName() = name and result.getAUse() = f
777+
)
770778
}
771779

772780
/** Holds if `f` is of the form `getattr(x, "name")` and x.name points to `(value, cls, origin)`. */
@@ -2055,6 +2063,16 @@ module PointsTo {
20552063
)
20562064
}
20572065

2066+
/** Maps the caller object/context to callee parameter/context for self in calls to methods */
2067+
private predicate self_in_method_call(ControlFlowNode obj, PointsToContext caller, ParameterDefinition self, PointsToContext callee) {
2068+
self.isSelf() and
2069+
exists(FunctionObject meth, CallNode call |
2070+
meth.getFunction() = self.getScope() and
2071+
callee.fromCall(call, meth, caller) and
2072+
call.getFunction().(AttrNode).getObject() = obj
2073+
)
2074+
}
2075+
20582076
pragma [noinline]
20592077
private predicate self_parameter_named_attribute_points_to(ParameterDefinition def, PointsToContext context, string name, Object value, ClassObject vcls, ControlFlowNode origin) {
20602078
exists(CfgOrigin orig |
@@ -2063,11 +2081,8 @@ module PointsTo {
20632081
context.isRuntime() and executes_in_runtime_context(def.getScope()) and
20642082
ssa_variable_named_attribute_points_to(preceding_self_variable(def), context, name, value, vcls, orig)
20652083
or
2066-
exists(FunctionObject meth, CallNode call, PointsToContext caller_context, ControlFlowNode obj |
2067-
meth.getFunction() = def.getScope() and
2068-
method_call(meth, caller_context, call) and
2069-
call.getFunction().(AttrNode).getObject() = obj and
2070-
context.fromCall(call, meth, caller_context) and
2084+
exists(PointsToContext caller_context, ControlFlowNode obj |
2085+
self_in_method_call(obj, caller_context, def, context) and
20712086
named_attribute_points_to(obj, caller_context, name, value, vcls, orig)
20722087
)
20732088
)

0 commit comments

Comments
 (0)