Skip to content

Commit 518f164

Browse files
committed
Rust: Address PR comments
1 parent 476fef4 commit 518f164

File tree

4 files changed

+25
-46
lines changed

4 files changed

+25
-46
lines changed

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,21 @@ private predicate callToMethod(CallExpr call) {
133133
)
134134
}
135135

136-
/** Holds if `arg` is an argument of `call` at the position `pos`. */
136+
/**
137+
* Holds if `arg` is an argument of `call` at the position `pos`.
138+
*
139+
* Note that this does not hold for the receiever expression of a method call
140+
* as the synthetic `ReceiverNode` is the argument for the `self` parameter.
141+
*/
137142
private predicate isArgumentForCall(ExprCfgNode arg, CallExprBaseCfgNode call, ParameterPosition pos) {
138143
if callToMethod(call.(CallExprCfgNode).getCallExpr())
139-
then (
144+
then
140145
// The first argument is for the `self` parameter
141146
arg = call.getArgument(0) and pos.isSelf()
142147
or
143148
// Succeeding arguments are shifted left
144149
arg = call.getArgument(pos.getPosition() + 1)
145-
) else (
146-
// The self argument in a method call.
147-
arg = call.(MethodCallExprCfgNode).getReceiver() and pos.isSelf()
148-
or
149-
arg = call.getArgument(pos.getPosition())
150-
)
150+
else arg = call.getArgument(pos.getPosition())
151151
}
152152

153153
/**
@@ -370,19 +370,15 @@ module Node {
370370
private CallExprBaseCfgNode call_;
371371
private RustDataFlow::ArgumentPosition pos_;
372372

373-
ExprArgumentNode() {
374-
isArgumentForCall(n, call_, pos_) and
375-
// For receivers in method calls the `ReceiverNode` is the argument.
376-
not call_.(MethodCallExprCfgNode).getReceiver() = n
377-
}
373+
ExprArgumentNode() { isArgumentForCall(n, call_, pos_) }
378374

379375
override predicate isArgumentOf(DataFlowCall call, RustDataFlow::ArgumentPosition pos) {
380376
call.asCallBaseExprCfgNode() = call_ and pos = pos_
381377
}
382378
}
383379

384380
/**
385-
* The receiver of a method call _after_ any implicit borrow or dereferences
381+
* The receiver of a method call _after_ any implicit borrow or dereferencing
386382
* has taken place.
387383
*/
388384
final class ReceiverNode extends ArgumentNode, TReceiverNode {
@@ -400,7 +396,7 @@ module Node {
400396

401397
override CfgScope getCfgScope() { result = n.getAstNode().getEnclosingCfgScope() }
402398

403-
override Location getLocation() { result = n.getLocation() }
399+
override Location getLocation() { result = this.getReceiver().getLocation() }
404400

405401
override string toString() { result = "receiver for " + this.getReceiver() }
406402
}
@@ -559,7 +555,7 @@ module Node {
559555

560556
override CfgScope getCfgScope() { result = n.getAstNode().getEnclosingCfgScope() }
561557

562-
override Location getLocation() { result = n.getLocation() }
558+
override Location getLocation() { result = n.getReceiver().getLocation() }
563559
}
564560

565561
final class SummaryPostUpdateNode extends FlowSummaryNode, PostUpdateNode {
@@ -1050,7 +1046,7 @@ predicate lambdaCallExpr(CallExprCfgNode call, LambdaCallKind kind, ExprCfgNode
10501046
}
10511047

10521048
/** Holds if `mc` implicitly borrows its receiver. */
1053-
predicate implicitBorrow(MethodCallExpr mc) {
1049+
private predicate implicitBorrow(MethodCallExpr mc) {
10541050
// Determining whether an implicit borrow happens depends on the type of the
10551051
// receiever as well as the target. As a heuristic we simply check if the
10561052
// target takes `self` as a borrow and limit the approximation to cases where
@@ -1060,7 +1056,7 @@ predicate implicitBorrow(MethodCallExpr mc) {
10601056
}
10611057

10621058
/** Holds if `mc` implicitly dereferences its receiver. */
1063-
predicate implicitDeref(MethodCallExpr mc) {
1059+
private predicate implicitDeref(MethodCallExpr mc) {
10641060
// Similarly to `implicitBorrow` this is an approximation.
10651061
mc.getReceiver() instanceof VariableAccess and
10661062
not mc.getStaticTarget().getParamList().getSelfParam().isRef()
@@ -1727,7 +1723,7 @@ private module Cached {
17271723
any(IndexExprCfgNode i).getBase(), any(FieldExprCfgNode access).getExpr(),
17281724
any(TryExprCfgNode try).getExpr(),
17291725
any(PrefixExprCfgNode pe | pe.getOperatorName() = "*").getExpr(),
1730-
any(AwaitExprCfgNode a).getExpr()
1726+
any(AwaitExprCfgNode a).getExpr(), any(MethodCallExprCfgNode mc).getReceiver()
17311727
]
17321728
} or
17331729
TReceiverNode(MethodCallExprCfgNode mc, Boolean isPost) or

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ module SsaInput implements SsaImplCommon::InputSig<Location> {
4747

4848
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
4949

50-
/**
51-
* A variable amenable to SSA construction.
52-
*
53-
* All immutable variables are amenable. Mutable variables are restricted to
54-
* those that are not borrowed (either explicitly using `& mut`, or
55-
* (potentially) implicit as borrowed receivers in a method call).
56-
*/
5750
class SourceVariable = Variable;
5851

5952
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
@@ -381,8 +374,13 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
381374
}
382375

383376
predicate allowFlowIntoUncertainDef(UncertainWriteDefinition def) {
384-
exists(Variable v, BasicBlock bb, int i |
385-
def.definesAt(v, bb, i) and mutablyBorrows(bb.getNode(i).getAstNode(), v)
377+
exists(CfgNodes::CallExprBaseCfgNode call, Variable v, BasicBlock bb, int i |
378+
def.definesAt(v, bb, i) and
379+
mutablyBorrows(bb.getNode(i).getAstNode(), v)
380+
|
381+
call.getArgument(_) = bb.getNode(i)
382+
or
383+
call.(CfgNodes::MethodCallExprCfgNode).getReceiver() = bb.getNode(i)
386384
)
387385
}
388386

rust/ql/test/library-tests/dataflow/pointers/inline-flow.expected

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ edges
4040
| main.rs:73:10:73:10 | [post] b [&ref] | main.rs:74:15:74:15 | b [&ref] | provenance | |
4141
| main.rs:73:14:73:23 | source(...) | main.rs:73:10:73:10 | [post] b [&ref] | provenance | |
4242
| main.rs:74:15:74:15 | b [&ref] | main.rs:74:14:74:15 | * ... | provenance | |
43-
| main.rs:90:11:90:16 | [post] &mut a [&ref] | main.rs:90:16:90:16 | [post] a | provenance | |
44-
| main.rs:90:16:90:16 | [post] a | main.rs:91:14:91:14 | a | provenance | |
45-
| main.rs:90:21:90:30 | source(...) | main.rs:90:11:90:16 | [post] &mut a [&ref] | provenance | |
46-
| main.rs:95:13:95:29 | mut to_be_cleared | main.rs:98:14:98:26 | to_be_cleared | provenance | |
47-
| main.rs:95:33:95:42 | source(...) | main.rs:95:13:95:29 | mut to_be_cleared | provenance | |
4843
| main.rs:105:10:105:10 | [post] c [&ref] | main.rs:106:15:106:15 | c [&ref] | provenance | |
4944
| main.rs:105:14:105:23 | source(...) | main.rs:105:10:105:10 | [post] c [&ref] | provenance | |
5045
| main.rs:106:15:106:15 | c [&ref] | main.rs:106:14:106:15 | * ... | provenance | |
@@ -178,13 +173,6 @@ nodes
178173
| main.rs:73:14:73:23 | source(...) | semmle.label | source(...) |
179174
| main.rs:74:14:74:15 | * ... | semmle.label | * ... |
180175
| main.rs:74:15:74:15 | b [&ref] | semmle.label | b [&ref] |
181-
| main.rs:90:11:90:16 | [post] &mut a [&ref] | semmle.label | [post] &mut a [&ref] |
182-
| main.rs:90:16:90:16 | [post] a | semmle.label | [post] a |
183-
| main.rs:90:21:90:30 | source(...) | semmle.label | source(...) |
184-
| main.rs:91:14:91:14 | a | semmle.label | a |
185-
| main.rs:95:13:95:29 | mut to_be_cleared | semmle.label | mut to_be_cleared |
186-
| main.rs:95:33:95:42 | source(...) | semmle.label | source(...) |
187-
| main.rs:98:14:98:26 | to_be_cleared | semmle.label | to_be_cleared |
188176
| main.rs:105:10:105:10 | [post] c [&ref] | semmle.label | [post] c [&ref] |
189177
| main.rs:105:14:105:23 | source(...) | semmle.label | source(...) |
190178
| main.rs:106:14:106:15 | * ... | semmle.label | * ... |
@@ -290,16 +278,13 @@ subpaths
290278
| main.rs:253:24:253:32 | my_number [MyNumber] | main.rs:149:14:149:24 | ...: MyNumber [MyNumber] | main.rs:149:34:153:1 | { ... } | main.rs:253:14:253:33 | to_number(...) |
291279
| main.rs:255:24:255:32 | my_number [MyNumber] | main.rs:149:14:149:24 | ...: MyNumber [MyNumber] | main.rs:149:34:153:1 | { ... } | main.rs:255:14:255:33 | to_number(...) |
292280
testFailures
293-
| main.rs:255:14:255:33 | to_number(...) | Unexpected result: hasValueFlow=99 |
294281
#select
295282
| main.rs:20:14:20:14 | c | main.rs:17:17:17:26 | source(...) | main.rs:20:14:20:14 | c | $@ | main.rs:17:17:17:26 | source(...) | source(...) |
296283
| main.rs:24:14:24:14 | n | main.rs:28:19:28:28 | source(...) | main.rs:24:14:24:14 | n | $@ | main.rs:28:19:28:28 | source(...) | source(...) |
297284
| main.rs:39:14:39:14 | b | main.rs:33:19:33:28 | source(...) | main.rs:39:14:39:14 | b | $@ | main.rs:33:19:33:28 | source(...) | source(...) |
298285
| main.rs:53:14:53:15 | * ... | main.rs:51:17:51:26 | source(...) | main.rs:53:14:53:15 | * ... | $@ | main.rs:51:17:51:26 | source(...) | source(...) |
299286
| main.rs:59:33:59:34 | * ... | main.rs:57:22:57:31 | source(...) | main.rs:59:33:59:34 | * ... | $@ | main.rs:57:22:57:31 | source(...) | source(...) |
300287
| main.rs:74:14:74:15 | * ... | main.rs:73:14:73:23 | source(...) | main.rs:74:14:74:15 | * ... | $@ | main.rs:73:14:73:23 | source(...) | source(...) |
301-
| main.rs:91:14:91:14 | a | main.rs:90:21:90:30 | source(...) | main.rs:91:14:91:14 | a | $@ | main.rs:90:21:90:30 | source(...) | source(...) |
302-
| main.rs:98:14:98:26 | to_be_cleared | main.rs:95:33:95:42 | source(...) | main.rs:98:14:98:26 | to_be_cleared | $@ | main.rs:95:33:95:42 | source(...) | source(...) |
303288
| main.rs:106:14:106:15 | * ... | main.rs:105:14:105:23 | source(...) | main.rs:106:14:106:15 | * ... | $@ | main.rs:105:14:105:23 | source(...) | source(...) |
304289
| main.rs:113:14:113:15 | * ... | main.rs:112:25:112:34 | source(...) | main.rs:113:14:113:15 | * ... | $@ | main.rs:112:25:112:34 | source(...) | source(...) |
305290
| main.rs:175:14:175:34 | my_number.to_number(...) | main.rs:174:44:174:53 | source(...) | main.rs:175:14:175:34 | my_number.to_number(...) | $@ | main.rs:174:44:174:53 | source(...) | source(...) |

rust/ql/test/library-tests/dataflow/pointers/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ mod intraprocedural_mutable_borrows {
8888
let mut a = 1;
8989
sink(a);
9090
*(&mut a) = source(87);
91-
sink(a); // $ hasValueFlow=87
91+
sink(a); // $ MISSING: hasValueFlow=87
9292
}
9393

9494
pub fn clear_through_borrow() {
9595
let mut to_be_cleared = source(34);
9696
let p = &mut to_be_cleared;
9797
*p = 0;
98-
sink(to_be_cleared); // $ SPURIOUS: hasValueFlow=34
98+
sink(to_be_cleared); // variable is cleared
9999
}
100100

101101
pub fn write_through_borrow_in_match(cond: bool) {
@@ -252,7 +252,7 @@ mod interprocedural_mutable_borrows {
252252
(&mut my_number).set(source(99));
253253
sink(to_number(my_number)); // $ hasValueFlow=99
254254
(&mut my_number).set(0);
255-
sink(to_number(my_number)); // SPURIOUS: hasValueFlow=99
255+
sink(to_number(my_number)); // $ SPURIOUS: hasValueFlow=99
256256
}
257257
}
258258

0 commit comments

Comments
 (0)