|
1 | 1 | private import cpp |
2 | | -// The `ValueNumbering` library has to be imported right after `cpp` to ensure |
3 | | -// that the cached IR gets the same checksum here as it does in queries that use |
4 | | -// `ValueNumbering` without `DataFlow`. |
5 | | -private import semmle.code.cpp.ir.ValueNumbering |
6 | 2 | private import semmle.code.cpp.ir.IR |
7 | | -private import semmle.code.cpp.ir.dataflow.DataFlow |
8 | 3 | private import semmle.code.cpp.ir.dataflow.internal.DataFlowUtil |
| 4 | +private import SsaInternals as Ssa |
9 | 5 | private import PrintIRUtilities |
10 | 6 |
|
11 | 7 | /** |
12 | 8 | * Gets the local dataflow from other nodes in the same function to this node. |
13 | 9 | */ |
14 | | -private string getFromFlow(DataFlow::Node useNode, int order1, int order2) { |
15 | | - exists(DataFlow::Node defNode, string prefix | |
16 | | - ( |
17 | | - simpleLocalFlowStep(defNode, useNode) and prefix = "" |
18 | | - or |
19 | | - any(DataFlow::Configuration cfg).isAdditionalFlowStep(defNode, useNode) and |
20 | | - defNode.getEnclosingCallable() = useNode.getEnclosingCallable() and |
21 | | - prefix = "+" |
22 | | - ) and |
23 | | - if defNode.asInstruction() = useNode.asOperand().getAnyDef() |
24 | | - then |
25 | | - // Shorthand for flow from the def of this operand. |
26 | | - result = prefix + "def" and |
27 | | - order1 = -1 and |
28 | | - order2 = 0 |
29 | | - else |
30 | | - if defNode.asOperand().getUse() = useNode.asInstruction() |
31 | | - then |
32 | | - // Shorthand for flow from an operand of this instruction |
33 | | - result = prefix + defNode.asOperand().getDumpId() and |
34 | | - order1 = -1 and |
35 | | - order2 = defNode.asOperand().getDumpSortOrder() |
36 | | - else result = prefix + nodeId(defNode, order1, order2) |
| 10 | +private string getFromFlow(Node node2, int order1, int order2) { |
| 11 | + exists(Node node1 | |
| 12 | + simpleLocalFlowStep(node1, node2) and |
| 13 | + result = nodeId(node1, order1, order2) |
37 | 14 | ) |
38 | 15 | } |
39 | 16 |
|
40 | 17 | /** |
41 | 18 | * Gets the local dataflow from this node to other nodes in the same function. |
42 | 19 | */ |
43 | | -private string getToFlow(DataFlow::Node defNode, int order1, int order2) { |
44 | | - exists(DataFlow::Node useNode, string prefix | |
45 | | - ( |
46 | | - simpleLocalFlowStep(defNode, useNode) and prefix = "" |
47 | | - or |
48 | | - any(DataFlow::Configuration cfg).isAdditionalFlowStep(defNode, useNode) and |
49 | | - defNode.getEnclosingCallable() = useNode.getEnclosingCallable() and |
50 | | - prefix = "+" |
51 | | - ) and |
52 | | - if useNode.asInstruction() = defNode.asOperand().getUse() |
53 | | - then |
54 | | - // Shorthand for flow to this operand's instruction. |
55 | | - result = prefix + "result" and |
56 | | - order1 = -1 and |
57 | | - order2 = 0 |
58 | | - else result = prefix + nodeId(useNode, order1, order2) |
| 20 | +private string getToFlow(Node node1, int order1, int order2) { |
| 21 | + exists(Node node2 | |
| 22 | + simpleLocalFlowStep(node1, node2) and |
| 23 | + result = nodeId(node2, order1, order2) |
59 | 24 | ) |
60 | 25 | } |
61 | 26 |
|
62 | 27 | /** |
63 | 28 | * Gets the properties of the dataflow node `node`. |
64 | 29 | */ |
65 | | -private string getNodeProperty(DataFlow::Node node, string key) { |
| 30 | +private string getNodeProperty(Node node, string key) { |
66 | 31 | // List dataflow into and out of this node. Flow into this node is printed as `src->@`, and flow |
67 | 32 | // out of this node is printed as `@->dest`. |
68 | 33 | key = "flow" and |
69 | 34 | result = |
70 | 35 | strictconcat(string flow, boolean to, int order1, int order2 | |
71 | | - flow = getFromFlow(node, order1, order2) + "->@" and to = false |
| 36 | + flow = getFromFlow(node, order1, order2) + "->" + starsForNode(node) + "@" and to = false |
72 | 37 | or |
73 | | - flow = "@->" + getToFlow(node, order1, order2) and to = true |
| 38 | + flow = starsForNode(node) + "@->" + getToFlow(node, order1, order2) and to = true |
74 | 39 | | |
75 | 40 | flow, ", " order by to, order1, order2, flow |
76 | 41 | ) |
77 | | - or |
78 | | - // Is this node a dataflow sink? |
79 | | - key = "sink" and |
80 | | - any(DataFlow::Configuration cfg).isSink(node) and |
81 | | - result = "true" |
82 | | - or |
83 | | - // Is this node a dataflow source? |
84 | | - key = "source" and |
85 | | - any(DataFlow::Configuration cfg).isSource(node) and |
86 | | - result = "true" |
87 | | - or |
88 | | - // Is this node a dataflow barrier, and if so, what kind? |
89 | | - key = "barrier" and |
90 | | - result = |
91 | | - strictconcat(string kind | |
92 | | - any(DataFlow::Configuration cfg).isBarrier(node) and kind = "full" |
93 | | - or |
94 | | - any(DataFlow::Configuration cfg).isBarrierIn(node) and kind = "in" |
95 | | - or |
96 | | - any(DataFlow::Configuration cfg).isBarrierOut(node) and kind = "out" |
97 | | - | |
98 | | - kind, ", " |
99 | | - ) |
100 | | - // or |
101 | | - // // Is there partial flow from a source to this node? |
102 | | - // // This property will only be emitted if partial flow is enabled by overriding |
103 | | - // // `DataFlow::Configuration::explorationLimit()`. |
104 | | - // key = "pflow" and |
105 | | - // result = |
106 | | - // strictconcat(DataFlow::PartialPathNode sourceNode, DataFlow::PartialPathNode destNode, int dist, |
107 | | - // int order1, int order2 | |
108 | | - // any(DataFlow::Configuration cfg).hasPartialFlow(sourceNode, destNode, dist) and |
109 | | - // destNode.getNode() = node and |
110 | | - // // Only print flow from a source in the same function. |
111 | | - // sourceNode.getNode().getEnclosingCallable() = node.getEnclosingCallable() |
112 | | - // | |
113 | | - // nodeId(sourceNode.getNode(), order1, order2) + "+" + dist.toString(), ", " |
114 | | - // order by |
115 | | - // order1, order2, dist desc |
116 | | - // ) |
117 | 42 | } |
118 | 43 |
|
119 | 44 | /** |
120 | 45 | * Property provider for local IR dataflow. |
121 | 46 | */ |
122 | 47 | class LocalFlowPropertyProvider extends IRPropertyProvider { |
123 | 48 | override string getOperandProperty(Operand operand, string key) { |
124 | | - exists(DataFlow::Node node | |
125 | | - operand = node.asOperand() and |
| 49 | + exists(Node node | |
| 50 | + operand = [node.asOperand(), node.(RawIndirectOperand).getOperand()] and |
126 | 51 | result = getNodeProperty(node, key) |
127 | 52 | ) |
128 | 53 | } |
129 | 54 |
|
130 | 55 | override string getInstructionProperty(Instruction instruction, string key) { |
131 | | - exists(DataFlow::Node node | |
132 | | - instruction = node.asInstruction() and |
| 56 | + exists(Node node | |
| 57 | + instruction = [node.asInstruction(), node.(RawIndirectInstruction).getInstruction()] |
| 58 | + | |
133 | 59 | result = getNodeProperty(node, key) |
134 | 60 | ) |
135 | 61 | } |
| 62 | + |
| 63 | + override predicate shouldPrintOperand(Operand operand) { not Ssa::ignoreOperand(operand) } |
| 64 | + |
| 65 | + override predicate shouldPrintInstruction(Instruction instr) { not Ssa::ignoreInstruction(instr) } |
136 | 66 | } |
0 commit comments