Skip to content

Commit 23ee033

Browse files
committed
C#: Review fixes
1 parent df6d86b commit 23ee033

File tree

13 files changed

+97
-77
lines changed

13 files changed

+97
-77
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import csharp
44
private import dotnet
5-
private import internal.FlowSummaryImplSpecific as FlowSummaryImplSpecific
65
private import internal.FlowSummaryImpl as Impl
76
private import internal.DataFlowDispatch as DataFlowDispatch
87

@@ -69,7 +68,7 @@ module SummaryComponent {
6968
SummaryComponent jump(Callable c) {
7069
result =
7170
return(any(DataFlowDispatch::JumpReturnKind jrk |
72-
jrk.getTarget().getUnderlyingCallable() = c.getUnboundDeclaration() and
71+
jrk.getTarget() = c.getUnboundDeclaration() and
7372
jrk.getTargetReturnKind() instanceof DataFlowDispatch::NormalReturnKind
7473
))
7574
}
@@ -161,13 +160,6 @@ abstract class SummarizedCallable extends DotNet::Callable {
161160
predicate isAutoGenerated() { none() }
162161
}
163162

164-
private class SourceSinkSummarizedCallable extends SummarizedCallable {
165-
SourceSinkSummarizedCallable() {
166-
FlowSummaryImplSpecific::sourceElement(this, _, _, _) or
167-
FlowSummaryImplSpecific::sinkElement(this, _, _, _)
168-
}
169-
}
170-
171163
private class SummarizedCallableAdapter extends Impl::Public::SummarizedCallable {
172164
private SummarizedCallable sc;
173165

@@ -209,8 +201,10 @@ private class RecordConstructorFlow extends SummarizedCallable {
209201
}
210202
}
211203

212-
private class SummarizedCallableDefaultClearsContent extends SummarizedCallable {
213-
SummarizedCallableDefaultClearsContent() { this instanceof SummarizedCallable or none() }
204+
private class SummarizedCallableDefaultClearsContent extends Impl::Public::SummarizedCallable {
205+
SummarizedCallableDefaultClearsContent() {
206+
this instanceof Impl::Public::SummarizedCallable or none()
207+
}
214208

215209
// By default, we assume that all stores into arguments are definite
216210
override predicate clearsContent(ParameterPosition pos, DataFlow::ContentSet content) {

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

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,27 @@ newtype TReturnKind =
7070
v = def.getSourceVariable().getAssignable()
7171
)
7272
} or
73-
TJumpReturnKind(DataFlowCallable target, ReturnKind rk) {
74-
rk instanceof NormalReturnKind and
73+
TJumpReturnKind(Callable target, ReturnKind rk) {
74+
target.isUnboundDeclaration() and
7575
(
76-
target.getUnderlyingCallable() instanceof Constructor or
77-
not target.getUnderlyingCallable().getReturnType() instanceof VoidType
76+
rk instanceof NormalReturnKind and
77+
(
78+
target instanceof Constructor or
79+
not target.getReturnType() instanceof VoidType
80+
)
81+
or
82+
exists(target.getParameter(rk.(OutRefReturnKind).getPosition()))
7883
)
79-
or
80-
exists(target.getUnderlyingCallable().getParameter(rk.(OutRefReturnKind).getPosition()))
8184
}
8285

8386
private module Cached {
87+
cached
88+
newtype TDataFlowCallable =
89+
TDotNetCallable(DotNet::Callable c) {
90+
c.isUnboundDeclaration() and not c instanceof FlowSummary::SummarizedCallable
91+
} or
92+
TSummarizedCallable(FlowSummary::SummarizedCallable c)
93+
8494
cached
8595
newtype TDataFlowCall =
8696
TNonDelegateCall(ControlFlow::Nodes::ElementNode cfn, DispatchCall dc) {
@@ -222,44 +232,37 @@ class ImplicitCapturedReturnKind extends ReturnKind, TImplicitCapturedReturnKind
222232
* one API entry point and out of another.
223233
*/
224234
class JumpReturnKind extends ReturnKind, TJumpReturnKind {
225-
private DataFlowCallable target;
235+
private Callable target;
226236
private ReturnKind rk;
227237

228238
JumpReturnKind() { this = TJumpReturnKind(target, rk) }
229239

230240
/** Gets the target of the jump. */
231-
DataFlowCallable getTarget() { result = target }
241+
Callable getTarget() { result = target }
232242

233243
/** Gets the return kind of the target. */
234244
ReturnKind getTargetReturnKind() { result = rk }
235245

236246
override string toString() { result = "jump to " + target }
237247
}
238248

239-
/**
240-
* A type for modeling dataflow callables.
241-
*/
242-
newtype TDataFlowCallable =
243-
TDotNetCallable(DotNet::Callable c) {
244-
c.isUnboundDeclaration() and not c instanceof FlowSummary::SummarizedCallable
245-
} or
246-
TSummarizedCallable(FlowSummary::SummarizedCallable c)
247-
249+
/** A callable used for data flow. */
248250
class DataFlowCallable extends TDataFlowCallable {
249251
/** Get the underlying source code callable, if any. */
250252
DotNet::Callable asCallable() { this = TDotNetCallable(result) }
251253

252254
/** Get the underlying summarized callable, if any. */
253255
FlowSummary::SummarizedCallable asSummarizedCallable() { this = TSummarizedCallable(result) }
254256

257+
/** Get the underlying callable. */
255258
DotNet::Callable getUnderlyingCallable() {
256259
result = this.asCallable() or result = this.asSummarizedCallable()
257260
}
258261

259262
/** Gets a textual representation of this dataflow callable. */
260263
string toString() { result = this.getUnderlyingCallable().toString() }
261264

262-
/** Get the location of this dataflow callable, if any. */
265+
/** Get the location of this dataflow callable. */
263266
Location getLocation() { result = this.getUnderlyingCallable().getLocation() }
264267
}
265268

@@ -320,13 +323,23 @@ class NonDelegateDataFlowCall extends DataFlowCall, TNonDelegateCall {
320323
override DataFlowCallable getARuntimeTarget() {
321324
result.asCallable() = getCallableForDataFlow(dc.getADynamicTarget())
322325
or
323-
exists(Callable c | result.asSummarizedCallable() = c.getUnboundDeclaration() |
324-
c = dc.getADynamicTarget()
326+
exists(Callable c, boolean static |
327+
result.asSummarizedCallable() = c and
328+
c = this.getATarget(static)
329+
|
330+
static = false
325331
or
326-
c = dc.getAStaticTarget() and not c instanceof RuntimeCallable
332+
static = true and not c instanceof RuntimeCallable
327333
)
328334
}
329335

336+
/** Gets a static or dynamic target of this call. */
337+
Callable getATarget(boolean static) {
338+
result = dc.getADynamicTarget().getUnboundDeclaration() and static = false
339+
or
340+
result = dc.getAStaticTarget().getUnboundDeclaration() and static = true
341+
}
342+
330343
override ControlFlow::Nodes::ElementNode getControlFlowNode() { result = cfn }
331344

332345
override DataFlow::ExprNode getNode() { result.getControlFlowNode() = cfn }

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,11 @@ private module Cached {
694694
AssignableDefinitions::ImplicitParameterDefinition
695695
} or
696696
TExplicitParameterNode(DotNet::Parameter p) {
697-
p = any(DataFlowCallable c).asCallable().getAParameter()
697+
p = any(DataFlowCallable dfc).asCallable().getAParameter()
698698
} or
699699
TInstanceParameterNode(Callable c) {
700-
c.isUnboundDeclaration() and not c.(Modifiable).isStatic()
700+
c = any(DataFlowCallable dfc).asCallable() and
701+
not c.(Modifiable).isStatic()
701702
} or
702703
TYieldReturnNode(ControlFlow::Nodes::ElementNode cfn) {
703704
any(Callable c).canYieldReturn(cfn.getElement())
@@ -911,7 +912,7 @@ private module ParameterNodes {
911912
Callable getCallable() { result = callable }
912913

913914
override predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) {
914-
callable = c.getUnderlyingCallable() and pos.isThisParameter()
915+
callable = c.asCallable() and pos.isThisParameter()
915916
}
916917

917918
override DataFlowCallable getEnclosingCallableImpl() {
@@ -1554,9 +1555,9 @@ predicate jumpStep(Node pred, Node succ) {
15541555
flr.hasNonlocalValue()
15551556
)
15561557
or
1557-
exists(JumpReturnKind jrk, DataFlowCall call |
1558+
exists(JumpReturnKind jrk, NonDelegateDataFlowCall call |
15581559
FlowSummaryImpl::Private::summaryReturnNode(pred, jrk) and
1559-
viableCallable(call) = jrk.getTarget() and
1560+
jrk.getTarget() = call.getATarget(_) and
15601561
succ = getAnOutNode(call, jrk.getTargetReturnKind())
15611562
)
15621563
}

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ private import FlowSummaryImplSpecific
1010
private import DataFlowImplSpecific::Private
1111
private import DataFlowImplSpecific::Public
1212
private import DataFlowImplCommon
13-
private import semmle.code.csharp.dataflow.FlowSummary as FlowSummary
1413

1514
/** Provides classes and predicates for defining flow summaries. */
1615
module Public {
@@ -910,6 +909,33 @@ module Private {
910909
}
911910
}
912911

912+
private class SummarizedCallableExternal extends SummarizedCallable {
913+
SummarizedCallableExternal() { summaryElement(this, _, _, _, _) }
914+
915+
private predicate relevantSummaryElement(AccessPath inSpec, AccessPath outSpec, string kind) {
916+
summaryElement(this, inSpec, outSpec, kind, false)
917+
or
918+
summaryElement(this, inSpec, outSpec, kind, true) and
919+
not summaryElement(this, _, _, _, false)
920+
}
921+
922+
override predicate propagatesFlow(
923+
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
924+
) {
925+
exists(AccessPath inSpec, AccessPath outSpec, string kind |
926+
this.relevantSummaryElement(inSpec, outSpec, kind) and
927+
interpretSpec(inSpec, input) and
928+
interpretSpec(outSpec, output)
929+
|
930+
kind = "value" and preservesValue = true
931+
or
932+
kind = "taint" and preservesValue = false
933+
)
934+
}
935+
936+
override predicate isAutoGenerated() { summaryElement(this, _, _, _, true) }
937+
}
938+
913939
/** Holds if component `c` of specification `spec` cannot be parsed. */
914940
predicate invalidSpecComponent(AccessPath spec, string c) {
915941
c = spec.getToken(_) and
@@ -1043,7 +1069,7 @@ module Private {
10431069
/** Provides a query predicate for outputting a set of relevant flow summaries. */
10441070
module TestOutput {
10451071
/** A flow summary to include in the `summary/3` query predicate. */
1046-
abstract class RelevantSummarizedCallable extends FlowSummary::SummarizedCallable {
1072+
abstract class RelevantSummarizedCallable extends SummarizedCallable {
10471073
/** Gets the string representation of this callable used by `summary/1`. */
10481074
abstract string getCallableCsv();
10491075

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

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ DataFlowType getReturnType(SummarizedCallable c, ReturnKind rk) {
5858
result = getReturnTypeBase(c.asSummarizedCallable(), rk)
5959
or
6060
rk =
61-
any(JumpReturnKind jrk |
62-
result = getReturnTypeBase(jrk.getTarget().getUnderlyingCallable(), jrk.getTargetReturnKind())
63-
)
61+
any(JumpReturnKind jrk | result = getReturnTypeBase(jrk.getTarget(), jrk.getTargetReturnKind()))
6462
}
6563

6664
/**
@@ -100,29 +98,6 @@ private predicate summaryElement0(
10098

10199
private class SummarizedCallableExternal extends FlowSummary::SummarizedCallable {
102100
SummarizedCallableExternal() { summaryElement0(this, _, _, _, _) }
103-
104-
private predicate relevantSummaryElement(AccessPath inSpec, AccessPath outSpec, string kind) {
105-
summaryElement0(this, inSpec, outSpec, kind, false)
106-
or
107-
summaryElement0(this, inSpec, outSpec, kind, true) and
108-
not summaryElement0(this, _, _, _, false)
109-
}
110-
111-
override predicate propagatesFlow(
112-
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
113-
) {
114-
exists(AccessPath inSpec, AccessPath outSpec, string kind |
115-
this.relevantSummaryElement(inSpec, outSpec, kind) and
116-
External::interpretSpec(inSpec, input) and
117-
External::interpretSpec(outSpec, output)
118-
|
119-
kind = "value" and preservesValue = true
120-
or
121-
kind = "taint" and preservesValue = false
122-
)
123-
}
124-
125-
override predicate isAutoGenerated() { summaryElement0(this, _, _, _, true) }
126101
}
127102

128103
/**
@@ -259,7 +234,7 @@ class InterpretNode extends TInterpretNode {
259234
DataFlowCallable asCallable() { result.getUnderlyingCallable() = this.asElement() }
260235

261236
/** Gets the target of this call, if any. */
262-
Callable getCallTarget() { result = viableCallable(this.asCall()).getUnderlyingCallable() }
237+
Callable getCallTarget() { result = this.asCall().(NonDelegateDataFlowCall).getATarget(_) }
263238

264239
/** Gets a textual representation of this node. */
265240
string toString() {

csharp/ql/test/library-tests/dataflow/collections/CollectionFlow.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Conf extends DataFlow::Configuration {
1717
)
1818
}
1919

20-
override int fieldFlowBranchLimit() { result = 10 }
20+
override int fieldFlowBranchLimit() { result = 100 }
2121
}
2222

2323
from DataFlow::PathNode source, DataFlow::PathNode sink, Conf conf

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ query predicate summarySetterStep(DataFlow::Node arg, DataFlow::Node out, Conten
4242
FlowSummaryImpl::Private::Steps::summarySetterStep(arg, c, out)
4343
}
4444

45-
query predicate clearsContent(SummarizedCallable c, DataFlow::Content k, ParameterPosition pos) {
45+
query predicate clearsContent(
46+
FlowSummaryImpl::Public::SummarizedCallable c, DataFlow::Content k, ParameterPosition pos
47+
) {
4648
c.clearsContent(pos, k) and
47-
c.fromSource()
49+
c.asSummarizedCallable().fromSource()
4850
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import shared.FlowSummaries
22

33
private class IncludeAllSummarizedCallable extends IncludeSummarizedCallable {
4-
IncludeAllSummarizedCallable() { this instanceof SummarizedCallable }
4+
IncludeAllSummarizedCallable() { exists(this) }
55
}

csharp/ql/test/library-tests/dataflow/library/FlowSummariesFiltered.ql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ private import semmle.code.csharp.dataflow.internal.DataFlowPrivate::Csv
33
private import semmle.code.csharp.dataflow.ExternalFlow
44

55
class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable {
6-
IncludeFilteredSummarizedCallable() { this instanceof SummarizedCallable }
6+
IncludeFilteredSummarizedCallable() { exists(this) }
77

88
/**
99
* Holds if flow is propagated between `input` and `output` and
@@ -14,10 +14,11 @@ class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable {
1414
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
1515
) {
1616
this.propagatesFlow(input, output, preservesValue) and
17-
not exists(IncludeSummarizedCallable rsc |
18-
isBaseCallableOrPrototype(rsc) and
17+
not exists(IncludeSummarizedCallable rsc, SummarizedCallable sc |
18+
sc = rsc.asSummarizedCallable() and
19+
isBaseCallableOrPrototype(sc) and
1920
rsc.propagatesFlow(input, output, preservesValue) and
20-
this.(UnboundCallable).overridesOrImplementsUnbound(rsc)
21+
this.asSummarizedCallable().(UnboundCallable).overridesOrImplementsUnbound(sc)
2122
)
2223
}
2324
}

csharp/ql/test/library-tests/frameworks/EntityFramework/FlowSummaries.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import shared.FlowSummaries
33
import semmle.code.csharp.dataflow.ExternalFlow as ExternalFlow
44

55
private class IncludeEFSummarizedCallable extends IncludeSummarizedCallable {
6-
IncludeEFSummarizedCallable() { this instanceof EFSummarizedCallable }
6+
IncludeEFSummarizedCallable() { this.asSummarizedCallable() instanceof EFSummarizedCallable }
77
}
88

99
query predicate sourceNode(DataFlow::Node node, string kind) {

0 commit comments

Comments
 (0)