Skip to content

Commit c03ee32

Browse files
committed
Python: Move cached predicates in type tracker library to same stage
1 parent 19305a2 commit c03ee32

File tree

1 file changed

+73
-62
lines changed

1 file changed

+73
-62
lines changed

python/ql/src/semmle/python/dataflow/new/internal/TypeTracker.qll

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,57 @@ class OptionalContentName extends string {
2323
OptionalContentName() { this instanceof ContentName or this = "" }
2424
}
2525

26-
/**
27-
* A description of a step on an inter-procedural data flow path.
28-
*/
29-
private newtype TStepSummary =
30-
LevelStep() or
31-
CallStep() or
32-
ReturnStep() or
33-
StoreStep(ContentName content) or
34-
LoadStep(ContentName content)
26+
cached
27+
private module Cached {
28+
/**
29+
* A description of a step on an inter-procedural data flow path.
30+
*/
31+
cached
32+
newtype TStepSummary =
33+
LevelStep() or
34+
CallStep() or
35+
ReturnStep() or
36+
StoreStep(ContentName content) or
37+
LoadStep(ContentName content)
38+
39+
/** Gets the summary resulting from appending `step` to type-tracking summary `tt`. */
40+
cached
41+
TypeTracker append(TypeTracker tt, StepSummary step) {
42+
exists(Boolean hasCall, OptionalContentName content | tt = MkTypeTracker(hasCall, content) |
43+
step = LevelStep() and result = tt
44+
or
45+
step = CallStep() and result = MkTypeTracker(true, content)
46+
or
47+
step = ReturnStep() and hasCall = false and result = tt
48+
or
49+
step = LoadStep(content) and result = MkTypeTracker(hasCall, "")
50+
or
51+
exists(string p | step = StoreStep(p) and content = "" and result = MkTypeTracker(hasCall, p))
52+
)
53+
}
54+
55+
/**
56+
* Gets the summary that corresponds to having taken a forwards
57+
* heap and/or intra-procedural step from `nodeFrom` to `nodeTo`.
58+
*
59+
* Steps contained in this predicate should _not_ depend on the call graph.
60+
*/
61+
cached
62+
predicate stepNoCall(LocalSourceNode nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
63+
exists(Node mid | nodeFrom.flowsTo(mid) and smallstepNoCall(mid, nodeTo, summary))
64+
}
65+
66+
/**
67+
* Gets the summary that corresponds to having taken a forwards
68+
* inter-procedural step from `nodeFrom` to `nodeTo`.
69+
*/
70+
cached
71+
predicate stepCall(LocalSourceNode nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
72+
exists(Node mid | nodeFrom.flowsTo(mid) and smallstepCall(mid, nodeTo, summary))
73+
}
74+
}
75+
76+
private import Cached
3577

3678
/**
3779
* INTERNAL: Use `TypeTracker` or `TypeBackTracker` instead.
@@ -53,28 +95,29 @@ class StepSummary extends TStepSummary {
5395
}
5496
}
5597

56-
/** Provides predicates for updating step summaries (`StepSummary`s). */
57-
module StepSummary {
58-
/**
59-
* Gets the summary that corresponds to having taken a forwards
60-
* heap and/or intra-procedural step from `nodeFrom` to `nodeTo`.
61-
*
62-
* Steps contained in this predicate should _not_ depend on the call graph.
63-
*/
64-
cached
65-
private predicate stepNoCall(LocalSourceNode nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
66-
exists(Node mid | nodeFrom.flowsTo(mid) and smallstepNoCall(mid, nodeTo, summary))
67-
}
98+
pragma[noinline]
99+
private predicate smallstepNoCall(Node nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
100+
jumpStep(nodeFrom, nodeTo) and
101+
summary = LevelStep()
102+
or
103+
exists(string content |
104+
StepSummary::localSourceStoreStep(nodeFrom, nodeTo, content) and
105+
summary = StoreStep(content)
106+
or
107+
basicLoadStep(nodeFrom, nodeTo, content) and summary = LoadStep(content)
108+
)
109+
}
68110

69-
/**
70-
* Gets the summary that corresponds to having taken a forwards
71-
* inter-procedural step from `nodeFrom` to `nodeTo`.
72-
*/
73-
cached
74-
private predicate stepCall(LocalSourceNode nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
75-
exists(Node mid | nodeFrom.flowsTo(mid) and smallstepCall(mid, nodeTo, summary))
76-
}
111+
pragma[noinline]
112+
private predicate smallstepCall(Node nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
113+
callStep(nodeFrom, nodeTo) and summary = CallStep()
114+
or
115+
returnStep(nodeFrom, nodeTo) and
116+
summary = ReturnStep()
117+
}
77118

119+
/** Provides predicates for updating step summaries (`StepSummary`s). */
120+
module StepSummary {
78121
/**
79122
* Gets the summary that corresponds to having taken a forwards
80123
* heap and/or inter-procedural step from `nodeFrom` to `nodeTo`.
@@ -92,27 +135,6 @@ module StepSummary {
92135
stepCall(nodeFrom, nodeTo, summary)
93136
}
94137

95-
pragma[noinline]
96-
private predicate smallstepNoCall(Node nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
97-
jumpStep(nodeFrom, nodeTo) and
98-
summary = LevelStep()
99-
or
100-
exists(string content |
101-
localSourceStoreStep(nodeFrom, nodeTo, content) and
102-
summary = StoreStep(content)
103-
or
104-
basicLoadStep(nodeFrom, nodeTo, content) and summary = LoadStep(content)
105-
)
106-
}
107-
108-
pragma[noinline]
109-
private predicate smallstepCall(Node nodeFrom, LocalSourceNode nodeTo, StepSummary summary) {
110-
callStep(nodeFrom, nodeTo) and summary = CallStep()
111-
or
112-
returnStep(nodeFrom, nodeTo) and
113-
summary = ReturnStep()
114-
}
115-
116138
/**
117139
* Gets the summary that corresponds to having taken a forwards
118140
* local, heap and/or inter-procedural step from `nodeFrom` to `nodeTo`.
@@ -193,18 +215,7 @@ class TypeTracker extends TTypeTracker {
193215
TypeTracker() { this = MkTypeTracker(hasCall, content) }
194216

195217
/** Gets the summary resulting from appending `step` to this type-tracking summary. */
196-
cached
197-
TypeTracker append(StepSummary step) {
198-
step = LevelStep() and result = this
199-
or
200-
step = CallStep() and result = MkTypeTracker(true, content)
201-
or
202-
step = ReturnStep() and hasCall = false and result = this
203-
or
204-
step = LoadStep(content) and result = MkTypeTracker(hasCall, "")
205-
or
206-
exists(string p | step = StoreStep(p) and content = "" and result = MkTypeTracker(hasCall, p))
207-
}
218+
TypeTracker append(StepSummary step) { result = append(this, step) }
208219

209220
/** Gets a textual representation of this summary. */
210221
string toString() {

0 commit comments

Comments
 (0)