-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRA.qll
More file actions
284 lines (219 loc) · 8.18 KB
/
RA.qll
File metadata and controls
284 lines (219 loc) · 8.18 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
overlay[local]
module;
/**
* A predicate that contains RA.
*/
signature class RApredicate {
string getLineOfRA(int n);
}
/**
* Parses strings of RA provided by an RA predicate, and represented the
*/
module RAParser<RApredicate Predicate> {
private string parseRaExpr(Predicate p, int line, int arity, int lhs) {
exists(string str | str = p.getLineOfRA(line).trim() |
arity = str.regexpCapture("\\{([0-9]+)\\} r([0-9]+) = (.+)", 1).toInt() and
lhs = str.regexpCapture("\\{([0-9]+)\\} r([0-9]+) = (.+)", 2).toInt() and
result = str.regexpCapture("\\{([0-9]+)\\} r([0-9]+) = (.+)", 3)
)
}
bindingset[str]
private int parseReturn(string str) {
result = str.trim().regexpCapture("return r([0-9]+)", 1).toInt()
}
bindingset[str]
private predicate parseScan(string str, int arity, int lhs, string rhs) {
exists(string r, string trimmed |
r = "\\{(\\d+)\\}\\s+r(\\d+)\\s+=\\s+SCAN\\s+([0-9a-zA-Z:#_]+)\\s.*" and
trimmed = str.trim()
|
arity = trimmed.regexpCapture(r, 1).toInt() and
lhs = trimmed.regexpCapture(r, 2).toInt() and
rhs = trimmed.regexpCapture(r, 3)
)
}
bindingset[str]
private predicate parseJoin(string str, int arity, int lhs, string left, string right) {
exists(string r, string trimmed |
r =
"\\{(\\d+)\\}\\s+r(\\d+)\\s+=\\s+JOIN\\s+([0-9a-zA-Z:#_]+)\\s+WITH\\s+([0-9a-zA-Z:#_]+)\\s.*" and
trimmed = str.trim()
|
arity = trimmed.regexpCapture(r, 1).toInt() and
lhs = trimmed.regexpCapture(r, 2).toInt() and
left = trimmed.regexpCapture(r, 3) and
right = trimmed.regexpCapture(r, 4)
)
}
bindingset[str]
private predicate parseSelect(string str, int arity, int lhs, string rhs) {
exists(string r, string trimmed |
r = "\\{(\\d+)\\}\\s+r(\\d+)\\s+=\\s+SELECT\\s+([0-9a-zA-Z:#_]+).*" and
trimmed = str.trim()
|
arity = trimmed.regexpCapture(r, 1).toInt() and
lhs = trimmed.regexpCapture(r, 2).toInt() and
rhs = trimmed.regexpCapture(r, 3)
)
}
bindingset[str]
private predicate parseAntiJoin(string str, int arity, int lhs, string left, string right) {
exists(string r, string trimmed |
r = "\\{(\\d+)\\}\\s+r(\\d+)\\s+=\\s+([0-9a-zA-Z:#_]+)\\s+AND\\s+NOT\\s+([0-9a-zA-Z:#_]+).*" and
trimmed = str.trim()
|
arity = trimmed.regexpCapture(r, 1).toInt() and
lhs = trimmed.regexpCapture(r, 2).toInt() and
left = trimmed.regexpCapture(r, 3) and
right = trimmed.regexpCapture(r, 4)
)
}
private newtype TRA =
TReturn(Predicate p, int line, int v) { v = parseReturn(p.getLineOfRA(line)) } or
TScan(Predicate p, int line, int arity, int lhs, string rhs) {
parseScan(p.getLineOfRA(line), arity, lhs, rhs)
} or
TJoin(Predicate p, int line, int arity, int lhs, string left, string right) {
parseJoin(p.getLineOfRA(line), arity, lhs, left, right)
} or
TSelect(Predicate p, int line, int arity, int lhs, string rhs) {
parseSelect(p.getLineOfRA(line), arity, lhs, rhs)
} or
TAntiJoin(Predicate p, int line, int arity, int lhs, string left, string right) {
parseAntiJoin(p.getLineOfRA(line), arity, lhs, left, right)
} or
TUnknown(Predicate p, int line, int lhs, int arity, string rhs) {
rhs = parseRaExpr(p, line, arity, lhs)
}
/** An RA Expression. */
abstract class RAExpr extends TRA {
/** Gets the predicate this RA expression belongs to. */
abstract Predicate getPredicate();
/** Gets the line index of this RA expression. */
abstract int getLine();
/** Gets the LHS of the expression of the form `rNN = ...` */
abstract int getLhs();
/** Gets a variable of the form `rNN` in the RHS of the RA expression. */
abstract int getARhsVariable();
/** Gets the given arity of the RA expression. */
abstract int getArity();
/** Gets a predicate name referenced in the RHS of an RA expression. */
abstract string getARhsPredicate();
final string toString() { result = this.getPredicate().getLineOfRA(this.getLine()) }
/** Gets a child of this RA expression - not by index yet. */
RAExpr getAChild() {
result.getPredicate() = this.getPredicate() and result.getLhs() = this.getARhsVariable()
}
}
/**
* A generic RA expression - where we haven't precisely parsed the RA expression type.
* For Hackathon purposes, we probably don't need more than this.
*/
class RAUnknownExpr extends RAExpr, TUnknown {
Predicate p;
int line;
string rhs;
int arity;
int lhs;
RAUnknownExpr() { this = TUnknown(p, line, lhs, arity, rhs) }
override int getLine() { result = line }
override Predicate getPredicate() { result = p }
override int getLhs() { result = lhs }
override int getARhsVariable() {
result = rhs.splitAt(" ").regexpCapture("r([0-9]+)", 1).toInt()
}
// This is a dumb regex to find a predicate name - they always contain a `#` (TODO...)
override string getARhsPredicate() { result = rhs.splitAt(" ") and result.indexOf("#") > 0 }
override int getArity() { result = arity }
}
class RAReturnExpr extends RAExpr, TReturn {
Predicate p;
int line;
int res;
RAReturnExpr() { this = TReturn(p, line, res) }
override Predicate getPredicate() { result = p }
override int getLine() { result = line }
override int getLhs() { none() }
override int getARhsVariable() { result = res }
override int getArity() { none() }
override string getARhsPredicate() { none() }
}
class RAScanExpr extends RAExpr, TScan {
Predicate p;
int line;
int arity;
int lhs;
string rhs;
RAScanExpr() { this = TScan(p, line, arity, lhs, rhs) }
override Predicate getPredicate() { result = p }
override int getLine() { result = line }
override int getLhs() { result = lhs }
override int getArity() { result = arity }
override int getARhsVariable() { isVariable(rhs, result) }
override string getARhsPredicate() {
result = rhs and
not isVariable(result, _)
}
}
bindingset[s]
private predicate isVariable(string s, int n) { n = s.regexpCapture("r(\\d+)", 1).toInt() }
class RAJoinExpr extends RAExpr, TJoin {
Predicate p;
int line;
int arity;
int lhs;
string left;
string right;
RAJoinExpr() { this = TJoin(p, line, arity, lhs, left, right) }
override Predicate getPredicate() { result = p }
override int getLine() { result = line }
override int getLhs() { result = lhs }
override int getArity() { result = arity }
// Note: We could return reasonable values here sometimes.
override int getARhsVariable() { isVariable([left, right], result) }
// Note: We could return reasonable values here sometimes.
override string getARhsPredicate() {
result = [left, right] and
not isVariable(result, _)
}
}
class RaSelectExpr extends RAExpr, TSelect {
Predicate p;
int line;
int arity;
int lhs;
string rhs;
RaSelectExpr() { this = TSelect(p, line, arity, lhs, rhs) }
override Predicate getPredicate() { result = p }
override int getLine() { result = line }
override int getLhs() { result = lhs }
override int getArity() { result = arity }
// Note: We could return reasonable values here sometimes.
override int getARhsVariable() { isVariable(rhs, result) }
// Note: We could return reasonable values here sometimes.
override string getARhsPredicate() {
result = rhs and
not isVariable(result, _)
}
}
class RaAntiJoinExpr extends RAExpr, TAntiJoin {
Predicate p;
int line;
int arity;
int lhs;
string left;
string right;
RaAntiJoinExpr() { this = TAntiJoin(p, line, arity, lhs, left, right) }
override Predicate getPredicate() { result = p }
override int getLine() { result = line }
override int getLhs() { result = lhs }
override int getArity() { result = arity }
// Note: We could return reasonable values here sometimes.
override int getARhsVariable() { isVariable([left, right], result) }
// Note: We could return reasonable values here sometimes.
override string getARhsPredicate() {
result = [left, right] and
not isVariable(result, _)
}
}
}