forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rs
More file actions
435 lines (359 loc) · 8.44 KB
/
main.rs
File metadata and controls
435 lines (359 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
fn source(i: i64) -> i64 {
1000 + i
}
fn sink<T: std::fmt::Debug>(s: T) {
println!("{:?}", s);
}
// has a flow model
fn identity(i: i64) -> i64 {
0
}
fn test_identify() {
let s = source(1);
sink(identity(s)); // $ hasValueFlow=1
}
// has a flow model
fn coerce(_i: i64) -> i64 {
0
}
fn test_coerce() {
let s = source(14);
sink(coerce(s)); // $ hasTaintFlow=14
}
enum MyPosEnum {
A(i64),
B(i64),
}
// has a manual flow model with flow from second argument to the return value
// and a wrong generated model with flow from first argument to the return value
fn snd(a: i64, b: i64) -> i64 {
0
}
fn test_snd() {
let s1 = source(99);
sink(snd(0, s1)); // $ hasValueFlow=99
let s2 = source(88);
sink(snd(s2, 0));
}
// has a flow model
fn get_var_pos(e: MyPosEnum) -> i64 {
0
}
fn test_get_var_pos() {
let s = source(2);
let e1 = MyPosEnum::A(s);
sink(get_var_pos(e1)); // $ hasValueFlow=2
let e2 = MyPosEnum::B(s);
sink(get_var_pos(e2));
}
// has a flow model
fn set_var_pos(i: i64) -> MyPosEnum {
MyPosEnum::A(0)
}
fn test_set_var_pos() {
let s = source(3);
let e1 = set_var_pos(s);
match e1 {
MyPosEnum::A(i) => sink(i),
MyPosEnum::B(i) => sink(i), // $ hasValueFlow=3
}
}
enum MyFieldEnum {
C { field_c: i64 },
D { field_d: i64 },
}
// has a flow model
fn get_var_field(e: MyFieldEnum) -> i64 {
0
}
fn test_get_var_field() {
let s = source(4);
let e1 = MyFieldEnum::C { field_c: s };
sink(get_var_field(e1)); // $ hasValueFlow=4
let e2 = MyFieldEnum::D { field_d: s };
sink(get_var_field(e2));
}
// has a flow model
fn set_var_field(i: i64) -> MyFieldEnum {
MyFieldEnum::C { field_c: 0 }
}
fn test_set_var_field() {
let s = source(5);
let e1 = set_var_field(s);
match e1 {
MyFieldEnum::C { field_c: i } => sink(i),
MyFieldEnum::D { field_d: i } => sink(i), // $ hasValueFlow=5
}
}
struct MyStruct {
field1: i64,
field2: i64,
}
// has a flow model
fn get_struct_field(s: MyStruct) -> i64 {
0
}
fn test_get_struct_field() {
let s = source(6);
let my_struct = MyStruct {
field1: s,
field2: 0,
};
sink(get_struct_field(my_struct)); // $ hasValueFlow=6
let my_struct2 = MyStruct {
field1: 0,
field2: s,
};
sink(get_struct_field(my_struct2));
}
// has a flow model
fn set_struct_field(i: i64) -> MyStruct {
MyStruct {
field1: 0,
field2: 1,
}
}
fn test_set_struct_field() {
let s = source(7);
let my_struct = set_struct_field(s);
sink(my_struct.field1);
sink(my_struct.field2); // $ hasValueFlow=7
}
// has a flow model
fn get_array_element(a: [i64; 1]) -> i64 {
0
}
fn test_get_array_element() {
let s = source(8);
sink(get_array_element([s])); // $ hasValueFlow=8
}
// has a flow model
fn set_array_element(i: i64) -> [i64; 1] {
[0]
}
fn test_set_array_element() {
let s = source(9);
let arr = set_array_element(s);
sink(arr[0]); // $ hasValueFlow=9
}
// has a flow model
fn get_tuple_element(a: (i64, i64)) -> i64 {
0
}
fn test_get_tuple_element() {
let s = source(10);
let t = (s, 0);
sink(get_tuple_element(t)); // $ hasValueFlow=10
let t = (0, s);
sink(get_tuple_element(t));
}
// has a flow model
fn set_tuple_element(i: i64) -> (i64, i64) {
(0, 1)
}
fn test_set_tuple_element() {
let s = source(11);
let t = set_tuple_element(s);
sink(t.0);
sink(t.1); // $ hasValueFlow=11
}
// has a flow model
pub fn apply<F>(n: i64, f: F) -> i64
where
F: FnOnce(i64) -> i64,
{
0
}
fn test_apply_flow_in() {
let s = source(83);
let f = |n| {
sink(n); // $ hasValueFlow=83
n + 3
};
apply(s, f);
}
fn test_apply_flow_out() {
let s = source(86);
let f = |n| if n != 0 { n } else { s };
let t = apply(34, f);
sink(t); // $ hasValueFlow=86
}
fn test_apply_flow_through() {
let s = source(33);
let f = |n| if n != 0 { n } else { 0 };
let t = apply(s, f);
sink(t); // $ hasValueFlow=33
}
// has a flow model with value flow from argument to returned future
async fn get_async_number(a: i64) -> i64 {
37
}
async fn test_get_async_number() {
let s = source(46);
let t = get_async_number(s).await;
sink(t); // $ hasValueFlow=46
}
impl MyFieldEnum {
// has a source model
fn source(&self, i: i64) -> MyFieldEnum {
MyFieldEnum::C { field_c: 0 }
}
// has a sink model
fn sink(self) {}
}
// has a source model
fn enum_source(i: i64) -> MyFieldEnum {
MyFieldEnum::C { field_c: 0 }
}
fn test_enum_source() {
let s = enum_source(12);
match s {
MyFieldEnum::C { field_c: i } => sink(i),
MyFieldEnum::D { field_d: i } => sink(i), // $ hasValueFlow=12
}
}
fn test_enum_method_source() {
let e = MyFieldEnum::D { field_d: 0 };
let s = e.source(13);
match s {
MyFieldEnum::C { field_c: i } => sink(i), // $ hasValueFlow=13
MyFieldEnum::D { field_d: i } => sink(i),
}
}
mod source_into_function {
use super::sink;
// has a source model
fn pass_source<A>(_i: i64, f: impl FnOnce(i64) -> A) -> A {
f(42)
}
fn test_source_into_function() {
let a = |a| sink(a); // $ hasValueFlow=1
pass_source(1, a);
pass_source(2, |a| {
sink(a); // $ hasValueFlow=2
});
fn f(a: i64) {
sink(a) // $ hasValueFlow=3
}
pass_source(3, f);
pass_source(4, async move |a| {
sink(a); // $ hasValueFlow=4
});
}
}
// has a sink model
fn enum_sink(e: MyFieldEnum) {}
fn test_enum_sink() {
let s = source(14);
enum_sink(MyFieldEnum::C { field_c: s }); // $ hasValueFlow=14
enum_sink(MyFieldEnum::D { field_d: s });
}
fn test_enum_method_sink() {
let s = source(15);
let e = MyFieldEnum::D { field_d: s };
e.sink(); // $ hasValueFlow=15
}
// has a source model
fn simple_source(i: i64) -> i64 {
0
}
fn test_simple_source() {
let s = simple_source(16);
sink(s) // $ hasValueFlow=16
}
// has a sink model
fn simple_sink(i: i64) {}
fn test_simple_sink() {
let s = source(17);
simple_sink(s); // $ hasValueFlow=17
}
// has a source model
fn arg_source(i: i64) {}
fn test_arg_source() {
let i = 19;
arg_source(i);
sink(i) // $ hasValueFlow=i
}
struct MyStruct2(i64);
impl PartialEq for MyStruct {
fn eq(&self, other: &Self) -> bool {
true
}
}
impl PartialEq for MyStruct2 {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for MyStruct {}
impl Eq for MyStruct2 {}
use std::cmp::Ordering;
impl PartialOrd for MyStruct {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ordering::Equal)
}
}
impl PartialOrd for MyStruct2 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.0.cmp(&other.0))
}
}
impl Ord for MyStruct {
fn cmp(&self, other: &Self) -> Ordering {
Ordering::Equal
}
}
impl Ord for MyStruct2 {
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
fn max(self, other: Self) -> Self {
other
}
}
fn test_trait_model<T: Ord>(x: T) {
let x1 = source(20).max(0);
sink(x1); // $ hasValueFlow=20
let x2 = (MyStruct {
field1: source(23),
field2: 0,
})
.max(MyStruct {
field1: 0,
field2: 0,
});
sink(x2.field1); // $ hasValueFlow=23
let x3 = MyStruct2(source(24)).max(MyStruct2(0));
sink(x3.0); // no flow, because the model does not apply when the target is in source code
let x4 = source(25).max(1);
sink(x4); // $ hasValueFlow=25
let x5 = source(26).lt(&1);
sink(x5); // $ hasTaintFlow=26
let x6 = source(27) < 1;
sink(x6); // $ hasTaintFlow=27
let x7 = (source(28) as i32) < 1;
sink(x7);
}
#[tokio::main]
async fn main() {
test_identify();
test_get_var_pos();
test_set_var_pos();
test_get_var_field();
test_set_var_field();
test_get_struct_field();
test_set_struct_field();
test_get_array_element();
test_set_array_element();
test_get_tuple_element();
test_set_tuple_element();
test_enum_source();
test_enum_method_source();
test_enum_sink();
test_enum_method_sink();
test_simple_source();
test_simple_sink();
test_get_async_number().await;
test_arg_source();
let dummy = Some(0); // ensure that the the `lang:core` crate is extracted
}