forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite3NoRECOracle.java
More file actions
206 lines (185 loc) · 8.38 KB
/
Copy pathSQLite3NoRECOracle.java
File metadata and controls
206 lines (185 loc) · 8.38 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
package sqlancer.sqlite3.oracle;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.Reproducer;
import sqlancer.common.oracle.NoRECBase;
import sqlancer.common.oracle.TestOracle;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLancerResultSet;
import sqlancer.sqlite3.SQLite3Errors;
import sqlancer.sqlite3.SQLite3GlobalState;
import sqlancer.sqlite3.SQLite3Visitor;
import sqlancer.sqlite3.ast.SQLite3Aggregate;
import sqlancer.sqlite3.ast.SQLite3Expression;
import sqlancer.sqlite3.ast.SQLite3Expression.Join;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3ColumnName;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3PostfixText;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3PostfixUnaryOperation;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3PostfixUnaryOperation.PostfixUnaryOperator;
import sqlancer.sqlite3.ast.SQLite3Select;
import sqlancer.sqlite3.gen.SQLite3Common;
import sqlancer.sqlite3.gen.SQLite3ExpressionGenerator;
import sqlancer.sqlite3.schema.SQLite3Schema;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Table;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Tables;
public class SQLite3NoRECOracle extends NoRECBase<SQLite3GlobalState> implements TestOracle<SQLite3GlobalState> {
private static final int NO_VALID_RESULT = -1;
private final SQLite3Schema s;
private SQLite3ExpressionGenerator gen;
private Reproducer<SQLite3GlobalState> reproducer;
private class SQLite3NoRECReproducer implements Reproducer<SQLite3GlobalState> {
private final Function<SQLite3GlobalState, Integer> optimizedQuery;
private final Function<SQLite3GlobalState, Integer> unoptimizedQuery;
SQLite3NoRECReproducer(Function<SQLite3GlobalState, Integer> optimizedQuery,
Function<SQLite3GlobalState, Integer> unoptimizedQuery) {
this.optimizedQuery = optimizedQuery;
this.unoptimizedQuery = unoptimizedQuery;
}
@Override
public boolean bugStillTriggers(SQLite3GlobalState globalState) {
return optimizedQuery.apply(globalState) != unoptimizedQuery.apply(globalState);
}
}
public SQLite3NoRECOracle(SQLite3GlobalState globalState) {
super(globalState);
this.s = globalState.getSchema();
SQLite3Errors.addExpectedExpressionErrors(errors);
SQLite3Errors.addMatchQueryErrors(errors);
SQLite3Errors.addQueryErrors(errors);
errors.add("misuse of aggregate");
errors.add("misuse of window function");
errors.add("second argument to nth_value must be a positive integer");
errors.add("no such table");
errors.add("no query solution");
errors.add("unable to use function MATCH in the requested context");
}
@Override
public void check() throws SQLException {
reproducer = null;
SQLite3Tables randomTables = s.getRandomTableNonEmptyTables();
List<SQLite3Column> columns = randomTables.getColumns();
gen = new SQLite3ExpressionGenerator(state).setColumns(columns);
SQLite3Expression randomWhereCondition = gen.generateExpression();
List<SQLite3Table> tables = randomTables.getTables();
List<Join> joinStatements = gen.getRandomJoinClauses(tables);
List<SQLite3Expression> tableRefs = SQLite3Common.getTableRefs(tables, s);
SQLite3Select select = new SQLite3Select();
select.setFromTables(tableRefs);
select.setJoinClauses(joinStatements);
Function<SQLite3GlobalState, Integer> optimizedQuery = getOptimizedQuery(select, randomWhereCondition);
Function<SQLite3GlobalState, Integer> unoptimizedQuery = getUnoptimizedQuery(select, randomWhereCondition);
int optimizedCount = optimizedQuery.apply(state);
int unoptimizedCount = unoptimizedQuery.apply(state);
if (optimizedCount == NO_VALID_RESULT || unoptimizedCount == NO_VALID_RESULT) {
throw new IgnoreMeException();
}
if (optimizedCount != unoptimizedCount) {
reproducer = new SQLite3NoRECReproducer(optimizedQuery, unoptimizedQuery);
state.getState().getLocalState().log(optimizedQueryString + ";\n" + unoptimizedQueryString + ";");
throw new AssertionError(optimizedCount + " " + unoptimizedCount);
}
}
@Override
public Reproducer<SQLite3GlobalState> getLastReproducer() {
return reproducer;
}
@Override
public String getLastQueryString() {
return optimizedQueryString;
}
private Function<SQLite3GlobalState, Integer> getUnoptimizedQuery(SQLite3Select select,
SQLite3Expression randomWhereCondition) throws SQLException {
SQLite3PostfixUnaryOperation isTrue = new SQLite3PostfixUnaryOperation(PostfixUnaryOperator.IS_TRUE,
randomWhereCondition);
SQLite3PostfixText asText = new SQLite3PostfixText(isTrue, " as count", null);
select.setFetchColumns(Arrays.asList(asText));
select.setWhereClause(null);
unoptimizedQueryString = "SELECT SUM(count) FROM (" + SQLite3Visitor.asString(select) + ")";
if (options.logEachSelect()) {
logger.writeCurrent(unoptimizedQueryString);
}
SQLQueryAdapter q = new SQLQueryAdapter(unoptimizedQueryString, errors);
return new Function<SQLite3GlobalState, Integer>() {
@Override
public Integer apply(SQLite3GlobalState state) {
return extractCounts(q, state);
}
};
}
private Function<SQLite3GlobalState, Integer> getOptimizedQuery(SQLite3Select select,
SQLite3Expression randomWhereCondition) throws SQLException {
boolean useAggregate = Randomly.getBoolean();
if (Randomly.getBoolean()) {
select.setOrderByExpressions(gen.generateOrderBys());
}
if (useAggregate) {
select.setFetchColumns(Arrays.asList(new SQLite3Aggregate(Collections.emptyList(),
SQLite3Aggregate.SQLite3AggregateFunction.COUNT_ALL)));
} else {
SQLite3ColumnName aggr = new SQLite3ColumnName(SQLite3Column.createDummy("*"), null);
select.setFetchColumns(Arrays.asList(aggr));
}
select.setWhereClause(randomWhereCondition);
optimizedQueryString = SQLite3Visitor.asString(select);
if (options.logEachSelect()) {
logger.writeCurrent(optimizedQueryString);
}
SQLQueryAdapter q = new SQLQueryAdapter(optimizedQueryString, errors);
return new Function<SQLite3GlobalState, Integer>() {
@Override
public Integer apply(SQLite3GlobalState state) {
return useAggregate ? extractCounts(q, state) : countRows(q, state);
}
};
}
private int countRows(SQLQueryAdapter q, SQLite3GlobalState globalState) {
int count = 0;
try (SQLancerResultSet rs = q.executeAndGet(globalState)) {
if (rs == null) {
return NO_VALID_RESULT;
} else {
try {
while (rs.next()) {
count++;
}
} catch (SQLException e) {
count = NO_VALID_RESULT;
}
}
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw (IgnoreMeException) e;
}
throw new AssertionError(unoptimizedQueryString, e);
}
return count;
}
private int extractCounts(SQLQueryAdapter q, SQLite3GlobalState globalState) {
int count = 0;
try (SQLancerResultSet rs = q.executeAndGet(globalState)) {
if (rs == null) {
return NO_VALID_RESULT;
} else {
try {
while (rs.next()) {
count += rs.getInt(1);
}
} catch (SQLException e) {
count = NO_VALID_RESULT;
}
}
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw (IgnoreMeException) e;
}
throw new AssertionError(unoptimizedQueryString, e);
}
return count;
}
}