Skip to content

Commit 3455dd5

Browse files
committed
C#: Re-factor telemetry queries to avoid code duplication.
1 parent 2bbfdcf commit 3455dd5

File tree

5 files changed

+32
-42
lines changed

5 files changed

+32
-42
lines changed

csharp/ql/src/Telemetry/ExternalApi.qll

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,28 @@ class ExternalApi extends DotNet::Callable {
114114
int resultLimit() { result = 1000 }
115115

116116
/**
117-
* Holds if the relevant usage count of api with `apiInfo` is `usages`.
117+
* Holds if it is relevant to count usages of `api`.
118118
*/
119-
signature predicate relevantUsagesSig(string apiInfo, int usages);
119+
signature predicate relevantApi(ExternalApi api);
120120

121121
/**
122122
* Given a predicate to count relevant API usages, this module provides a predicate
123123
* for restricting the number or returned results based on a certain limit.
124124
*/
125-
module Results<relevantUsagesSig/2 getRelevantUsages> {
125+
module Results<relevantApi/1 getRelevantUsages> {
126+
private int getUsages(string apiInfo) {
127+
result =
128+
strictcount(DispatchCall c, ExternalApi api |
129+
c = api.getACall() and
130+
apiInfo = api.getInfo() and
131+
getRelevantUsages(api)
132+
)
133+
}
134+
126135
private int getOrder(string apiInfo) {
127136
apiInfo =
128137
rank[result](string info, int usages |
129-
getRelevantUsages(info, usages)
138+
usages = getUsages(info)
130139
|
131140
info order by usages desc, info
132141
)
@@ -137,6 +146,7 @@ module Results<relevantUsagesSig/2 getRelevantUsages> {
137146
* and if it is in the top results (guarded by resultLimit).
138147
*/
139148
predicate restrict(string apiInfo, int usages) {
140-
getRelevantUsages(apiInfo, usages) and getOrder(apiInfo) <= resultLimit()
149+
usages = getUsages(apiInfo) and
150+
getOrder(apiInfo) <= resultLimit()
141151
}
142152
}

csharp/ql/src/Telemetry/SupportedExternalSinks.ql

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ private import csharp
1010
private import semmle.code.csharp.dispatch.Dispatch
1111
private import ExternalApi
1212

13-
private predicate getRelevantUsages(string apiInfo, int usages) {
14-
usages =
15-
strictcount(DispatchCall c, ExternalApi api |
16-
apiInfo = api.getInfo() and
17-
c = api.getACall() and
18-
not api.isUninteresting() and
19-
api.isSink()
20-
)
13+
private predicate relevant(ExternalApi api) {
14+
not api.isUninteresting() and
15+
api.isSink()
2116
}
2217

2318
from string info, int usages
24-
where Results<getRelevantUsages/2>::restrict(info, usages)
19+
where Results<relevant/1>::restrict(info, usages)
2520
select info, usages order by usages desc

csharp/ql/src/Telemetry/SupportedExternalSources.ql

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ private import csharp
1010
private import semmle.code.csharp.dispatch.Dispatch
1111
private import ExternalApi
1212

13-
private predicate getRelevantUsages(string apiInfo, int usages) {
14-
usages =
15-
strictcount(DispatchCall c, ExternalApi api |
16-
c = api.getACall() and
17-
apiInfo = api.getInfo() and
18-
not api.isUninteresting() and
19-
api.isSource()
20-
)
13+
private predicate relevant(ExternalApi api) {
14+
not api.isUninteresting() and
15+
api.isSource()
2116
}
2217

2318
from string info, int usages
24-
where Results<getRelevantUsages/2>::restrict(info, usages)
19+
where Results<relevant/1>::restrict(info, usages)
2520
select info, usages order by usages desc

csharp/ql/src/Telemetry/SupportedExternalTaint.ql

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ private import csharp
1010
private import semmle.code.csharp.dispatch.Dispatch
1111
private import ExternalApi
1212

13-
private predicate getRelevantUsages(string apiInfo, int usages) {
14-
usages =
15-
strictcount(DispatchCall c, ExternalApi api |
16-
apiInfo = api.getInfo() and
17-
c = api.getACall() and
18-
not api.isUninteresting() and
19-
api.hasSummary()
20-
)
13+
private predicate relevant(ExternalApi api) {
14+
not api.isUninteresting() and
15+
api.hasSummary()
2116
}
2217

2318
from string info, int usages
24-
where Results<getRelevantUsages/2>::restrict(info, usages)
19+
where Results<relevant/1>::restrict(info, usages)
2520
select info, usages order by usages desc

csharp/ql/src/Telemetry/UnsupportedExternalAPIs.ql

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSumma
1212
private import semmle.code.csharp.dataflow.internal.NegativeSummary
1313
private import ExternalApi
1414

15-
private predicate getRelevantUsages(string apiInfo, int usages) {
16-
usages =
17-
strictcount(DispatchCall c, ExternalApi api |
18-
apiInfo = api.getInfo() and
19-
c = api.getACall() and
20-
not api.isUninteresting() and
21-
not api.isSupported() and
22-
not api instanceof FlowSummaryImpl::Public::NegativeSummarizedCallable
23-
)
15+
private predicate relevant(ExternalApi api) {
16+
not api.isUninteresting() and
17+
not api.isSupported() and
18+
not api instanceof FlowSummaryImpl::Public::NegativeSummarizedCallable
2419
}
2520

2621
from string info, int usages
27-
where Results<getRelevantUsages/2>::restrict(info, usages)
22+
where Results<relevant/1>::restrict(info, usages)
2823
select info, usages order by usages desc

0 commit comments

Comments
 (0)