Skip to content

Commit 8d01bbc

Browse files
authored
Merge pull request #18628 from paldepind/rust-flow-summary-generation
Rust: Initial model generation setup
2 parents 294fd0a + cf4f657 commit 8d01bbc

19 files changed

+445
-5
lines changed

rust/ql/lib/codeql/rust/dataflow/DataFlow.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ module DataFlow {
1919
* The value of a parameter at function entry, viewed as a node in a data
2020
* flow graph.
2121
*/
22-
final class ParameterNode extends Node instanceof Node::SourceParameterNode {
23-
/** Gets the parameter that this node corresponds to. */
24-
ParamBase getParameter() { result = super.getParameter().getParamBase() }
25-
}
22+
final class ParameterNode extends Node instanceof Node::SourceParameterNode { }
2623

2724
final class PostUpdateNode = Node::PostUpdateNodePublic;
2825

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ module Node {
149149
*/
150150
ExprCfgNode asExpr() { none() }
151151

152+
/**
153+
* Gets the parameter that corresponds to this node, if any.
154+
*/
155+
ParamBase asParameter() { result = this.(SourceParameterNode).getParameter().getParamBase() }
156+
152157
/**
153158
* Gets the pattern that corresponds to this node, if any.
154159
*/
@@ -274,6 +279,7 @@ module Node {
274279
* flow graph.
275280
*/
276281
abstract class ParameterNode extends Node {
282+
/** Holds if this node is a parameter of `c` at position `pos`. */
277283
abstract predicate isParameterOf(DataFlowCallable c, ParameterPosition pos);
278284
}
279285

@@ -794,7 +800,7 @@ private class VariantRecordFieldContent extends VariantContent, TVariantRecordFi
794800
}
795801

796802
/** Content stored in a field on a struct. */
797-
private class StructFieldContent extends Content, TStructFieldContent {
803+
class StructFieldContent extends Content, TStructFieldContent {
798804
private Struct s;
799805
private string field_;
800806

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
private import rust as R
2+
private import codeql.mad.test.InlineMadTest
3+
4+
private module InlineMadTestLang implements InlineMadTestLangSig {
5+
class Callable = R::Function;
6+
7+
string getComment(R::Function callable) {
8+
exists(R::Comment comment |
9+
result = comment.getCommentText() and
10+
comment.getLocation().getFile() = callable.getLocation().getFile() and
11+
// When a function is preceded by comments its start line is the line of
12+
// the first comment. Hence all relevant comments are found by including
13+
// comments from the start line and up to the line with the function
14+
// name.
15+
callable.getLocation().getStartLine() <= comment.getLocation().getStartLine() and
16+
comment.getLocation().getStartLine() <= callable.getName().getLocation().getStartLine()
17+
)
18+
}
19+
}
20+
21+
import InlineMadTestImpl<InlineMadTestLang>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture content based summary models.
3+
* @description Finds applicable content based summary models to be used by other queries.
4+
* @kind diagnostic
5+
* @id rust/utils/modelgenerator/contentbased-summary-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSummaryTargetApi api, string flow
12+
where flow = ContentSensitive::captureFlow(api, _)
13+
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 rust/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 rust/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
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture neutral models.
3+
* @description Finds neutral models to be used by other queries.
4+
* @kind diagnostic
5+
* @id rust/utils/modelgenerator/neutral-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSummaryTargetApi api, string noflow
12+
where noflow = captureNoFlow(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 sink models.
3+
* @description Finds public methods that act as sinks as they flow into a known sink.
4+
* @kind diagnostic
5+
* @id rust/utils/modelgenerator/sink-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSinkTargetApi api, string sink
12+
where sink = captureSink(api)
13+
select sink order by sink
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture source models.
3+
* @description Finds APIs that act as sources as they expose already known sources.
4+
* @kind diagnostic
5+
* @id rust/utils/modelgenerator/source-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSourceTargetApi api, string source
12+
where source = captureSource(api)
13+
select source order by source
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Capture summary models.
3+
* @description Finds applicable summary models to be used by other queries.
4+
* @kind diagnostic
5+
* @id rust/utils/modelgenerator/summary-models
6+
* @tags modelgenerator
7+
*/
8+
9+
import internal.CaptureModels
10+
11+
from DataFlowSummaryTargetApi api, string flow
12+
where flow = captureFlow(api)
13+
select flow order by flow

0 commit comments

Comments
 (0)