Skip to content

Commit 11aef70

Browse files
authored
Merge pull request #19273 from MathiasVP/prepare-shared-mad-generation-for-cpp
Shared: Prepare model generation for C++ adoption
2 parents 877118f + ea3bb8c commit 11aef70

File tree

4 files changed

+65
-43
lines changed

4 files changed

+65
-43
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, CsharpDat
2222

2323
class Callable = CS::Callable;
2424

25-
class NodeExtended extends CS::DataFlow::Node {
26-
Callable getAsExprEnclosingCallable() { result = this.asExpr().getEnclosingCallable() }
25+
class NodeExtended = CS::DataFlow::Node;
26+
27+
Callable getAsExprEnclosingCallable(NodeExtended node) {
28+
result = node.asExpr().getEnclosingCallable()
2729
}
2830

31+
Callable getEnclosingCallable(NodeExtended node) { result = node.getEnclosingCallable() }
32+
33+
Parameter asParameter(NodeExtended node) { result = node.asParameter() }
34+
2935
/**
3036
* Holds if any of the parameters of `api` are `System.Func<>`.
3137
*/

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, JavaDataF
3232

3333
class Callable = J::Callable;
3434

35-
class NodeExtended extends DataFlow::Node {
36-
Callable getAsExprEnclosingCallable() { result = this.asExpr().getEnclosingCallable() }
35+
class NodeExtended = DataFlow::Node;
36+
37+
Callable getAsExprEnclosingCallable(NodeExtended node) {
38+
result = node.asExpr().getEnclosingCallable()
3739
}
3840

41+
Callable getEnclosingCallable(NodeExtended node) { result = node.getEnclosingCallable() }
42+
43+
Parameter asParameter(NodeExtended node) { result = node.asParameter() }
44+
3945
private predicate isInfrequentlyUsed(J::CompilationUnit cu) {
4046
cu.getPackage().getName().matches("javax.swing%") or
4147
cu.getPackage().getName().matches("java.awt%")

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, RustDataF
2020
class Callable = R::Callable;
2121

2222
class NodeExtended extends DataFlow::Node {
23-
Callable getAsExprEnclosingCallable() { result = this.asExpr().getScope() }
24-
2523
Type getType() { any() }
24+
}
2625

27-
Callable getEnclosingCallable() {
28-
result = this.(Node::Node).getEnclosingCallable().asCfgScope()
29-
}
26+
Callable getAsExprEnclosingCallable(NodeExtended node) { result = node.asExpr().getScope() }
27+
28+
Callable getEnclosingCallable(NodeExtended node) {
29+
result = node.(Node::Node).getEnclosingCallable().asCfgScope()
3030
}
3131

32+
Parameter asParameter(NodeExtended node) { result = node.asParameter() }
33+
3234
private predicate relevant(Function api) {
3335
// Only include functions that have a resolved path.
3436
api.hasCrateOrigin() and

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

+42-34
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,18 @@ signature module ModelGeneratorInputSig<LocationSig Location, InputSig<Location>
4444
* Gets the type of this node.
4545
*/
4646
Type getType();
47+
}
4748

48-
/**
49-
* Gets the enclosing callable of this node.
50-
*/
51-
Callable getEnclosingCallable();
49+
/** Gets the enclosing callable of `node`. */
50+
Callable getEnclosingCallable(NodeExtended node);
5251

53-
/**
54-
* Gets the enclosing callable of this node, when considered as an expression.
55-
*/
56-
Callable getAsExprEnclosingCallable();
52+
/**
53+
* Gets the enclosing callable of `node`, when considered as an expression.
54+
*/
55+
Callable getAsExprEnclosingCallable(NodeExtended node);
5756

58-
/**
59-
* Gets the parameter corresponding to this node, if any.
60-
*/
61-
Parameter asParameter();
62-
}
57+
/** Gets the parameter corresponding to this node, if any. */
58+
Parameter asParameter(NodeExtended n);
6359

6460
/**
6561
* A class of callables that are potentially relevant for generating summary or
@@ -226,6 +222,14 @@ signature module ModelGeneratorInputSig<LocationSig Location, InputSig<Location>
226222
*/
227223
default Lang::ParameterPosition getReturnKindParamPosition(Lang::ReturnKind node) { none() }
228224

225+
/**
226+
* Gets the string that represents the return value corresponding to the
227+
* return kind `kind`.
228+
*
229+
* For most languages this will be the string "ReturnValue".
230+
*/
231+
default string getReturnValueString(Lang::ReturnKind kind) { result = "ReturnValue" }
232+
229233
/**
230234
* Holds if it is irrelevant to generate models for `api` based on data flow analysis.
231235
*
@@ -321,9 +325,11 @@ module MakeModelGenerator<
321325

322326
private module PrintReturnNodeExt<printCallableParamSig/2 printCallableParam> {
323327
string getOutput(ReturnNodeExt node) {
324-
node.getKind() instanceof DataFlow::ValueReturnKind and
325-
not exists(node.getPosition()) and
326-
result = "ReturnValue"
328+
exists(DataFlow::ValueReturnKind valueReturnKind |
329+
valueReturnKind = node.getKind() and
330+
not exists(node.getPosition()) and
331+
result = getReturnValueString(valueReturnKind.getKind())
332+
)
327333
or
328334
exists(DataFlow::ParameterPosition pos |
329335
pos = node.getPosition() and
@@ -390,7 +396,7 @@ module MakeModelGenerator<
390396
* Gets the MaD string representation of the parameter node `p`.
391397
*/
392398
string parameterNodeAsInput(DataFlow::ParameterNode p) {
393-
result = parameterAccess(p.(NodeExtended).asParameter())
399+
result = parameterAccess(asParameter(p))
394400
or
395401
result = qualifierString() and p instanceof InstanceParameterNode
396402
}
@@ -465,7 +471,7 @@ module MakeModelGenerator<
465471
predicate isSource(DataFlow::Node source, FlowState state) {
466472
source instanceof DataFlow::ParameterNode and
467473
exists(Callable c |
468-
c = source.(NodeExtended).getEnclosingCallable() and
474+
c = getEnclosingCallable(source) and
469475
c instanceof DataFlowSummaryTargetApi and
470476
not isUninterestingForHeuristicDataFlowModels(c)
471477
) and
@@ -475,7 +481,7 @@ module MakeModelGenerator<
475481
predicate isSink(DataFlow::Node sink, FlowState state) {
476482
sink instanceof ReturnNodeExt and
477483
not isOwnInstanceAccessNode(sink) and
478-
not exists(captureQualifierFlow(sink.(NodeExtended).getAsExprEnclosingCallable())) and
484+
not exists(captureQualifierFlow(getAsExprEnclosingCallable(sink))) and
479485
(state instanceof TaintRead or state instanceof TaintStore)
480486
}
481487

@@ -519,8 +525,8 @@ module MakeModelGenerator<
519525
DataFlowSummaryTargetApi api, DataFlow::ParameterNode p, ReturnNodeExt returnNodeExt
520526
) {
521527
exists(string input, string output |
522-
p.(NodeExtended).getEnclosingCallable() = api and
523-
returnNodeExt.getEnclosingCallable() = api and
528+
getEnclosingCallable(p) = api and
529+
getEnclosingCallable(returnNodeExt) = api and
524530
input = parameterNodeAsInput(p) and
525531
output = getOutput(returnNodeExt) and
526532
input != output and
@@ -570,11 +576,12 @@ module MakeModelGenerator<
570576
private module PropagateContentFlowConfig implements ContentDataFlow::ConfigSig {
571577
predicate isSource(DataFlow::Node source) {
572578
source instanceof DataFlow::ParameterNode and
573-
source.(NodeExtended).getEnclosingCallable() instanceof DataFlowSummaryTargetApi
579+
getEnclosingCallable(source) instanceof DataFlowSummaryTargetApi
574580
}
575581

576582
predicate isSink(DataFlow::Node sink) {
577-
sink.(ReturnNodeExt).getEnclosingCallable() instanceof DataFlowSummaryTargetApi
583+
sink instanceof ReturnNodeExt and
584+
getEnclosingCallable(sink) instanceof DataFlowSummaryTargetApi
578585
}
579586

580587
predicate isAdditionalFlowStep = isAdditionalContentFlowStep/2;
@@ -613,7 +620,7 @@ module MakeModelGenerator<
613620
* when used in content flow.
614621
*/
615622
private string parameterNodeAsContentInput(DataFlow::ParameterNode p) {
616-
result = parameterContentAccess(p.(NodeExtended).asParameter())
623+
result = parameterContentAccess(asParameter(p))
617624
or
618625
result = qualifierString() and p instanceof InstanceParameterNode
619626
}
@@ -667,8 +674,8 @@ module MakeModelGenerator<
667674
PropagateContentFlow::AccessPath stores, boolean preservesValue
668675
) {
669676
PropagateContentFlow::flow(p, reads, returnNodeExt, stores, preservesValue) and
670-
returnNodeExt.getEnclosingCallable() = api and
671-
p.(NodeExtended).getEnclosingCallable() = api
677+
getEnclosingCallable(returnNodeExt) = api and
678+
getEnclosingCallable(p) = api
672679
}
673680

674681
/**
@@ -687,7 +694,7 @@ module MakeModelGenerator<
687694
private DataFlow::ParameterNode parameter;
688695

689696
ContentDataFlowSummaryTargetApi() {
690-
count(string input, string output |
697+
strictcount(string input, string output |
691698
exists(
692699
PropagateContentFlow::AccessPath reads, ReturnNodeExt returnNodeExt,
693700
PropagateContentFlow::AccessPath stores
@@ -712,8 +719,8 @@ module MakeModelGenerator<
712719
PropagateContentFlow::AccessPath stores, boolean preservesValue
713720
) {
714721
PropagateContentFlow::flow(p, reads, returnNodeExt, stores, preservesValue) and
715-
returnNodeExt.getEnclosingCallable() = api and
716-
p.(NodeExtended).getEnclosingCallable() = api and
722+
getEnclosingCallable(returnNodeExt) = api and
723+
getEnclosingCallable(p) = api and
717724
p = api.getARelevantParameterNode()
718725
}
719726

@@ -985,7 +992,8 @@ module MakeModelGenerator<
985992
}
986993

987994
predicate isSink(DataFlow::Node sink) {
988-
sink.(ReturnNodeExt).getEnclosingCallable() instanceof DataFlowSourceTargetApi
995+
sink instanceof ReturnNodeExt and
996+
getEnclosingCallable(sink) instanceof DataFlowSourceTargetApi
989997
}
990998

991999
DataFlow::FlowFeature getAFeature() { result instanceof DataFlow::FeatureHasSinkCallContext }
@@ -1008,8 +1016,8 @@ module MakeModelGenerator<
10081016
exists(NodeExtended source, ReturnNodeExt sink, string kind |
10091017
PropagateFromSource::flow(source, sink) and
10101018
sourceNode(source, kind) and
1011-
api = sink.getEnclosingCallable() and
1012-
not irrelevantSourceSinkApi(source.getEnclosingCallable(), api) and
1019+
api = getEnclosingCallable(sink) and
1020+
not irrelevantSourceSinkApi(getEnclosingCallable(source), api) and
10131021
result = ModelPrinting::asSourceModel(api, getOutput(sink), kind)
10141022
)
10151023
}
@@ -1024,7 +1032,7 @@ module MakeModelGenerator<
10241032
module PropagateToSinkConfig implements DataFlow::ConfigSig {
10251033
predicate isSource(DataFlow::Node source) {
10261034
apiSource(source) and
1027-
source.(NodeExtended).getEnclosingCallable() instanceof DataFlowSinkTargetApi
1035+
getEnclosingCallable(source) instanceof DataFlowSinkTargetApi
10281036
}
10291037

10301038
predicate isSink(DataFlow::Node sink) {
@@ -1053,7 +1061,7 @@ module MakeModelGenerator<
10531061
exists(NodeExtended src, NodeExtended sink, string kind |
10541062
PropagateToSink::flow(src, sink) and
10551063
sinkNode(sink, kind) and
1056-
api = src.getEnclosingCallable() and
1064+
api = getEnclosingCallable(src) and
10571065
result = ModelPrinting::asSinkModel(api, asInputArgument(src), kind)
10581066
)
10591067
}

0 commit comments

Comments
 (0)