Merge pull request #11721 from michaelnebel/csharpjava/refactorproven… · xia0le/codeql@18a815c · GitHub
Skip to content

Commit 18a815c

Browse files
authored
Merge pull request github#11721 from michaelnebel/csharpjava/refactorprovenance
C#/Java: Re-factor provenance related predicates.
2 parents 006eaf3 + 7e4f7a0 commit 18a815c

20 files changed

Lines changed: 269 additions & 291 deletions

File tree

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

Lines changed: 0 additions & 5 deletions

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -241,31 +241,39 @@ module Public {
241241
}
242242

243243
/**
244-
* Holds if the summary is auto generated and not manually generated.
244+
* Holds if all the summaries that apply to `this` are auto generated and not manually created.
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. */
256260
class NeutralCallable extends SummarizedCallableBase {
257261
NeutralCallable() { neutralElement(this, _) }
258262

259263
/**
260-
* Holds if the neutral is auto generated.
264+
* 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 there exists a manual neutral that applies to `this`.
267270
*/
268-
predicate hasProvenance(boolean generated) { neutralElement(this, generated) }
271+
final predicate isManual() { this.hasProvenance("manual") }
272+
273+
/**
274+
* Holds if the neutral has provenance `provenance`.
275+
*/
276+
predicate hasProvenance(string provenance) { neutralElement(this, provenance) }
269277
}
270278
}
271279

@@ -997,12 +1005,12 @@ module Private {
9971005
private predicate relevantSummaryElementGenerated(
9981006
AccessPath inSpec, AccessPath outSpec, string kind
9991007
) {
1000-
summaryElement(this, inSpec, outSpec, kind, true) and
1001-
not summaryElement(this, _, _, _, false)
1008+
summaryElement(this, inSpec, outSpec, kind, "generated") and
1009+
not summaryElement(this, _, _, _, "manual")
10021010
}
10031011

10041012
private predicate relevantSummaryElement(AccessPath inSpec, AccessPath outSpec, string kind) {
1005-
summaryElement(this, inSpec, outSpec, kind, false)
1013+
summaryElement(this, inSpec, outSpec, kind, "manual")
10061014
or
10071015
this.relevantSummaryElementGenerated(inSpec, outSpec, kind)
10081016
}
@@ -1021,10 +1029,8 @@ module Private {
10211029
)
10221030
}
10231031

1024-
override predicate isAutoGenerated() { this.relevantSummaryElementGenerated(_, _, _) }
1025-
1026-
override predicate hasProvenance(boolean generated) {
1027-
summaryElement(this, _, _, _, generated)
1032+
override predicate hasProvenance(string provenance) {
1033+
summaryElement(this, _, _, _, provenance)
10281034
}
10291035
}
10301036

‎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

‎go/ql/lib/semmle/go/dataflow/ExternalFlow.qll‎

Lines changed: 29 additions & 40 deletions

0 commit comments

Comments
 (0)