|
1 | | -import DataflowTestCommon |
| 1 | +import TestUtilities.dataflow.FlowTestCommon |
2 | 2 |
|
3 | | -class ASTDataFlowTest extends InlineExpectationsTest { |
4 | | - ASTDataFlowTest() { this = "ASTDataFlowTest" } |
| 3 | +module ASTTest { |
| 4 | + private import semmle.code.cpp.dataflow.DataFlow |
5 | 5 |
|
6 | | - override string getARelevantTag() { result = "ast" } |
| 6 | + /** |
| 7 | + * A `BarrierGuard` that stops flow to all occurrences of `x` within statement |
| 8 | + * S in `if (guarded(x)) S`. |
| 9 | + */ |
| 10 | + // This is tested in `BarrierGuard.cpp`. |
| 11 | + class TestBarrierGuard extends DataFlow::BarrierGuard { |
| 12 | + TestBarrierGuard() { this.(FunctionCall).getTarget().getName() = "guarded" } |
7 | 13 |
|
8 | | - override predicate hasActualResult(Location location, string element, string tag, string value) { |
9 | | - exists(DataFlow::Node source, DataFlow::Node sink, TestAllocationConfig conf, int n | |
10 | | - tag = "ast" and |
11 | | - conf.hasFlow(source, sink) and |
12 | | - n = strictcount(DataFlow::Node otherSource | conf.hasFlow(otherSource, sink)) and |
13 | | - ( |
14 | | - n = 1 and value = "" |
| 14 | + override predicate checks(Expr checked, boolean isTrue) { |
| 15 | + checked = this.(FunctionCall).getArgument(0) and |
| 16 | + isTrue = true |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + /** Common data flow configuration to be used by tests. */ |
| 21 | + class ASTTestAllocationConfig extends DataFlow::Configuration { |
| 22 | + ASTTestAllocationConfig() { this = "ASTTestAllocationConfig" } |
| 23 | + |
| 24 | + override predicate isSource(DataFlow::Node source) { |
| 25 | + source.asExpr().(FunctionCall).getTarget().getName() = "source" |
| 26 | + or |
| 27 | + source.asParameter().getName().matches("source%") |
| 28 | + or |
| 29 | + source.(DataFlow::DefinitionByReferenceNode).getParameter().getName().matches("ref_source%") |
| 30 | + or |
| 31 | + // Track uninitialized variables |
| 32 | + exists(source.asUninitialized()) |
| 33 | + } |
| 34 | + |
| 35 | + override predicate isSink(DataFlow::Node sink) { |
| 36 | + exists(FunctionCall call | |
| 37 | + call.getTarget().getName() = "sink" and |
| 38 | + sink.asExpr() = call.getAnArgument() |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + override predicate isBarrier(DataFlow::Node barrier) { |
| 43 | + barrier.asExpr().(VariableAccess).getTarget().hasName("barrier") |
| 44 | + } |
| 45 | + |
| 46 | + override predicate isBarrierGuard(DataFlow::BarrierGuard bg) { bg instanceof TestBarrierGuard } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +module IRTest { |
| 51 | + private import semmle.code.cpp.ir.dataflow.DataFlow |
| 52 | + private import semmle.code.cpp.ir.IR |
| 53 | + |
| 54 | + /** |
| 55 | + * A `BarrierGuard` that stops flow to all occurrences of `x` within statement |
| 56 | + * S in `if (guarded(x)) S`. |
| 57 | + */ |
| 58 | + // This is tested in `BarrierGuard.cpp`. |
| 59 | + class TestBarrierGuard extends DataFlow::BarrierGuard { |
| 60 | + TestBarrierGuard() { this.(CallInstruction).getStaticCallTarget().getName() = "guarded" } |
| 61 | + |
| 62 | + override predicate checksInstr(Instruction checked, boolean isTrue) { |
| 63 | + checked = this.(CallInstruction).getPositionalArgument(0) and |
| 64 | + isTrue = true |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** Common data flow configuration to be used by tests. */ |
| 69 | + class IRTestAllocationConfig extends DataFlow::Configuration { |
| 70 | + IRTestAllocationConfig() { this = "IRTestAllocationConfig" } |
| 71 | + |
| 72 | + override predicate isSource(DataFlow::Node source) { |
| 73 | + source.asExpr().(FunctionCall).getTarget().getName() = "source" |
| 74 | + or |
| 75 | + source.asParameter().getName().matches("source%") |
| 76 | + } |
| 77 | + |
| 78 | + override predicate isSink(DataFlow::Node sink) { |
| 79 | + exists(FunctionCall call | |
| 80 | + call.getTarget().getName() = "sink" and |
| 81 | + sink.asExpr() = call.getAnArgument() |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + override predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) { |
| 86 | + exists(GlobalOrNamespaceVariable var | var.getName().matches("flowTestGlobal%") | |
| 87 | + writesVariable(n1.asInstruction(), var) and |
| 88 | + var = n2.asVariable() |
15 | 89 | or |
16 | | - // If there is more than one source for this sink |
17 | | - // we specify the source location explicitly. |
18 | | - n > 1 and |
19 | | - value = |
20 | | - source.getLocation().getStartLine().toString() + ":" + |
21 | | - source.getLocation().getStartColumn() |
22 | | - ) and |
23 | | - location = sink.getLocation() and |
24 | | - element = sink.toString() |
25 | | - ) |
| 90 | + readsVariable(n2.asInstruction(), var) and |
| 91 | + var = n1.asVariable() |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + override predicate isBarrier(DataFlow::Node barrier) { |
| 96 | + barrier.asExpr().(VariableAccess).getTarget().hasName("barrier") |
| 97 | + } |
| 98 | + |
| 99 | + override predicate isBarrierGuard(DataFlow::BarrierGuard bg) { bg instanceof TestBarrierGuard } |
| 100 | + } |
| 101 | + |
| 102 | + private predicate readsVariable(LoadInstruction load, Variable var) { |
| 103 | + load.getSourceAddress().(VariableAddressInstruction).getASTVariable() = var |
| 104 | + } |
| 105 | + |
| 106 | + private predicate writesVariable(StoreInstruction store, Variable var) { |
| 107 | + store.getDestinationAddress().(VariableAddressInstruction).getASTVariable() = var |
26 | 108 | } |
27 | 109 | } |
0 commit comments