Skip to content

Commit b7bc540

Browse files
committed
C#: Adjust implementation to use the shared model printer.
1 parent 65e150b commit b7bc540

File tree

7 files changed

+22
-100
lines changed

7 files changed

+22
-100
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ private predicate callableInfo(Callable c, string name, UnboundValueOrRefType de
376376
private class InterpretedCallable extends Callable {
377377
InterpretedCallable() {
378378
exists(string namespace, string type, string name |
379-
partialModel(this, namespace, type, name, _) and
379+
partialModel(this, namespace, type, _, name, _) and
380380
elementSpec(namespace, type, _, name, _, _)
381381
)
382382
or
@@ -520,32 +520,19 @@ string parameterQualifiedTypeNamesToString(Callable c) {
520520
}
521521

522522
predicate partialModel(
523-
UnboundCallable c, string namespace, string type, string name, string parameters
523+
Callable c, string namespace, string type, string extensible, string name, string parameters
524524
) {
525525
QN::hasQualifiedName(c, namespace, type, name) and
526+
extensible = getCallableOverride(c) and
526527
parameters = "(" + parameterQualifiedTypeNamesToString(c) + ")"
527528
}
528529

529-
/** Computes the first 6 columns for positive CSV rows of `c`. */
530-
string asPartialModel(UnboundCallable c) {
531-
exists(string namespace, string type, string name, string parameters |
532-
partialModel(c, namespace, type, name, parameters) and
533-
result =
534-
namespace + ";" //
535-
+ type + ";" //
536-
+ getCallableOverride(c) + ";" //
537-
+ name + ";" //
538-
+ parameters + ";" //
539-
+ /* ext + */ ";" //
540-
)
541-
}
542-
543530
/**
544531
* Gets the signature of `c` in the format `namespace;type;name;parameters`.
545532
*/
546533
string getSignature(UnboundCallable c) {
547534
exists(string namespace, string type, string name, string parameters |
548-
partialModel(c, namespace, type, name, parameters)
535+
partialModel(c, namespace, type, _, name, parameters)
549536
|
550537
result =
551538
namespace + ";" //

csharp/ql/src/utils/modelconverter/InterpretModel.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ predicate interpretCallable(
88
) {
99
exists(Callable c, string signature1 |
1010
c = interpretBaseDeclaration(namespace0, type0, name0, signature0) and
11-
partialModel(c, namespace, type, name, signature1) and
11+
partialModel(c, namespace, type, _, name, signature1) and
1212
if signature0 = "" then signature = "" else signature = signature1
1313
)
1414
or

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class DataFlowTargetApi extends TargetApiSpecific {
3333
DataFlowTargetApi() { not isUninterestingForDataFlowModels(this) }
3434
}
3535

36-
private module Printing implements PrintingSig {
36+
private module ModelPrintingInput implements ModelPrintingSig {
3737
class Api = DataFlowTargetApi;
3838

3939
string getProvenance() { result = "df-generated" }
4040
}
4141

42-
module ModelPrinting = PrintingImpl<Printing>;
42+
module Printing = ModelPrinting<ModelPrintingInput>;
4343

4444
/**
4545
* Holds if `c` is a relevant content kind, where the underlying type is relevant.
@@ -94,7 +94,7 @@ string captureQualifierFlow(TargetApiSpecific api) {
9494
api = returnNodeEnclosingCallable(ret) and
9595
isOwnInstanceAccessNode(ret)
9696
) and
97-
result = ModelPrinting::asValueModel(api, qualifierString(), "ReturnValue")
97+
result = Printing::asValueModel(api, qualifierString(), "ReturnValue")
9898
}
9999

100100
private int accessPathLimit0() { result = 2 }
@@ -202,7 +202,7 @@ string captureThroughFlow(DataFlowTargetApi api) {
202202
input = parameterNodeAsInput(p) and
203203
output = returnNodeExt.getOutput() and
204204
input != output and
205-
result = ModelPrinting::asTaintModel(api, input, output)
205+
result = Printing::asTaintModel(api, input, output)
206206
)
207207
}
208208

@@ -250,7 +250,7 @@ string captureSource(DataFlowTargetApi api) {
250250
ExternalFlow::sourceNode(source, kind) and
251251
api = sink.getEnclosingCallable() and
252252
not irrelevantSourceSinkApi(source.getEnclosingCallable(), api) and
253-
result = ModelPrinting::asSourceModel(api, sink.getOutput(), kind)
253+
result = Printing::asSourceModel(api, sink.getOutput(), kind)
254254
)
255255
}
256256

@@ -291,6 +291,6 @@ string captureSink(DataFlowTargetApi api) {
291291
PropagateToSink::flow(src, sink) and
292292
ExternalFlow::sinkNode(sink, kind) and
293293
api = src.getEnclosingCallable() and
294-
result = ModelPrinting::asSinkModel(api, asInputArgument(src), kind)
294+
result = Printing::asSinkModel(api, asInputArgument(src), kind)
295295
)
296296
}
Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,11 @@
1-
private import CaptureModelsSpecific
1+
private import csharp as CS
2+
private import codeql.mad.modelgenerator.ModelPrinting
3+
private import semmle.code.csharp.dataflow.internal.ExternalFlow as ExternalFlow
24

3-
signature module PrintingSig {
4-
/**
5-
* The class of APIs relevant for model generation.
6-
*/
7-
class Api extends TargetApiSpecific;
5+
private module ModelPrintingLang implements ModelPrintingLangSig {
6+
class Callable = CS::Callable;
87

9-
/**
10-
* Gets the string representation of the provenance of the models.
11-
*/
12-
string getProvenance();
8+
predicate partialModel = ExternalFlow::partialModel/6;
139
}
1410

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 asNeutralSummaryModel(Printing::Api api) {
29-
result =
30-
asPartialNeutralModel(api) //
31-
+ "summary" + ";" //
32-
+ Printing::getProvenance()
33-
}
34-
35-
/**
36-
* Gets the value summary model for `api` with `input` and `output`.
37-
*/
38-
bindingset[input, output]
39-
string asValueModel(Printing::Api api, string input, string output) {
40-
result = asSummaryModel(api, input, output, "value")
41-
}
42-
43-
/**
44-
* Gets the taint summary model for `api` with `input` and `output`.
45-
*/
46-
bindingset[input, output]
47-
string asTaintModel(Printing::Api api, string input, string output) {
48-
result = asSummaryModel(api, input, output, "taint")
49-
}
50-
51-
/**
52-
* Gets the sink model for `api` with `input` and `kind`.
53-
*/
54-
bindingset[input, kind]
55-
string asSinkModel(Printing::Api api, string input, string kind) {
56-
result =
57-
asPartialModel(api) + input + ";" //
58-
+ kind + ";" //
59-
+ Printing::getProvenance()
60-
}
61-
62-
/**
63-
* Gets the source model for `api` with `output` and `kind`.
64-
*/
65-
bindingset[output, kind]
66-
string asSourceModel(Printing::Api api, string output, string kind) {
67-
result =
68-
asPartialModel(api) + output + ";" //
69-
+ kind + ";" //
70-
+ Printing::getProvenance()
71-
}
72-
}
11+
import ModelPrintingImpl<ModelPrintingLang>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ class TargetApiSpecific extends Callable {
130130
predicate isRelevant() { relevant(this) }
131131
}
132132

133-
string asPartialModel(TargetApiSpecific api) { result = ExternalFlow::asPartialModel(api.lift()) }
134-
135-
string asPartialNeutralModel(TargetApiSpecific api) { result = ExternalFlow::getSignature(api) }
136-
137133
/**
138134
* Holds if `t` is a type that is generally used for bulk data in collection types.
139135
* Eg. char[] is roughly equivalent to string and thus a highly

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,5 @@ string captureFlow(DataFlowTargetApi api) {
8989
string captureNoFlow(DataFlowTargetApi api) {
9090
not exists(DataFlowTargetApi api0 | exists(captureFlow(api0)) and api0.lift() = api.lift()) and
9191
api.isRelevant() and
92-
result = ModelPrinting::asNeutralSummaryModel(api)
92+
result = Printing::asNeutralSummaryModel(api)
9393
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ private predicate output(Callable callable, TypeParameter tp, string output) {
177177
delegateSink(callable, tp, output)
178178
}
179179

180-
private module Printing implements PrintingSig {
180+
private module ModelPrintingInput implements ModelPrintingSig {
181181
class Api = TypeBasedFlowTargetApi;
182182

183183
string getProvenance() { result = "tb-generated" }
184184
}
185185

186-
private module ModelPrinting = PrintingImpl<Printing>;
186+
private module Printing = ModelPrinting<ModelPrintingInput>;
187187

188188
/**
189189
* A class of callables that are relevant generating summaries for based
@@ -221,7 +221,7 @@ class TypeBasedFlowTargetApi extends Specific::TargetApiSpecific {
221221
output(this, tp, output) and
222222
input != output
223223
|
224-
result = ModelPrinting::asValueModel(this, input, output)
224+
result = Printing::asValueModel(this, input, output)
225225
)
226226
}
227227
}

0 commit comments

Comments
 (0)