-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariables.qll
More file actions
220 lines (185 loc) · 5.93 KB
/
Variables.qll
File metadata and controls
220 lines (185 loc) · 5.93 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
/**
* Bicep variable declarations.
*/
private import bicep
private import AstNodes
private import Calls
private import Idents
private import Stmts
private import codeql.bicep.controlflow.BasicBlocks as BasicBlocks
private import codeql.bicep.controlflow.ControlFlowGraph
// Internal
private import internal.VariableDeclaration
/**
* A VariableDeclaration unknown AST node.
*/
class VariableDeclaration extends AstNode instanceof VariableDeclarationImpl {
/**
* Gets the identifier of the variable declaration.
*/
Idents getIdentifier() { result = VariableDeclarationImpl.super.getIdentifier() }
/**
* Gets the initializer expression of the variable declaration.
*/
Expr getInitializer() { result = VariableDeclarationImpl.super.getInitializer() }
}
private predicate variableDecl(AstNode node, string name) {
exists(ParameterDeclaration param |
param.getName() = name and
node = param
)
or
exists(VariableDeclaration vardelc |
vardelc.getIdentifier().getName() = name and
node = vardelc
)
or
exists(Resource resource |
resource.getIdentifier().getName() = name and
node = resource.getResourceDeclaration()
)
or
exists(OutputDeclaration output |
output.getIdentifier().getName() = name and
node = output
)
}
/**
* Variable represents a variable defination.
*/
class Variable extends MkVariable {
private AstNode node;
private string name;
Variable() { this = MkVariable(node, name) }
string getName() { result = name }
string toString() { result = "Variable[" + name + "]" }
AstNode getAstNode() { result = node }
/**
* Get the location of this variable.
*/
Location getLocation() { result = node.getLocation() }
/**
* Geta the inner variable of this variable.
*/
VariableAccess getAnAccess() { result.getVariable() = this }
/**
* Gets the type of this variable, if any.
*/
Type getType() {
result = this.getParameter().getType()
or
result = this.getOutput().getType()
}
/**
* Gets the parameter of this variable, if any.
*/
ParameterDeclaration getParameter() {
exists(ParameterDeclaration param |
param.getName() = this.getName() and
param.getEnclosingCfgScope() = this.getEnclosingCfgScope() and
result = param
)
}
/**
* Gets the variable declaration of this variable, if any.
*/
OutputDeclaration getOutput() {
exists(OutputDeclaration output |
output.getIdentifier().getName() = this.getName() and
output.getEnclosingCfgScope() = this.getEnclosingCfgScope() and
result = output
)
}
/**
* Gets the enclosing scope of this variable, if any.
*/
CfgScope getEnclosingCfgScope() { result = node.getEnclosingCfgScope() }
// Expr getInitializer() { }
string getAPrimaryQlClass() { result = "Variable" }
}
private predicate access(AstNode node, Variable v, string name) {
exists(Identifier ident |
ident.getName() = name and
// Make sure they are not in a declare statement
not ident.getParent() instanceof VariableDeclaration and
// not ident.getParent() instanceof ParameterDeclaration and
// not ident.getParent() instanceof OutputDeclaration and
// Make sure they are in the same scope
ident.getName() = v.getName() and
ident.getEnclosingCfgScope() = v.getEnclosingCfgScope() and
ident = node
)
}
/**
* VariableAccess is a class that represents the access to a variable.
*/
class VariableAccess extends MkVariableAccess, TVariableAccess {
private string name;
private AstNode node;
private Variable v;
VariableAccess() { this = MkVariableAccess(node, v, name) }
string getName() { result = name }
AstNode getAstNode() { result = node }
Variable getVariable() { result = v }
string toString() { result = "VariableAccess[" + name + "]" }
/**
* Get the location of this variable.
*/
Location getLocation() { result = node.getLocation() }
/**
* Gets the type of this variable, if any.
*/
Type getType() { result = this.getVariable().getType() }
/**
* Gets the enclosing scope of this variable, if any.
*/
CfgScope getEnclosingCfgScope() { result = v.getEnclosingCfgScope() }
string getAPrimaryQlClass() { result = "VariableAccess" }
}
class VariableWriteAccess extends VariableAccess {
VariableWriteAccess() {
// Parameter
this.getAstNode().getParent() instanceof ParameterDeclaration
or
// SET
this.getAstNode().getParent() instanceof VariableDeclaration
or
this.getAstNode().getParent() instanceof ResourceDeclaration
or
// Output
this.getAstNode().getParent() instanceof OutputDeclaration
}
override string getAPrimaryQlClass() { result = "VariableWrite" }
}
class VariableReadAccess extends VariableAccess {
VariableReadAccess() { not this instanceof VariableWriteAccess }
override string getAPrimaryQlClass() { result = "VariableRead" }
}
/**
* Holds if the variable is written too.
*/
// private predicate variableWrite(Variable node) {
// exists(Parameter param |
// param.getName() = node.getName() and
// param.getEnclosingCfgScope() = node.getEnclosingCfgScope()
// )
// }
cached
private module Cached {
cached
newtype TVariable =
TResource(Resource resource, string name) { resource.getIdentifier().getName() = name } or
TVariableDecl(VariableDeclaration varDecl, string name) {
varDecl.getIdentifier().getName() = name
} or
TParameter(ParameterDeclaration param, string name) { param.getName() = name } or
TOutput(OutputDeclaration output, string name) { output.getIdentifier().getName() = name } or
MkVariable(AstNode definingNode, string name) { variableDecl(definingNode, name) }
cached
newtype TVariableAccess =
TIdent(Identifier ident, string name) { ident.getName() = name } or
MkVariableAccess(AstNode node, Variable v, string name) { variableAccess(node, v, name) }
cached
predicate variableAccess(AstNode node, Variable v, string name) { access(node, v, name) }
}
private import Cached