Skip to content

Commit e141b4e

Browse files
authored
Merge pull request #18612 from paldepind/shared-model-generation-row
Shared: Generalize the number of columns in a generated MaD row
2 parents ee5416f + 9d87f26 commit e141b4e

File tree

6 files changed

+76
-46
lines changed

6 files changed

+76
-46
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,29 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, CsharpDat
361361
c.isDelegateCallReturn() and result = "ReturnValue"
362362
}
363363

364-
predicate partialModel = ExternalFlow::partialModel/6;
364+
string partialModelRow(Callable api, int i) {
365+
i = 0 and ExternalFlow::partialModel(api, result, _, _, _, _) // package
366+
or
367+
i = 1 and ExternalFlow::partialModel(api, _, result, _, _, _) // type
368+
or
369+
i = 2 and ExternalFlow::partialModel(api, _, _, result, _, _) // extensible
370+
or
371+
i = 3 and ExternalFlow::partialModel(api, _, _, _, result, _) // name
372+
or
373+
i = 4 and ExternalFlow::partialModel(api, _, _, _, _, result) // parameters
374+
or
375+
i = 5 and result = "" and exists(api) // ext
376+
}
377+
378+
string partialNeutralModelRow(Callable api, int i) {
379+
i = 0 and result = partialModelRow(api, 0) // package
380+
or
381+
i = 1 and result = partialModelRow(api, 1) // type
382+
or
383+
i = 2 and result = partialModelRow(api, 3) // name
384+
or
385+
i = 3 and result = partialModelRow(api, 4) // parameters
386+
}
365387

366388
predicate sourceNode = ExternalFlow::sourceNode/2;
367389

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
private import csharp as CS
22
private import codeql.mad.modelgenerator.internal.ModelPrinting
3-
private import semmle.code.csharp.dataflow.internal.ExternalFlow as ExternalFlow
3+
private import CaptureModels::ModelGeneratorInput as ModelGeneratorInput
44

55
private module ModelPrintingLang implements ModelPrintingLangSig {
66
class Callable = CS::Callable;
77

8-
predicate partialModel = ExternalFlow::partialModel/6;
8+
predicate partialModelRow = ModelGeneratorInput::partialModelRow/2;
9+
10+
predicate partialNeutralModelRow = ModelGeneratorInput::partialNeutralModelRow/2;
911
}
1012

1113
import ModelPrintingImpl<ModelPrintingLang>

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,28 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, JavaDataF
282282
c instanceof DataFlowUtil::MapKeyContent and result = "MapKey"
283283
}
284284

285-
predicate partialModel(
286-
Callable api, string package, string type, string extensible, string name, string parameters
287-
) {
288-
qualifiedName(api, package, type) and
289-
extensible = isExtensible(api) and
290-
name = api.getName() and
291-
parameters = ExternalFlow::paramsString(api)
285+
string partialModelRow(Callable api, int i) {
286+
i = 0 and qualifiedName(api, result, _) // package
287+
or
288+
i = 1 and qualifiedName(api, _, result) // type
289+
or
290+
i = 2 and result = isExtensible(api) // extensible
291+
or
292+
i = 3 and result = api.getName() // name
293+
or
294+
i = 4 and result = ExternalFlow::paramsString(api) // parameters
295+
or
296+
i = 5 and result = "" and exists(api) // ext
297+
}
298+
299+
string partialNeutralModelRow(Callable api, int i) {
300+
i = 0 and qualifiedName(api, result, _) // package
301+
or
302+
i = 1 and qualifiedName(api, _, result) // type
303+
or
304+
i = 2 and result = api.getName() // name
305+
or
306+
i = 3 and result = ExternalFlow::paramsString(api) // parameters
292307
}
293308

294309
predicate sourceNode = ExternalFlow::sourceNode/2;

java/ql/src/utils/modelgenerator/internal/CaptureModelsPrinting.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ private import CaptureModels::ModelGeneratorInput as ModelGeneratorInput
55
private module ModelPrintingLang implements ModelPrintingLangSig {
66
class Callable = J::Callable;
77

8-
predicate partialModel = ModelGeneratorInput::partialModel/6;
8+
predicate partialModelRow = ModelGeneratorInput::partialModelRow/2;
9+
10+
predicate partialNeutralModelRow = ModelGeneratorInput::partialNeutralModelRow/2;
911
}
1012

1113
import ModelPrintingImpl<ModelPrintingLang>

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,14 @@ signature module ModelGeneratorInputSig<LocationSig Location, InputSig<Location>
238238
predicate isUninterestingForHeuristicDataFlowModels(Callable api);
239239

240240
/**
241-
* Holds if `namespace`, `type`, `extensible`, `name` and `parameters` are string representations
242-
* for the corresponding MaD columns for `api`.
241+
* Gets the string representation for the `i`th column in the MaD row for `api`.
243242
*/
244-
predicate partialModel(
245-
Callable api, string namespace, string type, string extensible, string name, string parameters
246-
);
243+
string partialModelRow(Callable api, int i);
244+
245+
/**
246+
* Gets the string representation for the `i`th column in the neutral MaD row for `api`.
247+
*/
248+
string partialNeutralModelRow(Callable api, int i);
247249

248250
/**
249251
* Holds if `node` is specified as a source with the given kind in a MaD flow
@@ -274,7 +276,9 @@ module MakeModelGenerator<
274276
private module ModelPrintingLang implements ModelPrintingLangSig {
275277
class Callable = ModelGeneratorInput::Callable;
276278

277-
predicate partialModel = ModelGeneratorInput::partialModel/6;
279+
predicate partialModelRow = ModelGeneratorInput::partialModelRow/2;
280+
281+
predicate partialNeutralModelRow = ModelGeneratorInput::partialNeutralModelRow/2;
278282
}
279283

280284
private import ModelPrintingImpl<ModelPrintingLang> as Printing
@@ -436,7 +440,7 @@ module MakeModelGenerator<
436440
}
437441

438442
/**
439-
* A data-flow configuration for tracking flow through APIs.
443+
* A data flow configuration for tracking flow through APIs.
440444
* The sources are the parameters of an API and the sinks are the return values (excluding `this`) and parameters.
441445
*
442446
* This can be used to generate Flow summaries for APIs from parameter to return.
@@ -952,7 +956,7 @@ module MakeModelGenerator<
952956
}
953957

954958
/**
955-
* A dataflow configuration used for finding new sources.
959+
* A data flow configuration used for finding new sources.
956960
* The sources are the already known existing sources and the sinks are the API return nodes.
957961
*
958962
* This can be used to generate Source summaries for an API, if the API expose an already known source
@@ -997,7 +1001,7 @@ module MakeModelGenerator<
9971001
}
9981002

9991003
/**
1000-
* A dataflow configuration used for finding new sinks.
1004+
* A data flow configuration used for finding new sinks.
10011005
* The sources are the parameters of the API and the fields of the enclosing type.
10021006
*
10031007
* This can be used to generate Sink summaries for APIs, if the API propagates a parameter (or enclosing type field)

shared/mad/codeql/mad/modelgenerator/internal/ModelPrinting.qll

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ signature module ModelPrintingLangSig {
55
class Callable;
66

77
/**
8-
* Holds if `container`, `type`, `name`, and `parameters` contain the type signature of `api`
9-
* and `extensible` is the string representation of a boolean that is true, if
10-
* `api` can be overridden (otherwise false).
8+
* Gets the string representation for the `i`th column in the MaD row for `api`.
119
*/
12-
predicate partialModel(
13-
Callable api, string container, string type, string extensible, string name, string parameters
14-
);
10+
string partialModelRow(Callable api, int i);
11+
12+
/**
13+
* Gets the string representation for the `i`th column in the neutral MaD row for `api`.
14+
*/
15+
string partialNeutralModelRow(Callable api, int i);
1516
}
1617

1718
module ModelPrintingImpl<ModelPrintingLangSig Lang> {
@@ -33,33 +34,17 @@ module ModelPrintingImpl<ModelPrintingLangSig Lang> {
3334

3435
module ModelPrinting<ModelPrintingSig Printing> {
3536
/**
36-
* Computes the first 6 columns for MaD rows used for summaries, sources and sinks.
37+
* Computes the first columns for MaD rows used for summaries, sources and sinks.
3738
*/
3839
private string asPartialModel(Lang::Callable api) {
39-
exists(string container, string type, string extensible, string name, string parameters |
40-
Lang::partialModel(api, container, type, extensible, name, parameters) and
41-
result =
42-
container + ";" //
43-
+ type + ";" //
44-
+ extensible + ";" //
45-
+ name + ";" //
46-
+ parameters + ";" //
47-
+ /* ext + */ ";" //
48-
)
40+
result = strictconcat(int i | | Lang::partialModelRow(api, i), ";" order by i) + ";"
4941
}
5042

5143
/**
52-
* Computes the first 4 columns for neutral MaD rows.
44+
* Computes the first columns for neutral MaD rows.
5345
*/
5446
private string asPartialNeutralModel(Printing::SummaryApi api) {
55-
exists(string container, string type, string name, string parameters |
56-
Lang::partialModel(api, container, type, _, name, parameters) and
57-
result =
58-
container + ";" //
59-
+ type + ";" //
60-
+ name + ";" //
61-
+ parameters + ";" //
62-
)
47+
result = strictconcat(int i | | Lang::partialNeutralModelRow(api, i), ";" order by i) + ";"
6348
}
6449

6550
/**

0 commit comments

Comments
 (0)