Skip to content

Commit e37bda2

Browse files
committed
Python points-to: Clarify use of '$' variable.
1 parent 50a75d2 commit e37bda2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ module PointsTo {
120120
exists(Module init |
121121
init = package.getInitModule().getModule() and
122122
not exists(EssaVariable var | var.getAUse() = init.getANormalExit() and var.getSourceVariable().getName() = name) and
123-
exists(EssaVariable var, Context context | var.getName() = "$" and
123+
exists(EssaVariable var, Context context |
124+
isModuleStateVariable(var) and var.getAUse() = init.getANormalExit() and
124125
context.isImport() and
125126
SSA::ssa_variable_named_attribute_points_to(var, context, name, undefinedVariable(), _, _) and
126127
origin = value and
@@ -148,7 +149,7 @@ module PointsTo {
148149
or
149150
not exists(EssaVariable var | var.getAUse() = m.getANormalExit() and var.getSourceVariable().getName() = name) and
150151
exists(EssaVariable var, PointsToContext imp |
151-
var.getAUse() = m.getANormalExit() and var.getName() = "$" |
152+
var.getAUse() = m.getANormalExit() and isModuleStateVariable(var) |
152153
SSA::ssa_variable_named_attribute_points_to(var, imp, name, obj, cls, origin) and
153154
imp.isImport() and obj != undefinedVariable()
154155
)
@@ -677,8 +678,8 @@ module PointsTo {
677678
or
678679
mod.getSourceModule() = f.getEnclosingModule() and
679680
not exists(EssaVariable var | var.getSourceVariable().getName() = name and var.getAUse() = f) and
680-
exists(EssaVariable dollar |
681-
dollar.getName() = "$" and dollar.getAUse() = f and
681+
exists(EssaVariable dollar |
682+
isModuleStateVariable(dollar) and dollar.getAUse() = f and
682683
SSA::ssa_variable_named_attribute_points_to(dollar, context, name, value, cls, orig)
683684
)
684685
or
@@ -1862,7 +1863,7 @@ module PointsTo {
18621863
)
18631864
or
18641865
origin = def.getDefiningNode() and
1865-
def.getSourceVariable().getName() = "$" and
1866+
isModuleStateVariable(def.getVariable()) and
18661867
context.isImport() and
18671868
exists(PackageObject package |
18681869
package.getInitModule().getModule() = def.getScope() |
@@ -1973,7 +1974,7 @@ module PointsTo {
19731974
/* Helper for import_star_named_attribute_points_to */
19741975
pragma [noinline]
19751976
private predicate star_variable_import_star_module(ImportStarRefinement def, ImportStarNode imp, PointsToContext context, ModuleObject mod) {
1976-
def.getSourceVariable().getName() = "$" and
1977+
isModuleStateVariable(def.getVariable()) and
19771978
exists(ControlFlowNode fmod |
19781979
fmod = imp.getModule() and
19791980
imp = def.getDefiningNode() and
@@ -1993,7 +1994,7 @@ module PointsTo {
19931994
/* Helper for ssa_star_variable_input_points_to */
19941995
pragma [noinline]
19951996
private predicate ssa_star_import_star_input(ImportStarRefinement def, EssaVariable var) {
1996-
def.getSourceVariable().getName() = "$" and var = def.getInput()
1997+
isModuleStateVariable(def.getVariable()) and var = def.getInput()
19971998
}
19981999

19992000
pragma [noinline]
@@ -2782,6 +2783,15 @@ module PointsTo {
27822783

27832784
}
27842785

2786+
/** Get the ESSA pseudo-variable used to retain module state
2787+
* during module initialization. Module attributes are handled
2788+
* as attributes of this variable, allowing the SSA form to track
2789+
* mutations of the module during its creation.
2790+
*/
2791+
private predicate isModuleStateVariable(EssaVariable var) {
2792+
var.getName() = "$" and var.getScope() instanceof Module
2793+
}
2794+
27852795
/** INTERNAL -- Public for testing only */
27862796
module Test {
27872797

0 commit comments

Comments
 (0)