Skip to content

Commit a91cc6b

Browse files
committed
Rust: Make SummarizedCallable extend Function instead of string
1 parent 94b4f8c commit a91cc6b

File tree

25 files changed

+528
-2065
lines changed

25 files changed

+528
-2065
lines changed

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

+1-26
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,17 @@
22

33
private import rust
44
private import internal.FlowSummaryImpl as Impl
5-
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl
65

76
// import all instances below
87
private module Summaries {
98
private import codeql.rust.Frameworks
109
private import codeql.rust.dataflow.internal.ModelsAsData
1110
}
1211

13-
/** Provides the `Range` class used to define the extent of `LibraryCallable`. */
14-
module LibraryCallable {
15-
/** A callable defined in library code, identified by a unique string. */
16-
abstract class Range extends string {
17-
bindingset[this]
18-
Range() { any() }
19-
20-
/** Gets a call to this library callable. */
21-
CallExprBase getACall() {
22-
exists(Resolvable r, string crate |
23-
r = CallExprBaseImpl::getCallResolvable(result) and
24-
this = crate + r.getResolvedPath()
25-
|
26-
crate = r.getResolvedCrateOrigin() + "::_::"
27-
or
28-
not r.hasResolvedCrateOrigin() and
29-
crate = ""
30-
)
31-
}
32-
}
33-
}
34-
35-
final class LibraryCallable = LibraryCallable::Range;
36-
3712
/** Provides the `Range` class used to define the extent of `SummarizedCallable`. */
3813
module SummarizedCallable {
3914
/** A callable with a flow summary, identified by a unique string. */
40-
abstract class Range extends LibraryCallable::Range, Impl::Public::SummarizedCallable {
15+
abstract class Range extends Impl::Public::SummarizedCallable {
4116
bindingset[this]
4217
Range() { any() }
4318

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ final class DataFlowCallable extends TDataFlowCallable {
4343
/**
4444
* Gets the underlying library callable, if any.
4545
*/
46-
LibraryCallable asLibraryCallable() { this = TLibraryCallable(result) }
46+
SummarizedCallable asSummarizedCallable() { this = TSummarizedCallable(result) }
4747

4848
/** Gets a textual representation of this callable. */
49-
string toString() { result = [this.asCfgScope().toString(), this.asLibraryCallable().toString()] }
49+
string toString() {
50+
result = [this.asCfgScope().toString(), this.asSummarizedCallable().toString()]
51+
}
5052

5153
/** Gets the location of this callable. */
5254
Location getLocation() { result = this.asCfgScope().getLocation() }
@@ -67,12 +69,9 @@ final class DataFlowCall extends TDataFlowCall {
6769
}
6870

6971
DataFlowCallable getEnclosingCallable() {
70-
result = TCfgScope(this.asCallBaseExprCfgNode().getExpr().getEnclosingCfgScope())
72+
result.asCfgScope() = this.asCallBaseExprCfgNode().getExpr().getEnclosingCfgScope()
7173
or
72-
exists(FlowSummaryImpl::Public::SummarizedCallable c |
73-
this.isSummaryCall(c, _) and
74-
result = TLibraryCallable(c)
75-
)
74+
this.isSummaryCall(result.asSummarizedCallable(), _)
7675
}
7776

7877
string toString() {
@@ -434,9 +433,13 @@ module RustDataFlow implements InputSig<Location> {
434433

435434
/** Gets a viable implementation of the target of the given `Call`. */
436435
DataFlowCallable viableCallable(DataFlowCall call) {
437-
result.asCfgScope() = call.asCallBaseExprCfgNode().getCallExprBase().getStaticTarget()
438-
or
439-
result.asLibraryCallable().getACall() = call.asCallBaseExprCfgNode().getCallExprBase()
436+
exists(Callable target |
437+
target = call.asCallBaseExprCfgNode().getCallExprBase().getStaticTarget()
438+
|
439+
target = result.asCfgScope()
440+
or
441+
target = result.asSummarizedCallable()
442+
)
440443
}
441444

442445
/**
@@ -784,7 +787,7 @@ module RustDataFlow implements InputSig<Location> {
784787
predicate allowParameterReturnInSelf(ParameterNode p) {
785788
exists(DataFlowCallable c, ParameterPosition pos |
786789
p.isParameterOf(c, pos) and
787-
FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(c.asLibraryCallable(), pos)
790+
FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(c.asSummarizedCallable(), pos)
788791
)
789792
or
790793
VariableCapture::Flow::heuristicAllowInstanceParameterReturnInSelf(p.(ClosureParameterNode)
@@ -995,7 +998,7 @@ private module Cached {
995998
cached
996999
newtype TDataFlowCallable =
9971000
TCfgScope(CfgScope scope) or
998-
TLibraryCallable(LibraryCallable c)
1001+
TSummarizedCallable(SummarizedCallable c)
9991002

10001003
/** This is the local flow predicate that is exposed. */
10011004
cached

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Input implements InputSig<Location, RustDataFlow> {
1313
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl
1414
private import codeql.rust.frameworks.stdlib.Stdlib
1515

16-
class SummarizedCallableBase = string;
16+
class SummarizedCallableBase = Function;
1717

1818
abstract private class SourceSinkBase extends AstNode {
1919
/** Gets the associated call. */
@@ -153,7 +153,7 @@ private import Make<Location, RustDataFlow, Input> as Impl
153153

154154
private module StepsInput implements Impl::Private::StepsInputSig {
155155
DataFlowCall getACall(Public::SummarizedCallable sc) {
156-
result.asCallBaseExprCfgNode().getCallExprBase() = sc.(LibraryCallable).getACall()
156+
result.asCallBaseExprCfgNode().getCallExprBase().getStaticTarget() = sc
157157
}
158158

159159
RustDataFlow::Node getSourceNode(Input::SourceBase source, Impl::Private::SummaryComponent sc) {

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private import rust
4747
private import codeql.rust.dataflow.FlowSummary
4848
private import codeql.rust.dataflow.FlowSource
4949
private import codeql.rust.dataflow.FlowSink
50+
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl
5051

5152
/**
5253
* Holds if in a call to the function with canonical path `path`, defined in the
@@ -114,13 +115,30 @@ predicate interpretModelForTest(QlBuiltins::ExtensionId madId, string model) {
114115
)
115116
}
116117

118+
private predicate sdf(CallExprBase call, Function f) {
119+
CallExprBaseImpl::getCallResolvable(call).getResolvedPath() = "<crate::option::Option>::unwrap" and
120+
// CallExprBaseImpl::getCallResolvable(call).getResolvedCrateOrigin() = "lang:core" and
121+
f = call.getStaticTarget()
122+
}
123+
124+
private predicate sdf2(CallExprBase call) {
125+
CallExprBaseImpl::getCallResolvable(call).getResolvedPath() = "<crate::option::Option>::unwrap"
126+
// CallExprBaseImpl::getCallResolvable(call).getResolvedCrateOrigin() = "lang:core" and
127+
// f = call.getStaticTarget()
128+
}
129+
117130
private class SummarizedCallableFromModel extends SummarizedCallable::Range {
118131
private string crate;
119132
private string path;
120133

121134
SummarizedCallableFromModel() {
122135
summaryModel(crate, path, _, _, _, _, _) and
123-
this = crate + "::_::" + path
136+
exists(CallExprBase call, Resolvable r |
137+
call.getStaticTarget() = this and
138+
r = CallExprBaseImpl::getCallResolvable(call) and
139+
r.getResolvedPath() = path and
140+
r.getResolvedCrateOrigin() = crate
141+
)
124142
}
125143

126144
override predicate propagatesFlow(

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class NodePublic extends TNode {
4444

4545
abstract class Node extends NodePublic {
4646
/** Gets the enclosing callable. */
47-
DataFlowCallable getEnclosingCallable() { result = TCfgScope(this.getCfgScope()) }
47+
DataFlowCallable getEnclosingCallable() { result.asCfgScope() = this.getCfgScope() }
4848

4949
/** Do not call: use `getEnclosingCallable()` instead. */
5050
abstract CfgScope getCfgScope();
@@ -102,9 +102,9 @@ class FlowSummaryNode extends Node, TFlowSummaryNode {
102102
}
103103

104104
override DataFlowCallable getEnclosingCallable() {
105-
result.asLibraryCallable() = this.getSummarizedCallable()
106-
or
107105
result.asCfgScope() = this.getCfgScope()
106+
or
107+
result.asSummarizedCallable() = this.getSummarizedCallable()
108108
}
109109

110110
override Location getLocation() {
@@ -195,7 +195,7 @@ final class SummaryParameterNode extends ParameterNode, FlowSummaryNode {
195195
}
196196

197197
override predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) {
198-
this.getSummarizedCallable() = c.asLibraryCallable() and pos = pos_
198+
this.getSummarizedCallable() = c.asSummarizedCallable() and pos = pos_
199199
}
200200
}
201201

rust/ql/lib/codeql/rust/elements/internal/SelfParamImpl.qll

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/frameworks/stdlib/Clone.qll

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ private import codeql.rust.dataflow.FlowSummary
66
/** A `clone` method. */
77
final class CloneCallable extends SummarizedCallable::Range {
88
CloneCallable() {
9-
// NOTE: The function target may not exist in the database, so we base this
10-
// on method calls.
11-
exists(MethodCallExpr c |
12-
c.getIdentifier().getText() = "clone" and
13-
c.getArgList().getNumberOfArgs() = 0 and
14-
this = c.getResolvedCrateOrigin() + "::_::" + c.getResolvedPath()
15-
)
9+
this.getParamList().getNumberOfParams() = 1 and
10+
this.getName().getText() = "clone"
1611
}
1712

1813
final override predicate propagatesFlow(

rust/ql/lib/codeql/rust/internal/PathResolution.qll

+58-2
Original file line numberDiff line numberDiff line change
@@ -1205,8 +1205,8 @@ private module Debug {
12051205
private Locatable getRelevantLocatable() {
12061206
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
12071207
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
1208-
filepath.matches("%/test_logging.rs") and
1209-
startline = 163
1208+
filepath.matches("%/test.rs") and
1209+
startline = 29
12101210
)
12111211
}
12121212

@@ -1243,3 +1243,59 @@ private module Debug {
12431243
fileImport(m, f)
12441244
}
12451245
}
1246+
1247+
private predicate sdfs(ImplItemNode impl) {
1248+
// resolvePath(impl.getSelfPath()).getLocation().getStartLine() = [35, 36]
1249+
impl.getLocation().getFile().getBaseName() = "main.rs" //and
1250+
// impl.getLocation().getStartLine() = [35 .. 38]
1251+
}
1252+
1253+
private import Type
1254+
private import TypeInference
1255+
1256+
private predicate sdf3(
1257+
AstNode n, Type t, ImplItemNode impl, string path, RelevantPath p, ImplOrTraitItemNode i,
1258+
TraitType tt, Function f, string name
1259+
) {
1260+
t = inferType(n) and
1261+
n.getLocation().getStartLine() = 29 and
1262+
t = TStruct(impl.resolveSelfTy()) and
1263+
// t = debugInferType(_, _) and
1264+
impl.toString() = "impl ...::Iterator for ...::Args { ... }" and
1265+
path = p.toStringDebug() and
1266+
p = impl.getTraitPath().getQualifier*() and
1267+
i = resolvePath(p) and
1268+
impl.isFullyParametric() and
1269+
tt = TTrait(i) and
1270+
f = tt.getMethod(name) and
1271+
f = i.getAnAssocItem() and
1272+
name = "collect" and
1273+
f = impl.resolveSelfTy().getASuccessor(name)
1274+
}
1275+
1276+
private predicate sdf4(
1277+
ImplItemNode impl, string path, RelevantPath p, ItemNode iter, Use u, string usePath,
1278+
RelevantPath usePathPrefix, ItemNode useResolved
1279+
) {
1280+
// t = debugInferType(_, _) and
1281+
impl.toString() = "impl ...::Iterator for ...::Args { ... }" and
1282+
path = p.toStringDebug() and
1283+
p = impl.getTraitPath().getQualifier*() and
1284+
iter = resolvePath(p) and
1285+
iter.getName() = "iter" and
1286+
u = iter.(Module).getItemList().getAnItem() and
1287+
usePath = u.getUseTree().getPath().toStringDebug() and
1288+
usePath = "self::traits::Iterator" and
1289+
usePathPrefix = u.getUseTree().getPath().getQualifier*() and
1290+
useResolved = resolvePath(usePathPrefix)
1291+
}
1292+
1293+
private predicate sdf5(ImplItemNode impl, string path, RelevantPath p, ItemNode iter, Module m) {
1294+
// t = debugInferType(_, _) and
1295+
impl.toString() = "impl ...::Iterator for ...::Args { ... }" and
1296+
path = p.toStringDebug() and
1297+
p = impl.getTraitPath().getQualifier*() and
1298+
iter = resolvePath(p) and
1299+
iter.getName() = "iter" and
1300+
m = iter.(Module).getItemList().getAnItem()
1301+
}

rust/ql/lib/codeql/rust/internal/TypeInference.qll

+52
Original file line numberDiff line numberDiff line change
@@ -1031,3 +1031,55 @@ import Cached
10311031
* Gets a type that `n` infers to, if any.
10321032
*/
10331033
Type inferType(AstNode n) { result = inferType(n, TypePath::nil()) }
1034+
1035+
/** Provides predicates for debugging the type inference implementation. */
1036+
private module Debug {
1037+
private Locatable getRelevantLocatable() {
1038+
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
1039+
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
1040+
filepath.matches("%/test.rs") and
1041+
startline = 65
1042+
)
1043+
}
1044+
1045+
Type debugInferType(AstNode n, TypePath path) {
1046+
n = getRelevantLocatable() and
1047+
result = inferType(n, path)
1048+
}
1049+
1050+
private predicate sdf(Type t, ImplItemNode impl, TraitItemNode trait) {
1051+
t = debugInferType(_, _) and
1052+
t = TStruct(impl.resolveSelfTy()) and
1053+
trait = impl.resolveTraitTy()
1054+
}
1055+
1056+
private predicate sdf3(Type t, ImplItemNode impl) {
1057+
t = debugInferType(_, _) and
1058+
t = TStruct(impl.resolveSelfTy()) and
1059+
not impl.(Impl).hasTrait()
1060+
}
1061+
1062+
private predicate sdf2(Type t, ImplItemNode impl, string path) {
1063+
// t = debugInferType(_, _) and
1064+
impl.toString() = "impl ...::Iterator for ...::Args { ... }" and
1065+
t = TStruct(impl.resolveSelfTy()) and
1066+
path = impl.getTraitPath().toStringDebug()
1067+
}
1068+
1069+
Function debugResolveMethodCallExpr(MethodCallExpr mce) {
1070+
mce = getRelevantLocatable() and
1071+
result = resolveMethodCallExpr(mce)
1072+
}
1073+
1074+
private Function testdebugResolveMethodCallExpr(MethodCallExpr mce, Param self) {
1075+
mce = getRelevantLocatable() and
1076+
result = resolveMethodCallExpr(mce) and
1077+
self = result.getParamList().getAParam()
1078+
}
1079+
1080+
private Function testdebugResolveMethodCallExpr2(MethodCallExpr mce, SelfParam self) {
1081+
mce = getRelevantLocatable() and
1082+
result = resolveMethodCallExpr(mce) and
1083+
self = result.getParamList().getSelfParam()
1084+
}
1085+
}

rust/ql/test/library-tests/dataflow/global/viableCallable.expected

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@
6060
| main.rs:238:17:238:25 | source(...) | main.rs:1:1:3:1 | fn source |
6161
| main.rs:239:9:239:15 | sink(...) | main.rs:5:1:7:1 | fn sink |
6262
| main.rs:242:5:242:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
63-
| main.rs:246:13:246:55 | ...::block_on(...) | file://:0:0:0:0 | repo:https://github.com/rust-lang/futures-rs:futures-executor::_::crate::local_pool::block_on |
63+
| main.rs:246:13:246:55 | ...::block_on(...) | file://:0:0:0:0 | fn block_on |
6464
| main.rs:246:41:246:54 | async_source(...) | main.rs:227:1:231:1 | fn async_source |
6565
| main.rs:247:5:247:11 | sink(...) | main.rs:5:1:7:1 | fn sink |
66-
| main.rs:249:5:249:62 | ...::block_on(...) | file://:0:0:0:0 | repo:https://github.com/rust-lang/futures-rs:futures-executor::_::crate::local_pool::block_on |
66+
| main.rs:249:5:249:62 | ...::block_on(...) | file://:0:0:0:0 | fn block_on |
6767
| main.rs:249:33:249:61 | test_async_await_async_part(...) | main.rs:233:1:243:1 | fn test_async_await_async_part |
6868
| main.rs:253:5:253:22 | data_out_of_call(...) | main.rs:16:1:19:1 | fn data_out_of_call |
6969
| main.rs:254:5:254:35 | data_out_of_call_side_effect1(...) | main.rs:35:1:40:1 | fn data_out_of_call_side_effect1 |

0 commit comments

Comments
 (0)