Skip to content

Commit

Permalink
First pass at hacking something out that algins inputs, and can tell …
Browse files Browse the repository at this point in the history
…a string from an int
  • Loading branch information
jon-bell committed Aug 5, 2024
1 parent 4db9856 commit b6ee18e
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
import edu.berkeley.cs.jqf.fuzz.junit.quickcheck.FastSourceOfRandomness;

public class DictionaryBackedStringGenerator extends Generator<String> {

Expand Down Expand Up @@ -66,15 +67,16 @@ public DictionaryBackedStringGenerator(String source, Generator<String> fallback

@Override
public String generate(SourceOfRandomness random, GenerationStatus status) {
if (true) {
int choice = random.nextInt(dictionary.size());
return dictionary.get(choice);
} else {
if (fallback == null) {
fallback = gen().type(String.class);
}
return fallback.generate(random, status);
}
// if (true) {
// int choice = random.nextInt(dictionary.size());
// return dictionary.get(choice);
// } else {
// if (fallback == null) {
// fallback = gen().type(String.class);
// }
// return fallback.generate(random, status);
// }
return ((FastSourceOfRandomness) random).nextString(dictionary);
}

}
96 changes: 50 additions & 46 deletions fuzz/src/main/java/edu/berkeley/cs/jqf/fuzz/ei/ZestGuidance.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public class ZestGuidance implements Guidance {
*/
protected int numSavedInputs = 0;

protected int numSavedInputsWithMisalignments = 0;
protected int numMisalignments = 0;
protected int numSavedAlignedInputs = 0;
protected int numAlignmentsInSavedInputs = 0;

/** Coverage statistics for a single run. */
protected ICoverage runCoverage = CoverageFactory.newInstance();

Expand Down Expand Up @@ -238,7 +243,7 @@ public class ZestGuidance implements Guidance {
public static final int MAX_INPUT_SIZE = Integer.getInteger("jqf.ei.MAX_INPUT_SIZE", 10240);

/** Whether to generate EOFs when we run out of bytes in the input, instead of randomly generating new bytes. **/
protected static final boolean GENERATE_EOF_WHEN_OUT = Boolean.getBoolean("jqf.ei.GENERATE_EOF_WHEN_OUT");
public static final boolean GENERATE_EOF_WHEN_OUT = Boolean.getBoolean("jqf.ei.GENERATE_EOF_WHEN_OUT");

/** Baseline number of mutated children to produce from a given parent input. */
protected final int NUM_CHILDREN_BASELINE = 50;
Expand Down Expand Up @@ -418,7 +423,7 @@ private void prepareOutputDirectory() throws IOException {

protected String getStatNames() {
return "# unix_time, cycles_done, cur_path, paths_total, pending_total, " +
"pending_favs, map_size, unique_crashes, unique_hangs, max_depth, execs_per_sec, valid_inputs, invalid_inputs, valid_cov, all_covered_probes, valid_covered_probes";
"pending_favs, map_size, unique_crashes, unique_hangs, max_depth, execs_per_sec, valid_inputs, invalid_inputs, valid_cov, all_covered_probes, valid_covered_probes,numSavedInputsWithMisalignments, numMisalignments, numSavedAlignedInputs, numAlignmentsInSavedInputs";
}

/* Writes a line of text to a given log file. */
Expand Down Expand Up @@ -521,17 +526,19 @@ protected void displayStats(boolean force) {
console.printf("Cycles completed: %d\n", cyclesCompleted);
console.printf("Unique failures: %,d\n", uniqueFailures.size());
console.printf("Queue size: %,d (%,d favored last cycle)\n", savedInputs.size(), numFavoredLastCycle);
console.printf("Misaligned inputs: %d (avg %d/input)\n", numSavedInputsWithMisalignments, numSavedInputsWithMisalignments > 0 ? numMisalignments / numSavedInputsWithMisalignments: 0);
console.printf("Realigned inputs: %d (avg %d/input)\n", numSavedAlignedInputs, numSavedAlignedInputs > 0 ? numAlignmentsInSavedInputs / numSavedAlignedInputs: 0);
console.printf("Current parent input: %s\n", currentParentInputDesc);
console.printf("Execution speed: %,d/sec now | %,d/sec overall\n", intervalExecsPerSec, execsPerSec);
console.printf("Total coverage: %,d branches (%.2f%% of map)\n", nonZeroCount, nonZeroFraction);
console.printf("Valid coverage: %,d branches (%.2f%% of map)\n", nonZeroValidCount, nonZeroValidFraction);
}
}

String plotData = String.format("%d, %d, %d, %d, %d, %d, %.2f%%, %d, %d, %d, %.2f, %d, %d, %.2f%%, %d, %d",
String plotData = String.format("%d, %d, %d, %d, %d, %d, %.2f%%, %d, %d, %d, %.2f, %d, %d, %.2f%%, %d, %d, %d, %d, %d, %d",
TimeUnit.MILLISECONDS.toSeconds(now.getTime()), cyclesCompleted, currentParentInputIdx,
numSavedInputs, 0, 0, nonZeroFraction, uniqueFailures.size(), 0, 0, intervalExecsPerSecDouble,
numValid, numTrials-numValid, nonZeroValidFraction, nonZeroCount, nonZeroValidCount);
numValid, numTrials-numValid, nonZeroValidFraction, nonZeroCount, nonZeroValidCount, numSavedInputsWithMisalignments, numMisalignments, numSavedAlignedInputs, numAlignmentsInSavedInputs);
appendLineToFile(statsFile, plotData);
}

Expand Down Expand Up @@ -752,6 +759,18 @@ public void handleResult(Result result, Throwable error) throws GuidanceExceptio
displayStats(false);
}

if(currentInput instanceof LinearInput){
LinearInput linearInput = (LinearInput) currentInput;
if(linearInput.misAlignments > 0){
numSavedInputsWithMisalignments++;
numMisalignments += linearInput.misAlignments;
}
if(linearInput.numAlignments > 0){
numSavedAlignedInputs++;
numAlignmentsInSavedInputs += linearInput.numAlignments;
}
}

infoLog("Saving new input (at run %d): " +
"input #%d " +
"of size %d; " +
Expand Down Expand Up @@ -1196,6 +1215,10 @@ public static class LinearInput extends Input<Integer> {
/** The number of bytes requested so far */
protected int requested = 0;

/** For stats **/
public int numAlignments;
public int misAlignments;

public LinearInput() {
super();
this.values = new ArrayList<>();
Expand All @@ -1207,45 +1230,7 @@ public LinearInput(LinearInput other) {
}

public TypedGeneratedValue getOrGenerateFresh(Integer key, TypedGeneratedValue.Type desired, Random random) {
// Otherwise, make sure we are requesting just beyond the end-of-list
// assert (key == values.size());
if (key != requested) {
throw new IllegalStateException(String.format("Bytes from linear input out of order. " +
"Size = %d, Key = %d", values.size(), key));
}

// Don't generate over the limit
if (requested >= MAX_INPUT_SIZE) {
throw new IllegalStateException(new EOFException("Input size limit exceeded"));
}

// If it exists in the list, return it
if (key < values.size()) {
requested++;
// infoLog("Returning old byte at key=%d, total requested=%d", key, requested);
TypedGeneratedValue ret = values.get(key);
// Check if the type is correct
if (ret.type == desired) {
return ret;
} else{
// If not, generate a new one and update the value in the list
TypedGeneratedValue newVal = TypedGeneratedValue.generate(desired, random);
values.set(key, newVal);
return newVal;
}
}

// Handle end of stream
if (GENERATE_EOF_WHEN_OUT) {
throw new IllegalStateException(new EOFException("End of input stream"));
} else {
// Just generate a random input
TypedGeneratedValue val = TypedGeneratedValue.generate(desired, random);
values.add(val);
requested++;
// infoLog("Generating fresh byte at key=%d, total requested=%d", key, requested);
return val;
}
throw new UnsupportedOperationException("This really seems like it should be the responsibility of the input stream, not the input...");
}

@Override
Expand Down Expand Up @@ -1281,15 +1266,15 @@ public Input fuzz(Random random) {
int numMutations = sampleGeometric(random, Math.max(this.values.size() * 0.1, MEAN_MUTATION_COUNT));
newInput.desc += ",havoc:"+numMutations;

boolean setToZero = random.nextDouble() < 0.1; // one out of 10 times
// boolean setToZero = random.nextDouble() < 0.1; // one out of 10 times

for (int mutation = 1; mutation <= numMutations; mutation++) {

// Select a random offset and size
int offset = random.nextInt(newInput.values.size());
// desc += String.format(":%d@%d", mutationSize, idx);
TypedGeneratedValue.Type desired = newInput.values.get(offset).type;
newInput.values.set(offset, TypedGeneratedValue.generate(desired, random));
TypedGeneratedValue existing = newInput.values.get(offset);
newInput.values.set(offset, TypedGeneratedValue.generate(existing.type, random, existing instanceof TypedGeneratedValue.StringValue ? ((TypedGeneratedValue.StringValue) existing).dictionary : null));
}

return newInput;
Expand All @@ -1306,6 +1291,25 @@ public void writeTo(DataOutputStream out) throws IOException{
value.writeTo(out);
}
}

public TypedGeneratedValue get(int idx) {
if(idx > requested){
requested = idx;
}
return values.get(idx);
}

public void insert(int insertAt, TypedGeneratedValue val) {
values.add(insertAt, val);

}

public void add(TypedGeneratedValue val) {
values.add(val);
if(values.size() > requested){
requested = values.size();
}
}
}

public static class SeedInput extends LinearInput {
Expand Down
Loading

0 comments on commit b6ee18e

Please sign in to comment.