forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLExpressionGenerator.java
More file actions
334 lines (300 loc) · 12.4 KB
/
MySQLExpressionGenerator.java
File metadata and controls
334 lines (300 loc) · 12.4 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package sqlancer.mysql.gen;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.common.gen.CERTGenerator;
import sqlancer.common.gen.TLPWhereGenerator;
import sqlancer.common.gen.UntypedExpressionGenerator;
import sqlancer.common.schema.AbstractTables;
import sqlancer.mysql.MySQLBugs;
import sqlancer.mysql.MySQLGlobalState;
import sqlancer.mysql.MySQLSchema.MySQLColumn;
import sqlancer.mysql.MySQLSchema.MySQLRowValue;
import sqlancer.mysql.MySQLSchema.MySQLTable;
import sqlancer.mysql.ast.MySQLBetweenOperation;
import sqlancer.mysql.ast.MySQLBinaryComparisonOperation;
import sqlancer.mysql.ast.MySQLBinaryComparisonOperation.BinaryComparisonOperator;
import sqlancer.mysql.ast.MySQLBinaryLogicalOperation;
import sqlancer.mysql.ast.MySQLBinaryLogicalOperation.MySQLBinaryLogicalOperator;
import sqlancer.mysql.ast.MySQLBinaryOperation;
import sqlancer.mysql.ast.MySQLBinaryOperation.MySQLBinaryOperator;
import sqlancer.mysql.ast.MySQLCastOperation;
import sqlancer.mysql.ast.MySQLColumnReference;
import sqlancer.mysql.ast.MySQLComputableFunction;
import sqlancer.mysql.ast.MySQLComputableFunction.MySQLFunction;
import sqlancer.mysql.ast.MySQLConstant;
import sqlancer.mysql.ast.MySQLConstant.MySQLDoubleConstant;
import sqlancer.mysql.ast.MySQLExists;
import sqlancer.mysql.ast.MySQLExpression;
import sqlancer.mysql.ast.MySQLInOperation;
import sqlancer.mysql.ast.MySQLJoin;
import sqlancer.mysql.ast.MySQLOrderByTerm;
import sqlancer.mysql.ast.MySQLOrderByTerm.MySQLOrder;
import sqlancer.mysql.ast.MySQLSelect;
import sqlancer.mysql.ast.MySQLStringExpression;
import sqlancer.mysql.ast.MySQLTableReference;
import sqlancer.mysql.ast.MySQLUnaryPostfixOperation;
import sqlancer.mysql.ast.MySQLUnaryPrefixOperation;
import sqlancer.mysql.ast.MySQLUnaryPrefixOperation.MySQLUnaryPrefixOperator;
public class MySQLExpressionGenerator extends UntypedExpressionGenerator<MySQLExpression, MySQLColumn>
implements TLPWhereGenerator<MySQLSelect, MySQLJoin, MySQLExpression, MySQLTable, MySQLColumn>,
CERTGenerator<MySQLSelect, MySQLJoin, MySQLExpression, MySQLTable, MySQLColumn> {
private final MySQLGlobalState state;
private MySQLRowValue rowVal;
private List<MySQLTable> tables;
public MySQLExpressionGenerator(MySQLGlobalState state) {
this.state = state;
}
public MySQLExpressionGenerator setRowVal(MySQLRowValue rowVal) {
this.rowVal = rowVal;
return this;
}
private enum Actions {
COLUMN, LITERAL, UNARY_PREFIX_OPERATION, UNARY_POSTFIX, COMPUTABLE_FUNCTION, BINARY_LOGICAL_OPERATOR,
BINARY_COMPARISON_OPERATION, CAST, IN_OPERATION, BINARY_OPERATION, EXISTS, BETWEEN_OPERATOR;
}
@Override
public MySQLExpression generateExpression(int depth) {
if (depth >= state.getOptions().getMaxExpressionDepth()) {
return generateLeafNode();
}
switch (Randomly.fromOptions(Actions.values())) {
case COLUMN:
return generateColumn();
case LITERAL:
return generateConstant();
case UNARY_PREFIX_OPERATION:
MySQLExpression subExpr = generateExpression(depth + 1);
MySQLUnaryPrefixOperator random = MySQLUnaryPrefixOperator.getRandom();
return new MySQLUnaryPrefixOperation(subExpr, random);
case UNARY_POSTFIX:
return new MySQLUnaryPostfixOperation(generateExpression(depth + 1),
Randomly.fromOptions(MySQLUnaryPostfixOperation.UnaryPostfixOperator.values()),
Randomly.getBoolean());
case COMPUTABLE_FUNCTION:
return getComputableFunction(depth + 1);
case BINARY_LOGICAL_OPERATOR:
return new MySQLBinaryLogicalOperation(generateExpression(depth + 1), generateExpression(depth + 1),
MySQLBinaryLogicalOperator.getRandom());
case BINARY_COMPARISON_OPERATION:
return new MySQLBinaryComparisonOperation(generateExpression(depth + 1), generateExpression(depth + 1),
BinaryComparisonOperator.getRandom());
case CAST:
return new MySQLCastOperation(generateExpression(depth + 1), MySQLCastOperation.CastType.getRandom());
case IN_OPERATION:
MySQLExpression expr = generateExpression(depth + 1);
List<MySQLExpression> rightList = new ArrayList<>();
for (int i = 0; i < 1 + Randomly.smallNumber(); i++) {
rightList.add(generateExpression(depth + 1));
}
return new MySQLInOperation(expr, rightList, Randomly.getBoolean());
case BINARY_OPERATION:
if (MySQLBugs.bug99135) {
throw new IgnoreMeException();
}
return new MySQLBinaryOperation(generateExpression(depth + 1), generateExpression(depth + 1),
MySQLBinaryOperator.getRandom());
case EXISTS:
return getExists();
case BETWEEN_OPERATOR:
if (MySQLBugs.bug99181) {
// TODO: there are a number of bugs that are triggered by the BETWEEN operator
throw new IgnoreMeException();
}
return new MySQLBetweenOperation(generateExpression(depth + 1), generateExpression(depth + 1),
generateExpression(depth + 1));
default:
throw new AssertionError();
}
}
private MySQLExpression getExists() {
if (Randomly.getBoolean()) {
return new MySQLExists(new MySQLStringExpression("SELECT 1", MySQLConstant.createTrue()));
} else {
return new MySQLExists(new MySQLStringExpression("SELECT 1 wHERE FALSE", MySQLConstant.createFalse()));
}
}
private MySQLExpression getComputableFunction(int depth) {
MySQLFunction func = MySQLFunction.getRandomFunction();
int nrArgs = func.getNrArgs();
if (func.isVariadic()) {
nrArgs += Randomly.smallNumber();
}
MySQLExpression[] args = new MySQLExpression[nrArgs];
for (int i = 0; i < args.length; i++) {
args[i] = generateExpression(depth + 1);
}
return new MySQLComputableFunction(func, args);
}
private enum ConstantType {
INT, NULL, STRING, DOUBLE;
public static ConstantType[] valuesPQS() {
return new ConstantType[] { INT, NULL, STRING };
}
}
@Override
public MySQLExpression generateConstant() {
ConstantType[] values;
if (state.usesPQS()) {
values = ConstantType.valuesPQS();
} else {
values = ConstantType.values();
}
switch (Randomly.fromOptions(values)) {
case INT:
return MySQLConstant.createIntConstant((int) state.getRandomly().getInteger());
case NULL:
return MySQLConstant.createNullConstant();
case STRING:
/* Replace characters that still trigger open bugs in MySQL */
String string = state.getRandomly().getString().replace("\\", "").replace("\n", "");
return MySQLConstant.createStringConstant(string);
case DOUBLE:
double val = state.getRandomly().getDouble();
return new MySQLDoubleConstant(val);
default:
throw new AssertionError();
}
}
@Override
protected MySQLExpression generateColumn() {
MySQLColumn c = Randomly.fromList(columns);
MySQLConstant val;
if (rowVal == null) {
val = null;
} else {
val = rowVal.getValues().get(c);
}
return MySQLColumnReference.create(c, val);
}
@Override
public MySQLExpression negatePredicate(MySQLExpression predicate) {
return new MySQLUnaryPrefixOperation(predicate, MySQLUnaryPrefixOperator.NOT);
}
@Override
public MySQLExpression isNull(MySQLExpression expr) {
return new MySQLUnaryPostfixOperation(expr, MySQLUnaryPostfixOperation.UnaryPostfixOperator.IS_NULL, false);
}
@Override
public List<MySQLExpression> generateOrderBys() {
List<MySQLExpression> expressions = super.generateOrderBys();
List<MySQLExpression> newOrderBys = new ArrayList<>();
for (MySQLExpression expr : expressions) {
if (Randomly.getBoolean()) {
MySQLOrderByTerm newExpr = new MySQLOrderByTerm(expr, MySQLOrder.getRandomOrder());
newOrderBys.add(newExpr);
} else {
newOrderBys.add(expr);
}
}
return newOrderBys;
}
@Override
public MySQLExpressionGenerator setTablesAndColumns(AbstractTables<MySQLTable, MySQLColumn> tables) {
this.columns = tables.getColumns();
this.tables = tables.getTables();
return this;
}
@Override
public MySQLExpression generateBooleanExpression() {
return generateExpression();
}
@Override
public MySQLSelect generateSelect() {
return new MySQLSelect();
}
@Override
public List<MySQLJoin> getRandomJoinClauses() {
return List.of();
}
@Override
public List<MySQLExpression> getTableRefs() {
return tables.stream().map(t -> new MySQLTableReference(t)).collect(Collectors.toList());
}
@Override
public List<MySQLExpression> generateFetchColumns(boolean shouldCreateDummy) {
return columns.stream().map(c -> new MySQLColumnReference(c, null)).collect(Collectors.toList());
}
@Override
public String generateExplainQuery(MySQLSelect select) {
return "EXPLAIN " + select.asString();
}
@Override
public boolean mutate(MySQLSelect select) {
List<Function<MySQLSelect, Boolean>> mutators = new ArrayList<>();
mutators.add(this::mutateWhere);
mutators.add(this::mutateGroupBy);
mutators.add(this::mutateHaving);
mutators.add(this::mutateAnd);
mutators.add(this::mutateOr);
mutators.add(this::mutateDistinct);
return Randomly.fromList(mutators).apply(select);
}
boolean mutateDistinct(MySQLSelect select) {
MySQLSelect.SelectType selectType = select.getFromOptions();
if (selectType != MySQLSelect.SelectType.ALL) {
select.setSelectType(MySQLSelect.SelectType.ALL);
return true;
} else {
select.setSelectType(MySQLSelect.SelectType.DISTINCT);
return false;
}
}
boolean mutateWhere(MySQLSelect select) {
boolean increase = select.getWhereClause() != null;
if (increase) {
select.setWhereClause(null);
} else {
select.setWhereClause(generateExpression());
}
return increase;
}
boolean mutateGroupBy(MySQLSelect select) {
boolean increase = select.getGroupByExpressions().size() > 0;
if (increase) {
select.clearGroupByExpressions();
} else {
select.setGroupByExpressions(select.getFetchColumns());
}
return increase;
}
boolean mutateHaving(MySQLSelect select) {
if (select.getGroupByExpressions().size() == 0) {
select.setGroupByExpressions(select.getFetchColumns());
select.setHavingClause(generateExpression());
return false;
} else {
if (select.getHavingClause() == null) {
select.setHavingClause(generateExpression());
return false;
} else {
select.setHavingClause(null);
return true;
}
}
}
boolean mutateAnd(MySQLSelect select) {
if (select.getWhereClause() == null) {
select.setWhereClause(generateExpression());
} else {
MySQLExpression newWhere = new MySQLBinaryLogicalOperation(select.getWhereClause(), generateExpression(),
MySQLBinaryLogicalOperator.AND);
select.setWhereClause(newWhere);
}
return false;
}
boolean mutateOr(MySQLSelect select) {
if (select.getWhereClause() == null) {
select.setWhereClause(generateExpression());
return false;
} else {
MySQLExpression newWhere = new MySQLBinaryLogicalOperation(select.getWhereClause(), generateExpression(),
MySQLBinaryLogicalOperator.OR);
select.setWhereClause(newWhere);
return true;
}
}
}