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

Skip to content

Commit 162bf51

Browse files
committed
Python points-to: Assorted improvements to performance and better compatibility.
1 parent ef0a6b6 commit 162bf51

7 files changed

Lines changed: 307 additions & 301 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class PythonFunctionObjectInternal extends CallableObjectInternal, TPythonFuncti
8080
this = TPythonFunctionObject(result)
8181
}
8282

83+
pragma [noinline]
8384
override predicate callResult(PointsToContext callee, ObjectInternal obj, CfgOrigin origin) {
8485
exists(Function func, ControlFlowNode rval |
8586
func = this.getScope() and
@@ -93,10 +94,11 @@ class PythonFunctionObjectInternal extends CallableObjectInternal, TPythonFuncti
9394
)
9495
}
9596

97+
pragma [noinline]
9698
override predicate callResult(ObjectInternal obj, CfgOrigin origin) {
9799
this.getScope().isProcedure() and
98100
obj = ObjectInternal::none_() and
99-
origin = CfgOrigin::unknown()
101+
origin = this.getScope().getEntryNode()
100102
}
101103

102104
override predicate calleeAndOffset(Function scope, int paramOffset) {
@@ -155,6 +157,7 @@ class BuiltinFunctionObjectInternal extends CallableObjectInternal, TBuiltinFunc
155157

156158
override predicate callResult(PointsToContext callee, ObjectInternal obj, CfgOrigin origin) { none() }
157159

160+
pragma [noinline]
158161
override predicate callResult(ObjectInternal obj, CfgOrigin origin) {
159162
exists(Builtin func, BuiltinClassObjectInternal cls |
160163
func = this.getBuiltin() and

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,7 @@ class TypeInternal extends ClassObjectInternal, TType {
236236
}
237237

238238
override predicate callResult(PointsToContext callee, ObjectInternal obj, CfgOrigin origin) {
239-
exists(CallNode call, PointsToContext caller, ObjectInternal instance |
240-
callee.fromCall(call, caller) |
241-
count(call.getAnArg()) = 1 and
242-
PointsToInternal::pointsTo(call.getArg(0), caller, instance, origin) and
243-
obj = instance.getClass()
244-
)
239+
none()
245240
}
246241

247242
override predicate callResult(ObjectInternal obj, CfgOrigin origin) {

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ abstract class ModuleObjectInternal extends ObjectInternal {
2323
none()
2424
}
2525

26-
override ControlFlowNode getOrigin() {
27-
result = this.getSourceModule().getEntryNode()
28-
}
29-
3026
override boolean isClass() { result = false }
3127

3228
override boolean isComparable() { result = true }
@@ -92,6 +88,10 @@ class BuiltinModuleObjectInternal extends ModuleObjectInternal, TBuiltinModuleOb
9288

9389
override predicate attributesUnknown() { none() }
9490

91+
override ControlFlowNode getOrigin() {
92+
none()
93+
}
94+
9595
}
9696

9797
class PackageObjectInternal extends ModuleObjectInternal, TPackageObject {
@@ -154,18 +154,14 @@ class PackageObjectInternal extends ModuleObjectInternal, TPackageObject {
154154
override predicate attribute(string name, ObjectInternal value, CfgOrigin origin) {
155155
this.getInitModule().attribute(name, value, origin)
156156
or
157-
// TO DO, dollar variable...
158-
//exists(Module init |
159-
// init = this.getSourceModule() and
160-
// not exists(EssaVariable var | var.getAUse() = init.getANormalExit() and var.getSourceVariable().getName() = name) and
161-
// exists(EssaVariable var, Context context |
162-
// isModuleStateVariable(var) and var.getAUse() = init.getANormalExit() and
163-
// context.isImport() and
164-
// SSA::ssa_variable_named_attribute_pointsTo(var, context, name, undefinedVariable(), _, origin) and
165-
// value = this.submodule(name)
166-
// )
167-
//)
168-
//or
157+
exists(Module init |
158+
init = this.getSourceModule() and
159+
not exists(EssaVariable var | var.getAUse() = init.getANormalExit() and var.getSourceVariable().getName() = name) and
160+
ModuleAttributes::pointsToAtExit(init, name, ObjectInternal::undefined(), _) and
161+
value = this.submodule(name) and
162+
origin = CfgOrigin::fromModule(value)
163+
)
164+
or
169165
this.hasNoInitModule() and
170166
exists(ModuleObjectInternal mod |
171167
mod = this.submodule(name) and
@@ -176,6 +172,10 @@ class PackageObjectInternal extends ModuleObjectInternal, TPackageObject {
176172

177173
override predicate attributesUnknown() { none() }
178174

175+
override ControlFlowNode getOrigin() {
176+
none()
177+
}
178+
179179
}
180180

181181
/** Get the ESSA pseudo-variable used to retain module state
@@ -249,5 +249,9 @@ class PythonModuleObjectInternal extends ModuleObjectInternal, TPythonModule {
249249

250250
override predicate attributesUnknown() { none() }
251251

252+
override ControlFlowNode getOrigin() {
253+
result = this.getSourceModule().getEntryNode()
254+
}
255+
252256
}
253257

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ class Value extends TObject {
5353
}
5454

5555
/* For backwards compatibility with old API */
56-
ObjectSource getSource() {
56+
deprecated ObjectSource getSource() {
5757
result = this.(ObjectInternal).getSource()
58+
or
59+
exists(Module p |
60+
p.isPackage() and
61+
p.getPath() = this.(PackageObjectInternal).getFolder() and
62+
result = p.getEntryNode()
63+
)
5864
}
5965

6066
/** Gets the `ControlFlowNode` that will be passed as the nth argument to `this` when called at `call`.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import python
22
private import semmle.python.pointsto.PointsToContext
33

4-
class Context = PointsToContext;
4+
class Context = PointsToContext;

0 commit comments

Comments
 (0)