Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f25e44b

Browse files
committedOct 11, 2024·
Fixup of prototype.
1 parent a765bf7 commit f25e44b

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed
 

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

+8-17
Original file line numberDiff line numberDiff line change
@@ -2282,16 +2282,7 @@ private predicate recordProperty(RecordType t, ContentSet c, string name) {
22822282
)
22832283
}
22842284

2285-
// node2 needs to be repsentation of the callable. In this case the parameter itself.
2286-
// node1 needs to be the argument position in the call.
2287-
// private predicate storeStepDelegateCall(Node node1, ContentSet c, Node node2) {
2288-
// exists(DelegateCall call, Parameter p, int i |
2289-
// node1.asExpr() = call.getArgument(i) and
2290-
// node2.asExpr() = call.getExpr() and
2291-
// call.getExpr() = p.getAnAccess() and
2292-
// c.isDelegateParameter(p, i)
2293-
// )
2294-
// }
2285+
// TODO: Make comment.
22952286
private predicate storeStepDelegateCall(Node node1, ContentSet c, Node node2) {
22962287
exists(DelegateCall call, Parameter p, int i |
22972288
node1.asExpr() = call.getArgument(i) and
@@ -2456,14 +2447,14 @@ private predicate readContentStep(Node node1, Content c, Node node2) {
24562447
}
24572448

24582449
// TODO: Make comment.
2459-
// Consider putting this into readContentStep.
24602450
private predicate readStepDelegateCall(Node node1, ContentSet c, Node node2) {
2461-
exists(DelegateCall call, Parameter p |
2462-
node1.asExpr() = call.getExpr() and
2463-
node2.asExpr() = call and
2464-
call.getExpr() = p.getAnAccess() and
2465-
c.isDelegateParameter(p, _)
2466-
)
2451+
none()
2452+
// exists(DelegateCall call, Parameter p |
2453+
// node1.asExpr() = call.getExpr() and
2454+
// node2.asExpr() = call and
2455+
// call.getExpr() = p.getAnAccess() and
2456+
// c.isDelegateParameter(p, _)
2457+
// )
24672458
}
24682459

24692460
/**

‎csharp/ql/src/utils/modelgenerator/internal/CaptureModels.qll

+10-10
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, CsharpDat
2727
}
2828

2929
class DelegateCallNode extends CS::DataFlow::Node {
30-
DelegateCallNode() { this.asExpr() instanceof CS::DelegateCall }
31-
}
30+
private CS::DelegateCall call;
3231

33-
/**
34-
* Holds if any of the parameters of `api` are `System.Func<>`.
35-
*/
36-
private predicate isHigherOrder(Callable api) { none() }
32+
DelegateCallNode() { call = this.asExpr() }
33+
34+
int getParameterPosition() {
35+
exists(Parameter p | p.getAnAccess() = call.getExpr() | result = p.getPosition())
36+
}
37+
}
3738

3839
private predicate irrelevantAccessor(CS::Accessor a) {
3940
a.getDeclaration().(CS::Property).isReadWrite()
4041
}
4142

4243
private predicate isUninterestingForModels(Callable api) {
43-
// TODO Comment this back in.
44-
// api.getDeclaringType().getNamespace().getFullName() = ""
45-
// or
44+
api.getDeclaringType().getNamespace().getFullName() = ""
45+
or
4646
api instanceof CS::ConversionOperator
4747
or
4848
api instanceof Util::MainMethod
@@ -102,7 +102,7 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, CsharpDat
102102
api = any(FlowSummaryImpl::Public::NeutralSinkCallable sc | sc.hasManualModel())
103103
}
104104

105-
predicate isUninterestingForDataFlowModels(Callable api) { isHigherOrder(api) }
105+
predicate isUninterestingForDataFlowModels(Callable api) { none() }
106106

107107
class SourceOrSinkTargetApi extends Callable {
108108
SourceOrSinkTargetApi() { relevant(this) }

‎shared/mad/codeql/mad/modelgenerator/internal/ModelGeneratorImpl.qll

+27-14
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ signature module ModelGeneratorInputSig<LocationSig Location, InputSig<Location>
6666
* Gets the enclosing callable of this node.
6767
*/
6868
Callable getEnclosingCallable();
69+
70+
/**
71+
* Gets the parameter position of the delegate callable for this
72+
* delegate call node.
73+
*/
74+
int getParameterPosition();
6975
}
7076

7177
/**
@@ -282,8 +288,11 @@ module MakeModelGenerator<
282288
private DataFlow::ReturnKindExt kind;
283289

284290
ReturnNodeExt() {
285-
kind = DataFlow::getValueReturnPosition(this).getKind() or
286-
kind = DataFlow::getParamReturnPosition(this, _).getKind()
291+
not this instanceof DelegateCallNode and
292+
(
293+
kind = DataFlow::getValueReturnPosition(this).getKind() or
294+
kind = DataFlow::getParamReturnPosition(this, _).getKind()
295+
)
287296
}
288297

289298
/**
@@ -295,20 +304,22 @@ module MakeModelGenerator<
295304
bindingset[c]
296305
private signature string printCallableParamSig(Callable c, DataFlow::ParameterPosition p);
297306

298-
private module PrintReturnNodeExt<printCallableParamSig/2 printCallableParam> {
299-
string getOutput(ReturnNodeExt node) {
300-
node.getKind() instanceof DataFlow::ValueReturnKind and
307+
private module PrintSinkNode<printCallableParamSig/2 printCallableParam> {
308+
string getOutput(NodeExtended node) {
309+
node.(ReturnNodeExt).getKind() instanceof DataFlow::ValueReturnKind and
301310
result = "ReturnValue"
302311
or
303312
exists(DataFlow::ParameterPosition pos |
304-
pos = node.getKind().(DataFlow::ParamUpdateReturnKind).getPosition() and
313+
pos = node.(ReturnNodeExt).getKind().(DataFlow::ParamUpdateReturnKind).getPosition() and
305314
result = printCallableParam(returnNodeEnclosingCallable(node), pos)
306315
)
316+
or
317+
result = "Argument[" + node.(DelegateCallNode).getParameterPosition() + "]"
307318
}
308319
}
309320

310321
string getOutput(ReturnNodeExt node) {
311-
result = PrintReturnNodeExt<paramReturnNodeAsOutput/2>::getOutput(node)
322+
result = PrintSinkNode<paramReturnNodeAsOutput/2>::getOutput(node)
312323
}
313324

314325
final private class SummaryTargetApiFinal = SummaryTargetApi;
@@ -538,6 +549,7 @@ module MakeModelGenerator<
538549

539550
private module PropagateContentFlowConfig implements ContentDataFlow::ConfigSig {
540551
predicate isSource(DataFlow::Node source) {
552+
// TODO: We also need to consider delegate calls as source nodes.
541553
source instanceof DataFlow::ParameterNode and
542554
source.(NodeExtended).getEnclosingCallable() instanceof DataFlowSummaryTargetApi
543555
}
@@ -574,8 +586,8 @@ module MakeModelGenerator<
574586

575587
private module ContentModelPrinting = Printing::ModelPrinting<ContentModelPrintingInput>;
576588

577-
private string getContentOutput(ReturnNodeExt node) {
578-
result = PrintReturnNodeExt<paramReturnNodeAsContentOutput/2>::getOutput(node)
589+
private string getContentOutput(NodeExtended node) {
590+
result = PrintSinkNode<paramReturnNodeAsContentOutput/2>::getOutput(node)
579591
}
580592

581593
/**
@@ -615,11 +627,12 @@ module MakeModelGenerator<
615627

616628
private predicate apiFlow(
617629
DataFlowSummaryTargetApi api, DataFlow::ParameterNode p,
618-
PropagateContentFlow::AccessPath reads, ReturnNodeExt returnNodeExt,
630+
PropagateContentFlow::AccessPath reads, NodeExtended returnNodeExt,
619631
PropagateContentFlow::AccessPath stores, boolean preservesValue
620632
) {
621633
PropagateContentFlow::flow(p, reads, returnNodeExt, stores, preservesValue) and
622634
returnNodeExt.getEnclosingCallable() = api and
635+
// TODO: We also need to consider delegate calls within an API to be sources.
623636
p.(NodeExtended).getEnclosingCallable() = api
624637
}
625638

@@ -641,7 +654,7 @@ module MakeModelGenerator<
641654
ContentDataFlowSummaryTargetApi() {
642655
count(string input, string output |
643656
exists(
644-
PropagateContentFlow::AccessPath reads, ReturnNodeExt returnNodeExt,
657+
PropagateContentFlow::AccessPath reads, NodeExtended returnNodeExt,
645658
PropagateContentFlow::AccessPath stores
646659
|
647660
apiFlow(this, parameter, reads, returnNodeExt, stores, _) and
@@ -712,7 +725,7 @@ module MakeModelGenerator<
712725
Type t1, PropagateContentFlow::AccessPath read, Type t2,
713726
PropagateContentFlow::AccessPath store
714727
) {
715-
exists(DataFlow::ParameterNode p, ReturnNodeExt returnNodeExt |
728+
exists(DataFlow::ParameterNode p, NodeExtended returnNodeExt |
716729
p.(NodeExtended).getType() = t1 and
717730
returnNodeExt.getType() = t2 and
718731
apiContentFlow(_, p, read, returnNodeExt, store, _)
@@ -829,7 +842,7 @@ module MakeModelGenerator<
829842
*/
830843
private predicate apiRelevantContentFlow(
831844
ContentDataFlowSummaryTargetApi api, DataFlow::ParameterNode p,
832-
PropagateContentFlow::AccessPath read, ReturnNodeExt returnNodeExt,
845+
PropagateContentFlow::AccessPath read, NodeExtended returnNodeExt,
833846
PropagateContentFlow::AccessPath store, boolean preservesValue
834847
) {
835848
apiContentFlow(api, p, read, returnNodeExt, store, preservesValue) and
@@ -847,7 +860,7 @@ module MakeModelGenerator<
847860
boolean lift
848861
) {
849862
exists(
850-
DataFlow::ParameterNode p, ReturnNodeExt returnNodeExt,
863+
DataFlow::ParameterNode p, NodeExtended returnNodeExt,
851864
PropagateContentFlow::AccessPath reads, PropagateContentFlow::AccessPath stores
852865
|
853866
apiRelevantContentFlow(api, p, reads, returnNodeExt, stores, preservesValue) and

0 commit comments

Comments
 (0)
Please sign in to comment.