Skip to content

Commit

Permalink
New idiom to wrap checked exceptions to GuidanceException(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanpadhye committed Apr 6, 2020
1 parent c6a3176 commit 494319f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
16 changes: 4 additions & 12 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 @@ -708,11 +708,8 @@ public void handleResult(Result result, Throwable error) throws GuidanceExceptio
nonZeroAfter);

// Save input to queue and to disk
try {
saveCurrentInput(responsibilities, why);
} catch (IOException e) {
throw new GuidanceException(e);
}
final String reason = why;
GuidanceException.wrap(() -> saveCurrentInput(responsibilities, reason));

}
} else if (result == Result.FAILURE || result == Result.TIMEOUT) {
Expand All @@ -734,30 +731,25 @@ public void handleResult(Result result, Throwable error) throws GuidanceExceptio
assert(currentInput.size() > 0) : String.format("Empty input: %s", currentInput.desc);

// Save crash to disk
try {
int crashIdx = uniqueFailures.size()-1;
String saveFileName = String.format("id_%06d", crashIdx);
File saveFile = new File(savedFailuresDirectory, saveFileName);
writeCurrentInputToFile(saveFile);
GuidanceException.wrap(() -> writeCurrentInputToFile(saveFile));
infoLog("%s","Found crash: " + error.getClass() + " - " + (msg != null ? msg : ""));
String how = currentInput.desc;
String why = result == Result.FAILURE ? "+crash" : "+hang";
infoLog("Saved - %s %s %s", saveFile.getPath(), how, why);

if (EXACT_CRASH_PATH != null && !EXACT_CRASH_PATH.equals("")) {
File exactCrashFile = new File(EXACT_CRASH_PATH);
writeCurrentInputToFile(exactCrashFile);
GuidanceException.wrap(() -> writeCurrentInputToFile(exactCrashFile));
}

// libFuzzerCompat stats are only displayed when they hit new coverage or crashes
if (console != null && LIBFUZZER_COMPAT_OUTPUT) {
displayStats();
}

} catch (IOException e) {
throw new GuidanceException(e);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
package edu.berkeley.cs.jqf.fuzz.guidance;

import edu.berkeley.cs.jqf.fuzz.util.ThrowingRunnable;

public class GuidanceException extends RuntimeException {

public GuidanceException(String msg) {
Expand All @@ -42,5 +44,11 @@ public GuidanceException(String msg, Throwable e) {
super(msg, e);
}


public static void wrap(ThrowingRunnable task) {
try {
task.run();
} catch (Exception e) {
throw new GuidanceException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, The Regents of the University of California
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package edu.berkeley.cs.jqf.fuzz.util;

/**
* A version of {@link Runnable} that throws arbitrary, possibly checked, exceptions.
*
* Useful for wrapping checked exceptions with unchecked exceptions, such as during
* guided fuzzing.
*
* @see {@link edu.berkeley.cs.jqf.fuzz.guidance.GuidanceException#wrap(ThrowingRunnable)}
*/
@FunctionalInterface
public interface ThrowingRunnable {
void run() throws Exception;
}

0 comments on commit 494319f

Please sign in to comment.