Skip to content

Commit 26d5eb6

Browse files
committed
C#/Java: Initial merge ModelGeneratorUtils into CaptureModels.
1 parent 9b7691a commit 26d5eb6

15 files changed

+383
-394
lines changed

config/identical-files.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@
7575
"java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/SsaReadPositionCommon.qll",
7676
"csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/SsaReadPositionCommon.qll"
7777
],
78-
"Model as Data Generation Java/C# - Utils": [
79-
"java/ql/src/utils/model-generator/internal/ModelGeneratorUtils.qll",
80-
"csharp/ql/src/utils/model-generator/internal/ModelGeneratorUtils.qll"
81-
],
8278
"Model as Data Generation Java/C# - CaptureModels": [
8379
"java/ql/src/utils/model-generator/internal/CaptureModels.qll",
8480
"csharp/ql/src/utils/model-generator/internal/CaptureModels.qll"
@@ -549,4 +545,4 @@
549545
"javascript/ql/lib/semmle/javascript/security/dataflow/HttpToFileAccessCustomizations.qll",
550546
"ruby/ql/lib/codeql/ruby/security/HttpToFileAccessCustomizations.qll"
551547
]
552-
}
548+
}

csharp/ql/src/utils/model-generator/CaptureSinkModels.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @id csharp/utils/model-generator/sink-models
55
*/
66

7-
private import internal.ModelGeneratorUtils
87
private import internal.CaptureModels
98

109
from TargetApi api, string sink

csharp/ql/src/utils/model-generator/CaptureSourceModels.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @id csharp/utils/model-generator/sink-models
55
*/
66

7-
private import internal.ModelGeneratorUtils
87
private import internal.CaptureModels
98

109
from TargetApi api, string source

csharp/ql/src/utils/model-generator/CaptureSummaryModels.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @id csharp/utils/model-generator/summary-models
55
*/
66

7-
private import internal.ModelGeneratorUtils
87
private import internal.CaptureModels
98

109
/**

csharp/ql/src/utils/model-generator/internal/CaptureModels.qll

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,82 @@
33
* and sink models of the Standard or a 3rd party library.
44
*/
55

6-
private import ModelGeneratorUtils
76
private import CaptureModelsSpecific
87

8+
class TargetApi = TargetApiSpecific;
9+
10+
/**
11+
* Holds if data can flow from `node1` to `node2` either via a read or a write of an intermediate field `f`.
12+
*/
13+
private predicate isRelevantTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
14+
exists(DataFlow::Content f |
15+
DataFlowPrivate::readStep(node1, f, node2) and
16+
if f instanceof DataFlow::FieldContent
17+
then isRelevantType(f.(DataFlow::FieldContent).getField().getType())
18+
else
19+
if f instanceof DataFlow::SyntheticFieldContent
20+
then isRelevantType(f.(DataFlow::SyntheticFieldContent).getField().getType())
21+
else any()
22+
)
23+
or
24+
exists(DataFlow::Content f | DataFlowPrivate::storeStep(node1, f, node2) |
25+
DataFlowPrivate::containerContent(f)
26+
)
27+
}
28+
29+
/**
30+
* Holds if content `c` is either a field or synthetic field of a relevant type
31+
* or a container like content.
32+
*/
33+
private predicate isRelevantContent(DataFlow::Content c) {
34+
isRelevantType(c.(DataFlow::FieldContent).getField().getType()) or
35+
isRelevantType(c.(DataFlow::SyntheticFieldContent).getField().getType()) or
36+
DataFlowPrivate::containerContent(c)
37+
}
38+
39+
/**
40+
* Gets the summary model for `api` with `input`, `output` and `kind`.
41+
*/
42+
bindingset[input, output, kind]
43+
private string asSummaryModel(TargetApi api, string input, string output, string kind) {
44+
result =
45+
asPartialModel(api) + input + ";" //
46+
+ output + ";" //
47+
+ kind
48+
}
49+
50+
/**
51+
* Gets the value summary model for `api` with `input` and `output`.
52+
*/
53+
bindingset[input, output]
54+
private string asValueModel(TargetApi api, string input, string output) {
55+
result = asSummaryModel(api, input, output, "value")
56+
}
57+
58+
/**
59+
* Gets the taint summary model for `api` with `input` and `output`.
60+
*/
61+
bindingset[input, output]
62+
private string asTaintModel(TargetApi api, string input, string output) {
63+
result = asSummaryModel(api, input, output, "taint")
64+
}
65+
66+
/**
67+
* Gets the sink model for `api` with `input` and `kind`.
68+
*/
69+
bindingset[input, kind]
70+
private string asSinkModel(TargetApi api, string input, string kind) {
71+
result = asPartialModel(api) + input + ";" + kind
72+
}
73+
74+
/**
75+
* Gets the source model for `api` with `output` and `kind`.
76+
*/
77+
bindingset[output, kind]
78+
private string asSourceModel(TargetApi api, string output, string kind) {
79+
result = asPartialModel(api) + output + ";" + kind
80+
}
81+
982
/**
1083
* Gets the summary model of `api`, if it follows the `fluent` programming pattern (returns `this`).
1184
*/

csharp/ql/src/utils/model-generator/internal/CaptureModelsSpecific.qll

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,78 @@
44

55
import csharp
66
private import semmle.code.csharp.dataflow.TaintTracking
7-
private import semmle.code.csharp.dataflow.internal.DataFlowPrivate
8-
private import ModelGeneratorUtils
7+
private import semmle.code.csharp.commons.Util as Util
8+
private import semmle.code.csharp.commons.Collections
9+
private import semmle.code.csharp.dataflow.internal.DataFlowDispatch
910
import semmle.code.csharp.dataflow.ExternalFlow as ExternalFlow
1011
import semmle.code.csharp.dataflow.internal.DataFlowImplCommon as DataFlowImplCommon
12+
import semmle.code.csharp.dataflow.internal.DataFlowPrivate as DataFlowPrivate
13+
14+
/**
15+
* Holds if it is relevant to generate models for `api`.
16+
*/
17+
private predicate isRelevantForModels(Callable api) {
18+
[api.(Modifiable), api.(Accessor).getDeclaration()].isEffectivelyPublic() and
19+
not api instanceof Util::MainMethod
20+
}
21+
22+
/**
23+
* A class of callables that are relevant generating summary, source and sinks models for.
24+
*
25+
* In the Standard library and 3rd party libraries it the callables that can be called
26+
* from outside the library itself.
27+
*/
28+
class TargetApiSpecific extends DataFlowCallable {
29+
TargetApiSpecific() {
30+
this.fromSource() and
31+
isRelevantForModels(this)
32+
}
33+
}
34+
35+
predicate asPartialModel = DataFlowPrivate::Csv::asPartialModel/1;
36+
37+
/**
38+
* Holds for type `t` for fields that are relevant as an intermediate
39+
* read or write step in the data flow analysis.
40+
*/
41+
predicate isRelevantType(Type t) { not t instanceof Enum }
42+
43+
private string parameterAccess(Parameter p) {
44+
if isCollectionType(p.getType())
45+
then result = "Argument[" + p.getPosition() + "].Element"
46+
else result = "Argument[" + p.getPosition() + "]"
47+
}
48+
49+
/**
50+
* Gets the CSV string representation of the parameter node `p`.
51+
*/
52+
string parameterNodeAsInput(DataFlow::ParameterNode p) {
53+
result = parameterAccess(p.asParameter())
54+
or
55+
result = "Argument[Qualifier]" and p instanceof DataFlowPrivate::InstanceParameterNode
56+
}
57+
58+
pragma[nomagic]
59+
private Parameter getParameter(DataFlowImplCommon::ReturnNodeExt node, ParameterPosition pos) {
60+
result = node.getEnclosingCallable().getParameter(pos.getPosition())
61+
}
62+
63+
/**
64+
* Gets the CSV string represention of the the return node `node`.
65+
*/
66+
string returnNodeAsOutput(DataFlowImplCommon::ReturnNodeExt node) {
67+
if node.getKind() instanceof DataFlowImplCommon::ValueReturnKind
68+
then result = "ReturnValue"
69+
else
70+
exists(ParameterPosition pos |
71+
pos = node.getKind().(DataFlowImplCommon::ParamUpdateReturnKind).getPosition()
72+
|
73+
result = parameterAccess(getParameter(node, pos))
74+
or
75+
pos.isThisParameter() and
76+
result = "Argument[Qualifier]"
77+
)
78+
}
1179

1280
/**
1381
* Gets the enclosing callable of `ret`.
@@ -19,7 +87,9 @@ Callable returnNodeEnclosingCallable(DataFlowImplCommon::ReturnNodeExt ret) {
1987
/**
2088
* Holds if `node` is an own instance access.
2189
*/
22-
predicate isOwnInstanceAccessNode(ReturnNode node) { node.asExpr() instanceof ThisAccess }
90+
predicate isOwnInstanceAccessNode(DataFlowPrivate::ReturnNode node) {
91+
node.asExpr() instanceof ThisAccess
92+
}
2393

2494
/**
2595
* Gets the CSV string representation of the qualifier.
@@ -65,6 +135,6 @@ string asInputArgument(DataFlow::Node source) {
65135
result = "Argument[" + pos + "]"
66136
)
67137
or
68-
source.asExpr() instanceof FieldOrPropertyAccess and
138+
source.asExpr() instanceof DataFlowPrivate::FieldOrPropertyAccess and
69139
result = qualifierString()
70140
}

csharp/ql/src/utils/model-generator/internal/ModelGeneratorUtils.qll

Lines changed: 0 additions & 73 deletions
This file was deleted.

csharp/ql/src/utils/model-generator/internal/ModelGeneratorUtilsSpecific.qll

Lines changed: 0 additions & 70 deletions
This file was deleted.

java/ql/src/utils/model-generator/CaptureSinkModels.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @id java/utils/model-generator/sink-models
55
*/
66

7-
private import internal.ModelGeneratorUtils
87
private import internal.CaptureModels
98

109
from TargetApi api, string sink

java/ql/src/utils/model-generator/CaptureSourceModels.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @id java/utils/model-generator/sink-models
55
*/
66

7-
private import internal.ModelGeneratorUtils
87
private import internal.CaptureModels
98

109
from TargetApi api, string source

0 commit comments

Comments
 (0)