Skip to content

Commit c82ab68

Browse files
authored
Merge pull request #9256 from michaelnebel/csharp/test-ranking
C#: Rank summaries and source code in dataflow callables.
2 parents f221894 + f141336 commit c82ab68

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,26 @@ newtype TReturnKind =
8383
)
8484
}
8585

86+
/**
87+
* Holds if the summary for `c` should be used for dataflow analysis.
88+
*/
89+
predicate useFlowSummary(FlowSummary::SummarizedCallable c) {
90+
not c.fromSource()
91+
or
92+
c.fromSource() and not c.isAutoGenerated()
93+
}
94+
8695
private module Cached {
96+
/**
97+
* The following heuristic is used to rank when to use source code or when to use summaries for DataFlowCallables.
98+
* 1. Use hand written summaries.
99+
* 2. Use source code.
100+
* 3. Use auto generated summaries.
101+
*/
87102
cached
88103
newtype TDataFlowCallable =
89-
TDotNetCallable(DotNet::Callable c) {
90-
c.isUnboundDeclaration() and not c instanceof FlowSummary::SummarizedCallable
91-
} or
92-
TSummarizedCallable(FlowSummary::SummarizedCallable c)
104+
TDotNetCallable(DotNet::Callable c) { c.isUnboundDeclaration() and not useFlowSummary(c) } or
105+
TSummarizedCallable(FlowSummary::SummarizedCallable c) { useFlowSummary(c) }
93106

94107
cached
95108
newtype TDataFlowCall =

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,11 @@ private module Cached {
743743
FlowSummaryImpl::Public::SummarizedCallable c,
744744
FlowSummaryImpl::Private::SummaryNodeState state
745745
) {
746+
useFlowSummary(c) and
746747
FlowSummaryImpl::Private::summaryNodeRange(c, state)
747748
} or
748749
TSummaryParameterNode(FlowSummaryImpl::Public::SummarizedCallable c, ParameterPosition pos) {
750+
useFlowSummary(c) and
749751
FlowSummaryImpl::Private::summaryParameterNodeRange(c, pos)
750752
} or
751753
TParamsArgumentNode(ControlFlow::Node callCfn) {

csharp/ql/test/library-tests/dataflow/external-models/ExternalFlow.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class Conf extends TaintTracking::Configuration {
5353
}
5454
}
5555

56+
/**
57+
* Simulate that methods with summaries are not included in the source code.
58+
* This is relevant for dataflow analysis using summaries tagged as generated.
59+
*/
60+
private class MyMethod extends Method {
61+
override predicate fromSource() { none() }
62+
}
63+
5664
from DataFlow::PathNode source, DataFlow::PathNode sink, Conf conf
5765
where conf.hasFlowPath(source, sink)
5866
select sink, source, sink, "$@", source, source.toString()

0 commit comments

Comments
 (0)