Skip to content

Commit 3420f1f

Browse files
committed
Address review comments, store step for tuple indexing
1 parent d89678f commit 3420f1f

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ private class StructCanonicalPath extends MkStructCanonicalPath {
465465
}
466466

467467
/** Content stored in a field on a struct. */
468-
private class StructFieldContent extends VariantContent, TStructFieldContent {
468+
private class StructFieldContent extends Content, TStructFieldContent {
469469
private StructCanonicalPath s;
470470
private string field_;
471471

@@ -482,7 +482,7 @@ private class StructFieldContent extends VariantContent, TStructFieldContent {
482482
* NOTE: Unlike `struct`s and `enum`s tuples are structural and not nominal,
483483
* hence we don't store a canonical path for them.
484484
*/
485-
private class TuplePositionContent extends VariantContent, TTuplePositionContent {
485+
private class TuplePositionContent extends Content, TTuplePositionContent {
486486
private int pos;
487487

488488
TuplePositionContent() { this = TTuplePositionContent(pos) }
@@ -492,6 +492,11 @@ private class TuplePositionContent extends VariantContent, TTuplePositionContent
492492
override string toString() { result = "tuple." + pos.toString() }
493493
}
494494

495+
/** Holds if `access` indexes a tuple at an index corresponding to `c`. */
496+
private predicate fieldTuplePositionContent(FieldExprCfgNode access, TuplePositionContent c) {
497+
access.getNameRef().getText().toInt() = c.getPosition()
498+
}
499+
495500
/** A value that represents a set of `Content`s. */
496501
abstract class ContentSet extends TContentSet {
497502
/** Gets a textual representation of this element. */
@@ -687,7 +692,7 @@ module RustDataFlow implements InputSig<Location> {
687692
pathResolveToVariantCanonicalPath(p.getPath(), v)
688693
}
689694

690-
/** Holds if `p` destructs an struct `s`. */
695+
/** Holds if `p` destructs a struct `s`. */
691696
pragma[nomagic]
692697
private predicate structDestruction(RecordPat p, StructCanonicalPath s) {
693698
pathResolveToStructCanonicalPath(p.getPath(), s)
@@ -722,7 +727,7 @@ module RustDataFlow implements InputSig<Location> {
722727
or
723728
exists(FieldExprCfgNode access |
724729
// Read of a tuple entry
725-
access.getNameRef().getText().toInt() = c.(TuplePositionContent).getPosition() and
730+
fieldTuplePositionContent(access, c) and
726731
// TODO: Handle read of a struct field.
727732
node1.asExpr() = access.getExpr() and
728733
node2.asExpr() = access
@@ -742,7 +747,7 @@ module RustDataFlow implements InputSig<Location> {
742747
pathResolveToVariantCanonicalPath(re.getPath(), v)
743748
}
744749

745-
/** Holds if `re` constructs a struct value of type `v`. */
750+
/** Holds if `re` constructs a struct value of type `s`. */
746751
pragma[nomagic]
747752
private predicate structConstruction(RecordExpr re, StructCanonicalPath s) {
748753
pathResolveToStructCanonicalPath(re.getPath(), s)
@@ -780,6 +785,13 @@ module RustDataFlow implements InputSig<Location> {
780785
node1.asExpr() = tuple.getField(c.(TuplePositionContent).getPosition()) and
781786
node2.asExpr() = tuple
782787
)
788+
or
789+
exists(AssignmentExprCfgNode assignment, FieldExprCfgNode access |
790+
assignment.getLhs() = access and
791+
fieldTuplePositionContent(access, c) and
792+
node1.asExpr() = assignment.getRhs() and
793+
node2.asExpr() = access.getExpr()
794+
)
783795
)
784796
}
785797

@@ -788,12 +800,11 @@ module RustDataFlow implements InputSig<Location> {
788800
* any value stored inside `f` is cleared at the pre-update node associated with `x`
789801
* in `x.f = newValue`.
790802
*/
791-
predicate clearsContent(Node n, ContentSet c) {
803+
predicate clearsContent(Node n, ContentSet cs) {
792804
exists(AssignmentExprCfgNode assignment, FieldExprCfgNode access |
793805
assignment.getLhs() = access and
794806
n.asExpr() = access.getExpr() and
795-
access.getNameRef().getText().toInt() =
796-
c.(SingletonContentSet).getContent().(TuplePositionContent).getPosition()
807+
fieldTuplePositionContent(access, cs.(SingletonContentSet).getContent())
797808
)
798809
}
799810

rust/ql/test/library-tests/dataflow/local/DataFlowStep.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ storeStep
336336
| main.rs:100:29:100:29 | 2 | tuple.2 | main.rs:100:13:100:30 | TupleExpr |
337337
| main.rs:108:18:108:18 | 2 | tuple.0 | main.rs:108:17:108:31 | TupleExpr |
338338
| main.rs:108:21:108:30 | source(...) | tuple.1 | main.rs:108:17:108:31 | TupleExpr |
339+
| main.rs:111:11:111:20 | source(...) | tuple.0 | main.rs:111:5:111:5 | a |
340+
| main.rs:112:11:112:11 | 2 | tuple.1 | main.rs:112:5:112:5 | a |
339341
| main.rs:118:14:118:14 | 3 | tuple.0 | main.rs:118:13:118:27 | TupleExpr |
340342
| main.rs:118:17:118:26 | source(...) | tuple.1 | main.rs:118:13:118:27 | TupleExpr |
341343
| main.rs:119:14:119:14 | a | tuple.0 | main.rs:119:13:119:18 | TupleExpr |

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ edges
1111
| main.rs:108:17:108:31 | TupleExpr [tuple.1] | main.rs:110:10:110:10 | a [tuple.1] | provenance | |
1212
| main.rs:108:21:108:30 | source(...) | main.rs:108:17:108:31 | TupleExpr [tuple.1] | provenance | |
1313
| main.rs:110:10:110:10 | a [tuple.1] | main.rs:110:10:110:12 | a.1 | provenance | |
14+
| main.rs:111:5:111:5 | a [tuple.0] | main.rs:112:5:112:5 | a [tuple.0] | provenance | |
15+
| main.rs:111:11:111:20 | source(...) | main.rs:111:5:111:5 | a [tuple.0] | provenance | |
16+
| main.rs:112:5:112:5 | a [tuple.0] | main.rs:113:10:113:10 | a [tuple.0] | provenance | |
17+
| main.rs:113:10:113:10 | a [tuple.0] | main.rs:113:10:113:12 | a.0 | provenance | |
1418
| main.rs:118:13:118:27 | TupleExpr [tuple.1] | main.rs:119:14:119:14 | a [tuple.1] | provenance | |
1519
| main.rs:118:17:118:26 | source(...) | main.rs:118:13:118:27 | TupleExpr [tuple.1] | provenance | |
1620
| main.rs:119:13:119:18 | TupleExpr [tuple.0, tuple.1] | main.rs:121:10:121:10 | b [tuple.0, tuple.1] | provenance | |
@@ -59,6 +63,11 @@ nodes
5963
| main.rs:108:21:108:30 | source(...) | semmle.label | source(...) |
6064
| main.rs:110:10:110:10 | a [tuple.1] | semmle.label | a [tuple.1] |
6165
| main.rs:110:10:110:12 | a.1 | semmle.label | a.1 |
66+
| main.rs:111:5:111:5 | a [tuple.0] | semmle.label | a [tuple.0] |
67+
| main.rs:111:11:111:20 | source(...) | semmle.label | source(...) |
68+
| main.rs:112:5:112:5 | a [tuple.0] | semmle.label | a [tuple.0] |
69+
| main.rs:113:10:113:10 | a [tuple.0] | semmle.label | a [tuple.0] |
70+
| main.rs:113:10:113:12 | a.0 | semmle.label | a.0 |
6271
| main.rs:118:13:118:27 | TupleExpr [tuple.1] | semmle.label | TupleExpr [tuple.1] |
6372
| main.rs:118:17:118:26 | source(...) | semmle.label | source(...) |
6473
| main.rs:119:13:119:18 | TupleExpr [tuple.0, tuple.1] | semmle.label | TupleExpr [tuple.0, tuple.1] |
@@ -103,6 +112,7 @@ testFailures
103112
| main.rs:54:10:54:10 | i | main.rs:53:9:53:17 | source(...) | main.rs:54:10:54:10 | i | $@ | main.rs:53:9:53:17 | source(...) | source(...) |
104113
| main.rs:95:10:95:12 | a.0 | main.rs:94:14:94:22 | source(...) | main.rs:95:10:95:12 | a.0 | $@ | main.rs:94:14:94:22 | source(...) | source(...) |
105114
| main.rs:110:10:110:12 | a.1 | main.rs:108:21:108:30 | source(...) | main.rs:110:10:110:12 | a.1 | $@ | main.rs:108:21:108:30 | source(...) | source(...) |
115+
| main.rs:113:10:113:12 | a.0 | main.rs:111:11:111:20 | source(...) | main.rs:113:10:113:12 | a.0 | $@ | main.rs:111:11:111:20 | source(...) | source(...) |
106116
| main.rs:121:10:121:14 | ... .1 | main.rs:118:17:118:26 | source(...) | main.rs:121:10:121:14 | ... .1 | $@ | main.rs:118:17:118:26 | source(...) | source(...) |
107117
| main.rs:158:10:158:10 | a | main.rs:154:12:154:21 | source(...) | main.rs:158:10:158:10 | a | $@ | main.rs:154:12:154:21 | source(...) | source(...) |
108118
| main.rs:217:25:217:25 | n | main.rs:214:19:214:28 | source(...) | main.rs:217:25:217:25 | n | $@ | main.rs:214:19:214:28 | source(...) | source(...) |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn tuple_mutation() {
110110
sink(a.1); // $ hasValueFlow=38
111111
a.0 = source(70);
112112
a.1 = 2;
113-
sink(a.0); // $ MISSING: hasValueFlow=70
113+
sink(a.0); // $ hasValueFlow=70
114114
sink(a.1);
115115
}
116116

0 commit comments

Comments
 (0)