From b52058ffed656575d5fc5bd4063672a849bbba00 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Fri, 22 Nov 2024 14:52:59 +0100 Subject: [PATCH] Rust: Add more flow tests --- .../dataflow/local/DataFlowStep.expected | 127 +++++++++++++----- .../test/library-tests/dataflow/local/main.rs | 101 ++++++++++---- 2 files changed, 165 insertions(+), 63 deletions(-) diff --git a/rust/ql/test/library-tests/dataflow/local/DataFlowStep.expected b/rust/ql/test/library-tests/dataflow/local/DataFlowStep.expected index 86bc28d7940b..433f38706457 100644 --- a/rust/ql/test/library-tests/dataflow/local/DataFlowStep.expected +++ b/rust/ql/test/library-tests/dataflow/local/DataFlowStep.expected @@ -75,42 +75,95 @@ | main.rs:97:38:97:38 | p | main.rs:97:9:97:34 | RecordPat | | main.rs:104:9:104:10 | [SSA] s1 | main.rs:106:11:106:12 | s1 | | main.rs:104:9:104:10 | s1 | main.rs:104:9:104:10 | [SSA] s1 | -| main.rs:104:14:104:28 | CallExpr | main.rs:104:9:104:10 | s1 | +| main.rs:104:14:104:37 | CallExpr | main.rs:104:9:104:10 | s1 | | main.rs:105:9:105:10 | [SSA] s2 | main.rs:110:11:110:12 | s2 | | main.rs:105:9:105:10 | s2 | main.rs:105:9:105:10 | [SSA] s2 | -| main.rs:105:14:105:20 | CallExpr | main.rs:105:9:105:10 | s2 | -| main.rs:107:14:107:14 | [SSA] n | main.rs:107:25:107:25 | n | -| main.rs:107:14:107:14 | n | main.rs:107:14:107:14 | [SSA] n | -| main.rs:107:20:107:26 | CallExpr | main.rs:106:5:109:5 | MatchExpr | -| main.rs:108:17:108:23 | CallExpr | main.rs:106:5:109:5 | MatchExpr | -| main.rs:110:5:113:5 | MatchExpr | main.rs:103:27:114:1 | BlockExpr | -| main.rs:111:14:111:14 | [SSA] n | main.rs:111:25:111:25 | n | -| main.rs:111:14:111:14 | n | main.rs:111:14:111:14 | [SSA] n | -| main.rs:111:20:111:26 | CallExpr | main.rs:110:5:113:5 | MatchExpr | -| main.rs:112:17:112:23 | CallExpr | main.rs:110:5:113:5 | MatchExpr | -| main.rs:117:9:117:9 | [SSA] a | main.rs:118:5:118:5 | a | -| main.rs:117:9:117:9 | a | main.rs:117:9:117:9 | [SSA] a | -| main.rs:117:13:117:17 | BlockExpr | main.rs:117:9:117:9 | a | -| main.rs:117:15:117:15 | 0 | main.rs:117:13:117:17 | BlockExpr | -| main.rs:118:5:118:5 | a | main.rs:116:31:119:1 | BlockExpr | -| main.rs:121:22:121:22 | [SSA] b | main.rs:123:12:123:12 | b | -| main.rs:121:22:121:22 | b | main.rs:121:22:121:22 | [SSA] b | -| main.rs:121:22:121:28 | Param | main.rs:121:22:121:22 | b | -| main.rs:122:9:122:9 | [SSA] a | main.rs:128:5:128:5 | a | -| main.rs:122:9:122:9 | a | main.rs:122:9:122:9 | [SSA] a | -| main.rs:122:13:127:5 | BlockExpr | main.rs:122:9:122:9 | a | -| main.rs:124:13:124:26 | BreakExpr | main.rs:122:13:127:5 | BlockExpr | -| main.rs:124:26:124:26 | 1 | main.rs:124:13:124:26 | BreakExpr | -| main.rs:126:9:126:9 | 2 | main.rs:122:13:127:5 | BlockExpr | -| main.rs:128:5:128:5 | a | main.rs:121:38:129:1 | BlockExpr | -| main.rs:131:22:131:22 | [SSA] b | main.rs:133:12:133:12 | b | -| main.rs:131:22:131:22 | b | main.rs:131:22:131:22 | [SSA] b | -| main.rs:131:22:131:28 | Param | main.rs:131:22:131:22 | b | -| main.rs:132:9:132:9 | [SSA] a | main.rs:138:5:138:5 | a | -| main.rs:132:9:132:9 | a | main.rs:132:9:132:9 | [SSA] a | -| main.rs:132:13:137:5 | BlockExpr | main.rs:132:9:132:9 | a | -| main.rs:134:13:134:26 | BreakExpr | main.rs:132:13:137:5 | BlockExpr | -| main.rs:134:26:134:26 | 1 | main.rs:134:13:134:26 | BreakExpr | -| main.rs:136:9:136:22 | BreakExpr | main.rs:132:13:137:5 | BlockExpr | -| main.rs:136:22:136:22 | 2 | main.rs:136:9:136:22 | BreakExpr | -| main.rs:138:5:138:5 | a | main.rs:131:38:139:1 | BlockExpr | +| main.rs:105:14:105:28 | CallExpr | main.rs:105:9:105:10 | s2 | +| main.rs:107:22:107:22 | [SSA] n | main.rs:107:33:107:33 | n | +| main.rs:107:22:107:22 | n | main.rs:107:22:107:22 | [SSA] n | +| main.rs:107:28:107:34 | CallExpr | main.rs:106:5:109:5 | MatchExpr | +| main.rs:108:25:108:31 | CallExpr | main.rs:106:5:109:5 | MatchExpr | +| main.rs:110:5:113:5 | MatchExpr | main.rs:103:37:114:1 | BlockExpr | +| main.rs:111:22:111:22 | [SSA] n | main.rs:111:33:111:33 | n | +| main.rs:111:22:111:22 | n | main.rs:111:22:111:22 | [SSA] n | +| main.rs:111:28:111:34 | CallExpr | main.rs:110:5:113:5 | MatchExpr | +| main.rs:112:25:112:31 | CallExpr | main.rs:110:5:113:5 | MatchExpr | +| main.rs:117:9:117:10 | [SSA] s1 | main.rs:119:11:119:12 | s1 | +| main.rs:117:9:117:10 | s1 | main.rs:117:9:117:10 | [SSA] s1 | +| main.rs:117:14:117:29 | CallExpr | main.rs:117:9:117:10 | s1 | +| main.rs:118:9:118:10 | [SSA] s2 | main.rs:123:11:123:12 | s2 | +| main.rs:118:9:118:10 | s2 | main.rs:118:9:118:10 | [SSA] s2 | +| main.rs:118:14:118:20 | CallExpr | main.rs:118:9:118:10 | s2 | +| main.rs:120:14:120:14 | [SSA] n | main.rs:120:25:120:25 | n | +| main.rs:120:14:120:14 | n | main.rs:120:14:120:14 | [SSA] n | +| main.rs:120:20:120:26 | CallExpr | main.rs:119:5:122:5 | MatchExpr | +| main.rs:121:17:121:23 | CallExpr | main.rs:119:5:122:5 | MatchExpr | +| main.rs:123:5:126:5 | MatchExpr | main.rs:116:39:127:1 | BlockExpr | +| main.rs:124:14:124:14 | [SSA] n | main.rs:124:25:124:25 | n | +| main.rs:124:14:124:14 | n | main.rs:124:14:124:14 | [SSA] n | +| main.rs:124:20:124:26 | CallExpr | main.rs:123:5:126:5 | MatchExpr | +| main.rs:125:17:125:23 | CallExpr | main.rs:123:5:126:5 | MatchExpr | +| main.rs:135:9:135:10 | [SSA] s1 | main.rs:137:11:137:12 | s1 | +| main.rs:135:9:135:10 | s1 | main.rs:135:9:135:10 | [SSA] s1 | +| main.rs:135:14:135:34 | CallExpr | main.rs:135:9:135:10 | s1 | +| main.rs:136:9:136:10 | [SSA] s2 | main.rs:141:11:141:12 | s2 | +| main.rs:136:9:136:10 | s2 | main.rs:136:9:136:10 | [SSA] s2 | +| main.rs:136:14:136:25 | CallExpr | main.rs:136:9:136:10 | s2 | +| main.rs:138:19:138:19 | [SSA] n | main.rs:138:30:138:30 | n | +| main.rs:138:19:138:19 | n | main.rs:138:19:138:19 | [SSA] n | +| main.rs:138:25:138:31 | CallExpr | main.rs:137:5:140:5 | MatchExpr | +| main.rs:139:19:139:19 | [SSA] n | main.rs:139:30:139:30 | n | +| main.rs:139:19:139:19 | n | main.rs:139:19:139:19 | [SSA] n | +| main.rs:139:25:139:31 | CallExpr | main.rs:137:5:140:5 | MatchExpr | +| main.rs:141:5:144:5 | MatchExpr | main.rs:134:42:145:1 | BlockExpr | +| main.rs:142:19:142:19 | [SSA] n | main.rs:142:30:142:30 | n | +| main.rs:142:19:142:19 | n | main.rs:142:19:142:19 | [SSA] n | +| main.rs:142:25:142:31 | CallExpr | main.rs:141:5:144:5 | MatchExpr | +| main.rs:143:19:143:19 | [SSA] n | main.rs:143:30:143:30 | n | +| main.rs:143:19:143:19 | n | main.rs:143:19:143:19 | [SSA] n | +| main.rs:143:25:143:31 | CallExpr | main.rs:141:5:144:5 | MatchExpr | +| main.rs:150:9:150:10 | [SSA] s1 | main.rs:152:11:152:12 | s1 | +| main.rs:150:9:150:10 | s1 | main.rs:150:9:150:10 | [SSA] s1 | +| main.rs:150:14:150:26 | CallExpr | main.rs:150:9:150:10 | s1 | +| main.rs:151:9:151:10 | [SSA] s2 | main.rs:156:11:156:12 | s2 | +| main.rs:151:9:151:10 | s2 | main.rs:151:9:151:10 | [SSA] s2 | +| main.rs:151:14:151:17 | CallExpr | main.rs:151:9:151:10 | s2 | +| main.rs:153:11:153:11 | [SSA] n | main.rs:153:22:153:22 | n | +| main.rs:153:11:153:11 | n | main.rs:153:11:153:11 | [SSA] n | +| main.rs:153:17:153:23 | CallExpr | main.rs:152:5:155:5 | MatchExpr | +| main.rs:154:11:154:11 | [SSA] n | main.rs:154:22:154:22 | n | +| main.rs:154:11:154:11 | n | main.rs:154:11:154:11 | [SSA] n | +| main.rs:154:17:154:23 | CallExpr | main.rs:152:5:155:5 | MatchExpr | +| main.rs:156:5:159:5 | MatchExpr | main.rs:149:44:160:1 | BlockExpr | +| main.rs:157:11:157:11 | [SSA] n | main.rs:157:22:157:22 | n | +| main.rs:157:11:157:11 | n | main.rs:157:11:157:11 | [SSA] n | +| main.rs:157:17:157:23 | CallExpr | main.rs:156:5:159:5 | MatchExpr | +| main.rs:158:11:158:11 | [SSA] n | main.rs:158:22:158:22 | n | +| main.rs:158:11:158:11 | n | main.rs:158:11:158:11 | [SSA] n | +| main.rs:158:17:158:23 | CallExpr | main.rs:156:5:159:5 | MatchExpr | +| main.rs:163:9:163:9 | [SSA] a | main.rs:164:5:164:5 | a | +| main.rs:163:9:163:9 | a | main.rs:163:9:163:9 | [SSA] a | +| main.rs:163:13:163:17 | BlockExpr | main.rs:163:9:163:9 | a | +| main.rs:163:15:163:15 | 0 | main.rs:163:13:163:17 | BlockExpr | +| main.rs:164:5:164:5 | a | main.rs:162:31:165:1 | BlockExpr | +| main.rs:167:22:167:22 | [SSA] b | main.rs:169:12:169:12 | b | +| main.rs:167:22:167:22 | b | main.rs:167:22:167:22 | [SSA] b | +| main.rs:167:22:167:28 | Param | main.rs:167:22:167:22 | b | +| main.rs:168:9:168:9 | [SSA] a | main.rs:174:5:174:5 | a | +| main.rs:168:9:168:9 | a | main.rs:168:9:168:9 | [SSA] a | +| main.rs:168:13:173:5 | BlockExpr | main.rs:168:9:168:9 | a | +| main.rs:170:13:170:26 | BreakExpr | main.rs:168:13:173:5 | BlockExpr | +| main.rs:170:26:170:26 | 1 | main.rs:170:13:170:26 | BreakExpr | +| main.rs:172:9:172:9 | 2 | main.rs:168:13:173:5 | BlockExpr | +| main.rs:174:5:174:5 | a | main.rs:167:38:175:1 | BlockExpr | +| main.rs:177:22:177:22 | [SSA] b | main.rs:179:12:179:12 | b | +| main.rs:177:22:177:22 | b | main.rs:177:22:177:22 | [SSA] b | +| main.rs:177:22:177:28 | Param | main.rs:177:22:177:22 | b | +| main.rs:178:9:178:9 | [SSA] a | main.rs:184:5:184:5 | a | +| main.rs:178:9:178:9 | a | main.rs:178:9:178:9 | [SSA] a | +| main.rs:178:13:183:5 | BlockExpr | main.rs:178:9:178:9 | a | +| main.rs:180:13:180:26 | BreakExpr | main.rs:178:13:183:5 | BlockExpr | +| main.rs:180:26:180:26 | 1 | main.rs:180:13:180:26 | BreakExpr | +| main.rs:182:9:182:22 | BreakExpr | main.rs:178:13:183:5 | BlockExpr | +| main.rs:182:22:182:22 | 2 | main.rs:182:9:182:22 | BreakExpr | +| main.rs:184:5:184:5 | a | main.rs:177:38:185:1 | BlockExpr | diff --git a/rust/ql/test/library-tests/dataflow/local/main.rs b/rust/ql/test/library-tests/dataflow/local/main.rs index 50aa4fdb965e..9f31b08f680f 100644 --- a/rust/ql/test/library-tests/dataflow/local/main.rs +++ b/rust/ql/test/library-tests/dataflow/local/main.rs @@ -16,24 +16,24 @@ fn direct() { } fn variable_usage() { - let s = source(1); - sink(s); // $ hasValueFlow=1 + let s = source(2); + sink(s); // $ hasValueFlow=2 } fn if_expression(cond: bool) { - let a = source(1); + let a = source(3); let b = 2; let c = if cond { a } else { b }; - sink(c); // $ hasValueFlow=1 + sink(c); // $ hasValueFlow=3 } fn match_expression(m: Option) { - let a = source(1); + let a = source(4); let b = match m { Some(_) => a, None => 0, }; - sink(b); // $ hasValueFlow=1 + sink(b); // $ hasValueFlow=4 } fn loop_with_break() { @@ -42,29 +42,29 @@ fn loop_with_break() { }; sink(a); let b = loop { - break source(1); + break source(5); }; - sink(b); // $ hasValueFlow=1 + sink(b); // $ hasValueFlow=5 } fn assignment() { let mut i = 1; sink(i); - i = source(2); - sink(i); // $ hasValueFlow=2 + i = source(6); + sink(i); // $ hasValueFlow=6 } // ----------------------------------------------------------------------------- // Data flow through data structures by writing and reading fn box_deref() { - let i = Box::new(source(1)); - sink(*i); // $ MISSING: hasValueFlow=1 + let i = Box::new(source(7)); + sink(*i); // $ MISSING: hasValueFlow=7 } fn tuple() { - let a = (source(1), 2); - sink(a.0); // $ MISSING: hasValueFlow=1 + let a = (source(8), 2); + sink(a.0); // $ MISSING: hasValueFlow=8 sink(a.1); } @@ -76,13 +76,13 @@ struct Point { fn struct_field() { let p = Point { - x: source(1), + x: source(9), y: 2, - z: source(3), + z: source(10), }; - sink(p.x); // MISSING: hasValueFlow=1 + sink(p.x); // $ MISSING: hasValueFlow=9 sink(p.y); - sink(p.z); // MISSING: hasValueFlow=3 + sink(p.z); // $ MISSING: hasValueFlow=10 } // ----------------------------------------------------------------------------- @@ -90,21 +90,34 @@ fn struct_field() { fn struct_pattern_match() { let p = Point { - x: source(1), + x: source(11), y: 2, - z: source(3), + z: source(12), }; let Point { x: a, y: b, z: c } = p; - sink(a); // MISSING: hasValueFlow=1 + sink(a); // $ MISSING: hasValueFlow=11 sink(b); - sink(c); // MISSING: hasValueFlow=3 + sink(c); // $ MISSING: hasValueFlow=12 } -fn option_pattern_match() { - let s1 = Some(source(1)); +fn option_pattern_match_qualified() { + let s1 = Option::Some(source(13)); + let s2 = Option::Some(2); + match s1 { + Option::Some(n) => sink(n), // $ MISSING: hasValueFlow=13 + Option::None => sink(0), + } + match s2 { + Option::Some(n) => sink(n), + Option::None => sink(0), + } +} + +fn option_pattern_match_unqualified() { + let s1 = Some(source(14)); let s2 = Some(2); match s1 { - Some(n) => sink(n), // MISSING: hasValueFlow=3 + Some(n) => sink(n), // $ MISSING: hasValueFlow=14 None => sink(0), } match s2 { @@ -113,6 +126,39 @@ fn option_pattern_match() { } } +enum MyEnum { + A(i64), + B(i64), +} + +fn custom_enum_pattern_match_qualified() { + let s1 = MyEnum::A(source(15)); + let s2 = MyEnum::B(2); + match s1 { + MyEnum::A(n) => sink(n), // $ MISSING: hasValueFlow=15 + MyEnum::B(n) => sink(n), + } + match s2 { + MyEnum::A(n) => sink(n), + MyEnum::B(n) => sink(n), + } +} + +use crate::MyEnum::*; + +fn custom_enum_pattern_match_unqualified() { + let s1 = A(source(16)); + let s2 = B(2); + match s1 { + A(n) => sink(n), // $ MISSING: hasValueFlow=16 + B(n) => sink(n), + } + match s2 { + A(n) => sink(n), + B(n) => sink(n), + } +} + fn block_expression1() -> i64 { let a = { 0 }; a @@ -149,7 +195,10 @@ fn main() { tuple(); struct_field(); struct_pattern_match(); - option_pattern_match(); + option_pattern_match_qualified(); + option_pattern_match_unqualified(); + custom_enum_pattern_match_qualified(); + custom_enum_pattern_match_unqualified(); block_expression1(); block_expression2(true); block_expression3(true);