Skip to content

Commit 2bbfdcf

Browse files
committed
C#: Use api info string ordering and results to avoid multiplicity issues.
1 parent 42a97b2 commit 2bbfdcf

File tree

5 files changed

+56
-40
lines changed

5 files changed

+56
-40
lines changed

csharp/ql/src/Telemetry/ExternalApi.qll

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

116116
/**
117-
* Holds if the relevant usage count of `api` is `usages`.
117+
* Holds if the relevant usage count of api with `apiInfo` is `usages`.
118118
*/
119-
signature predicate relevantUsagesSig(ExternalApi api, int usages);
119+
signature predicate relevantUsagesSig(string apiInfo, int usages);
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
*/
125125
module Results<relevantUsagesSig/2 getRelevantUsages> {
126-
private int getOrder(ExternalApi api) {
127-
api =
128-
rank[result](ExternalApi a, int usages |
129-
getRelevantUsages(a, usages)
126+
private int getOrder(string apiInfo) {
127+
apiInfo =
128+
rank[result](string info, int usages |
129+
getRelevantUsages(info, usages)
130130
|
131-
a order by usages desc, a.getInfo()
131+
info order by usages desc, info
132132
)
133133
}
134134

135135
/**
136-
* Holds if `api` is being used `usages` times and if it is
137-
* in the top results (guarded by resultLimit).
136+
* Holds if there exists an API with `apiInfo` that is being used `usages` times
137+
* and if it is in the top results (guarded by resultLimit).
138138
*/
139-
predicate restrict(ExternalApi api, int usages) {
140-
getRelevantUsages(api, usages) and getOrder(api) <= resultLimit()
139+
predicate restrict(string apiInfo, int usages) {
140+
getRelevantUsages(apiInfo, usages) and getOrder(apiInfo) <= resultLimit()
141141
}
142142
}

csharp/ql/src/Telemetry/SupportedExternalSinks.ql

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

13-
private predicate getRelevantUsages(ExternalApi api, int usages) {
14-
not api.isUninteresting() and
15-
api.isSink() and
16-
usages = strictcount(DispatchCall c | c = api.getACall())
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+
)
1721
}
1822

19-
from ExternalApi api, int usages
20-
where Results<getRelevantUsages/2>::restrict(api, usages)
21-
select api.getInfo() as info, usages order by usages desc
23+
from string info, int usages
24+
where Results<getRelevantUsages/2>::restrict(info, usages)
25+
select info, usages order by usages desc

csharp/ql/src/Telemetry/SupportedExternalSources.ql

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

13-
private predicate getRelevantUsages(ExternalApi api, int usages) {
14-
not api.isUninteresting() and
15-
api.isSource() and
16-
usages = strictcount(DispatchCall c | c = api.getACall())
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+
)
1721
}
1822

19-
from ExternalApi api, int usages
20-
where Results<getRelevantUsages/2>::restrict(api, usages)
21-
select api.getInfo() as info, usages order by usages desc
23+
from string info, int usages
24+
where Results<getRelevantUsages/2>::restrict(info, usages)
25+
select info, usages order by usages desc

csharp/ql/src/Telemetry/SupportedExternalTaint.ql

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

13-
private predicate getRelevantUsages(ExternalApi api, int usages) {
14-
not api.isUninteresting() and
15-
api.hasSummary() and
16-
usages = strictcount(DispatchCall c | c = api.getACall())
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+
)
1721
}
1822

19-
from ExternalApi api, int usages
20-
where Results<getRelevantUsages/2>::restrict(api, usages)
21-
select api.getInfo() as info, usages order by usages desc
23+
from string info, int usages
24+
where Results<getRelevantUsages/2>::restrict(info, usages)
25+
select info, usages order by usages desc

csharp/ql/src/Telemetry/UnsupportedExternalAPIs.ql

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ 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(ExternalApi api, int usages) {
16-
not api.isUninteresting() and
17-
not api.isSupported() and
18-
not api instanceof FlowSummaryImpl::Public::NegativeSummarizedCallable and
19-
usages = strictcount(DispatchCall c | c = api.getACall())
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+
)
2024
}
2125

22-
from ExternalApi api, int usages
23-
where Results<getRelevantUsages/2>::restrict(api, usages)
24-
select api.getInfo() as info, usages order by usages desc
26+
from string info, int usages
27+
where Results<getRelevantUsages/2>::restrict(info, usages)
28+
select info, usages order by usages desc

0 commit comments

Comments
 (0)