/
Main.java
516 lines (453 loc) · 18.9 KB
/
Main.java
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package sqlancer;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.JCommander.Builder;
import sqlancer.clickhouse.ClickHouseProvider;
import sqlancer.cockroachdb.CockroachDBProvider;
import sqlancer.duckdb.DuckDBProvider;
import sqlancer.mariadb.MariaDBProvider;
import sqlancer.mysql.MySQLProvider;
import sqlancer.postgres.PostgresProvider;
import sqlancer.sqlite3.SQLite3Provider;
import sqlancer.tidb.TiDBProvider;
public final class Main {
public static final File LOG_DIRECTORY = new File("logs");
public static volatile AtomicLong nrQueries = new AtomicLong();
public static volatile AtomicLong nrDatabases = new AtomicLong();
public static volatile AtomicLong nrSuccessfulActions = new AtomicLong();
public static volatile AtomicLong nrUnsuccessfulActions = new AtomicLong();
static int threadsShutdown;
static {
if (!LOG_DIRECTORY.exists()) {
LOG_DIRECTORY.mkdir();
}
}
private Main() {
}
public static final class StateLogger {
private final File loggerFile;
private File curFile;
private FileWriter logFileWriter;
public FileWriter currentFileWriter;
private static final List<String> INITIALIZED_PROVIDER_NAMES = new ArrayList<>();
private final boolean logEachSelect;
private final DatabaseProvider<?, ?> provider;
private static final class AlsoWriteToConsoleFileWriter extends FileWriter {
AlsoWriteToConsoleFileWriter(File file) throws IOException {
super(file);
}
@Override
public Writer append(CharSequence arg0) throws IOException {
System.err.println(arg0);
return super.append(arg0);
}
@Override
public void write(String str) throws IOException {
System.err.println(str);
super.write(str);
}
}
public StateLogger(String databaseName, DatabaseProvider<?, ?> provider, MainOptions options) {
this.provider = provider;
File dir = new File(LOG_DIRECTORY, provider.getDBMSName());
if (dir.exists() && !dir.isDirectory()) {
throw new AssertionError(dir);
}
ensureExistsAndIsEmpty(dir, provider);
loggerFile = new File(dir, databaseName + ".log");
logEachSelect = options.logEachSelect();
if (logEachSelect) {
curFile = new File(dir, databaseName + "-cur.log");
}
}
private synchronized void ensureExistsAndIsEmpty(File dir, DatabaseProvider<?, ?> provider) {
if (INITIALIZED_PROVIDER_NAMES.contains(provider.getDBMSName())) {
return;
}
if (!dir.exists()) {
try {
Files.createDirectories(dir.toPath());
} catch (IOException e) {
throw new AssertionError(e);
}
}
File[] listFiles = dir.listFiles();
assert listFiles != null : "directory was just created, so it should exist";
for (File file : listFiles) {
if (!file.isDirectory()) {
file.delete();
}
}
INITIALIZED_PROVIDER_NAMES.add(provider.getDBMSName());
}
private FileWriter getLogFileWriter() {
if (logFileWriter == null) {
try {
logFileWriter = new AlsoWriteToConsoleFileWriter(loggerFile);
} catch (IOException e) {
throw new AssertionError(e);
}
}
return logFileWriter;
}
public FileWriter getCurrentFileWriter() {
if (!logEachSelect) {
throw new UnsupportedOperationException();
}
if (currentFileWriter == null) {
try {
currentFileWriter = new FileWriter(curFile, false);
} catch (IOException e) {
throw new AssertionError(e);
}
}
return currentFileWriter;
}
public void writeCurrent(StateToReproduce state) {
if (!logEachSelect) {
throw new UnsupportedOperationException();
}
printState(getCurrentFileWriter(), state);
try {
currentFileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeCurrent(String queryString) {
if (!logEachSelect) {
throw new UnsupportedOperationException();
}
try {
getCurrentFileWriter().write(queryString + ";\n");
currentFileWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void logRowNotFound(StateToReproduce state) {
printState(getLogFileWriter(), state);
try {
getLogFileWriter().flush();
} catch (IOException e) {
throw new AssertionError(e);
}
}
public void logException(Throwable reduce, StateToReproduce state) {
String stackTrace = getStackTrace(reduce);
FileWriter logFileWriter2 = getLogFileWriter();
try {
logFileWriter2.write(stackTrace);
printState(logFileWriter2, state);
} catch (IOException e) {
throw new AssertionError(e);
} finally {
try {
logFileWriter2.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private String getStackTrace(Throwable e1) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e1.printStackTrace(pw);
return "--" + sw.toString().replace("\n", "\n--");
}
private void printState(FileWriter writer, StateToReproduce state) {
StringBuilder sb = new StringBuilder();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
sb.append("-- Time: " + dateFormat.format(date) + "\n");
sb.append("-- Database: " + state.getDatabaseName() + "\n");
sb.append("-- Database version: " + state.getDatabaseVersion() + "\n");
sb.append("-- seed value: " + state.getSeedValue() + "\n");
for (Query s : state.getStatements()) {
if (s.getQueryString().endsWith(";")) {
sb.append(s.getQueryString());
} else {
sb.append(s.getQueryString() + ";");
}
sb.append('\n');
}
if (state.getQueryString() != null) {
sb.append(state.getQueryString() + ";\n");
}
try {
writer.write(sb.toString());
} catch (IOException e) {
throw new AssertionError(e);
}
provider.printDatabaseSpecificState(writer, state);
}
}
public static class QueryManager {
private final Connection con;
private final StateToReproduce stateToRepro;
QueryManager(Connection con, StateToReproduce state) {
if (con == null || state == null) {
throw new IllegalArgumentException();
}
this.con = con;
this.stateToRepro = state;
}
public boolean execute(Query q) throws SQLException {
stateToRepro.statements.add(q);
boolean success = q.execute(con);
Main.nrSuccessfulActions.addAndGet(1);
return success;
}
public void incrementSelectQueryCount() {
Main.nrQueries.addAndGet(1);
}
public void incrementCreateDatabase() {
Main.nrDatabases.addAndGet(1);
}
}
public static void printArray(Object... arr) {
for (Object o : arr) {
System.out.println(o);
}
}
public static void main(String[] args) {
System.exit(executeMain(args));
}
public static class DBMSExecutor<G extends GlobalState<O>, O> {
private final DatabaseProvider<G, O> provider;
private final MainOptions options;
private final O command;
private final String databaseName;
private final long seed;
private StateLogger logger;
private StateToReproduce stateToRepro;
public DBMSExecutor(DatabaseProvider<G, O> provider, MainOptions options, O dbmsSpecificOptions,
String databaseName, long seed) {
this.provider = provider;
this.options = options;
this.databaseName = databaseName;
this.seed = seed;
this.command = dbmsSpecificOptions;
}
private G createGlobalState() {
try {
return provider.getGlobalStateClass().getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new AssertionError(e);
}
}
public O getCommand() {
return command;
}
public void run() throws SQLException {
G state = createGlobalState();
stateToRepro = provider.getStateToReproduce(databaseName);
stateToRepro.seedValue = seed;
state.setState(stateToRepro);
logger = new StateLogger(databaseName, provider, options);
Randomly r = new Randomly(seed);
state.setRandomly(r);
state.setDatabaseName(databaseName);
state.setMainOptions(options);
state.setDmbsSpecificOptions(command);
try (Connection con = provider.createDatabase(state)) {
QueryManager manager = new QueryManager(con, stateToRepro);
try {
java.sql.DatabaseMetaData meta = con.getMetaData();
stateToRepro.databaseVersion = meta.getDatabaseProductVersion();
} catch (SQLFeatureNotSupportedException e) {
// ignore
}
state.setConnection(con);
state.setStateLogger(logger);
state.setManager(manager);
provider.generateAndTestDatabase(state);
}
}
public StateLogger getLogger() {
return logger;
}
public StateToReproduce getStateToReproduce() {
return stateToRepro;
}
}
public static class DBMSExecutorFactory<G extends GlobalState<O>, O> {
private final DatabaseProvider<G, O> provider;
private final MainOptions options;
private final O command;
public DBMSExecutorFactory(DatabaseProvider<G, O> provider, MainOptions options) {
this.provider = provider;
this.options = options;
this.command = createCommand();
}
private O createCommand() {
try {
return provider.getOptionClass().getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new AssertionError(e);
}
}
public O getCommand() {
return command;
}
@SuppressWarnings("unchecked")
public DBMSExecutor<G, O> getDBMSExecutor(String databaseName, long seed) {
try {
return new DBMSExecutor<G, O>(provider.getClass().getDeclaredConstructor().newInstance(), options,
command, databaseName, seed);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
public static int executeMain(String... args) throws AssertionError {
List<DatabaseProvider<?, ?>> providers = getDBMSProviders();
Map<String, DBMSExecutorFactory<?, ?>> nameToProvider = new HashMap<>();
MainOptions options = new MainOptions();
Builder commandBuilder = JCommander.newBuilder().addObject(options);
for (DatabaseProvider<?, ?> provider : providers) {
String name = provider.getDBMSName();
if (!name.toLowerCase().equals(name)) {
throw new AssertionError(name + " should be in lowercase!");
}
DBMSExecutorFactory<?, ?> executorFactory = new DBMSExecutorFactory<>(provider, options);
commandBuilder = commandBuilder.addCommand(name, executorFactory.getCommand());
nameToProvider.put(name, executorFactory);
}
JCommander jc = commandBuilder.programName("SQLancer").build();
jc.parse(args);
if (jc.getParsedCommand() == null) {
jc.usage();
return options.getErrorExitCode();
}
if (options.printProgressInformation()) {
startProgressMonitor();
}
ExecutorService execService = Executors.newFixedThreadPool(options.getNumberConcurrentThreads());
DBMSExecutorFactory<?, ?> executorFactory = nameToProvider.get(jc.getParsedCommand());
for (int i = 0; i < options.getTotalNumberTries(); i++) {
final String databaseName = "database" + i;
final long seed;
if (options.getRandomSeed() == -1) {
seed = System.currentTimeMillis() + i;
} else {
seed = options.getRandomSeed() + i;
}
execService.execute(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName(databaseName);
runThread(databaseName);
}
private void runThread(final String databaseName) {
while (true) {
DBMSExecutor<?, ?> executor = executorFactory.getDBMSExecutor(databaseName, seed);
try {
executor.run();
} catch (IgnoreMeException e) {
continue;
} catch (Throwable reduce) {
reduce.printStackTrace();
executor.getStateToReproduce().exception = reduce.getMessage();
executor.getLogger().logFileWriter = null;
executor.getLogger().logException(reduce, executor.getStateToReproduce());
threadsShutdown++;
break;
} finally {
try {
if (options.logEachSelect()) {
if (executor.getLogger().currentFileWriter != null) {
executor.getLogger().currentFileWriter.close();
}
executor.getLogger().currentFileWriter = null;
}
} catch (IOException e) {
e.printStackTrace();
}
if (threadsShutdown == options.getTotalNumberTries()) {
execService.shutdown();
}
}
}
}
});
}
try {
if (options.getTimeoutSeconds() == -1) {
execService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} else {
execService.awaitTermination(options.getTimeoutSeconds(), TimeUnit.SECONDS);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return threadsShutdown == 0 ? 0 : options.getErrorExitCode();
}
static List<DatabaseProvider<?, ?>> getDBMSProviders() {
List<DatabaseProvider<?, ?>> providers = new ArrayList<>();
providers.add(new SQLite3Provider());
providers.add(new CockroachDBProvider());
providers.add(new MySQLProvider());
providers.add(new MariaDBProvider());
providers.add(new TiDBProvider());
providers.add(new PostgresProvider());
providers.add(new ClickHouseProvider());
providers.add(new DuckDBProvider());
return providers;
}
private static void startProgressMonitor() {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(new Runnable() {
private long timeMillis = System.currentTimeMillis();
private long lastNrQueries = 0;
private long lastNrDbs;
{
timeMillis = System.currentTimeMillis();
}
@Override
public void run() {
long elapsedTimeMillis = System.currentTimeMillis() - timeMillis;
long currentNrQueries = nrQueries.get();
long nrCurrentQueries = currentNrQueries - lastNrQueries;
double throughput = nrCurrentQueries / (elapsedTimeMillis / 1000d);
long currentNrDbs = nrDatabases.get();
long nrCurrentDbs = currentNrDbs - lastNrDbs;
double throughputDbs = nrCurrentDbs / (elapsedTimeMillis / 1000d);
long successfulStatementsRatio = (long) (100.0 * nrSuccessfulActions.get()
/ (nrSuccessfulActions.get() + nrUnsuccessfulActions.get()));
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(String.format(
"[%s] Executed %d queries (%d queries/s; %.2f/s dbs, successful statements: %2d%%). Threads shut down: %d.",
dateFormat.format(date), currentNrQueries, (int) throughput, throughputDbs,
successfulStatementsRatio, threadsShutdown));
timeMillis = System.currentTimeMillis();
lastNrQueries = currentNrQueries;
lastNrDbs = currentNrDbs;
}
}, 5, 5, TimeUnit.SECONDS);
}
}