Skip to content

Commit e7d2c1e

Browse files
committed
Python: Convert ObjectOrCfg back to using extensionals for speed.
1 parent bcf4df8 commit e7d2c1e

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

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

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,8 @@ private import Filters as BaseFilters
3131
import semmle.dataflow.SSA
3232
private import MRO
3333

34-
//library class ObjectOrCfg extends @py_object {
35-
//
36-
// string toString() {
37-
// /* Not to be displayed */
38-
// none()
39-
// }
40-
//
41-
// ControlFlowNode getOrigin() {
42-
// result = this
43-
// }
44-
//
45-
// /** Get a `ControlFlowNode` from `this` or `here`.
46-
// * If `this` is a ControlFlowNode then use that, otherwise fall back on `here`
47-
// */
48-
// pragma[inline]
49-
// ControlFlowNode fromObjectOrHere(ControlFlowNode here) {
50-
// result = this
51-
// or
52-
// not this instanceof ControlFlowNode and result = here
53-
// }
54-
//
55-
//}
56-
57-
private newtype TObjectOrCfg =
58-
TUnknownOrigin()
59-
or
60-
TCfgOrigin(ControlFlowNode f)
61-
62-
library class ObjectOrCfg extends TObjectOrCfg {
34+
/* Use this version for speed */
35+
library class ObjectOrCfg extends @py_object {
6336

6437
string toString() {
6538
/* Not to be displayed */
@@ -70,31 +43,70 @@ library class ObjectOrCfg extends TObjectOrCfg {
7043
* If `this` is a ControlFlowNode then use that, otherwise fall back on `here`
7144
*/
7245
pragma[inline]
73-
ControlFlowNode fromObjectOrHere(ControlFlowNode here) {
74-
this = TUnknownOrigin() and result = here
46+
ControlFlowNode asCfgNodeOrHere(ControlFlowNode here) {
47+
result = this
7548
or
76-
this = TCfgOrigin(result)
49+
not this instanceof ControlFlowNode and result = here
7750
}
7851

7952
ControlFlowNode toCfgNode() {
80-
this = TCfgOrigin(result)
53+
result = this
8154
}
8255

56+
pragma[inline]
8357
ObjectOrCfg fix(ControlFlowNode here) {
84-
this = TUnknownOrigin() and result = TCfgOrigin(here)
85-
or
86-
not this = TUnknownOrigin() and result = this
58+
if this = unknownValue() then
59+
result = here
60+
else
61+
result = this
8762
}
63+
8864
}
8965

66+
/* Use this version for stronger type-checking */
67+
//private newtype TObjectOrCfg =
68+
// TUnknownOrigin()
69+
// or
70+
// TCfgOrigin(ControlFlowNode f)
71+
//
72+
//library class ObjectOrCfg extends TObjectOrCfg {
73+
//
74+
// string toString() {
75+
// /* Not to be displayed */
76+
// none()
77+
// }
78+
//
79+
// /** Get a `ControlFlowNode` from `this` or `here`.
80+
// * If `this` is a ControlFlowNode then use that, otherwise fall back on `here`
81+
// */
82+
// pragma[inline]
83+
// ControlFlowNode asCfgNodeOrHere(ControlFlowNode here) {
84+
// this = TUnknownOrigin() and result = here
85+
// or
86+
// this = TCfgOrigin(result)
87+
// }
88+
//
89+
// ControlFlowNode toCfgNode() {
90+
// this = TCfgOrigin(result)
91+
// }
92+
//
93+
// ObjectOrCfg fix(ControlFlowNode here) {
94+
// this = TUnknownOrigin() and result = TCfgOrigin(here)
95+
// or
96+
// not this = TUnknownOrigin() and result = this
97+
// }
98+
//}
99+
//
100+
101+
90102
module ObjectOrCfg {
91103

92104
ObjectOrCfg fromCfgNode(ControlFlowNode f) {
93-
result = TCfgOrigin(f)
105+
result = f
94106
}
95107

96108
ObjectOrCfg unknown() {
97-
result = TUnknownOrigin()
109+
result = unknownValue()
98110
}
99111

100112
}
@@ -479,7 +491,7 @@ module PointsTo {
479491
or
480492
package_attribute_points_to(mod, name, value, cls, origin)
481493
or
482-
builtin_module_attribute(mod, name, value, cls) and origin = TUnknownOrigin()
494+
builtin_module_attribute(mod, name, value, cls) and origin = ObjectOrCfg::unknown()
483495
}
484496

485497
}
@@ -520,7 +532,7 @@ module PointsTo {
520532
or
521533
exists(ObjectOrCfg orig |
522534
getattr_points_to(f, context, value, cls, orig) and
523-
origin = orig.fromObjectOrHere(f)
535+
origin = orig.asCfgNodeOrHere(f)
524536
)
525537
or
526538
if_exp_points_to(f, context, value, cls, origin)
@@ -627,7 +639,7 @@ module PointsTo {
627639
exists(ObjectOrCfg origin_or_obj |
628640
value != undefinedVariable() and
629641
use_points_to_maybe_origin(f, context, value, cls, origin_or_obj) |
630-
origin = origin_or_obj.fromObjectOrHere(f)
642+
origin = origin_or_obj.asCfgNodeOrHere(f)
631643
)
632644
}
633645

@@ -667,7 +679,7 @@ module PointsTo {
667679
f.isLoad() and
668680
exists(string name |
669681
exists(ObjectOrCfg orig |
670-
origin = orig.fromObjectOrHere(f) and
682+
origin = orig.asCfgNodeOrHere(f) and
671683
named_attribute_points_to(f.getObject(name), context, name, value, cls, orig)
672684
)
673685
or
@@ -707,7 +719,7 @@ module PointsTo {
707719
exists(Object cls_or_mod, string name, ObjectOrCfg orig |
708720
receiver_object(f, context, cls_or_mod, name) and
709721
class_or_module_attribute(cls_or_mod, name, value, cls, orig) and
710-
origin = orig.fromObjectOrHere(f)
722+
origin = orig.asCfgNodeOrHere(f)
711723
)
712724
or
713725
points_to(f.getObject(), context, unknownValue(), theUnknownType(), origin) and value = unknownValue() and cls = theUnknownType()
@@ -737,7 +749,7 @@ module PointsTo {
737749
private predicate from_import_points_to(ImportMemberNode f, PointsToContext context, Object value, ClassObject cls, ControlFlowNode origin) {
738750
exists(string name, ModuleObject mod, ObjectOrCfg orig |
739751
points_to(f.getModule(name), context, mod, _, _) and
740-
origin = orig.fromObjectOrHere(f)
752+
origin = orig.asCfgNodeOrHere(f)
741753
|
742754
mod.getSourceModule() = f.getEnclosingModule() and
743755
exists(EssaVariable var |
@@ -1653,7 +1665,7 @@ module PointsTo {
16531665
)
16541666
or
16551667
exists(EssaVariable obj, PointsToContext caller, ObjectOrCfg orig |
1656-
origin = orig.fromObjectOrHere(def.getDefiningNode()) and
1668+
origin = orig.asCfgNodeOrHere(def.getDefiningNode()) and
16571669
ssa_variable_points_to(obj, caller, value, cls, orig) and
16581670
callsite_self_argument_transfer(obj, caller, def, context)
16591671
)
@@ -1758,7 +1770,7 @@ module PointsTo {
17581770
exists(EssaVariable var, PointsToContext outer, ObjectOrCfg orig |
17591771
Flow::scope_entry_value_transfer(var, outer, def, context) and
17601772
ssa_variable_points_to(var, outer, value, cls, orig) and
1761-
origin = orig.fromObjectOrHere(def.getDefiningNode())
1773+
origin = orig.asCfgNodeOrHere(def.getDefiningNode())
17621774
)
17631775
or
17641776
/* Undefined variable */
@@ -1950,7 +1962,7 @@ module PointsTo {
19501962
exists(EssaVariable var, PointsToContext outer, ObjectOrCfg orig |
19511963
Flow::scope_entry_value_transfer(var, outer, def, context) and
19521964
ssa_variable_named_attribute_points_to(var, outer, name, value, cls, orig) and
1953-
origin = orig.fromObjectOrHere(def.getDefiningNode())
1965+
origin = orig.asCfgNodeOrHere(def.getDefiningNode())
19541966
)
19551967
or
19561968
origin = def.getDefiningNode() and
@@ -1968,7 +1980,7 @@ module PointsTo {
19681980
private predicate assignment_named_attribute_points_to(AssignmentDefinition def, PointsToContext context, string name, Object value, ClassObject cls, ControlFlowNode origin) {
19691981
exists(ObjectOrCfg orig |
19701982
named_attribute_points_to(def.getValue(), context, name, value, cls, orig) and
1971-
origin = orig.fromObjectOrHere(def.getDefiningNode())
1983+
origin = orig.asCfgNodeOrHere(def.getDefiningNode())
19721984
)
19731985
}
19741986

0 commit comments

Comments
 (0)