|
| 1 | +package sqlancer; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.time.Instant; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import sqlancer.common.query.Query; |
| 10 | + |
| 11 | +/** |
| 12 | + * Reduces the transformed query of a {@link TransformationReproducer} by disabling transformation sites, searching |
| 13 | + * (with the same delta-debugging strategy as {@link StatementReducer}) for a minimal set of sites that still triggers |
| 14 | + * the bug. Because each site is an individually equivalence-preserving rewrite, any subset of sites yields a |
| 15 | + * transformed query that is still semantically equivalent to the original query, so the reduction is sound. This |
| 16 | + * reducer runs after statement reduction, evaluating each candidate against the already-reduced database; for |
| 17 | + * reproducers that do not implement {@link TransformationReproducer}, it does nothing. |
| 18 | + * |
| 19 | + * @param <G> |
| 20 | + * the DBMS-specific global state class |
| 21 | + * @param <O> |
| 22 | + * the DBMS-specific options class |
| 23 | + * @param <C> |
| 24 | + * the DBMS-specific connection class |
| 25 | + */ |
| 26 | +public class TransformationReducer<G extends GlobalState<O, ?, C>, O extends DBMSSpecificOptions<?>, C extends SQLancerDBConnection> |
| 27 | + implements Reducer<G> { |
| 28 | + |
| 29 | + private final DatabaseProvider<G, O, C> provider; |
| 30 | + private List<Query<C>> statements; |
| 31 | + private boolean observedChange; |
| 32 | + private int partitionNum; |
| 33 | + |
| 34 | + private long currentReduceSteps; |
| 35 | + private long currentReduceTime; |
| 36 | + |
| 37 | + private long maxReduceSteps; |
| 38 | + private long maxReduceTime; |
| 39 | + |
| 40 | + private Instant timeOfReductionBegins; |
| 41 | + |
| 42 | + public TransformationReducer(DatabaseProvider<G, O, C> provider) { |
| 43 | + this.provider = provider; |
| 44 | + } |
| 45 | + |
| 46 | + private boolean hasNotReachedLimit(long curr, long limit) { |
| 47 | + if (limit == MainOptions.NO_REDUCE_LIMIT) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + return curr < limit; |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + @Override |
| 55 | + public void reduce(G state, Reproducer<G> reproducer, G newGlobalState) throws Exception { |
| 56 | + if (!(reproducer instanceof TransformationReproducer)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + TransformationReproducer<G> transformationReproducer = (TransformationReproducer<G>) reproducer; |
| 60 | + |
| 61 | + maxReduceTime = state.getOptions().getMaxStatementReduceTime(); |
| 62 | + maxReduceSteps = state.getOptions().getMaxStatementReduceSteps(); |
| 63 | + |
| 64 | + // Snapshot the (already reduced) generation statements once: createDatabase logs its setup statements |
| 65 | + // (DROP/CREATE/USE) into the state, so the state's statement list must be reset for every candidate rather |
| 66 | + // than read back, lest the setup statements accumulate and get re-executed mid-test-case. |
| 67 | + statements = new ArrayList<>(); |
| 68 | + for (Query<?> stat : newGlobalState.getState().getStatements()) { |
| 69 | + statements.add((Query<C>) stat); |
| 70 | + } |
| 71 | + |
| 72 | + List<Integer> enabledSites = new ArrayList<>(); |
| 73 | + for (int site = 0; site < transformationReproducer.getTransformationSiteCount(); site++) { |
| 74 | + enabledSites.add(site); |
| 75 | + } |
| 76 | + // With every site disabled the transformed query renders as the original one, which cannot mismatch with |
| 77 | + // itself, so a single remaining site cannot be reduced further. |
| 78 | + if (enabledSites.size() < 2) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + timeOfReductionBegins = Instant.now(); |
| 83 | + currentReduceSteps = 0; |
| 84 | + currentReduceTime = 0; |
| 85 | + partitionNum = 2; |
| 86 | + |
| 87 | + while (enabledSites.size() >= 2 && hasNotReachedLimit(currentReduceSteps, maxReduceSteps) |
| 88 | + && hasNotReachedLimit(currentReduceTime, maxReduceTime)) { |
| 89 | + observedChange = false; |
| 90 | + |
| 91 | + enabledSites = tryReduction(transformationReproducer, newGlobalState, enabledSites); |
| 92 | + |
| 93 | + if (!observedChange) { |
| 94 | + if (partitionNum == enabledSites.size()) { |
| 95 | + break; |
| 96 | + } |
| 97 | + // increase the search granularity |
| 98 | + partitionNum = Math.min(partitionNum * 2, enabledSites.size()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Leave the reproducer holding the reduced transformed query (the last candidate tried may have failed), so |
| 103 | + // the final bug information reflects the reduction. |
| 104 | + transformationReproducer.setEnabledTransformationSites(new HashSet<>(enabledSites)); |
| 105 | + newGlobalState.getState().setStatements(new ArrayList<>(statements)); |
| 106 | + newGlobalState.getLogger().updateReducedBugInformation(transformationReproducer.getBugInformation()); |
| 107 | + newGlobalState.getLogger().logReduced(newGlobalState.getState(), |
| 108 | + "Transformation reduction finished; the transformed query was reduced to the one shown below"); |
| 109 | + } |
| 110 | + |
| 111 | + private List<Integer> tryReduction(TransformationReproducer<G> transformationReproducer, G newGlobalState, |
| 112 | + List<Integer> enabledSites) throws Exception { |
| 113 | + |
| 114 | + List<Integer> sites = enabledSites; |
| 115 | + |
| 116 | + int start = 0; |
| 117 | + int subLength = sites.size() / partitionNum; |
| 118 | + while (start < sites.size()) { |
| 119 | + // candidateSites = sites[:start] + sites[start+subLength:] |
| 120 | + // in other words, remove [start, start+subLength) from sites |
| 121 | + List<Integer> candidateSites = new ArrayList<>(sites); |
| 122 | + int endPoint = Math.min(start + subLength, candidateSites.size()); |
| 123 | + candidateSites.subList(start, endPoint).clear(); |
| 124 | + |
| 125 | + if (bugStillTriggersWith(transformationReproducer, newGlobalState, candidateSites)) { |
| 126 | + observedChange = true; |
| 127 | + sites = candidateSites; |
| 128 | + partitionNum = Math.max(partitionNum - 1, 2); |
| 129 | + newGlobalState.getLogger().updateReducedBugInformation(transformationReproducer.getBugInformation()); |
| 130 | + newGlobalState.getLogger().logReduced(newGlobalState.getState()); |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + currentReduceSteps++; |
| 135 | + currentReduceTime = Duration.between(timeOfReductionBegins, Instant.now()).getSeconds(); |
| 136 | + if (!hasNotReachedLimit(currentReduceSteps, maxReduceSteps) |
| 137 | + || !hasNotReachedLimit(currentReduceTime, maxReduceTime)) { |
| 138 | + return sites; |
| 139 | + } |
| 140 | + start = start + subLength; |
| 141 | + } |
| 142 | + return sites; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Whether the bug still triggers with only {@code candidateSites} applied to the transformed query, evaluated |
| 147 | + * against a freshly recreated database populated with the (already reduced) generation statements. |
| 148 | + * |
| 149 | + * @param transformationReproducer |
| 150 | + * the reproducer whose transformed query is being reduced |
| 151 | + * @param newGlobalState |
| 152 | + * the state the candidate is evaluated against |
| 153 | + * @param candidateSites |
| 154 | + * the transformation sites to keep applied |
| 155 | + * |
| 156 | + * @return {@code true} if the bug still triggers with the candidate sites |
| 157 | + */ |
| 158 | + private boolean bugStillTriggersWith(TransformationReproducer<G> transformationReproducer, G newGlobalState, |
| 159 | + List<Integer> candidateSites) { |
| 160 | + transformationReproducer.setEnabledTransformationSites(new HashSet<>(candidateSites)); |
| 161 | + try (C con2 = provider.createDatabase(newGlobalState)) { |
| 162 | + newGlobalState.setConnection(con2); |
| 163 | + // discard the setup statements createDatabase just logged into the state |
| 164 | + newGlobalState.getState().setStatements(new ArrayList<>(statements)); |
| 165 | + for (Query<C> s : statements) { |
| 166 | + try { |
| 167 | + s.execute(newGlobalState); |
| 168 | + } catch (Throwable ignoredException) { |
| 169 | + // ignore |
| 170 | + } |
| 171 | + } |
| 172 | + try { |
| 173 | + return transformationReproducer.bugStillTriggers(newGlobalState); |
| 174 | + } catch (Throwable ignoredException) { |
| 175 | + // fall through: this candidate no longer triggers the bug |
| 176 | + } |
| 177 | + } catch (Exception e) { |
| 178 | + e.printStackTrace(); |
| 179 | + } |
| 180 | + return false; |
| 181 | + } |
| 182 | +} |
0 commit comments