-
Notifications
You must be signed in to change notification settings - Fork 397
Feat: Integrating DQE Testing Approaches into SQLancer first in MySQL #1251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c8166b
Introduce DQE for MySQL in SQLancer and add test to workflow
luliqwert 1f64426
Fix: remove MySQL specific logic and reuse existing method
luliqwert 83cf899
refactor(MySQLDQEOracle): extract duplicated code as method
luliqwert e1b5861
refactor(MySQLDQEOracle): run mvn formatter:format
luliqwert 131febb
refactor: fix some code style
luliqwert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package sqlancer.common.oracle; | ||
|
|
||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import sqlancer.Main; | ||
| import sqlancer.MainOptions; | ||
| import sqlancer.SQLConnection; | ||
| import sqlancer.SQLGlobalState; | ||
| import sqlancer.common.query.ExpectedErrors; | ||
| import sqlancer.common.query.SQLQueryAdapter; | ||
| import sqlancer.common.query.SQLQueryError; | ||
| import sqlancer.common.schema.AbstractRelationalTable; | ||
| import sqlancer.mysql.MySQLSchema.MySQLTables; | ||
|
|
||
| /* | ||
| * In DBMSs, SELECT, UPDATE and DELETE queries utilize predicates (i.e., WHERE clauses) to specify which rows to retrieve, update or delete, respectively. | ||
| * If they use the same predicate φ, they should access the same rows in a database. | ||
| * Ideally, DBMSs can adopt the same implementations for predicate evaluation in SELECT, UPDATE and DELETE queries. | ||
| * However, a DBMS usually adopts different implementations for predicate evaluation in SELECT, UPDATE and DELETE queries due to various optimization choices. | ||
| * Inconsistent implementations for predicate evaluation among these queries can cause SELECT, UPDATE and DELETE queries with the same predicate φ to access different rows. | ||
| * | ||
| * | ||
| * Inspired by this key observation, we propose Differential Query Execution(DQE), a novel and general approach to detect logic bugs in SELECT, UPDATE and DELETE queries. | ||
| * DQE solves the test oracle problem by executing SELECT, UPDATE and DELETE queries with the same predicate φ, and observing inconsistencies among their execution results. | ||
| * For example, if a row that is updated by an UPDATE query with a predicate φ does not appear in the query result of a SELECT query with the same predicate φ, a logic bug is detected in the target DBMS. | ||
| * The key challenge of DQE is to automatically obtain the accessed rows for a given SELECT, UPDATE or DELETE query. | ||
| * To address this challenge, we append two extra columns to each table in a database, to uniquely identify each row and track whether a row has been modified, respectively. | ||
| * We further rewrite SELECT and UPDATE queries to identify their accessed rows. | ||
| * | ||
| * more information see [DQE paper](https://ieeexplore.ieee.org/document/10172736) | ||
| */ | ||
|
|
||
| public abstract class DQEBase<S extends SQLGlobalState<?, ?>> { | ||
|
|
||
| public static final String COLUMN_ROWID = "rowId"; | ||
| public static final String COLUMN_UPDATED = "updated"; | ||
|
|
||
| protected final S state; | ||
| protected final ExpectedErrors selectExpectedErrors = new ExpectedErrors(); | ||
| protected final ExpectedErrors updateExpectedErrors = new ExpectedErrors(); | ||
| protected final ExpectedErrors deleteExpectedErrors = new ExpectedErrors(); | ||
|
|
||
| protected final Main.StateLogger logger; | ||
| protected final MainOptions options; | ||
| protected final SQLConnection con; | ||
|
|
||
| public DQEBase(S state) { | ||
| this.state = state; | ||
| this.con = state.getConnection(); | ||
| this.logger = state.getLogger(); | ||
| this.options = state.getOptions(); | ||
| } | ||
|
|
||
| public abstract String generateSelectStatement(MySQLTables tables, String tableName, String whereClauseStr); | ||
|
|
||
| public abstract String generateUpdateStatement(MySQLTables tables, String tableName, String whereClauseStr); | ||
|
|
||
| public abstract String generateDeleteStatement(MySQLTables tables, String tableName, String whereClauseStr); | ||
|
|
||
| /** | ||
| * Add auxiliary columns to the database A abstract method, subclasses need to implement it. | ||
| * | ||
| * @param table | ||
| * | ||
| * @throws SQLException | ||
| */ | ||
| public abstract void addAuxiliaryColumns(AbstractRelationalTable<?, ?, ?> table) throws SQLException; | ||
|
|
||
| public void dropAuxiliaryColumns(AbstractRelationalTable<?, ?, ?> table) throws SQLException { | ||
| String tableName = table.getName(); | ||
| String dropColumnRowId = String.format("ALTER TABLE %s DROP COLUMN %s", tableName, COLUMN_ROWID); | ||
| new SQLQueryAdapter(dropColumnRowId).execute(state); | ||
| String dropColumnUpdated = String.format("ALTER TABLE %s DROP COLUMN %s", tableName, COLUMN_UPDATED); | ||
| new SQLQueryAdapter(dropColumnUpdated).execute(state); | ||
| } | ||
|
|
||
| // This interface is to record Error code | ||
| public interface UpdateErrorCodes { | ||
|
|
||
| } | ||
|
|
||
| public interface ErrorCodeStrategy { | ||
| Set<Integer> getUpdateSpecificErrorCodes(); | ||
|
|
||
| Set<Integer> getDeleteSpecificErrorCodes(); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * The core idea of DQE is that the SELECT, UPDATE and DELETE queries with the same predicate φ should access the | ||
| * same rows. If these queries access different rows, DQE reveals a potential logic bug in the target DBMS. | ||
| */ | ||
| public static class SQLQueryResult { | ||
|
|
||
| private final Map<AbstractRelationalTable<?, ?, ?>, Set<String>> accessedRows; // Table name with respect rows | ||
| private final List<SQLQueryError> queryErrors; | ||
|
|
||
| public SQLQueryResult(Map<AbstractRelationalTable<?, ?, ?>, Set<String>> accessedRows, | ||
| List<SQLQueryError> queryErrors) { | ||
| this.accessedRows = accessedRows; | ||
| this.queryErrors = queryErrors; | ||
| } | ||
|
|
||
| public Map<AbstractRelationalTable<?, ?, ?>, Set<String>> getAccessedRows() { | ||
| return accessedRows; | ||
| } | ||
|
|
||
| public List<SQLQueryError> getQueryErrors() { | ||
| return queryErrors; | ||
| } | ||
|
|
||
| public boolean hasEmptyErrors() { | ||
| return queryErrors.isEmpty(); | ||
| } | ||
|
|
||
| public boolean hasSameErrors(SQLQueryResult that) { | ||
luliqwerty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (queryErrors.size() != that.getQueryErrors().size()) { | ||
| return false; | ||
| } else { | ||
| for (int i = 0; i < queryErrors.size(); i++) { | ||
| if (!queryErrors.get(i).equals(that.getQueryErrors().get(i))) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public boolean hasAccessedRows() { | ||
| if (accessedRows.isEmpty()) { | ||
| return false; | ||
| } | ||
| for (Set<String> accessedRow : accessedRows.values()) { | ||
| if (!accessedRow.isEmpty()) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public boolean hasSameAccessedRows(SQLQueryResult that) { | ||
| return accessedRows.equals(that.getAccessedRows()); | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.