Skip to content

Commit 385df92

Browse files
committed
WIP
1 parent bf6014b commit 385df92

File tree

19 files changed

+77
-9
lines changed

19 files changed

+77
-9
lines changed

python/ql/lib/semmle/python/ApiGraphs.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ module API {
451451
* allowing this predicate to be used in a negative
452452
* context when constructing new nodes.
453453
*/
454+
overlay[local]
454455
predicate moduleImportExists(string m) {
455456
Impl::isImported(m) and
456457
// restrict `moduleImport` so it will never give results for a dotted name. Note
@@ -695,6 +696,7 @@ module API {
695696
*
696697
* This is determined syntactically.
697698
*/
699+
overlay[local]
698700
cached
699701
predicate isImported(string name) {
700702
// Ignore the following module name for Python 2, as we alias `__builtin__` to `builtins` elsewhere

python/ql/lib/semmle/python/dataflow/new/FlowSummary.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ deprecated class SummaryComponentStack = Impl::Private::SummaryComponentStack;
2323
deprecated module SummaryComponentStack = Impl::Private::SummaryComponentStack;
2424

2525
/** A callable with a flow summary, identified by a unique string. */
26+
overlay[local]
2627
abstract class SummarizedCallable extends LibraryCallable, Impl::Public::SummarizedCallable {
2728
bindingset[this]
2829
SummarizedCallable() { any() }

python/ql/lib/semmle/python/dataflow/new/internal/Builtins.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** Provides predicates for reasoning about built-ins in Python. */
2+
overlay[local]
3+
module;
24

35
private import python
46
private import semmle.python.dataflow.new.DataFlow

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* Note: This hasn't been 100% realized yet, so we don't currently expose a predicate to
3232
* ask what targets any data-flow node has. But it's still the plan to do this!
3333
*/
34+
overlay[local?]
35+
module;
3436

3537
private import python
3638
private import DataFlowPublic
@@ -39,6 +41,7 @@ private import FlowSummaryImpl as FlowSummaryImpl
3941
private import semmle.python.internal.CachedStages
4042
private import semmle.python.dataflow.new.internal.TypeTrackingImpl::CallGraphConstruction as CallGraphConstruction
4143

44+
overlay[local]
4245
newtype TParameterPosition =
4346
/** Used for `self` in methods, and `cls` in classmethods. */
4447
TSelfParameterPosition() or
@@ -84,6 +87,7 @@ newtype TParameterPosition =
8487
TSynthDictSplatParameterPosition()
8588

8689
/** A parameter position. */
90+
overlay[local]
8791
class ParameterPosition extends TParameterPosition {
8892
/** Holds if this position represents a `self`/`cls` parameter. */
8993
predicate isSelf() { this = TSelfParameterPosition() }
@@ -146,6 +150,7 @@ class ParameterPosition extends TParameterPosition {
146150
}
147151
}
148152

153+
overlay[local]
149154
newtype TArgumentPosition =
150155
/** Used for `self` in methods, and `cls` in classmethods. */
151156
TSelfArgumentPosition() or
@@ -180,6 +185,7 @@ newtype TArgumentPosition =
180185
TDictSplatArgumentPosition()
181186

182187
/** An argument position. */
188+
overlay[local]
183189
class ArgumentPosition extends TArgumentPosition {
184190
/** Holds if this position represents a `self`/`cls` argument. */
185191
predicate isSelf() { this = TSelfArgumentPosition() }
@@ -248,6 +254,7 @@ predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) {
248254
* `@staticmethod` decorator or by convention
249255
* (like a `__new__` method on a class is a classmethod even without the decorator).
250256
*/
257+
overlay[local]
251258
predicate isStaticmethod(Function func) {
252259
exists(NameNode id | id.getId() = "staticmethod" and id.isGlobal() |
253260
func.getADecorator() = id.getNode()
@@ -259,6 +266,7 @@ predicate isStaticmethod(Function func) {
259266
* `@classmethod` decorator or by convention
260267
* (like a `__new__` method on a class is a classmethod even without the decorator).
261268
*/
269+
overlay[local]
262270
predicate isClassmethod(Function func) {
263271
exists(NameNode id | id.getId() = "classmethod" and id.isGlobal() |
264272
func.getADecorator() = id.getNode()
@@ -275,6 +283,7 @@ predicate isClassmethod(Function func) {
275283
}
276284

277285
/** Holds if the function `func` has a `property` decorator. */
286+
overlay[local]
278287
predicate hasPropertyDecorator(Function func) {
279288
exists(NameNode id | id.getId() = "property" and id.isGlobal() |
280289
func.getADecorator() = id.getNode()
@@ -284,6 +293,7 @@ predicate hasPropertyDecorator(Function func) {
284293
/**
285294
* Holds if the function `func` has a `contextlib.contextmanager`.
286295
*/
296+
overlay[local]
287297
predicate hasContextmanagerDecorator(Function func) {
288298
exists(ControlFlowNode contextmanager |
289299
contextmanager.(NameNode).getId() = "contextmanager" and contextmanager.(NameNode).isGlobal()
@@ -298,20 +308,25 @@ predicate hasContextmanagerDecorator(Function func) {
298308
// Callables
299309
// =============================================================================
300310
/** A callable defined in library code, identified by a unique string. */
311+
overlay[local]
301312
abstract class LibraryCallable extends string {
302313
bindingset[this]
303314
LibraryCallable() { any() }
304315

305316
/** Gets a call to this library callable. */
317+
overlay[global]
306318
abstract CallCfgNode getACall();
307319

308320
/** Same as `getACall` but without referring to the call graph or API graph. */
321+
overlay[global]
309322
CallCfgNode getACallSimple() { none() }
310323

311324
/** Gets a data-flow node, where this library callable is used as a call-back. */
325+
overlay[global]
312326
abstract ArgumentNode getACallback();
313327
}
314328

329+
overlay[local]
315330
newtype TDataFlowCallable =
316331
/**
317332
* Is used as the target for all calls: plain functions, lambdas, methods on classes,
@@ -329,6 +344,7 @@ newtype TDataFlowCallable =
329344
TLibraryCallable(LibraryCallable callable)
330345

331346
/** A callable. */
347+
overlay[local]
332348
abstract class DataFlowCallable extends TDataFlowCallable {
333349
/** Gets a textual representation of this element. */
334350
abstract string toString();
@@ -350,6 +366,7 @@ abstract class DataFlowCallable extends TDataFlowCallable {
350366
}
351367

352368
/** A callable function. */
369+
overlay[local]
353370
abstract class DataFlowFunction extends DataFlowCallable, TFunction {
354371
Function func;
355372

@@ -370,6 +387,7 @@ abstract class DataFlowFunction extends DataFlowCallable, TFunction {
370387
/** Gets the positional parameter offset, to take into account self/cls parameters. */
371388
int positionalOffset() { result = 0 }
372389

390+
overlay[local]
373391
override ParameterNode getParameter(ParameterPosition ppos) {
374392
// Do not handle lower bound positions (such as `[1..]`) here
375393
// they are handled by parameter matching and would create
@@ -408,11 +426,13 @@ abstract class DataFlowFunction extends DataFlowCallable, TFunction {
408426
}
409427

410428
/** A plain (non-method) function. */
429+
overlay[local]
411430
class DataFlowPlainFunction extends DataFlowFunction {
412431
DataFlowPlainFunction() { not this instanceof DataFlowMethod }
413432
}
414433

415434
/** A method. */
435+
overlay[local]
416436
class DataFlowMethod extends DataFlowFunction {
417437
Class cls;
418438

@@ -431,11 +451,13 @@ class DataFlowMethod extends DataFlowFunction {
431451
}
432452

433453
/** A classmethod. */
454+
overlay[local]
434455
class DataFlowClassmethod extends DataFlowMethod {
435456
DataFlowClassmethod() { isClassmethod(func) }
436457
}
437458

438459
/** A staticmethod. */
460+
overlay[local]
439461
class DataFlowStaticmethod extends DataFlowMethod, DataFlowFunction {
440462
DataFlowStaticmethod() { isStaticmethod(func) }
441463

@@ -450,6 +472,7 @@ class DataFlowStaticmethod extends DataFlowMethod, DataFlowFunction {
450472
* A module. This is not actually a callable, but we need this so a
451473
* `ModuleVariableNode` have an enclosing callable.
452474
*/
475+
overlay[local]
453476
class DataFlowModuleScope extends DataFlowCallable, TModule {
454477
Module mod;
455478

@@ -466,6 +489,7 @@ class DataFlowModuleScope extends DataFlowCallable, TModule {
466489
override ParameterNode getParameter(ParameterPosition ppos) { none() }
467490
}
468491

492+
overlay[local]
469493
class LibraryCallableValue extends DataFlowCallable, TLibraryCallable {
470494
LibraryCallable callable;
471495

@@ -476,6 +500,7 @@ class LibraryCallableValue extends DataFlowCallable, TLibraryCallable {
476500
override string getQualifiedName() { result = callable.toString() }
477501

478502
/** Gets a data-flow node, where this library callable is used as a call-back. */
503+
overlay[global]
479504
ArgumentNode getACallback() { result = callable.getACallback() }
480505

481506
override Scope getScope() { none() }
@@ -1589,6 +1614,7 @@ class SummaryCall extends DataFlowCall, TSummaryCall {
15891614
* The value of a parameter at function entry, viewed as a node in a data
15901615
* flow graph.
15911616
*/
1617+
overlay[local]
15921618
abstract class ParameterNodeImpl extends Node {
15931619
/** Gets the `Parameter` this `ParameterNode` represents. */
15941620
abstract Parameter getParameter();
@@ -1610,6 +1636,7 @@ abstract class ParameterNodeImpl extends Node {
16101636
*
16111637
* This is used for tracking flow through captured variables.
16121638
*/
1639+
overlay[local]
16131640
class SynthCapturedVariablesParameterNode extends ParameterNodeImpl,
16141641
TSynthCapturedVariablesParameterNode
16151642
{
@@ -1634,6 +1661,7 @@ class SynthCapturedVariablesParameterNode extends ParameterNodeImpl,
16341661
}
16351662

16361663
/** A parameter for a library callable with a flow summary. */
1664+
overlay[local]
16371665
class SummaryParameterNode extends ParameterNodeImpl, FlowSummaryNode {
16381666
SummaryParameterNode() {
16391667
FlowSummaryImpl::Private::summaryParameterNode(this.getSummaryNode(), _)
@@ -1800,12 +1828,14 @@ DataFlowCallable viableCallable(DataFlowCall call) {
18001828
// =============================================================================
18011829
// Remaining required data-flow things
18021830
// =============================================================================
1831+
overlay[local]
18031832
private newtype TReturnKind = TNormalReturnKind()
18041833

18051834
/**
18061835
* A return kind. A return kind describes how a value can be returned
18071836
* from a callable. For Python, this is simply a method return.
18081837
*/
1838+
overlay[local]
18091839
class ReturnKind extends TReturnKind {
18101840
/** Gets a textual representation of this element. */
18111841
string toString() { result = "return" }

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPrivate.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
overlay[local?]
2+
module;
3+
14
private import python
25
private import DataFlowPublic
36
private import semmle.python.essa.SsaCompute
@@ -39,6 +42,7 @@ predicate isArgumentNode(ArgumentNode arg, DataFlowCall c, ArgumentPosition pos)
3942
//--------
4043
// Nodes
4144
//--------
45+
overlay[local]
4246
predicate isExpressionNode(ControlFlowNode node) { node.getNode() instanceof Expr }
4347

4448
// =============================================================================
@@ -111,6 +115,7 @@ class SyntheticPreUpdateNode extends Node, TSyntheticPreUpdateNode {
111115
* func = foo if <cond> else bar
112116
* func(1, 2, 3)
113117
*/
118+
overlay[local]
114119
class SynthStarArgsElementParameterNode extends ParameterNodeImpl,
115120
TSynthStarArgsElementParameterNode
116121
{
@@ -241,6 +246,7 @@ private predicate dictSplatParameterNodeClearStep(ParameterNode n, DictionaryEle
241246
* (c) since the synthesized nodes are hidden, the reported data-flow paths will be
242247
* collapsed anyway.
243248
*/
249+
overlay[local]
244250
class SynthDictSplatParameterNode extends ParameterNodeImpl, TSynthDictSplatParameterNode {
245251
DataFlowCallable callable;
246252

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPublic.qll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* Provides Python-specific definitions for use in the data flow library.
33
*/
4+
overlay[local]
5+
module;
46

57
private import python
68
private import DataFlowPrivate
@@ -22,6 +24,7 @@ private import semmle.python.frameworks.data.ModelsAsData
2224
* - Module variable nodes: These represent global variables and act as canonical targets for reads and writes of these.
2325
* - Synthetic nodes: These handle flow in various special cases.
2426
*/
27+
overlay[local]
2528
newtype TNode =
2629
/** A node corresponding to a control flow node. */
2730
TCfgNode(ControlFlowNode node) {
@@ -76,15 +79,7 @@ newtype TNode =
7679
node.getNode() = any(Comp c).getIterable()
7780
} or
7881
/** A node representing a global (module-level) variable in a specific module. */
79-
TModuleVariableNode(Module m, GlobalVariable v) {
80-
v.getScope() = m and
81-
(
82-
v.escapes()
83-
or
84-
isAccessedThroughImportStar(m) and
85-
ImportStar::globalNameDefinedInModule(v.getId(), m)
86-
)
87-
} or
82+
TModuleVariableNode(Module m, GlobalVariable v) { v.getScope() = m } or
8883
/**
8984
* A synthetic node representing that an iterable sequence flows to consumer.
9085
*/
@@ -151,6 +146,7 @@ private import semmle.python.internal.CachedStages
151146
* An element, viewed as a node in a data flow graph. Either an SSA variable
152147
* (`EssaNode`) or a control flow node (`CfgNode`).
153148
*/
149+
overlay[local]
154150
class Node extends TNode {
155151
/** Gets a textual representation of this element. */
156152
cached
@@ -318,6 +314,7 @@ class ScopeEntryDefinitionNode extends Node, TScopeEntryDefinitionNode {
318314
* The value of a parameter at function entry, viewed as a node in a data
319315
* flow graph.
320316
*/
317+
overlay[local]
321318
class ParameterNode extends Node instanceof ParameterNodeImpl {
322319
/** Gets the parameter corresponding to this node, if any. */
323320
final Parameter getParameter() { result = super.getParameter() }
@@ -613,6 +610,7 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
613610
* Algebraic datatype for tracking data content associated with values.
614611
* Content can be collection elements or object attributes.
615612
*/
613+
overlay[local]
616614
newtype TContent =
617615
/** An element of a list. */
618616
TListElementContent() or
@@ -684,6 +682,7 @@ newtype TContent =
684682
* If the value is a collection, it can have elements,
685683
* if it is an object, it can have attribute values.
686684
*/
685+
overlay[local]
687686
class Content extends TContent {
688687
/** Gets a textual representation of this element. */
689688
string toString() { result = "Content" }

python/ql/lib/semmle/python/dataflow/new/internal/FlowSummaryImpl.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* Provides classes and predicates for defining flow summaries.
33
*/
4+
overlay[local]
5+
module;
46

57
private import python
68
private import codeql.dataflow.internal.FlowSummaryImpl

python/ql/lib/semmle/python/dataflow/new/internal/ImportStar.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** Provides predicates for reasoning about uses of `import *` in Python. */
2+
overlay[local]
3+
module;
24

35
private import python
46
private import semmle.python.dataflow.new.internal.Builtins
@@ -92,6 +94,7 @@ module ImportStar {
9294
*
9395
* this would return the data-flow nodes corresponding to `foo.bar` and `quux`.
9496
*/
97+
overlay[local]
9598
cached
9699
ControlFlowNode potentialImportStarBase(Scope s) {
97100
result = any(ImportStarNode n | n.getScope() = s).getModule()

python/ql/lib/semmle/python/dataflow/new/internal/IterableUnpacking.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@
166166
*
167167
* `c`: [ListElementContent]
168168
*/
169+
overlay[local]
170+
module;
169171

170172
private import python
171173
private import DataFlowPublic

python/ql/lib/semmle/python/dataflow/new/internal/LocalSources.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Note that unlike `TypeTracker.qll`, this library only performs
66
* local tracking within a function.
77
*/
8+
overlay[local]
9+
module;
810

911
private import python
1012
import DataFlowPublic

0 commit comments

Comments
 (0)