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
177 lines (164 loc) · 7.14 KB
/
SQLite3NoRECOracle.java
File metadata and controls
177 lines (164 loc) · 7.14 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
package sqlancer.sqlite3.oracle;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import sqlancer.IgnoreMeException;
import sqlancer.Main.StateLogger;
import sqlancer.MainOptions;
import sqlancer.QueryAdapter;
import sqlancer.Randomly;
import sqlancer.StateToReproduce.SQLite3StateToReproduce;
import sqlancer.TestOracle;
import sqlancer.sqlite3.SQLite3Errors;
import sqlancer.sqlite3.SQLite3Provider.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 implements TestOracle {
private static final int NO_VALID_RESULT = -1;
private final SQLite3Schema s;
private final Connection con;
private final SQLite3StateToReproduce state;
private final Set<String> errors = new HashSet<>();
private final StateLogger logger;
private final MainOptions options;
private final SQLite3GlobalState globalState;
private SQLite3ExpressionGenerator gen;
private String firstQueryString;
private String secondQueryString;
public SQLite3NoRECOracle(SQLite3GlobalState globalState) {
this.s = globalState.getSchema();
this.con = globalState.getConnection();
this.state = (SQLite3StateToReproduce) globalState.getState();
this.logger = globalState.getLogger();
this.options = globalState.getOptions();
this.globalState = globalState;
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 {
SQLite3Tables randomTables = s.getRandomTableNonEmptyTables();
List<SQLite3Column> columns = randomTables.getColumns();
gen = new SQLite3ExpressionGenerator(globalState).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);
int optimizedCount = getOptimizedQuery(select, randomWhereCondition);
int unoptimizedCount = getUnoptimizedQuery(select, randomWhereCondition);
if (optimizedCount == NO_VALID_RESULT || unoptimizedCount == NO_VALID_RESULT) {
throw new IgnoreMeException();
}
if (optimizedCount != unoptimizedCount) {
state.queryString = firstQueryString + ";\n" + secondQueryString + ";";
throw new AssertionError(optimizedCount + " " + unoptimizedCount);
}
}
private int 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);
secondQueryString = "SELECT SUM(count) FROM (" + SQLite3Visitor.asString(select) + ")";
if (options.logEachSelect()) {
logger.writeCurrent(secondQueryString);
}
QueryAdapter q = new QueryAdapter(secondQueryString, errors);
return extractCounts(q);
}
private int 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);
firstQueryString = SQLite3Visitor.asString(select);
if (options.logEachSelect()) {
logger.writeCurrent(firstQueryString);
}
QueryAdapter q = new QueryAdapter(firstQueryString, errors);
return useAggregate ? extractCounts(q) : countRows(q);
}
private int countRows(QueryAdapter q) {
int count = 0;
try (ResultSet rs = q.executeAndGet(con)) {
if (rs == null) {
return NO_VALID_RESULT;
} else {
try {
while (rs.next()) {
count++;
}
} catch (SQLException e) {
count = NO_VALID_RESULT;
}
rs.getStatement().close();
}
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw (IgnoreMeException) e;
}
throw new AssertionError(secondQueryString, e);
}
return count;
}
private int extractCounts(QueryAdapter q) {
int count = 0;
try (ResultSet rs = q.executeAndGet(con)) {
if (rs == null) {
return NO_VALID_RESULT;
} else {
try {
while (rs.next()) {
count += rs.getInt(1);
}
} catch (SQLException e) {
count = NO_VALID_RESULT;
}
rs.getStatement().close();
}
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw (IgnoreMeException) e;
}
throw new AssertionError(secondQueryString, e);
}
return count;
}
}