Skip to content

Commit 7422029

Browse files
committed
C#: Re-factor provenance related predicates for summarized callable.
1 parent 4b47b08 commit 7422029

File tree

6 files changed

+38
-57
lines changed

6 files changed

+38
-57
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/ExternalFlow.qll

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,6 @@ Element interpretElement(
410410
)
411411
}
412412

413-
/**
414-
* Holds if `c` has a `generated` summary.
415-
*/
416-
predicate hasSummary(Callable c, boolean generated) { summaryElement(c, _, _, _, generated) }
417-
418413
cached
419414
private module Cached {
420415
/**

csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,19 @@ module Public {
241241
}
242242

243243
/**
244-
* Holds if the summary is auto generated and not manually generated.
244+
* Holds if the summaries that apply to `this` are auto generated and not manually generated.
245245
*/
246-
predicate isAutoGenerated() { none() }
246+
final predicate isAutoGenerated() { this.hasProvenance("generated") and not this.isManual() }
247247

248248
/**
249-
* Holds if the summary has the given provenance where `true` is
250-
* `generated` and `false` is `manual`.
249+
* Holds if there exists a manual summary that applies to `this`.
251250
*/
252-
predicate hasProvenance(boolean generated) { none() }
251+
final predicate isManual() { this.hasProvenance("manual") }
252+
253+
/**
254+
* Holds if there exists a summary that applies to `this` that has provenance `provenance`.
255+
*/
256+
predicate hasProvenance(string provenance) { none() }
253257
}
254258

255259
/** A callable where there is no flow via the callable. */
@@ -259,13 +263,12 @@ module Public {
259263
/**
260264
* Holds if the neutral is auto generated.
261265
*/
262-
predicate isAutoGenerated() { neutralElement(this, true) }
266+
predicate isAutoGenerated() { neutralElement(this, "generated") }
263267

264268
/**
265-
* Holds if the neutral has the given provenance where `true` is
266-
* `generated` and `false` is `manual`.
269+
* Holds if the neutral has provenance `provenance`.
267270
*/
268-
predicate hasProvenance(boolean generated) { neutralElement(this, generated) }
271+
predicate hasProvenance(string provenance) { neutralElement(this, provenance) }
269272
}
270273
}
271274

@@ -997,12 +1000,12 @@ module Private {
9971000
private predicate relevantSummaryElementGenerated(
9981001
AccessPath inSpec, AccessPath outSpec, string kind
9991002
) {
1000-
summaryElement(this, inSpec, outSpec, kind, true) and
1001-
not summaryElement(this, _, _, _, false)
1003+
summaryElement(this, inSpec, outSpec, kind, "generated") and
1004+
not summaryElement(this, _, _, _, "manual")
10021005
}
10031006

10041007
private predicate relevantSummaryElement(AccessPath inSpec, AccessPath outSpec, string kind) {
1005-
summaryElement(this, inSpec, outSpec, kind, false)
1008+
summaryElement(this, inSpec, outSpec, kind, "manual")
10061009
or
10071010
this.relevantSummaryElementGenerated(inSpec, outSpec, kind)
10081011
}
@@ -1021,10 +1024,8 @@ module Private {
10211024
)
10221025
}
10231026

1024-
override predicate isAutoGenerated() { this.relevantSummaryElementGenerated(_, _, _) }
1025-
1026-
override predicate hasProvenance(boolean generated) {
1027-
summaryElement(this, _, _, _, generated)
1027+
override predicate hasProvenance(string provenance) {
1028+
summaryElement(this, _, _, _, provenance)
10281029
}
10291030
}
10301031

csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImplSpecific.qll

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,69 +97,52 @@ DataFlowType getSyntheticGlobalType(SummaryComponent::SyntheticGlobal sg) {
9797
result = Gvn::getGlobalValueNumber(any(ObjectType t))
9898
}
9999

100-
bindingset[provenance]
101-
private boolean isGenerated(string provenance) {
102-
provenance = "generated" and result = true
103-
or
104-
provenance != "generated" and result = false
105-
}
106-
107100
/**
108101
* Holds if an external flow summary exists for `c` with input specification
109-
* `input`, output specification `output`, kind `kind`, and a flag `generated`
110-
* stating whether the summary is autogenerated.
102+
* `input`, output specification `output`, kind `kind`, and provenance `provenance`.
111103
*/
112-
predicate summaryElement(Callable c, string input, string output, string kind, boolean generated) {
104+
predicate summaryElement(Callable c, string input, string output, string kind, string provenance) {
113105
exists(
114-
string namespace, string type, boolean subtypes, string name, string signature, string ext,
115-
string provenance
106+
string namespace, string type, boolean subtypes, string name, string signature, string ext
116107
|
117108
summaryModel(namespace, type, subtypes, name, signature, ext, input, output, kind, provenance) and
118-
generated = isGenerated(provenance) and
119109
c = interpretElement(namespace, type, subtypes, name, signature, ext)
120110
)
121111
}
122112

123113
/**
124-
* Holds if a neutral model exists for `c`, which means that there is no
125-
* flow through `c`. The flag `generated` states whether the neutral model is autogenerated.
114+
* Holds if a neutral model exists for `c` with provenance `provenace`,
115+
* which means that there is no flow through `c`.
126116
*/
127-
predicate neutralElement(Callable c, boolean generated) {
128-
exists(string namespace, string type, string name, string signature, string provenance |
117+
predicate neutralElement(Callable c, string provenance) {
118+
exists(string namespace, string type, string name, string signature |
129119
neutralModel(namespace, type, name, signature, provenance) and
130-
generated = isGenerated(provenance) and
131120
c = interpretElement(namespace, type, false, name, signature, "")
132121
)
133122
}
134123

135124
/**
136125
* Holds if an external source specification exists for `e` with output specification
137-
* `output`, kind `kind`, and a flag `generated` stating whether the source specification is
138-
* autogenerated.
126+
* `output`, kind `kind`, and provenance `provenance`.
139127
*/
140-
predicate sourceElement(Element e, string output, string kind, boolean generated) {
128+
predicate sourceElement(Element e, string output, string kind, string provenance) {
141129
exists(
142-
string namespace, string type, boolean subtypes, string name, string signature, string ext,
143-
string provenance
130+
string namespace, string type, boolean subtypes, string name, string signature, string ext
144131
|
145132
sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance) and
146-
generated = isGenerated(provenance) and
147133
e = interpretElement(namespace, type, subtypes, name, signature, ext)
148134
)
149135
}
150136

151137
/**
152138
* Holds if an external sink specification exists for `e` with input specification
153-
* `input`, kind `kind` and a flag `generated` stating whether the sink specification is
154-
* autogenerated.
139+
* `input`, kind `kind` and provenance `provenance`.
155140
*/
156-
predicate sinkElement(Element e, string input, string kind, boolean generated) {
141+
predicate sinkElement(Element e, string input, string kind, string provenance) {
157142
exists(
158-
string namespace, string type, boolean subtypes, string name, string signature, string ext,
159-
string provenance
143+
string namespace, string type, boolean subtypes, string name, string signature, string ext
160144
|
161145
sinkModel(namespace, type, subtypes, name, signature, ext, input, kind, provenance) and
162-
generated = isGenerated(provenance) and
163146
e = interpretElement(namespace, type, subtypes, name, signature, ext)
164147
)
165148
}

csharp/ql/src/utils/modelgenerator/CaptureDiscardedSummaryModels.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* @id cs/utils/modelgenerator/discarded-summary-models
55
*/
66

7-
import semmle.code.csharp.dataflow.ExternalFlow
7+
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
88
import internal.CaptureModels
99
import internal.CaptureSummaryFlow
1010

1111
from DataFlowTargetApi api, string flow
12-
where flow = captureFlow(api) and hasSummary(api, false)
12+
where
13+
flow = captureFlow(api) and
14+
api.(FlowSummaryImpl::Public::SummarizedCallable).isManual()
1315
select flow order by flow

csharp/ql/src/utils/modelgenerator/CaptureNeutralModels.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* @tags modelgenerator
77
*/
88

9-
import semmle.code.csharp.dataflow.ExternalFlow
9+
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
1010
import internal.CaptureModels
1111
import internal.CaptureSummaryFlow
1212

1313
from DataFlowTargetApi api, string noflow
1414
where
1515
noflow = captureNoFlow(api) and
16-
not hasSummary(api, false)
16+
not api.(FlowSummaryImpl::Public::SummarizedCallable).isManual()
1717
select noflow order by noflow

csharp/ql/src/utils/modelgenerator/CaptureSummaryModels.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* @tags modelgenerator
77
*/
88

9-
import semmle.code.csharp.dataflow.ExternalFlow
9+
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
1010
import internal.CaptureModels
1111
import internal.CaptureSummaryFlow
1212

1313
from DataFlowTargetApi api, string flow
14-
where flow = captureFlow(api) and not hasSummary(api, false)
14+
where flow = captureFlow(api) and not api.(FlowSummaryImpl::Public::SummarizedCallable).isManual()
1515
select flow order by flow

0 commit comments

Comments
 (0)