Skip to content

Commit c8783c1

Browse files
committed
Rust: Add source/sink/barrier MaD trait tests
1 parent b756a08 commit c8783c1

8 files changed

Lines changed: 385 additions & 267 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub trait MyBarrierTrait2 {
2+
fn sanitize2(s: &str);
3+
}
4+
5+
impl<T> MyBarrierTrait2 for T {
6+
// inherits model from the trait function
7+
fn sanitize2(s: &str) {}
8+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ edges
44
| main.rs:21:13:21:21 | source(...) | main.rs:21:9:21:9 | s | provenance | |
55
| main.rs:32:9:32:9 | s | main.rs:33:10:33:10 | s | provenance | |
66
| main.rs:32:13:32:21 | source(...) | main.rs:32:9:32:9 | s | provenance | |
7+
| main.rs:63:9:63:9 | s | main.rs:65:10:65:10 | s | provenance | |
8+
| main.rs:63:13:63:21 | source(...) | main.rs:63:9:63:9 | s | provenance | |
79
nodes
810
| main.rs:17:10:17:18 | source(...) | semmle.label | source(...) |
911
| main.rs:21:9:21:9 | s | semmle.label | s |
@@ -12,9 +14,13 @@ nodes
1214
| main.rs:32:9:32:9 | s | semmle.label | s |
1315
| main.rs:32:13:32:21 | source(...) | semmle.label | source(...) |
1416
| main.rs:33:10:33:10 | s | semmle.label | s |
17+
| main.rs:63:9:63:9 | s | semmle.label | s |
18+
| main.rs:63:13:63:21 | source(...) | semmle.label | source(...) |
19+
| main.rs:65:10:65:10 | s | semmle.label | s |
1520
subpaths
1621
testFailures
1722
#select
1823
| main.rs:17:10:17:18 | source(...) | main.rs:17:10:17:18 | source(...) | main.rs:17:10:17:18 | source(...) | $@ | main.rs:17:10:17:18 | source(...) | source(...) |
1924
| main.rs:22:10:22:10 | s | main.rs:21:13:21:21 | source(...) | main.rs:22:10:22:10 | s | $@ | main.rs:21:13:21:21 | source(...) | source(...) |
2025
| main.rs:33:10:33:10 | s | main.rs:32:13:32:21 | source(...) | main.rs:33:10:33:10 | s | $@ | main.rs:32:13:32:21 | source(...) | source(...) |
26+
| main.rs:65:10:65:10 | s | main.rs:63:13:63:21 | source(...) | main.rs:65:10:65:10 | s | $@ | main.rs:63:13:63:21 | source(...) | source(...) |

rust/ql/test/library-tests/dataflow/barrier/inline-flow.ext.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ extensions:
44
extensible: barrierModel
55
data:
66
- ["main::sanitize", "ReturnValue", "test-barrier", "manual"]
7+
- ["main::external_file::MyBarrierTrait2::sanitize2", "Argument[0]", "test-barrier", "manual"]
8+
- ["<_ as main::MyBarrierTrait3>::sanitize3", "Argument[0]", "test-barrier", "manual"]
79
- addsTo:
810
pack: codeql/rust-all
911
extensible: barrierGuardModel
1012
data:
1113
- ["main::verify_safe", "Argument[0]", "true", "test-barrier", "manual"]
14+
- addsTo:
15+
pack: codeql/rust-all
16+
extensible: additionalExternalFile
17+
data:
18+
- ["external_file.rs"]
19+

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,23 @@ fn with_barrier_guard() {
4646
sink(s);
4747
}
4848
}
49+
50+
mod external_file;
51+
use external_file::*;
52+
53+
trait MyBarrierTrait3 {
54+
fn sanitize3(s: &str);
55+
}
56+
57+
impl<T> MyBarrierTrait3 for T {
58+
// has an explicit model
59+
fn sanitize3(s: &str) {}
60+
}
61+
62+
fn with_trait_barriers() {
63+
let s = source(2);
64+
<()>::sanitize2(s);
65+
sink(s); // $ SPURIOUS: hasValueFlow=2
66+
<()>::sanitize3(s);
67+
sink(s);
68+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,23 @@ impl<T> MyTrait2 for T {
3838
0
3939
}
4040
}
41+
42+
pub trait MySourceTrait2 {
43+
fn produce2(i: i64) -> i64;
44+
}
45+
46+
impl<T> MySourceTrait2 for T {
47+
// inherits model from the trait function
48+
fn produce2(i: i64) -> i64 {
49+
0
50+
}
51+
}
52+
53+
pub trait MySinkTrait2 {
54+
fn consume2(i: i64);
55+
}
56+
57+
impl<T> MySinkTrait2 for T {
58+
// inherits model from the trait function
59+
fn consume2(i: i64) {}
60+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,26 @@ impl<T> MyTrait3 for T {
471471
}
472472
}
473473

474+
trait MySourceTrait3 {
475+
fn produce3(i: i64) -> i64;
476+
}
477+
478+
impl<T> MySourceTrait3 for T {
479+
// has an explicit model
480+
fn produce3(i: i64) -> i64 {
481+
0
482+
}
483+
}
484+
485+
trait MySinkTrait3 {
486+
fn consume3(i: i64);
487+
}
488+
489+
impl<T> MySinkTrait3 for T {
490+
// has an explicit model
491+
fn consume3(i: i64) {}
492+
}
493+
474494
fn test_trait_model<T: Ord>(x: T) {
475495
let x1 = source(20).max(0);
476496
sink(x1); // $ hasValueFlow=20
@@ -505,6 +525,16 @@ fn test_trait_model<T: Ord>(x: T) {
505525

506526
let x9 = <()>::flow_through3(source(30));
507527
sink(x9); // $ hasValueFlow=30
528+
529+
let x10 = <()>::produce2(31);
530+
sink(x10); // $ MISSING: hasValueFlow=31
531+
532+
let x11 = <()>::produce3(32);
533+
sink(x11); // $ hasValueFlow=32
534+
535+
<()>::consume2(source(33)); // $ MISSING: hasValueFlow=33
536+
537+
<()>::consume3(source(34)); // $ hasValueFlow=34
508538
}
509539

510540
mod external_file;

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

Lines changed: 289 additions & 267 deletions
Large diffs are not rendered by default.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ extensions:
1414
- ["main::external_file::generated_source", "ReturnValue", "test-source", "dfc-generated"] # not actually generated, but we want to test behaviour of generated models here.
1515
- ["main::external_file::neutral_generated_source", "ReturnValue", "test-source", "dfc-generated"]
1616
- ["main::external_file::neutral_manual_source", "ReturnValue", "test-source", "manual"]
17+
- ["main::external_file::MySourceTrait2::produce2", "ReturnValue", "test-source", "manual"]
18+
- ["<_ as main::MySourceTrait3>::produce3", "ReturnValue", "test-source", "manual"]
1719
- addsTo:
1820
pack: codeql/rust-all
1921
extensible: sinkModel
@@ -27,6 +29,8 @@ extensions:
2729
- ["main::external_file::generated_sink", "Argument[0]", "test-sink", "dfc-generated"]
2830
- ["main::external_file::neutral_generated_sink", "Argument[0]", "test-sink", "dfc-generated"]
2931
- ["main::external_file::neutral_manual_sink", "Argument[0]", "test-sink", "manual"]
32+
- ["main::external_file::MySinkTrait2::consume2", "Argument[0]", "test-sink", "manual"]
33+
- ["<_ as main::MySinkTrait3>::consume3", "Argument[0]", "test-sink", "manual"]
3034
- addsTo:
3135
pack: codeql/rust-all
3236
extensible: summaryModel

0 commit comments

Comments
 (0)