Skip to content

Commit 8310faa

Browse files
committed
C#/Java: Add a query that uses both content based and non-content based model generation.
1 parent a128383 commit 8310faa

File tree

9 files changed

+93
-7
lines changed

9 files changed

+93
-7
lines changed

csharp/ql/src/utils/modelgenerator/CaptureContentSummaryModels.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
import internal.CaptureModels
1010

1111
from DataFlowSummaryTargetApi api, string flow
12-
where flow = ContentSensitive::captureFlow(api)
12+
where flow = ContentSensitive::captureFlow(api, _)
1313
select flow order by flow
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture mixed neutral models.
3+
* @description Finds neutral models to be used by other queries.
4+
* @kind diagnostic
5+
* @id cs/utils/modelgenerator/mixed-neutral-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSummaryTargetApi api, string noflow
12+
where noflow = captureMixedNeutral(api)
13+
select noflow order by noflow
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture mixed summary models.
3+
* @description Finds applicable summary models to be used by other queries.
4+
* @kind diagnostic
5+
* @id cs/utils/modelgenerator/mixed-summary-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSummaryTargetApi api, string flow
12+
where flow = captureMixedFlow(api, _)
13+
select flow order by flow

csharp/ql/test/utils/modelgenerator/dataflow/CaptureContentSummaryModels.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import utils.modelgenerator.internal.CaptureModels
33
import TestUtilities.InlineMadTest
44

55
module InlineMadTestConfig implements InlineMadTestConfigSig {
6-
string getCapturedModel(Callable c) { result = ContentSensitive::captureFlow(c) }
6+
string getCapturedModel(Callable c) { result = ContentSensitive::captureFlow(c, _) }
77

88
string getKind() { result = "contentbased-summary" }
99
}

java/ql/src/utils/modelgenerator/CaptureContentSummaryModels.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
import internal.CaptureModels
1010

1111
from DataFlowSummaryTargetApi api, string flow
12-
where flow = ContentSensitive::captureFlow(api)
12+
where flow = ContentSensitive::captureFlow(api, _)
1313
select flow order by flow
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture mixed neutral models.
3+
* @description Finds neutral models to be used by other queries.
4+
* @kind diagnostic
5+
* @id java/utils/modelgenerator/mixed-neutral-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSummaryTargetApi api, string noflow
12+
where noflow = captureMixedNeutral(api)
13+
select noflow order by noflow
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture mixed summary models.
3+
* @description Finds applicable summary models to be used by other queries.
4+
* @kind diagnostic
5+
* @id java/utils/modelgenerator/mixed-summary-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSummaryTargetApi api, string flow
12+
where flow = captureMixedFlow(api, _)
13+
select flow order by flow

java/ql/test/utils/modelgenerator/dataflow/CaptureContentSummaryModels.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import utils.modelgenerator.internal.CaptureModels
33
import TestUtilities.InlineMadTest
44

55
module InlineMadTestConfig implements InlineMadTestConfigSig {
6-
string getCapturedModel(Callable c) { result = ContentSensitive::captureFlow(c) }
6+
string getCapturedModel(Callable c) { result = ContentSensitive::captureFlow(c, _) }
77

88
string getKind() { result = "contentbased-summary" }
99
}

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,20 +854,54 @@ module MakeModelGenerator<
854854

855855
/**
856856
* Gets the content based summary model(s) of the API `api` (if there is flow from a parameter to
857-
* the return value or a parameter).
857+
* the return value or a parameter). `lift` is true, if the model should be lifted, otherwise false.
858858
*
859859
* Models are lifted to the best type in case the read and store access paths do not
860860
* contain a field or synthetic field access.
861861
*/
862-
string captureFlow(ContentDataFlowSummaryTargetApi api) {
863-
exists(string input, string output, boolean lift, boolean preservesValue |
862+
string captureFlow(ContentDataFlowSummaryTargetApi api, boolean lift) {
863+
exists(string input, string output, boolean preservesValue |
864864
captureFlow0(api, input, output, _, lift) and
865865
preservesValue = max(boolean p | captureFlow0(api, input, output, p, lift)) and
866866
result = ContentModelPrinting::asModel(api, input, output, preservesValue, lift)
867867
)
868868
}
869869
}
870870

871+
/**
872+
* Gets the summary model(s) for `api`, if any. `lift` is true if the model is lifted
873+
* otherwise false.
874+
* The following heuristic is applied:
875+
* 1. If content based flow yields at lease one summary for an API, then we use that.
876+
* 2. If content based flow does not yield any summary for an API, then we try and
877+
* generate flow summaries using the non-content based summary generator.
878+
*/
879+
string captureMixedFlow(DataFlowSummaryTargetApi api, boolean lift) {
880+
result = ContentSensitive::captureFlow(api, lift)
881+
or
882+
not exists(ContentSensitive::captureFlow(api, lift)) and
883+
result = captureFlow(api) and
884+
lift = true
885+
}
886+
887+
/**
888+
* Gets the neutral summary model for `api`, if any.
889+
* A neutral summary model is generated, if we are not generating
890+
* a mixed summary model that applies to `api`.
891+
*/
892+
string captureMixedNeutral(DataFlowSummaryTargetApi api) {
893+
not exists(DataFlowSummaryTargetApi api0, boolean lift |
894+
exists(captureMixedFlow(api0, lift)) and
895+
(
896+
lift = false and api0 = api
897+
or
898+
lift = true and api0.lift() = api.lift()
899+
)
900+
) and
901+
api.isRelevant() and
902+
result = ModelPrinting::asNeutralSummaryModel(api)
903+
}
904+
871905
/**
872906
* A dataflow configuration used for finding new sources.
873907
* The sources are the already known existing sources and the sinks are the API return nodes.

0 commit comments

Comments
 (0)