Skip to content

Commit 6a7092d

Browse files
committed
C#: Make a parameterized module for model printing and adjust the model generator printing to the new provenance.
1 parent dab4a61 commit 6a7092d

File tree

4 files changed

+93
-59
lines changed

4 files changed

+93
-59
lines changed

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

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
*/
55

66
private import CaptureModelsSpecific
7+
private import CaptureModelsPrinting
78

89
class DataFlowTargetApi extends TargetApiSpecific {
910
DataFlowTargetApi() { isRelevantForDataFlowModels(this) }
1011
}
1112

13+
private module Printing implements PrintingSig {
14+
class Api = DataFlowTargetApi;
15+
16+
string getProvenance() { result = "df-generated" }
17+
}
18+
19+
module ModelPrinting = PrintingImpl<Printing>;
20+
1221
/**
1322
* Holds if data can flow from `node1` to `node2` either via a read or a write of an intermediate field `f`.
1423
*/
@@ -52,58 +61,6 @@ string parameterNodeAsInput(DataFlow::ParameterNode p) {
5261
*/
5362
string asInputArgument(DataFlow::Node source) { result = asInputArgumentSpecific(source) }
5463

55-
/**
56-
* Gets the summary model for `api` with `input`, `output` and `kind`.
57-
*/
58-
bindingset[input, output, kind]
59-
private string asSummaryModel(TargetApiSpecific api, string input, string output, string kind) {
60-
result =
61-
asPartialModel(api) + input + ";" //
62-
+ output + ";" //
63-
+ kind + ";" //
64-
+ "generated"
65-
}
66-
67-
string asNeutralModel(TargetApiSpecific api) { result = asPartialNeutralModel(api) + "generated" }
68-
69-
/**
70-
* Gets the value summary model for `api` with `input` and `output`.
71-
*/
72-
bindingset[input, output]
73-
string asValueModel(TargetApiSpecific api, string input, string output) {
74-
result = asSummaryModel(api, input, output, "value")
75-
}
76-
77-
/**
78-
* Gets the taint summary model for `api` with `input` and `output`.
79-
*/
80-
bindingset[input, output]
81-
private string asTaintModel(TargetApiSpecific api, string input, string output) {
82-
result = asSummaryModel(api, input, output, "taint")
83-
}
84-
85-
/**
86-
* Gets the sink model for `api` with `input` and `kind`.
87-
*/
88-
bindingset[input, kind]
89-
private string asSinkModel(TargetApiSpecific api, string input, string kind) {
90-
result =
91-
asPartialModel(api) + input + ";" //
92-
+ kind + ";" //
93-
+ "generated"
94-
}
95-
96-
/**
97-
* Gets the source model for `api` with `output` and `kind`.
98-
*/
99-
bindingset[output, kind]
100-
private string asSourceModel(TargetApiSpecific api, string output, string kind) {
101-
result =
102-
asPartialModel(api) + output + ";" //
103-
+ kind + ";" //
104-
+ "generated"
105-
}
106-
10764
/**
10865
* Gets the summary model of `api`, if it follows the `fluent` programming pattern (returns `this`).
10966
*/
@@ -112,7 +69,7 @@ string captureQualifierFlow(TargetApiSpecific api) {
11269
api = returnNodeEnclosingCallable(ret) and
11370
isOwnInstanceAccessNode(ret)
11471
) and
115-
result = asValueModel(api, qualifierString(), "ReturnValue")
72+
result = ModelPrinting::asValueModel(api, qualifierString(), "ReturnValue")
11673
}
11774

11875
private int accessPathLimit() { result = 2 }
@@ -225,7 +182,7 @@ string captureThroughFlow(DataFlowTargetApi api) {
225182
input = parameterNodeAsInput(p) and
226183
output = returnNodeAsOutput(returnNodeExt) and
227184
input != output and
228-
result = asTaintModel(api, input, output)
185+
result = ModelPrinting::asTaintModel(api, input, output)
229186
)
230187
}
231188

@@ -264,7 +221,7 @@ string captureSource(DataFlowTargetApi api) {
264221
ExternalFlow::sourceNode(source, kind) and
265222
api = sink.getEnclosingCallable() and
266223
isRelevantSourceKind(kind) and
267-
result = asSourceModel(api, returnNodeAsOutput(sink), kind)
224+
result = ModelPrinting::asSourceModel(api, returnNodeAsOutput(sink), kind)
268225
)
269226
}
270227

@@ -296,6 +253,6 @@ string captureSink(DataFlowTargetApi api) {
296253
ExternalFlow::sinkNode(sink, kind) and
297254
api = src.getEnclosingCallable() and
298255
isRelevantSinkKind(kind) and
299-
result = asSinkModel(api, asInputArgument(src), kind)
256+
result = ModelPrinting::asSinkModel(api, asInputArgument(src), kind)
300257
)
301258
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
private import CaptureModelsSpecific
2+
3+
signature module PrintingSig {
4+
/**
5+
* The class of APIs relevant for model generation.
6+
*/
7+
class Api extends TargetApiSpecific;
8+
9+
/**
10+
* Gets the string representation of the provenance of the models.
11+
*/
12+
string getProvenance();
13+
}
14+
15+
module PrintingImpl<PrintingSig Printing> {
16+
/**
17+
* Gets the summary model for `api` with `input`, `output` and `kind`.
18+
*/
19+
bindingset[input, output, kind]
20+
private string asSummaryModel(Printing::Api api, string input, string output, string kind) {
21+
result =
22+
asPartialModel(api) + input + ";" //
23+
+ output + ";" //
24+
+ kind + ";" //
25+
+ Printing::getProvenance()
26+
}
27+
28+
string asNeutralModel(Printing::Api api) {
29+
result = asPartialNeutralModel(api) + Printing::getProvenance()
30+
}
31+
32+
/**
33+
* Gets the value summary model for `api` with `input` and `output`.
34+
*/
35+
bindingset[input, output]
36+
string asValueModel(Printing::Api api, string input, string output) {
37+
result = asSummaryModel(api, input, output, "value")
38+
}
39+
40+
/**
41+
* Gets the taint summary model for `api` with `input` and `output`.
42+
*/
43+
bindingset[input, output]
44+
string asTaintModel(Printing::Api api, string input, string output) {
45+
result = asSummaryModel(api, input, output, "taint")
46+
}
47+
48+
/**
49+
* Gets the sink model for `api` with `input` and `kind`.
50+
*/
51+
bindingset[input, kind]
52+
string asSinkModel(Printing::Api api, string input, string kind) {
53+
result =
54+
asPartialModel(api) + input + ";" //
55+
+ kind + ";" //
56+
+ Printing::getProvenance()
57+
}
58+
59+
/**
60+
* Gets the source model for `api` with `output` and `kind`.
61+
*/
62+
bindingset[output, kind]
63+
string asSourceModel(Printing::Api api, string output, string kind) {
64+
result =
65+
asPartialModel(api) + output + ";" //
66+
+ kind + ";" //
67+
+ Printing::getProvenance()
68+
}
69+
}

csharp/ql/src/utils/modelgenerator/internal/CaptureSummaryFlowQuery.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ string captureFlow(DataFlowTargetApi api) {
8686
*/
8787
string captureNoFlow(DataFlowTargetApi api) {
8888
not exists(captureFlow(api)) and
89-
result = asNeutralModel(api)
89+
result = ModelPrinting::asNeutralModel(api)
9090
}

csharp/ql/src/utils/modelgenerator/internal/CaptureTypeBasedSummaryModels.qll

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ private import semmle.code.csharp.frameworks.system.collections.Generic as Gener
44
private import semmle.code.csharp.dataflow.internal.DataFlowPrivate
55
private import semmle.code.csharp.frameworks.system.linq.Expressions
66
private import CaptureModelsSpecific as Specific
7-
private import CaptureModels
7+
private import CaptureModelsPrinting
88

99
/**
1010
* Holds if `t` is a subtype (reflexive/transitive) of `IEnumerable<T>`, where `T` = `tp`.
@@ -178,6 +178,14 @@ private predicate output(DotNet::Callable callable, TypeParameter tp, string out
178178
delegateSink(callable, tp, output)
179179
}
180180

181+
private module Printing implements PrintingSig {
182+
class Api = TypeBasedFlowTargetApi;
183+
184+
string getProvenance() { result = "tb-generated" }
185+
}
186+
187+
private module ModelPrinting = PrintingImpl<Printing>;
188+
181189
/**
182190
* A class of callables that are relevant generating summaries for based
183191
* on the Theorems for Free approach.
@@ -214,7 +222,7 @@ class TypeBasedFlowTargetApi extends Specific::TargetApiSpecific {
214222
output(this, tp, output) and
215223
input != output
216224
|
217-
result = asValueModel(this, input, output)
225+
result = ModelPrinting::asValueModel(this, input, output)
218226
)
219227
}
220228
}

0 commit comments

Comments
 (0)