Skip to content

Commit a0e2d59

Browse files
committed
C++: Add tests for global-var support
1 parent f40acc1 commit a0e2d59

File tree

10 files changed

+93
-0
lines changed

10 files changed

+93
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| globals.cpp:13:15:13:20 | call to getenv | globals.cpp:2:17:2:25 | sinkParam | global1 |
2+
| globals.cpp:13:15:13:20 | call to getenv | globals.cpp:12:10:12:16 | global1 | global1 |
3+
| globals.cpp:23:15:23:20 | call to getenv | globals.cpp:2:17:2:25 | sinkParam | global2 |
4+
| globals.cpp:23:15:23:20 | call to getenv | globals.cpp:19:10:19:16 | global2 | global2 |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import semmle.code.cpp.ir.dataflow.DefaultTaintTracking
2+
3+
from Expr source, Element tainted, string globalVar
4+
where
5+
taintedIncludingGlobalVars(source, tainted, globalVar) and
6+
globalVar != ""
7+
select source, tainted, globalVar
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
char * getenv(const char *);
2+
void sink(char *sinkParam);
3+
4+
void throughLocal() {
5+
char * local = getenv("VAR");
6+
sink(local); // flow
7+
}
8+
9+
char * global1 = 0;
10+
11+
void readWriteGlobal1() {
12+
sink(global1); // flow
13+
global1 = getenv("VAR");
14+
}
15+
16+
static char * global2 = 0;
17+
18+
void readGlobal2() {
19+
sink(global2); // flow
20+
}
21+
22+
void writeGlobal2() {
23+
global2 = getenv("VAR");
24+
}

cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/tainted.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@
101101
| defaulttainttracking.cpp:88:18:88:23 | call to getenv | defaulttainttracking.cpp:88:18:88:23 | call to getenv |
102102
| defaulttainttracking.cpp:88:18:88:23 | call to getenv | defaulttainttracking.cpp:88:18:88:30 | (reference to) |
103103
| defaulttainttracking.cpp:88:18:88:23 | call to getenv | test_diff.cpp:1:11:1:20 | p#0 |
104+
| globals.cpp:5:20:5:25 | call to getenv | globals.cpp:2:17:2:25 | sinkParam |
105+
| globals.cpp:5:20:5:25 | call to getenv | globals.cpp:5:12:5:16 | local |
106+
| globals.cpp:5:20:5:25 | call to getenv | globals.cpp:5:20:5:25 | call to getenv |
107+
| globals.cpp:5:20:5:25 | call to getenv | globals.cpp:6:10:6:14 | local |
108+
| globals.cpp:13:15:13:20 | call to getenv | globals.cpp:9:8:9:14 | global1 |
109+
| globals.cpp:13:15:13:20 | call to getenv | globals.cpp:13:15:13:20 | call to getenv |
110+
| globals.cpp:23:15:23:20 | call to getenv | globals.cpp:16:15:16:21 | global2 |
111+
| globals.cpp:23:15:23:20 | call to getenv | globals.cpp:23:15:23:20 | call to getenv |
104112
| test_diff.cpp:92:10:92:13 | argv | defaulttainttracking.cpp:9:11:9:20 | p#0 |
105113
| test_diff.cpp:92:10:92:13 | argv | test_diff.cpp:1:11:1:20 | p#0 |
106114
| test_diff.cpp:92:10:92:13 | argv | test_diff.cpp:92:10:92:13 | argv |

cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/test_diff.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
| defaulttainttracking.cpp:88:18:88:23 | call to getenv | defaulttainttracking.cpp:88:8:88:32 | (reference dereference) | IR only |
1616
| defaulttainttracking.cpp:88:18:88:23 | call to getenv | defaulttainttracking.cpp:88:18:88:30 | (reference to) | IR only |
1717
| defaulttainttracking.cpp:88:18:88:23 | call to getenv | test_diff.cpp:1:11:1:20 | p#0 | IR only |
18+
| globals.cpp:13:15:13:20 | call to getenv | globals.cpp:13:5:13:11 | global1 | AST only |
19+
| globals.cpp:23:15:23:20 | call to getenv | globals.cpp:23:5:23:11 | global2 | AST only |
1820
| test_diff.cpp:104:12:104:15 | argv | test_diff.cpp:104:11:104:20 | (...) | IR only |
1921
| test_diff.cpp:108:10:108:13 | argv | test_diff.cpp:36:24:36:24 | p | AST only |
2022
| test_diff.cpp:111:10:111:13 | argv | defaulttainttracking.cpp:9:11:9:20 | p#0 | AST only |

cpp/ql/test/library-tests/dataflow/dataflow-tests/IRDataflowTestCommon.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,27 @@ class TestAllocationConfig extends DataFlow::Configuration {
3636
)
3737
}
3838

39+
override predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
40+
exists(GlobalOrNamespaceVariable var | var.getName().matches("flowTestGlobal%") |
41+
writesVariable(n1.asInstruction(), var) and
42+
var = n2.asVariable()
43+
or
44+
readsVariable(n2.asInstruction(), var) and
45+
var = n1.asVariable()
46+
)
47+
}
48+
3949
override predicate isBarrier(DataFlow::Node barrier) {
4050
barrier.asExpr().(VariableAccess).getTarget().hasName("barrier")
4151
}
4252

4353
override predicate isBarrierGuard(DataFlow::BarrierGuard bg) { bg instanceof TestBarrierGuard }
4454
}
55+
56+
private predicate readsVariable(LoadInstruction load, Variable var) {
57+
load.getSourceAddress().(VariableAddressInstruction).getASTVariable() = var
58+
}
59+
60+
private predicate writesVariable(StoreInstruction store, Variable var) {
61+
store.getDestinationAddress().(VariableAddressInstruction).getASTVariable() = var
62+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int source();
2+
void sink(int);
3+
4+
void throughLocal() {
5+
int local = source();
6+
sink(local); // flow
7+
}
8+
9+
int flowTestGlobal1 = 0;
10+
11+
void readWriteGlobal1() {
12+
sink(flowTestGlobal1); // flow
13+
flowTestGlobal1 = source();
14+
}
15+
16+
static int flowTestGlobal2 = 0;
17+
18+
void readGlobal2() {
19+
sink(flowTestGlobal2); // flow
20+
}
21+
22+
void writeGlobal2() {
23+
flowTestGlobal2 = source();
24+
}

cpp/ql/test/library-tests/dataflow/dataflow-tests/test.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| dispatch.cpp:36:16:36:25 | call to notSource2 | dispatch.cpp:10:37:10:42 | call to source |
2323
| dispatch.cpp:43:15:43:24 | call to notSource1 | dispatch.cpp:9:37:9:42 | call to source |
2424
| dispatch.cpp:44:15:44:24 | call to notSource2 | dispatch.cpp:10:37:10:42 | call to source |
25+
| globals.cpp:6:10:6:14 | local | globals.cpp:5:17:5:22 | call to source |
2526
| lambdas.cpp:14:3:14:6 | t | lambdas.cpp:8:10:8:15 | call to source |
2627
| lambdas.cpp:18:8:18:8 | call to operator() | lambdas.cpp:8:10:8:15 | call to source |
2728
| lambdas.cpp:21:3:21:6 | t | lambdas.cpp:8:10:8:15 | call to source |

cpp/ql/test/library-tests/dataflow/dataflow-tests/test_diff.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
| dispatch.cpp:107:17:107:22 | dispatch.cpp:96:8:96:8 | IR only |
1818
| dispatch.cpp:140:8:140:13 | dispatch.cpp:96:8:96:8 | IR only |
1919
| dispatch.cpp:144:8:144:13 | dispatch.cpp:96:8:96:8 | IR only |
20+
| globals.cpp:13:23:13:28 | globals.cpp:12:10:12:24 | IR only |
21+
| globals.cpp:23:23:23:28 | globals.cpp:19:10:19:24 | IR only |
2022
| lambdas.cpp:8:10:8:15 | lambdas.cpp:14:3:14:6 | AST only |
2123
| lambdas.cpp:8:10:8:15 | lambdas.cpp:18:8:18:8 | AST only |
2224
| lambdas.cpp:8:10:8:15 | lambdas.cpp:21:3:21:6 | AST only |

cpp/ql/test/library-tests/dataflow/dataflow-tests/test_ir.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
| dispatch.cpp:96:8:96:8 | x | dispatch.cpp:107:17:107:22 | call to source |
3636
| dispatch.cpp:96:8:96:8 | x | dispatch.cpp:140:8:140:13 | call to source |
3737
| dispatch.cpp:96:8:96:8 | x | dispatch.cpp:144:8:144:13 | call to source |
38+
| globals.cpp:6:10:6:14 | local | globals.cpp:5:17:5:22 | call to source |
39+
| globals.cpp:12:10:12:24 | flowTestGlobal1 | globals.cpp:13:23:13:28 | call to source |
40+
| globals.cpp:19:10:19:24 | flowTestGlobal2 | globals.cpp:23:23:23:28 | call to source |
3841
| lambdas.cpp:35:8:35:8 | a | lambdas.cpp:8:10:8:15 | call to source |
3942
| test.cpp:7:8:7:9 | t1 | test.cpp:6:12:6:17 | call to source |
4043
| test.cpp:9:8:9:9 | t1 | test.cpp:6:12:6:17 | call to source |

0 commit comments

Comments
 (0)