Skip to content

Commit aaad5fa

Browse files
committed
Rust: Adapt to changes in FlowSummaryImpl
1 parent cc254d9 commit aaad5fa

File tree

4 files changed

+44
-61
lines changed

4 files changed

+44
-61
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ module SummarizedCallable {
1717
Range() { any() }
1818

1919
override predicate propagatesFlow(
20-
string input, string output, boolean preservesValue, string model
20+
string input, string output, boolean preservesValue, Provenance p, boolean isExact,
21+
string model
2122
) {
22-
this.propagatesFlow(input, output, preservesValue) and model = ""
23+
this.propagatesFlow(input, output, preservesValue) and
24+
p = "manual" and
25+
isExact = true and
26+
model = "QL"
2327
}
2428

2529
/**
@@ -31,6 +35,6 @@ module SummarizedCallable {
3135
}
3236
}
3337

34-
final class SummarizedCallable = SummarizedCallable::Range;
38+
final class SummarizedCallable = Impl::Public::RelevantSummarizedCallable;
3539

3640
final class Provenance = Impl::Public::Provenance;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ module Input implements InputSig<Location, RustDataFlow> {
3030

3131
class SummarizedCallableBase = Function;
3232

33+
predicate callableFromSource(SummarizedCallableBase c) { c.fromSource() }
34+
3335
abstract private class SourceSinkBase extends AstNode {
3436
/** Gets the associated call. */
3537
abstract Call getCall();

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

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -111,60 +111,36 @@ predicate interpretModelForTest(QlBuiltins::ExtensionId madId, string model) {
111111
)
112112
}
113113

114-
private predicate summaryModel(
115-
Function f, string input, string output, string kind, Provenance provenance, boolean isInherited,
116-
QlBuiltins::ExtensionId madId
117-
) {
118-
exists(string path, Function f0 |
119-
summaryModel(path, input, output, kind, provenance, madId) and
120-
f0.getCanonicalPath() = path
121-
|
122-
f = f0 and
123-
isInherited = false
124-
or
125-
f.implements(f0) and
126-
isInherited = true
127-
)
128-
}
129-
130-
private predicate summaryModelRelevant(
131-
Function f, string input, string output, string kind, Provenance provenance, boolean isInherited,
132-
QlBuiltins::ExtensionId madId
133-
) {
134-
summaryModel(f, input, output, kind, provenance, isInherited, madId) and
135-
// Only apply generated or inherited models to functions in library code and
136-
// when no strictly better model exists
137-
if provenance.isGenerated() or isInherited = true
138-
then
139-
not f.fromSource() and
140-
not exists(Provenance other | summaryModel(f, _, _, _, other, false, _) |
141-
provenance.isGenerated() and other.isManual()
114+
private class SummarizedCallableFromModel extends SummarizedCallable::Range {
115+
string input_;
116+
string output_;
117+
string kind;
118+
Provenance p_;
119+
boolean isExact_;
120+
QlBuiltins::ExtensionId madId;
121+
122+
SummarizedCallableFromModel() {
123+
exists(string path, Function f |
124+
summaryModel(path, input_, output_, kind, p_, madId) and
125+
f.getCanonicalPath() = path
126+
|
127+
this = f and isExact_ = true
142128
or
143-
provenance = other and isInherited = true
129+
this.implements(f) and
130+
isExact_ = false and
131+
not this.fromSource()
144132
)
145-
else any()
146-
}
147-
148-
private class SummarizedCallableFromModel extends SummarizedCallable::Range {
149-
SummarizedCallableFromModel() { summaryModelRelevant(this, _, _, _, _, _, _) }
150-
151-
override predicate hasProvenance(Provenance provenance) {
152-
summaryModelRelevant(this, _, _, _, provenance, _, _)
153133
}
154134

155135
override predicate propagatesFlow(
156-
string input, string output, boolean preservesValue, string model
136+
string input, string output, boolean preservesValue, Provenance p, boolean isExact, string model
157137
) {
158-
exists(string kind, QlBuiltins::ExtensionId madId |
159-
summaryModelRelevant(this, input, output, kind, _, _, madId) and
160-
model = "MaD:" + madId.toString()
161-
|
162-
kind = "value" and
163-
preservesValue = true
164-
or
165-
kind = "taint" and
166-
preservesValue = false
167-
)
138+
input = input_ and
139+
output = output_ and
140+
(if kind = "value" then preservesValue = true else preservesValue = false) and
141+
p = p_ and
142+
isExact = isExact_ and
143+
model = "MaD:" + madId.toString()
168144
}
169145
}
170146

@@ -211,7 +187,7 @@ private module Debug {
211187
private predicate relevantManualModel(SummarizedCallableImpl sc, string can) {
212188
exists(Provenance manual |
213189
can = sc.getCanonicalPath() and
214-
summaryModelRelevant(sc, _, _, _, manual, false, _) and
190+
sc.(SummarizedCallableFromModel).propagatesFlow(_, _, _, manual, true, _) and
215191
manual.isManual()
216192
)
217193
}
@@ -221,7 +197,7 @@ private module Debug {
221197
) {
222198
exists(RustDataFlow::ParameterPosition pos, TypeMention tm |
223199
relevantManualModel(sc, can) and
224-
sc.propagatesFlow(input, _, _, _) and
200+
sc.propagatesFlow(input, _, _, _, _, _) and
225201
input.head() = SummaryComponent::argument(pos) and
226202
p = pos.getParameterIn(sc.getParamList()) and
227203
tm.resolveType() instanceof RefType and
@@ -238,7 +214,7 @@ private module Debug {
238214
) {
239215
exists(TypeMention tm |
240216
relevantManualModel(sc, can) and
241-
sc.propagatesFlow(_, output, _, _) and
217+
sc.propagatesFlow(_, output, _, _, _, _) and
242218
tm.resolveType() instanceof RefType and
243219
output.head() = SummaryComponent::return(_) and
244220
not output.tail().head() =

rust/ql/test/library-tests/dataflow/models/models.ql

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ import codeql.rust.dataflow.FlowSink
1313
import PathGraph
1414

1515
query predicate invalidSpecComponent(SummarizedCallable sc, string s, string c) {
16-
(sc.propagatesFlow(s, _, _) or sc.propagatesFlow(_, s, _)) and
17-
Private::External::invalidSpecComponent(s, c)
16+
exists(Provenance p |
17+
Private::External::invalidSpecComponent(s, c) and
18+
p.isManual()
19+
|
20+
sc.propagatesFlow(s, _, _, p, _, _) or sc.propagatesFlow(_, s, _, p, _, _)
21+
)
1822
}
1923

2024
// not defined in `models.ext.yml`, in order to test that we can also define
2125
// models directly in QL
2226
private class SummarizedCallableIdentity extends SummarizedCallable::Range {
2327
SummarizedCallableIdentity() { this.getName().getText() = "identity" }
2428

25-
override predicate propagatesFlow(
26-
string input, string output, boolean preservesValue, string provenance
27-
) {
29+
override predicate propagatesFlow(string input, string output, boolean preservesValue) {
2830
input = "Argument[0]" and
2931
output = "ReturnValue" and
30-
preservesValue = true and
31-
provenance = "QL"
32+
preservesValue = true
3233
}
3334
}
3435

0 commit comments

Comments
 (0)